Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image loading in normalMap sample #310

Merged
merged 23 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/img/brickwall_diffuse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img/brickwall_height.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img/brickwall_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img/spiral_height.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img/spiral_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img/toybox_height.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img/toybox_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/img/wood_diffuse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = {
basePath: BASE_PATH,
compress: true,
reactStrictMode: true,
eslint: {
cmhhelgeson marked this conversation as resolved.
Show resolved Hide resolved
ignoreDuringBuilds: true,
},
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
config.module.rules.push({
test: /\.(png|jpe?g|gif|webm)$/i,
Expand Down
137 changes: 101 additions & 36 deletions src/sample/normalMap/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import normalMapWGSL from './normalMap.wgsl';
import { createMeshRenderable } from '../../meshes/mesh';
import { createBoxMeshWithTangents } from '../../meshes/box';
import {
PBRDescriptor,
createPBRDescriptor,
createBindGroupDescriptor,
create3DRenderPipeline,
createTextureFromImage,
} from './utils';

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

// Create PBR info (diffuse, normal, and depth/height textures)
let spiralPBR: Required<PBRDescriptor>;
// Fetch the image and upload it into a GPUTexture.
let woodDiffuseTexture: GPUTexture;
{
const response = await createPBRDescriptor(device, [
'wood_diffuse.png',
'spiral_normal.png',
'spiral_height.png',
]);
spiralPBR = response as Required<PBRDescriptor>;
const response = await fetch(
new URL(
'../../../assets/img/wood_diffuse.png',
import.meta.url
).toString()
);
const imageBitmap = await createImageBitmap(await response.blob());
woodDiffuseTexture = createTextureFromImage(device, imageBitmap);
}

let toyboxPBR: Required<PBRDescriptor>;
let spiralNormalTexture: GPUTexture;
{
const response = await createPBRDescriptor(device, [
'wood_diffuse.png',
'toybox_normal.png',
'toybox_height.png',
]);
toyboxPBR = response as Required<PBRDescriptor>;
const response = await fetch(
new URL(
'../../../assets/img/spiral_normal.png',
import.meta.url
).toString()
);
const imageBitmap = await createImageBitmap(await response.blob());
spiralNormalTexture = createTextureFromImage(device, imageBitmap);
}

let brickWallPBR: Required<PBRDescriptor>;
let spiralHeightTexture: GPUTexture;
{
const response = await createPBRDescriptor(device, [
'brickwall_diffuse.png',
'brickwall_normal.png',
'brickwall_height.png',
]);
brickWallPBR = response as Required<PBRDescriptor>;
const response = await fetch(
new URL(
'../../../assets/img/spiral_height.png',
import.meta.url
).toString()
);
const imageBitmap = await createImageBitmap(await response.blob());
spiralHeightTexture = createTextureFromImage(device, imageBitmap);
}

let toyboxNormalTexture: GPUTexture;
{
const response = await fetch(
new URL(
'../../../assets/img/toybox_normal.png',
import.meta.url
).toString()
);
const imageBitmap = await createImageBitmap(await response.blob());
toyboxNormalTexture = createTextureFromImage(device, imageBitmap);
}

let toyboxHeightTexture: GPUTexture;
{
const response = await fetch(
new URL(
'../../../assets/img/toybox_height.png',
import.meta.url
).toString()
);
const imageBitmap = await createImageBitmap(await response.blob());
toyboxHeightTexture = createTextureFromImage(device, imageBitmap);
}

let brickwallDiffuseTexture: GPUTexture;
{
const response = await fetch(
new URL(
'../../../assets/img/brickwall_diffuse.png',
import.meta.url
).toString()
);
const imageBitmap = await createImageBitmap(await response.blob());
brickwallDiffuseTexture = createTextureFromImage(device, imageBitmap);
}

let brickwallNormalTexture: GPUTexture;
{
const response = await fetch(
new URL(
'../../../assets/img/brickwall_normal.png',
import.meta.url
).toString()
);
const imageBitmap = await createImageBitmap(await response.blob());
brickwallNormalTexture = createTextureFromImage(device, imageBitmap);
}

let brickwallHeightTexture: GPUTexture;
{
const response = await fetch(
new URL(
'../../../assets/img/brickwall_height.png',
import.meta.url
).toString()
);
const imageBitmap = await createImageBitmap(await response.blob());
brickwallHeightTexture = createTextureFromImage(device, imageBitmap);
}

// Create a sampler with linear filtering for smooth interpolation.
Expand Down Expand Up @@ -179,21 +244,21 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
[
[
sampler,
spiralPBR.diffuse.createView(),
spiralPBR.normal.createView(),
spiralPBR.height.createView(),
woodDiffuseTexture.createView(),
spiralNormalTexture.createView(),
spiralHeightTexture.createView(),
],
[
sampler,
toyboxPBR.diffuse.createView(),
toyboxPBR.normal.createView(),
toyboxPBR.height.createView(),
woodDiffuseTexture.createView(),
toyboxNormalTexture.createView(),
toyboxHeightTexture.createView(),
],
[
sampler,
brickWallPBR.diffuse.createView(),
brickWallPBR.normal.createView(),
brickWallPBR.height.createView(),
brickwallDiffuseTexture.createView(),
brickwallNormalTexture.createView(),
brickwallHeightTexture.createView(),
],
],
'Surface',
Expand All @@ -219,7 +284,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
function getModelMatrix() {
const modelMatrix = mat4.create();
mat4.identity(modelMatrix);
mat4.rotateX(modelMatrix, 10, modelMatrix);
mat4.rotateX(modelMatrix, 0, modelMatrix);
cmhhelgeson marked this conversation as resolved.
Show resolved Hide resolved
const now = Date.now() / 1000;
mat4.rotateY(modelMatrix, now * -0.5, modelMatrix);
return modelMatrix;
Expand Down Expand Up @@ -275,8 +340,8 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
const depthFolder = gui.addFolder('Depth');
lightFolder.add(settings, 'Reset Light').onChange(() => {
lightPosXController.setValue(1.7);
lightPosYController.setValue(-0.7);
lightPosZController.setValue(1.9);
lightPosYController.setValue(0.7);
lightPosZController.setValue(-1.9);
lightIntensityController.setValue(0.02);
});
const lightPosXController = lightFolder
Expand Down
60 changes: 0 additions & 60 deletions src/sample/normalMap/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,63 +205,3 @@ export const createTextureFromImage = (
);
return texture;
};

export interface PBRDescriptor {
diffuse?: GPUTexture;
normal?: GPUTexture;
height?: GPUTexture;
}

interface URLLoad {
url: string;
type: keyof PBRDescriptor;
}

export const createPBRDescriptor = async (
device: GPUDevice,
urls: string[]
): Promise<PBRDescriptor> => {
const imgAssetPrepend = '/img/';
const loads = urls.map((url) => {
const splits = url.split('_');
const ttype = splits[splits.length - 1].split('.')[0];
const load: URLLoad = {
url: imgAssetPrepend + url,
type: ttype as keyof PBRDescriptor,
};
return load;
});
console.log(loads);
const pbr: PBRDescriptor = {};
for (let i = 0; i < loads.length; i++) {
console.log(loads[i].url);
console.log(import.meta.url);
let texture: GPUTexture;
{
const response = await fetch(loads[i].url);
const imageBitmap = await createImageBitmap(await response.blob());
texture = createTextureFromImage(device, imageBitmap);
}

console.log(loads[i].type);

switch (loads[i].type) {
case 'diffuse':
{
pbr.diffuse = texture;
}
break;
case 'height':
{
pbr.height = texture;
}
break;
case 'normal':
{
pbr.normal = texture;
}
break;
}
}
return pbr;
};
5 changes: 5 additions & 0 deletions src/sample/texturedCube/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ const init: SampleInit = async ({ canvas, pageState }) => {
// Fetch the image and upload it into a GPUTexture.
let cubeTexture: GPUTexture;
{
const url = new URL(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this changed? revert?

'../../../assets/img/Di-3d.png',
import.meta.url
).toString();
console.log(url);
const response = await fetch(
new URL('../../../assets/img/Di-3d.png', import.meta.url).toString()
);
Expand Down
Loading