| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - postprocessing - godrays</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="example.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
- <div class="title-wrapper">
- <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Godrays</span>
- </div>
- <small>
- Screen-space raymarched Godrays.
- </small>
- </div>
- <script type="importmap">
- {
- "imports": {
- "three": "../build/three.webgpu.js",
- "three/webgpu": "../build/three.webgpu.js",
- "three/tsl": "../build/three.tsl.js",
- "three/addons/": "./jsm/"
- }
- }
- </script>
- <script type="module">
- import * as THREE from 'three/webgpu';
- import { pass, uniform, float, int, color } from 'three/tsl';
- import { godrays } from 'three/addons/tsl/display/GodraysNode.js';
- import { bilateralBlur } from 'three/addons/tsl/display/BilateralBlurNode.js';
- import { depthAwareBlend } from 'three/addons/tsl/display/depthAwareBlend.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
- import { Inspector } from 'three/addons/inspector/Inspector.js';
- let camera, controls, scene, renderer, renderPipeline;
- const params = {
- enabledBlur: true,
- };
- init();
- async function init() {
- camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 );
- camera.position.set( - 175, 50, 0 );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x000000 );
- // asset
- const loader = new GLTFLoader();
- const gltf = await loader.loadAsync( 'models/gltf/godrays_demo.glb' );
- scene.add( gltf.scene );
- const pillars = gltf.scene.getObjectByName( 'concrete' );
- pillars.material = new THREE.MeshStandardMaterial( {
- color: 0x333333,
- } );
- const base = gltf.scene.getObjectByName( 'base' );
- base.material = new THREE.MeshStandardMaterial( {
- color: 0x333333,
- side: THREE.DoubleSide,
- } );
- // lights
- const lightPos = new THREE.Vector3( 0, 50, 0 );
- const lightSphereMaterial = new THREE.MeshBasicMaterial( {
- color: 0xffffff,
- } );
- const lightSphere = new THREE.Mesh( new THREE.SphereGeometry( 0.5, 16, 16 ), lightSphereMaterial );
- lightSphere.position.copy( lightPos );
- scene.add( lightSphere );
- scene.add( new THREE.AmbientLight( 0xcccccc, 0.4 ) );
- const pointLight = new THREE.PointLight( 0xf6287d, 10000 );
- pointLight.castShadow = true;
- pointLight.shadow.bias = - 0.00001;
- pointLight.shadow.mapSize.width = 2048;
- pointLight.shadow.mapSize.height = 2048;
- pointLight.position.copy( lightPos );
- scene.add( pointLight );
- setupBackdrop();
- // shadow setup
- scene.traverse( obj => {
- if ( obj.isMesh === true ) {
- obj.castShadow = true;
- obj.receiveShadow = true;
- }
- } );
- lightSphere.castShadow = false;
- lightSphere.receiveShadow = false;
- // renderer
- renderer = new THREE.WebGPURenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setAnimationLoop( animate );
- renderer.inspector = new Inspector();
- renderer.shadowMap.enabled = true;
- document.body.appendChild( renderer.domElement );
- // post processing
- renderPipeline = new THREE.RenderPipeline( renderer );
- // beauty
- const scenePass = pass( scene, camera );
- const scenePassColor = scenePass.getTextureNode( 'output' );
- const scenePassDepth = scenePass.getTextureNode( 'depth' );
- // godrays
-
- const godraysPass = godrays( scenePassDepth, camera, pointLight );
- const godraysPassColor = godraysPass.getTextureNode();
- // blur
- const blurPass = bilateralBlur( godraysPassColor );
- const blurPassColor = blurPass.getTextureNode();
- // composite
- const blendColor = uniform( color( 0xf6287d ) );
- const edgeRadius = uniform( int( 2 ) );
- const edgeStrength = uniform( float( 2 ) );
- const outputBlurred = depthAwareBlend( scenePassColor, blurPassColor, scenePassDepth, camera, { blendColor, edgeRadius, edgeStrength } );
- const outputRaw = depthAwareBlend( scenePassColor, godraysPassColor, scenePassDepth, camera, { blendColor, edgeRadius, edgeStrength } );
-
- renderPipeline.outputNode = outputBlurred;
- // GUI
- const gui = renderer.inspector.createParameters( 'Settings' );
- const godraysFolder = gui.addFolder( 'Godrays' );
- godraysFolder.add( godraysPass.raymarchSteps, 'value', 24, 120 ).step( 1 ).name( 'raymarch steps' );
- godraysFolder.add( godraysPass.density, 'value', 0, 1 ).name( 'density' );
- godraysFolder.add( godraysPass.maxDensity, 'value', 0, 1 ).name( 'max density' );
- godraysFolder.add( godraysPass.distanceAttenuation, 'value', 0, 5 ).name( 'distance attenuation' );
-
- const compositeFolder = gui.addFolder( 'composite' );
- compositeFolder.add( edgeRadius, 'value', 0, 5, 1 ).name( 'edge radius' );
- compositeFolder.add( edgeStrength, 'value', 0, 5 ).name( 'edge strength' );
-
- const blurFolder = gui.addFolder( 'blur' );
- blurFolder.add( params, 'enabledBlur' ).name( 'enabled' ).onChange( ( value ) => {
- if ( value === true ) {
- renderPipeline.outputNode = outputBlurred;
- } else {
- renderPipeline.outputNode = outputRaw;
- }
- renderPipeline.needsUpdate = true;
- } );
- //
- controls = new OrbitControls( camera, renderer.domElement );
- controls.target.set( 0, 0.5, 0 );
- controls.enableDamping = true;
- controls.maxDistance = 200;
- controls.update();
- window.addEventListener( 'resize', onWindowResize );
- }
- function setupBackdrop() {
- const backdropDistance = 200;
- // Add backdrop walls `backdropDistance` units away from the origin
- const backdropGeometry = new THREE.PlaneGeometry( 400, 200 );
- const backdropMaterial = new THREE.MeshBasicMaterial( {
- color: 0x000000,
- side: THREE.DoubleSide,
- } );
- const backdropLeft = new THREE.Mesh( backdropGeometry, backdropMaterial );
- backdropLeft.position.set( - backdropDistance, 100, 0 );
- backdropLeft.rotateY( Math.PI / 2 );
- scene.add( backdropLeft );
-
- const backdropRight = new THREE.Mesh( backdropGeometry, backdropMaterial );
- backdropRight.position.set( backdropDistance, 100, 0 );
- backdropRight.rotateY( Math.PI / 2 );
- scene.add( backdropRight );
-
- const backdropFront = new THREE.Mesh( backdropGeometry, backdropMaterial );
- backdropFront.position.set( 0, 100, - backdropDistance );
- scene.add( backdropFront );
-
- const backdropBack = new THREE.Mesh( backdropGeometry, backdropMaterial );
- backdropBack.position.set( 0, 100, backdropDistance );
- scene.add( backdropBack );
-
- const backdropTop = new THREE.Mesh( backdropGeometry, backdropMaterial );
- backdropTop.position.set( 0, 200, 0 );
- backdropTop.rotateX( Math.PI / 2 );
- backdropTop.scale.set( 3, 6, 1 );
- scene.add( backdropTop );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- controls.update();
- renderPipeline.render();
- }
- </script>
- </body>
- </html>
|