|
|
@@ -0,0 +1,211 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+ <head>
|
|
|
+ <title>three.js webgpu - fog - scattering</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" class="invert">
|
|
|
+ <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>Fog - Scattering</span>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <small>
|
|
|
+ The demo simulates light scattering due to fog.<br/>
|
|
|
+ </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 { positionWorld, densityFogFactor, pass, vec2, uniform, mix, color } from 'three/tsl';
|
|
|
+ import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';
|
|
|
+
|
|
|
+ import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
|
+ import { Inspector } from 'three/addons/inspector/Inspector.js';
|
|
|
+
|
|
|
+ let camera, scene, renderer, postProcessing, controls;
|
|
|
+
|
|
|
+ const params = {
|
|
|
+ scatteringEnabled: true
|
|
|
+ };
|
|
|
+
|
|
|
+ init();
|
|
|
+
|
|
|
+ async function init() {
|
|
|
+
|
|
|
+ camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 600 );
|
|
|
+ camera.position.set( 30, 10, 30 );
|
|
|
+
|
|
|
+ scene = new THREE.Scene();
|
|
|
+
|
|
|
+ // fog
|
|
|
+
|
|
|
+ scene.fog = new THREE.FogExp2( 0xcccccc, 0.014 );
|
|
|
+ scene.background = new THREE.Color( 0xcccccc );
|
|
|
+
|
|
|
+ // builds
|
|
|
+
|
|
|
+ const buildWindows = positionWorld.y.mul( 10 ).floor().mod( 4 ).sign().mix( color( 0x000066 ), color( 0xffffff ) );
|
|
|
+
|
|
|
+ const buildGeometry = new THREE.BoxGeometry( 1, 1, 1 );
|
|
|
+ const buildMaterial = new THREE.MeshPhongNodeMaterial( {
|
|
|
+ colorNode: buildWindows
|
|
|
+ } );
|
|
|
+
|
|
|
+ const buildMesh = new THREE.InstancedMesh( buildGeometry, buildMaterial, 4000 );
|
|
|
+ scene.add( buildMesh );
|
|
|
+
|
|
|
+ const dummy = new THREE.Object3D();
|
|
|
+ const center = new THREE.Vector3();
|
|
|
+
|
|
|
+ for ( let i = 0; i < buildMesh.count; i ++ ) {
|
|
|
+
|
|
|
+ const scaleY = Math.random() * 7 + .5;
|
|
|
+
|
|
|
+ dummy.position.x = Math.random() * 600 - 300;
|
|
|
+ dummy.position.z = Math.random() * 600 - 300;
|
|
|
+
|
|
|
+ const distance = Math.max( dummy.position.distanceTo( center ) * .012, 1 );
|
|
|
+
|
|
|
+ dummy.position.y = .5 * scaleY * distance;
|
|
|
+
|
|
|
+ dummy.scale.x = dummy.scale.z = Math.random() * 3 + .5;
|
|
|
+ dummy.scale.y = scaleY * distance;
|
|
|
+
|
|
|
+ dummy.updateMatrix();
|
|
|
+
|
|
|
+ buildMesh.setMatrixAt( i, dummy.matrix );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // lights
|
|
|
+
|
|
|
+ scene.add( new THREE.HemisphereLight( 0xf0f5f5, 0xd0dee7, 0.5 ) );
|
|
|
+
|
|
|
+ // geometry
|
|
|
+
|
|
|
+ const planeGeometry = new THREE.PlaneGeometry( 200, 200 );
|
|
|
+ const planeMaterial = new THREE.MeshPhongMaterial( {
|
|
|
+ color: 0x999999
|
|
|
+ } );
|
|
|
+
|
|
|
+ const ground = new THREE.Mesh( planeGeometry, planeMaterial );
|
|
|
+ ground.rotation.x = - Math.PI / 2;
|
|
|
+ ground.scale.multiplyScalar( 3 );
|
|
|
+ ground.castShadow = true;
|
|
|
+ ground.receiveShadow = true;
|
|
|
+ scene.add( ground );
|
|
|
+
|
|
|
+ // renderer
|
|
|
+
|
|
|
+ renderer = new THREE.WebGPURenderer( { antialias: true } );
|
|
|
+ renderer.setPixelRatio( window.devicePixelRatio );
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+ renderer.setAnimationLoop( animate );
|
|
|
+ renderer.inspector = new Inspector();
|
|
|
+ document.body.appendChild( renderer.domElement );
|
|
|
+
|
|
|
+ // controls
|
|
|
+
|
|
|
+ controls = new OrbitControls( camera, renderer.domElement );
|
|
|
+ controls.target.set( 0, 2, 0 );
|
|
|
+ controls.minDistance = 7;
|
|
|
+ controls.maxDistance = 100;
|
|
|
+ controls.maxPolarAngle = Math.PI / 2;
|
|
|
+ controls.enableDamping = true;
|
|
|
+ controls.update();
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ postProcessing = new THREE.PostProcessing( renderer );
|
|
|
+
|
|
|
+ // uniforms
|
|
|
+
|
|
|
+ const density = uniform( 0.014 );
|
|
|
+ const scattering = uniform( 2 );
|
|
|
+
|
|
|
+ // scene pass
|
|
|
+
|
|
|
+ const scenePass = pass( scene, camera );
|
|
|
+
|
|
|
+ const scenePassColor = scenePass.getTextureNode( 'output' );
|
|
|
+ const scenePassViewZ = scenePass.getViewZNode();
|
|
|
+
|
|
|
+ // blur pass (always downsampled to improve performance)
|
|
|
+
|
|
|
+ const sceneColorBlurred = gaussianBlur( scenePassColor, vec2( scattering ), 4, { resolutionScale: 0.5 } );
|
|
|
+
|
|
|
+ // composite
|
|
|
+
|
|
|
+ const fogFactor = densityFogFactor( density ).context( { getViewZ: () => scenePassViewZ } );
|
|
|
+
|
|
|
+ const compositeNode = mix( scenePassColor, sceneColorBlurred, fogFactor );
|
|
|
+
|
|
|
+ postProcessing.outputNode = compositeNode;
|
|
|
+
|
|
|
+ // gui
|
|
|
+
|
|
|
+ const gui = renderer.inspector.createParameters( 'Settings' );
|
|
|
+ gui.add( density, 'value', 0.005, 0.03 ).step( 0.0001 ).name( 'fog density' ).onChange( ( value ) => {
|
|
|
+
|
|
|
+ scene.fog.density = value;
|
|
|
+
|
|
|
+ } );
|
|
|
+ gui.add( scattering, 'value', 0, 5 ).name( 'scattering factor' );
|
|
|
+ gui.add( params, 'scatteringEnabled' ).name( 'enable scattering' ).onChange( ( value ) => {
|
|
|
+
|
|
|
+ if ( value === true ) {
|
|
|
+
|
|
|
+ postProcessing.outputNode = compositeNode;
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ postProcessing.outputNode = scenePassColor;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ postProcessing.needsUpdate = true;
|
|
|
+
|
|
|
+ } );
|
|
|
+
|
|
|
+ window.addEventListener( 'resize', resize );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function resize() {
|
|
|
+
|
|
|
+ camera.aspect = window.innerWidth / window.innerHeight;
|
|
|
+ camera.updateProjectionMatrix();
|
|
|
+
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function animate() {
|
|
|
+
|
|
|
+ controls.update();
|
|
|
+
|
|
|
+ postProcessing.render();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ </script>
|
|
|
+ </body>
|
|
|
+</html>
|