gkjohnson / three-bvh-csg

A flexible, memory compact, fast and dynamic CSG implementation on top of three-mesh-bvh
MIT License
552 stars 45 forks source link

Two THREE.BoxGeometry intersect error. #145

Closed kxh4892636 closed 10 months ago

kxh4892636 commented 10 months ago

The example code is as follows.

import { BoxGeometry, BufferGeometry, PlaneGeometry } from 'three'
import { Brush, Evaluator, INTERSECTION } from 'three-bvh-csg'

const intersection = (
  geometry1: BufferGeometry,
  geometry2: BufferGeometry,
): BufferGeometry => {
  const brush1 = new Brush(geometry1)
  const brush2 = new Brush(geometry2)

  const csgEvaluator = new Evaluator()
  const result = csgEvaluator.evaluate(brush1, brush2, INTERSECTION)

  return result.geometry
}

const geo1 = new BoxGeometry(10, 10, 10)
const geo2 = new PlaneGeometry(10, 10)
geo2.translate(0, 0, 5)
const geo3 = new BoxGeometry(10, 10, 10)
geo3.translate(0, 0, 10)

const intersection1 = intersection(geo1, geo2)
const intersection2 = intersection(geo1, geo3)
console.log(intersection1.getAttribute('position'))
console.log(intersection2.getAttribute('position'))

The bottom surface of geo3 is the same as geo2, but when the two BoxGeometry intersect, the result is null. However, when BoxGeometry intersects with PlaneGeometry, the result is correct.

The result is as follows.

BufferAttribute {
  isBufferAttribute: true,
  name: '',
  array: Float32Array(18) [
    -5, 5,  5, -5, -5, 5,  5,
     5, 5, -5, -5,  5, 5, -5,
     5, 5,  5,  5
  ],
  itemSize: 3,
  count: 6,
  normalized: false,
  usage: 35044,
  updateRange: { offset: 0, count: -1 },
  gpuType: 1015,
  version: 1
}
BufferAttribute {
  isBufferAttribute: true,
  name: '',
  array: Float32Array(0) [],
  itemSize: 3,
  count: 0,
  normalized: false,
  usage: 35044,
  updateRange: { offset: 0, count: -1 },
  gpuType: 1015,
  version: 1
}
gkjohnson commented 10 months ago

Please provide a live example with something like jsfiddle and pictures of the issue

kxh4892636 commented 10 months ago

I create the live example, the link as follow.

The bottom surface of geo3 is the same as geo2. geo1 and geo3 have no intersection (but the intersection should be a surface), while geo1 and geo2 have the correct intersection result. You can see the intersecting results in the console.

https://codesandbox.io/s/kind-wave-3jmvks

gkjohnson commented 10 months ago

Again - please provide pictures and explain what you expect. If you look at this there's nothing incorrect about the result. The result of an intersection of two shapes in a just-touch configuration has no volume and therefore no shape.

image

However, when BoxGeometry intersects with PlaneGeometry, the result is correct.

Using PlaneGeometry is not valid since it is not two manifold and has no volume.