hI guys, I really need help, I have model(building model) and my task is to get and show building floor info when user clicks on specific floor(for example 3floor or 5 floor).I read some topics but I can’t do it.
Should I use meshes?
here is my code
<head>
<title>threejs - models</title>
</head>
<body>
<canvas id="myCanvas"></canvas>
<script type="module">
import * as THREE from '../build/three.module.js';
import { GLTFLoader } from './jsm/loaders/GLTFLoader.js';
import { DRACOLoader } from './jsm/loaders/DRACOLoader.js';
import { OrbitControls } from './jsm/controls/OrbitControls.js';
var camera, scene, renderer;
// Instantiate a loader
var loader = new GLTFLoader();
var renderer;
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
renderer = new THREE.WebGLRenderer( { alpha: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var controls = new OrbitControls( camera, renderer.domElement );
controls.addEventListener( 'change', render );
controls.target.set( 0, 1.2, 2 );
controls.minDistance = 1;
controls.maxDistance = 300;
controls.update();
window.addEventListener( 'resize', onWindowResize, false );
function render() {
renderer.render( scene, camera );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
render();
}
// Color for white
renderer.setClearColor( 0xffffff, 1 );
//LIGHTS
var light = new THREE.AmbientLight(0xffff, 0.5);
scene.add(light);
var light2 = new THREE.PointLight(0xffffff, 2);
scene.add(light2);
var dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath( '/examples/js/libs/draco/' );
loader.setDRACOLoader( dracoLoader );
var toIntersect = [];
// Load a glTF resource
loader.load(
//'max.gltf',
'./last.gltf',
function ( gltf ) {
console.log('gltf', gltf)
camera.position.z = 40;
scene.add( gltf.scene );
var t = gltf.scenes[0].children;
scene.traverse(function (t) {
if (t instanceof THREE.Mesh) {
toIntersect.push(t);
}
});
},
// called while loading is progressing
function ( xhr ) {
console.log('done')
console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
},
// called when loading has errors
function ( error ) {
console.log(error);
}
);
var raycaster, mouse = { x : 0, y : 0 };
raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
renderer.domElement.addEventListener( 'click', raycast, false );
function raycast ( e ) {
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( toIntersect );
console.log(intersects)
var INTERSECTED;
if ( intersects.length > 0 )
{
var color = (Math.random() * 0xffffff);
intersects[0].object.material.color.setHex(color);
}
}
</script>
</body>
</html>