Esempio n. 1
0
    void OnEnable()
    {
        DeformedMesh = target as DeformedMesh;

        if (Delete == null)
        {
            Delete = new GUIContent((Texture2D)EditorGUIUtility.Load("Assets/ElseForty/Media/Delete.png"));
        }
        if (Visible == null)
        {
            Visible = new GUIContent((Texture2D)EditorGUIUtility.Load("Assets/ElseForty/Media/Visible.png"));
        }
        if (Unvisible == null)
        {
            Unvisible = new GUIContent((Texture2D)EditorGUIUtility.Load("Assets/ElseForty/Media/Unvisible.png"));
        }
        if (SelectionFrame == null)
        {
            SelectionFrame = new GUIContent((Texture2D)EditorGUIUtility.Load("Assets/ElseForty/Media/SelectionFrame.png"));
        }
        if (MeshDeformBanner == null)
        {
            MeshDeformBanner = new GUIContent((Texture2D)EditorGUIUtility.Load("Assets/ElseForty/Media/MeshDeformBanner.png"));
        }
        if (Link == null)
        {
            Link = new GUIContent((Texture2D)EditorGUIUtility.Load("Assets/ElseForty/Media/Link.png"));
        }
        if (Unlink == null)
        {
            Unlink = new GUIContent((Texture2D)EditorGUIUtility.Load("Assets/ElseForty/Media/Unlink.png"));
        }

        EditorApplication.update += Update;
        SceneViewUI.PivotEditDel += PivotEdit;
        SplineEditing.BranchSelectionChangedDel += BranchSelectionChanged;

        DeformedMesh.IsUpdateBase = true;
        DeformedMesh.DrawMeshOnEachBranch();
    }
Esempio n. 2
0
        public void BuildMesh()
        {
            GetDeformedMesh();
            var         modelGroup  = new Model3DGroup();
            MeshBuilder meshBuilder = new MeshBuilder(false, true);

            //List<Point3D> pointDisps = DeformedMesh.Select(p => new Point3D(p.X, p.Y, p.Z)).ToList();
            DeformedMesh.ForEach(p =>
            {
                meshBuilder.Positions.Add(p);
                meshBuilder.TextureCoordinates.Add(new System.Windows.Point());
            });
            results.T3.ForEach(t =>
            {
                meshBuilder.AddTriangle(new List <int> {
                    t[0], t[1], t[2]
                });
                meshBuilder.AddCylinder(DeformedMesh[t[0]], DeformedMesh[t[1]], 0.05, 4);
                meshBuilder.AddCylinder(DeformedMesh[t[1]], DeformedMesh[t[2]], 0.05, 4);
                meshBuilder.AddCylinder(DeformedMesh[t[0]], DeformedMesh[t[2]], 0.05, 4);
            });
            //fem.modelGeo.Beams.ForEach(b =>
            //{
            //    meshBuilder.AddCylinder(new Point3D(b.Start.X, b.Start.Y, b.Start.Z), new Point3D(b.End.X, b.End.Y, b.End.Z), 0.1, 8);
            //});
            results.L2.ForEach(l =>
            {
                var p1 = DeformedMesh[l[0]];
                var p2 = DeformedMesh[l[1]];
                meshBuilder.AddCylinder(p1, p2, 0.1, 8);
            });

            var color = Color.FromArgb(150, 200, 0, 0);
            var mesh  = new GeometryModel3D(meshBuilder.ToMesh(), MaterialHelper.CreateMaterial(color));

            mesh.BackMaterial = mesh.Material;
            modelGroup.Children.Add(mesh);
            Mesh = modelGroup;
        }
    void Start()
    {
        skin             = GetComponent <SkinnedMeshRenderer>();
        mesh             = skin.sharedMesh;
        meshForCPUOutput = Instantiate(mesh);

        deformedMesh = new DeformedMesh(mesh.vertexCount);

        adjacencyMatrix = GetCachedAdjacencyMatrix(mesh, adjacencyMatchingVertexTolerance);

        // Compute
        if (SystemInfo.supportsComputeShaders && computeShader && ductTapedShader)
        {
            verticesCB = new ComputeBuffer(mesh.vertices.Length, 3 * sizeof(float));
            normalsCB  = new ComputeBuffer(mesh.vertices.Length, 3 * sizeof(float));
            weightsCB  = new ComputeBuffer(mesh.vertices.Length, 4 * sizeof(float) + 4 * sizeof(int));
            verticesCB.SetData(mesh.vertices);
            normalsCB.SetData(mesh.normals);
            weightsCB.SetData(mesh.boneWeights);

            adjacencyCB = new ComputeBuffer(adjacencyMatrix.Length, sizeof(int));
            var adjArray = new int[adjacencyMatrix.Length];
            Buffer.BlockCopy(adjacencyMatrix, 0, adjArray, 0, adjacencyMatrix.Length * sizeof(int));
            adjacencyCB.SetData(adjArray);

            bonesCB  = new ComputeBuffer(skin.bones.Length, 16 * sizeof(float));
            deltavCB = new ComputeBuffer(mesh.vertices.Length, 3 * sizeof(float));
            deltanCB = new ComputeBuffer(mesh.vertices.Length, 3 * sizeof(float));

            outputCB[0] = new ComputeBuffer(mesh.vertices.Length, 6 * sizeof(float));
            outputCB[1] = new ComputeBuffer(mesh.vertices.Length, 6 * sizeof(float));
            outputCB[2] = new ComputeBuffer(mesh.vertices.Length, 6 * sizeof(float));

            deformKernel = computeShader.FindKernel("DeformMesh");
            computeShader.SetBuffer(deformKernel, "Vertices", verticesCB);
            computeShader.SetBuffer(deformKernel, "Normals", normalsCB);
            computeShader.SetBuffer(deformKernel, "Weights", weightsCB);
            computeShader.SetBuffer(deformKernel, "Bones", bonesCB);
            computeShader.SetInt("VertexCount", mesh.vertices.Length);

            laplacianKernel = GetSmoothKernel();
            computeShader.SetBuffer(laplacianKernel, "Adjacency", adjacencyCB);
            computeShader.SetInt("AdjacentNeighborCount", adjacencyMatrix.GetLength(1));

            uint threadGroupSizeX, threadGroupSizeY, threadGroupSizeZ;
            computeShader.GetKernelThreadGroupSizes(deformKernel, out threadGroupSizeX, out threadGroupSizeY, out threadGroupSizeZ);
            computeThreadGroupSizeX = (int)threadGroupSizeX;
            computeShader.GetKernelThreadGroupSizes(laplacianKernel, out threadGroupSizeX, out threadGroupSizeY, out threadGroupSizeZ);
            Debug.Assert(computeThreadGroupSizeX == (int)threadGroupSizeX);

            ductTapedMaterial = new Material(ductTapedShader);
            ductTapedMaterial.CopyPropertiesFromMaterial(skin.sharedMaterial);
        }
        else
        {
            useCompute = false;
        }

        UpdateDeltaVectors();

        // Experiment with blending bone weights
        BoneWeight[] bw = mesh.boneWeights;
        prefilteredBoneWeights = new float[mesh.vertexCount, skin.bones.Length];
        for (int i = 0; i < mesh.vertexCount; ++i)
        {
            prefilteredBoneWeights[i, bw[i].boneIndex0] = bw[i].weight0;
            prefilteredBoneWeights[i, bw[i].boneIndex1] = bw[i].weight1;
            prefilteredBoneWeights[i, bw[i].boneIndex2] = bw[i].weight2;
            prefilteredBoneWeights[i, bw[i].boneIndex3] = bw[i].weight3;
        }
        for (int i = 0; i < iterations; i++)
        {
            prefilteredBoneWeights = SmoothFilter.distanceWeightedLaplacianFilter(mesh.vertices, prefilteredBoneWeights, adjacencyMatrix);
        }

        var boneCount = skin.bones.Length;

        for (int i = 0; i < mesh.vertexCount; ++i)
        {
            float l = 0.0f;
            for (int b = 0; b < boneCount; ++b)
            {
                l += prefilteredBoneWeights[i, b];
            }
            for (int b = 0; b < boneCount; ++b)
            {
                prefilteredBoneWeights[i, b] += prefilteredBoneWeights[i, b] * (1.0f - l);
            }
        }
    }