From ac0449fb90afa81f4f736c19ef9783575e9aa894 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Tue, 24 Dec 2024 23:34:59 +0900 Subject: [PATCH] Update gated logic, three.js (#895) --- package-lock.json | 2 +- package.json | 2 +- .../metadata/utilities/TextureReadUtility.js | 83 +++---------------- src/three/TilesRenderer.js | 30 ------- src/three/raycastTraverse.js | 23 +---- 5 files changed, 16 insertions(+), 124 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4b676c011..00191490f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -44,7 +44,7 @@ "react-dom": "^18.3.1" }, "peerDependencies": { - "three": ">=0.123.0" + "three": ">=0.166.0" } }, "node_modules/@ampproject/remapping": { diff --git a/package.json b/package.json index 4f3631fb2..6abaf9ba5 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,7 @@ "vite": "^5.3.1" }, "peerDependencies": { - "three": ">=0.123.0" + "three": ">=0.166.0" }, "optionalDependencies": { "@react-three/fiber": "^8.17.9", diff --git a/src/plugins/three/gltf/metadata/utilities/TextureReadUtility.js b/src/plugins/three/gltf/metadata/utilities/TextureReadUtility.js index 7b95e2b98..be9d43305 100644 --- a/src/plugins/three/gltf/metadata/utilities/TextureReadUtility.js +++ b/src/plugins/three/gltf/metadata/utilities/TextureReadUtility.js @@ -1,11 +1,7 @@ -import { WebGLRenderTarget, WebGLRenderer, Box2, Vector2, Vector4, ShaderMaterial, REVISION, CustomBlending, ZeroFactor, OneFactor } from 'three'; +import { WebGLRenderTarget, WebGLRenderer, Box2, Vector2, ShaderMaterial, CustomBlending, ZeroFactor, OneFactor } from 'three'; import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js'; -const REVISION_GEQ_165 = parseInt( REVISION ) >= 165; -const REVISION_GEQ_166 = parseInt( REVISION ) >= 166; const _box = /* @__PURE__ */ new Box2(); -const _currentScissor = /* @__PURE__ */ new Vector4(); -const _pos = /* @__PURE__ */ new Vector2(); // Utility for reading sets of individual pixel values from textures export const TextureReadUtility = new ( class { @@ -64,15 +60,7 @@ export const TextureReadUtility = new ( class { readDataAsync( buffer ) { const { _renderer, _target } = this; - if ( REVISION_GEQ_165 ) { - - return _renderer.readRenderTargetPixelsAsync( _target, 0, 0, buffer.length / 4, 1, buffer ); - - } else { - - return Promise.resolve().then( () => this.readData( buffer ) ); - - } + return _renderer.readRenderTargetPixelsAsync( _target, 0, 0, buffer.length / 4, 1, buffer ); } @@ -88,66 +76,15 @@ export const TextureReadUtility = new ( class { // takes the texture, pixel to read from, and pixel to render in to renderPixelToTarget( texture, pixel, dstPixel ) { - const { _quad, _renderer, _target, _texTarget } = this; - - if ( REVISION_GEQ_166 ) { - - // copies the pixel directly to the target buffer - _box.min.copy( pixel ); - _box.max.copy( pixel ); - _box.max.x += 1; - _box.max.y += 1; - _renderer.initRenderTarget( _target ); - _renderer.copyTextureToTexture( texture, _target.texture, _box, dstPixel, 0 ); - - } else { - - // save state - const currentAutoClear = _renderer.autoClear; - const currentTarget = _renderer.getRenderTarget(); - const currentScissorTest = _renderer.getScissorTest(); - _renderer.getScissor( _currentScissor ); - - // initialize the render target - _texTarget.setSize( texture.image.width, texture.image.height ); - _renderer.setRenderTarget( _texTarget ); - - // render the data - _pos.set( 0, 0 ); - if ( REVISION_GEQ_165 ) { - - _renderer.copyTextureToTexture( texture, _texTarget.texture, null, _pos ); - - } else { - - _renderer.copyTextureToTexture( _pos, texture, _texTarget.texture ); - - } - - _quad.material.uniforms.map.value = _texTarget.texture; - _quad.material.uniforms.pixel.value.copy( pixel ); - - // if we set the render target after setting scissor state it is reset - _renderer.setRenderTarget( _target ); - - // init state - _renderer.setScissorTest( true ); - _renderer.setScissor( dstPixel.x, dstPixel.y, 1, 1 ); - _renderer.autoClear = false; - - // render - _quad.render( _renderer ); - - // reset state - _renderer.setScissorTest( currentScissorTest ); - _renderer.setScissor( _currentScissor ); - _renderer.setRenderTarget( currentTarget ); - _renderer.autoClear = currentAutoClear; - - // remove the memory - _texTarget.dispose(); + const { _renderer, _target } = this; - } + // copies the pixel directly to the target buffer + _box.min.copy( pixel ); + _box.max.copy( pixel ); + _box.max.x += 1; + _box.max.y += 1; + _renderer.initRenderTarget( _target ); + _renderer.copyTextureToTexture( texture, _target.texture, _box, dstPixel, 0 ); } diff --git a/src/three/TilesRenderer.js b/src/three/TilesRenderer.js index 4604ed881..caaa7471f 100644 --- a/src/three/TilesRenderer.js +++ b/src/three/TilesRenderer.js @@ -12,7 +12,6 @@ import { Euler, LoadingManager, EventDispatcher, - REVISION, } from 'three'; import { raycastTraverse, raycastTraverseFirstHit } from './raycastTraverse.js'; import { readMagicBytes } from '../utilities/readMagicBytes.js'; @@ -25,7 +24,6 @@ const _mat = new Matrix4(); const _euler = new Euler(); // In three.js r165 and higher raycast traversal can be ended early -const REVISION_LESS_165 = parseInt( REVISION ) < 165; const INITIAL_FRUSTUM_CULLED = Symbol( 'INITIAL_FRUSTUM_CULLED' ); const tempMat = new Matrix4(); const tempMat2 = new Matrix4(); @@ -108,23 +106,6 @@ export class TilesRenderer extends TilesRendererBase { // saved for event dispatcher functions this._listeners = {}; - if ( REVISION_LESS_165 ) { - - // Setting up the override raycasting function to be used by - // 3D objects created by this renderer - const tilesRenderer = this; - this._overridenRaycast = function ( raycaster, intersects ) { - - if ( ! tilesRenderer.optimizeRaycast ) { - - Object.getPrototypeOf( this ).raycast.call( this, raycaster, intersects ); - - } - - }; - - } - } addEventListener( ...args ) { @@ -740,17 +721,6 @@ export class TilesRenderer extends TilesRendererBase { } ); updateFrustumCulled( scene, ! this.autoDisableRendererCulling ); - if ( REVISION_LESS_165 ) { - - // We handle raycasting in a custom way so remove it from here - scene.traverse( c => { - - c.raycast = this._overridenRaycast; - - } ); - - } - const materials = []; const geometry = []; const textures = []; diff --git a/src/three/raycastTraverse.js b/src/three/raycastTraverse.js index 9aa49e6cc..a225630b3 100644 --- a/src/three/raycastTraverse.js +++ b/src/three/raycastTraverse.js @@ -1,7 +1,6 @@ -import { Matrix4, Ray, Vector3, REVISION } from 'three'; +import { Matrix4, Ray, Vector3 } from 'three'; // In three.js r165 and higher raycast traversal can be ended early -const REVISION_LESS_165 = parseInt( REVISION ) < 165; const _mat = new Matrix4(); const _localRay = new Ray(); const _vec = new Vector3(); @@ -16,24 +15,10 @@ function distanceSort( a, b ) { function intersectTileScene( tile, raycaster, renderer, intersects ) { const { scene } = tile.cached; - if ( REVISION_LESS_165 ) { + const didRaycast = renderer.invokeOnePlugin( plugin => plugin.raycastTile && plugin.raycastTile( tile, scene, raycaster, intersects ) ); + if ( ! didRaycast ) { - // Don't intersect the box3 helpers because those are used for debugging - scene.traverse( c => { - - // We set the default raycast function to empty so three.js doesn't automatically cast against it - Object.getPrototypeOf( c ).raycast.call( c, raycaster, intersects ); - - } ); - - } else { - - const didRaycast = renderer.invokeOnePlugin( plugin => plugin.raycastTile && plugin.raycastTile( tile, scene, raycaster, intersects ) ); - if ( ! didRaycast ) { - - raycaster.intersectObject( scene, true, intersects ); - - } + raycaster.intersectObject( scene, true, intersects ); }