Hello,
Question about DecalGeometry. I am trying to use DecalGeometry to paste a mesh on to a GLTF/GLB model. I did somehow manage to get something until the DecalGeometry line, where whenever I run the code it throws off a Cannot read property ‘isGeometry’ of undefined error. It seems like the method isn’t recognizing the mesh (3D model)
What am I doing wrong? I have looked at other tutorials but I still can’t wrap my head around it.
Thank you for your time, I appreciate any insights!
var mesh, renderer, scene, camera, controls;
var mouse, raycaster, helper, decalMaterial;
init();
animate();
function init() {
// renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setPixelRatio( window.devicePixelRatio );
document.body.appendChild( renderer.domElement );
// scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);
// camera
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 20, 20, 20 );
// controls
controls = new THREE.OrbitControls( camera, renderer.domElement );
// ambient
scene.add( new THREE.AmbientLight( 0x222222 ) );
// light
var light = new THREE.DirectionalLight( 0xffffff, 1 );
light.position.set( 20,20, 0 );
scene.add( light );
// axes
// Displays axes; THREE.AxesHelper(length_of_axes)
scene.add( new THREE.AxesHelper( 300 ) );
let loader = new THREE.GLTFLoader();
loader.load(
"./iphone.glb",
( gltf ) => {
var mesh = gltf.scene.children[0];
mesh.rotation.x = (90/180) * Math.PI;
mesh.rotation.y = Math.PI;
scene.add(mesh);
scene.scale.set(10,10,10)
// decal related stuff
mouse = new THREE.Vector2(); // assign mouse as a 2d vector
raycaster = new THREE.Raycaster();
helper = new THREE.Object3D();
decalMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff, depthWrite: false, polygonOffset: true, polygonOffsetFactor: - 4, } );
document.addEventListener( 'click', onClick );
function onClick( event ) {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
// whatever camera is pointing at, plus the position of mouse is going to define where beam is cast
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( [mesh], true);
console.log(intersects)
helper = new THREE.Object3D();
decalMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff, depthWrite: false, polygonOffset: true, polygonOffsetFactor: - 4, } );
if ( intersects.length > 0 ) {
var n = intersects[ 0 ].face.normal.clone();
console.log(n)
n.transformDirection( gltf.scene.matrixWorld ); // transform vector 3 to matrix
n.add( intersects[ 0 ].point );
helper.position.copy( intersects[ 0 ].point );
helper.lookAt( n );
var position = intersects[ 0 ].point;
var size = new THREE.Vector3( 1, 1, 1 );
console.log(mesh)
var decalGeometry = new THREE.DecalGeometry( mesh, position, helper.rotation, size )
}
}
},
( xhr ) => {
console.log(xhr);
}
);
}
function animate() {
requestAnimationFrame( animate );
renderer.render( scene, camera );
}