Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

Raycaster: a single-material mesh is hit on every face it draws (#114)

Kevin Rajane74af5e9261d
diff --git a/packages/scene/src/raycaster.ts b/packages/scene/src/raycaster.ts
index 2f936e8..b21cea0 100644
--- a/packages/scene/src/raycaster.ts
+++ b/packages/scene/src/raycaster.ts
@@ -166,7 +166,8 @@ export class Raycaster {
if (local.intersectBox(g.boundingBox!) === null) return;
const p = g.positions, uv = g.uvs, idx = g.triangleIndices();
const a = new Vector3(), b = new Vector3(), c = new Vector3();
- const groups = g.groups.length ? g.groups : [{ start: 0, count: idx.length, materialIndex: 0 }];
+ // The faces the engine draws: by group only for several materials on a geometry with groups (meshMaterials).
+ const groups = materials.length > 1 && g.groups.length ? g.groups : [{ start: 0, count: idx.length, materialIndex: 0 }];
for (const grp of groups) {
const mat = materials[Math.min(grp.materialIndex, materials.length - 1)];
for (let i = grp.start; i + 2 < Math.min(grp.start + grp.count, idx.length); i += 3) {
diff --git a/packages/scene/test/three-compat.test.ts b/packages/scene/test/three-compat.test.ts
index dfa1a50..738cb67 100644
--- a/packages/scene/test/three-compat.test.ts
+++ b/packages/scene/test/three-compat.test.ts
@@ -275,6 +275,37 @@ test("the binding: ambient lights as the environment, black background, three.js
assert.ok(compatNotes().some((n) => n.startsWith("mapped: MeshPhongMaterial")));
});
+test("Raycaster: a single-material mesh is hit outside its geometry groups, as three.js does and the engine draws it", () => {
+ const geometry = new THREE.BufferGeometry();
+ geometry.setAttribute("position", new THREE.Float32BufferAttribute([
+ -3, -1, 0, -1, -1, 0, -3, 1, 0,
+ 1, -1, 0, 3, -1, 0, 1, 1, 0,
+ ], 3));
+ geometry.setAttribute("uv", new THREE.Float32BufferAttribute([
+ 0, 0, 1, 0, 0, 1,
+ 0, 0, 1, 0, 0, 1,
+ ], 2));
+ geometry.setIndex([0, 1, 2, 3, 4, 5]);
+ geometry.addGroup(0, 3, 0); // only triangle 0 is in a group; triangle 1 must still be hit for one material
+ const mesh = new THREE.Mesh(geometry, new THREE.MeshStandardMaterial());
+ const rc = new THREE.Raycaster(new THREE.Vector3(1.5, -0.3, 5), new THREE.Vector3(0, 0, -1));
+ const target: THREE.Intersection[] = [];
+ const hits = rc.intersectObject(mesh, false, target);
+ assert.equal(hits, target, "the optional target array is returned by identity");
+ assert.equal(hits.length, 1, "a single-material mesh raycasts faces outside geometry groups");
+ assert.equal(hits[0].object, mesh);
+ assert.equal(hits[0].faceIndex, 1);
+ assert.equal(hits[0].face!.materialIndex, 0);
+ assert.ok(Math.abs(hits[0].distance - 5) < 1e-9);
+ assert.deepEqual(hits[0].point.toArray(), [1.5, -0.3, 0]);
+ assert.ok(hits[0].uv!.distanceTo(new THREE.Vector2(0.25, 0.35)) < 1e-9);
+ const twoMaterials = [new THREE.MeshStandardMaterial(), new THREE.MeshStandardMaterial()];
+ assert.equal(rc.intersectObject(new THREE.Mesh(geometry, twoMaterials)).length, 0, "several materials: only grouped faces");
+ const ungrouped = geometry.clone();
+ ungrouped.clearGroups();
+ assert.equal(rc.intersectObject(new THREE.Mesh(ungrouped, twoMaterials)).length, 1, "several materials, no groups: drawn with the first, so hit");
+});
+
test("Bugbot on #125: exposure edited in place is the scene's; a transformed geometry's faceted copy is made again; a cloned InstancedMesh has its own tables", () => {
const scene = new THREE.Scene();
scene.add(new THREE.AmbientLight(0xffffff, 1));