From 49b25e755b70ec412feaaf0b898d6f7e09d2bea6 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 28 Jun 2022 09:40:37 +0800 Subject: +node example --- .../LibNoiseEditor/Nodes/OutputTexture2D.cs | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Other/NodeEditorExamples/Assets/Examples/LibNoiseEditor/Nodes/OutputTexture2D.cs (limited to 'Other/NodeEditorExamples/Assets/Examples/LibNoiseEditor/Nodes/OutputTexture2D.cs') 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(); + + 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(); + } +} -- cgit v1.1-26-g67d0