| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgpu - volumetric cloud</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>Volumetric Cloud</span>
- </div>
- <small>
- Volumetric cloud using 3D texture raymarching.
- </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 { float, vec3, vec4, If, Break, Fn, smoothstep, texture3D, uniform } from 'three/tsl';
- import { RaymarchingBox } from 'three/addons/tsl/utils/Raymarching.js';
- import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
- import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';
- import { Inspector } from 'three/addons/inspector/Inspector.js';
- let renderer, scene, camera;
- let mesh;
- init();
- function init() {
- 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 );
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
- camera.position.set( 0, 0, 1.5 );
- new OrbitControls( camera, renderer.domElement );
- // Sky
- const canvas = document.createElement( 'canvas' );
- canvas.width = 1;
- canvas.height = 32;
- const context = canvas.getContext( '2d' );
- const gradient = context.createLinearGradient( 0, 0, 0, 32 );
- gradient.addColorStop( 0.0, '#014a84' );
- gradient.addColorStop( 0.5, '#0561a0' );
- gradient.addColorStop( 1.0, '#437ab6' );
- context.fillStyle = gradient;
- context.fillRect( 0, 0, 1, 32 );
- const skyMap = new THREE.CanvasTexture( canvas );
- skyMap.colorSpace = THREE.SRGBColorSpace;
- const sky = new THREE.Mesh(
- new THREE.SphereGeometry( 10 ),
- new THREE.MeshBasicNodeMaterial( { map: skyMap, side: THREE.BackSide } )
- );
- scene.add( sky );
- // Texture
- const size = 128;
- const data = new Uint8Array( size * size * size );
- let i = 0;
- const scale = 0.05;
- const perlin = new ImprovedNoise();
- const vector = new THREE.Vector3();
- for ( let z = 0; z < size; z ++ ) {
- for ( let y = 0; y < size; y ++ ) {
- for ( let x = 0; x < size; x ++ ) {
- const d = 1.0 - vector.set( x, y, z ).subScalar( size / 2 ).divideScalar( size ).length();
- data[ i ] = ( 128 + 128 * perlin.noise( x * scale / 1.5, y * scale, z * scale / 1.5 ) ) * d * d;
- i ++;
- }
- }
- }
- const texture = new THREE.Data3DTexture( data, size, size, size );
- texture.format = THREE.RedFormat;
- texture.minFilter = THREE.LinearFilter;
- texture.magFilter = THREE.LinearFilter;
- texture.unpackAlignment = 1;
- texture.needsUpdate = true;
- // Shader
- const transparentRaymarchingTexture = Fn( ( {
- texture,
- range = float( 0.1 ),
- threshold = float( 0.25 ),
- opacity = float( 0.25 ),
- steps = float( 100 )
- } ) => {
- const finalColor = vec4( 0 ).toVar();
- RaymarchingBox( steps, ( { positionRay } ) => {
- const mapValue = float( texture.sample( positionRay.add( 0.5 ) ).r ).toVar();
- mapValue.assign( smoothstep( threshold.sub( range ), threshold.add( range ), mapValue ).mul( opacity ) );
- const shading = texture.sample( positionRay.add( vec3( - 0.01 ) ) ).r.sub( texture.sample( positionRay.add( vec3( 0.01 ) ) ).r );
- const col = shading.mul( 3.0 ).add( positionRay.x.add( positionRay.y ).mul( 0.25 ) ).add( 0.2 );
- finalColor.rgb.addAssign( finalColor.a.oneMinus().mul( mapValue ).mul( col ) );
- finalColor.a.addAssign( finalColor.a.oneMinus().mul( mapValue ) );
- If( finalColor.a.greaterThanEqual( 0.95 ), () => {
- Break();
- } );
- } );
- return finalColor;
- } );
- // Material
- const baseColor = uniform( new THREE.Color( 0x798aa0 ) );
- const range = uniform( 0.1 );
- const threshold = uniform( 0.25 );
- const opacity = uniform( 0.25 );
- const steps = uniform( 100 );
- const cloud3d = transparentRaymarchingTexture( {
- texture: texture3D( texture, null, 0 ),
- range,
- threshold,
- opacity,
- steps
- } );
- const finalCloud = cloud3d.setRGB( cloud3d.rgb.add( baseColor ) );
- const material = new THREE.NodeMaterial();
- material.colorNode = finalCloud;
- material.side = THREE.BackSide;
- material.transparent = true;
- mesh = new THREE.Mesh( new THREE.BoxGeometry( 1, 1, 1 ), material );
- scene.add( mesh );
- //
- const gui = renderer.inspector.createParameters( 'Parameters' );
- gui.add( threshold, 'value', 0, 1, 0.01 ).name( 'threshold' );
- gui.add( opacity, 'value', 0, 1, 0.01 ).name( 'opacity' );
- gui.add( range, 'value', 0, 1, 0.01 ).name( 'range' );
- gui.add( steps, 'value', 0, 200, 1 ).name( 'steps' );
- window.addEventListener( 'resize', onWindowResize );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- mesh.rotation.y = - performance.now() / 7500;
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|