Browse Source

Added random offsets to noise generator

James Peret 1 year ago
parent
commit
9de11d6106
5 changed files with 28 additions and 9 deletions
  1. 6 0
      ChangeLog.md
  2. 3 1
      Readme.md
  3. 9 0
      Runtime/NoiseCombinator.cs
  4. 9 7
      Runtime/NoiseGenerator.cs
  5. 1 1
      package.json

+ 6 - 0
ChangeLog.md

@@ -0,0 +1,6 @@
+# Change Log
+
+### v0.0.2
+
+- Added randomOffset to Noise Generators
+- Added radom seed to noise group to generate randomOffsets

+ 3 - 1
Readme.md

@@ -1 +1,3 @@
-# Noise Utilities v0.0.1
+# Noise Utilities v0.0.2
+
+[Change Log](ChangeLog.md)

+ 9 - 0
Runtime/NoiseCombinator.cs

@@ -65,5 +65,14 @@ namespace KairoEngine.NoiseUtilities
             for (int i = 0; i < generators.Count; i++) generators[i].OnChangeDelegate = GenerateTexture;
             GenerateTexture();
         }
+
+        public void SetSeed(int seed)
+        {
+            UnityEngine.Random.InitState(seed);
+            for (int i = 0; i < generators.Count; i++) 
+            {
+                generators[i].randomOffset = new Vector2(UnityEngine.Random.Range(-0.99f, 0.99f), UnityEngine.Random.Range(-0.99f, 0.99f));
+            }
+        }
     }
 }

+ 9 - 7
Runtime/NoiseGenerator.cs

@@ -22,6 +22,7 @@ namespace KairoEngine.NoiseUtilities
         [HorizontalGroup("Slpit"), VerticalGroup("Slpit/Config"), LabelWidth(80), Range(0, 1f), OnValueChanged("OnChange")] public float opacity = 1f;
         [HorizontalGroup("Slpit"), VerticalGroup("Slpit/Config"), LabelWidth(80), OnValueChanged("OnChange")] public NoiseBlendingModes blending = NoiseBlendingModes.Blend;
         [HorizontalGroup("Slpit"), VerticalGroup("Slpit/Config"), LabelWidth(80), HideInInspector] public Vector2Int size = new Vector2Int(128, 128);
+        [HideInInspector] public Vector2 randomOffset = new Vector2();
 
         [HideInInspector] public System.Action OnChangeDelegate;
 
@@ -36,32 +37,33 @@ namespace KairoEngine.NoiseUtilities
 
         public float SamplePoint(float x, float y)
         {
-            float xCoord = (x * scale) + offset.x;
-            float yCoord = (y * scale) + offset.y;
+            float xCoord = (x * scale) + offset.x + randomOffset.x;
+            float yCoord = (y * scale) + offset.y + randomOffset.y;
             return Mathf.PerlinNoise(xCoord, yCoord);
         }
 
         public float SamplePixel(int x, int y)
         {
-            float xCoord = (((float)x / size.x) * scale) + offset.x;
-            float yCoord = (((float)y / size.y) * scale) + offset.y;
+            float xCoord = (((float)x / size.x) * scale) + offset.x + randomOffset.x;
+            float yCoord = (((float)y / size.y) * scale) + offset.y + randomOffset.y;
             return Mathf.PerlinNoise(xCoord, yCoord);
         }
 
         //[HorizontalGroup("Slpit"), VerticalGroup("Slpit/Preview"), Button("Preview")]
         public void GenerateTexture()
         {
-            texture = new Texture2D(size.x, size.y);
+            var newTexture = new Texture2D(size.x, size.y);
             for (int x = 0; x < size.x; x++)
             {
                 for (int y = 0; y < size.y; y++)
                 {
                     float sample = SamplePixel(x, y);
                     Color color = new Color(sample, sample, sample);
-                    texture.SetPixel(x, y, color);
+                    newTexture.SetPixel(x, y, color);
                 }
             }
-            texture.Apply();
+            newTexture.Apply();
+            texture = newTexture;
         }
 
         private void OnChange()

+ 1 - 1
package.json

@@ -1,7 +1,7 @@
 {
     "name": "at.kairoscope.kairoengine.noise-utilities",
     "displayName" : "KairoEngine Noise Utilities",
-    "version": "0.0.1",
+    "version": "0.0.2",
     "unity": "2020.3",
     "description": "Noise utilities for KairoEngine",
     "repository": {