0

I experience a problem when substracting a mesh from an other mesh using ThreeCSG. My main mesh is a ring and the mesh to substract is a diamond. Before the process to scene looks like this: Mesh fine. But after substracting the meshes the ring becomes angular: Mesh Broken. I do apply the same material / shading as before. Here is the code I use:

var ring_bsp = new ThreeBSP(ring);
var stone_bsp = new ThreeBSP(stone);
var substract_bsp = ring_bsp.subtract( stone_bsp );
var result = substract_bsp.toMesh( ringMaterial );

result.geometry.computeVertexNormals();

result.material.needsUpdate = true;
result.geometry.buffersNeedUpdate = true;
result.geometry.uvsNeedUpdate = true;

result.scale.x = result.scale.y = result.scale.z = 19;

scene.remove(ring);
scene.add(result);

Update one: If I remove "result.geometry.computeVertexNormals();" the result looks even worst: link.

Update two: I created a jsfiddle with a minimal case

Update three: After looking some more into the problem and Wilts last update, I saw that after I use ThreeBSP the vertexes are messed up. You can see this very well in this fiddle.

Update four: The problem seems to be within the "fromGeometry / toGeometry" functions as I get the same broken mesh if I don't do any substraction at all.

Martin
  • 665
  • 1
  • 7
  • 24
  • I updated my answer... Might have found your problem. – Wilt Sep 21 '14 at 15:33
  • Looking at your image, I would say that after you called `computeVertexNormals()` the vertexNormals seem to have disappeared totally. – Wilt Sep 21 '14 at 15:35

1 Answers1

0

It looks like (some of) your vertex normals get lost during translation (translating your geometry to CSG and translating back to Three.js). You should check out the source code to see where this goes wrong.

UPDATE 1:

I looked into the source code of ThreeCSG.js it seems there is a bug on line 48.

It should be:

vertex = new ThreeBSP.Vertex( vertex.x, vertex.y, vertex.z, face.vertexNormals[1], uvs );

The index for the vertexNormals should be 1 instead of 2. Maybe that bug causes the wrong export result.

UPDATE 2:

Try updating the vertexNormals of the geometry before you convert to CSG:

var geometry = ring.geometry;
geometry.computeFaceNormals();
geometry.computeVertexNormals();

Note. You need to call computeNormals() first for the correct result.

UPDATE 3:

In the conversion of faces from Three.js geometries to CSG geometries the ThreeBSP.Polygon.prototype.classifySide method checks wheter the vertex of the adjacent face is in the front, in the back or coplanar to the current face. If the point is coplanar the CSG face will be defined as a Face with four vertex points. Because of this process some of your THREE.Face3 get converted to a 4 point CSG face. When later translating it back to a THREE.Face3 the Face vertexNormals become different from their initial values.

The vertex is classified FRONT, BACK or COPLANAR using an EPSILON value to compare the vertex normal with the face normal. If the difference is too small the Vertex is considered coplanar. By increasing the EPSILON value in your ThreeBSP library you can control the precision. If you set EPSILON to 10 your triangles will never be considered coplanar and the conversion result will be correct.

So at line 5 of your ThreeBSP library set:

EPSILON = 10,
Wilt
  • 41,477
  • 12
  • 152
  • 203
  • Thanks for your help. But modifying this line did't help. The result just stayed exactly the same. But I'll look further into that direction. – Martin Sep 22 '14 at 16:58
  • @Matin I edited my answer. Calling `computeVertexNormals();` needs to be preceded by `computeFaceNormals();` You should try calling those **before** you convert to CSG – Wilt Sep 23 '14 at 11:10
  • Thanks again for your time. I tried to change stuff like you said, but it didn't help. The only thing that chainges anything is adding computeFace/VertexNormals to the result. [example](http://i.stack.imgur.com/TCTek.png). As you can see on the right side, the ring becomes a little less angular. – Martin Sep 23 '14 at 15:14
  • I already tried to play around with this variable. But it allways resulted into the model to disappear for big parts. I also created a jsfiddle. Finally :D – Martin Sep 24 '14 at 17:06
  • since I were still not able to solve this problem and I have a strict deadline in two weeks I am willing to pay you for solving this problem. If you are interested please contact me. – Martin Oct 26 '14 at 15:02