SoftBody CreateFromIndexedMesh(Vector3Array vertexArray, IntArray triangleVertexIndexArray, bool createBendLinks) { SoftBody softBody = new SoftBody(SoftWorld.WorldInfo, vertexArray, null); Material structuralMaterial = softBody.AppendMaterial(); Material bendMaterial; if (createBendLinks) { bendMaterial = softBody.AppendMaterial(); bendMaterial.Lst = 0.7f; } else { bendMaterial = null; } structuralMaterial.Lst = 1.0f; int numVertices = vertexArray.Count; int numTriangles = triangleVertexIndexArray.Count / 3; // List of values for each link saying which triangle is associated with that link // -1 to start. Once a value is entered we know the "other" triangle // and can add a link across the link AlignedIntArray triangleForLinks = new AlignedIntArray(); triangleForLinks.Resize(numVertices * numVertices, -1); for (int triangle = 0; triangle < numTriangles; ++triangle) { int[] index = new int[] { triangleVertexIndexArray[triangle * 3], triangleVertexIndexArray[triangle * 3 + 1], triangleVertexIndexArray[triangle * 3 + 2] }; softBody.AppendFace(index[0], index[1], index[2]); // Generate the structural links directly from the triangles TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[0], index[1], index[2], structuralMaterial, createBendLinks, bendMaterial); TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[1], index[2], index[0], structuralMaterial, createBendLinks, bendMaterial); TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[2], index[0], index[1], structuralMaterial, createBendLinks, bendMaterial); } return(softBody); }
// Helper to test and add links correctly. // Records links that have already been generated static bool TestAndAddLink(AlignedIntArray trianglesForLinks, SoftBody softBody, int triangle, IntArray triangleVertexIndexArray, int numVertices, int vertex0, int vertex1, int nonLinkVertex, Material structuralMaterial, bool createBendLinks, Material bendMaterial) { if (trianglesForLinks[numVertices * vertex0 + vertex1] >= 0 && createBendLinks) { // Already have link so find other triangle and generate cross link int otherTriangle = trianglesForLinks[numVertices * vertex0 + vertex1]; int[] otherIndices = new int[] { triangleVertexIndexArray[otherTriangle * 3], triangleVertexIndexArray[otherTriangle * 3 + 1], triangleVertexIndexArray[otherTriangle * 3 + 2] }; int nodeA = 0; // Test all links of the other triangle against this link. The one that's not part of it is what we want. if (otherIndices[0] != vertex0 && otherIndices[0] != vertex1) { nodeA = otherIndices[0]; } if (otherIndices[1] != vertex0 && otherIndices[1] != vertex1) { nodeA = otherIndices[1]; } if (otherIndices[2] != vertex0 && otherIndices[2] != vertex1) { nodeA = otherIndices[2]; } softBody.AppendLink(nodeA, nonLinkVertex, bendMaterial); } else { // Don't yet have link so create it softBody.AppendLink(vertex0, vertex1, structuralMaterial); // If we added a new link, set the triangle array trianglesForLinks[numVertices * vertex0 + vertex1] = triangle; trianglesForLinks[numVertices * vertex1 + vertex0] = triangle; } return(true); }
SoftBody CreateFromIndexedMesh(Vector3Array vertexArray, IntArray triangleVertexIndexArray, bool createBendLinks) { SoftBody softBody = new SoftBody(SoftWorld.WorldInfo, vertexArray, null); Material structuralMaterial = softBody.AppendMaterial(); Material bendMaterial; if (createBendLinks) { bendMaterial = softBody.AppendMaterial(); bendMaterial.Lst = 0.7f; } else { bendMaterial = null; } structuralMaterial.Lst = 1.0f; int numVertices = vertexArray.Count; int numTriangles = triangleVertexIndexArray.Count / 3; // List of values for each link saying which triangle is associated with that link // -1 to start. Once a value is entered we know the "other" triangle // and can add a link across the link AlignedIntArray triangleForLinks = new AlignedIntArray(); triangleForLinks.Resize(numVertices * numVertices, -1); for (int triangle = 0; triangle < numTriangles; ++triangle) { int[] index = new int[] { triangleVertexIndexArray[triangle * 3], triangleVertexIndexArray[triangle * 3 + 1], triangleVertexIndexArray[triangle * 3 + 2] }; softBody.AppendFace(index[0], index[1], index[2]); // Generate the structural links directly from the triangles TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[0], index[1], index[2], structuralMaterial, createBendLinks, bendMaterial); TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[1], index[2], index[0], structuralMaterial, createBendLinks, bendMaterial); TestAndAddLink(triangleForLinks, softBody, triangle, triangleVertexIndexArray, numVertices, index[2], index[0], index[1], structuralMaterial, createBendLinks, bendMaterial); } return softBody; }
// Helper to test and add links correctly. // Records links that have already been generated static bool TestAndAddLink(AlignedIntArray trianglesForLinks, SoftBody softBody, int triangle, IntArray triangleVertexIndexArray, int numVertices, int vertex0, int vertex1, int nonLinkVertex, Material structuralMaterial, bool createBendLinks, Material bendMaterial) { if (trianglesForLinks[numVertices * vertex0 + vertex1] >= 0 && createBendLinks) { // Already have link so find other triangle and generate cross link int otherTriangle = trianglesForLinks[numVertices * vertex0 + vertex1]; int[] otherIndices = new int[] { triangleVertexIndexArray[otherTriangle * 3], triangleVertexIndexArray[otherTriangle * 3 + 1], triangleVertexIndexArray[otherTriangle * 3 + 2] }; int nodeA = 0; // Test all links of the other triangle against this link. The one that's not part of it is what we want. if (otherIndices[0] != vertex0 && otherIndices[0] != vertex1) nodeA = otherIndices[0]; if (otherIndices[1] != vertex0 && otherIndices[1] != vertex1) nodeA = otherIndices[1]; if (otherIndices[2] != vertex0 && otherIndices[2] != vertex1) nodeA = otherIndices[2]; softBody.AppendLink(nodeA, nonLinkVertex, bendMaterial); } else { // Don't yet have link so create it softBody.AppendLink(vertex0, vertex1, structuralMaterial); // If we added a new link, set the triangle array trianglesForLinks[numVertices * vertex0 + vertex1] = triangle; trianglesForLinks[numVertices * vertex1 + vertex0] = triangle; } return true; }