瀏覽代碼

Add raycast to b3dm example. Related to #115, #68

Garrett Johnson 5 年之前
父節點
當前提交
c5490f58fe
共有 1 個文件被更改,包括 32 次插入0 次删除
  1. 32 0
      example/b3dmExample.js

+ 32 - 0
example/b3dmExample.js

@@ -8,11 +8,14 @@ import {
 	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();
@@ -71,8 +74,37 @@ function init() {
 
 		} );
 
+	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 ) );
+
+		}
+
+	}
 
 }