Skip to content

Commit

Permalink
Update gated logic, three.js (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson authored Dec 24, 2024
1 parent d37208d commit ac0449f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 124 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"vite": "^5.3.1"
},
"peerDependencies": {
"three": ">=0.123.0"
"three": ">=0.166.0"
},
"optionalDependencies": {
"@react-three/fiber": "^8.17.9",
Expand Down
83 changes: 10 additions & 73 deletions src/plugins/three/gltf/metadata/utilities/TextureReadUtility.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 );

}

Expand All @@ -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 );

}

Expand Down
30 changes: 0 additions & 30 deletions src/three/TilesRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
Euler,
LoadingManager,
EventDispatcher,
REVISION,
} from 'three';
import { raycastTraverse, raycastTraverseFirstHit } from './raycastTraverse.js';
import { readMagicBytes } from '../utilities/readMagicBytes.js';
Expand All @@ -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();
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 = [];
Expand Down
23 changes: 4 additions & 19 deletions src/three/raycastTraverse.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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 );

}

Expand Down

0 comments on commit ac0449f

Please sign in to comment.