1
- import { mat4 } from 'wgpu-matrix' ;
1
+ import { mat4 , vec3 } from 'wgpu-matrix' ;
2
2
import { makeSample , SampleInit } from '../../components/SampleLayout' ;
3
3
import normalMapWGSL from './normalMap.wgsl' ;
4
4
import { createMeshRenderable } from '../../meshes/mesh' ;
@@ -33,7 +33,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
33
33
34
34
interface GUISettings {
35
35
'Bump Mode' :
36
- | 'Diffuse Texture'
36
+ | 'Albedo Texture'
37
37
| 'Normal Texture'
38
38
| 'Depth Texture'
39
39
| 'Normal Map'
@@ -60,7 +60,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
60
60
lightPosX : 1.7 ,
61
61
lightPosY : 0.7 ,
62
62
lightPosZ : - 1.9 ,
63
- lightIntensity : 0.02 ,
63
+ lightIntensity : 5.0 ,
64
64
depthScale : 0.05 ,
65
65
depthLayers : 16 ,
66
66
Texture : 'Spiral' ,
@@ -76,24 +76,26 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
76
76
usage : GPUTextureUsage . RENDER_ATTACHMENT ,
77
77
} ) ;
78
78
79
- const uniformBuffer = device . createBuffer ( {
79
+ const spaceTransformsBuffer = device . createBuffer ( {
80
80
// Buffer holding projection, view, and model matrices plus padding bytes
81
81
size : MAT4X4_BYTES * 4 ,
82
82
usage : GPUBufferUsage . UNIFORM | GPUBufferUsage . COPY_DST ,
83
83
} ) ;
84
84
85
- const mapMethodBuffer = device . createBuffer ( {
85
+ const mapInfoBuffer = device . createBuffer ( {
86
86
// Buffer holding mapping type, light uniforms, and depth uniforms
87
- size : Float32Array . BYTES_PER_ELEMENT * 7 ,
87
+ size : Float32Array . BYTES_PER_ELEMENT * 8 ,
88
88
usage : GPUBufferUsage . UNIFORM | GPUBufferUsage . COPY_DST ,
89
89
} ) ;
90
+ const mapInfoArray = new ArrayBuffer ( mapInfoBuffer . size ) ;
91
+ const mapInfoView = new DataView ( mapInfoArray , 0 , mapInfoArray . byteLength ) ;
90
92
91
93
// Fetch the image and upload it into a GPUTexture.
92
- let woodDiffuseTexture : GPUTexture ;
94
+ let woodAlbedoTexture : GPUTexture ;
93
95
{
94
- const response = await fetch ( '../assets/img/wood_diffuse .png' ) ;
96
+ const response = await fetch ( '../assets/img/wood_albedo .png' ) ;
95
97
const imageBitmap = await createImageBitmap ( await response . blob ( ) ) ;
96
- woodDiffuseTexture = createTextureFromImage ( device , imageBitmap ) ;
98
+ woodAlbedoTexture = createTextureFromImage ( device , imageBitmap ) ;
97
99
}
98
100
99
101
let spiralNormalTexture : GPUTexture ;
@@ -124,11 +126,11 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
124
126
toyboxHeightTexture = createTextureFromImage ( device , imageBitmap ) ;
125
127
}
126
128
127
- let brickwallDiffuseTexture : GPUTexture ;
129
+ let brickwallAlbedoTexture : GPUTexture ;
128
130
{
129
- const response = await fetch ( '../assets/img/brickwall_diffuse .png' ) ;
131
+ const response = await fetch ( '../assets/img/brickwall_albedo .png' ) ;
130
132
const imageBitmap = await createImageBitmap ( await response . blob ( ) ) ;
131
- brickwallDiffuseTexture = createTextureFromImage ( device , imageBitmap ) ;
133
+ brickwallAlbedoTexture = createTextureFromImage ( device , imageBitmap ) ;
132
134
}
133
135
134
136
let brickwallNormalTexture : GPUTexture ;
@@ -184,7 +186,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
184
186
] ,
185
187
[ 'buffer' , 'buffer' ] ,
186
188
[ { type : 'uniform' } , { type : 'uniform' } ] ,
187
- [ [ { buffer : uniformBuffer } , { buffer : mapMethodBuffer } ] ] ,
189
+ [ [ { buffer : spaceTransformsBuffer } , { buffer : mapInfoBuffer } ] ] ,
188
190
'Frame' ,
189
191
device
190
192
) ;
@@ -204,19 +206,19 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
204
206
[
205
207
[
206
208
sampler ,
207
- woodDiffuseTexture . createView ( ) ,
209
+ woodAlbedoTexture . createView ( ) ,
208
210
spiralNormalTexture . createView ( ) ,
209
211
spiralHeightTexture . createView ( ) ,
210
212
] ,
211
213
[
212
214
sampler ,
213
- woodDiffuseTexture . createView ( ) ,
215
+ woodAlbedoTexture . createView ( ) ,
214
216
toyboxNormalTexture . createView ( ) ,
215
217
toyboxHeightTexture . createView ( ) ,
216
218
] ,
217
219
[
218
220
sampler ,
219
- brickwallDiffuseTexture . createView ( ) ,
221
+ brickwallAlbedoTexture . createView ( ) ,
220
222
brickwallNormalTexture . createView ( ) ,
221
223
brickwallHeightTexture . createView ( ) ,
222
224
] ,
@@ -250,9 +252,9 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
250
252
}
251
253
252
254
// Change the model mapping type
253
- const getMappingType = ( ) : number => {
255
+ const getMode = ( ) : number => {
254
256
switch ( settings [ 'Bump Mode' ] ) {
255
- case 'Diffuse Texture' :
257
+ case 'Albedo Texture' :
256
258
return 0 ;
257
259
case 'Normal Texture' :
258
260
return 1 ;
@@ -285,7 +287,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
285
287
} ;
286
288
287
289
gui . add ( settings , 'Bump Mode' , [
288
- 'Diffuse Texture' ,
290
+ 'Albedo Texture' ,
289
291
'Normal Texture' ,
290
292
'Depth Texture' ,
291
293
'Normal Map' ,
@@ -301,7 +303,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
301
303
lightPosXController . setValue ( 1.7 ) ;
302
304
lightPosYController . setValue ( 0.7 ) ;
303
305
lightPosZController . setValue ( - 1.9 ) ;
304
- lightIntensityController . setValue ( 0.02 ) ;
306
+ lightIntensityController . setValue ( 5.0 ) ;
305
307
} ) ;
306
308
const lightPosXController = lightFolder
307
309
. add ( settings , 'lightPosX' , - 5 , 5 )
@@ -313,53 +315,54 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
313
315
. add ( settings , 'lightPosZ' , - 5 , 5 )
314
316
. step ( 0.1 ) ;
315
317
const lightIntensityController = lightFolder
316
- . add ( settings , 'lightIntensity' , 0.0 , 0.1 )
317
- . step ( 0.002 ) ;
318
+ . add ( settings , 'lightIntensity' , 0.0 , 10 )
319
+ . step ( 0.1 ) ;
318
320
depthFolder . add ( settings , 'depthScale' , 0.0 , 0.1 ) . step ( 0.01 ) ;
319
321
depthFolder . add ( settings , 'depthLayers' , 1 , 32 ) . step ( 1 ) ;
320
322
321
323
function frame ( ) {
322
324
if ( ! pageState . active ) return ;
323
325
324
- // Write to normal map shader
326
+ // Update spaceTransformsBuffer
325
327
const viewMatrix = getViewMatrix ( ) ;
326
-
327
- const modelMatrix = getModelMatrix ( ) ;
328
-
328
+ const worldViewMatrix = mat4 . mul ( viewMatrix , getModelMatrix ( ) ) ;
329
+ const worldViewProjMatrix = mat4 . mul ( projectionMatrix , worldViewMatrix ) ;
329
330
const matrices = new Float32Array ( [
330
- ...projectionMatrix ,
331
- ...viewMatrix ,
332
- ...modelMatrix ,
331
+ ...worldViewProjMatrix ,
332
+ ...worldViewMatrix ,
333
333
] ) ;
334
334
335
- const mappingType = getMappingType ( ) ;
336
-
335
+ // Update mapInfoBuffer
336
+ const lightPosWS = vec3 . create (
337
+ settings . lightPosX ,
338
+ settings . lightPosY ,
339
+ settings . lightPosZ
340
+ ) ;
341
+ const lightPosVS = vec3 . transformMat4 ( lightPosWS , viewMatrix ) ;
342
+ const mode = getMode ( ) ;
337
343
device . queue . writeBuffer (
338
- uniformBuffer ,
344
+ spaceTransformsBuffer ,
339
345
0 ,
340
346
matrices . buffer ,
341
347
matrices . byteOffset ,
342
348
matrices . byteLength
343
349
) ;
344
350
345
- device . queue . writeBuffer (
346
- mapMethodBuffer ,
347
- 0 ,
348
- new Uint32Array ( [ mappingType ] )
349
- ) ;
350
-
351
- device . queue . writeBuffer (
352
- mapMethodBuffer ,
353
- 4 ,
354
- new Float32Array ( [
355
- settings . lightPosX ,
356
- settings . lightPosY ,
357
- settings . lightPosZ ,
358
- settings . lightIntensity ,
359
- settings . depthScale ,
360
- settings . depthLayers ,
361
- ] )
362
- ) ;
351
+ // struct MapInfo {
352
+ // lightPosVS: vec3f,
353
+ // mode: u32,
354
+ // lightIntensity: f32,
355
+ // depthScale: f32,
356
+ // depthLayers: f32,
357
+ // }
358
+ mapInfoView . setFloat32 ( 0 , lightPosVS [ 0 ] , true ) ;
359
+ mapInfoView . setFloat32 ( 4 , lightPosVS [ 1 ] , true ) ;
360
+ mapInfoView . setFloat32 ( 8 , lightPosVS [ 2 ] , true ) ;
361
+ mapInfoView . setUint32 ( 12 , mode , true ) ;
362
+ mapInfoView . setFloat32 ( 16 , settings . lightIntensity , true ) ;
363
+ mapInfoView . setFloat32 ( 20 , settings . depthScale , true ) ;
364
+ mapInfoView . setFloat32 ( 24 , settings . depthLayers , true ) ;
365
+ device . queue . writeBuffer ( mapInfoBuffer , 0 , mapInfoArray ) ;
363
366
364
367
renderPassDescriptor . colorAttachments [ 0 ] . view = context
365
368
. getCurrentTexture ( )
0 commit comments