Hi! I'm writing a function to create an object where a sphere and another object collide. The script is attached to the sphere and copy is the new object. I first duplicate the object, then go through every triangle of the new sphere to check if it is inside the collision area of the object it is colliding with. If it isn't, it will delete the triangle. so far, this works great. I am having trouble now though because I am left with empty space. I know Turbo Slicer has an infill solution, but I don't know if that would work with my model since it will not always be cut in a straight line. Here is my code: Code (csharp): void CreateCopy() { DestroyCopy(); GameObject collidingObject = collidingObjects[collidingObjects.Count - 1]; // Create new game object of the sphere copy = Instantiate(transform.gameObject); copy.name = "Copy"; // Make it a normal sphere Destroy(copy.GetComponent<CopySphere>()); Destroy(copy.GetComponent<Collider>()); // SetMaterial to match copied object copyMeshRenderer = copy.GetComponent<MeshRenderer>(); copyMeshRenderer.material = collidingObject.GetComponent<MeshRenderer>().material; Mesh mesh = copy.GetComponent<MeshFilter>().mesh; int[] triangles = mesh.triangles; Vector3[] vertices = mesh.vertices; Vector2[] uv = mesh.uv; Vector3[] normals = mesh.normals; List<Vector3> vertList = new List<Vector3>(); List<Vector2> uvList = new List<Vector2>(); List<Vector3> normalsList = new List<Vector3>(); List<int> trianglesList = new List<int>(); int i = 0; while (i < vertices.Length) { vertList.Add(vertices[i]); uvList.Add(uv[i]); normalsList.Add(normals[i]); i++; } for (int triCount = 0; triCount < triangles.Length; triCount += 3) { Bounds bounds = collidingObject.GetComponent<Collider>().bounds; if (bounds.Contains(copy.transform.TransformPoint(vertices[triangles[triCount]])) && bounds.Contains(copy.transform.TransformPoint(vertices[triangles[triCount + 1]])) && bounds.Contains(copy.transform.TransformPoint(vertices[triangles[triCount + 2]]))) { trianglesList.Add(triangles[triCount]); trianglesList.Add(triangles[triCount + 1]); trianglesList.Add(triangles[triCount + 2]); } } triangles = trianglesList.ToArray(); vertices = vertList.ToArray(); uv = uvList.ToArray(); normals = normalsList.ToArray(); mesh.triangles = triangles; mesh.vertices = vertices; mesh.uv = uv; mesh.normals = normals; mesh.RecalculateBounds(); mesh.RecalculateNormals(); mesh.Optimize(); copyMeshRenderer.enabled = true; copy.GetComponent<Rigidbody>().useGravity = true; SphereCollider copyMeshCollider = copy.AddComponent<SphereCollider>(); } Here is the result: Thank you for the help!
Maybe this can be of use.. I have been thinking of using it, but have not been up to test it yet. https://www.assetstore.unity3d.com/en/#!/content/18544
This looks like exactly what I'm looking for. It looks like a plugin for the editor though, when I want to do the action in run-time. I will investigate it further.
http://forum.unity3d.com/threads/boolean-mesh-project.227196/ Maybe to same help. https://app.box.com/s/a97omj348mazfh0vasx5 Is the download.