diff options
author | chai <chaifix@163.com> | 2022-06-28 09:40:37 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2022-06-28 09:40:37 +0800 |
commit | 49b25e755b70ec412feaaf0b898d6f7e09d2bea6 (patch) | |
tree | 3c5f4260f30d1c2d7196db93153700d7ddec3157 /Other/NodeEditorExamples/Assets/Examples/LibNoiseEditor/Nodes/OutputTexture2D.cs | |
parent | c92269331692feca2c276649f6c4ee8911f1f859 (diff) |
+node example
Diffstat (limited to 'Other/NodeEditorExamples/Assets/Examples/LibNoiseEditor/Nodes/OutputTexture2D.cs')
-rw-r--r-- | Other/NodeEditorExamples/Assets/Examples/LibNoiseEditor/Nodes/OutputTexture2D.cs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/Other/NodeEditorExamples/Assets/Examples/LibNoiseEditor/Nodes/OutputTexture2D.cs b/Other/NodeEditorExamples/Assets/Examples/LibNoiseEditor/Nodes/OutputTexture2D.cs new file mode 100644 index 00000000..c20a99d8 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/Examples/LibNoiseEditor/Nodes/OutputTexture2D.cs @@ -0,0 +1,70 @@ + +using UnityEngine; +using UnityEditor; +using UNEB; + +public class OutputTexture2D : Node +{ + private Texture2D texPreview; + + NodeInput inputNoise; + + private int _texRes = 100; + public int Resolution + { + get { return _texRes; } + set { _texRes = Mathf.Clamp(value, 10, 300); } + } + + public override void Init() + { + inputNoise = AddInput(); + inputNoise.name = "Input Noise"; + + texPreview = new Texture2D(200, 200); + + FitKnobs(); + bodyRect.height += 245; + bodyRect.width = 210f; + } + + public override void OnBodyGUI() + { + EditorGUI.BeginChangeCheck(); + Resolution = EditorGUILayout.IntField("Resolution", Resolution); + + GUILayout.Box(texPreview, GUILayout.Width(texPreview.width), GUILayout.Height(texPreview.height)); + + if (GUILayout.Button("Update")) { + UpdateTexture(); + } + + if (EditorGUI.EndChangeCheck()) { + UpdateTexture(); + } + } + + public void UpdateTexture() + { + if (!inputNoise.HasOutputConnected()) { + return; + } + + var noise = inputNoise.Outputs[0].GetValue<LibNoise.Generator.Perlin>(); + + for (int x = 0; x < texPreview.width; ++x) { + for (int y = 0; y < texPreview.height; ++y) { + + var point = new Vector3(x, y, 0f) / _texRes; + float value = (float)noise.GetValue(point); + + value = Mathf.Clamp01((value + 1) / 2f); + Color color = Color.HSVToRGB(value, 1f, 1f); + + texPreview.SetPixel(x, y, color); + } + } + + texPreview.Apply(); + } +} |