12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- 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 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;
- float yCoord = (y * scale) + offset.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;
- return Mathf.PerlinNoise(xCoord, yCoord);
- }
- //[HorizontalGroup("Slpit"), VerticalGroup("Slpit/Preview"), Button("Preview")]
- public void GenerateTexture()
- {
- texture = 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);
- }
- }
- texture.Apply();
- }
- private void OnChange()
- {
- GenerateTexture();
- if(OnChangeDelegate != null) OnChangeDelegate();
- }
- }
- }
|