Decal geometry on child meshes

I am having a play with decal geometry, I raycast, get an intersect and it all works nicely if I have a single static mesh, like in the example. But with anything more involved, e.g. a child that sits in a group (or nested groups) I am struggling to place the decal correctly.

I would assume its not unreasonable to add the decal mesh to the object you are putting in on, so that everything moves together, in which case its local to that mesh…

In the constructor of the decal geometry I assume the position is in world space?

Are there any examples of someone applying decals to child meshes that are positioned/rotated (e.g. not 0,0,0)?

edit: - also, the size is a Vector 3, which since we are loading a 2D image seems odd, does the Z value actually do anything?

managed to answer my own question. Maybe obvious to some, in case its useful for anyone else

let size = new THREE.Vector3 (1,1,0.1) // the z seems to stop it coming through to the other side of the object?
let decalGeometry = new THREE.DecalGeometry(intersect.object, intersect.position, rotation, size);
let mesh = new THREE.Mesh(decalGeometry, decalMaterial);
intersect.object.worldToLocal(m.position);
let quaternion = New THREE.Quaternion().copy(intersect.object.quaternion).invert();
m.quaternion.multiply(quaternion );
intersect.object.add(mesh)
1 Like

Thank you so much, I tried for hours how to fix this.

You use m.position and m.quaternion but you are not defined m in the code.
it worked if m is mesh (mesh is declared in the line 3), so the final code is:

let size = new THREE.Vector3 (1,1,0.1) // the z seems to stop it coming through to the other side of the object?
let decalGeometry = new THREE.DecalGeometry(intersect.object, intersect.position, rotation, size);
let mesh = new THREE.Mesh(decalGeometry, decalMaterial);
intersect.object.worldToLocal(mesh.position);
let quaternion = New THREE.Quaternion().copy(intersect.object.quaternion).invert();
mesh.quaternion.multiply(quaternion );
intersect.object.add(mesh)