Skip to content
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

WebGPURenderer: enable shadow rendering in WebGLBackend #27023

Merged
merged 5 commits into from Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions examples/jsm/nodes/lighting/AnalyticLightNode.js
Expand Up @@ -7,6 +7,7 @@ import { reference } from '../accessors/ReferenceNode.js';
import { texture } from '../accessors/TextureNode.js';
import { positionWorld } from '../accessors/PositionNode.js';
import { normalWorld } from '../accessors/NormalNode.js';
import { WebGPUCoordinateSystem } from 'three';
//import { add } from '../math/OperatorNode.js';

import { Color, DepthTexture, NearestFilter, LessCompare } from 'three';
Expand Down Expand Up @@ -73,11 +74,24 @@ class AnalyticLightNode extends LightingNode {
.and( shadowCoord.y.lessThanEqual( 1 ) )
.and( shadowCoord.z.lessThanEqual( 1 ) );

shadowCoord = vec3(
shadowCoord.x,
shadowCoord.y.oneMinus(), // WebGPU: Flip Y
shadowCoord.z.add( bias ).mul( 2 ).sub( 1 ) // WebGPU: Convertion [ 0, 1 ] to [ - 1, 1 ]
);

if ( builder.renderer.coordinateSystem === WebGPUCoordinateSystem ) {

shadowCoord = vec3(
shadowCoord.x,
shadowCoord.y.oneMinus(), // WebGPU: Flip Y
shadowCoord.z.add( bias ).mul( 2 ).sub( 1 ) // WebGPU: Convertion [ 0, 1 ] to [ - 1, 1 ]
);

} else {

shadowCoord = vec3(
shadowCoord.x,
shadowCoord.y,
shadowCoord.z.add( bias )
);

}

const textureCompare = ( depthTexture, shadowCoord, compare ) => texture( depthTexture, shadowCoord ).compare( compare );
//const textureCompare = ( depthTexture, shadowCoord, compare ) => compare.step( texture( depthTexture, shadowCoord ) );
Expand Down
4 changes: 2 additions & 2 deletions examples/jsm/renderers/common/Renderer.js
Expand Up @@ -179,7 +179,6 @@ class Renderer {
const nodeFrame = this._nodes.nodeFrame;

const previousRenderId = nodeFrame.renderId;
const previousRenderState = this._currentRenderContext;

//

Expand All @@ -190,6 +189,7 @@ class Renderer {
const activeCubeFace = this._activeCubeFace;
const activeMipmapLevel = this._activeMipmapLevel;

this._previousRenderContext = this._currentRenderContext;
this._currentRenderContext = renderContext;

nodeFrame.renderId ++;
Expand Down Expand Up @@ -332,7 +332,7 @@ class Renderer {
// restore render tree

nodeFrame.renderId = previousRenderId;
this._currentRenderContext = previousRenderState;
this._currentRenderContext = this._previousRenderContext;
sunag marked this conversation as resolved.
Show resolved Hide resolved

this._lastRenderContext = renderContext;

Expand Down
20 changes: 20 additions & 0 deletions examples/jsm/renderers/webgl/WebGLBackend.js
Expand Up @@ -121,6 +121,26 @@ class WebGLBackend extends Backend {

finishRender( renderContext ) {

const lastRenderContext = this.renderer._previousRenderContext;

if ( lastRenderContext !== renderContext && lastRenderContext !== null) {

this._setFramebuffer( lastRenderContext );

if ( lastRenderContext.viewport ) {

this.updateViewport( lastRenderContext );

} else {

const gl = this.gl;

gl.viewport( 0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight );

}

}

const occlusionQueryCount = renderContext.occlusionQueryCount;

if ( occlusionQueryCount > 0 ) {
Expand Down
Binary file modified examples/screenshots/webgpu_shadowmap.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions examples/webgpu_shadowmap.html
Expand Up @@ -26,6 +26,8 @@
import * as THREE from 'three';

import WebGPU from 'three/addons/capabilities/WebGPU.js';
import WebGL from 'three/addons/capabilities/WebGL.js';

import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';

import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
Expand All @@ -38,11 +40,11 @@

function init() {

if ( WebGPU.isAvailable() === false ) {
if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {

document.body.appendChild( WebGPU.getErrorMessage() );

throw new Error( 'No WebGPU support' );
throw new Error( 'No WebGPU or WebGL2 support' );

}

Expand Down
1 change: 0 additions & 1 deletion test/e2e/puppeteer.js
Expand Up @@ -125,7 +125,6 @@ const exceptionList = [
'webgpu_particles',
'webgpu_sandbox',
'webgpu_shadertoy',
'webgpu_shadowmap',
'webgpu_sprites',
'webgpu_tsl_editor',
'webgpu_tsl_transpiler',
Expand Down