You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
5.3 KiB
177 lines
5.3 KiB
<html lang="en">
|
|
<head>
|
|
<title>three.js - WebGPU - Particles</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 - Particles
|
|
</div>
|
|
|
|
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
|
|
|
|
<script type="importmap">
|
|
{
|
|
"imports": {
|
|
"three": "../build/three.module.js",
|
|
"three/addons/": "./jsm/",
|
|
"three/nodes": "./jsm/nodes/Nodes.js"
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<script type="module">
|
|
|
|
import * as THREE from 'three';
|
|
import * as Nodes from 'three/nodes';
|
|
|
|
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
|
|
|
|
import { range, texture, mix, uv, mul, mod, rotateUV, color, max, min, div, clamp, positionWorld, invert, timerLocal } from 'three/nodes';
|
|
|
|
import WebGPU from 'three/addons/capabilities/WebGPU.js';
|
|
import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
|
|
|
|
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
|
|
|
|
let camera, scene, renderer;
|
|
let controls;
|
|
|
|
init();
|
|
|
|
function init() {
|
|
|
|
if ( WebGPU.isAvailable() === false ) {
|
|
|
|
document.body.appendChild( WebGPU.getErrorMessage() );
|
|
|
|
throw new Error( 'No WebGPU support' );
|
|
|
|
}
|
|
|
|
const { innerWidth, innerHeight } = window;
|
|
|
|
camera = new THREE.PerspectiveCamera( 60, innerWidth / innerHeight, 1, 5000 );
|
|
camera.position.set( 1300, 500, 0 );
|
|
|
|
scene = new THREE.Scene();
|
|
//scene.fogNode = new Nodes.FogRangeNode( Nodes.color( 0x0000ff ), Nodes.float( 1500 ), Nodes.float( 2100 ) );
|
|
|
|
// textures
|
|
|
|
const textureLoader = new THREE.TextureLoader();
|
|
const map = textureLoader.load( 'textures/opengameart/smoke1.png' );
|
|
|
|
// create nodes
|
|
|
|
const lifeRange = range( .1, 1 );
|
|
const offsetRange = range( new THREE.Vector3( - 2, 3, - 2 ), new THREE.Vector3( 2, 5, 2 ) );
|
|
|
|
const timer = timerLocal( .2, 1/*100000*/ ); // @TODO: need to work with 64-bit precision
|
|
|
|
const lifeTime = mod( mul( timer, lifeRange ), 1 );
|
|
const scaleRange = range( .3, 2 );
|
|
const rotateRange = range( .1, 4 );
|
|
|
|
const life = div( lifeTime, lifeRange );
|
|
|
|
const fakeLightEffect = max( .2, invert( positionWorld.y ) );
|
|
|
|
const textureNode = texture( map, rotateUV( uv(), mul( timer, rotateRange ) ) );
|
|
|
|
const opacityNode = mul( textureNode.a, invert( life ) );
|
|
|
|
const smokeColor = mix( color( 0x2c1501 ), color( 0x222222 ), clamp( mul( positionWorld.y, 3 ) ) );
|
|
|
|
// create particles
|
|
|
|
const smokeNodeMaterial = new Nodes.SpriteNodeMaterial();
|
|
smokeNodeMaterial.colorNode = mul( mix( color( 0xf27d0c ), smokeColor, min( mul( life, 2.5 ), 1 ) ), fakeLightEffect );
|
|
smokeNodeMaterial.opacityNode = opacityNode;
|
|
smokeNodeMaterial.positionNode = mul( offsetRange, lifeTime );
|
|
smokeNodeMaterial.scaleNode = mul( scaleRange, max( .3, lifeTime ) );
|
|
smokeNodeMaterial.depthWrite = false;
|
|
smokeNodeMaterial.transparent = true;
|
|
|
|
const smokeInstancedSprite = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), smokeNodeMaterial );
|
|
smokeInstancedSprite.scale.setScalar( 400 );
|
|
smokeInstancedSprite.isInstancedMesh = true;
|
|
smokeInstancedSprite.count = 2000;
|
|
scene.add( smokeInstancedSprite );
|
|
|
|
//
|
|
|
|
const fireNodeMaterial = new Nodes.SpriteNodeMaterial();
|
|
fireNodeMaterial.colorNode = mix( color( 0xb72f17 ), color( 0xb72f17 ), life );
|
|
fireNodeMaterial.positionNode = mul( range( new THREE.Vector3( - 1, 1, - 1 ), new THREE.Vector3( 1, 2, 1 ) ), lifeTime );
|
|
fireNodeMaterial.scaleNode = smokeNodeMaterial.scaleNode;
|
|
fireNodeMaterial.opacityNode = opacityNode;
|
|
fireNodeMaterial.blending = THREE.AdditiveBlending;
|
|
fireNodeMaterial.transparent = true;
|
|
fireNodeMaterial.depthWrite = false;
|
|
|
|
const fireInstancedSprite = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), fireNodeMaterial );
|
|
fireInstancedSprite.scale.setScalar( 400 );
|
|
fireInstancedSprite.isInstancedMesh = true;
|
|
fireInstancedSprite.count = 100;
|
|
fireInstancedSprite.position.y = - 100;
|
|
fireInstancedSprite.renderOrder = 1;
|
|
scene.add( fireInstancedSprite );
|
|
|
|
//
|
|
|
|
const helper = new THREE.GridHelper( 3000, 40, 0x303030, 0x303030 );
|
|
helper.material.colorNode = new Nodes.AttributeNode( 'color' );
|
|
helper.position.y = - 75;
|
|
scene.add( helper );
|
|
|
|
//
|
|
|
|
renderer = new WebGPURenderer();
|
|
renderer.setPixelRatio( window.devicePixelRatio );
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
renderer.setAnimationLoop( render );
|
|
document.body.appendChild( renderer.domElement );
|
|
|
|
//
|
|
|
|
controls = new OrbitControls( camera, renderer.domElement );
|
|
controls.maxDistance = 2700;
|
|
controls.target.set( 0, 500, 0 );
|
|
controls.update();
|
|
|
|
//
|
|
|
|
window.addEventListener( 'resize', onWindowResize );
|
|
|
|
// gui
|
|
|
|
const gui = new GUI();
|
|
|
|
gui.add( timer, 'scale', 0, 1, 0.01 ).name( 'speed' );
|
|
|
|
}
|
|
|
|
function onWindowResize() {
|
|
|
|
const { innerWidth, innerHeight } = window;
|
|
|
|
camera.aspect = innerWidth / innerHeight;
|
|
camera.updateProjectionMatrix();
|
|
|
|
renderer.setSize( innerWidth, innerHeight );
|
|
|
|
}
|
|
|
|
function render() {
|
|
|
|
renderer.render( scene, camera );
|
|
|
|
}
|
|
|
|
</script>
|
|
</body>
|
|
</html>
|
|
|