summaryrefslogtreecommitdiff
path: root/Other/NodeEditorExamples/Assets/Examples/LibNoiseEditor/Nodes/GeneratorNodes/PerlinNode.cs
blob: 1ffac177b1b2d9435489e96afdbb180265a93a7b (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
71
72
73

using System.Collections.Generic;

using UnityEngine;
using UnityEditor;

using UNEB;
using LibNoise.Generator;

public class PerlinNode : Node
{
    private Perlin _noise = new Perlin();

    public override void Init() { 
        var noiseIn = AddInput();
        noiseIn.name = "Input";

        var mask = AddInput();
        mask.name = "Mask";

        var noiseOut = AddOutput();
        noiseOut.name = "Output";
        noiseOut.getValue = () => { return _noise; };

        FitKnobs();

        bodyRect.height += 95f;
        bodyRect.width = 150f;
    }

    public override void OnBodyGUI()
    {
        EditorGUI.BeginChangeCheck();

        _noise.Seed = EditorGUILayout.IntField("Seed", _noise.Seed);
        _noise.OctaveCount = EditorGUILayout.IntField("Octaves", _noise.OctaveCount);
        _noise.Persistence = EditorGUILayout.DoubleField("Persistence", _noise.Persistence);
        _noise.Frequency = EditorGUILayout.DoubleField("Frequency", _noise.Frequency);
        _noise.Lacunarity = EditorGUILayout.DoubleField("Lacunarity", _noise.Lacunarity);

        if (EditorGUI.EndChangeCheck()) {
            updateOutputNodes();
        }
    }

    // Uses a simple DFS traversal to find the connected outputs.
    // Assumes a tree-like structure following output to input.
    // Does not handle cycles.
    private void updateOutputNodes()
    {
        // Temp solution.
        var dfs = new Stack<Node>();

        dfs.Push(this);

        while (dfs.Count != 0) {

            var node = dfs.Pop();

            // Search neighbors
            foreach (var output in node.Outputs) {
                foreach (var input in output.Inputs) {
                    dfs.Push(input.ParentNode);
                }
            }

            var outputNode = node as OutputTexture2D;
            if (outputNode != null) {
                outputNode.UpdateTexture();
            }
        }
    }
}