forked from visgl/deck.gl
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(geo-layers): Private MeshLayer with pbr material support (I3S) (v…
…isgl#5761) Co-authored-by: Victor Belomestnov <belom88@yandex.ru>
- Loading branch information
Showing
6 changed files
with
226 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
modules/geo-layers/src/mesh-layer/mesh-layer-fragment.glsl.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
export default `#version 300 es | ||
#define SHADER_NAME simple-mesh-layer-fs | ||
precision highp float; | ||
uniform bool hasTexture; | ||
uniform sampler2D sampler; | ||
uniform bool flatShading; | ||
uniform float opacity; | ||
in vec2 vTexCoord; | ||
in vec3 cameraPosition; | ||
in vec3 normals_commonspace; | ||
in vec4 position_commonspace; | ||
in vec4 vColor; | ||
out vec4 fragColor; | ||
void main(void) { | ||
#ifdef MODULE_PBR | ||
fragColor = vColor * pbr_filterColor(vec4(0)); | ||
geometry.uv = pbr_vUV; | ||
fragColor.a *= opacity; | ||
#else | ||
geometry.uv = vTexCoord; | ||
vec3 normal; | ||
if (flatShading) { | ||
// NOTE(Tarek): This is necessary because | ||
// headless.gl reports the extension as | ||
// available but does not support it in | ||
// the shader. | ||
#ifdef DERIVATIVES_AVAILABLE | ||
normal = normalize(cross(dFdx(position_commonspace.xyz), dFdy(position_commonspace.xyz))); | ||
#else | ||
normal = vec3(0.0, 0.0, 1.0); | ||
#endif | ||
} else { | ||
normal = normals_commonspace; | ||
} | ||
vec4 color = hasTexture ? texture(sampler, vTexCoord) : vColor; | ||
vec3 lightColor = lighting_getLightColor(color.rgb, cameraPosition, position_commonspace.xyz, normal); | ||
fragColor = vec4(lightColor, color.a * opacity); | ||
#endif | ||
DECKGL_FILTER_COLOR(fragColor, geometry); | ||
} | ||
`; |
67 changes: 67 additions & 0 deletions
67
modules/geo-layers/src/mesh-layer/mesh-layer-vertex.glsl.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
export default `#version 300 es | ||
#define SHADER_NAME simple-mesh-layer-vs | ||
// Scale the model | ||
uniform float sizeScale; | ||
uniform bool composeModelMatrix; | ||
// Primitive attributes | ||
in vec3 positions; | ||
in vec3 normals; | ||
in vec3 colors; | ||
in vec2 texCoords; | ||
// Instance attributes | ||
in vec4 instanceColors; | ||
in vec3 instancePickingColors; | ||
in mat3 instanceModelMatrix; | ||
// Outputs to fragment shader | ||
out vec2 vTexCoord; | ||
out vec3 cameraPosition; | ||
out vec3 normals_commonspace; | ||
out vec4 position_commonspace; | ||
out vec4 vColor; | ||
void main(void) { | ||
geometry.uv = texCoords; | ||
geometry.pickingColor = instancePickingColors; | ||
#ifdef MODULE_PBR | ||
// set PBR data | ||
#ifdef HAS_NORMALS | ||
pbr_vNormal = project_normal(instanceModelMatrix * normals); | ||
geometry.normal = pbr_vNormal; | ||
#endif | ||
#ifdef HAS_UV | ||
pbr_vUV = texCoords; | ||
#else | ||
pbr_vUV = vec2(0., 0.); | ||
#endif | ||
geometry.uv = pbr_vUV; | ||
#endif | ||
vTexCoord = texCoords; | ||
cameraPosition = project_uCameraPosition; | ||
normals_commonspace = project_normal(instanceModelMatrix * normals); | ||
vColor = vec4(colors * instanceColors.rgb, instanceColors.a); | ||
geometry.normal = normals_commonspace; | ||
vec3 pos = (instanceModelMatrix * positions) * sizeScale; | ||
vec3 projectedPosition = project_position(positions); | ||
position_commonspace = vec4(projectedPosition, 1.0); | ||
gl_Position = project_common_position_to_clipspace(position_commonspace); | ||
geometry.position = position_commonspace; | ||
#ifdef MODULE_PBR | ||
// set PBR data | ||
pbr_vPosition = geometry.position.xyz; | ||
#endif | ||
DECKGL_FILTER_GL_POSITION(gl_Position, geometry); | ||
DECKGL_FILTER_COLOR(vColor, geometry); | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import {GLTFMaterialParser} from '@luma.gl/experimental'; | ||
import {Model, pbr} from '@luma.gl/core'; | ||
import {SimpleMeshLayer} from '@deck.gl/mesh-layers'; | ||
|
||
import vs from './mesh-layer-vertex.glsl'; | ||
import fs from './mesh-layer-fragment.glsl'; | ||
|
||
function validateGeometryAttributes(attributes) { | ||
const hasColorAttribute = attributes.COLOR_0 || attributes.colors; | ||
if (!hasColorAttribute) { | ||
attributes.colors = {constant: true, value: new Float32Array([1, 1, 1])}; | ||
} | ||
} | ||
|
||
const defaultProps = { | ||
// PBR material object. _lighting must be pbr for this to work | ||
pbrMaterial: {type: 'object', value: null} | ||
}; | ||
|
||
export default class _MeshLayer extends SimpleMeshLayer { | ||
getShaders() { | ||
const shaders = super.getShaders(); | ||
const modules = shaders.modules; | ||
modules.push(pbr); | ||
return {...shaders, vs, fs}; | ||
} | ||
|
||
updateState({props, oldProps, changeFlags}) { | ||
super.updateState({props, oldProps, changeFlags}); | ||
if (props.pbrMaterial !== oldProps.pbrMaterial) { | ||
this.updatePbrMaterialUniforms(props.pbrMaterial); | ||
} | ||
} | ||
|
||
draw(opts) { | ||
if (!this.state.model) { | ||
return; | ||
} | ||
this.state.model.setUniforms({ | ||
// Needed for PBR (TODO: find better way to get it) | ||
u_Camera: this.state.model.getUniforms().project_uCameraPosition | ||
}); | ||
super.draw(opts); | ||
} | ||
|
||
getModel(mesh) { | ||
const pbrMaterial = this.props.pbrMaterial; | ||
const materialParser = this.parseMaterial(pbrMaterial, mesh); | ||
const shaders = this.getShaders(); | ||
validateGeometryAttributes(mesh.attributes); | ||
const model = new Model(this.context.gl, { | ||
...this.getShaders(), | ||
id: this.props.id, | ||
geometry: mesh, | ||
defines: {...shaders.defines, ...materialParser?.defines}, | ||
parameters: materialParser?.parameters, | ||
isInstanced: true | ||
}); | ||
|
||
return model; | ||
} | ||
|
||
updatePbrMaterialUniforms(pbrMaterial) { | ||
const {model} = this.state; | ||
if (model) { | ||
const {mesh} = this.props; | ||
const materialParser = this.parseMaterial(pbrMaterial, mesh); | ||
model.setUniforms(materialParser.uniforms); | ||
} | ||
} | ||
|
||
parseMaterial(pbrMaterial, mesh) { | ||
const unlit = Boolean( | ||
pbrMaterial.pbrMetallicRoughness && pbrMaterial.pbrMetallicRoughness.baseColorTexture | ||
); | ||
const materialParser = new GLTFMaterialParser(this.context.gl, { | ||
attributes: {NORMAL: mesh.attributes.normals, TEXCOORD_0: mesh.attributes.texCoords}, | ||
material: {unlit, ...pbrMaterial}, | ||
pbrDebug: false, | ||
imageBasedLightingEnvironment: null, | ||
lights: true, | ||
useTangents: false | ||
}); | ||
return materialParser; | ||
} | ||
} | ||
|
||
_MeshLayer.layerName = '_MeshLayer'; | ||
_MeshLayer.defaultProps = defaultProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters