123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- import { DoubleSide, FloatType, HalfFloatType, PlaneGeometry, Mesh, RenderTarget, Scene, MeshPhongNodeMaterial, NodeMaterial } from 'three/webgpu';
- import { add, float, mix, output, sub, texture, uniform, uv, vec2, vec4 } from 'three/tsl';
- import { potpack } from '../libs/potpack.module.js';
- class ProgressiveLightMap {
-
- constructor( renderer, resolution = 1024 ) {
-
- this.renderer = renderer;
-
- this.resolution = resolution;
- this._lightMapContainers = [];
- this._scene = new Scene();
- this._buffer1Active = false;
- this._labelMesh = null;
- this._blurringPlane = null;
-
- const type = /(Android|iPad|iPhone|iPod)/g.test( navigator.userAgent ) ? HalfFloatType : FloatType;
- this._progressiveLightMap1 = new RenderTarget( this.resolution, this.resolution, { type: type } );
- this._progressiveLightMap2 = new RenderTarget( this.resolution, this.resolution, { type: type } );
- this._progressiveLightMap2.texture.channel = 1;
-
- this._averagingWindow = uniform( 100 );
- this._previousShadowMap = texture( this._progressiveLightMap1.texture );
-
- const uvNode = uv( 1 ).flipY();
- this._uvMat = new MeshPhongNodeMaterial();
- this._uvMat.vertexNode = vec4( sub( uvNode, vec2( 0.5 ) ).mul( 2 ), 1, 1 );
- this._uvMat.outputNode = vec4( mix( this._previousShadowMap.sample( uv( 1 ) ), output, float( 1 ).div( this._averagingWindow ) ) );
- }
-
- addObjectsToLightMap( objects ) {
-
- const uv_boxes = [];
- const padding = 3 / this.resolution;
- for ( let ob = 0; ob < objects.length; ob ++ ) {
- const object = objects[ ob ];
-
- if ( object.isLight ) {
- this._scene.attach( object ); continue;
- }
- if ( object.geometry.hasAttribute( 'uv' ) === false ) {
- console.warn( 'THREE.ProgressiveLightMap: All lightmap objects need uvs.' ); continue;
- }
- if ( this._blurringPlane === null ) {
- this._initializeBlurPlane();
- }
-
- object.material.lightMap = this._progressiveLightMap2.texture;
- object.material.dithering = true;
- object.castShadow = true;
- object.receiveShadow = true;
- object.renderOrder = 1000 + ob;
-
-
- uv_boxes.push( { w: 1 + ( padding * 2 ), h: 1 + ( padding * 2 ), index: ob, x: 0, y: 0 } );
- this._lightMapContainers.push( { basicMat: object.material, object: object } );
- }
-
- const dimensions = potpack( uv_boxes );
- uv_boxes.forEach( ( box ) => {
- const uv1 = objects[ box.index ].geometry.getAttribute( 'uv' ).clone();
- for ( let i = 0; i < uv1.array.length; i += uv1.itemSize ) {
- uv1.array[ i ] = ( uv1.array[ i ] + box.x + padding ) / dimensions.w;
- uv1.array[ i + 1 ] = 1 - ( ( uv1.array[ i + 1 ] + box.y + padding ) / dimensions.h );
- }
- objects[ box.index ].geometry.setAttribute( 'uv1', uv1 );
- objects[ box.index ].geometry.getAttribute( 'uv1' ).needsUpdate = true;
- } );
- }
-
- dispose() {
- this._progressiveLightMap1.dispose();
- this._progressiveLightMap2.dispose();
- this._uvMat.dispose();
- if ( this._blurringPlane !== null ) {
- this._blurringPlane.geometry.dispose();
- this._blurringPlane.material.dispose();
- }
- if ( this._labelMesh !== null ) {
- this._labelMesh.geometry.dispose();
- this._labelMesh.material.dispose();
- }
- }
-
- update( camera, blendWindow = 100, blurEdges = true ) {
- if ( this._blurringPlane === null ) {
- return;
- }
-
- const currentRenderTarget = this.renderer.getRenderTarget();
-
- this._blurringPlane.visible = blurEdges;
-
- for ( let l = 0; l < this._lightMapContainers.length; l ++ ) {
- this._lightMapContainers[ l ].object.oldScene = this._lightMapContainers[ l ].object.parent;
- this._scene.attach( this._lightMapContainers[ l ].object );
- }
-
- for ( let l = 0; l < this._lightMapContainers.length; l ++ ) {
- this._averagingWindow.value = blendWindow;
- this._lightMapContainers[ l ].object.material = this._uvMat;
- this._lightMapContainers[ l ].object.oldFrustumCulled = this._lightMapContainers[ l ].object.frustumCulled;
- this._lightMapContainers[ l ].object.frustumCulled = false;
- }
-
- const activeMap = this._buffer1Active ? this._progressiveLightMap1 : this._progressiveLightMap2;
- const inactiveMap = this._buffer1Active ? this._progressiveLightMap2 : this._progressiveLightMap1;
-
- this.renderer.setRenderTarget( activeMap );
- this._previousShadowMap.value = inactiveMap.texture;
- this._buffer1Active = ! this._buffer1Active;
- this.renderer.render( this._scene, camera );
-
- for ( let l = 0; l < this._lightMapContainers.length; l ++ ) {
- this._lightMapContainers[ l ].object.frustumCulled = this._lightMapContainers[ l ].object.oldFrustumCulled;
- this._lightMapContainers[ l ].object.material = this._lightMapContainers[ l ].basicMat;
- this._lightMapContainers[ l ].object.oldScene.attach( this._lightMapContainers[ l ].object );
- }
-
- this.renderer.setRenderTarget( currentRenderTarget );
- }
-
- showDebugLightmap( visible, position = null ) {
- if ( this._lightMapContainers.length === 0 ) {
- console.warn( 'THREE.ProgressiveLightMap: Call .showDebugLightmap() after adding the objects.' );
- return;
- }
- if ( this._labelMesh === null ) {
- const labelMaterial = new NodeMaterial();
- labelMaterial.colorNode = texture( this._progressiveLightMap1.texture ).sample( uv().flipY() );
- labelMaterial.side = DoubleSide;
- const labelGeometry = new PlaneGeometry( 100, 100 );
- this._labelMesh = new Mesh( labelGeometry, labelMaterial );
- this._labelMesh.position.y = 250;
- this._lightMapContainers[ 0 ].object.parent.add( this._labelMesh );
- }
- if ( position !== null ) {
- this._labelMesh.position.copy( position );
- }
- this._labelMesh.visible = visible;
- }
-
- _initializeBlurPlane() {
- const blurMaterial = new NodeMaterial();
- blurMaterial.polygonOffset = true;
- blurMaterial.polygonOffsetFactor = - 1;
- blurMaterial.polygonOffsetUnits = 3;
- blurMaterial.vertexNode = vec4( sub( uv(), vec2( 0.5 ) ).mul( 2 ), 1, 1 );
- const uvNode = uv().flipY().toVar();
- const pixelOffset = float( 0.5 ).div( float( this.resolution ) ).toVar();
- const color = add(
- this._previousShadowMap.sample( uvNode.add( vec2( pixelOffset, 0 ) ) ),
- this._previousShadowMap.sample( uvNode.add( vec2( 0, pixelOffset ) ) ),
- this._previousShadowMap.sample( uvNode.add( vec2( 0, pixelOffset.negate() ) ) ),
- this._previousShadowMap.sample( uvNode.add( vec2( pixelOffset.negate(), 0 ) ) ),
- this._previousShadowMap.sample( uvNode.add( vec2( pixelOffset, pixelOffset ) ) ),
- this._previousShadowMap.sample( uvNode.add( vec2( pixelOffset.negate(), pixelOffset ) ) ),
- this._previousShadowMap.sample( uvNode.add( vec2( pixelOffset, pixelOffset.negate() ) ) ),
- this._previousShadowMap.sample( uvNode.add( vec2( pixelOffset.negate(), pixelOffset.negate() ) ) ),
- ).div( 8 );
- blurMaterial.fragmentNode = color;
- this._blurringPlane = new Mesh( new PlaneGeometry( 1, 1 ), blurMaterial );
- this._blurringPlane.name = 'Blurring Plane';
- this._blurringPlane.frustumCulled = false;
- this._blurringPlane.renderOrder = 0;
- this._blurringPlane.material.depthWrite = false;
- this._scene.add( this._blurringPlane );
- }
- }
- export { ProgressiveLightMap };
|