123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import { B3DMLoader } from '../src/index.js';
- import {
- Scene,
- DirectionalLight,
- AmbientLight,
- WebGLRenderer,
- PerspectiveCamera,
- Box3,
- sRGBEncoding,
- PCFSoftShadowMap,
- Vector2,
- Raycaster,
- } from 'three';
- import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
- let camera, controls, scene, renderer;
- let box, dirLight;
- let raycaster, mouse;
- init();
- animate();
- function init() {
- scene = new Scene();
- // primary camera view
- renderer = new WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setClearColor( 0x151c1f );
- renderer.shadowMap.enabled = true;
- renderer.shadowMap.type = PCFSoftShadowMap;
- renderer.outputEncoding = sRGBEncoding;
- document.body.appendChild( renderer.domElement );
- camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
- camera.position.set( 400, 400, 400 );
- // controls
- controls = new OrbitControls( camera, renderer.domElement );
- controls.screenSpacePanning = false;
- controls.minDistance = 1;
- controls.maxDistance = 2000;
- // lights
- dirLight = new DirectionalLight( 0xffffff, 1.25 );
- dirLight.position.set( 1, 2, 3 ).multiplyScalar( 40 );
- dirLight.castShadow = true;
- dirLight.shadow.bias = - 0.01;
- dirLight.shadow.mapSize.setScalar( 2048 );
- const shadowCam = dirLight.shadow.camera;
- shadowCam.left = - 200;
- shadowCam.bottom = - 200;
- shadowCam.right = 200;
- shadowCam.top = 200;
- shadowCam.updateProjectionMatrix();
- scene.add( dirLight );
- const ambLight = new AmbientLight( 0xffffff, 0.05 );
- scene.add( ambLight );
- box = new Box3();
- new B3DMLoader()
- .load( 'https://raw.githubusercontent.com/CesiumGS/3d-tiles-samples/master/tilesets/TilesetWithRequestVolume/city/lr.b3dm' )
- .then( res => {
- console.log( res );
- scene.add( res.scene );
- } );
- raycaster = new Raycaster();
- mouse = new Vector2();
- onWindowResize();
- window.addEventListener( 'resize', onWindowResize, false );
- renderer.domElement.addEventListener( 'mousemove', onMouseMove, false );
- }
- function onMouseMove( e ) {
- const bounds = this.getBoundingClientRect();
- mouse.x = e.clientX - bounds.x;
- mouse.y = e.clientY - bounds.y;
- mouse.x = ( mouse.x / bounds.width ) * 2 - 1;
- mouse.y = - ( mouse.y / bounds.height ) * 2 + 1;
- raycaster.setFromCamera( mouse, camera );
- const intersects = raycaster.intersectObject( scene, true );
- if ( intersects.length ) {
- const { face, object } = intersects[ 0 ];
- const batchid = object.geometry.getAttribute( '_batchid' );
- if ( batchid ) {
- console.log( '_batchid', batchid.getX( face.a ), batchid.getX( face.b ), batchid.getX( face.c ) );
- }
- }
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- camera.updateProjectionMatrix();
- }
- function animate() {
- requestAnimationFrame( animate );
- render();
- }
- function render() {
- renderer.render( scene, camera );
- }
|