public static UnityEngine.Vector3[] CreateMultiSphere(MultiSphereShape shape, out int[] indices) { List<UnityEngine.Vector3[]> allVertices = new List<UnityEngine.Vector3[]>(); List<int[]> allIndices = new List<int[]>(); int vertexCount = 0; int indexCount = 0; int i; for (i = 0; i < shape.SphereCount; i++) { int[] sphereIndices; UnityEngine.Vector3[] sphereVertices = CreateSphere(shape.GetSphereRadius(i), out sphereIndices); // Adjust sphere position UnityEngine.Vector3 position = shape.GetSpherePosition(i).ToUnity(); for (int j = 0; j < sphereVertices.Length / 2; j++) { sphereVertices[j * 2] += position; } // Adjust indices if (indexCount != 0) { int indexOffset = vertexCount / 2; for (int j = 0; j < sphereIndices.Length; j++) { sphereIndices[j] += (int)indexOffset; } } allVertices.Add(sphereVertices); allIndices.Add(sphereIndices); vertexCount += sphereVertices.Length; indexCount += sphereIndices.Length; } UnityEngine.Vector3[] finalVertices = new UnityEngine.Vector3[vertexCount]; int vo = 0; foreach (UnityEngine.Vector3[] v in allVertices) { v.CopyTo(finalVertices, vo); vo += v.Length; } indices = new int[indexCount]; int io = 0; foreach (int[] ind in allIndices) { ind.CopyTo(indices, io); io += ind.Length; } return finalVertices; }
Mesh CreateMultiSphereShape(MultiSphereShape shape) { Mesh mesh = null; int i; for (i = 0; i < shape.SphereCount; i++) { Vector3 position = shape.GetSpherePosition(i); Mesh sphereMesh = Mesh.CreateSphere(device, shape.GetSphereRadius(i), 12, 12); if (i == 0) { Matrix[] transform = new Matrix[] { Matrix.Translation(position) }; mesh = Mesh.Concatenate(device, new Mesh[] { sphereMesh }, MeshFlags.Managed, transform, null); } else { Mesh multiSphereMeshNew; Matrix[] transform = new Matrix[] { Matrix.Identity, Matrix.Translation(position) }; multiSphereMeshNew = Mesh.Concatenate(device, new Mesh[] { mesh, sphereMesh }, MeshFlags.Managed, transform, null); mesh.Dispose(); mesh = multiSphereMeshNew; } sphereMesh.Dispose(); } complexShapes.Add(shape, mesh); return mesh; }