Hi, I’m having trouble trying to add a decal to an intersected object. Everything seems fine when I add it to the scene but I can’t figure out how to transform the position from world space to object’s local space
Sandbox and code:
const hit = raycaster.intersectObjects(scene.children)[0];
const position = hit.point;
const normal = hit.face.normal;
const object = hit.object;
const inverseMatrix = new THREE.Matrix4().copy(object.matrixWorld).invert();
const localNormal = normal.clone().transformDirection(inverseMatrix);
const up = new THREE.Vector3(0, 0, 1);
const quaternion = new THREE.Quaternion().setFromUnitVectors(
up,
localNormal
);
const localPosition = position.clone().applyMatrix4(inverseMatrix);
let geometry = new DecalGeometry(
object,
localPosition,
new THREE.Euler().setFromQuaternion(quaternion),
new THREE.Vector3(1, 1, 1)
);
const mesh = new THREE.Mesh(geometry, material);
object.add(mesh);