public override bool Calculate()
        {
            LibNoise.ModuleBase noiseModule = null;

            var mnode = !Inputs[0].IsValueNull ? Inputs[0].GetValue <NodeModuleBase>() : null;

            if (mnode != null && mnode.DependenciesMet)
            {
                noiseModule = mnode.Module;
            }

            if (noiseModule != null)
            {
                if (_previewTexture == null)
                {
                    _previewTexture = NodeModuleBase.GenerateDefaultTexture(_previewWidth, _previewHeight);
                }

                ++_nextSequenceId;
                int asyncId = _nextSequenceId;

                AsyncWorkQueue.QueueBackgroundWork(GetType().Name, () =>
                {
                    // expensive stuff
                    LibNoise.Noise2D noise2d = new LibNoise.Noise2D(_previewWidth, _previewHeight, noiseModule);
                    noise2d.GeneratePlanar(PreviewLeft, PreviewRight, PreviewTop, PreviewBottom, PreviewSeamless);

                    // when we're done, queue the texture creation
                    AsyncWorkQueue.QueueMainThreadWork(() =>
                    {
                        if (asyncId >= _textureSequenceId)
                        {
                            _textureSequenceId = asyncId;
                            _previewTexture    = NodeModuleBase.CreateNoiseTexture(noise2d, Colorization);

                            NodeEditorFramework.NodeEditor.RepaintClients();
                        }
                    });
                });
            }
            else
            {
                _previewTexture = NodeModuleBase.GenerateDefaultTexture(_previewWidth, _previewHeight);
            }
            NodeEditorFramework.NodeEditor.RepaintClients();

            return(true);
        }
Example #2
0
        public override bool Calculate()
        {
            if (DependenciesMet)
            {
                PreGenerate();

                if (_previewTexture == null)
                {
                    _previewTexture = GenerateDefaultTexture(_previewWidth, _previewHeight);
                }

                ++_nextSequenceId;
                int asyncId = _nextSequenceId;

                AsyncWorkQueue.QueueBackgroundWork(GetType().Name, () =>
                {
                    // expensive stuff
                    LibNoise.Noise2D noise2d = new LibNoise.Noise2D(_previewWidth, _previewHeight, Module);
                    noise2d.GeneratePlanar(PreviewLeft, PreviewRight, PreviewTop, PreviewBottom, PreviewSeamless);

                    // when we're done, queue the texture creation
                    AsyncWorkQueue.QueueMainThreadWork(() =>
                    {
                        if (asyncId >= _textureSequenceId)
                        {
                            _textureSequenceId = asyncId;
                            _previewTexture    = CreateNoiseTexture(noise2d, Colorization);

                            NodeEditorFramework.NodeEditor.RepaintClients();
                        }
                    });
                });
            }
            else
            {
                _previewTexture = GenerateDefaultTexture(_previewWidth, _previewHeight);
            }
            NodeEditorFramework.NodeEditor.RepaintClients();

            for (int i = 0; i < Outputs.Count; ++i)
            {
                Outputs[i].SetValue <NodeModuleBase>(this);
            }
            return(true);
        }
        public override bool Calculate()
        {
            LibNoise.ModuleBase noiseModule = null;

            var mnode = !Inputs[0].IsValueNull ? Inputs[0].GetValue <NodeModuleBase>() : null;

            if (mnode != null && mnode.DependenciesMet)
            {
                noiseModule = mnode.Module;
            }

            if (noiseModule != null)
            {
                if (_previewTexture == null)
                {
                    _previewTexture = NodeModuleBase.GenerateDefaultTexture(_previewWidth, _previewHeight);
                }

                ++_nextSequenceId;
                int asyncId = _nextSequenceId;

                AsyncWorkQueue.QueueBackgroundWork(GetType().Name, () =>
                {
                    LibNoise.Noise2D noise2d = new LibNoise.Noise2D(_previewWidth, _previewHeight, noiseModule);
                    noise2d.GeneratePlanar(PreviewLeft, PreviewRight, PreviewTop, PreviewBottom, PreviewSeamless);

                    float[,] noiseData = noise2d.GetData();

                    bool[,] cellular = new bool[noiseData.GetLength(0), noiseData.GetLength(1)];

                    for (var x = 0; x < noiseData.GetLength(0); x++)
                    {
                        for (var y = 0; y < noiseData.GetLength(1); y++)
                        {
                            cellular[x, y] = noiseData[x, y] > Cutoff;
                        }
                    }

                    bool[,] dstMap = new bool[noiseData.GetLength(0), noiseData.GetLength(1)];
                    for (int i = 0; i < SmoothIterations; ++i)
                    {
                        SmoothMap(cellular, dstMap);

                        // swap the array references
                        bool[,] tmp = cellular;
                        cellular    = dstMap;
                        dstMap      = tmp;
                    }

                    // when we're done, queue the texture creation
                    AsyncWorkQueue.QueueMainThreadWork(() =>
                    {
                        if (asyncId >= _textureSequenceId)
                        {
                            _textureSequenceId = asyncId;

                            var pixels = new Color[cellular.GetLength(0) * cellular.GetLength(1)];

                            _previewTexture                     = new Texture2D(cellular.GetLength(0), cellular.GetLength(1));
                            _previewTexture.wrapMode            = TextureWrapMode.Clamp;
                            _previewTexture.alphaIsTransparency = true;

                            for (var x = 0; x < noiseData.GetLength(0); x++)
                            {
                                for (var y = 0; y < noiseData.GetLength(1); y++)
                                {
                                    pixels[x + y * noiseData.GetLength(0)] = cellular[x, y] ? Color.black : Color.white;
                                }
                            }

                            _previewTexture.SetPixels(pixels);
                            _previewTexture.Apply();

                            NodeEditorFramework.NodeEditor.RepaintClients();
                        }
                    });
                });
            }
            else
            {
                _previewTexture = NodeModuleBase.GenerateDefaultTexture(_previewWidth, _previewHeight);
            }
            NodeEditorFramework.NodeEditor.RepaintClients();

            return(true);
        }