123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- namespace KairoEngine.NoiseUtilities
- {
- [System.Serializable]
- public class NoiseCombinator
- {
- [PreviewField(256, ObjectFieldAlignment.Center), HideLabel, PropertyOrder(0)]
- public Texture2D texture;
- [PropertyOrder(2), OnCollectionChanged("Before", "After"), OnInspectorInit("After")] public List<NoiseGenerator> generators = new List<NoiseGenerator>();
- [PropertyOrder(3), HideInInspector] public Vector2Int size = new Vector2Int(128, 128);
- public float SamplePoint(float x, float y)
- {
- float value = 0;
- for (int i = 0; i < generators.Count; i++)
- {
- switch (generators[i].blending)
- {
- case NoiseBlendingModes.Blend:
- value = BlendSubpixel(value, generators[i].SamplePoint(x, y), 1, 1f - generators[i].opacity);
- break;
- case NoiseBlendingModes.Addictive:
- value += generators[i].SamplePoint(x, y) * generators[i].opacity;
- break;
- case NoiseBlendingModes.Subtractive:
- value -= generators[i].SamplePoint(x, y) * generators[i].opacity;
- break;
- }
- value = Mathf.Clamp(value, 0, 1f);
-
- }
- return value;
- }
- //[Button("Preview"), PropertyOrder(1)]
- 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 xCoord = (((float)x / size.x));
- float yCoord = (((float)y / size.y));
- float sample = SamplePoint(xCoord, yCoord);
- Color color = new Color(sample, sample, sample);
- texture.SetPixel(x, y, color);
- }
- }
- texture.Apply();
- }
- static float BlendSubpixel(float top, float bottom, float alphaTop, float alphaBottom){
- return (top * alphaTop) + ( (bottom-1f) * (alphaBottom-alphaTop) );
- }
- public void Before() { }
- public void After()
- {
- 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));
- }
- }
- }
- }
|