Browse Source

XRPlanes: RapierPhysics.js-friendly geometries and dispatch planeschanged event.

Mr.doob 2 years ago
parent
commit
8acb815ef5
1 changed files with 14 additions and 2 deletions
  1. 14 2
      examples/jsm/webxr/XRPlanes.js

+ 14 - 2
examples/jsm/webxr/XRPlanes.js

@@ -12,7 +12,6 @@ class XRPlanes extends Object3D {
 
 
 		super();
 		super();
 
 
-		const geometry = new BoxGeometry();
 		const matrix = new Matrix4();
 		const matrix = new Matrix4();
 
 
 		const currentPlanes = new Map();
 		const currentPlanes = new Map();
@@ -26,15 +25,20 @@ class XRPlanes extends Object3D {
 
 
 			const referenceSpace = xr.getReferenceSpace();
 			const referenceSpace = xr.getReferenceSpace();
 
 
+			let planeschanged = false;
+
 			for ( const [ plane, mesh ] of currentPlanes ) {
 			for ( const [ plane, mesh ] of currentPlanes ) {
 
 
 				if ( planes.has( plane ) === false ) {
 				if ( planes.has( plane ) === false ) {
 
 
+					mesh.geometry.dispose();
 					mesh.material.dispose();
 					mesh.material.dispose();
 					this.remove( mesh );
 					this.remove( mesh );
 
 
 					currentPlanes.delete( plane );
 					currentPlanes.delete( plane );
 
 
+					planeschanged = true;
+
 				}
 				}
 
 
 			}
 			}
@@ -65,20 +69,28 @@ class XRPlanes extends Object3D {
 					const width = maxX - minX;
 					const width = maxX - minX;
 					const height = maxZ - minZ;
 					const height = maxZ - minZ;
 
 
+					const geometry = new BoxGeometry( width, 0.01, height );
 					const material = new MeshBasicMaterial( { color: 0xffffff * Math.random() } );
 					const material = new MeshBasicMaterial( { color: 0xffffff * Math.random() } );
 
 
 					const mesh = new Mesh( geometry, material );
 					const mesh = new Mesh( geometry, material );
 					mesh.position.setFromMatrixPosition( matrix );
 					mesh.position.setFromMatrixPosition( matrix );
 					mesh.quaternion.setFromRotationMatrix( matrix );
 					mesh.quaternion.setFromRotationMatrix( matrix );
-					mesh.scale.set( width, 0.01, height );
 					this.add( mesh );
 					this.add( mesh );
 
 
 					currentPlanes.set( plane, mesh );
 					currentPlanes.set( plane, mesh );
 
 
+					planeschanged = true;
+
 				}
 				}
 
 
 			}
 			}
 
 
+			if ( planeschanged ) {
+
+				this.dispatchEvent( { type: 'planeschanged' } );
+
+			}
+
 		} );
 		} );
 
 
 	}
 	}