最新代码

This commit is contained in:
Teo
2025-07-29 11:22:30 +08:00
parent d503e64098
commit b01d143ea6
1490 changed files with 680232 additions and 28 deletions

View File

@ -0,0 +1,50 @@
class Animation {
constructor( nodes, info ) {
this.nodes = nodes;
this.info = info;
this.animationLoop = null;
this.requestId = null;
this._init();
}
_init() {
const update = ( time, frame ) => {
this.requestId = self.requestAnimationFrame( update );
if ( this.info.autoReset === true ) this.info.reset();
this.nodes.nodeFrame.update();
this.info.frame = this.nodes.nodeFrame.frameId;
if ( this.animationLoop !== null ) this.animationLoop( time, frame );
};
update();
}
dispose() {
self.cancelAnimationFrame( this.requestId );
this.requestId = null;
}
setAnimationLoop( callback ) {
this.animationLoop = callback;
}
}
export default Animation;

View File

@ -0,0 +1,75 @@
import DataMap from './DataMap.js';
import { AttributeType } from './Constants.js';
import { DynamicDrawUsage } from 'three';
class Attributes extends DataMap {
constructor( backend ) {
super();
this.backend = backend;
}
delete( attribute ) {
const attributeData = super.delete( attribute );
if ( attributeData !== undefined ) {
this.backend.destroyAttribute( attribute );
}
}
update( attribute, type ) {
const data = this.get( attribute );
if ( data.version === undefined ) {
if ( type === AttributeType.VERTEX ) {
this.backend.createAttribute( attribute );
} else if ( type === AttributeType.INDEX ) {
this.backend.createIndexAttribute( attribute );
} else if ( type === AttributeType.STORAGE ) {
this.backend.createStorageAttribute( attribute );
}
data.version = this._getBufferAttribute( attribute ).version;
} else {
const bufferAttribute = this._getBufferAttribute( attribute );
if ( data.version < bufferAttribute.version || bufferAttribute.usage === DynamicDrawUsage ) {
this.backend.updateAttribute( attribute );
data.version = bufferAttribute.version;
}
}
}
_getBufferAttribute( attribute ) {
if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
return attribute;
}
}
export default Attributes;

View File

@ -0,0 +1,195 @@
let vector2 = null;
let vector4 = null;
let color4 = null;
import Color4 from './Color4.js';
import { Vector2, Vector4, REVISION, createCanvasElement } from 'three';
class Backend {
constructor( parameters = {} ) {
this.parameters = Object.assign( {}, parameters );
this.data = new WeakMap();
this.renderer = null;
this.domElement = null;
}
async init( renderer ) {
this.renderer = renderer;
}
// render context
begin( renderContext ) { }
finish( renderContext ) { }
// render object
draw( renderObject, info ) { }
// program
createProgram( program ) { }
destroyProgram( program ) { }
// bindings
createBindings( renderObject ) { }
updateBindings( renderObject ) { }
// pipeline
createRenderPipeline( renderObject ) { }
createComputePipeline( computeNode, pipeline ) { }
destroyPipeline( pipeline ) { }
// cache key
needsRenderUpdate( renderObject ) { } // return Boolean ( fast test )
getRenderCacheKey( renderObject ) { } // return String
// node builder
createNodeBuilder( renderObject ) { } // return NodeBuilder (ADD IT)
// textures
createSampler( texture ) { }
createDefaultTexture( texture ) { }
createTexture( texture ) { }
copyTextureToBuffer( texture, x, y, width, height ) {}
// attributes
createAttribute( attribute ) { }
createIndexAttribute( attribute ) { }
updateAttribute( attribute ) { }
destroyAttribute( attribute ) { }
// canvas
getContext() { }
updateSize() { }
// utils
resolveTimestampAsync( renderContext, type ) { }
hasFeatureAsync( name ) { } // return Boolean
hasFeature( name ) { } // return Boolean
getInstanceCount( renderObject ) {
const { object, geometry } = renderObject;
return geometry.isInstancedBufferGeometry ? geometry.instanceCount : ( object.isInstancedMesh ? object.count : 1 );
}
getDrawingBufferSize() {
vector2 = vector2 || new Vector2();
return this.renderer.getDrawingBufferSize( vector2 );
}
getScissor() {
vector4 = vector4 || new Vector4();
return this.renderer.getScissor( vector4 );
}
setScissorTest( boolean ) { }
getClearColor() {
const renderer = this.renderer;
color4 = color4 || new Color4();
renderer.getClearColor( color4 );
color4.getRGB( color4, this.renderer.currentColorSpace );
return color4;
}
getDomElement() {
let domElement = this.domElement;
if ( domElement === null ) {
domElement = ( this.parameters.canvas !== undefined ) ? this.parameters.canvas : createCanvasElement();
// OffscreenCanvas does not have setAttribute, see #22811
if ( 'setAttribute' in domElement ) domElement.setAttribute( 'data-engine', `three.js r${REVISION} webgpu` );
this.domElement = domElement;
}
return domElement;
}
// resource properties
set( object, value ) {
this.data.set( object, value );
}
get( object ) {
let map = this.data.get( object );
if ( map === undefined ) {
map = {};
this.data.set( object, map );
}
return map;
}
has( object ) {
return this.data.has( object );
}
delete( object ) {
this.data.delete( object );
}
}
export default Backend;

View File

@ -0,0 +1,134 @@
import DataMap from './DataMap.js';
import Color4 from './Color4.js';
import { Mesh, SphereGeometry, BackSide, LinearSRGBColorSpace } from 'three';
import { vec4, context, normalWorld, backgroundBlurriness, backgroundIntensity, NodeMaterial, modelViewProjection } from '../../nodes/Nodes.js';
const _clearColor = new Color4();
class Background extends DataMap {
constructor( renderer, nodes ) {
super();
this.renderer = renderer;
this.nodes = nodes;
}
update( scene, renderList, renderContext ) {
const renderer = this.renderer;
const background = this.nodes.getBackgroundNode( scene ) || scene.background;
let forceClear = false;
if ( background === null ) {
// no background settings, use clear color configuration from the renderer
renderer._clearColor.getRGB( _clearColor, LinearSRGBColorSpace );
_clearColor.a = renderer._clearColor.a;
} else if ( background.isColor === true ) {
// background is an opaque color
background.getRGB( _clearColor, LinearSRGBColorSpace );
_clearColor.a = 1;
forceClear = true;
} else if ( background.isNode === true ) {
const sceneData = this.get( scene );
const backgroundNode = background;
_clearColor.copy( renderer._clearColor );
let backgroundMesh = sceneData.backgroundMesh;
if ( backgroundMesh === undefined ) {
const backgroundMeshNode = context( vec4( backgroundNode ).mul( backgroundIntensity ), {
// @TODO: Add Texture2D support using node context
getUV: () => normalWorld,
getTextureLevel: () => backgroundBlurriness
} );
let viewProj = modelViewProjection();
viewProj = viewProj.setZ( viewProj.w );
const nodeMaterial = new NodeMaterial();
nodeMaterial.side = BackSide;
nodeMaterial.depthTest = false;
nodeMaterial.depthWrite = false;
nodeMaterial.fog = false;
nodeMaterial.vertexNode = viewProj;
nodeMaterial.fragmentNode = backgroundMeshNode;
sceneData.backgroundMeshNode = backgroundMeshNode;
sceneData.backgroundMesh = backgroundMesh = new Mesh( new SphereGeometry( 1, 32, 32 ), nodeMaterial );
backgroundMesh.frustumCulled = false;
backgroundMesh.onBeforeRender = function ( renderer, scene, camera ) {
this.matrixWorld.copyPosition( camera.matrixWorld );
};
}
const backgroundCacheKey = backgroundNode.getCacheKey();
if ( sceneData.backgroundCacheKey !== backgroundCacheKey ) {
sceneData.backgroundMeshNode.node = vec4( backgroundNode ).mul( backgroundIntensity );
backgroundMesh.material.needsUpdate = true;
sceneData.backgroundCacheKey = backgroundCacheKey;
}
renderList.unshift( backgroundMesh, backgroundMesh.geometry, backgroundMesh.material, 0, 0, null );
} else {
console.error( 'THREE.Renderer: Unsupported background configuration.', background );
}
//
if ( renderer.autoClear === true || forceClear === true ) {
_clearColor.multiplyScalar( _clearColor.a );
const clearColorValue = renderContext.clearColorValue;
clearColorValue.r = _clearColor.r;
clearColorValue.g = _clearColor.g;
clearColorValue.b = _clearColor.b;
clearColorValue.a = _clearColor.a;
renderContext.depthClearValue = renderer._clearDepth;
renderContext.stencilClearValue = renderer._clearStencil;
renderContext.clearColor = renderer.autoClearColor === true;
renderContext.clearDepth = renderer.autoClearDepth === true;
renderContext.clearStencil = renderer.autoClearStencil === true;
} else {
renderContext.clearColor = false;
renderContext.clearDepth = false;
renderContext.clearStencil = false;
}
}
}
export default Background;

View File

@ -0,0 +1,25 @@
class Binding {
constructor( name = '' ) {
this.name = name;
this.visibility = 0;
}
setVisibility( visibility ) {
this.visibility |= visibility;
}
clone() {
return Object.assign( new this.constructor(), this );
}
}
export default Binding;

View File

@ -0,0 +1,173 @@
import DataMap from './DataMap.js';
import { AttributeType } from './Constants.js';
class Bindings extends DataMap {
constructor( backend, nodes, textures, attributes, pipelines, info ) {
super();
this.backend = backend;
this.textures = textures;
this.pipelines = pipelines;
this.attributes = attributes;
this.nodes = nodes;
this.info = info;
this.pipelines.bindings = this; // assign bindings to pipelines
}
getForRender( renderObject ) {
const bindings = renderObject.getBindings();
const data = this.get( renderObject );
if ( data.bindings !== bindings ) {
// each object defines an array of bindings (ubos, textures, samplers etc.)
data.bindings = bindings;
this._init( bindings );
this.backend.createBindings( bindings );
}
return data.bindings;
}
getForCompute( computeNode ) {
const data = this.get( computeNode );
if ( data.bindings === undefined ) {
const nodeBuilderState = this.nodes.getForCompute( computeNode );
const bindings = nodeBuilderState.bindings;
data.bindings = bindings;
this._init( bindings );
this.backend.createBindings( bindings );
}
return data.bindings;
}
updateForCompute( computeNode ) {
this._update( computeNode, this.getForCompute( computeNode ) );
}
updateForRender( renderObject ) {
this._update( renderObject, this.getForRender( renderObject ) );
}
_init( bindings ) {
for ( const binding of bindings ) {
if ( binding.isSampledTexture ) {
this.textures.updateTexture( binding.texture );
} else if ( binding.isStorageBuffer ) {
const attribute = binding.attribute;
this.attributes.update( attribute, AttributeType.STORAGE );
}
}
}
_update( object, bindings ) {
const { backend } = this;
let needsBindingsUpdate = false;
// iterate over all bindings and check if buffer updates or a new binding group is required
for ( const binding of bindings ) {
if ( binding.isNodeUniformsGroup ) {
const updated = this.nodes.updateGroup( binding );
if ( ! updated ) continue;
}
if ( binding.isUniformBuffer ) {
const updated = binding.update();
if ( updated ) {
backend.updateBinding( binding );
}
} else if ( binding.isSampledTexture ) {
const texture = binding.texture;
if ( binding.needsBindingsUpdate ) needsBindingsUpdate = true;
const updated = binding.update();
if ( updated ) {
this.textures.updateTexture( binding.texture );
}
if ( texture.isStorageTexture === true ) {
const textureData = this.get( texture );
if ( binding.store === true ) {
textureData.needsMipmap = true;
} else if ( texture.generateMipmaps === true && this.textures.needsMipmaps( texture ) && textureData.needsMipmap === true ) {
this.backend.generateMipmaps( texture );
textureData.needsMipmap = false;
}
}
}
}
if ( needsBindingsUpdate === true ) {
const pipeline = this.pipelines.getForRender( object );
this.backend.updateBindings( bindings, pipeline );
}
}
}
export default Bindings;

View File

@ -0,0 +1,38 @@
import Binding from './Binding.js';
import { getFloatLength } from './BufferUtils.js';
class Buffer extends Binding {
constructor( name, buffer = null ) {
super( name );
this.isBuffer = true;
this.bytesPerElement = Float32Array.BYTES_PER_ELEMENT;
this._buffer = buffer;
}
get byteLength() {
return getFloatLength( this._buffer.byteLength );
}
get buffer() {
return this._buffer;
}
update() {
return true;
}
}
export default Buffer;

View File

@ -0,0 +1,33 @@
import { GPU_CHUNK_BYTES } from './Constants.js';
function getFloatLength( floatLength ) {
// ensure chunk size alignment (STD140 layout)
return floatLength + ( ( GPU_CHUNK_BYTES - ( floatLength % GPU_CHUNK_BYTES ) ) % GPU_CHUNK_BYTES );
}
function getVectorLength( count, vectorLength = 4 ) {
const strideLength = getStrideLength( vectorLength );
const floatLength = strideLength * count;
return getFloatLength( floatLength );
}
function getStrideLength( vectorLength ) {
const strideLength = 4;
return vectorLength + ( ( strideLength - ( vectorLength % strideLength ) ) % strideLength );
}
export {
getFloatLength,
getVectorLength,
getStrideLength
};

View File

@ -0,0 +1,89 @@
export default class ChainMap {
constructor() {
this.weakMap = new WeakMap();
}
get( keys ) {
if ( Array.isArray( keys ) ) {
let map = this.weakMap;
for ( let i = 0; i < keys.length; i ++ ) {
map = map.get( keys[ i ] );
if ( map === undefined ) return undefined;
}
return map.get( keys[ keys.length - 1 ] );
} else {
return super.get( keys );
}
}
set( keys, value ) {
if ( Array.isArray( keys ) ) {
let map = this.weakMap;
for ( let i = 0; i < keys.length; i ++ ) {
const key = keys[ i ];
if ( map.has( key ) === false ) map.set( key, new WeakMap() );
map = map.get( key );
}
return map.set( keys[ keys.length - 1 ], value );
} else {
return super.set( keys, value );
}
}
delete( keys ) {
if ( Array.isArray( keys ) ) {
let map = this.weakMap;
for ( let i = 0; i < keys.length; i ++ ) {
map = map.get( keys[ i ] );
if ( map === undefined ) return false;
}
return map.delete( keys[ keys.length - 1 ] );
} else {
return super.delete( keys );
}
}
dispose() {
this.weakMap.clear();
}
}

View File

@ -0,0 +1,165 @@
import { Matrix3, Plane, Vector4 } from 'three';
const _plane = new Plane();
const _viewNormalMatrix = new Matrix3();
let _clippingContextVersion = 0;
class ClippingContext {
constructor() {
this.version = ++ _clippingContextVersion;
this.globalClippingCount = 0;
this.localClippingCount = 0;
this.localClippingEnabled = false;
this.localClipIntersection = false;
this.planes = [];
this.parentVersion = 0;
}
projectPlanes( source, offset ) {
const l = source.length;
const planes = this.planes;
for ( let i = 0; i < l; i ++ ) {
_plane.copy( source[ i ] ).applyMatrix4( this.viewMatrix, _viewNormalMatrix );
const v = planes[ offset + i ];
const normal = _plane.normal;
v.x = - normal.x;
v.y = - normal.y;
v.z = - normal.z;
v.w = _plane.constant;
}
}
updateGlobal( renderer, camera ) {
const rendererClippingPlanes = renderer.clippingPlanes;
this.viewMatrix = camera.matrixWorldInverse;
_viewNormalMatrix.getNormalMatrix( this.viewMatrix );
let update = false;
if ( Array.isArray( rendererClippingPlanes ) && rendererClippingPlanes.length !== 0 ) {
const l = rendererClippingPlanes.length;
if ( l !== this.globalClippingCount ) {
const planes = [];
for ( let i = 0; i < l; i ++ ) {
planes.push( new Vector4() );
}
this.globalClippingCount = l;
this.planes = planes;
update = true;
}
this.projectPlanes( rendererClippingPlanes, 0 );
} else if ( this.globalClippingCount !== 0 ) {
this.globalClippingCount = 0;
this.planes = [];
update = true;
}
if ( renderer.localClippingEnabled !== this.localClippingEnabled ) {
this.localClippingEnabled = renderer.localClippingEnabled;
update = true;
}
if ( update ) this.version = _clippingContextVersion ++;
}
update( parent, material ) {
let update = false;
if ( this !== parent && parent.version !== this.parentVersion ) {
this.globalClippingCount = material.isShadowNodeMaterial ? 0 : parent.globalClippingCount;
this.localClippingEnabled = parent.localClippingEnabled;
this.planes = Array.from( parent.planes );
this.parentVersion = parent.version;
this.viewMatrix = parent.viewMatrix;
update = true;
}
if ( this.localClippingEnabled ) {
const localClippingPlanes = material.clippingPlanes;
if ( ( Array.isArray( localClippingPlanes ) && localClippingPlanes.length !== 0 ) ) {
const l = localClippingPlanes.length;
const planes = this.planes;
const offset = this.globalClippingCount;
if ( update || l !== this.localClippingCount ) {
planes.length = offset + l;
for ( let i = 0; i < l; i ++ ) {
planes[ offset + i ] = new Vector4();
}
this.localClippingCount = l;
update = true;
}
this.projectPlanes( localClippingPlanes, offset );
} else if ( this.localClippingCount !== 0 ) {
this.localClippingCount = 0;
update = true;
}
if ( this.localClipIntersection !== material.clipIntersection ) {
this.localClipIntersection = material.clipIntersection;
update = true;
}
}
if ( update ) this.version = _clippingContextVersion ++;
}
}
export default ClippingContext;

View File

@ -0,0 +1,37 @@
import { Color } from 'three';
class Color4 extends Color {
constructor( r, g, b, a = 1 ) {
super( r, g, b );
this.a = a;
}
set( r, g, b, a = 1 ) {
this.a = a;
return super.set( r, g, b );
}
copy( color ) {
if ( color.a !== undefined ) this.a = color.a;
return super.copy( color );
}
clone() {
return new this.constructor( this.r, this.g, this.b, this.a );
}
}
export default Color4;

View File

@ -0,0 +1,17 @@
import Pipeline from './Pipeline.js';
class ComputePipeline extends Pipeline {
constructor( cacheKey, computeProgram ) {
super( cacheKey );
this.computeProgram = computeProgram;
this.isComputePipeline = true;
}
}
export default ComputePipeline;

View File

@ -0,0 +1,14 @@
export const AttributeType = {
VERTEX: 1,
INDEX: 2,
STORAGE: 4
};
// size of a chunk in bytes (STD140 layout)
export const GPU_CHUNK_BYTES = 16;
// @TODO: Move to src/constants.js
export const BlendColorFactor = 211;
export const OneMinusBlendColorFactor = 212;

View File

@ -0,0 +1,65 @@
import { WebGLCubeRenderTarget, Scene, CubeCamera, BoxGeometry, Mesh, BackSide, NoBlending, LinearFilter, LinearMipmapLinearFilter } from 'three';
import { equirectUV } from '../../nodes/utils/EquirectUVNode.js';
import { texture as TSL_Texture } from '../../nodes/accessors/TextureNode.js';
import { positionWorldDirection } from '../../nodes/accessors/PositionNode.js';
import { createNodeMaterialFromType } from '../../nodes/materials/NodeMaterial.js';
// @TODO: Consider rename WebGLCubeRenderTarget to just CubeRenderTarget
class CubeRenderTarget extends WebGLCubeRenderTarget {
constructor( size = 1, options = {} ) {
super( size, options );
this.isCubeRenderTarget = true;
}
fromEquirectangularTexture( renderer, texture ) {
const currentMinFilter = texture.minFilter;
const currentGenerateMipmaps = texture.generateMipmaps;
texture.generateMipmaps = true;
this.texture.type = texture.type;
this.texture.colorSpace = texture.colorSpace;
this.texture.generateMipmaps = texture.generateMipmaps;
this.texture.minFilter = texture.minFilter;
this.texture.magFilter = texture.magFilter;
const geometry = new BoxGeometry( 5, 5, 5 );
const uvNode = equirectUV( positionWorldDirection );
const material = createNodeMaterialFromType( 'MeshBasicNodeMaterial' );
material.colorNode = TSL_Texture( texture, uvNode, 0 );
material.side = BackSide;
material.blending = NoBlending;
const mesh = new Mesh( geometry, material );
const scene = new Scene();
scene.add( mesh );
// Avoid blurred poles
if ( texture.minFilter === LinearMipmapLinearFilter ) texture.minFilter = LinearFilter;
const camera = new CubeCamera( 1, 10, this );
camera.update( renderer, scene );
texture.minFilter = currentMinFilter;
texture.currentGenerateMipmaps = currentGenerateMipmaps;
mesh.geometry.dispose();
mesh.material.dispose();
return this;
}
}
export default CubeRenderTarget;

View File

@ -0,0 +1,54 @@
class DataMap {
constructor() {
this.data = new WeakMap();
}
get( object ) {
let map = this.data.get( object );
if ( map === undefined ) {
map = {};
this.data.set( object, map );
}
return map;
}
delete( object ) {
let map;
if ( this.data.has( object ) ) {
map = this.data.get( object );
this.data.delete( object );
}
return map;
}
has( object ) {
return this.data.has( object );
}
dispose() {
this.data = new WeakMap();
}
}
export default DataMap;

View File

@ -0,0 +1,215 @@
import DataMap from './DataMap.js';
import { AttributeType } from './Constants.js';
import { Uint32BufferAttribute, Uint16BufferAttribute } from 'three';
function arrayNeedsUint32( array ) {
// assumes larger values usually on last
for ( let i = array.length - 1; i >= 0; -- i ) {
if ( array[ i ] >= 65535 ) return true; // account for PRIMITIVE_RESTART_FIXED_INDEX, #24565
}
return false;
}
function getWireframeVersion( geometry ) {
return ( geometry.index !== null ) ? geometry.index.version : geometry.attributes.position.version;
}
function getWireframeIndex( geometry ) {
const indices = [];
const geometryIndex = geometry.index;
const geometryPosition = geometry.attributes.position;
if ( geometryIndex !== null ) {
const array = geometryIndex.array;
for ( let i = 0, l = array.length; i < l; i += 3 ) {
const a = array[ i + 0 ];
const b = array[ i + 1 ];
const c = array[ i + 2 ];
indices.push( a, b, b, c, c, a );
}
} else {
const array = geometryPosition.array;
for ( let i = 0, l = ( array.length / 3 ) - 1; i < l; i += 3 ) {
const a = i + 0;
const b = i + 1;
const c = i + 2;
indices.push( a, b, b, c, c, a );
}
}
const attribute = new ( arrayNeedsUint32( indices ) ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 );
attribute.version = getWireframeVersion( geometry );
return attribute;
}
class Geometries extends DataMap {
constructor( attributes, info ) {
super();
this.attributes = attributes;
this.info = info;
this.wireframes = new WeakMap();
this.attributeCall = new WeakMap();
}
has( renderObject ) {
const geometry = renderObject.geometry;
return super.has( geometry ) && this.get( geometry ).initialized === true;
}
updateForRender( renderObject ) {
if ( this.has( renderObject ) === false ) this.initGeometry( renderObject );
this.updateAttributes( renderObject );
}
initGeometry( renderObject ) {
const geometry = renderObject.geometry;
const geometryData = this.get( geometry );
geometryData.initialized = true;
this.info.memory.geometries ++;
const onDispose = () => {
this.info.memory.geometries --;
const index = geometry.index;
const geometryAttributes = renderObject.getAttributes();
if ( index !== null ) {
this.attributes.delete( index );
}
for ( const geometryAttribute of geometryAttributes ) {
this.attributes.delete( geometryAttribute );
}
const wireframeAttribute = this.wireframes.get( geometry );
if ( wireframeAttribute !== undefined ) {
this.attributes.delete( wireframeAttribute );
}
geometry.removeEventListener( 'dispose', onDispose );
};
geometry.addEventListener( 'dispose', onDispose );
}
updateAttributes( renderObject ) {
const attributes = renderObject.getAttributes();
for ( const attribute of attributes ) {
this.updateAttribute( attribute, AttributeType.VERTEX );
}
const index = this.getIndex( renderObject );
if ( index !== null ) {
this.updateAttribute( index, AttributeType.INDEX );
}
}
updateAttribute( attribute, type ) {
const callId = this.info.render.calls;
if ( this.attributeCall.get( attribute ) !== callId ) {
this.attributes.update( attribute, type );
this.attributeCall.set( attribute, callId );
}
}
getIndex( renderObject ) {
const { geometry, material } = renderObject;
let index = geometry.index;
if ( material.wireframe === true ) {
const wireframes = this.wireframes;
let wireframeAttribute = wireframes.get( geometry );
if ( wireframeAttribute === undefined ) {
wireframeAttribute = getWireframeIndex( geometry );
wireframes.set( geometry, wireframeAttribute );
} else if ( wireframeAttribute.version !== getWireframeVersion( geometry ) ) {
this.attributes.delete( wireframeAttribute );
wireframeAttribute = getWireframeIndex( geometry );
wireframes.set( geometry, wireframeAttribute );
}
index = wireframeAttribute;
}
return index;
}
}
export default Geometries;

View File

@ -0,0 +1,99 @@
class Info {
constructor() {
this.autoReset = true;
this.frame = 0;
this.calls = 0;
this.render = {
calls: 0,
drawCalls: 0,
triangles: 0,
points: 0,
lines: 0,
timestamp: 0
};
this.compute = {
calls: 0,
computeCalls: 0,
timestamp: 0
};
this.memory = {
geometries: 0,
textures: 0
};
}
update( object, count, instanceCount ) {
this.render.drawCalls ++;
if ( object.isMesh || object.isSprite ) {
this.render.triangles += instanceCount * ( count / 3 );
} else if ( object.isPoints ) {
this.render.points += instanceCount * count;
} else if ( object.isLineSegments ) {
this.render.lines += instanceCount * ( count / 2 );
} else if ( object.isLine ) {
this.render.lines += instanceCount * ( count - 1 );
} else {
console.error( 'THREE.WebGPUInfo: Unknown object type.' );
}
}
updateTimestamp( type, time ) {
this[ type ].timestamp += time;
}
reset() {
this.render.drawCalls = 0;
this.compute.computeCalls = 0;
this.render.triangles = 0;
this.render.points = 0;
this.render.lines = 0;
this.render.timestamp = 0;
this.compute.timestamp = 0;
}
dispose() {
this.reset();
this.calls = 0;
this.render.calls = 0;
this.compute.calls = 0;
this.render.timestamp = 0;
this.compute.timestamp = 0;
this.memory.geometries = 0;
this.memory.textures = 0;
}
}
export default Info;

View File

@ -0,0 +1,13 @@
class Pipeline {
constructor( cacheKey ) {
this.cacheKey = cacheKey;
this.usedTimes = 0;
}
}
export default Pipeline;

View File

@ -0,0 +1,322 @@
import DataMap from './DataMap.js';
import RenderPipeline from './RenderPipeline.js';
import ComputePipeline from './ComputePipeline.js';
import ProgrammableStage from './ProgrammableStage.js';
class Pipelines extends DataMap {
constructor( backend, nodes ) {
super();
this.backend = backend;
this.nodes = nodes;
this.bindings = null; // set by the bindings
this.caches = new Map();
this.programs = {
vertex: new Map(),
fragment: new Map(),
compute: new Map()
};
}
getForCompute( computeNode, bindings ) {
const { backend } = this;
const data = this.get( computeNode );
if ( this._needsComputeUpdate( computeNode ) ) {
const previousPipeline = data.pipeline;
if ( previousPipeline ) {
previousPipeline.usedTimes --;
previousPipeline.computeProgram.usedTimes --;
}
// get shader
const nodeBuilderState = this.nodes.getForCompute( computeNode );
// programmable stage
let stageCompute = this.programs.compute.get( nodeBuilderState.computeShader );
if ( stageCompute === undefined ) {
if ( previousPipeline && previousPipeline.computeProgram.usedTimes === 0 ) this._releaseProgram( previousPipeline.computeProgram );
stageCompute = new ProgrammableStage( nodeBuilderState.computeShader, 'compute', nodeBuilderState.transforms, nodeBuilderState.nodeAttributes );
this.programs.compute.set( nodeBuilderState.computeShader, stageCompute );
backend.createProgram( stageCompute );
}
// determine compute pipeline
const cacheKey = this._getComputeCacheKey( computeNode, stageCompute );
let pipeline = this.caches.get( cacheKey );
if ( pipeline === undefined ) {
if ( previousPipeline && previousPipeline.usedTimes === 0 ) this._releasePipeline( computeNode );
pipeline = this._getComputePipeline( computeNode, stageCompute, cacheKey, bindings );
}
// keep track of all used times
pipeline.usedTimes ++;
stageCompute.usedTimes ++;
//
data.version = computeNode.version;
data.pipeline = pipeline;
}
return data.pipeline;
}
getForRender( renderObject, promises = null ) {
const { backend } = this;
const data = this.get( renderObject );
if ( this._needsRenderUpdate( renderObject ) ) {
const previousPipeline = data.pipeline;
if ( previousPipeline ) {
previousPipeline.usedTimes --;
previousPipeline.vertexProgram.usedTimes --;
previousPipeline.fragmentProgram.usedTimes --;
}
// get shader
const nodeBuilderState = renderObject.getNodeBuilderState();
// programmable stages
let stageVertex = this.programs.vertex.get( nodeBuilderState.vertexShader );
if ( stageVertex === undefined ) {
if ( previousPipeline && previousPipeline.vertexProgram.usedTimes === 0 ) this._releaseProgram( previousPipeline.vertexProgram );
stageVertex = new ProgrammableStage( nodeBuilderState.vertexShader, 'vertex' );
this.programs.vertex.set( nodeBuilderState.vertexShader, stageVertex );
backend.createProgram( stageVertex );
}
let stageFragment = this.programs.fragment.get( nodeBuilderState.fragmentShader );
if ( stageFragment === undefined ) {
if ( previousPipeline && previousPipeline.fragmentProgram.usedTimes === 0 ) this._releaseProgram( previousPipeline.fragmentProgram );
stageFragment = new ProgrammableStage( nodeBuilderState.fragmentShader, 'fragment' );
this.programs.fragment.set( nodeBuilderState.fragmentShader, stageFragment );
backend.createProgram( stageFragment );
}
// determine render pipeline
const cacheKey = this._getRenderCacheKey( renderObject, stageVertex, stageFragment );
let pipeline = this.caches.get( cacheKey );
if ( pipeline === undefined ) {
if ( previousPipeline && previousPipeline.usedTimes === 0 ) this._releasePipeline( previousPipeline );
pipeline = this._getRenderPipeline( renderObject, stageVertex, stageFragment, cacheKey, promises );
} else {
renderObject.pipeline = pipeline;
}
// keep track of all used times
pipeline.usedTimes ++;
stageVertex.usedTimes ++;
stageFragment.usedTimes ++;
//
data.pipeline = pipeline;
}
return data.pipeline;
}
delete( object ) {
const pipeline = this.get( object ).pipeline;
if ( pipeline ) {
// pipeline
pipeline.usedTimes --;
if ( pipeline.usedTimes === 0 ) this._releasePipeline( pipeline );
// programs
if ( pipeline.isComputePipeline ) {
pipeline.computeProgram.usedTimes --;
if ( pipeline.computeProgram.usedTimes === 0 ) this._releaseProgram( pipeline.computeProgram );
} else {
pipeline.fragmentProgram.usedTimes --;
pipeline.vertexProgram.usedTimes --;
if ( pipeline.vertexProgram.usedTimes === 0 ) this._releaseProgram( pipeline.vertexProgram );
if ( pipeline.fragmentProgram.usedTimes === 0 ) this._releaseProgram( pipeline.fragmentProgram );
}
}
super.delete( object );
}
dispose() {
super.dispose();
this.caches = new Map();
this.programs = {
vertex: new Map(),
fragment: new Map(),
compute: new Map()
};
}
updateForRender( renderObject ) {
this.getForRender( renderObject );
}
_getComputePipeline( computeNode, stageCompute, cacheKey, bindings ) {
// check for existing pipeline
cacheKey = cacheKey || this._getComputeCacheKey( computeNode, stageCompute );
let pipeline = this.caches.get( cacheKey );
if ( pipeline === undefined ) {
pipeline = new ComputePipeline( cacheKey, stageCompute );
this.caches.set( cacheKey, pipeline );
this.backend.createComputePipeline( pipeline, bindings );
}
return pipeline;
}
_getRenderPipeline( renderObject, stageVertex, stageFragment, cacheKey, promises ) {
// check for existing pipeline
cacheKey = cacheKey || this._getRenderCacheKey( renderObject, stageVertex, stageFragment );
let pipeline = this.caches.get( cacheKey );
if ( pipeline === undefined ) {
pipeline = new RenderPipeline( cacheKey, stageVertex, stageFragment );
this.caches.set( cacheKey, pipeline );
renderObject.pipeline = pipeline;
this.backend.createRenderPipeline( renderObject, promises );
}
return pipeline;
}
_getComputeCacheKey( computeNode, stageCompute ) {
return computeNode.id + ',' + stageCompute.id;
}
_getRenderCacheKey( renderObject, stageVertex, stageFragment ) {
return stageVertex.id + ',' + stageFragment.id + ',' + this.backend.getRenderCacheKey( renderObject );
}
_releasePipeline( pipeline ) {
this.caches.delete( pipeline.cacheKey );
}
_releaseProgram( program ) {
const code = program.code;
const stage = program.stage;
this.programs[ stage ].delete( code );
}
_needsComputeUpdate( computeNode ) {
const data = this.get( computeNode );
return data.pipeline === undefined || data.version !== computeNode.version;
}
_needsRenderUpdate( renderObject ) {
const data = this.get( renderObject );
return data.pipeline === undefined || this.backend.needsRenderUpdate( renderObject );
}
}
export default Pipelines;

View File

@ -0,0 +1,33 @@
import { vec4, NodeMaterial } from '../../nodes/Nodes.js';
import QuadMesh from '../../objects/QuadMesh.js';
const quadMesh = new QuadMesh( new NodeMaterial() );
class PostProcessing {
constructor( renderer, outputNode = vec4( 0, 0, 1, 1 ) ) {
this.renderer = renderer;
this.outputNode = outputNode;
}
render() {
quadMesh.material.fragmentNode = this.outputNode;
quadMesh.render( this.renderer );
}
renderAsync() {
quadMesh.material.fragmentNode = this.outputNode;
return quadMesh.renderAsync( this.renderer );
}
}
export default PostProcessing;

View File

@ -0,0 +1,20 @@
let _id = 0;
class ProgrammableStage {
constructor( code, type, transforms = null, attributes = null ) {
this.id = _id ++;
this.code = code;
this.stage = type;
this.transforms = transforms;
this.attributes = attributes;
this.usedTimes = 0;
}
}
export default ProgrammableStage;

View File

@ -0,0 +1,43 @@
import { Vector4 } from 'three';
let id = 0;
class RenderContext {
constructor() {
this.id = id ++;
this.color = true;
this.clearColor = true;
this.clearColorValue = { r: 0, g: 0, b: 0, a: 1 };
this.depth = true;
this.clearDepth = true;
this.clearDepthValue = 1;
this.stencil = false;
this.clearStencil = true;
this.clearStencilValue = 1;
this.viewport = false;
this.viewportValue = new Vector4();
this.scissor = false;
this.scissorValue = new Vector4();
this.textures = null;
this.depthTexture = null;
this.activeCubeFace = 0;
this.sampleCount = 1;
this.width = 0;
this.height = 0;
this.isRenderContext = true;
}
}
export default RenderContext;

View File

@ -0,0 +1,63 @@
import ChainMap from './ChainMap.js';
import RenderContext from './RenderContext.js';
class RenderContexts {
constructor() {
this.chainMaps = {};
}
get( scene, camera, renderTarget = null ) {
const chainKey = [ scene, camera ];
let attachmentState;
if ( renderTarget === null ) {
attachmentState = 'default';
} else {
const format = renderTarget.texture.format;
const count = renderTarget.count;
attachmentState = `${ count }:${ format }:${ renderTarget.samples }:${ renderTarget.depthBuffer }:${ renderTarget.stencilBuffer }`;
}
const chainMap = this.getChainMap( attachmentState );
let renderState = chainMap.get( chainKey );
if ( renderState === undefined ) {
renderState = new RenderContext();
chainMap.set( chainKey, renderState );
}
if ( renderTarget !== null ) renderState.sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples;
return renderState;
}
getChainMap( attachmentState ) {
return this.chainMaps[ attachmentState ] || ( this.chainMaps[ attachmentState ] = new ChainMap() );
}
dispose() {
this.chainMaps = {};
}
}
export default RenderContexts;

View File

@ -0,0 +1,186 @@
import { LightsNode } from '../../nodes/Nodes.js';
function painterSortStable( a, b ) {
if ( a.groupOrder !== b.groupOrder ) {
return a.groupOrder - b.groupOrder;
} else if ( a.renderOrder !== b.renderOrder ) {
return a.renderOrder - b.renderOrder;
} else if ( a.material.id !== b.material.id ) {
return a.material.id - b.material.id;
} else if ( a.z !== b.z ) {
return a.z - b.z;
} else {
return a.id - b.id;
}
}
function reversePainterSortStable( a, b ) {
if ( a.groupOrder !== b.groupOrder ) {
return a.groupOrder - b.groupOrder;
} else if ( a.renderOrder !== b.renderOrder ) {
return a.renderOrder - b.renderOrder;
} else if ( a.z !== b.z ) {
return b.z - a.z;
} else {
return a.id - b.id;
}
}
class RenderList {
constructor() {
this.renderItems = [];
this.renderItemsIndex = 0;
this.opaque = [];
this.transparent = [];
this.lightsNode = new LightsNode( [] );
this.lightsArray = [];
this.occlusionQueryCount = 0;
}
begin() {
this.renderItemsIndex = 0;
this.opaque.length = 0;
this.transparent.length = 0;
this.lightsArray.length = 0;
this.occlusionQueryCount = 0;
return this;
}
getNextRenderItem( object, geometry, material, groupOrder, z, group ) {
let renderItem = this.renderItems[ this.renderItemsIndex ];
if ( renderItem === undefined ) {
renderItem = {
id: object.id,
object: object,
geometry: geometry,
material: material,
groupOrder: groupOrder,
renderOrder: object.renderOrder,
z: z,
group: group
};
this.renderItems[ this.renderItemsIndex ] = renderItem;
} else {
renderItem.id = object.id;
renderItem.object = object;
renderItem.geometry = geometry;
renderItem.material = material;
renderItem.groupOrder = groupOrder;
renderItem.renderOrder = object.renderOrder;
renderItem.z = z;
renderItem.group = group;
}
this.renderItemsIndex ++;
return renderItem;
}
push( object, geometry, material, groupOrder, z, group ) {
const renderItem = this.getNextRenderItem( object, geometry, material, groupOrder, z, group );
if ( object.occlusionTest === true ) this.occlusionQueryCount ++;
( material.transparent === true || material.transmission > 0 ? this.transparent : this.opaque ).push( renderItem );
}
unshift( object, geometry, material, groupOrder, z, group ) {
const renderItem = this.getNextRenderItem( object, geometry, material, groupOrder, z, group );
( material.transparent === true ? this.transparent : this.opaque ).unshift( renderItem );
}
pushLight( light ) {
this.lightsArray.push( light );
}
getLightsNode() {
return this.lightsNode.fromLights( this.lightsArray );
}
sort( customOpaqueSort, customTransparentSort ) {
if ( this.opaque.length > 1 ) this.opaque.sort( customOpaqueSort || painterSortStable );
if ( this.transparent.length > 1 ) this.transparent.sort( customTransparentSort || reversePainterSortStable );
}
finish() {
// update lights
this.lightsNode.fromLights( this.lightsArray );
// Clear references from inactive renderItems in the list
for ( let i = this.renderItemsIndex, il = this.renderItems.length; i < il; i ++ ) {
const renderItem = this.renderItems[ i ];
if ( renderItem.id === null ) break;
renderItem.id = null;
renderItem.object = null;
renderItem.geometry = null;
renderItem.material = null;
renderItem.groupOrder = null;
renderItem.renderOrder = null;
renderItem.z = null;
renderItem.group = null;
}
}
}
export default RenderList;

View File

@ -0,0 +1,38 @@
import ChainMap from './ChainMap.js';
import RenderList from './RenderList.js';
class RenderLists {
constructor() {
this.lists = new ChainMap();
}
get( scene, camera ) {
const lists = this.lists;
const keys = [ scene, camera ];
let list = lists.get( keys );
if ( list === undefined ) {
list = new RenderList();
lists.set( keys, list );
}
return list;
}
dispose() {
this.lists = new ChainMap();
}
}
export default RenderLists;

View File

@ -0,0 +1,263 @@
import ClippingContext from './ClippingContext.js';
let id = 0;
function getKeys( obj ) {
const keys = Object.keys( obj );
let proto = Object.getPrototypeOf( obj );
while ( proto ) {
const descriptors = Object.getOwnPropertyDescriptors( proto );
for ( const key in descriptors ) {
if ( descriptors[ key ] !== undefined ) {
const descriptor = descriptors[ key ];
if ( descriptor && typeof descriptor.get === 'function' ) {
keys.push( key );
}
}
}
proto = Object.getPrototypeOf( proto );
}
return keys;
}
export default class RenderObject {
constructor( nodes, geometries, renderer, object, material, scene, camera, lightsNode, renderContext ) {
this._nodes = nodes;
this._geometries = geometries;
this.id = id ++;
this.renderer = renderer;
this.object = object;
this.material = material;
this.scene = scene;
this.camera = camera;
this.lightsNode = lightsNode;
this.context = renderContext;
this.geometry = object.geometry;
this.version = material.version;
this.drawRange = null;
this.attributes = null;
this.pipeline = null;
this.vertexBuffers = null;
this.updateClipping( renderContext.clippingContext );
this.clippingContextVersion = this.clippingContext.version;
this.initialNodesCacheKey = this.getNodesCacheKey();
this.initialCacheKey = this.getCacheKey();
this._nodeBuilderState = null;
this._bindings = null;
this.onDispose = null;
this.isRenderObject = true;
this.onMaterialDispose = () => {
this.dispose();
};
this.material.addEventListener( 'dispose', this.onMaterialDispose );
}
updateClipping( parent ) {
const material = this.material;
let clippingContext = this.clippingContext;
if ( Array.isArray( material.clippingPlanes ) ) {
if ( clippingContext === parent || ! clippingContext ) {
clippingContext = new ClippingContext();
this.clippingContext = clippingContext;
}
clippingContext.update( parent, material );
} else if ( this.clippingContext !== parent ) {
this.clippingContext = parent;
}
}
get clippingNeedsUpdate() {
if ( this.clippingContext.version === this.clippingContextVersion ) return false;
this.clippingContextVersion = this.clippingContext.version;
return true;
}
getNodeBuilderState() {
return this._nodeBuilderState || ( this._nodeBuilderState = this._nodes.getForRender( this ) );
}
getBindings() {
return this._bindings || ( this._bindings = this.getNodeBuilderState().createBindings() );
}
getIndex() {
return this._geometries.getIndex( this );
}
getChainArray() {
return [ this.object, this.material, this.context, this.lightsNode ];
}
getAttributes() {
if ( this.attributes !== null ) return this.attributes;
const nodeAttributes = this.getNodeBuilderState().nodeAttributes;
const geometry = this.geometry;
const attributes = [];
const vertexBuffers = new Set();
for ( const nodeAttribute of nodeAttributes ) {
const attribute = nodeAttribute.node && nodeAttribute.node.attribute ? nodeAttribute.node.attribute : geometry.getAttribute( nodeAttribute.name );
if ( attribute === undefined ) continue;
attributes.push( attribute );
const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
vertexBuffers.add( bufferAttribute );
}
this.attributes = attributes;
this.vertexBuffers = Array.from( vertexBuffers.values() );
return attributes;
}
getVertexBuffers() {
if ( this.vertexBuffers === null ) this.getAttributes();
return this.vertexBuffers;
}
getMaterialCacheKey() {
const { object, material } = this;
let cacheKey = material.customProgramCacheKey();
for ( const property of getKeys( material ) ) {
if ( /^(is[A-Z]|_)|^(visible|version|uuid|name|opacity|userData)$/.test( property ) ) continue;
let value = material[ property ];
if ( value !== null ) {
const type = typeof value;
if ( type === 'number' ) value = value !== 0 ? '1' : '0'; // Convert to on/off, important for clearcoat, transmission, etc
else if ( type === 'object' ) value = '{}';
}
cacheKey += /*property + ':' +*/ value + ',';
}
cacheKey += this.clippingContextVersion + ',';
if ( object.skeleton ) {
cacheKey += object.skeleton.bones.length + ',';
}
if ( object.morphTargetInfluences ) {
cacheKey += object.morphTargetInfluences.length + ',';
}
if ( object.isBatchedMesh ) {
cacheKey += object._matricesTexture.uuid + ',';
}
return cacheKey;
}
get needsUpdate() {
return this.initialNodesCacheKey !== this.getNodesCacheKey() || this.clippingNeedsUpdate;
}
getNodesCacheKey() {
// Environment Nodes Cache Key
return this._nodes.getCacheKey( this.scene, this.lightsNode );
}
getCacheKey() {
return this.getMaterialCacheKey() + ',' + this.getNodesCacheKey();
}
dispose() {
this.material.removeEventListener( 'dispose', this.onMaterialDispose );
this.onDispose();
}
}

View File

@ -0,0 +1,93 @@
import ChainMap from './ChainMap.js';
import RenderObject from './RenderObject.js';
class RenderObjects {
constructor( renderer, nodes, geometries, pipelines, bindings, info ) {
this.renderer = renderer;
this.nodes = nodes;
this.geometries = geometries;
this.pipelines = pipelines;
this.bindings = bindings;
this.info = info;
this.chainMaps = {};
}
get( object, material, scene, camera, lightsNode, renderContext, passId ) {
const chainMap = this.getChainMap( passId );
const chainArray = [ object, material, renderContext, lightsNode ];
let renderObject = chainMap.get( chainArray );
if ( renderObject === undefined ) {
renderObject = this.createRenderObject( this.nodes, this.geometries, this.renderer, object, material, scene, camera, lightsNode, renderContext, passId );
chainMap.set( chainArray, renderObject );
} else {
renderObject.updateClipping( renderContext.clippingContext );
if ( renderObject.version !== material.version || renderObject.needsUpdate ) {
if ( renderObject.initialCacheKey !== renderObject.getCacheKey() ) {
renderObject.dispose();
renderObject = this.get( object, material, scene, camera, lightsNode, renderContext, passId );
} else {
renderObject.version = material.version;
}
}
}
return renderObject;
}
getChainMap( passId = 'default' ) {
return this.chainMaps[ passId ] || ( this.chainMaps[ passId ] = new ChainMap() );
}
dispose() {
this.chainMaps = {};
}
createRenderObject( nodes, geometries, renderer, object, material, scene, camera, lightsNode, renderContext, passId ) {
const chainMap = this.getChainMap( passId );
const renderObject = new RenderObject( nodes, geometries, renderer, object, material, scene, camera, lightsNode, renderContext );
renderObject.onDispose = () => {
this.pipelines.delete( renderObject );
this.bindings.delete( renderObject );
this.nodes.delete( renderObject );
chainMap.delete( renderObject.getChainArray() );
};
return renderObject;
}
}
export default RenderObjects;

View File

@ -0,0 +1,16 @@
import Pipeline from './Pipeline.js';
class RenderPipeline extends Pipeline {
constructor( cacheKey, vertexProgram, fragmentProgram ) {
super( cacheKey );
this.vertexProgram = vertexProgram;
this.fragmentProgram = fragmentProgram;
}
}
export default RenderPipeline;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,83 @@
import Binding from './Binding.js';
let id = 0;
class SampledTexture extends Binding {
constructor( name, texture ) {
super( name );
this.id = id ++;
this.texture = texture;
this.version = texture ? texture.version : 0;
this.store = false;
this.isSampledTexture = true;
}
get needsBindingsUpdate() {
const { texture, version } = this;
return texture.isVideoTexture ? true : version !== texture.version; // @TODO: version === 0 && texture.version > 0 ( add it just to External Textures like PNG,JPG )
}
update() {
const { texture, version } = this;
if ( version !== texture.version ) {
this.version = texture.version;
return true;
}
return false;
}
}
class SampledArrayTexture extends SampledTexture {
constructor( name, texture ) {
super( name, texture );
this.isSampledArrayTexture = true;
}
}
class Sampled3DTexture extends SampledTexture {
constructor( name, texture ) {
super( name, texture );
this.isSampled3DTexture = true;
}
}
class SampledCubeTexture extends SampledTexture {
constructor( name, texture ) {
super( name, texture );
this.isSampledCubeTexture = true;
}
}
export { SampledTexture, SampledArrayTexture, Sampled3DTexture, SampledCubeTexture };

View File

@ -0,0 +1,18 @@
import Binding from './Binding.js';
class Sampler extends Binding {
constructor( name, texture ) {
super( name );
this.texture = texture;
this.version = texture ? texture.version : 0;
this.isSampler = true;
}
}
export default Sampler;

View File

@ -0,0 +1,17 @@
import Buffer from './Buffer.js';
class StorageBuffer extends Buffer {
constructor( name, attribute ) {
super( name, attribute ? attribute.array : null );
this.attribute = attribute;
this.isStorageBuffer = true;
}
}
export default StorageBuffer;

View File

@ -0,0 +1,17 @@
import { BufferAttribute } from 'three';
class StorageBufferAttribute extends BufferAttribute {
constructor( array, itemSize, typeClass = Float32Array ) {
if ( ArrayBuffer.isView( array ) === false ) array = new typeClass( array * itemSize );
super( array, itemSize );
this.isStorageBufferAttribute = true;
}
}
export default StorageBufferAttribute;

View File

@ -0,0 +1,17 @@
import { InstancedBufferAttribute } from 'three';
class StorageInstancedBufferAttribute extends InstancedBufferAttribute {
constructor( array, itemSize, typeClass = Float32Array ) {
if ( ArrayBuffer.isView( array ) === false ) array = new typeClass( array * itemSize );
super( array, itemSize );
this.isStorageInstancedBufferAttribute = true;
}
}
export default StorageInstancedBufferAttribute;

View File

@ -0,0 +1,20 @@
import { Texture, LinearFilter } from 'three';
class StorageTexture extends Texture {
constructor( width = 1, height = 1 ) {
super();
this.image = { width, height };
this.magFilter = LinearFilter;
this.minFilter = LinearFilter;
this.isStorageTexture = true;
}
}
export default StorageTexture;

View File

@ -0,0 +1,344 @@
import DataMap from './DataMap.js';
import { Vector3, DepthTexture, DepthStencilFormat, DepthFormat, UnsignedIntType, UnsignedInt248Type, LinearFilter, NearestFilter, EquirectangularReflectionMapping, EquirectangularRefractionMapping, CubeReflectionMapping, CubeRefractionMapping, UnsignedByteType } from 'three';
const _size = new Vector3();
class Textures extends DataMap {
constructor( renderer, backend, info ) {
super();
this.renderer = renderer;
this.backend = backend;
this.info = info;
}
updateRenderTarget( renderTarget, activeMipmapLevel = 0 ) {
const renderTargetData = this.get( renderTarget );
const sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples;
const depthTextureMips = renderTargetData.depthTextureMips || ( renderTargetData.depthTextureMips = {} );
const texture = renderTarget.texture;
const textures = renderTarget.textures;
const size = this.getSize( texture );
const mipWidth = size.width >> activeMipmapLevel;
const mipHeight = size.height >> activeMipmapLevel;
let depthTexture = renderTarget.depthTexture || depthTextureMips[ activeMipmapLevel ];
let textureNeedsUpdate = false;
if ( depthTexture === undefined ) {
depthTexture = new DepthTexture();
depthTexture.format = renderTarget.stencilBuffer ? DepthStencilFormat : DepthFormat;
depthTexture.type = renderTarget.stencilBuffer ? UnsignedInt248Type : UnsignedIntType; // FloatType
depthTexture.image.width = mipWidth;
depthTexture.image.height = mipHeight;
depthTextureMips[ activeMipmapLevel ] = depthTexture;
}
if ( renderTargetData.width !== size.width || size.height !== renderTargetData.height ) {
textureNeedsUpdate = true;
depthTexture.needsUpdate = true;
depthTexture.image.width = mipWidth;
depthTexture.image.height = mipHeight;
}
renderTargetData.width = size.width;
renderTargetData.height = size.height;
renderTargetData.textures = textures;
renderTargetData.depthTexture = depthTexture;
renderTargetData.depth = renderTarget.depthBuffer;
renderTargetData.stencil = renderTarget.stencilBuffer;
renderTargetData.renderTarget = renderTarget;
if ( renderTargetData.sampleCount !== sampleCount ) {
textureNeedsUpdate = true;
depthTexture.needsUpdate = true;
renderTargetData.sampleCount = sampleCount;
}
//
const options = { sampleCount };
for ( let i = 0; i < textures.length; i ++ ) {
const texture = textures[ i ];
if ( textureNeedsUpdate ) texture.needsUpdate = true;
this.updateTexture( texture, options );
}
this.updateTexture( depthTexture, options );
// dispose handler
if ( renderTargetData.initialized !== true ) {
renderTargetData.initialized = true;
// dispose
const onDispose = () => {
renderTarget.removeEventListener( 'dispose', onDispose );
if ( textures !== undefined ) {
for ( let i = 0; i < textures.length; i ++ ) {
this._destroyTexture( textures[ i ] );
}
} else {
this._destroyTexture( texture );
}
this._destroyTexture( depthTexture );
};
renderTarget.addEventListener( 'dispose', onDispose );
}
}
updateTexture( texture, options = {} ) {
const textureData = this.get( texture );
if ( textureData.initialized === true && textureData.version === texture.version ) return;
const isRenderTarget = texture.isRenderTargetTexture || texture.isDepthTexture || texture.isFramebufferTexture;
const backend = this.backend;
if ( isRenderTarget && textureData.initialized === true ) {
// it's an update
backend.destroySampler( texture );
backend.destroyTexture( texture );
}
//
if ( texture.isFramebufferTexture ) {
const renderer = this.renderer;
const renderTarget = renderer.getRenderTarget();
if ( renderTarget ) {
texture.type = renderTarget.texture.type;
} else {
texture.type = UnsignedByteType;
}
}
//
const { width, height, depth } = this.getSize( texture );
options.width = width;
options.height = height;
options.depth = depth;
options.needsMipmaps = this.needsMipmaps( texture );
options.levels = options.needsMipmaps ? this.getMipLevels( texture, width, height ) : 1;
//
if ( isRenderTarget || texture.isStorageTexture === true ) {
backend.createSampler( texture );
backend.createTexture( texture, options );
} else {
const needsCreate = textureData.initialized !== true;
if ( needsCreate ) backend.createSampler( texture );
if ( texture.version > 0 ) {
const image = texture.image;
if ( image === undefined ) {
console.warn( 'THREE.Renderer: Texture marked for update but image is undefined.' );
} else if ( image.complete === false ) {
console.warn( 'THREE.Renderer: Texture marked for update but image is incomplete.' );
} else {
if ( texture.images ) {
const images = [];
for ( const image of texture.images ) {
images.push( image );
}
options.images = images;
} else {
options.image = image;
}
if ( textureData.isDefaultTexture === undefined || textureData.isDefaultTexture === true ) {
backend.createTexture( texture, options );
textureData.isDefaultTexture = false;
}
if ( texture.source.dataReady === true ) backend.updateTexture( texture, options );
if ( options.needsMipmaps && texture.mipmaps.length === 0 ) backend.generateMipmaps( texture );
}
} else {
// async update
backend.createDefaultTexture( texture );
textureData.isDefaultTexture = true;
}
}
// dispose handler
if ( textureData.initialized !== true ) {
textureData.initialized = true;
//
this.info.memory.textures ++;
// dispose
const onDispose = () => {
texture.removeEventListener( 'dispose', onDispose );
this._destroyTexture( texture );
this.info.memory.textures --;
};
texture.addEventListener( 'dispose', onDispose );
}
//
textureData.version = texture.version;
}
getSize( texture, target = _size ) {
let image = texture.images ? texture.images[ 0 ] : texture.image;
if ( image ) {
if ( image.image !== undefined ) image = image.image;
target.width = image.width;
target.height = image.height;
target.depth = texture.isCubeTexture ? 6 : ( image.depth || 1 );
} else {
target.width = target.height = target.depth = 1;
}
return target;
}
getMipLevels( texture, width, height ) {
let mipLevelCount;
if ( texture.isCompressedTexture ) {
mipLevelCount = texture.mipmaps.length;
} else {
mipLevelCount = Math.floor( Math.log2( Math.max( width, height ) ) ) + 1;
}
return mipLevelCount;
}
needsMipmaps( texture ) {
if ( this.isEnvironmentTexture( texture ) ) return true;
return ( texture.isCompressedTexture === true ) || ( ( texture.minFilter !== NearestFilter ) && ( texture.minFilter !== LinearFilter ) );
}
isEnvironmentTexture( texture ) {
const mapping = texture.mapping;
return ( mapping === EquirectangularReflectionMapping || mapping === EquirectangularRefractionMapping ) || ( mapping === CubeReflectionMapping || mapping === CubeRefractionMapping );
}
_destroyTexture( texture ) {
this.backend.destroySampler( texture );
this.backend.destroyTexture( texture );
this.delete( texture );
}
}
export default Textures;

View File

@ -0,0 +1,140 @@
import { Color, Matrix3, Matrix4, Vector2, Vector3, Vector4 } from 'three';
class Uniform {
constructor( name, value = null ) {
this.name = name;
this.value = value;
this.boundary = 0; // used to build the uniform buffer according to the STD140 layout
this.itemSize = 0;
this.offset = 0; // this property is set by WebGPUUniformsGroup and marks the start position in the uniform buffer
}
setValue( value ) {
this.value = value;
}
getValue() {
return this.value;
}
}
class FloatUniform extends Uniform {
constructor( name, value = 0 ) {
super( name, value );
this.isFloatUniform = true;
this.boundary = 4;
this.itemSize = 1;
}
}
class Vector2Uniform extends Uniform {
constructor( name, value = new Vector2() ) {
super( name, value );
this.isVector2Uniform = true;
this.boundary = 8;
this.itemSize = 2;
}
}
class Vector3Uniform extends Uniform {
constructor( name, value = new Vector3() ) {
super( name, value );
this.isVector3Uniform = true;
this.boundary = 16;
this.itemSize = 3;
}
}
class Vector4Uniform extends Uniform {
constructor( name, value = new Vector4() ) {
super( name, value );
this.isVector4Uniform = true;
this.boundary = 16;
this.itemSize = 4;
}
}
class ColorUniform extends Uniform {
constructor( name, value = new Color() ) {
super( name, value );
this.isColorUniform = true;
this.boundary = 16;
this.itemSize = 3;
}
}
class Matrix3Uniform extends Uniform {
constructor( name, value = new Matrix3() ) {
super( name, value );
this.isMatrix3Uniform = true;
this.boundary = 48;
this.itemSize = 12;
}
}
class Matrix4Uniform extends Uniform {
constructor( name, value = new Matrix4() ) {
super( name, value );
this.isMatrix4Uniform = true;
this.boundary = 64;
this.itemSize = 16;
}
}
export {
FloatUniform,
Vector2Uniform, Vector3Uniform, Vector4Uniform, ColorUniform,
Matrix3Uniform, Matrix4Uniform
};

View File

@ -0,0 +1,15 @@
import Buffer from './Buffer.js';
class UniformBuffer extends Buffer {
constructor( name, buffer = null ) {
super( name, buffer );
this.isUniformBuffer = true;
}
}
export default UniformBuffer;

View File

@ -0,0 +1,301 @@
import UniformBuffer from './UniformBuffer.js';
import { GPU_CHUNK_BYTES } from './Constants.js';
class UniformsGroup extends UniformBuffer {
constructor( name ) {
super( name );
this.isUniformsGroup = true;
// the order of uniforms in this array must match the order of uniforms in the shader
this.uniforms = [];
}
addUniform( uniform ) {
this.uniforms.push( uniform );
return this;
}
removeUniform( uniform ) {
const index = this.uniforms.indexOf( uniform );
if ( index !== - 1 ) {
this.uniforms.splice( index, 1 );
}
return this;
}
get buffer() {
let buffer = this._buffer;
if ( buffer === null ) {
const byteLength = this.byteLength;
buffer = new Float32Array( new ArrayBuffer( byteLength ) );
this._buffer = buffer;
}
return buffer;
}
get byteLength() {
let offset = 0; // global buffer offset in bytes
for ( let i = 0, l = this.uniforms.length; i < l; i ++ ) {
const uniform = this.uniforms[ i ];
const { boundary, itemSize } = uniform;
// offset within a single chunk in bytes
const chunkOffset = offset % GPU_CHUNK_BYTES;
const remainingSizeInChunk = GPU_CHUNK_BYTES - chunkOffset;
// conformance tests
if ( chunkOffset !== 0 && ( remainingSizeInChunk - boundary ) < 0 ) {
// check for chunk overflow
offset += ( GPU_CHUNK_BYTES - chunkOffset );
} else if ( chunkOffset % boundary !== 0 ) {
// check for correct alignment
offset += ( chunkOffset % boundary );
}
uniform.offset = ( offset / this.bytesPerElement );
offset += ( itemSize * this.bytesPerElement );
}
return Math.ceil( offset / GPU_CHUNK_BYTES ) * GPU_CHUNK_BYTES;
}
update() {
let updated = false;
for ( const uniform of this.uniforms ) {
if ( this.updateByType( uniform ) === true ) {
updated = true;
}
}
return updated;
}
updateByType( uniform ) {
if ( uniform.isFloatUniform ) return this.updateNumber( uniform );
if ( uniform.isVector2Uniform ) return this.updateVector2( uniform );
if ( uniform.isVector3Uniform ) return this.updateVector3( uniform );
if ( uniform.isVector4Uniform ) return this.updateVector4( uniform );
if ( uniform.isColorUniform ) return this.updateColor( uniform );
if ( uniform.isMatrix3Uniform ) return this.updateMatrix3( uniform );
if ( uniform.isMatrix4Uniform ) return this.updateMatrix4( uniform );
console.error( 'THREE.WebGPUUniformsGroup: Unsupported uniform type.', uniform );
}
updateNumber( uniform ) {
let updated = false;
const a = this.buffer;
const v = uniform.getValue();
const offset = uniform.offset;
if ( a[ offset ] !== v ) {
a[ offset ] = v;
updated = true;
}
return updated;
}
updateVector2( uniform ) {
let updated = false;
const a = this.buffer;
const v = uniform.getValue();
const offset = uniform.offset;
if ( a[ offset + 0 ] !== v.x || a[ offset + 1 ] !== v.y ) {
a[ offset + 0 ] = v.x;
a[ offset + 1 ] = v.y;
updated = true;
}
return updated;
}
updateVector3( uniform ) {
let updated = false;
const a = this.buffer;
const v = uniform.getValue();
const offset = uniform.offset;
if ( a[ offset + 0 ] !== v.x || a[ offset + 1 ] !== v.y || a[ offset + 2 ] !== v.z ) {
a[ offset + 0 ] = v.x;
a[ offset + 1 ] = v.y;
a[ offset + 2 ] = v.z;
updated = true;
}
return updated;
}
updateVector4( uniform ) {
let updated = false;
const a = this.buffer;
const v = uniform.getValue();
const offset = uniform.offset;
if ( a[ offset + 0 ] !== v.x || a[ offset + 1 ] !== v.y || a[ offset + 2 ] !== v.z || a[ offset + 4 ] !== v.w ) {
a[ offset + 0 ] = v.x;
a[ offset + 1 ] = v.y;
a[ offset + 2 ] = v.z;
a[ offset + 3 ] = v.w;
updated = true;
}
return updated;
}
updateColor( uniform ) {
let updated = false;
const a = this.buffer;
const c = uniform.getValue();
const offset = uniform.offset;
if ( a[ offset + 0 ] !== c.r || a[ offset + 1 ] !== c.g || a[ offset + 2 ] !== c.b ) {
a[ offset + 0 ] = c.r;
a[ offset + 1 ] = c.g;
a[ offset + 2 ] = c.b;
updated = true;
}
return updated;
}
updateMatrix3( uniform ) {
let updated = false;
const a = this.buffer;
const e = uniform.getValue().elements;
const offset = uniform.offset;
if ( a[ offset + 0 ] !== e[ 0 ] || a[ offset + 1 ] !== e[ 1 ] || a[ offset + 2 ] !== e[ 2 ] ||
a[ offset + 4 ] !== e[ 3 ] || a[ offset + 5 ] !== e[ 4 ] || a[ offset + 6 ] !== e[ 5 ] ||
a[ offset + 8 ] !== e[ 6 ] || a[ offset + 9 ] !== e[ 7 ] || a[ offset + 10 ] !== e[ 8 ] ) {
a[ offset + 0 ] = e[ 0 ];
a[ offset + 1 ] = e[ 1 ];
a[ offset + 2 ] = e[ 2 ];
a[ offset + 4 ] = e[ 3 ];
a[ offset + 5 ] = e[ 4 ];
a[ offset + 6 ] = e[ 5 ];
a[ offset + 8 ] = e[ 6 ];
a[ offset + 9 ] = e[ 7 ];
a[ offset + 10 ] = e[ 8 ];
updated = true;
}
return updated;
}
updateMatrix4( uniform ) {
let updated = false;
const a = this.buffer;
const e = uniform.getValue().elements;
const offset = uniform.offset;
if ( arraysEqual( a, e, offset ) === false ) {
a.set( e, offset );
updated = true;
}
return updated;
}
}
function arraysEqual( a, b, offset ) {
for ( let i = 0, l = b.length; i < l; i ++ ) {
if ( a[ offset + i ] !== b[ i ] ) return false;
}
return true;
}
export default UniformsGroup;

View File

@ -0,0 +1,773 @@
import NodeMaterial from '../../../nodes/materials/NodeMaterial.js';
import { getDirection, blur } from '../../../nodes/pmrem/PMREMUtils.js';
import { equirectUV } from '../../../nodes/utils/EquirectUVNode.js';
import { uniform } from '../../../nodes/core/UniformNode.js';
import { uniforms } from '../../../nodes/accessors/UniformsNode.js';
import { texture } from '../../../nodes/accessors/TextureNode.js';
import { cubeTexture } from '../../../nodes/accessors/CubeTextureNode.js';
import { float, vec3 } from '../../../nodes/shadernode/ShaderNode.js';
import { uv } from '../../../nodes/accessors/UVNode.js';
import { attribute } from '../../../nodes/core/AttributeNode.js';
import {
OrthographicCamera,
Color,
Vector3,
BufferGeometry,
BufferAttribute,
RenderTarget,
Mesh,
CubeReflectionMapping,
CubeRefractionMapping,
CubeUVReflectionMapping,
LinearFilter,
NoBlending,
RGBAFormat,
HalfFloatType,
BackSide,
LinearSRGBColorSpace,
PerspectiveCamera,
MeshBasicMaterial,
BoxGeometry
} from 'three';
const LOD_MIN = 4;
// The standard deviations (radians) associated with the extra mips. These are
// chosen to approximate a Trowbridge-Reitz distribution function times the
// geometric shadowing function. These sigma values squared must match the
// variance #defines in cube_uv_reflection_fragment.glsl.js.
const EXTRA_LOD_SIGMA = [ 0.125, 0.215, 0.35, 0.446, 0.526, 0.582 ];
// The maximum length of the blur for loop. Smaller sigmas will use fewer
// samples and exit early, but not recompile the shader.
const MAX_SAMPLES = 20;
const _flatCamera = /*@__PURE__*/ new OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
const _cubeCamera = /*@__PURE__*/ new PerspectiveCamera( 90, 1 );
const _clearColor = /*@__PURE__*/ new Color();
let _oldTarget = null;
let _oldActiveCubeFace = 0;
let _oldActiveMipmapLevel = 0;
// Golden Ratio
const PHI = ( 1 + Math.sqrt( 5 ) ) / 2;
const INV_PHI = 1 / PHI;
// Vertices of a dodecahedron (except the opposites, which represent the
// same axis), used as axis directions evenly spread on a sphere.
const _axisDirections = [
/*@__PURE__*/ new Vector3( - PHI, INV_PHI, 0 ),
/*@__PURE__*/ new Vector3( PHI, INV_PHI, 0 ),
/*@__PURE__*/ new Vector3( - INV_PHI, 0, PHI ),
/*@__PURE__*/ new Vector3( INV_PHI, 0, PHI ),
/*@__PURE__*/ new Vector3( 0, PHI, - INV_PHI ),
/*@__PURE__*/ new Vector3( 0, PHI, INV_PHI ),
/*@__PURE__*/ new Vector3( - 1, 1, - 1 ),
/*@__PURE__*/ new Vector3( 1, 1, - 1 ),
/*@__PURE__*/ new Vector3( - 1, 1, 1 ),
/*@__PURE__*/ new Vector3( 1, 1, 1 )
];
//
// WebGPU Face indices
const _faceLib = [
3, 1, 5,
0, 4, 2
];
const direction = getDirection( uv(), attribute( 'faceIndex' ) ).normalize();
const outputDirection = vec3( direction.x, direction.y.negate(), direction.z );
/**
* This class generates a Prefiltered, Mipmapped Radiance Environment Map
* (PMREM) from a cubeMap environment texture. This allows different levels of
* blur to be quickly accessed based on material roughness. It is packed into a
* special CubeUV format that allows us to perform custom interpolation so that
* we can support nonlinear formats such as RGBE. Unlike a traditional mipmap
* chain, it only goes down to the LOD_MIN level (above), and then creates extra
* even more filtered 'mips' at the same LOD_MIN resolution, associated with
* higher roughness levels. In this way we maintain resolution to smoothly
* interpolate diffuse lighting while limiting sampling computation.
*
* Paper: Fast, Accurate Image-Based Lighting
* https://drive.google.com/file/d/15y8r_UpKlU9SvV4ILb0C3qCPecS8pvLz/view
*/
class PMREMGenerator {
constructor( renderer ) {
this._renderer = renderer;
this._pingPongRenderTarget = null;
this._lodMax = 0;
this._cubeSize = 0;
this._lodPlanes = [];
this._sizeLods = [];
this._sigmas = [];
this._lodMeshes = [];
this._blurMaterial = null;
this._cubemapMaterial = null;
this._equirectMaterial = null;
this._backgroundBox = null;
}
/**
* Generates a PMREM from a supplied Scene, which can be faster than using an
* image if networking bandwidth is low. Optional sigma specifies a blur radius
* in radians to be applied to the scene before PMREM generation. Optional near
* and far planes ensure the scene is rendered in its entirety (the cubeCamera
* is placed at the origin).
*/
fromScene( scene, sigma = 0, near = 0.1, far = 100 ) {
_oldTarget = this._renderer.getRenderTarget();
_oldActiveCubeFace = this._renderer.getActiveCubeFace();
_oldActiveMipmapLevel = this._renderer.getActiveMipmapLevel();
this._setSize( 256 );
const cubeUVRenderTarget = this._allocateTargets();
cubeUVRenderTarget.depthBuffer = true;
this._sceneToCubeUV( scene, near, far, cubeUVRenderTarget );
if ( sigma > 0 ) {
this._blur( cubeUVRenderTarget, 0, 0, sigma );
}
this._applyPMREM( cubeUVRenderTarget );
this._cleanup( cubeUVRenderTarget );
return cubeUVRenderTarget;
}
/**
* Generates a PMREM from an equirectangular texture, which can be either LDR
* or HDR. The ideal input image size is 1k (1024 x 512),
* as this matches best with the 256 x 256 cubemap output.
*/
fromEquirectangular( equirectangular, renderTarget = null ) {
return this._fromTexture( equirectangular, renderTarget );
}
/**
* Generates a PMREM from an cubemap texture, which can be either LDR
* or HDR. The ideal input cube size is 256 x 256,
* as this matches best with the 256 x 256 cubemap output.
*/
fromCubemap( cubemap, renderTarget = null ) {
return this._fromTexture( cubemap, renderTarget );
}
/**
* Pre-compiles the cubemap shader. You can get faster start-up by invoking this method during
* your texture's network fetch for increased concurrency.
*/
compileCubemapShader() {
if ( this._cubemapMaterial === null ) {
this._cubemapMaterial = _getCubemapMaterial();
this._compileMaterial( this._cubemapMaterial );
}
}
/**
* Pre-compiles the equirectangular shader. You can get faster start-up by invoking this method during
* your texture's network fetch for increased concurrency.
*/
compileEquirectangularShader() {
if ( this._equirectMaterial === null ) {
this._equirectMaterial = _getEquirectMaterial();
this._compileMaterial( this._equirectMaterial );
}
}
/**
* Disposes of the PMREMGenerator's internal memory. Note that PMREMGenerator is a static class,
* so you should not need more than one PMREMGenerator object. If you do, calling dispose() on
* one of them will cause any others to also become unusable.
*/
dispose() {
this._dispose();
if ( this._cubemapMaterial !== null ) this._cubemapMaterial.dispose();
if ( this._equirectMaterial !== null ) this._equirectMaterial.dispose();
if ( this._backgroundBox !== null ) {
this._backgroundBox.geometry.dispose();
this._backgroundBox.material.dispose();
}
}
// private interface
_setSize( cubeSize ) {
this._lodMax = Math.floor( Math.log2( cubeSize ) );
this._cubeSize = Math.pow( 2, this._lodMax );
}
_dispose() {
if ( this._blurMaterial !== null ) this._blurMaterial.dispose();
if ( this._pingPongRenderTarget !== null ) this._pingPongRenderTarget.dispose();
for ( let i = 0; i < this._lodPlanes.length; i ++ ) {
this._lodPlanes[ i ].dispose();
}
}
_cleanup( outputTarget ) {
this._renderer.setRenderTarget( _oldTarget, _oldActiveCubeFace, _oldActiveMipmapLevel );
outputTarget.scissorTest = false;
_setViewport( outputTarget, 0, 0, outputTarget.width, outputTarget.height );
}
_fromTexture( texture, renderTarget ) {
if ( texture.mapping === CubeReflectionMapping || texture.mapping === CubeRefractionMapping ) {
this._setSize( texture.image.length === 0 ? 16 : ( texture.image[ 0 ].width || texture.image[ 0 ].image.width ) );
} else { // Equirectangular
this._setSize( texture.image.width / 4 );
}
_oldTarget = this._renderer.getRenderTarget();
_oldActiveCubeFace = this._renderer.getActiveCubeFace();
_oldActiveMipmapLevel = this._renderer.getActiveMipmapLevel();
const cubeUVRenderTarget = renderTarget || this._allocateTargets();
this._textureToCubeUV( texture, cubeUVRenderTarget );
this._applyPMREM( cubeUVRenderTarget );
this._cleanup( cubeUVRenderTarget );
return cubeUVRenderTarget;
}
_allocateTargets() {
const width = 3 * Math.max( this._cubeSize, 16 * 7 );
const height = 4 * this._cubeSize;
const params = {
magFilter: LinearFilter,
minFilter: LinearFilter,
generateMipmaps: false,
type: HalfFloatType,
format: RGBAFormat,
colorSpace: LinearSRGBColorSpace,
//depthBuffer: false
};
const cubeUVRenderTarget = _createRenderTarget( width, height, params );
if ( this._pingPongRenderTarget === null || this._pingPongRenderTarget.width !== width || this._pingPongRenderTarget.height !== height ) {
if ( this._pingPongRenderTarget !== null ) {
this._dispose();
}
this._pingPongRenderTarget = _createRenderTarget( width, height, params );
const { _lodMax } = this;
( { sizeLods: this._sizeLods, lodPlanes: this._lodPlanes, sigmas: this._sigmas, lodMeshes: this._lodMeshes } = _createPlanes( _lodMax ) );
this._blurMaterial = _getBlurShader( _lodMax, width, height );
}
return cubeUVRenderTarget;
}
_compileMaterial( material ) {
const tmpMesh = this._lodMeshes[ 0 ];
tmpMesh.material = material;
this._renderer.compile( tmpMesh, _flatCamera );
}
_sceneToCubeUV( scene, near, far, cubeUVRenderTarget ) {
const cubeCamera = _cubeCamera;
cubeCamera.near = near;
cubeCamera.far = far;
// px, py, pz, nx, ny, nz
const upSign = [ - 1, 1, - 1, - 1, - 1, - 1 ];
const forwardSign = [ 1, 1, 1, - 1, - 1, - 1 ];
const renderer = this._renderer;
const originalAutoClear = renderer.autoClear;
renderer.getClearColor( _clearColor );
renderer.autoClear = false;
let backgroundBox = this._backgroundBox;
if ( backgroundBox === null ) {
const backgroundMaterial = new MeshBasicMaterial( {
name: 'PMREM.Background',
side: BackSide,
depthWrite: false,
depthTest: false
} );
backgroundBox = new Mesh( new BoxGeometry(), backgroundMaterial );
}
let useSolidColor = false;
const background = scene.background;
if ( background ) {
if ( background.isColor ) {
backgroundBox.material.color.copy( background );
scene.background = null;
useSolidColor = true;
}
} else {
backgroundBox.material.color.copy( _clearColor );
useSolidColor = true;
}
renderer.setRenderTarget( cubeUVRenderTarget );
renderer.clear();
if ( useSolidColor ) {
renderer.render( backgroundBox, cubeCamera );
}
for ( let i = 0; i < 6; i ++ ) {
const col = i % 3;
if ( col === 0 ) {
cubeCamera.up.set( 0, upSign[ i ], 0 );
cubeCamera.lookAt( forwardSign[ i ], 0, 0 );
} else if ( col === 1 ) {
cubeCamera.up.set( 0, 0, upSign[ i ] );
cubeCamera.lookAt( 0, forwardSign[ i ], 0 );
} else {
cubeCamera.up.set( 0, upSign[ i ], 0 );
cubeCamera.lookAt( 0, 0, forwardSign[ i ] );
}
const size = this._cubeSize;
_setViewport( cubeUVRenderTarget, col * size, i > 2 ? size : 0, size, size );
renderer.render( scene, cubeCamera );
}
renderer.autoClear = originalAutoClear;
scene.background = background;
}
_textureToCubeUV( texture, cubeUVRenderTarget ) {
const renderer = this._renderer;
const isCubeTexture = ( texture.mapping === CubeReflectionMapping || texture.mapping === CubeRefractionMapping );
if ( isCubeTexture ) {
if ( this._cubemapMaterial === null ) {
this._cubemapMaterial = _getCubemapMaterial( texture );
}
} else {
if ( this._equirectMaterial === null ) {
this._equirectMaterial = _getEquirectMaterial( texture );
}
}
const material = isCubeTexture ? this._cubemapMaterial : this._equirectMaterial;
material.fragmentNode.value = texture;
const mesh = this._lodMeshes[ 0 ];
mesh.material = material;
const size = this._cubeSize;
_setViewport( cubeUVRenderTarget, 0, 0, 3 * size, 2 * size );
renderer.setRenderTarget( cubeUVRenderTarget );
renderer.render( mesh, _flatCamera );
}
_applyPMREM( cubeUVRenderTarget ) {
const renderer = this._renderer;
const autoClear = renderer.autoClear;
renderer.autoClear = false;
const n = this._lodPlanes.length;
for ( let i = 1; i < n; i ++ ) {
const sigma = Math.sqrt( this._sigmas[ i ] * this._sigmas[ i ] - this._sigmas[ i - 1 ] * this._sigmas[ i - 1 ] );
const poleAxis = _axisDirections[ ( n - i - 1 ) % _axisDirections.length ];
this._blur( cubeUVRenderTarget, i - 1, i, sigma, poleAxis );
}
renderer.autoClear = autoClear;
}
/**
* This is a two-pass Gaussian blur for a cubemap. Normally this is done
* vertically and horizontally, but this breaks down on a cube. Here we apply
* the blur latitudinally (around the poles), and then longitudinally (towards
* the poles) to approximate the orthogonally-separable blur. It is least
* accurate at the poles, but still does a decent job.
*/
_blur( cubeUVRenderTarget, lodIn, lodOut, sigma, poleAxis ) {
const pingPongRenderTarget = this._pingPongRenderTarget;
this._halfBlur(
cubeUVRenderTarget,
pingPongRenderTarget,
lodIn,
lodOut,
sigma,
'latitudinal',
poleAxis );
this._halfBlur(
pingPongRenderTarget,
cubeUVRenderTarget,
lodOut,
lodOut,
sigma,
'longitudinal',
poleAxis );
}
_halfBlur( targetIn, targetOut, lodIn, lodOut, sigmaRadians, direction, poleAxis ) {
const renderer = this._renderer;
const blurMaterial = this._blurMaterial;
if ( direction !== 'latitudinal' && direction !== 'longitudinal' ) {
console.error( 'blur direction must be either latitudinal or longitudinal!' );
}
// Number of standard deviations at which to cut off the discrete approximation.
const STANDARD_DEVIATIONS = 3;
const blurMesh = this._lodMeshes[ lodOut ];
blurMesh.material = blurMaterial;
const blurUniforms = blurMaterial.uniforms;
const pixels = this._sizeLods[ lodIn ] - 1;
const radiansPerPixel = isFinite( sigmaRadians ) ? Math.PI / ( 2 * pixels ) : 2 * Math.PI / ( 2 * MAX_SAMPLES - 1 );
const sigmaPixels = sigmaRadians / radiansPerPixel;
const samples = isFinite( sigmaRadians ) ? 1 + Math.floor( STANDARD_DEVIATIONS * sigmaPixels ) : MAX_SAMPLES;
if ( samples > MAX_SAMPLES ) {
console.warn( `sigmaRadians, ${
sigmaRadians}, is too large and will clip, as it requested ${
samples} samples when the maximum is set to ${MAX_SAMPLES}` );
}
const weights = [];
let sum = 0;
for ( let i = 0; i < MAX_SAMPLES; ++ i ) {
const x = i / sigmaPixels;
const weight = Math.exp( - x * x / 2 );
weights.push( weight );
if ( i === 0 ) {
sum += weight;
} else if ( i < samples ) {
sum += 2 * weight;
}
}
for ( let i = 0; i < weights.length; i ++ ) {
weights[ i ] = weights[ i ] / sum;
}
targetIn.texture.frame = ( targetIn.texture.frame || 0 ) + 1;
blurUniforms.envMap.value = targetIn.texture;
blurUniforms.samples.value = samples;
blurUniforms.weights.array = weights;
blurUniforms.latitudinal.value = direction === 'latitudinal' ? 1 : 0;
if ( poleAxis ) {
blurUniforms.poleAxis.value = poleAxis;
}
const { _lodMax } = this;
blurUniforms.dTheta.value = radiansPerPixel;
blurUniforms.mipInt.value = _lodMax - lodIn;
const outputSize = this._sizeLods[ lodOut ];
const x = 3 * outputSize * ( lodOut > _lodMax - LOD_MIN ? lodOut - _lodMax + LOD_MIN : 0 );
const y = 4 * ( this._cubeSize - outputSize );
_setViewport( targetOut, x, y, 3 * outputSize, 2 * outputSize );
renderer.setRenderTarget( targetOut );
renderer.render( blurMesh, _flatCamera );
}
}
function _createPlanes( lodMax ) {
const lodPlanes = [];
const sizeLods = [];
const sigmas = [];
const lodMeshes = [];
let lod = lodMax;
const totalLods = lodMax - LOD_MIN + 1 + EXTRA_LOD_SIGMA.length;
for ( let i = 0; i < totalLods; i ++ ) {
const sizeLod = Math.pow( 2, lod );
sizeLods.push( sizeLod );
let sigma = 1.0 / sizeLod;
if ( i > lodMax - LOD_MIN ) {
sigma = EXTRA_LOD_SIGMA[ i - lodMax + LOD_MIN - 1 ];
} else if ( i === 0 ) {
sigma = 0;
}
sigmas.push( sigma );
const texelSize = 1.0 / ( sizeLod - 2 );
const min = - texelSize;
const max = 1 + texelSize;
const uv1 = [ min, min, max, min, max, max, min, min, max, max, min, max ];
const cubeFaces = 6;
const vertices = 6;
const positionSize = 3;
const uvSize = 2;
const faceIndexSize = 1;
const position = new Float32Array( positionSize * vertices * cubeFaces );
const uv = new Float32Array( uvSize * vertices * cubeFaces );
const faceIndex = new Float32Array( faceIndexSize * vertices * cubeFaces );
for ( let face = 0; face < cubeFaces; face ++ ) {
const x = ( face % 3 ) * 2 / 3 - 1;
const y = face > 2 ? 0 : - 1;
const coordinates = [
x, y, 0,
x + 2 / 3, y, 0,
x + 2 / 3, y + 1, 0,
x, y, 0,
x + 2 / 3, y + 1, 0,
x, y + 1, 0
];
const faceIdx = _faceLib[ face ];
position.set( coordinates, positionSize * vertices * faceIdx );
uv.set( uv1, uvSize * vertices * faceIdx );
const fill = [ faceIdx, faceIdx, faceIdx, faceIdx, faceIdx, faceIdx ];
faceIndex.set( fill, faceIndexSize * vertices * faceIdx );
}
const planes = new BufferGeometry();
planes.setAttribute( 'position', new BufferAttribute( position, positionSize ) );
planes.setAttribute( 'uv', new BufferAttribute( uv, uvSize ) );
planes.setAttribute( 'faceIndex', new BufferAttribute( faceIndex, faceIndexSize ) );
lodPlanes.push( planes );
lodMeshes.push( new Mesh( planes, null ) );
if ( lod > LOD_MIN ) {
lod --;
}
}
return { lodPlanes, sizeLods, sigmas, lodMeshes };
}
function _createRenderTarget( width, height, params ) {
const cubeUVRenderTarget = new RenderTarget( width, height, params );
cubeUVRenderTarget.texture.mapping = CubeUVReflectionMapping;
cubeUVRenderTarget.texture.name = 'PMREM.cubeUv';
cubeUVRenderTarget.texture.isPMREMTexture = true;
cubeUVRenderTarget.scissorTest = true;
return cubeUVRenderTarget;
}
function _setViewport( target, x, y, width, height ) {
const viewY = target.height - height - y;
target.viewport.set( x, viewY, width, height );
target.scissor.set( x, viewY, width, height );
}
function _getMaterial() {
const material = new NodeMaterial();
material.depthTest = false;
material.depthWrite = false;
material.blending = NoBlending;
return material;
}
function _getBlurShader( lodMax, width, height ) {
const weights = uniforms( new Array( MAX_SAMPLES ).fill( 0 ) );
const poleAxis = uniform( new Vector3( 0, 1, 0 ) );
const dTheta = uniform( 0 );
const n = float( MAX_SAMPLES );
const latitudinal = uniform( 0 ); // false, bool
const samples = uniform( 1 ); // int
const envMap = texture( null );
const mipInt = uniform( 0 ); // int
const CUBEUV_TEXEL_WIDTH = float( 1 / width );
const CUBEUV_TEXEL_HEIGHT = float( 1 / height );
const CUBEUV_MAX_MIP = float( lodMax );
const materialUniforms = {
n,
latitudinal,
weights,
poleAxis,
outputDirection,
dTheta,
samples,
envMap,
mipInt,
CUBEUV_TEXEL_WIDTH,
CUBEUV_TEXEL_HEIGHT,
CUBEUV_MAX_MIP
};
const material = _getMaterial();
material.uniforms = materialUniforms; // TODO: Move to outside of the material
material.fragmentNode = blur( { ...materialUniforms, latitudinal: latitudinal.equal( 1 ) } );
return material;
}
function _getCubemapMaterial( envTexture ) {
const material = _getMaterial();
material.fragmentNode = cubeTexture( envTexture, outputDirection );
return material;
}
function _getEquirectMaterial( envTexture ) {
const material = _getMaterial();
material.fragmentNode = texture( envTexture, equirectUV( outputDirection ), 0 );
return material;
}
export default PMREMGenerator;

View File

@ -0,0 +1,44 @@
class NodeBuilderState {
constructor( vertexShader, fragmentShader, computeShader, nodeAttributes, bindings, updateNodes, updateBeforeNodes, transforms = [] ) {
this.vertexShader = vertexShader;
this.fragmentShader = fragmentShader;
this.computeShader = computeShader;
this.transforms = transforms;
this.nodeAttributes = nodeAttributes;
this.bindings = bindings;
this.updateNodes = updateNodes;
this.updateBeforeNodes = updateBeforeNodes;
this.usedTimes = 0;
}
createBindings() {
const bindingsArray = [];
for ( const instanceBinding of this.bindings ) {
let binding = instanceBinding;
if ( instanceBinding.shared !== true ) {
binding = instanceBinding.clone();
}
bindingsArray.push( binding );
}
return bindingsArray;
}
}
export default NodeBuilderState;

View File

@ -0,0 +1,49 @@
import { SampledTexture } from '../SampledTexture.js';
class NodeSampledTexture extends SampledTexture {
constructor( name, textureNode ) {
super( name, textureNode ? textureNode.value : null );
this.textureNode = textureNode;
}
get needsBindingsUpdate() {
return this.textureNode.value !== this.texture || super.needsBindingsUpdate;
}
update() {
const { textureNode } = this;
if ( this.texture !== textureNode.value ) {
this.texture = textureNode.value;
return true;
}
return super.update();
}
}
class NodeSampledCubeTexture extends NodeSampledTexture {
constructor( name, textureNode ) {
super( name, textureNode );
this.isSampledCubeTexture = true;
}
}
export { NodeSampledTexture, NodeSampledCubeTexture };

View File

@ -0,0 +1,15 @@
import Sampler from '../Sampler.js';
class NodeSampler extends Sampler {
constructor( name, textureNode ) {
super( name, textureNode ? textureNode.value : null );
this.textureNode = textureNode;
}
}
export default NodeSampler;

View File

@ -0,0 +1,23 @@
import StorageBuffer from '../StorageBuffer.js';
let _id = 0;
class NodeStorageBuffer extends StorageBuffer {
constructor( nodeUniform ) {
super( 'StorageBuffer_' + _id ++, nodeUniform ? nodeUniform.value : null );
this.nodeUniform = nodeUniform;
}
get buffer() {
return this.nodeUniform.value;
}
}
export default NodeStorageBuffer;

View File

@ -0,0 +1,135 @@
import {
FloatUniform, Vector2Uniform, Vector3Uniform, Vector4Uniform,
ColorUniform, Matrix3Uniform, Matrix4Uniform
} from '../Uniform.js';
class FloatNodeUniform extends FloatUniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
class Vector2NodeUniform extends Vector2Uniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
class Vector3NodeUniform extends Vector3Uniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
class Vector4NodeUniform extends Vector4Uniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
class ColorNodeUniform extends ColorUniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
class Matrix3NodeUniform extends Matrix3Uniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
class Matrix4NodeUniform extends Matrix4Uniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
export {
FloatNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform,
ColorNodeUniform, Matrix3NodeUniform, Matrix4NodeUniform
};

View File

@ -0,0 +1,23 @@
import UniformBuffer from '../UniformBuffer.js';
let _id = 0;
class NodeUniformBuffer extends UniformBuffer {
constructor( nodeUniform ) {
super( 'UniformBuffer_' + _id ++, nodeUniform ? nodeUniform.value : null );
this.nodeUniform = nodeUniform;
}
get buffer() {
return this.nodeUniform.value;
}
}
export default NodeUniformBuffer;

View File

@ -0,0 +1,44 @@
import UniformsGroup from '../UniformsGroup.js';
let id = 0;
class NodeUniformsGroup extends UniformsGroup {
constructor( name, groupNode ) {
super( name );
this.id = id ++;
this.groupNode = groupNode;
this.isNodeUniformsGroup = true;
}
get shared() {
return this.groupNode.shared;
}
getNodes() {
const nodes = [];
for ( const uniform of this.uniforms ) {
const node = uniform.nodeUniform.node;
if ( ! node ) throw new Error( 'NodeUniformsGroup: Uniform has no node.' );
nodes.push( node );
}
return nodes;
}
}
export default NodeUniformsGroup;

View File

@ -0,0 +1,467 @@
import DataMap from '../DataMap.js';
import ChainMap from '../ChainMap.js';
import NodeBuilderState from './NodeBuilderState.js';
import { EquirectangularReflectionMapping, EquirectangularRefractionMapping, NoToneMapping, SRGBColorSpace } from 'three';
import { NodeFrame, vec4, objectGroup, renderGroup, frameGroup, cubeTexture, texture, rangeFog, densityFog, reference, viewportBottomLeft, normalWorld, pmremTexture, viewportTopLeft } from '../../../nodes/Nodes.js';
class Nodes extends DataMap {
constructor( renderer, backend ) {
super();
this.renderer = renderer;
this.backend = backend;
this.nodeFrame = new NodeFrame();
this.nodeBuilderCache = new Map();
this.callHashCache = new ChainMap();
this.groupsData = new ChainMap();
}
updateGroup( nodeUniformsGroup ) {
const groupNode = nodeUniformsGroup.groupNode;
const name = groupNode.name;
// objectGroup is every updated
if ( name === objectGroup.name ) return true;
// renderGroup is updated once per render/compute call
if ( name === renderGroup.name ) {
const uniformsGroupData = this.get( nodeUniformsGroup );
const renderId = this.nodeFrame.renderId;
if ( uniformsGroupData.renderId !== renderId ) {
uniformsGroupData.renderId = renderId;
return true;
}
return false;
}
// frameGroup is updated once per frame
if ( name === frameGroup.name ) {
const uniformsGroupData = this.get( nodeUniformsGroup );
const frameId = this.nodeFrame.frameId;
if ( uniformsGroupData.frameId !== frameId ) {
uniformsGroupData.frameId = frameId;
return true;
}
return false;
}
// other groups are updated just when groupNode.needsUpdate is true
const groupChain = [ groupNode, nodeUniformsGroup ];
let groupData = this.groupsData.get( groupChain );
if ( groupData === undefined ) this.groupsData.set( groupChain, groupData = {} );
if ( groupData.version !== groupNode.version ) {
groupData.version = groupNode.version;
return true;
}
return false;
}
getForRenderCacheKey( renderObject ) {
return renderObject.initialCacheKey;
}
getForRender( renderObject ) {
const renderObjectData = this.get( renderObject );
let nodeBuilderState = renderObjectData.nodeBuilderState;
if ( nodeBuilderState === undefined ) {
const { nodeBuilderCache } = this;
const cacheKey = this.getForRenderCacheKey( renderObject );
nodeBuilderState = nodeBuilderCache.get( cacheKey );
if ( nodeBuilderState === undefined ) {
const nodeBuilder = this.backend.createNodeBuilder( renderObject.object, this.renderer, renderObject.scene );
nodeBuilder.material = renderObject.material;
nodeBuilder.context.material = renderObject.material;
nodeBuilder.lightsNode = renderObject.lightsNode;
nodeBuilder.environmentNode = this.getEnvironmentNode( renderObject.scene );
nodeBuilder.fogNode = this.getFogNode( renderObject.scene );
nodeBuilder.clippingContext = renderObject.clippingContext;
nodeBuilder.build();
nodeBuilderState = this._createNodeBuilderState( nodeBuilder );
nodeBuilderCache.set( cacheKey, nodeBuilderState );
}
nodeBuilderState.usedTimes ++;
renderObjectData.nodeBuilderState = nodeBuilderState;
}
return nodeBuilderState;
}
delete( object ) {
if ( object.isRenderObject ) {
const nodeBuilderState = this.get( object ).nodeBuilderState;
nodeBuilderState.usedTimes --;
if ( nodeBuilderState.usedTimes === 0 ) {
this.nodeBuilderCache.delete( this.getForRenderCacheKey( object ) );
}
}
return super.delete( object );
}
getForCompute( computeNode ) {
const computeData = this.get( computeNode );
let nodeBuilderState = computeData.nodeBuilderState;
if ( nodeBuilderState === undefined ) {
const nodeBuilder = this.backend.createNodeBuilder( computeNode, this.renderer );
nodeBuilder.build();
nodeBuilderState = this._createNodeBuilderState( nodeBuilder );
computeData.nodeBuilderState = nodeBuilderState;
}
return nodeBuilderState;
}
_createNodeBuilderState( nodeBuilder ) {
return new NodeBuilderState(
nodeBuilder.vertexShader,
nodeBuilder.fragmentShader,
nodeBuilder.computeShader,
nodeBuilder.getAttributesArray(),
nodeBuilder.getBindings(),
nodeBuilder.updateNodes,
nodeBuilder.updateBeforeNodes,
nodeBuilder.transforms
);
}
getEnvironmentNode( scene ) {
return scene.environmentNode || this.get( scene ).environmentNode || null;
}
getBackgroundNode( scene ) {
return scene.backgroundNode || this.get( scene ).backgroundNode || null;
}
getFogNode( scene ) {
return scene.fogNode || this.get( scene ).fogNode || null;
}
getCacheKey( scene, lightsNode ) {
const chain = [ scene, lightsNode ];
const callId = this.renderer.info.calls;
let cacheKeyData = this.callHashCache.get( chain );
if ( cacheKeyData === undefined || cacheKeyData.callId !== callId ) {
const environmentNode = this.getEnvironmentNode( scene );
const fogNode = this.getFogNode( scene );
const cacheKey = [];
if ( lightsNode ) cacheKey.push( lightsNode.getCacheKey() );
if ( environmentNode ) cacheKey.push( environmentNode.getCacheKey() );
if ( fogNode ) cacheKey.push( fogNode.getCacheKey() );
cacheKeyData = {
callId,
cacheKey: cacheKey.join( ',' )
};
this.callHashCache.set( chain, cacheKeyData );
}
return cacheKeyData.cacheKey;
}
updateScene( scene ) {
this.updateEnvironment( scene );
this.updateFog( scene );
this.updateBackground( scene );
}
get isToneMappingState() {
return this.renderer.getRenderTarget() ? false : true;
}
updateBackground( scene ) {
const sceneData = this.get( scene );
const background = scene.background;
if ( background ) {
if ( sceneData.background !== background ) {
let backgroundNode = null;
if ( background.isCubeTexture === true || ( background.mapping === EquirectangularReflectionMapping || background.mapping === EquirectangularRefractionMapping ) ) {
backgroundNode = pmremTexture( background, normalWorld );
} else if ( background.isTexture === true ) {
backgroundNode = texture( background, viewportBottomLeft ).setUpdateMatrix( true );
} else if ( background.isColor !== true ) {
console.error( 'WebGPUNodes: Unsupported background configuration.', background );
}
sceneData.backgroundNode = backgroundNode;
sceneData.background = background;
}
} else if ( sceneData.backgroundNode ) {
delete sceneData.backgroundNode;
delete sceneData.background;
}
}
updateFog( scene ) {
const sceneData = this.get( scene );
const fog = scene.fog;
if ( fog ) {
if ( sceneData.fog !== fog ) {
let fogNode = null;
if ( fog.isFogExp2 ) {
fogNode = densityFog( reference( 'color', 'color', fog ), reference( 'density', 'float', fog ) );
} else if ( fog.isFog ) {
fogNode = rangeFog( reference( 'color', 'color', fog ), reference( 'near', 'float', fog ), reference( 'far', 'float', fog ) );
} else {
console.error( 'WebGPUNodes: Unsupported fog configuration.', fog );
}
sceneData.fogNode = fogNode;
sceneData.fog = fog;
}
} else {
delete sceneData.fogNode;
delete sceneData.fog;
}
}
updateEnvironment( scene ) {
const sceneData = this.get( scene );
const environment = scene.environment;
if ( environment ) {
if ( sceneData.environment !== environment ) {
let environmentNode = null;
if ( environment.isCubeTexture === true ) {
environmentNode = cubeTexture( environment );
} else if ( environment.isTexture === true ) {
environmentNode = texture( environment );
} else {
console.error( 'Nodes: Unsupported environment configuration.', environment );
}
sceneData.environmentNode = environmentNode;
sceneData.environment = environment;
}
} else if ( sceneData.environmentNode ) {
delete sceneData.environmentNode;
delete sceneData.environment;
}
}
getNodeFrame( renderer = this.renderer, scene = null, object = null, camera = null, material = null ) {
const nodeFrame = this.nodeFrame;
nodeFrame.renderer = renderer;
nodeFrame.scene = scene;
nodeFrame.object = object;
nodeFrame.camera = camera;
nodeFrame.material = material;
return nodeFrame;
}
getNodeFrameForRender( renderObject ) {
return this.getNodeFrame( renderObject.renderer, renderObject.scene, renderObject.object, renderObject.camera, renderObject.material );
}
getOutputNode( outputTexture ) {
let output = texture( outputTexture, viewportTopLeft );
if ( this.isToneMappingState ) {
if ( this.renderer.toneMappingNode ) {
output = vec4( this.renderer.toneMappingNode.context( { color: output.rgb } ), output.a );
} else if ( this.renderer.toneMapping !== NoToneMapping ) {
output = output.toneMapping( this.renderer.toneMapping );
}
}
if ( this.renderer.currentColorSpace === SRGBColorSpace ) {
output = output.linearToColorSpace( this.renderer.currentColorSpace );
}
return output;
}
updateBefore( renderObject ) {
const nodeFrame = this.getNodeFrameForRender( renderObject );
const nodeBuilder = renderObject.getNodeBuilderState();
for ( const node of nodeBuilder.updateBeforeNodes ) {
nodeFrame.updateBeforeNode( node );
}
}
updateForCompute( computeNode ) {
const nodeFrame = this.getNodeFrame();
const nodeBuilder = this.getForCompute( computeNode );
for ( const node of nodeBuilder.updateNodes ) {
nodeFrame.updateNode( node );
}
}
updateForRender( renderObject ) {
const nodeFrame = this.getNodeFrameForRender( renderObject );
const nodeBuilder = renderObject.getNodeBuilderState();
for ( const node of nodeBuilder.updateNodes ) {
nodeFrame.updateNode( node );
}
}
dispose() {
super.dispose();
this.nodeFrame = new NodeFrame();
this.nodeBuilderCache = new Map();
}
}
export default Nodes;