Skip to content

Commit

Permalink
Revert "shadertools: PBR module using UBO (#2173)"
Browse files Browse the repository at this point in the history
This reverts commit 0cfb490.
  • Loading branch information
felixpalmer committed Sep 2, 2024
1 parent 739f683 commit 9a70709
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 215 deletions.
23 changes: 11 additions & 12 deletions examples/tutorials/hello-gltf/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,17 @@ export default class AppAnimationLoopTemplate extends AnimationLoopTemplate {
const eye = worldMatrix.transformAsPoint(vantage);
const center = worldMatrix.transformAsPoint(this.center);
const viewMatrix = new Matrix4().lookAt({eye, center});
const modelViewProjectionMatrix = new Matrix4(projectionMatrix)
const u_MVPMatrix = new Matrix4(projectionMatrix)
.multiplyRight(viewMatrix)
.multiplyRight(worldMatrix);

model.shaderInputs.setProps({
lighting: lightSources,
pbrProjection: {
camera: eye,
modelViewProjectionMatrix,
modelMatrix: worldMatrix,
normalMatrix: new Matrix4(worldMatrix).invert().transpose()
}
model.setUniforms({
u_Camera: eye,
u_MVPMatrix,
u_ModelMatrix: worldMatrix,
u_NormalMatrix: new Matrix4(worldMatrix).invert().transpose()
});

model.updateModuleSettings({lightSources});
model.draw(renderPass);
});
renderPass.end();
Expand All @@ -79,7 +77,7 @@ export default class AppAnimationLoopTemplate extends AnimationLoopTemplate {
canvas.style.opacity = '0.1';

const gltf = await load(
`https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/main/Models/${modelName}/glTF/${modelName}.gltf`,
`https://github.khronos.org/glTF-Sample-Viewer-Release/assets/models/Models/${modelName}/glTF/${modelName}.gltf`,
GLTFLoader
);
const processedGLTF = postProcessGLTF(gltf);
Expand Down Expand Up @@ -111,16 +109,17 @@ const lightSources: LightingProps = {
type: 'ambient'
},
directionalLights: [
// @ts-expect-error Remove once npm package updated with new types
{
color: [222, 244, 255],
direction: [1, -0.5, 0.5],
intensity: 10,
position: [0, 0, 0],
type: 'directional'
}
],
pointLights: [
{
attenuation: 0,
color: [255, 222, 222],
position: [3, 10, 0],
intensity: 5,
Expand Down
21 changes: 6 additions & 15 deletions modules/gltf/src/gltf/create-gltf-model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {Device, RenderPipelineParameters, log} from '@luma.gl/core';
import {pbrMaterial} from '@luma.gl/shadertools';
import {pbr} from '@luma.gl/shadertools';
import {Geometry, Model, ModelNode, ModelProps} from '@luma.gl/engine';
import {ParsePBRMaterialOptions, parsePBRMaterial} from '../pbr/parse-pbr-material';
import {ShaderModule} from '@luma.gl/shadertools';

// TODO rename attributes to POSITION/NORMAL etc
// See gpu-geometry.ts: getAttributeBuffersFromGeometry()
Expand Down Expand Up @@ -49,7 +48,7 @@ const vs = `
#endif
pbr_setPositionNormalTangentUV(positions, _NORMAL, _TANGENT, _TEXCOORD_0);
gl_Position = pbrProjection.modelViewProjectionMatrix * positions;
gl_Position = u_MVPMatrix * positions;
}
`;

Expand Down Expand Up @@ -101,26 +100,18 @@ export function createGLTFModel(device: Device, options: CreateGLTFModelOptions)
geometry,
topology: geometry.topology,
vertexCount,
modules: [pbrMaterial as unknown as ShaderModule],
modules: [pbr],
vs: addVersionToShader(device, vs),
fs: addVersionToShader(device, fs),
// TODO can this be removed? Does deck need it?
...modelOptions,

bindings: {...parsedMaterial.bindings, ...modelOptions.bindings},
defines: {...parsedMaterial.defines, ...modelOptions.defines},
parameters: {...parameters, ...parsedMaterial.parameters, ...modelOptions.parameters}
parameters: {...parameters, ...parsedMaterial.parameters, ...modelOptions.parameters},
uniforms: {...parsedMaterial.uniforms, ...modelOptions.uniforms}
};

const model = new Model(device, modelProps);

const {camera, ...pbrMaterialProps} = {
...parsedMaterial.uniforms,
...modelOptions.uniforms,
...parsedMaterial.bindings,
...modelOptions.bindings
};

model.shaderInputs.setProps({pbrMaterial: pbrMaterialProps, pbrProjection: {camera}});
return new ModelNode({managedResources, model});
}

Expand Down
94 changes: 43 additions & 51 deletions modules/gltf/src/pbr/parse-pbr-material.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {Device, Texture, Parameters} from '@luma.gl/core';
import type {Device, Texture, Binding, Parameters} from '@luma.gl/core';
import {log} from '@luma.gl/core';
import {PBREnvironment} from './pbr-environment';
import {PBRMaterialBindings, PBRMaterialUniforms, PBRProjectionProps} from '@luma.gl/shadertools';

/* eslint-disable camelcase */

Expand All @@ -18,8 +17,8 @@ export type ParsePBRMaterialOptions = {

export type ParsedPBRMaterial = {
readonly defines: Record<string, number | boolean>;
readonly bindings: Partial<PBRMaterialBindings>;
readonly uniforms: Partial<PBRProjectionProps & PBRMaterialUniforms>;
readonly bindings: Record<string, Binding>;
readonly uniforms: Record<string, any>;
readonly parameters: Parameters;
readonly glParameters: Record<string, any>;
/** List of all generated textures, makes it easy to destroy them later */
Expand Down Expand Up @@ -58,9 +57,9 @@ export function parsePBRMaterial(
bindings: {},
uniforms: {
// TODO: find better values?
camera: [0, 0, 0], // Model should override
u_Camera: [0, 0, 0], // Model should override

metallicRoughnessValues: [1, 1] // Default is 1 and 1
u_MetallicRoughnessValues: [1, 1] // Default is 1 and 1
},
parameters: {},
glParameters: {},
Expand All @@ -72,18 +71,17 @@ export function parsePBRMaterial(

const {imageBasedLightingEnvironment} = options;
if (imageBasedLightingEnvironment) {
parsedMaterial.bindings.pbr_diffuseEnvSampler = imageBasedLightingEnvironment.diffuseEnvSampler;
parsedMaterial.bindings.pbr_specularEnvSampler =
imageBasedLightingEnvironment.specularEnvSampler;
parsedMaterial.bindings.pbr_BrdfLUT = imageBasedLightingEnvironment.brdfLutTexture;
parsedMaterial.uniforms.scaleIBLAmbient = [1, 1];
parsedMaterial.bindings.u_DiffuseEnvSampler = imageBasedLightingEnvironment.diffuseEnvSampler;
parsedMaterial.bindings.u_SpecularEnvSampler = imageBasedLightingEnvironment.specularEnvSampler;
parsedMaterial.bindings.u_brdfLUT = imageBasedLightingEnvironment.brdfLutTexture;
parsedMaterial.uniforms.u_ScaleIBLAmbient = [1, 1];
}

if (options?.pbrDebug) {
parsedMaterial.defines.PBR_DEBUG = 1;
// Override final color for reference app visualization of various parameters in the lighting equation.
parsedMaterial.uniforms.scaleDiffBaseMR = [0, 0, 0, 0];
parsedMaterial.uniforms.scaleFGDSpec = [0, 0, 0, 0];
parsedMaterial.uniforms.u_ScaleDiffBaseMR = [0, 0, 0, 0];
parsedMaterial.uniforms.u_ScaleFGDSpec = [0, 0, 0, 0];
}

if (attributes.NORMAL) parsedMaterial.defines.HAS_NORMALS = 1;
Expand All @@ -102,51 +100,45 @@ export function parsePBRMaterial(

/** Parse GLTF material record */
function parseMaterial(device: Device, material, parsedMaterial: ParsedPBRMaterial): void {
parsedMaterial.uniforms.unlit = Boolean(material.unlit);
parsedMaterial.uniforms.pbr_uUnlit = Boolean(material.unlit);

if (material.pbrMetallicRoughness) {
parsePbrMetallicRoughness(device, material.pbrMetallicRoughness, parsedMaterial);
}
if (material.normalTexture) {
addTexture(
device,
material.normalTexture,
'pbr_normalSampler',
'HAS_NORMALMAP',
parsedMaterial
);
addTexture(device, material.normalTexture, 'u_NormalSampler', 'HAS_NORMALMAP', parsedMaterial);

const {scale = 1} = material.normalTexture;
parsedMaterial.uniforms.normalScale = scale;
parsedMaterial.uniforms.u_NormalScale = scale;
}
if (material.occlusionTexture) {
addTexture(
device,
material.occlusionTexture,
'pbr_occlusionSampler',
'u_OcclusionSampler',
'HAS_OCCLUSIONMAP',
parsedMaterial
);

const {strength = 1} = material.occlusionTexture;
parsedMaterial.uniforms.occlusionStrength = strength;
parsedMaterial.uniforms.u_OcclusionStrength = strength;
}
if (material.emissiveTexture) {
addTexture(
device,
material.emissiveTexture,
'pbr_emissiveSampler',
'u_EmissiveSampler',
'HAS_EMISSIVEMAP',
parsedMaterial
);
parsedMaterial.uniforms.emissiveFactor = material.emissiveFactor || [0, 0, 0];
parsedMaterial.uniforms.u_EmissiveFactor = material.emissiveFactor || [0, 0, 0];
}

switch (material.alphaMode) {
case 'MASK':
const {alphaCutoff = 0.5} = material;
parsedMaterial.defines.ALPHA_CUTOFF = 1;
parsedMaterial.uniforms.alphaCutoff = alphaCutoff;
parsedMaterial.uniforms.u_AlphaCutoff = alphaCutoff;
break;
case 'BLEND':
log.warn('glTF BLEND alphaMode might not work well because it requires mesh sorting')();
Expand Down Expand Up @@ -184,24 +176,24 @@ function parsePbrMetallicRoughness(
addTexture(
device,
pbrMetallicRoughness.baseColorTexture,
'pbr_baseColorSampler',
'u_BaseColorSampler',
'HAS_BASECOLORMAP',
parsedMaterial
);
}
parsedMaterial.uniforms.baseColorFactor = pbrMetallicRoughness.baseColorFactor || [1, 1, 1, 1];
parsedMaterial.uniforms.u_BaseColorFactor = pbrMetallicRoughness.baseColorFactor || [1, 1, 1, 1];

if (pbrMetallicRoughness.metallicRoughnessTexture) {
addTexture(
device,
pbrMetallicRoughness.metallicRoughnessTexture,
'pbr_metallicRoughnessSampler',
'u_MetallicRoughnessSampler',
'HAS_METALROUGHNESSMAP',
parsedMaterial
);
}
const {metallicFactor = 1, roughnessFactor = 1} = pbrMetallicRoughness;
parsedMaterial.uniforms.metallicRoughnessValues = [metallicFactor, roughnessFactor];
parsedMaterial.uniforms.u_MetallicRoughnessValues = [metallicFactor, roughnessFactor];
}

/** Create a texture from a glTF texture/sampler/image combo and add it to bindings */
Expand Down Expand Up @@ -276,9 +268,9 @@ export class PBRMaterialParser {
this.uniforms = {
// TODO: find better values?
camera: [0, 0, 0], // Model should override
u_Camera: [0, 0, 0], // Model should override
metallicRoughnessValues: [1, 1] // Default is 1 and 1
u_MetallicRoughnessValues: [1, 1] // Default is 1 and 1
};
this.bindings = {};
Expand All @@ -287,17 +279,17 @@ export class PBRMaterialParser {
this.generatedTextures = [];
if (imageBasedLightingEnvironment) {
this.bindings.pbr_diffuseEnvSampler = imageBasedLightingEnvironment.getDiffuseEnvSampler();
this.bindings.pbr_specularEnvSampler = imageBasedLightingEnvironment.getSpecularEnvSampler();
this.bindings.pbr_BrdfLUT = imageBasedLightingEnvironment.getBrdfTexture();
this.uniforms.scaleIBLAmbient = [1, 1];
this.bindings.u_DiffuseEnvSampler = imageBasedLightingEnvironment.getDiffuseEnvSampler();
this.bindings.u_SpecularEnvSampler = imageBasedLightingEnvironment.getSpecularEnvSampler();
this.bindings.u_brdfLUT = imageBasedLightingEnvironment.getBrdfTexture();
this.uniforms.u_ScaleIBLAmbient = [1, 1];
}
if (pbrDebug) {
// Override final color for reference app visualization
// of various parameters in the lighting equation.
this.uniforms.scaleDiffBaseMR = [0, 0, 0, 0];
this.uniforms.scaleFGDSpec = [0, 0, 0, 0];
this.uniforms.u_ScaleDiffBaseMR = [0, 0, 0, 0];
this.uniforms.u_ScaleFGDSpec = [0, 0, 0, 0];
}
this.defineIfPresent(attributes.NORMAL, 'HAS_NORMALS');
Expand Down Expand Up @@ -329,31 +321,31 @@ export class PBRMaterialParser {
/** Parse GLTF material record *
parseMaterial(material) {
this.uniforms.unlit = Boolean(material.unlit);
this.uniforms.pbr_uUnlit = Boolean(material.unlit);
if (material.pbrMetallicRoughness) {
this.parsePbrMetallicRoughness(material.pbrMetallicRoughness);
}
if (material.normalTexture) {
this.addTexture(material.normalTexture, 'pbr_normalSampler', 'HAS_NORMALMAP');
this.addTexture(material.normalTexture, 'u_NormalSampler', 'HAS_NORMALMAP');
const {scale = 1} = material.normalTexture;
this.uniforms.normalScale = scale;
this.uniforms.u_NormalScale = scale;
}
if (material.occlusionTexture) {
this.addTexture(material.occlusionTexture, 'pbr_occlusionSampler', 'HAS_OCCLUSIONMAP');
this.addTexture(material.occlusionTexture, 'u_OcclusionSampler', 'HAS_OCCLUSIONMAP');
const {strength = 1} = material.occlusionTexture;
this.uniforms.occlusionStrength = strength;
this.uniforms.u_OcclusionStrength = strength;
}
if (material.emissiveTexture) {
this.addTexture(material.emissiveTexture, 'pbr_emissiveSampler', 'HAS_EMISSIVEMAP');
this.uniforms.emissiveFactor = material.emissiveFactor || [0, 0, 0];
this.addTexture(material.emissiveTexture, 'u_EmissiveSampler', 'HAS_EMISSIVEMAP');
this.uniforms.u_EmissiveFactor = material.emissiveFactor || [0, 0, 0];
}
if (material.alphaMode === 'MASK') {
const {alphaCutoff = 0.5} = material;
this.defines.ALPHA_CUTOFF = 1;
this.uniforms.alphaCutoff = alphaCutoff;
this.uniforms.u_AlphaCutoff = alphaCutoff;
} else if (material.alphaMode === 'BLEND') {
log.warn('BLEND alphaMode might not work well because it requires mesh sorting')();
Object.assign(this.parameters, {
Expand All @@ -369,21 +361,21 @@ export class PBRMaterialParser {
if (pbrMetallicRoughness.baseColorTexture) {
this.addTexture(
pbrMetallicRoughness.baseColorTexture,
'pbr_baseColorSampler',
'u_BaseColorSampler',
'HAS_BASECOLORMAP'
);
}
this.uniforms.baseColorFactor = pbrMetallicRoughness.baseColorFactor || [1, 1, 1, 1];
this.uniforms.u_BaseColorFactor = pbrMetallicRoughness.baseColorFactor || [1, 1, 1, 1];
if (pbrMetallicRoughness.metallicRoughnessTexture) {
this.addTexture(
pbrMetallicRoughness.metallicRoughnessTexture,
'pbr_metallicRoughnessSampler',
'u_MetallicRoughnessSampler',
'HAS_METALROUGHNESSMAP'
);
}
const {metallicFactor = 1, roughnessFactor = 1} = pbrMetallicRoughness;
this.uniforms.metallicRoughnessValues = [metallicFactor, roughnessFactor];
this.uniforms.u_MetallicRoughnessValues = [metallicFactor, roughnessFactor];
}
/** Create a texture from a glTF texture/sampler/image combo and add it to bindings *
Expand Down
8 changes: 1 addition & 7 deletions modules/shadertools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,7 @@ export type {GouraudMaterialProps} from './modules/lighting/gouraud-material/gou
export {gouraudMaterial} from './modules/lighting/gouraud-material/gouraud-material';
export type {PhongMaterialProps} from './modules/lighting/phong-material/phong-material';
export {phongMaterial} from './modules/lighting/phong-material/phong-material';
export type {
PBRMaterialBindings,
PBRMaterialProps,
PBRMaterialUniforms
} from './modules/lighting/pbr-material/pbr-material';
export type {PBRProjectionProps} from './modules/lighting/pbr-material/pbr-projection';

// export type {PBRMaterialSettings, PBRMaterialUniforms} from './modules/lighting/pbr-material/pbr';
export {pbrMaterial} from './modules/lighting/pbr-material/pbr-material';

// POST PROCESSING / SHADER PASS MODULES
Expand Down
Loading

0 comments on commit 9a70709

Please sign in to comment.