-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lensflare: Add WebGPU version. #29451
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mugen87
reviewed
Sep 20, 2024
Um, the new version handles color spaces more correctly. Maybe we should apply the same changes to import {
AdditiveBlending,
Box2,
BufferGeometry,
Color,
FramebufferTexture,
InterleavedBuffer,
InterleavedBufferAttribute,
Mesh,
MeshBasicMaterial,
RawShaderMaterial,
ShaderMaterial,
UnsignedByteType,
Vector2,
Vector3,
Vector4
} from 'three';
class Lensflare extends Mesh {
constructor() {
super( Lensflare.Geometry, new MeshBasicMaterial( { opacity: 0, transparent: true } ) );
this.isLensflare = true;
this.type = 'Lensflare';
this.frustumCulled = false;
this.renderOrder = Infinity;
//
const positionScreen = new Vector3();
const positionView = new Vector3();
// textures
const tempMap = new FramebufferTexture( 16, 16 );
const occlusionMap = new FramebufferTexture( 16, 16 );
let currentType = UnsignedByteType;
// material
const geometry = Lensflare.Geometry;
const material1a = new RawShaderMaterial( {
uniforms: {
'scale': { value: null },
'screenPosition': { value: null }
},
vertexShader: /* glsl */`
precision highp float;
uniform vec3 screenPosition;
uniform vec2 scale;
attribute vec3 position;
void main() {
gl_Position = vec4( position.xy * scale + screenPosition.xy, screenPosition.z, 1.0 );
}`,
fragmentShader: /* glsl */`
precision highp float;
void main() {
gl_FragColor = vec4( 1.0, 0.0, 1.0, 1.0 );
}`,
depthTest: true,
depthWrite: false,
transparent: false
} );
const material1b = new RawShaderMaterial( {
uniforms: {
'map': { value: tempMap },
'scale': { value: null },
'screenPosition': { value: null }
},
vertexShader: /* glsl */`
precision highp float;
uniform vec3 screenPosition;
uniform vec2 scale;
attribute vec3 position;
attribute vec2 uv;
varying vec2 vUV;
void main() {
vUV = uv;
gl_Position = vec4( position.xy * scale + screenPosition.xy, screenPosition.z, 1.0 );
}`,
fragmentShader: /* glsl */`
precision highp float;
uniform sampler2D map;
varying vec2 vUV;
void main() {
gl_FragColor = texture2D( map, vUV );
}`,
depthTest: false,
depthWrite: false,
transparent: false
} );
// the following object is used for occlusionMap generation
const mesh1 = new Mesh( geometry, material1a );
//
const elements = [];
const shader = LensflareElement.Shader;
const material2 = new ShaderMaterial( {
name: shader.name,
uniforms: {
'map': { value: null },
'occlusionMap': { value: occlusionMap },
'color': { value: new Color( 0xffffff ) },
'scale': { value: new Vector2() },
'screenPosition': { value: new Vector3() }
},
vertexShader: shader.vertexShader,
fragmentShader: shader.fragmentShader,
blending: AdditiveBlending,
transparent: true,
depthWrite: false
} );
const mesh2 = new Mesh( geometry, material2 );
this.addElement = function ( element ) {
elements.push( element );
};
//
const scale = new Vector2();
const screenPositionPixels = new Vector2();
const validArea = new Box2();
const viewport = new Vector4();
this.onBeforeRender = function ( renderer, scene, camera ) {
renderer.getCurrentViewport( viewport );
const renderTarget = renderer.getRenderTarget();
const type = ( renderTarget !== null ) ? renderTarget.texture.type : UnsignedByteType;
if ( currentType !== type ) {
tempMap.dispose();
occlusionMap.dispose();
tempMap.type = occlusionMap.type = type;
currentType = type;
}
const invAspect = viewport.w / viewport.z;
const halfViewportWidth = viewport.z / 2.0;
const halfViewportHeight = viewport.w / 2.0;
let size = 16 / viewport.w;
scale.set( size * invAspect, size );
validArea.min.set( viewport.x, viewport.y );
validArea.max.set( viewport.x + ( viewport.z - 16 ), viewport.y + ( viewport.w - 16 ) );
// calculate position in screen space
positionView.setFromMatrixPosition( this.matrixWorld );
positionView.applyMatrix4( camera.matrixWorldInverse );
if ( positionView.z > 0 ) return; // lensflare is behind the camera
positionScreen.copy( positionView ).applyMatrix4( camera.projectionMatrix );
// horizontal and vertical coordinate of the lower left corner of the pixels to copy
screenPositionPixels.x = viewport.x + ( positionScreen.x * halfViewportWidth ) + halfViewportWidth - 8;
screenPositionPixels.y = viewport.y + ( positionScreen.y * halfViewportHeight ) + halfViewportHeight - 8;
// screen cull
if ( validArea.containsPoint( screenPositionPixels ) ) {
// save current RGB to temp texture
renderer.copyFramebufferToTexture( tempMap, screenPositionPixels );
// render pink quad
let uniforms = material1a.uniforms;
uniforms[ 'scale' ].value = scale;
uniforms[ 'screenPosition' ].value = positionScreen;
renderer.renderBufferDirect( camera, null, geometry, material1a, mesh1, null );
// copy result to occlusionMap
renderer.copyFramebufferToTexture( occlusionMap, screenPositionPixels );
// restore graphics
uniforms = material1b.uniforms;
uniforms[ 'scale' ].value = scale;
uniforms[ 'screenPosition' ].value = positionScreen;
renderer.renderBufferDirect( camera, null, geometry, material1b, mesh1, null );
// render elements
const vecX = - positionScreen.x * 2;
const vecY = - positionScreen.y * 2;
for ( let i = 0, l = elements.length; i < l; i ++ ) {
const element = elements[ i ];
const uniforms = material2.uniforms;
uniforms[ 'color' ].value.copy( element.color ).convertSRGBToLinear();
uniforms[ 'map' ].value = element.texture;
uniforms[ 'screenPosition' ].value.x = positionScreen.x + vecX * element.distance;
uniforms[ 'screenPosition' ].value.y = positionScreen.y + vecY * element.distance;
size = element.size / viewport.w;
const invAspect = viewport.w / viewport.z;
uniforms[ 'scale' ].value.set( size * invAspect, size );
material2.uniformsNeedUpdate = true;
renderer.renderBufferDirect( camera, null, geometry, material2, mesh2, null );
}
}
};
this.dispose = function () {
material1a.dispose();
material1b.dispose();
material2.dispose();
tempMap.dispose();
occlusionMap.dispose();
for ( let i = 0, l = elements.length; i < l; i ++ ) {
elements[ i ].texture.dispose();
}
};
}
}
//
class LensflareElement {
constructor( texture, size = 1, distance = 0, color = new Color( 0xffffff ) ) {
this.texture = texture;
this.size = size;
this.distance = distance;
this.color = color;
}
}
LensflareElement.Shader = {
name: 'LensflareElementShader',
uniforms: {
'map': { value: null },
'occlusionMap': { value: null },
'color': { value: null },
'scale': { value: null },
'screenPosition': { value: null }
},
vertexShader: /* glsl */`
uniform vec3 screenPosition;
uniform vec2 scale;
uniform sampler2D occlusionMap;
varying vec2 vUV;
varying float vVisibility;
void main() {
vUV = uv;
vec2 pos = position.xy;
vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) );
visibility += texture2D( occlusionMap, vec2( 0.5, 0.1 ) );
visibility += texture2D( occlusionMap, vec2( 0.9, 0.1 ) );
visibility += texture2D( occlusionMap, vec2( 0.9, 0.5 ) );
visibility += texture2D( occlusionMap, vec2( 0.9, 0.9 ) );
visibility += texture2D( occlusionMap, vec2( 0.5, 0.9 ) );
visibility += texture2D( occlusionMap, vec2( 0.1, 0.9 ) );
visibility += texture2D( occlusionMap, vec2( 0.1, 0.5 ) );
visibility += texture2D( occlusionMap, vec2( 0.5, 0.5 ) );
vVisibility = visibility.r / 9.0;
vVisibility *= 1.0 - visibility.g / 9.0;
vVisibility *= visibility.b / 9.0;
gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );
}`,
fragmentShader: /* glsl */`
uniform sampler2D map;
uniform vec3 color;
varying vec2 vUV;
varying float vVisibility;
void main() {
vec4 texture = texture2D( map, vUV );
texture.a *= vVisibility;
gl_FragColor = texture;
gl_FragColor.rgb *= color;
#include <tonemapping_fragment>
#include <colorspace_fragment>
}`
};
Lensflare.Geometry = ( function () {
const geometry = new BufferGeometry();
const float32Array = new Float32Array( [
- 1, - 1, 0, 0, 0,
1, - 1, 0, 1, 0,
1, 1, 0, 1, 1,
- 1, 1, 0, 0, 1
] );
const interleavedBuffer = new InterleavedBuffer( float32Array, 5 );
geometry.setIndex( [ 0, 1, 2, 0, 2, 3 ] );
geometry.setAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
geometry.setAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
return geometry;
} )();
export { Lensflare, LensflareElement }; This would assume the lens flare texture are marked as sRGB in the examples. |
Mugen87
reviewed
Sep 20, 2024
Mugen87
reviewed
Sep 20, 2024
Mugen87
changed the title
LensflareGPU: WebGPURenderer compatible lensflare.
Lensflare: Add WebGPU version.
Sep 21, 2024
Let's merge this! Color spaces are handled more correctly in |
This was referenced Sep 25, 2024
LD2Studio
pushed a commit
to LD2Studio/LD2Studio-Editor
that referenced
this pull request
Sep 27, 2024
BatchedMesh: add `deleteInstance()` (mrdoob#29449) * BatchedMesh: add `deleteInstance()` * BatchedMesh: prioritize reusing freed instance ids when adding instance Update Three.js r169 Lensflare: Add WebGPU version. (mrdoob#29451) * lensflare example * fix depth * remove forceWebGL * rename * fix typo * follow original code * Update LensflareMesh.js --------- Co-authored-by: aardgoose <[email protected]> Co-authored-by: Michael Herzog <[email protected]> Examples: Update lensflare colors. (mrdoob#29458) WebGLRenderer: add reverse-z via EXT_clip_control (mrdoob#29445) * WebGLRenderer: add reverse-z via EXT_clip_control * WebGLRenderer: move conversion methods to utils Updated builds. Docs: Improve `WebGLRenderer` page. (mrdoob#29459) WebGLProgram: add USE_REVERSEDEPTHBUF define (mrdoob#29461) CylinderGeometry: Don't add degenerate triangles (mrdoob#29460) * CylinderGeometry: Don't add degenerate triangles * Change comparison RenderObject: Introduce `getGeometryCacheKey()` (mrdoob#29465) * RenderObject: Added `getGeometryCacheKey()` * NodeMaterialObserver: Improve skinnedmesh and morph supports * cleanup Update actions/setup-node digest to 0a44ba7 (mrdoob#29466) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Update github/codeql-action digest to 294a9d9 (mrdoob#29467) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Update devDependencies (non-major) (mrdoob#29468) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Updated builds. CurveModifierGPU: WebGPURenderer port. (mrdoob#29453) * curve mod * lint * update screenshot * use correct constant * use filtered texture --------- Co-authored-by: aardgoose <[email protected]> Examples: Clean up. (mrdoob#29473) * Examples: Clean up. * E2E: Update screenshot. SpriteNodeMaterial: Add sizeAttenuation (mrdoob#29394) * SpriteNodeMaterial: Add sizeAttenuation * Regenerate screenshot * revert mat4 mul with vec4(,1)? * revert * using let and toVar fixes the issue? * merge upstream/dev * revert screenshot * does reverting SpriteNode even fix? * revert fix * revert everything fix tsl galaxy? * checkout dev SpriteNodeMaterial.js... * smaller change test * convert scale to var * convert scale to var * simply adding toVar breaks tsl_galaxy? * fix multiple spritematerial with different center position * feedbacks * cleanup * feedbacks * cleanup * cleanup --------- Co-authored-by: sunag <[email protected]> WebGPURenderer: Prevent out of bounds `textureLoad` access in WGSL (mrdoob#29470) Co-authored-by: aardgoose <[email protected]> SkyMesh,WaterMesh: Fix NodeMaterial imports (mrdoob#29477) WebGPURenderer: Reuse LightNode when available (mrdoob#29480) * WebGPURenderer: Reuse LightNode when available * cleanup DecalGeometry: Transform normal with normal matrix (mrdoob#29476) * Transform normal with normal matrix * Transform normal with normal matrix Examples: add reflection mask in `retargeting_readyplayer` (mrdoob#29485) BatchedMesh: Add `getGeometryRangeAt` (mrdoob#29409) * BatchedMesh: Add getGeometryRangeAt Co-authored-by: Luigi Denora <[email protected]> * Fix eslint error Co-authored-by: Luigi Denora <[email protected]> * Doc changed * Add optional target --------- Co-authored-by: Luigi Denora <[email protected]> WebGPURenderer: Introduce hash-based cache key (mrdoob#29479) * introduce numeric cache key * cleanup * cleanup * simplification * revision * rev * rev * cleanup Nodes: Access Remaining Compute Builtins (mrdoob#29469) * init * remove duplicated function Add decal as child of mesh (mrdoob#29486) Update webgl_postprocessing_ssaa.html Update webgpu_postprocessing_ssaa.html ToonOutlinePassNode: Add FX pass for toon outlines. (mrdoob#29483) * ToonOutlineNode: Add FX pass for toon outlines. * ToonOutlinePassNode: Refactor code. * E2E: Update screenshot. * ToonOutlinePassNode: Clean up. * ToonOutlinePassNode: More clean up. * ToonOutlinePassNode: More clean up. Update Addons.js (mrdoob#29493) leftovers after removing `PackedPhongMaterial ` mrdoob#29382 ReferenceNode: Fix null reference using `getNodeType()` (mrdoob#29498) * fix null reference using `getNodeType()` * add `sprite.center` check NodeBuilder: Introduce `addFlowCodeHierarchy()` / `NodeBlock` (mrdoob#29495) * VolumeNodeMaterial: simplify a little * cleanup * NodeBuilder: Introduce `addFlowCodeHierarchy()` Examples: Improve shadow map size in `webgpu_tsl_angular_slicing` (mrdoob#29499) WebGPURenderer: respect the `renderer.shadowMap.enabled` property (mrdoob#29492) * enable/disable shadow * enable shadowmaps * enable more examples * and another one * Update Nodes.js --------- Co-authored-by: aardgoose <[email protected]> Updated builds. GLTFLoader: Remove deprecated code. (mrdoob#29502) TiltLoader: Remove loader. (mrdoob#29471) Update Chinese translation of InstancedMesh. (mrdoob#29506) r169
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related issue: #29295
Conversion of lenflare object to TSL, some differences in color handling but the basic mechanics work