diff --git a/typescript/packages/subsurface-viewer/src/layers/map/privateMapLayer.ts b/typescript/packages/subsurface-viewer/src/layers/map/privateMapLayer.ts index f41e2fc59..28eec1bdb 100644 --- a/typescript/packages/subsurface-viewer/src/layers/map/privateMapLayer.ts +++ b/typescript/packages/subsurface-viewer/src/layers/map/privateMapLayer.ts @@ -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"; @@ -173,7 +173,7 @@ export default class privateMapLayer extends Layer { 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. }); diff --git a/typescript/packages/subsurface-viewer/src/layers/shader_modules/index.ts b/typescript/packages/subsurface-viewer/src/layers/shader_modules/index.ts index 63e202fd5..b95f61b94 100644 --- a/typescript/packages/subsurface-viewer/src/layers/shader_modules/index.ts +++ b/typescript/packages/subsurface-viewer/src/layers/shader_modules/index.ts @@ -1 +1,2 @@ export { default as decoder } from "./decoder"; +export { default as localPhongLighting } from "./localPhongLighting"; diff --git a/typescript/packages/subsurface-viewer/src/layers/shader_modules/lighting.fs.glsl.ts b/typescript/packages/subsurface-viewer/src/layers/shader_modules/lighting.fs.glsl.ts new file mode 100644 index 000000000..f5868c975 --- /dev/null +++ b/typescript/packages/subsurface-viewer/src/layers/shader_modules/lighting.fs.glsl.ts @@ -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; diff --git a/typescript/packages/subsurface-viewer/src/layers/shader_modules/localPhongLighting.ts b/typescript/packages/subsurface-viewer/src/layers/shader_modules/localPhongLighting.ts new file mode 100644 index 000000000..ebf2c47d8 --- /dev/null +++ b/typescript/packages/subsurface-viewer/src/layers/shader_modules/localPhongLighting.ts @@ -0,0 +1,8 @@ +import fs from "./lighting.fs.glsl"; +import { phongLighting } from "@luma.gl/shadertools"; + +export default { + name: "localPhongLighting", + fs, + dependencies: [phongLighting], +}; diff --git a/typescript/packages/subsurface-viewer/src/layers/triangle/fragment.fs.glsl.ts b/typescript/packages/subsurface-viewer/src/layers/triangle/fragment.fs.glsl.ts index 74d98271a..61bd3e945 100644 --- a/typescript/packages/subsurface-viewer/src/layers/triangle/fragment.fs.glsl.ts +++ b/typescript/packages/subsurface-viewer/src/layers/triangle/fragment.fs.glsl.ts @@ -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; @@ -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); diff --git a/typescript/packages/subsurface-viewer/src/layers/triangle/privateTriangleLayer.ts b/typescript/packages/subsurface-viewer/src/layers/triangle/privateTriangleLayer.ts index 5771a390e..aed9403a9 100644 --- a/typescript/packages/subsurface-viewer/src/layers/triangle/privateTriangleLayer.ts +++ b/typescript/packages/subsurface-viewer/src/layers/triangle/privateTriangleLayer.ts @@ -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; @@ -104,7 +105,7 @@ export default class PrivateTriangleLayer extends Layer