Skip to content

Commit

Permalink
geo-layers UBO (#9036)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixpalmer authored Jul 23, 2024
1 parent 4e3ea96 commit f05f5d9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 28 deletions.
9 changes: 3 additions & 6 deletions modules/geo-layers/src/mesh-layer/mesh-layer-fragment.glsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ export default `#version 300 es
precision highp float;
uniform bool hasTexture;
uniform sampler2D sampler;
uniform bool flatShading;
uniform float opacity;
in vec2 vTexCoord;
in vec3 cameraPosition;
Expand All @@ -22,21 +19,21 @@ void main(void) {
fragColor = vColor * pbr_filterColor(vec4(0));
geometry.uv = pbr_vUV;
fragColor.a *= opacity;
fragColor.a *= layer.opacity;
#else
geometry.uv = vTexCoord;
vec3 normal;
if (flatShading) {
if (simpleMesh.flatShading) {
normal = normalize(cross(dFdx(position_commonspace.xyz), dFdy(position_commonspace.xyz)));
} else {
normal = normals_commonspace;
}
vec4 color = hasTexture ? texture(sampler, vTexCoord) : vColor;
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);
Expand Down
21 changes: 21 additions & 0 deletions modules/geo-layers/src/mesh-layer/mesh-layer-uniforms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {ShaderModule} from '@luma.gl/shadertools';
import {UniformTypes} from '@deck.gl/core';

const uniformBlock = `\
uniform meshUniforms {
bool pickFeatureIds;
} mesh;
`;

export type MeshProps = {
pickFeatureIds: boolean;
};

export const meshUniforms = {
name: 'mesh',
vs: uniformBlock,
fs: uniformBlock,
uniformTypes: {
pickFeatureIds: 'f32'
} as const satisfies UniformTypes<MeshProps>
} as const satisfies ShaderModule<MeshProps>;
9 changes: 2 additions & 7 deletions modules/geo-layers/src/mesh-layer/mesh-layer-vertex.glsl.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
export default `#version 300 es
#define SHADER_NAME simple-mesh-layer-vs
// Scale the model
uniform float sizeScale;
uniform bool composeModelMatrix;
uniform bool pickFeatureIds;
// Primitive attributes
in vec3 positions;
in vec3 normals;
Expand Down Expand Up @@ -41,7 +36,7 @@ void main(void) {
vec2 uv = applyUVRegion(texCoords);
geometry.uv = uv;
if (pickFeatureIds) {
if (mesh.pickFeatureIds) {
geometry.pickingColor = featureIdsPickingColors;
} else {
geometry.pickingColor = instancePickingColors;
Expand All @@ -53,7 +48,7 @@ void main(void) {
cameraPosition = project.cameraPosition;
vColor = vec4(colors * instanceColors.rgb, instanceColors.a);
vec3 pos = (instanceModelMatrix * positions) * sizeScale;
vec3 pos = (instanceModelMatrix * positions) * simpleMesh.sizeScale;
vec3 projectedPosition = project_position(positions);
position_commonspace = vec4(projectedPosition, 1.0);
gl_Position = project_common_position_to_clipspace(position_commonspace);
Expand Down
17 changes: 12 additions & 5 deletions modules/geo-layers/src/mesh-layer/mesh-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {MeshAttribute, MeshAttributes} from '@loaders.gl/schema';
import type {UpdateParameters, DefaultProps, LayerContext} from '@deck.gl/core';
import {SimpleMeshLayer, SimpleMeshLayerProps} from '@deck.gl/mesh-layers';

import {MeshProps, meshUniforms} from './mesh-layer-uniforms';
import vs from './mesh-layer-vertex.glsl';
import fs from './mesh-layer-fragment.glsl';

Expand Down Expand Up @@ -58,7 +59,7 @@ export default class MeshLayer<DataT = any, ExtraProps extends {} = {}> extends
getShaders() {
const shaders = super.getShaders();
const modules = shaders.modules;
modules.push(pbr);
modules.push(pbr, meshUniforms);
return {...shaders, vs, fs};
}

Expand Down Expand Up @@ -92,14 +93,20 @@ export default class MeshLayer<DataT = any, ExtraProps extends {} = {}> extends

draw(opts) {
const {featureIds} = this.props;
if (!this.state.model) {
const {model} = this.state;
if (!model) {
return;
}
this.state.model.setUniforms({
// Needed for PBR (TODO: find better way to get it)
u_Camera: this.state.model.uniforms.cameraPosition,
const meshProps: MeshProps = {
pickFeatureIds: Boolean(featureIds)
};
// 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)
u_Camera: model.uniforms.cameraPosition
});
model.shaderInputs.setProps({mesh: meshProps});

super.draw(opts);
}
Expand Down
27 changes: 27 additions & 0 deletions modules/geo-layers/src/trips-layer/trips-layer-uniforms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {ShaderModule} from '@luma.gl/shadertools';
import {UniformTypes} from '@deck.gl/core';

const uniformBlock = `\
uniform tripsUniforms {
bool fadeTrail;
float trailLength;
float currentTime;
} trips;
`;

export type TripsProps = {
fadeTrail: boolean;
trailLength: number;
currentTime: number;
};

export const tripsUniforms = {
name: 'trips',
vs: uniformBlock,
fs: uniformBlock,
uniformTypes: {
fadeTrail: 'f32',
trailLength: 'f32',
currentTime: 'f32'
} as const satisfies UniformTypes<Required<TripsProps>>
} as const satisfies ShaderModule<TripsProps>;
18 changes: 8 additions & 10 deletions modules/geo-layers/src/trips-layer/trips-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import type {NumericArray} from '@math.gl/core';
import {AccessorFunction, DefaultProps} from '@deck.gl/core';
import {PathLayer, PathLayerProps} from '@deck.gl/layers';

import {tripsUniforms, TripsProps} from './trips-layer-uniforms';

const defaultProps: DefaultProps<TripsLayerProps> = {
fadeTrail: true,
trailLength: {type: 'number', value: 120, min: 0},
Expand Down Expand Up @@ -67,7 +69,6 @@ export default class TripsLayer<DataT = any, ExtraProps extends {} = {}> extends
const shaders = super.getShaders();
shaders.inject = {
'vs:#decl': `\
uniform float trailLength;
in float instanceTimestamps;
in float instanceNextTimestamps;
out float vTime;
Expand All @@ -77,24 +78,22 @@ out float vTime;
vTime = instanceTimestamps + (instanceNextTimestamps - instanceTimestamps) * vPathPosition.y / vPathLength;
`,
'fs:#decl': `\
uniform bool fadeTrail;
uniform float trailLength;
uniform float currentTime;
in float vTime;
`,
// Drop the segments outside of the time window
'fs:#main-start': `\
if(vTime > currentTime || (fadeTrail && (vTime < currentTime - trailLength))) {
if(vTime > trips.currentTime || (trips.fadeTrail && (vTime < trips.currentTime - trips.trailLength))) {
discard;
}
`,
// Fade the color (currentTime - 100%, end of trail - 0%)
'fs:DECKGL_FILTER_COLOR': `\
if(fadeTrail) {
color.a *= 1.0 - (currentTime - vTime) / trailLength;
if(trips.fadeTrail) {
color.a *= 1.0 - (trips.currentTime - vTime) / trips.trailLength;
}
`
};
shaders.modules = [...shaders.modules, tripsUniforms];
return shaders;
}

Expand All @@ -120,11 +119,10 @@ if(fadeTrail) {

draw(params) {
const {fadeTrail, trailLength, currentTime} = this.props;
const uniforms = {fadeTrail, trailLength, currentTime};
const tripsProps: TripsProps = {fadeTrail, trailLength, currentTime};

// TODO port to UBO with rest of geo-layers
const model = this.state.model!;
model.setUniforms(uniforms);
model.shaderInputs.setProps({trips: tripsProps});
super.draw(params);
}
}

0 comments on commit f05f5d9

Please sign in to comment.