Bladeren bron

Add variable resolution per camera

Garrett Johnson 5 jaren geleden
bovenliggende
commit
89930e3bc3
2 gewijzigde bestanden met toevoegingen van 94 en 34 verwijderingen
  1. 5 5
      example/index.js
  2. 89 29
      src/three/TilesRenderer.js

+ 5 - 5
example/index.js

@@ -65,9 +65,6 @@ function reinstantiateTiles() {
 	}
 
 	tiles = new TilesRenderer( url );
-	tiles.camera = camera;
-	tiles.setResolutionFromRenderer( renderer );
-
 	offsetParent.add( tiles.group );
 
 }
@@ -316,10 +313,13 @@ function animate() {
 	tiles.loadSiblings = params.loadSiblings;
 	tiles.displayActiveTiles = params.displayActiveTiles;
 	tiles.maxDepth = params.maxDepth;
-	tiles.camera = params.orthographic ? orthoCamera : camera;
 	tiles.displayBoxBounds = params.displayBoxBounds;
 	tiles.colorMode = parseFloat( params.colorMode );
-	tiles.setResolutionFromRenderer( renderer );
+
+	tiles.setCamera( params.orthographic ? orthoCamera : camera );
+	tiles.setResolutionFromRenderer( orthoCamera, renderer );
+	tiles.setResolutionFromRenderer( camera, renderer );
+	tiles.deleteCamera( params.orthographic ? camera : orthoCamera );
 
 	offsetParent.rotation.set( 0, 0, 0 );
 	if ( params.up === '-Z' ) {

+ 89 - 29
src/three/TilesRenderer.js

@@ -31,25 +31,12 @@ function emptyRaycast() {}
 
 export class TilesRenderer extends TilesRendererBase {
 
-	get camera() {
-
-		return this.cameras[ 0 ];
-
-	}
-
-	set camera( camera ) {
-
-		const cameras = this.cameras;
-		cameras.length = 1;
-		cameras[ 0 ] = camera;
-
-	}
-
 	constructor( ...args ) {
 
 		super( ...args );
 		this.group = new TilesGroup( this );
 		this.cameras = [];
+		this.cameraMap = new Map();
 		this.resolution = new Vector2();
 		this.cameraInfo = [];
 		this.activeTiles = new Set();
@@ -110,11 +97,78 @@ export class TilesRenderer extends TilesRendererBase {
 
 	}
 
-	setResolutionFromRenderer( renderer ) {
+	hasCamera( camera ) {
+
+		return this.cameraMap.has( camera );
+
+	}
+
+	setCamera( camera ) {
+
+		const cameras = this.cameras;
+		const cameraMap = this.cameraMap;
+		if ( ! cameraMap.has( camera ) ) {
+
+			cameraMap.set( camera, new Vector2() );
+			cameras.push( camera );
+			return true;
+
+		}
+		return false;
+
+	}
+
+	setResolution( camera, xOrVec, y ) {
 
-		const resolution = this.resolution;
+		const cameraMap = this.cameraMap;
+		if ( ! cameraMap.has( camera ) ) {
+
+			return false;
+
+		}
+
+		if ( xOrVec instanceof Vector2 ) {
+
+			cameraMap.get( camera ).copy( xOrVec );
+
+		} else {
+
+			cameraMap.get( camera ).set( xOrVec, y );
+
+		}
+		return true;
+
+	}
+
+	setResolutionFromRenderer( camera, renderer ) {
+
+		const cameraMap = this.cameraMap;
+		if ( ! cameraMap.has( camera ) ) {
+
+			return false;
+
+		}
+
+		const resolution = cameraMap.get( camera );
 		renderer.getSize( resolution );
 		resolution.multiplyScalar( renderer.getPixelRatio() );
+		return true;
+
+	}
+
+	deleteCamera( camera ) {
+
+		const cameras = this.cameras;
+		const cameraMap = this.cameraMap;
+		if ( cameraMap.has( camera ) ) {
+
+			const index = cameras.indexOf( camera );
+			cameras.splice( index, 1 );
+			cameraMap.delete( camera );
+			return true;
+
+		}
+		return false;
 
 	}
 
@@ -123,8 +177,8 @@ export class TilesRenderer extends TilesRendererBase {
 
 		const group = this.group;
 		const cameras = this.cameras;
+		const cameraMap = this.cameraMap;
 		const cameraInfo = this.cameraInfo;
-		const resolution = this.resolution;
 
 		if ( cameras.length === 0 ) {
 
@@ -133,13 +187,6 @@ export class TilesRenderer extends TilesRendererBase {
 
 		}
 
-		if ( resolution.width === 0 || resolution.height === 0 ) {
-
-			console.warn( 'TilesRenderer: resolution for error calculation is not set. Cannot updated 3d tiles.' );
-			return;
-
-		}
-
 		// automatically scale the array of cameraInfo to match the cameras
 		while ( cameraInfo.length > cameras.length ) {
 
@@ -155,11 +202,13 @@ export class TilesRenderer extends TilesRendererBase {
 				sseDenominator: - 1,
 				position: new Vector3(),
 				invScale: - 1,
+				pixelSize: 0,
 
 			} );
 
 		}
 
+		// extract scale of group container
 		tempMat2.getInverse( group.matrixWorld );
 
 		let invScale;
@@ -179,6 +228,13 @@ export class TilesRenderer extends TilesRendererBase {
 			const info = cameraInfo[ i ];
 			const frustum = info.frustum;
 			const position = info.position;
+			const resolution = cameraMap.get( camera );
+
+			if ( resolution.width === 0 || resolution.height === 0 ) {
+
+				console.warn( 'TilesRenderer: resolution for error calculation is not set. Cannot updated 3d tiles.' );
+
+			}
 
 			if ( camera.isPerspectiveCamera ) {
 
@@ -186,6 +242,14 @@ export class TilesRenderer extends TilesRendererBase {
 
 			}
 
+			if ( camera.isOrthographicCamera ) {
+
+				const w = camera.right - camera.left;
+				const h = camera.top - camera.bottom;
+				info.pixelSize = Math.max( h / resolution.height, w / resolution.width );
+
+			}
+
 			info.invScale = invScale;
 
 			tempMat.copy( group.matrixWorld );
@@ -520,8 +584,6 @@ export class TilesRenderer extends TilesRendererBase {
 
 		// TODO: Use the content bounding volume here?
 		const boundingVolume = tile.boundingVolume;
-		const resolution = this.resolution;
-
 		if ( 'box' in boundingVolume ) {
 
 			const boundingBox = cached.box;
@@ -543,9 +605,7 @@ export class TilesRenderer extends TilesRendererBase {
 				let error;
 				if ( camera.isOrthographicCamera ) {
 
-					const w = camera.right - camera.left;
-					const h = camera.top - camera.bottom;
-					const pixelSize = Math.max( h / resolution.height, w / resolution.width );
+					const pixelSize = info.pixelSize;
 					error = tile.geometricError / ( pixelSize * invScale );
 
 				} else {