コード例 #1
0
        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);
        }
コード例 #2
0
        // Random random;

        // protected override void Enable()
        // {
        //  random = new Random(4242);
        // }

        protected override bool ProcessNode(CommandBuffer cmd)
        {
            if (list == null || list.Count == 0)
            {
                return(false);
            }

            index = Mathf.Clamp(index, 0, list.Count - 1);
            elem  = list[index];

            return(true);
        }
コード例 #3
0
        protected override bool ProcessNode(CommandBuffer cmd)
        {
            if (input?.mesh == null || inputPoints == null)
            {
                return(false);
            }

            Color[] colors = new Color[input.mesh.vertices.Length];

            foreach (var p in inputPoints)
            {
                if (!p.ContainsKey("index"))
                {
                    continue;
                }

                p.TryGetValue("index", out var index);
                int i = (int)index;
                if (p.TryGetValue("position", out var position) && position is Vector3 pos)
                {
                    input.mesh.vertices[i] = pos;
                }
                if (p.TryGetValue("normal", out var normal) && normal is Vector3 n)
                {
                    input.mesh.normals[i] = n;
                }
                if (p.TryGetValue("color", out var color) && color is Color c)
                {
                    colors[i] = c;
                }
                if (p.TryGetValue("uv", out var uv) && uv is Vector2 u)
                {
                    input.mesh.uv[i] = u;
                }
            }

            input.mesh.colors = colors;
            input.mesh.UploadMeshData(false);
            input.mesh.RecalculateBounds();

            output = input.Clone();

            return(true);
        }
コード例 #4
0
ファイル: Mesh.cs プロジェクト: KiJou/Mixture
        protected override bool ProcessNode(CommandBuffer cmd)
        {
            if (mesh == null)
            {
                return(false);
            }

            output = new MixtureMesh(mesh);

            // Apply matrix to mesh
            var combine = new CombineInstance[1];

            combine[0].mesh      = output.mesh;
            combine[0].transform = Matrix4x4.TRS(pos, Quaternion.Euler(eulerAngles), scale);

            output.mesh = new Mesh {
                indexFormat = IndexFormat.UInt32
            };
            output.mesh.CombineMeshes(combine);

            return(true);
        }
コード例 #5
0
        // Functions with Attributes must be either protected or public otherwise they can't be accessed by the reflection code
        // [CustomPortBehavior(nameof(inputMeshes))]
        // public IEnumerable< PortData > ListMaterialProperties(List< SerializableEdge > edges)
        // {
        //     yield return new PortData
        //     {
        //         identifier = nameof(inputMeshes),
        //         displayName = "Input Meshes",
        //         allowMultiple = true,
        //         displayType()
        //     };
        // }

        // [CustomPortInput(nameof(inputMeshes), typeof(MixtureMesh))]
        // protected void GetMaterialInputs(List< SerializableEdge > edges)
        // {
        //     if (inputMeshes == null)
        //         inputMeshes = new List<MixtureMesh>();
        //     inputMeshes.Clear();
        //  foreach (var edge in edges)
        //     {
        //         if (edge.passThroughBuffer is MixtureMesh m)
        //             inputMeshes.Add(m);
        //     }
        // }

        protected override bool ProcessNode(CommandBuffer cmd)
        {
            if (inputMesh == null || inputMesh.mesh == null)
            {
                return(false);
            }

            output = inputMesh.Clone();

            if (inputAttrib != null)
            {
                // Try to get values from attribute in param:
                inputAttrib.TryGetValue("position", out var position);
                inputAttrib.TryGetValue("rotation", out var rotation);
                inputAttrib.TryGetValue("scale", out var scale);
                inputAttrib.TryGetValue("normal", out var normal);

                if (normal != null && normal is Vector3 n)
                {
                    rotation = Quaternion.LookRotation(n, Vector3.up) * Quaternion.Euler(90, 0, 0);
                }
                if (position == null)
                {
                    position = Vector3.zero;
                }
                if (rotation == null)
                {
                    rotation = Quaternion.identity;
                }
                if (scale == null)
                {
                    scale = Vector3.one;
                }

                // float4x4 m1 = output.localToWorld;
                // float4x4 m2 = new float4x4((quaternion)(Quaternion)rotation, (float3)(Vector3)position);
                // Vector3 s = (Vector3)scale;
                // // m2.c0.x *= s.x;
                // // m2.c1.y *= s.y;
                // // m2.c2.z *= s.z;
                // m2 *= m1;
                // output.localToWorld = transpose(m2);

                output.localToWorld *= Matrix4x4.TRS((Vector3)position, (Quaternion)rotation, (Vector3)scale);
                // output.localToWorld = MultiplyMatrix(output.localToWorld, Matrix4x4.TRS((Vector3)position, (Quaternion)rotation, (Vector3)scale));

                // Is this needed ?
                // Solid s = new Solid(output.mesh.vertices, output.mesh.triangles, output.mesh.normals);
                // s.ApplyMatrix(output.localToWorld);
            }
            else
            {
                output.localToWorld = Matrix4x4.TRS(pos, Quaternion.Euler(eulerAngles), scale);
            }

            var combine = new CombineInstance[1];

            combine[0].mesh      = output.mesh;
            combine[0].transform = output.localToWorld;

            output.mesh = new Mesh {
                indexFormat = IndexFormat.UInt32
            };
            output.localToWorld = Matrix4x4.identity;
            output.mesh.CombineMeshes(combine);

            return(true);
        }