Browse Source

fix error calculation

Garrett Johnson 5 years ago
parent
commit
e24a1c90d6
3 changed files with 12 additions and 19 deletions
  1. 2 2
      example/index.js
  2. 7 14
      src/three/TilesRenderer.js
  3. 3 3
      src/three/raycastTraverse.js

+ 2 - 2
example/index.js

@@ -116,10 +116,10 @@ function init() {
 
 
 	// lights
 	// lights
 	var dirLight = new DirectionalLight( 0xffffff );
 	var dirLight = new DirectionalLight( 0xffffff );
-	dirLight.position.set( 1, 1, 1 );
+	dirLight.position.set( 1, 2, 3 );
 	scene.add( dirLight );
 	scene.add( dirLight );
 
 
-	var ambLight = new AmbientLight( 0x222222 );
+	var ambLight = new AmbientLight( 0xffffff, 0.2 );
 	scene.add( ambLight );
 	scene.add( ambLight );
 
 
 	box = new Box3();
 	box = new Box3();

+ 7 - 14
src/three/TilesRenderer.js

@@ -24,7 +24,6 @@ const resVector = new Vector2();
 const vecX = new Vector3();
 const vecX = new Vector3();
 const vecY = new Vector3();
 const vecY = new Vector3();
 const vecZ = new Vector3();
 const vecZ = new Vector3();
-const ray = new Ray();
 const _sphere = new Sphere();
 const _sphere = new Sphere();
 
 
 function emptyRaycast() {}
 function emptyRaycast() {}
@@ -418,26 +417,20 @@ export class TilesRenderer extends TilesRendererBase {
 
 
 			// TODO: these can likely be cached? Or the world transform mat can be used
 			// TODO: these can likely be cached? Or the world transform mat can be used
 			// transformMat can be rolled into oobMat
 			// transformMat can be rolled into oobMat
-			tempMat.copy( transformMat );
+			tempMat.copy( group.matrixWorld );
+			tempMat.multiply( transformMat );
 			tempMat.multiply( obbMat );
 			tempMat.multiply( obbMat );
 			tempMat.getInverse( tempMat );
 			tempMat.getInverse( tempMat );
 
 
 			// NOTE: scale is inverted here.
 			// NOTE: scale is inverted here.
 			// assume the scales on all axes are uniform.
 			// assume the scales on all axes are uniform.
-			let scale;
-			let isInvalid;
+			let invScale;
 
 
 			// account for tile scale.
 			// account for tile scale.
 			tempVector.setFromMatrixScale( tempMat );
 			tempVector.setFromMatrixScale( tempMat );
-			scale = tempVector.x;
-			isInvalid = Math.abs( Math.max( tempVector.x - tempVector.y, tempVector.x - tempVector.z ) ) > 1e-6;
-
-			// account for parent group scale. Divide because this matrix has not been inverted like the previous one.
-			tempVector.setFromMatrixScale( group.matrixWorld );
-			scale /= tempVector.x;
-			isInvalid = isInvalid || Math.abs( Math.max( tempVector.x - tempVector.y, tempVector.x - tempVector.z ) ) > 1e-6;
+			invScale = tempVector.x;
 
 
-			if ( isInvalid ) {
+			if ( Math.abs( Math.max( tempVector.x - tempVector.y, tempVector.x - tempVector.z ) ) > 1e-6 ) {
 
 
 				console.warn( 'ThreeTilesRenderer : Non uniform scale used for tile which may cause issues when calculating screen space error.' );
 				console.warn( 'ThreeTilesRenderer : Non uniform scale used for tile which may cause issues when calculating screen space error.' );
 
 
@@ -457,12 +450,12 @@ export class TilesRenderer extends TilesRendererBase {
 					const w = cam.right - cam.left;
 					const w = cam.right - cam.left;
 					const h = cam.top - cam.bottom;
 					const h = cam.top - cam.bottom;
 					const pixelSize = Math.max( h / resVector.height, w / resVector.width );
 					const pixelSize = Math.max( h / resVector.height, w / resVector.width );
-					error = tile.geometricError / ( pixelSize * scale );
+					error = tile.geometricError / ( pixelSize * invScale );
 
 
 				} else {
 				} else {
 
 
 					const distance = boundingBox.distanceToPoint( tempVector );
 					const distance = boundingBox.distanceToPoint( tempVector );
-					const scaledDistance = distance * scale;
+					const scaledDistance = distance * invScale;
 					const sseDenominator = 2 * Math.tan( 0.5 * cam.fov * DEG2RAD );
 					const sseDenominator = 2 * Math.tan( 0.5 * cam.fov * DEG2RAD );
 					error = ( tile.geometricError * resVector.height ) / ( scaledDistance * sseDenominator );
 					error = ( tile.geometricError * resVector.height ) / ( scaledDistance * sseDenominator );
 
 

+ 3 - 3
src/three/raycastTraverse.js

@@ -70,9 +70,9 @@ export function raycastTraverseFirstHit( root, group, activeSet, raycaster ) {
 			if ( _ray.intersectBox( boundingBox, _vec ) ) {
 			if ( _ray.intersectBox( boundingBox, _vec ) ) {
 
 
 				// account for tile scale
 				// account for tile scale
-				let scale;
+				let invScale;
 				_vec2.setFromMatrixScale( _mat );
 				_vec2.setFromMatrixScale( _mat );
-				scale = _vec2.x;
+				invScale = _vec2.x;
 
 
 				if ( Math.abs( Math.max( _vec2.x - _vec2.y, _vec2.x - _vec2.z ) ) > 1e-6 ) {
 				if ( Math.abs( Math.max( _vec2.x - _vec2.y, _vec2.x - _vec2.z ) ) > 1e-6 ) {
 
 
@@ -87,7 +87,7 @@ export function raycastTraverseFirstHit( root, group, activeSet, raycaster ) {
 				};
 				};
 				array.push( data );
 				array.push( data );
 
 
-				data.distance = _vec.distanceToSquared( _ray.origin ) * scale * scale;
+				data.distance = _vec.distanceToSquared( _ray.origin ) * invScale * invScale;
 				data.tile = tile;
 				data.tile = tile;
 
 
 			} else {
 			} else {