summaryrefslogtreecommitdiff
path: root/Other/NodeEditorExamples/Assets/Examples/LibNoiseEditor/Nodes/OutputTexture2D.cs
blob: c20a99d8f290bb5c0e47e3c96846e6f99248a147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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();
    }
}