Ejemplo n.º 1
0
        void Start()
        {
            m_msm         = GetComponent <PhysXMassSpringModel>();
            m_mesh        = GetComponent <MeshFilter>().mesh;
            m_collider    = GetComponent <MeshCollider>();
            m_vertices    = m_mesh.vertices;
            m_normals     = m_mesh.normals;
            this.mappings = new int[m_mesh.vertexCount];
            for (int i = 0; i < m_mesh.vertexCount; i++)
            {
                Vector3 v            = m_vertices[i];
                bool    mappingFound = false;

                float minDistance = 100000.0f;
                int   minId       = 0;

                //     for (int j = 0; j < m_rigidBodies.Length; j++)
                for (int j = 0; j < m_msm.m_rigidBodiesCount; j++)
                {
                    float dist = Vector3.Distance(v, transform.InverseTransformPoint(m_msm.m_rigidBodies[j].position));
                    if (dist < minDistance)
                    {
                        minDistance = dist;
                        minId       = j;
                    }
                }

                if (minDistance < this.m_maxSearchDistance)
                {
                    this.mappings[i] = minId;
                    mappingFound     = true;
                }

                if (!mappingFound)
                {
                    Debug.Log("MappingMissing: " + i);
                }
            }
        }
Ejemplo n.º 2
0
        void Start()
        {
            m_msm = GetComponent<PhysXMassSpringModel>();
            m_mesh = GetComponent<MeshFilter>().mesh;
            m_collider = GetComponent<MeshCollider>();
            m_vertices = m_mesh.vertices;
            m_normals = m_mesh.normals;
            this.mappings = new int[m_mesh.vertexCount];
            for (int i = 0; i < m_mesh.vertexCount; i++)
            {
                Vector3 v = m_vertices[i];
                bool mappingFound = false;

                float minDistance = 100000.0f;
                int minId = 0;

                //     for (int j = 0; j < m_rigidBodies.Length; j++)
                for (int j = 0; j < m_msm.m_rigidBodiesCount; j++)
                {
                    float dist = Vector3.Distance(v, transform.InverseTransformPoint(m_msm.m_rigidBodies[j].position));
                    if (dist < minDistance)
                    {
                        minDistance = dist;
                        minId = j;
                    }
                }

                if (minDistance < this.m_maxSearchDistance)
                {
                    this.mappings[i] = minId;
                    mappingFound = true;
                }

                if (!mappingFound)
                    Debug.Log("MappingMissing: " + i);

            }
        }
Ejemplo n.º 3
0
        private void Generate()
        {
            Vector3[] vertices    = inputMesh.vertices;
            int       vertexCount = inputMesh.vertexCount;

            int[] triangles      = inputMesh.triangles;
            int   trianglesCount = triangles.Length / 3;


            int nodesCount  = 0;
            int edgesCount  = 0;
            int facesCount  = 0;
            int tetrasCount = 0;


            int err = TetrahedralizeGetSizes2(vertices, vertexCount, triangles, trianglesCount, out nodesCount, out edgesCount, out facesCount, out tetrasCount);

            if (err != 0)
            {
                PrintError(err);
                return;
            }

            float[]    nodesOut           = new float[nodesCount * 3];
            float[]    attribsOut         = new float[nodesCount * 2];
            Link[]     edgesOut           = new Link[edgesCount];
            int[]      edgesNeighboursOut = new int[edgesCount];
            Triangle[] facesOut           = new Triangle[facesCount];
            int[]      facesNeighboursOut = new int[facesCount * 2];
            int[]      facesMarkersOut    = new int[facesCount];


            Tetrahedron[] tetrasOut = new Tetrahedron[tetrasCount];


            int err2 = Tetrahedralize2(vertices, vertexCount, triangles, trianglesCount, nodesOut, out nodesCount, edgesOut, out edgesCount, facesOut, out facesCount, tetrasOut, out tetrasCount);

            if (err2 != 0)
            {
                PrintError(err2);
                return;
            }

            GameObject go = new GameObject();

            go.name = newName;

            go.layer = colliderLayer;

            TetMesh tetMesh = go.AddComponent <TetMesh>();

            tetMesh.InitArrays(nodesCount);

            for (int i = 0; i < nodesCount; i++)
            {
                tetMesh.nodesPositions[i] = new Vector3(nodesOut[i * 3 + 0], nodesOut[i * 3 + 1], nodesOut[i * 3 + 2]);
            }

            tetMesh.edges      = new Link[edgesCount];
            tetMesh.edgesCount = edgesCount;
            for (int i = 0; i < edgesCount; i++)
            {
                tetMesh.edges[i] = edgesOut[i];
            }

            tetMesh.trianglesCount = facesCount;
            tetMesh.triangles      = new Triangle[facesCount];
            for (int i = 0; i < facesCount; i++)
            {
                tetMesh.triangles[i] = facesOut[i];
            }

            tetMesh.tetrasCount = tetrasCount;
            tetMesh.tetras      = new Tetrahedron[tetrasCount];
            for (int i = 0; i < tetrasCount; i++)
            {
                tetMesh.tetras[i] = tetrasOut[i];
            }


            //NODES GENERATING
            if (generateNodes && nodePrefab != null)
            {
                Rigidbody[] rigidbodies = new Rigidbody[tetMesh.pointsCount];


                PhysXMassSpringModel physXmsm = go.AddComponent <PhysXMassSpringModel>();
                physXmsm.m_rigidBodiesCount = nodesCount;

                physXmsm.m_rigidBodies = rigidbodies;
                physXmsm.m_stiffness   = springKs;
                physXmsm.m_damping     = springKd;

                PhysXMeshUpdater softMesh = go.AddComponent <PhysXMeshUpdater>();

                for (int i = 0; i < tetMesh.pointsCount; i++)
                {
                    GameObject nodeGO = PrefabUtility.InstantiatePrefab(nodePrefab) as GameObject;
                    nodeGO.transform.position = tetMesh.nodesPositions[i];

                    nodeGO.name             = newName + "Node_" + i;
                    nodeGO.transform.parent = tetMesh.transform;

                    rigidbodies[i] = nodeGO.GetComponent <Rigidbody>();
                }

                if (generateSpringJoints)
                {
                    SpringJoint[] springJoints = new SpringJoint[tetMesh.edgesCount];

                    for (int i = 0; i < tetMesh.edgesCount; i++)
                    {
                        Link link = tetMesh.edges[i];

                        int idA = link.idA;
                        int idB = link.idB;

                        Rigidbody rbA = rigidbodies[idA].GetComponent <Rigidbody>();
                        Rigidbody rbB = rigidbodies[idB].GetComponent <Rigidbody>();

                        SpringJoint sj = rbA.gameObject.AddComponent <SpringJoint>();
                        sj.connectedBody = rbB;
                        sj.spring        = springKs;
                        sj.damper        = springKd;

                        springJoints[i] = sj;
                    }
                    physXmsm.m_springsCount = edgesCount;
                    physXmsm.m_springJoints = springJoints;
                }
            }

            //MESH GENERATING
            MeshFilter   meshFilter   = go.AddComponent <MeshFilter>();
            MeshRenderer meshRenderer = go.AddComponent <MeshRenderer>();

            Material mat = new Material(Shader.Find("Diffuse"));

            mat.name = this.newName + "Mat";
            meshRenderer.material = mat;

            meshFilter.sharedMesh = this.inputMesh;
            meshFilter.sharedMesh.MarkDynamic();

            if (addMeshCollider)
            {
                MeshCollider mc = go.AddComponent <MeshCollider>();
                mc.sharedMesh = this.inputMesh;
            }

            //   if (deletePrevious && previousGameObject != null)
            //       DestroyImmediate(previousGameObject, false);


            previousGameObject = go;
            Selection.objects  = new Object[] { go };
            Debug.Log("TetGen: " + nodesCount + " nodes, " + edgesCount + " links, " + facesCount + " faces, " + tetrasCount + " tetras.");

            this.Close();
        }