Using ThreeCSG with BufferGeometry is giving me weird results - missing faces

463 Views Asked by At

When trying to subtract or union two meshes (TextGeometry and BufferGeometry), result meshes have missing faces and opposite results, I am using ThreeCSG that supposedly support buffer geometry, I tried converting to normal Geometry and results were the same.

Union :

enter image description here

Subtract :

enter image description here

Here is my code:

async function addObject(values, step, heightScale, offset) {
  var depth = 8;

  var geometry = new THREE.BufferGeometry();

  values.unshift(0);
  values.push(0);

  var positions = [];

  for (var i = 0; i < values.length - 1; i++) {
    //back
    positions.push(i * step, 0, 0);
    positions.push(i * step, values[i] * heightScale + offset, 0);
    positions.push((i + 1) * step, values[i + 1] * heightScale + offset, 0);

    positions.push(i * step, 0, 0);
    positions.push((i + 1) * step, values[i + 1] * heightScale + offset, 0);
    positions.push((i + 1) * step, 0, 0);

    //front
    positions.push((i + 1) * step, values[i + 1] * heightScale + offset, depth);
    positions.push(i * step, values[i] * heightScale + offset, depth);
    positions.push(i * step, 0, depth);

    positions.push((i + 1) * step, values[i + 1] * heightScale + offset, depth);
    positions.push((i + 1) * step, 0, depth);
    positions.push(i * step, 0, depth);

    //top
    positions.push(i * step, values[i] * heightScale + offset, 0);
    positions.push(i * step, values[i] * heightScale + offset, depth);
    positions.push((i + 1) * step, values[i + 1] * heightScale + offset, 0);

    positions.push((i + 1) * step, values[i + 1] * heightScale + offset, 0);
    positions.push(i * step, values[i] * heightScale + offset, depth);
    positions.push((i + 1) * step, values[i + 1] * heightScale + offset, depth);

    //bottom
    positions.push(i * step, 0, depth);
    positions.push((i + 1) * step, 0, 0);
    positions.push(i * step, 0, 0);

    positions.push((i + 1) * step, 0, 0);
    positions.push(i * step, 0, depth);
    positions.push((i + 1) * step, 0, depth);
  }

  // itemSize = 3 because there are 3 values (components) per vertex
  geometry.addAttribute(
    "position",
    new THREE.Float32BufferAttribute(positions, 3)
  );

  geometry.computeVertexNormals();

  var material = new THREE.MeshPhongMaterial({
    color: new THREE.Color(0x00ff00),
    shininess: 66,
    opacity: 1,
    transparent: true,
    side: THREE.DoubleSide
  });


  //var geometry = new THREE.Geometry().fromBufferGeometry(geometry);
  //geometry.mergeVertices();
  var mesh = new THREE.Mesh(geometry, material);

  return mesh;
}


async function addText(mesh, textLocal, depth, textSize, type, x, y, z) {
  var font = await loadFont("assets/font2.json");

  var mesh_bsp = new ThreeBSP(mesh);
  var textGeo = new THREE.TextGeometry(textLocal, {
    size: textSize,
    height: depth,
    curveSegments: 6,
    font: font 
  });

  var text = new THREE.Mesh(textGeo, g_material);
  text.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(x, y, z));

  var text_bsp = new ThreeBSP(text);
  if (type == "subtract") {
    var subtract_bsp = mesh_bsp.subtract(text_bsp);
  } else {
    var subtract_bsp = mesh_bsp.union(text_bsp);
  }
  result = subtract_bsp.toMesh(g_material);

  return result;
}

I get different results if I uncomment these two lines:

//var geometry = new THREE.Geometry().fromBufferGeometry(geometry);
//geometry.mergeVertices();

Results look like that:

Union normal geometry :

enter image description here

Subtract front :

enter image description here

Subtract back :

enter image description here

Any ideas what I am missing?

0

There are 0 best solutions below