|
|
@@ -0,0 +1,148 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+ <head>
|
|
|
+ <title>three.js webgpu - height fog</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>Height Fog</span>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <small>
|
|
|
+ Exponential Height Fog with TSL.
|
|
|
+ </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 { exponentialHeightFogFactor, uniform, fog, color } from 'three/tsl';
|
|
|
+
|
|
|
+ import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
|
+ import { Inspector } from 'three/addons/inspector/Inspector.js';
|
|
|
+
|
|
|
+ let camera, scene, renderer;
|
|
|
+ let controls;
|
|
|
+
|
|
|
+ init();
|
|
|
+
|
|
|
+ function init() {
|
|
|
+
|
|
|
+ camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 600 );
|
|
|
+ camera.position.set( 20, 10, 25 );
|
|
|
+
|
|
|
+ scene = new THREE.Scene();
|
|
|
+
|
|
|
+ // height fog
|
|
|
+
|
|
|
+ const density = uniform( 0.04 );
|
|
|
+ const height = uniform( 2 );
|
|
|
+
|
|
|
+ const fogFactor = exponentialHeightFogFactor( density, height );
|
|
|
+
|
|
|
+ scene.fogNode = fog( color( 0xffdfc1 ), fogFactor );
|
|
|
+ scene.backgroundNode = color( 0xffdfc1 );
|
|
|
+
|
|
|
+ // meshes
|
|
|
+
|
|
|
+ const geometry = new THREE.BoxGeometry( 1, 25, 1 );
|
|
|
+ const material = new THREE.MeshPhongNodeMaterial( { color: 0xcd959a } );
|
|
|
+
|
|
|
+ const mesh = new THREE.InstancedMesh( geometry, material, 100 );
|
|
|
+ mesh.position.y = - 10;
|
|
|
+ scene.add( mesh );
|
|
|
+
|
|
|
+ const dummy = new THREE.Object3D();
|
|
|
+
|
|
|
+ let index = 0;
|
|
|
+
|
|
|
+ for ( let i = 0; i < 10; i ++ ) {
|
|
|
+
|
|
|
+ for ( let j = 0; j < 10; j ++ ) {
|
|
|
+
|
|
|
+ dummy.position.x = - 18 + ( i * 4 );
|
|
|
+ dummy.position.z = - 18 + ( j * 4 );
|
|
|
+
|
|
|
+ dummy.updateMatrix();
|
|
|
+
|
|
|
+ mesh.setMatrixAt( index ++, dummy.matrix );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // lights
|
|
|
+
|
|
|
+ const directionalLight = new THREE.DirectionalLight( 0xffc0cb, 2 );
|
|
|
+ directionalLight.position.set( - 10, 10, 10 );
|
|
|
+ scene.add( directionalLight );
|
|
|
+
|
|
|
+ const ambientLight = new THREE.AmbientLight( 0xcccccc );
|
|
|
+ scene.add( ambientLight );
|
|
|
+
|
|
|
+ // 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 );
|
|
|
+
|
|
|
+ // gui
|
|
|
+
|
|
|
+ const gui = renderer.inspector.createParameters( 'Fog Settings' );
|
|
|
+
|
|
|
+ gui.add( density, 'value', 0.001, 0.1 ).step( 0.0001 ).name( 'Density' );
|
|
|
+ gui.add( height, 'value', - 5, 5 ).name( 'Height' );
|
|
|
+
|
|
|
+ // controls
|
|
|
+
|
|
|
+ controls = new OrbitControls( camera, renderer.domElement );
|
|
|
+ controls.minDistance = 7;
|
|
|
+ controls.maxDistance = 100;
|
|
|
+ controls.maxPolarAngle = Math.PI / 2;
|
|
|
+ controls.enableDamping = 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();
|
|
|
+
|
|
|
+ renderer.render( scene, camera );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ </script>
|
|
|
+ </body>
|
|
|
+</html>
|