Project finished: Intersection Image: Union Image: Difference Image: Download: https://app.box.com/s/a97omj348mazfh0vasx5 Texture Material Included. Example script: Code (csharp): using UnityEngine; using System.Collections; public class csSceneBoolean : MonoBehaviour { public MeshCollider meshColliderA; public MeshCollider meshColliderB; // Use this for initialization void Start () { // Create new GameObject GameObject newObject = new GameObject(); newObject.transform.localScale*=2f; MeshFilter meshFilter = newObject.AddComponent<MeshFilter>(); MeshRenderer meshRenderer = newObject.AddComponent<MeshRenderer>(); meshRenderer.materials = new Material[2]{meshColliderA.transform.renderer.materials[0],meshColliderB.transform.renderer.materials[0]}; // Assign booleanMesh BooleanMesh booleanMesh = new BooleanMesh(meshColliderA,meshColliderB); //meshFilter.mesh = booleanMesh.Difference(); //meshFilter.mesh = booleanMesh.Union(); meshFilter.mesh = booleanMesh.Intersection(); } }
I have found information about is necessary to adjust vertices order in triangle if cross product of vector v2-v1,v3-v1, multiply by normals is negative. Code (csharp): void recalculateTriangles(Vector3[] vertices,Vector3[] normals,int[] triangles){ Vector3 a,b,c; int v1,v2,v3; for(int i=0;i<triangles.Length;i+=3){ v1 = triangles[i]; v2 = triangles[i+1]; v3 = triangles[i+2]; a = vertices[v1]; b = vertices[v2]; c = vertices[v3]; if(Vector3.Dot(normals[v1]+normals[v2]+normals[v3],Vector3.Cross((b-a),(c-a)))<0f){ triangles[i+2]=v1; triangles[i]=v3; } } } And this problem is solved. But, another problem has appeared. The second mesh present problems of normals. The triangles exist, the problem is another time about normals. If you change mesh order, from A to B, by B to A, the problem appears always in the second mesh. Image Screen: Someone know what adjust type is necessary to join vertices and triangles groups of two meshes?.
Was another bug, wasn't normal problem. The second problem was due to mesh order between recording processes vertices inside or outside of the mesh and the calculation of intersections, the intersections and which are calculated based on whether the tip is in or out of mesh. (If the vertex of the mesh is within a raycast is not drawn, that is, the intersection is not calculated). * Project ended with texture mapping included. The download link is in the first message.
Would it be possible to modify this to receive an array or list of meshes? 3+ operands? Thanks, -Dustin It seems like the easiest way might be to modify lines 17-23 of csSceneBoolean.cs into a loop and modify the input to include the object and the action to be performed per object. Then the loop could apply each object and its operation starting with the first in the list. *Sunday booked* Obviously this loop could be in my code on the game side, but I want it as simple as possible so I need to try to do as much within the scripts provided. Thanks for the start!
Running into issues retrieving the proceduraly generated mesh (for collision detection on next operand) from the newGameobject. Any recommendations? Thanks, -Dustin
Using these two lines you can use the newly created mesh to generate the new MeshCollider on the GameObject that was created by the script. Code (csharp): MeshCollider meshCollider = newObject.AddComponent<MeshCollider>(); meshCollider.sharedMesh = meshFilter.sharedMesh; Edit: They need to be placed after the new mesh is generated in csSceneBoolean of course.
This clears the error and shows the mesh applied to the meshCollider, but they have no name (not even 'instance'), just blank. This isn't a problem... But, when pulling newObject.getComponent<MeshCollider> on the next 'BooleanMesh ='/booleanmesh.difference action, the second operand isn't applied? Will debug more tomorrow. Thanks!
Finally got to debugging the multi-operand problem, the first mesh (generated from first two operands) gets generated and handed to the Boolean function fine from within my looping script (loop apply operands+meshes until all done). Infinite loop problem now, BooleanMesh.cs under the function bool ConcaveHull gets stuck 'while' (270-280) looping the same raycast when csBooleanMesh calls BooleanMesh.Difference passing the dynamically generated mesh and a third mesh present as scene start (To make the next hole or the third operand dynamically). Some of the steps below are from reading about meshes being null'd before re-written and a lot of sharedmesh copying to get the generated mesh passed to the boolean function successfully. Would love a cleaner version, plan to clean up once i get working. Code (csharp): public MeshCollider meshColliderA; Sphere with sphere meshcollider added public MeshCollider meshColliderB; Cube-log used to poke hole in sphere, cube meshcollider added public MeshCollider meshColliderC; Cube-log used to poke hole in sphere, cube meshcollider added <...> GameObject newObject = new GameObject (); newObject.transform.localScale *= 2f; MeshFilter meshFilter = newObject.AddComponent<MeshFilter> (); MeshRenderer meshRenderer = newObject.AddComponent<MeshRenderer> (); MeshCollider newMeshCollider = newObject.AddComponent<MeshCollider>(); meshRenderer.materials = new Material[2] { meshColliderA.transform.renderer.materials [0], meshColliderB.transform.renderer.materials [0] }; BooleanMesh booleanMesh = new BooleanMesh (meshColliderA, meshColliderB); meshFilter.mesh = booleanMesh.Difference (); newMeshCollider.sharedMesh = meshFilter.sharedMesh; //second operation test BooleanMesh booleanMeshB = new BooleanMesh (newMeshCollider, meshColliderC); meshFilter.mesh = null; meshFilter.mesh = booleanMeshB.Difference(); meshFilter.sharedMesh = null; meshFilter.sharedMesh = booleanMeshB.Difference(); newMeshCollider.sharedMesh = null; newMeshCollider.sharedMesh = meshFilter.sharedMesh; For some reason the generated mesh and a third static mesh (C) causes BooleanMesh.cs\bool ConcaveHull to 'infinite while' loop with the same raycast. I'll keep digging but appreciate any help. Thanks, -Dustin
I am eager to try this. I have tried a few Boolean operation solutions in Unity (two of which I paid for) and none of them worked without bugs, and by bugs I mean extremely common bugs (missing normals usually). Most of them were based off the CSG.js solution which I guess doesn't work properly in Unity. I will be testing this one out and posting back my results.
The code is a start, as idlebyte said. It works correctly with two meshes that have a single material, in the first Boolean operation. If multiple Boolean operations are performed, or a Boolean operation is performed with meshes that have two or more materials each, not working properly, since the code is not designed to create an array of materials with the two meshes (created only two submesh). The first thing to do is to create a submesh for each material. Secondly, would do more tests with concave objects. The code use a Raycast to detect breakpoints, but Raycast detects the collision depending on the normal of the triangle. In principle, if the vertex of the object is inside the other object, not takes Raycast. So I say that I should be testing with concave objects. May be necessary to modify this feature. May the Force be with you.
It sounds like a really valuable contribution to the community. Thank you for sharing it, and please keep up the great work!
I have an issue that I've had with all the other Boolean operations packages I've tried. I just have two cubes with mesh colliders that are exactly beside each other. The result is always broken. Here is an example pic of what I mean: I also tried with the same material and same result. The z axis on both cubes are exactly the same which seems to be causing issues with the normals.
Damn. First I thought this was awesome, but it doesn't work with multiple operations. The result is pretty messy.
I am figuring this out right now for a proof-of-concept game of mine. Thanks a lot IsGreen, I heart you. I will make sure your contribution is not forgotten.
I always get a mesh of the two original meshes that were intersected if it is convex. I tried the code written by bondematt, but it made now difference. Could it have something to do with the resulting mesh having two sub-meshes?
I am not being able to download the package. When I click the button at app.box website in order to download, nothing happens. Would it be possible to make it available again? Many thanks.
It is nice. Is their any update? Can it be used in run time for continuous Boolean subtraction viz. simulation of grinding/cutting/drilling type scene.. Thanks...