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.
159 lines
3.8 KiB
159 lines
3.8 KiB
2 years ago
|
<html lang="en">
|
||
|
<head>
|
||
|
<title>three.js - WebGPU - Sprites</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 - Sprites
|
||
|
</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 { texture, uv, mul, float, color, userData } from 'three/nodes';
|
||
|
|
||
|
import WebGPU from 'three/addons/capabilities/WebGPU.js';
|
||
|
import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
|
||
|
|
||
|
let camera, scene, renderer;
|
||
|
|
||
|
let map;
|
||
|
|
||
|
let group;
|
||
|
|
||
|
let imageWidth = 1, imageHeight = 1;
|
||
|
|
||
|
init();
|
||
|
|
||
|
function init() {
|
||
|
|
||
|
if ( WebGPU.isAvailable() === false ) {
|
||
|
|
||
|
document.body.appendChild( WebGPU.getErrorMessage() );
|
||
|
|
||
|
throw new Error( 'No WebGPU support' );
|
||
|
|
||
|
}
|
||
|
|
||
|
const width = window.innerWidth;
|
||
|
const height = window.innerHeight;
|
||
|
|
||
|
camera = new THREE.PerspectiveCamera( 60, width / height, 1, 2100 );
|
||
|
camera.position.z = 1500;
|
||
|
|
||
|
scene = new THREE.Scene();
|
||
|
scene.fogNode = new Nodes.FogRangeNode( color( 0x0000ff ), float( 1500 ), float( 2100 ) );
|
||
|
|
||
|
// create sprites
|
||
|
|
||
|
const amount = 200;
|
||
|
const radius = 500;
|
||
|
|
||
|
const textureLoader = new THREE.TextureLoader();
|
||
|
|
||
|
map = textureLoader.load( 'textures/sprite1.png', ( map ) => {
|
||
|
|
||
|
imageWidth = map.image.width;
|
||
|
imageHeight = map.image.height;
|
||
|
|
||
|
} );
|
||
|
|
||
|
group = new THREE.Group();
|
||
|
|
||
|
const textureNode = texture( map );
|
||
|
|
||
|
const material = new Nodes.SpriteNodeMaterial();
|
||
|
material.colorNode = mul( textureNode, mul( uv(), 2 ) );
|
||
|
material.opacityNode = textureNode.a;
|
||
|
material.rotationNode = userData( 'rotation', 'float' ); // get value of: sprite.userData.rotation
|
||
|
material.transparent = true;
|
||
|
|
||
|
for ( let a = 0; a < amount; a ++ ) {
|
||
|
|
||
|
const x = Math.random() - 0.5;
|
||
|
const y = Math.random() - 0.5;
|
||
|
const z = Math.random() - 0.5;
|
||
|
|
||
|
const sprite = new THREE.Sprite( material );
|
||
|
|
||
|
sprite.position.set( x, y, z );
|
||
|
sprite.position.normalize();
|
||
|
sprite.position.multiplyScalar( radius );
|
||
|
|
||
|
// individual rotation per sprite
|
||
|
sprite.userData.rotation = 0;
|
||
|
|
||
|
group.add( sprite );
|
||
|
|
||
|
}
|
||
|
|
||
|
scene.add( group );
|
||
|
|
||
|
//
|
||
|
|
||
|
renderer = new WebGPURenderer( { requiredFeatures: [ 'texture-compression-bc' ] } );
|
||
|
renderer.setPixelRatio( window.devicePixelRatio );
|
||
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
||
|
renderer.setAnimationLoop( render );
|
||
|
document.body.appendChild( renderer.domElement );
|
||
|
|
||
|
window.addEventListener( 'resize', onWindowResize );
|
||
|
|
||
|
}
|
||
|
|
||
|
function onWindowResize() {
|
||
|
|
||
|
const width = window.innerWidth;
|
||
|
const height = window.innerHeight;
|
||
|
|
||
|
camera.aspect = width / height;
|
||
|
camera.updateProjectionMatrix();
|
||
|
|
||
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
||
|
|
||
|
}
|
||
|
|
||
|
function render() {
|
||
|
|
||
|
const time = Date.now() / 1000;
|
||
|
|
||
|
for ( let i = 0, l = group.children.length; i < l; i ++ ) {
|
||
|
|
||
|
const sprite = group.children[ i ];
|
||
|
const scale = Math.sin( time + sprite.position.x * 0.01 ) * 0.3 + 1.0;
|
||
|
|
||
|
sprite.userData.rotation += 0.1 * ( i / l );
|
||
|
sprite.scale.set( scale * imageWidth, scale * imageHeight, 1.0 );
|
||
|
|
||
|
}
|
||
|
|
||
|
group.rotation.x = time * 0.5;
|
||
|
group.rotation.y = time * 0.75;
|
||
|
group.rotation.z = time * 1.0;
|
||
|
|
||
|
renderer.render( scene, camera );
|
||
|
|
||
|
}
|
||
|
|
||
|
</script>
|
||
|
</body>
|
||
|
</html>
|