PerlinNoise3DGizmo.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace KairoEngine.NoiseUtilities
  6. {
  7. public class PerlinNoise3DGizmo : MonoBehaviour
  8. {
  9. public bool cubeBoundries = false;
  10. public int size = 10;
  11. public int height = 20;
  12. public int width = 20;
  13. public int depth = 20;
  14. public float frequency = 1.01f;
  15. private Vector3[] points;
  16. void Start()
  17. {
  18. InstantiatePoints();
  19. }
  20. private void FixedUpdate()
  21. {
  22. UpdateMesh();
  23. }
  24. void UpdateMesh()
  25. {
  26. if (cubeBoundries)
  27. {
  28. height = size;
  29. width = size;
  30. depth = size;
  31. }
  32. InstantiatePoints();
  33. }
  34. void InstantiatePoints()
  35. {
  36. points = new Vector3[height * width * depth];
  37. for (int z = 0, i = 0; z < depth; z++)
  38. {
  39. for (int y = 0; y < height; y++)
  40. {
  41. for (int x = 0; x < width; x++)
  42. {
  43. points[i] = new Vector3(x, y, z);
  44. i++;
  45. }
  46. }
  47. }
  48. }
  49. private void OnDrawGizmos()
  50. {
  51. if (points != null)
  52. {
  53. for (int i = 0; i < points.Length; i++)
  54. {
  55. float sample = PerlinNoise3D.get3DPerlinNoise(points[i], frequency);
  56. sample = (sample + 1f) / 2f;
  57. Gizmos.color = new Color(sample, sample, sample, 1);
  58. Gizmos.DrawSphere(points[i], 0.3f);
  59. }
  60. }
  61. }
  62. [CustomEditor(typeof(PerlinNoise3DGizmo))]
  63. public class PerlinNoise3DGizmoEditor : Editor
  64. {
  65. override public void OnInspectorGUI()
  66. {
  67. var myScript = target as PerlinNoise3DGizmo;
  68. myScript.cubeBoundries = GUILayout.Toggle(myScript.cubeBoundries, "Enable Box Size:");
  69. if (!myScript.cubeBoundries)
  70. {
  71. myScript.depth = EditorGUILayout.IntSlider("Depth:", myScript.depth, 1, 30);
  72. myScript.height = EditorGUILayout.IntSlider("Heigth:", myScript.height, 1, 30);
  73. myScript.width = EditorGUILayout.IntSlider("Width:", myScript.width, 1, 30);
  74. }
  75. else
  76. {
  77. myScript.size = EditorGUILayout.IntSlider("Box Size:", myScript.depth, 1, 30);
  78. }
  79. myScript.frequency = EditorGUILayout.FloatField("Frequency:", myScript.frequency);
  80. }
  81. }
  82. }
  83. }