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); }
protected override bool ProcessNode(CommandBuffer cmd) { if (!base.ProcessNode(cmd) || input == null) { return(false); } #if UNITY_2021_2_OR_NEWER cmd.SetBufferCounterValue(vertices, 0); #else cmd.SetComputeBufferCounterValue(vertices, 0); #endif // TODO: non pot texture3Ds cmd.SetComputeVectorParam(computeShader, "_VolumeSize", new Vector4(input.width, input.height, TextureUtils.GetSliceCount(input))); cmd.SetComputeFloatParam(computeShader, "_VoxelResolution", 1); cmd.SetComputeFloatParam(computeShader, "_Threshold", threshold); cmd.SetComputeTextureParam(computeShader, marchingCubes, "_VolumeTexture", input); cmd.SetComputeBufferParam(computeShader, marchingCubes, "_Vertices", vertices); cmd.SetComputeBufferParam(computeShader, marchingCubes, "_Normals", normals); cmd.SetComputeBufferParam(computeShader, marchingCubes, "_Triangles", triangles); DispatchCompute(cmd, marchingCubes, input.width, input.height, TextureUtils.GetSliceCount(input)); MixtureGraphProcessor.AddGPUAndCPUBarrier(cmd); ComputeBuffer.CopyCount(vertices, counterReadback, 0); int[] count = new int[1]; counterReadback.GetData(count); int vertexCount = count[0] * 3; // Readback all buffers Vector3[] vBuffer = new Vector3[vertexCount]; vertices.GetData(vBuffer, 0, 0, vertexCount); int[] iBuffer = new int[vertexCount]; triangles.GetData(iBuffer, 0, 0, vertexCount); var mesh = new Mesh { indexFormat = IndexFormat.UInt32 }; mesh.vertices = vBuffer; mesh.indexFormat = IndexFormat.UInt32; // mesh.triangles = iBuffer; mesh.SetIndices(iBuffer, MeshTopology.Triangles, 0); mesh.RecalculateBounds(); mesh.RecalculateNormals(); mesh.UploadMeshData(false); output = new MixtureMesh { mesh = mesh }; return(true); }
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); }
protected override bool ProcessNode(CommandBuffer cmd) { if (loopStart == null) { Debug.Log("For End node is not connected to a start"); return(false); } if (mode == AggregationMode.FeedbackToStartNode) { FeedbackInputToStartNode(cmd); } // So right now we need to flush the command buffer because we are changing // the inputs to some potential CRT nodes in the loop, which result in a change // of material value and because this change is not registered within the command // buffer, it would cause issues to just change values without flushing the cmd. // We need to consider either material property blocks or a material pool in the loop // for these nodes. MixtureGraphProcessor.AddGPUAndCPUBarrier(cmd); return(true); }