Skip to content

Commit

Permalink
refactor: Fix of [NGRM] - move shader code to local shader module equ…
Browse files Browse the repository at this point in the history
  • Loading branch information
nilscb committed Aug 31, 2023
1 parent 77c171b commit 0100295
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
Layer,
picking,
project,
phongLighting,
} from "@deck.gl/core/typed";
import { localPhongLighting } from "../shader_modules";
import type { LayerPickInfo, PropertyDataType } from "../utils/layerTools";
import { createPropertyData } from "../utils/layerTools";
import { Model, Geometry } from "@luma.gl/engine";
Expand Down Expand Up @@ -173,7 +173,7 @@ export default class privateMapLayer extends Layer<privateMapLayerProps> {
vertexCount: this.props.mesh.vertexCount,
indices: this.props.mesh.indices,
}),
modules: [project, picking, phongLighting],
modules: [project, picking, localPhongLighting],
isInstanced: false, // This only works when set to false.
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as decoder } from "./decoder";
export { default as localPhongLighting } from "./localPhongLighting";
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fs = `
// Note: modification of luma.gl's functions to get two sided phong lighting.
// Ref original file modules/shadertools/src/modules/phong-lighting/phong-lighting.glsl.ts in luma source.
vec3 getPhongLightColor(vec3 surfaceColor, vec3 light_direction, vec3 view_direction, vec3 normal_worldspace, vec3 color) {
vec3 halfway_direction = normalize(light_direction + view_direction);
float lambertian = abs(dot(light_direction, normal_worldspace));
float specular_angle = abs(dot(normal_worldspace, halfway_direction));
float specular = pow(specular_angle, lighting_uShininess);
return (lambertian * lighting_uDiffuse * surfaceColor + specular * lighting_uSpecularColor) * color;
}
vec3 getPhongLightColor(vec3 surfaceColor,vec3 cameraPosition, vec3 position_worldspace, vec3 normal_worldspace) {
vec3 lightColor = surfaceColor;
if (lighting_uEnabled) {
vec3 view_direction = normalize(cameraPosition - position_worldspace);
lightColor = lighting_uAmbient * surfaceColor * lighting_uAmbientLight.color;
for (int i = 0; i < MAX_LIGHTS; i++) {
if (i >= lighting_uPointLightCount) {
break;
}
PointLight pointLight = lighting_uPointLight[i];
vec3 light_position_worldspace = pointLight.position;
vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
lightColor += getPhongLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
}
for (int i = 0; i < MAX_LIGHTS; i++) {
if (i >= lighting_uDirectionalLightCount) {
break;
}
DirectionalLight directionalLight = lighting_uDirectionalLight[i];
lightColor += getPhongLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
}
}
return lightColor;
}
`;

export default fs;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import fs from "./lighting.fs.glsl";
import { phongLighting } from "@luma.gl/shadertools";

export default {
name: "localPhongLighting",
fs,
dependencies: [phongLighting],
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,6 @@ uniform sampler2D colormap;
uniform vec4 uColor;
uniform bool smoothShading;
vec3 getLightColor(vec3 surfaceColor, vec3 light_direction, vec3 view_direction, vec3 normal_worldspace, vec3 color) {
vec3 halfway_direction = normalize(light_direction + view_direction);
float lambertian = abs(dot(light_direction, normal_worldspace));
float specular_angle = abs(dot(normal_worldspace, halfway_direction));
float specular = pow(specular_angle, lighting_uShininess);
return (lambertian * lighting_uDiffuse * surfaceColor + specular * lighting_uSpecularColor) * color;
}
vec3 getLightColor(vec3 surfaceColor,vec3 cameraPosition, vec3 position_worldspace, vec3 normal_worldspace) {
vec3 lightColor = surfaceColor;
if (lighting_uEnabled) {
vec3 view_direction = normalize(cameraPosition - position_worldspace);
lightColor = lighting_uAmbient * surfaceColor * lighting_uAmbientLight.color;
for (int i = 0; i < MAX_LIGHTS; i++) {
if (i >= lighting_uPointLightCount) {
break;
}
PointLight pointLight = lighting_uPointLight[i];
vec3 light_position_worldspace = pointLight.position;
vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
lightColor += getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
}
for (int i = 0; i < MAX_LIGHTS; i++) {
if (i >= lighting_uDirectionalLightCount) {
break;
}
DirectionalLight directionalLight = lighting_uDirectionalLight[i];
lightColor += getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
}
}
return lightColor;
}
void main(void) {
//geometry.uv = vTexCoord;
Expand Down Expand Up @@ -116,7 +75,7 @@ void main(void) {
}
// Use normal lighting. This has no effect if "material" property is not set.
vec3 lightColor = getLightColor(color.rgb, cameraPosition, position_commonspace.xyz, normal);
vec3 lightColor = getPhongLightColor(color.rgb, cameraPosition, position_commonspace.xyz, normal);
fragColor = vec4(lightColor, 1.0);
DECKGL_FILTER_COLOR(fragColor, geometry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import fsShader from "./fragment.fs.glsl";
import vsLineShader from "./vertex_lines.glsl";
import fsLineShader from "./fragment_lines.glsl";
import GL from "@luma.gl/constants";
import { localPhongLighting } from "../shader_modules";

export type GeometryTriangles = {
drawMode: number;
Expand Down Expand Up @@ -104,7 +105,7 @@ export default class PrivateTriangleLayer extends Layer<PrivateTriangleLayerProp
vs: vsShader,
fs: fsShader,
geometry: new Geometry(this.props.geometryTriangles),
modules: [project, picking, phongLighting],
modules: [project, picking, localPhongLighting],
isInstanced: false, // This only works when set to false.
});

Expand Down

0 comments on commit 0100295

Please sign in to comment.