示例#1
0
        protected override bool ProcessNode(CommandBuffer cmd)
        {
            if (input == null)
            {
                return(false);
            }

            if (input.dimension != TextureDimension.Tex2D)
            {
                return(false);
            }

            Color32[] colors = null;
            cache            = new Texture2D(input.width, input.height, settings.GetGraphicsFormat(graph), TextureCreationFlags.None);
            cache.filterMode = FilterMode.Point;
            if (input is RenderTexture rt)
            {
                cmd.RequestAsyncReadback(rt, (r) => {
                    colors = r.GetData <Color32>().ToArray();
                });
                cmd.WaitAllAsyncReadbackRequests();
                MixtureGraphProcessor.AddGPUAndCPUBarrier(cmd);
            }
            else
            {
                colors = (input as Texture2D).GetPixels32();
            }

            var r = new System.Random(42);

            points = new MixtureAttributeList();

            for (int i = 0; i < colors.Length; i++)
            {
                float f = (float)r.NextDouble();
                var   p = colors[i];

                float a = (float)p.a / 255.0f;
                if (f * a > density)
                {
                    colors[i] = Color.white;
                    Vector3 position = new Vector3((float)(i % input.width), 0, (Mathf.Floor(i / (float)input.width)));
                    Vector2 uv       = new Vector2(position.x / (float)input.width, position.z / (float)input.height);
                    points.Add(new MixtureAttribute {
                        { "uv", uv },
                        { "position", position },
                        { "density", p.a }
                    });
                }
                else
                {
                    colors[i] = Color.black;
                }
            }

            cache.SetPixels32(colors);
            cache.Apply();

            return(true);
        }
示例#2
0
        protected override bool ProcessNode(CommandBuffer cmd)
        {
            if (input == null || inputPoints == null)
            {
                return(false);
            }

            if (input.dimension != TextureDimension.Tex2D)
            {
                return(false);
            }

            Color32[] colors = null;
            cache            = new Texture2D(rtSettings.GetWidth(graph), rtSettings.GetHeight(graph), rtSettings.GetGraphicsFormat(graph), TextureCreationFlags.None);
            cache.filterMode = FilterMode.Point;

            if (input is RenderTexture rt)
            {
                cmd.RequestAsyncReadback(rt, (r) => {
                    colors = r.GetData <Color32>().ToArray();
                });
                cmd.WaitAllAsyncReadbackRequests();
                MixtureGraphProcessor.AddGPUAndCPUBarrier(cmd);
            }
            else
            {
                colors = (input as Texture2D).GetPixels32();
            }

            var r = new System.Random(42);

            outputPoints = new MixtureAttributeList();

            Color32[] previewColor = new Color32[cache.width * cache.height];
            foreach (var attr in inputPoints)
            {
                // Sample the input texture with points:
                if (attr.TryGetValue("uv", out var ouv) && ouv is Vector2 uv)
                {
                    int     index = (int)(uv.x * input.width + uv.y * input.height * input.width);
                    Vector4 color = new Vector4(colors[index].r / 255f, colors[index].g / 255f, colors[index].b / 255f, colors[index].a / 255f);

                    AddAttribute(attr, uv, color, ref previewColor);
                }
                outputPoints.Add(attr);
            }

            cache.SetPixels32(previewColor);
            cache.Apply();

            return(true);
        }