Hello! I ve got an three js and tried to use it with my project. The thing is - i needed to select my custom mesh loaded from OBJ file.
I created simple raycaster, simple cube, and my model (which is cube also). The thing is - i can have cube raycasted but it doesn t see my model. Where can i have the problem?
var container, stats;
var camera, scene, projector, renderer;
var particleMaterial;
var textureLoader;
var modelLoader;
var objects = [];
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 0, 300, 500 );
scene = new THREE.Scene();
var manager = new THREE.LoadingManager();
textureLoader = new THREE.ImageLoader( manager );
modelLoader = new THREE.OBJLoader(manager);
modelLoader.load( 'cube.obj', function ( object ) {
object.traverse( function ( child ) {
if ( child instanceof THREE.Mesh ) {
console.log("instance");
child.geometry.computeFaceNormals();
child.material = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, opacity: 0.5 } );
child.material.side = THREE.DoubleSided;
}
} );
objects.push(object);
object.position.x = 10;
object.position.y = 10;
object.scale.set(100,100,100);
scene.add(object);
});
var geometry = new THREE.SphereGeometry(150, 100, 100);
for ( var i = 0; i < 1; i ++ ) {
var object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, opacity: 0.5 } ) );
object.position.x = Math.random() * 800 - 400;
object.position.y = Math.random() * 800 - 400;
object.position.z = Math.random() * 800 - 400;
object.scale.x = Math.random() * 2 + 1;
object.scale.y = Math.random() * 2 + 1;
object.scale.z = Math.random() * 2 + 1;
object.rotation.x = Math.random() * 2 * Math.PI;
object.rotation.y = Math.random() * 2 * Math.PI;
object.rotation.z = Math.random() * 2 * Math.PI;
scene.add( object );
objects.push( object );
}
var PI2 = Math.PI * 2;
particleMaterial = new THREE.SpriteCanvasMaterial( {
color: 0x000000,
program: function ( context ) {
context.beginPath();
context.arc( 0, 0, 0.5, 0, PI2, true );
context.fill();
}
} );
projector = new THREE.Projector();
renderer = new THREE.WebGLRenderer();
renderer.setClearColor( 0xf0f0f0 );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onDocumentMouseDown( event ) {
event.preventDefault();
var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
projector.unprojectVector( vector, camera );
var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
var intersects = raycaster.intersectObjects( objects );
if ( intersects.length > 0 ) {
console.log("intersected");
intersects[ 0 ].object.material.color.setHex( Math.random() * 0xffffff );
var particle = new THREE.Sprite( particleMaterial );
particle.position = intersects[ 0 ].point;
particle.scale.x = particle.scale.y = 16;
scene.add( particle );
}
}
//
function animate() {
requestAnimationFrame( animate );
scene.updateMatrixWorld();
render();
stats.update();
}
var radius = 600;
var theta = 0;
function render() {
theta += 0.1;
camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) );
camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
camera.lookAt( scene.position );
renderer.render( scene, camera );
}
So i can see sphere selected by can t select my custom obj. Where can be the problem? Three js r66
UPDATED:
Problem was solved. I just neeeded to set var intersects = raycaster.intersectObjects( objects,true );
ThreeJS Raycaster 2022-04-30 09:05:46 ThreeJS Raycaster This class is intended to help with raycasting. Raycasting is used for a variety of purposes, including mouse picking (determining which objects in 3D space the mouse is over). Code Example
three.js Object picking / Raycasting. Example. Raycasting means throwing a ray from the mouse position on the screen to the scene, this is how threejs determines what object you want to click on if you have implemented it.
*Note* that for meshes, faces must be pointed towards the origin of the in order to be detected; intersections of the ray passing through the back of a face will not be detected. To raycast against both faces of an object, you'll want to set the's property to *THREE.
“ Ray casting is the use of ray–surface intersection tests to solve a variety of problems in 3D computer graphics and computational geometry .” When working with 3D graphics on the web using Three.js, graphics are rendered using the HTML canvas API. With some “exceptions”, this can stop you from accessing your rendered objects as DOM elements.
You need to pass the recursive flag
var intersects = raycaster.intersectObjects( objects, true );
https://threejs.org/docs/#api/core/Raycaster.intersectObjects
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With