forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebgpu_sprites.html
158 lines (102 loc) · 3.81 KB
/
webgpu_sprites.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<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/[email protected]/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>