-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathwebgpu_lights_tiled.html
More file actions
236 lines (157 loc) · 6.35 KB
/
webgpu_lights_tiled.html
File metadata and controls
236 lines (157 loc) · 6.35 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgpu - tiled lighting</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>Tiled Lighting</span>
</div>
<small>
Custom compute-based Tiled Lighting.
</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 { texture, uv, pass, normalMap, uniform } from 'three/tsl';
import { bloom } from 'three/addons/tsl/display/BloomNode.js';
import { TiledLighting } from 'three/addons/lighting/TiledLighting.js';
import { Inspector } from 'three/addons/inspector/Inspector.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import WebGPU from 'three/addons/capabilities/WebGPU.js';
let camera, scene, renderer,
lights, lightDummy,
controls,
compose, tileInfluence,
lighting,
count,
renderPipeline;
init();
function init() {
if ( WebGPU.isAvailable() === false ) {
document.body.appendChild( WebGPU.getErrorMessage() );
throw new Error( 'No WebGPU support' );
}
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 600 );
camera.position.z = 200;
camera.position.y = 30;
scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x111111, 300, 500 );
scene.background = new THREE.Color( 0x111111 );
count = 1000;
const material = new THREE.MeshBasicMaterial();
lightDummy = new THREE.InstancedMesh( new THREE.SphereGeometry( 0.1, 16, 8 ), material, count );
lightDummy.instanceMatrix.setUsage( THREE.DynamicDrawUsage );
scene.add( lightDummy );
// lights
lights = new THREE.Group();
scene.add( lights );
const addLight = ( hexColor, power = 10, distance = 3 ) => {
const light = new THREE.PointLight( hexColor, 1, distance );
light.position.set( Math.random() * 300 - 150, 1, Math.random() * 300 - 150 );
light.power = power;
light.userData.fixedPosition = light.position.clone();
lights.add( light );
return light;
};
const color = new THREE.Color();
for ( let i = 0; i < count; i ++ ) {
const hex = ( Math.random() * 0xffffff ) + 0x666666;
lightDummy.setColorAt( i, color.setHex( hex ) );
addLight( hex );
}
//
const lightAmbient = new THREE.AmbientLight( 0xffffff, .1 );
scene.add( lightAmbient );
// textures
const textureLoader = new THREE.TextureLoader();
const floorColor = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Diffuse.jpg' );
floorColor.wrapS = THREE.RepeatWrapping;
floorColor.wrapT = THREE.RepeatWrapping;
floorColor.colorSpace = THREE.SRGBColorSpace;
const floorNormal = textureLoader.load( 'textures/floors/FloorsCheckerboard_S_Normal.jpg' );
floorNormal.wrapS = THREE.RepeatWrapping;
floorNormal.wrapT = THREE.RepeatWrapping;
const uvTile = uv().mul( 50 );
const planeGeometry = new THREE.PlaneGeometry( 1000, 1000 );
const planeMaterial = new THREE.MeshPhongNodeMaterial( {
colorNode: texture( floorColor, uvTile ),
normalNode: normalMap( texture( floorNormal, uvTile ) ),
} );
const ground = new THREE.Mesh( planeGeometry, planeMaterial );
ground.rotation.x = - Math.PI / 2;
ground.position.y = 0;
ground.castShadow = true;
ground.receiveShadow = true;
scene.add( ground );
// renderer
lighting = new TiledLighting(); // ( maxLights = 1024, tileSize = 32 )
renderer = new THREE.WebGPURenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
renderer.lighting = lighting; // set lighting system
renderer.toneMapping = THREE.NeutralToneMapping;
renderer.toneMappingExposure = 5;
renderer.inspector = new Inspector();
document.body.appendChild( renderer.domElement );
// controls
controls = new OrbitControls( camera, renderer.domElement );
controls.maxDistance = 400;
// events
window.addEventListener( 'resize', onWindowResize );
// post processing
const scenePass = pass( scene, camera );
const bloomPass = bloom( scenePass, 3, .9, .2 );
// compose
compose = scenePass.add( bloomPass );
tileInfluence = uniform( 0 );
renderPipeline = new THREE.RenderPipeline( renderer );
updatePostProcessing();
// gui
const gui = renderer.inspector.createParameters( 'Settings' );
gui.add( tileInfluence, 'value', 0, 1 ).name( 'tile indexes debug' );
}
function updatePostProcessing() {
// tile indexes debug, needs to be updated every time the renderer size changes
const debugBlockIndexes = lighting.getNode( scene ).setSize( window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio ).getBlock().toColor().div( count * 2 );
renderPipeline.outputNode = compose.add( debugBlockIndexes.mul( tileInfluence ) );
renderPipeline.needsUpdate = true;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
updatePostProcessing();
}
function animate() {
const time = performance.now() / 1000;
for ( let i = 0; i < lights.children.length; i ++ ) {
const light = lights.children[ i ];
const lightTime = ( time * 0.5 ) + light.id;
light.position.copy( light.userData.fixedPosition );
light.position.x += Math.sin( lightTime * 0.7 ) * 3;
light.position.y += Math.cos( lightTime * 0.5 ) * .5;
light.position.z += Math.cos( lightTime * 0.3 ) * 3;
lightDummy.setMatrixAt( i, light.matrixWorld );
}
renderPipeline.render();
}
</script>
</body>
</html>