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

Update PBR code to use luma pbrMaterial #9093

Merged
merged 10 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
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
2 changes: 2 additions & 0 deletions examples/website/i3s/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
},
"dependencies": {
"@loaders.gl/i3s": "^4.2.0",
"maplibre-gl": "^3.0.0",
"react-map-gl": "^7.1.0",
felixpalmer marked this conversation as resolved.
Show resolved Hide resolved
"deck.gl": "^9.0.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions modules/aggregation-layers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"prepublishOnly": "npm run build-bundle && npm run build-bundle -- --env=dev"
},
"dependencies": {
"@luma.gl/constants": "^9.0.23",
"@luma.gl/shadertools": "^9.0.23",
"@luma.gl/constants": "^9.0.24",
"@luma.gl/shadertools": "^9.0.24",
"@math.gl/web-mercator": "^4.0.0",
"d3-hexbin": "^0.2.1"
},
Expand Down
2 changes: 1 addition & 1 deletion modules/arcgis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"prepublishOnly": "npm run build-bundle && npm run build-bundle -- --env=dev"
},
"dependencies": {
"@luma.gl/constants": "^9.0.23",
"@luma.gl/constants": "^9.0.24",
"esri-loader": "^3.7.0"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions modules/carto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"@loaders.gl/mvt": "^4.2.0",
"@loaders.gl/schema": "^4.2.0",
"@loaders.gl/tiles": "^4.2.0",
"@luma.gl/core": "^9.0.23",
"@luma.gl/shadertools": "^9.0.23",
"@luma.gl/core": "^9.0.24",
"@luma.gl/shadertools": "^9.0.24",
"@math.gl/web-mercator": "^4.0.0",
"@types/d3-array": "^3.0.2",
"@types/d3-color": "^1.4.2",
Expand Down
10 changes: 5 additions & 5 deletions modules/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
"dependencies": {
"@loaders.gl/core": "^4.2.0",
"@loaders.gl/images": "^4.2.0",
"@luma.gl/constants": "^9.0.23",
"@luma.gl/core": "^9.0.23",
"@luma.gl/engine": "^9.0.23",
"@luma.gl/shadertools": "^9.0.23",
"@luma.gl/webgl": "^9.0.23",
"@luma.gl/constants": "^9.0.24",
"@luma.gl/core": "^9.0.24",
"@luma.gl/engine": "^9.0.24",
"@luma.gl/shadertools": "^9.0.24",
"@luma.gl/webgl": "^9.0.24",
"@math.gl/core": "^4.0.0",
"@math.gl/sun": "^4.0.0",
"@math.gl/web-mercator": "^4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions modules/extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"prepublishOnly": "npm run build-bundle && npm run build-bundle -- --env=dev"
},
"dependencies": {
"@luma.gl/constants": "^9.0.23",
"@luma.gl/shadertools": "^9.0.23",
"@luma.gl/constants": "^9.0.24",
"@luma.gl/shadertools": "^9.0.24",
"@math.gl/core": "^4.0.0"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions modules/geo-layers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
"@loaders.gl/terrain": "^4.2.0",
"@loaders.gl/tiles": "^4.2.0",
"@loaders.gl/wms": "^4.2.0",
"@luma.gl/gltf": "^9.0.23",
"@luma.gl/shadertools": "^9.0.23",
"@luma.gl/gltf": "^9.0.24",
"@luma.gl/shadertools": "^9.0.24",
"@math.gl/core": "^4.0.0",
"@math.gl/culling": "^4.0.0",
"@math.gl/web-mercator": "^4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions modules/geo-layers/src/mesh-layer/mesh-layer-fragment.glsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ out vec4 fragColor;

void main(void) {

#ifdef MODULE_PBR
#ifdef MODULE_PBRMATERIAL

fragColor = vColor * pbr_filterColor(vec4(0));
geometry.uv = pbr_vUV;
Expand All @@ -35,7 +35,7 @@ void main(void) {

vec4 color = simpleMesh.hasTexture ? texture(sampler, vTexCoord) : vColor;
vec3 lightColor = lighting_getLightColor(color.rgb, cameraPosition, position_commonspace.xyz, normal);
fragColor = vec4(lightColor, color.a * opacity);
fragColor = vec4(lightColor, color.a * layer.opacity);

#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void main(void) {

DECKGL_FILTER_GL_POSITION(gl_Position, geometry);

#ifdef MODULE_PBR
#ifdef MODULE_PBRMATERIAL
// set PBR data
pbr_vPosition = geometry.position.xyz;
#ifdef HAS_NORMALS
Expand Down
44 changes: 28 additions & 16 deletions modules/geo-layers/src/mesh-layer/mesh-layer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {NumericArray} from '@math.gl/core';
import {parsePBRMaterial, ParsedPBRMaterial} from '@luma.gl/gltf';
import {pbr} from '@luma.gl/shadertools';
import {pbrMaterial} from '@luma.gl/shadertools';
import {Model} from '@luma.gl/engine';
import type {MeshAttribute, MeshAttributes} from '@loaders.gl/schema';
import type {UpdateParameters, DefaultProps, LayerContext} from '@deck.gl/core';
Expand Down Expand Up @@ -59,7 +59,7 @@ export default class MeshLayer<DataT = any, ExtraProps extends {} = {}> extends
getShaders() {
const shaders = super.getShaders();
const modules = shaders.modules;
modules.push(pbr, meshUniforms);
modules.push(pbrMaterial, meshUniforms);
return {...shaders, vs, fs};
}

Expand Down Expand Up @@ -100,20 +100,22 @@ export default class MeshLayer<DataT = any, ExtraProps extends {} = {}> extends
const meshProps: MeshProps = {
pickFeatureIds: Boolean(featureIds)
};
// TODO replace with shaderInputs.setProps({pbr: u_Camera}) once
// luma pbr module ported to UBO
model.setUniforms({
const pbrProjectionProps = {
// Needed for PBR (TODO: find better way to get it)
u_Camera: model.uniforms.cameraPosition
// eslint-disable-next-line camelcase
felixpalmer marked this conversation as resolved.
Show resolved Hide resolved
camera: model.uniforms.cameraPosition as [number, number, number]
};
model.shaderInputs.setProps({
pbrProjection: pbrProjectionProps,
mesh: meshProps
});
model.shaderInputs.setProps({mesh: meshProps});

super.draw(opts);
}

protected getModel(mesh: Mesh): Model {
const {id, pbrMaterial} = this.props;
const parsedPBRMaterial = this.parseMaterial(pbrMaterial, mesh);
const {id} = this.props;
const parsedPBRMaterial = this.parseMaterial(this.props.pbrMaterial, mesh);
// Keep material to explicitly remove textures
this.setState({parsedPBRMaterial});
const shaders = this.getShaders();
Expand All @@ -135,26 +137,36 @@ export default class MeshLayer<DataT = any, ExtraProps extends {} = {}> extends
return model;
}

updatePbrMaterialUniforms(pbrMaterial) {
updatePbrMaterialUniforms(material) {
const {model} = this.state;
if (model) {
const {mesh} = this.props;
const parsedPBRMaterial = this.parseMaterial(pbrMaterial, mesh as Mesh);
const parsedPBRMaterial = this.parseMaterial(material, mesh as Mesh);
// Keep material to explicitly remove textures
this.setState({parsedPBRMaterial});
model.setBindings(parsedPBRMaterial.bindings);
model.setUniforms(parsedPBRMaterial.uniforms);

const {pbr_baseColorSampler} = parsedPBRMaterial.bindings;
const {emptyTexture} = this.state;
const simpleMeshProps = {
sampler: pbr_baseColorSampler || emptyTexture,
hasTexture: Boolean(pbr_baseColorSampler)
};
const {camera, ...pbrMaterialProps} = {
...parsedPBRMaterial.bindings,
...parsedPBRMaterial.uniforms
};
model.shaderInputs.setProps({simpleMesh: simpleMeshProps, pbrMaterial: pbrMaterialProps});
}
}

parseMaterial(pbrMaterial, mesh: Mesh): ParsedPBRMaterial {
parseMaterial(material, mesh: Mesh): ParsedPBRMaterial {
felixpalmer marked this conversation as resolved.
Show resolved Hide resolved
const unlit = Boolean(
pbrMaterial.pbrMetallicRoughness && pbrMaterial.pbrMetallicRoughness.baseColorTexture
material.pbrMetallicRoughness && material.pbrMetallicRoughness.baseColorTexture
felixpalmer marked this conversation as resolved.
Show resolved Hide resolved
);

return parsePBRMaterial(
this.context.device,
{unlit, ...pbrMaterial},
{unlit, ...material},
{NORMAL: mesh.attributes.normals, TEXCOORD_0: mesh.attributes.texCoords},
{
pbrDebug: false,
Expand Down
2 changes: 1 addition & 1 deletion modules/google-maps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"prepublishOnly": "npm run build-bundle && npm run build-bundle -- --env=dev"
},
"dependencies": {
"@luma.gl/constants": "^9.0.23",
"@luma.gl/constants": "^9.0.24",
"@math.gl/core": "^4.0.0",
"@types/google.maps": "^3.48.6"
},
Expand Down
2 changes: 1 addition & 1 deletion modules/jupyter-widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"@loaders.gl/3d-tiles": "^4.2.0",
"@loaders.gl/core": "^4.2.0",
"@loaders.gl/csv": "^4.2.0",
"@luma.gl/core": "^9.0.23",
"@luma.gl/core": "^9.0.24",
"d3-dsv": "^1.0.8",
"mapbox-gl": "^1.13.2"
},
Expand Down
4 changes: 2 additions & 2 deletions modules/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
"@deck.gl/react": "9.0.0-beta.11",
"@deck.gl/widgets": "9.0.0-beta.11",
"@loaders.gl/core": "^4.2.0",
"@luma.gl/core": "^9.0.23",
"@luma.gl/engine": "^9.0.23"
"@luma.gl/core": "^9.0.24",
"@luma.gl/engine": "^9.0.24"
},
"peerDependencies": {
"@arcgis/core": "^4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion modules/mapbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"prepublishOnly": "npm run build-bundle && npm run build-bundle -- --env=dev"
},
"dependencies": {
"@luma.gl/constants": "^9.0.23",
"@luma.gl/constants": "^9.0.24",
"@math.gl/web-mercator": "^4.0.0"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions modules/mesh-layers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
},
"dependencies": {
"@loaders.gl/gltf": "^4.2.0",
"@luma.gl/gltf": "^9.0.23",
"@luma.gl/shadertools": "^9.0.23"
"@luma.gl/gltf": "^9.0.24",
"@luma.gl/shadertools": "^9.0.24"
},
"peerDependencies": {
"@deck.gl/core": "^9.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ in vec4 vColor;

out vec4 fragColor;

// MODULE_PBR contains all the varying definitions needed
#ifndef MODULE_PBR
// pbrMaterial contains all the varying definitions needed
#ifndef LIGHTING_PBR
#if defined(HAS_UV) && defined(HAS_BASECOLORMAP)
in vec2 vTEXCOORD_0;
uniform sampler2D u_BaseColorSampler;
uniform sampler2D pbr_baseColorSampler;
#endif
#endif

void main(void) {
#ifdef MODULE_PBR
#ifdef LIGHTING_PBR
fragColor = vColor * pbr_filterColor(vec4(0));
geometry.uv = pbr_vUV;
#else
#if defined(HAS_UV) && defined(HAS_BASECOLORMAP)
fragColor = vColor * texture(u_BaseColorSampler, vTEXCOORD_0);
fragColor = vColor * texture(pbr_baseColorSampler, vTEXCOORD_0);
geometry.uv = vTEXCOORD_0;
#else
fragColor = vColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ in vec3 positions;
#ifdef HAS_UV
in vec2 texCoords;
#endif
#ifdef MODULE_PBR
#ifdef LIGHTING_PBR
#ifdef HAS_NORMALS
in vec3 normals;
#endif
Expand All @@ -27,16 +27,16 @@ in vec3 positions;
// Varying
out vec4 vColor;

// MODULE_PBR contains all the varying definitions needed
#ifndef MODULE_PBR
// pbrMaterial contains all the varying definitions needed
#ifndef LIGHTING_PBR
#ifdef HAS_UV
out vec2 vTEXCOORD_0;
#endif
#endif

// Main
void main(void) {
#if defined(HAS_UV) && !defined(MODULE_PBR)
#if defined(HAS_UV) && !defined(LIGHTING_PBR)
vTEXCOORD_0 = texCoords;
geometry.uv = texCoords;
#endif
Expand All @@ -47,7 +47,7 @@ void main(void) {
mat3 instanceModelMatrix = mat3(instanceModelMatrixCol0, instanceModelMatrixCol1, instanceModelMatrixCol2);

vec3 normal = vec3(0.0, 0.0, 1.0);
#ifdef MODULE_PBR
#ifdef LIGHTING_PBR
#ifdef HAS_NORMALS
normal = instanceModelMatrix * (scenegraph.sceneModelMatrix * vec4(normals, 0.0)).xyz;
#endif
Expand All @@ -74,7 +74,7 @@ void main(void) {
}
DECKGL_FILTER_GL_POSITION(gl_Position, geometry);

#ifdef MODULE_PBR
#ifdef LIGHTING_PBR
// set PBR data
pbr_vPosition = geometry.position.xyz;
#ifdef HAS_NORMALS
Expand Down
33 changes: 21 additions & 12 deletions modules/mesh-layers/src/scenegraph-layer/scenegraph-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import {Layer, project32, picking, log} from '@deck.gl/core';
import type {Device} from '@luma.gl/core';
import {pbr} from '@luma.gl/shadertools';
import {pbrMaterial} from '@luma.gl/shadertools';
import {ScenegraphNode, GroupNode, ModelNode, Model} from '@luma.gl/engine';
import {GLTFAnimator, PBREnvironment, createScenegraphsFromGLTF} from '@luma.gl/gltf';
import {GLTFLoader, postProcessGLTF} from '@loaders.gl/gltf';
Expand Down Expand Up @@ -188,13 +188,20 @@ export default class ScenegraphLayer<DataT = any, ExtraPropsT extends {} = {}> e
};

getShaders() {
const modules = [project32, picking, scenegraphUniforms];
const defines: {LIGHTING_PBR?: 1} = {};
let pbr;

if (this.props._lighting === 'pbr') {
modules.push(pbr);
pbr = pbrMaterial;
defines.LIGHTING_PBR = 1;
felixpalmer marked this conversation as resolved.
Show resolved Hide resolved
} else {
// Dummy shader module needed to handle
// pbrMaterial.pbr_baseColorSampler binding
pbr = {name: 'pbrMaterial'};
}

return super.getShaders({vs, fs, modules});
const modules = [project32, picking, scenegraphUniforms, pbr];
return super.getShaders({defines, vs, fs, modules});
}

initializeState() {
Expand Down Expand Up @@ -368,22 +375,24 @@ export default class ScenegraphLayer<DataT = any, ExtraPropsT extends {} = {}> e
if (node instanceof ModelNode) {
const {model} = node;
model.setInstanceCount(numInstances);

const pbrProjectionProps = {
// Needed for PBR (TODO: find better way to get it)
// eslint-disable-next-line camelcase
felixpalmer marked this conversation as resolved.
Show resolved Hide resolved
camera: model.uniforms.cameraPosition as [number, number, number]
};
const scenegraphProps: ScenegraphProps = {
sizeScale,
sizeMinPixels,
sizeMaxPixels,
composeModelMatrix: shouldComposeModelMatrix(viewport, coordinateSystem),
sceneModelMatrix: worldMatrix
};
// TODO replace with shaderInputs.setProps({pbr: u_Camera}) once
// luma pbr module ported to UBO
model.setUniforms({
// Needed for PBR (TODO: find better way to get it)
// eslint-disable-next-line camelcase
u_Camera: model.uniforms.cameraPosition
});

model.shaderInputs.setProps({scenegraph: scenegraphProps});
model.shaderInputs.setProps({
pbrProjection: pbrProjectionProps,
scenegraph: scenegraphProps
felixpalmer marked this conversation as resolved.
Show resolved Hide resolved
});
model.draw(renderPass);
}
});
Expand Down
2 changes: 1 addition & 1 deletion modules/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"src"
],
"dependencies": {
"@luma.gl/test-utils": "^9.0.23"
"@luma.gl/test-utils": "^9.0.24"
},
"peerDependencies": {
"@deck.gl/core": "^9.0.0",
Expand Down
Loading
Loading