123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- namespace KairoEngine.NoiseUtilities
- {
- public enum NoiseBlendingModes
- {
- Blend,
- Addictive,
- Subtractive
- }
- [System.Serializable]
- public class NoiseGenerator
- {
- [HorizontalGroup("Slpit", 80, MarginRight = 15), VerticalGroup("Slpit/Preview"), PreviewField(80, ObjectFieldAlignment.Left), HideLabel]
- public Texture2D texture;
- [HorizontalGroup("Slpit"), VerticalGroup("Slpit/Config"), LabelWidth(80), OnValueChanged("OnChange")] public float scale = 1f;
- [HorizontalGroup("Slpit"), VerticalGroup("Slpit/Config"), LabelWidth(80), OnValueChanged("OnChange")] public Vector2 offset = new Vector2();
- [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;
- public NoiseGenerator()
- {
- this.size = new Vector2Int(128, 128);;
- this.scale = 1f;
- this.offset = new Vector2();
- this.opacity = 1f;
- //GenerateTexture(); // Disabled to fix weird bug when unity starts
- }
- public float SamplePoint(float x, float 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 + 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()
- {
- 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);
- newTexture.SetPixel(x, y, color);
- }
- }
- newTexture.Apply();
- texture = newTexture;
- }
- private void OnChange()
- {
- GenerateTexture();
- if(OnChangeDelegate != null) OnChangeDelegate();
- }
- }
- }
|