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: Add webgpu_shadowmap_opacity #28973

Merged
merged 5 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@
"webgpu_shadertoy",
"webgpu_tsl_interoperability",
"webgpu_shadowmap",
"webgpu_shadowmap_opacity",
"webgpu_skinning",
"webgpu_skinning_instancing",
"webgpu_skinning_points",
Expand Down
Binary file added examples/screenshots/webgpu_shadowmap_opacity.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
147 changes: 147 additions & 0 deletions examples/webgpu_shadowmap_opacity.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - shadowmap + opacity</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>

<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - shadowmap + opacity
</div>

<script type="importmap">
{
"imports": {
"three": "../build/three.webgpu.js",
"three/tsl": "../build/three.webgpu.js",
"three/addons/": "./jsm/"
}
}
</script>

<script type="module">

import * as THREE from 'three';
import { tslFn, vec4 } from 'three/tsl';

import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

let camera, scene, renderer;

init().then( render );

async function init() {

const container = document.createElement( 'div' );
document.body.appendChild( container );

camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 40 );
camera.position.set( - 4, 2, 6 );

renderer = new THREE.WebGPURenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( render );
renderer.toneMapping = THREE.AgXToneMapping;
renderer.toneMappingExposure = 1.5;
container.appendChild( renderer.domElement );

scene = new THREE.Scene();
scene.background = new THREE.Color( 0x9e9eff );

// light + shadow

const hemi = new THREE.AmbientLight( 0xffffff, .5 );
scene.add( hemi );

const dirLight = new THREE.DirectionalLight( 0x6666ff, 10 );
dirLight.position.set( 3, 5, 17 );
dirLight.castShadow = true;
dirLight.shadow.camera.near = 0.1;
dirLight.shadow.camera.far = 50;
dirLight.shadow.camera.right = 5;
dirLight.shadow.camera.left = - 5;
dirLight.shadow.camera.top = 5;
dirLight.shadow.camera.bottom = - 5;
dirLight.shadow.mapSize.width = 2048;
dirLight.shadow.mapSize.height = 2048;
dirLight.shadow.radius = 4;
dirLight.shadow.bias = - 0.0005;
scene.add( dirLight );

//

const loader = new GLTFLoader();
const gltf = await loader.loadAsync( 'models/gltf/DragonAttenuation.glb' );
gltf.scene.position.set( 0, 0, - .5 );

const floor = gltf.scene.children[ 0 ];
floor.scale.x += 4;
floor.scale.y += 4;

const dragon = gltf.scene.children[ 1 ];
dragon.position.set( - 1.5, - 0.8, 1 );

const dragon2 = dragon.clone();
dragon2.material = dragon.material.clone();
dragon2.material.attenuationColor = new THREE.Color( 0xff0000 );
dragon2.position.x += 4;
gltf.scene.add( dragon2 );

// shadow node

const customShadow = tslFn( ( [ color, opacity = .8 ] ) => {

return vec4( color, opacity );

} );

// apply shadow

floor.receiveShadow = true;

dragon.castShadow = dragon2.castShadow = true;
dragon.receiveShadow = dragon2.receiveShadow = true;

dragon.material.shadowNode = customShadow( dragon.material.attenuationColor );
dragon2.material.shadowNode = customShadow( dragon2.material.attenuationColor );

//

scene.add( gltf.scene );

const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 0.1;
controls.maxDistance = 10;
controls.target.set( 0, 0, 0 );
controls.update();

window.addEventListener( 'resize', onWindowResize );

}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

}

//

function render() {

renderer.render( scene, camera );

}

</script>

</body>
</html>
3 changes: 2 additions & 1 deletion src/nodes/lighting/AnalyticLightNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,13 @@ class AnalyticLightNode extends LightingNode {

}

const shadowColor = texture( shadowMap.texture, shadowCoord );
const shadowNode = frustumTest.cond( filterFn( { depthTexture, shadowCoord, shadow } ), float( 1 ) );

this.shadowMap = shadowMap;

this.shadowNode = shadowNode;
this.shadowColorNode = shadowColorNode = this.colorNode.mul( mix( 1, shadowNode, shadowIntensity ) );
this.shadowColorNode = shadowColorNode = this.colorNode.mul( mix( 1, shadowNode.rgb.mix( shadowColor, 1 ), shadowIntensity.mul( shadowColor.a ) ) );

this.baseColorNode = this.colorNode;

Expand Down