Example #1
0
            static UnityEngine.Mesh CreateReferenceMesh(PrimitiveType type)
            {
                GameObject refGo = GameObject.CreatePrimitive(type);

                UnityEngine.Mesh mesh = refGo.GetComponent <MeshFilter>().sharedMesh;
                Destroy(refGo);
                return(mesh);
            }
Example #2
0
 static UnityEngine.Mesh GetReferenceMesh(ref UnityEngine.Mesh cache, PrimitiveType type)
 {
     if (cache == null)
     {
         cache = CreateReferenceMesh(type);
     }
     return(cache);
 }
Example #3
0
            private static void AppendConvex(ref ConvexHull hull, RigidTransform worldFromCollider, ref List <DisplayResult> results)
            {
                int totalNumVertices = 0;

                for (int f = 0; f < hull.NumFaces; f++)
                {
                    totalNumVertices += hull.Faces[f].NumVertices + 1;
                }

                Vector3[] vertices  = new Vector3[totalNumVertices];
                Vector3[] normals   = new Vector3[totalNumVertices];
                int[]     triangles = new int[(totalNumVertices - hull.NumFaces) * 3];

                int startVertexIndex = 0;
                int curTri           = 0;

                for (int f = 0; f < hull.NumFaces; f++)
                {
                    Vector3 avgFace    = Vector3.zero;
                    Vector3 faceNormal = hull.Planes[f].Normal;

                    for (int fv = 0; fv < hull.Faces[f].NumVertices; fv++)
                    {
                        int origV = hull.FaceVertexIndices[hull.Faces[f].FirstIndex + fv];
                        vertices[startVertexIndex + fv] = hull.Vertices[origV];
                        normals[startVertexIndex + fv]  = faceNormal;

                        Vector3 v = hull.Vertices[origV];
                        avgFace += v;

                        triangles[curTri * 3 + 0] = startVertexIndex + fv;
                        triangles[curTri * 3 + 1] = startVertexIndex + (fv + 1) % hull.Faces[f].NumVertices;
                        triangles[curTri * 3 + 2] = startVertexIndex + hull.Faces[f].NumVertices;
                        curTri++;
                    }
                    avgFace *= 1.0f / hull.Faces[f].NumVertices;
                    vertices[startVertexIndex + hull.Faces[f].NumVertices] = avgFace;
                    normals[startVertexIndex + hull.Faces[f].NumVertices]  = faceNormal;

                    startVertexIndex += hull.Faces[f].NumVertices + 1;
                }

                UnityEngine.Mesh mesh = new UnityEngine.Mesh();
                mesh.vertices  = vertices;
                mesh.normals   = normals;
                mesh.triangles = triangles;

                results.Add(new DisplayResult
                {
                    Mesh        = mesh,
                    Scale       = Vector3.one,
                    Position    = worldFromCollider.pos,
                    Orientation = worldFromCollider.rot
                });
            }