Skip to content

Commit f2eaf77

Browse files
authored
Fix image loading in normalMap sample (#310)
* Moved images to assets folder * Fixed image loading * Fixed incorrect light repositioning on Reset Light * Fixed unintended changes
1 parent 513ba7e commit f2eaf77

File tree

10 files changed

+100
-96
lines changed

10 files changed

+100
-96
lines changed

assets/img/brickwall_diffuse.png

112 KB
Loading

assets/img/brickwall_height.png

48.6 KB
Loading

assets/img/brickwall_normal.png

57.9 KB
Loading

assets/img/spiral_height.png

69.3 KB
Loading

assets/img/spiral_normal.png

374 KB
Loading

assets/img/toybox_height.png

29.7 KB
Loading

assets/img/toybox_normal.png

53.1 KB
Loading

assets/img/wood_diffuse.png

61.4 KB
Loading

src/sample/normalMap/main.ts

Lines changed: 100 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import normalMapWGSL from './normalMap.wgsl';
44
import { createMeshRenderable } from '../../meshes/mesh';
55
import { createBoxMeshWithTangents } from '../../meshes/box';
66
import {
7-
PBRDescriptor,
8-
createPBRDescriptor,
97
createBindGroupDescriptor,
108
create3DRenderPipeline,
9+
createTextureFromImage,
1110
} from './utils';
1211

1312
const MAT4X4_BYTES = 64;
@@ -89,35 +88,101 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
8988
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
9089
});
9190

92-
// Create PBR info (diffuse, normal, and depth/height textures)
93-
let spiralPBR: Required<PBRDescriptor>;
91+
// Fetch the image and upload it into a GPUTexture.
92+
let woodDiffuseTexture: GPUTexture;
9493
{
95-
const response = await createPBRDescriptor(device, [
96-
'wood_diffuse.png',
97-
'spiral_normal.png',
98-
'spiral_height.png',
99-
]);
100-
spiralPBR = response as Required<PBRDescriptor>;
94+
const response = await fetch(
95+
new URL(
96+
'../../../assets/img/wood_diffuse.png',
97+
import.meta.url
98+
).toString()
99+
);
100+
const imageBitmap = await createImageBitmap(await response.blob());
101+
woodDiffuseTexture = createTextureFromImage(device, imageBitmap);
101102
}
102103

103-
let toyboxPBR: Required<PBRDescriptor>;
104+
let spiralNormalTexture: GPUTexture;
104105
{
105-
const response = await createPBRDescriptor(device, [
106-
'wood_diffuse.png',
107-
'toybox_normal.png',
108-
'toybox_height.png',
109-
]);
110-
toyboxPBR = response as Required<PBRDescriptor>;
106+
const response = await fetch(
107+
new URL(
108+
'../../../assets/img/spiral_normal.png',
109+
import.meta.url
110+
).toString()
111+
);
112+
const imageBitmap = await createImageBitmap(await response.blob());
113+
spiralNormalTexture = createTextureFromImage(device, imageBitmap);
111114
}
112115

113-
let brickWallPBR: Required<PBRDescriptor>;
116+
let spiralHeightTexture: GPUTexture;
114117
{
115-
const response = await createPBRDescriptor(device, [
116-
'brickwall_diffuse.png',
117-
'brickwall_normal.png',
118-
'brickwall_height.png',
119-
]);
120-
brickWallPBR = response as Required<PBRDescriptor>;
118+
const response = await fetch(
119+
new URL(
120+
'../../../assets/img/spiral_height.png',
121+
import.meta.url
122+
).toString()
123+
);
124+
const imageBitmap = await createImageBitmap(await response.blob());
125+
spiralHeightTexture = createTextureFromImage(device, imageBitmap);
126+
}
127+
128+
let toyboxNormalTexture: GPUTexture;
129+
{
130+
const response = await fetch(
131+
new URL(
132+
'../../../assets/img/toybox_normal.png',
133+
import.meta.url
134+
).toString()
135+
);
136+
const imageBitmap = await createImageBitmap(await response.blob());
137+
toyboxNormalTexture = createTextureFromImage(device, imageBitmap);
138+
}
139+
140+
let toyboxHeightTexture: GPUTexture;
141+
{
142+
const response = await fetch(
143+
new URL(
144+
'../../../assets/img/toybox_height.png',
145+
import.meta.url
146+
).toString()
147+
);
148+
const imageBitmap = await createImageBitmap(await response.blob());
149+
toyboxHeightTexture = createTextureFromImage(device, imageBitmap);
150+
}
151+
152+
let brickwallDiffuseTexture: GPUTexture;
153+
{
154+
const response = await fetch(
155+
new URL(
156+
'../../../assets/img/brickwall_diffuse.png',
157+
import.meta.url
158+
).toString()
159+
);
160+
const imageBitmap = await createImageBitmap(await response.blob());
161+
brickwallDiffuseTexture = createTextureFromImage(device, imageBitmap);
162+
}
163+
164+
let brickwallNormalTexture: GPUTexture;
165+
{
166+
const response = await fetch(
167+
new URL(
168+
'../../../assets/img/brickwall_normal.png',
169+
import.meta.url
170+
).toString()
171+
);
172+
const imageBitmap = await createImageBitmap(await response.blob());
173+
brickwallNormalTexture = createTextureFromImage(device, imageBitmap);
174+
}
175+
176+
let brickwallHeightTexture: GPUTexture;
177+
{
178+
const response = await fetch(
179+
new URL(
180+
'../../../assets/img/brickwall_height.png',
181+
import.meta.url
182+
).toString()
183+
);
184+
const imageBitmap = await createImageBitmap(await response.blob());
185+
brickwallHeightTexture = createTextureFromImage(device, imageBitmap);
121186
}
122187

123188
// Create a sampler with linear filtering for smooth interpolation.
@@ -179,21 +244,21 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
179244
[
180245
[
181246
sampler,
182-
spiralPBR.diffuse.createView(),
183-
spiralPBR.normal.createView(),
184-
spiralPBR.height.createView(),
247+
woodDiffuseTexture.createView(),
248+
spiralNormalTexture.createView(),
249+
spiralHeightTexture.createView(),
185250
],
186251
[
187252
sampler,
188-
toyboxPBR.diffuse.createView(),
189-
toyboxPBR.normal.createView(),
190-
toyboxPBR.height.createView(),
253+
woodDiffuseTexture.createView(),
254+
toyboxNormalTexture.createView(),
255+
toyboxHeightTexture.createView(),
191256
],
192257
[
193258
sampler,
194-
brickWallPBR.diffuse.createView(),
195-
brickWallPBR.normal.createView(),
196-
brickWallPBR.height.createView(),
259+
brickwallDiffuseTexture.createView(),
260+
brickwallNormalTexture.createView(),
261+
brickwallHeightTexture.createView(),
197262
],
198263
],
199264
'Surface',
@@ -219,7 +284,6 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
219284
function getModelMatrix() {
220285
const modelMatrix = mat4.create();
221286
mat4.identity(modelMatrix);
222-
mat4.rotateX(modelMatrix, 10, modelMatrix);
223287
const now = Date.now() / 1000;
224288
mat4.rotateY(modelMatrix, now * -0.5, modelMatrix);
225289
return modelMatrix;
@@ -275,8 +339,8 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
275339
const depthFolder = gui.addFolder('Depth');
276340
lightFolder.add(settings, 'Reset Light').onChange(() => {
277341
lightPosXController.setValue(1.7);
278-
lightPosYController.setValue(-0.7);
279-
lightPosZController.setValue(1.9);
342+
lightPosYController.setValue(0.7);
343+
lightPosZController.setValue(-1.9);
280344
lightIntensityController.setValue(0.02);
281345
});
282346
const lightPosXController = lightFolder

src/sample/normalMap/utils.ts

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -205,63 +205,3 @@ export const createTextureFromImage = (
205205
);
206206
return texture;
207207
};
208-
209-
export interface PBRDescriptor {
210-
diffuse?: GPUTexture;
211-
normal?: GPUTexture;
212-
height?: GPUTexture;
213-
}
214-
215-
interface URLLoad {
216-
url: string;
217-
type: keyof PBRDescriptor;
218-
}
219-
220-
export const createPBRDescriptor = async (
221-
device: GPUDevice,
222-
urls: string[]
223-
): Promise<PBRDescriptor> => {
224-
const imgAssetPrepend = '/img/';
225-
const loads = urls.map((url) => {
226-
const splits = url.split('_');
227-
const ttype = splits[splits.length - 1].split('.')[0];
228-
const load: URLLoad = {
229-
url: imgAssetPrepend + url,
230-
type: ttype as keyof PBRDescriptor,
231-
};
232-
return load;
233-
});
234-
console.log(loads);
235-
const pbr: PBRDescriptor = {};
236-
for (let i = 0; i < loads.length; i++) {
237-
console.log(loads[i].url);
238-
console.log(import.meta.url);
239-
let texture: GPUTexture;
240-
{
241-
const response = await fetch(loads[i].url);
242-
const imageBitmap = await createImageBitmap(await response.blob());
243-
texture = createTextureFromImage(device, imageBitmap);
244-
}
245-
246-
console.log(loads[i].type);
247-
248-
switch (loads[i].type) {
249-
case 'diffuse':
250-
{
251-
pbr.diffuse = texture;
252-
}
253-
break;
254-
case 'height':
255-
{
256-
pbr.height = texture;
257-
}
258-
break;
259-
case 'normal':
260-
{
261-
pbr.normal = texture;
262-
}
263-
break;
264-
}
265-
}
266-
return pbr;
267-
};

0 commit comments

Comments
 (0)