@luma.gl/shadertools
Version:
Shader module system for luma.gl
151 lines • 13.4 kB
TypeScript
import type { NumberArray2, NumberArray3 } from '@math.gl/core';
/** Supported light source descriptions accepted by the lighting shader module. */
export type Light = AmbientLight | PointLight | SpotLight | DirectionalLight;
/** Ambient light contribution shared across the entire scene. */
export type AmbientLight = {
/** Discriminator used to identify ambient lights in `lights: Light[]`. */
type: 'ambient';
/** RGB light color in the existing `0..255` convention used by luma.gl materials. */
color?: Readonly<NumberArray3>;
/** Scalar intensity multiplier applied to the light color. */
intensity?: number;
};
/** Omnidirectional point light emitted from a world-space position. */
export type PointLight = {
/** Discriminator used to identify point lights in `lights: Light[]`. */
type: 'point';
/** World-space light position. */
position: Readonly<NumberArray3>;
/** RGB light color in the existing `0..255` convention used by luma.gl materials. */
color?: Readonly<NumberArray3>;
/** Scalar intensity multiplier applied to the light color. */
intensity?: number;
/** Constant, linear, and quadratic attenuation coefficients. */
attenuation?: Readonly<NumberArray3>;
};
/** Directional light defined only by its incoming world-space direction. */
export type DirectionalLight = {
/** Discriminator used to identify directional lights in `lights: Light[]`. */
type: 'directional';
/** World-space light direction. */
direction: Readonly<NumberArray3>;
/** RGB light color in the existing `0..255` convention used by luma.gl materials. */
color?: Readonly<NumberArray3>;
/** Scalar intensity multiplier applied to the light color. */
intensity?: number;
};
/** Cone-shaped light emitted from a position and focused along a direction. */
export type SpotLight = {
/** Discriminator used to identify spot lights in `lights: Light[]`. */
type: 'spot';
/** World-space light position. */
position: Readonly<NumberArray3>;
/** World-space light direction. */
direction: Readonly<NumberArray3>;
/** RGB light color in the existing `0..255` convention used by luma.gl materials. */
color?: Readonly<NumberArray3>;
/** Scalar intensity multiplier applied to the light color. */
intensity?: number;
/** Constant, linear, and quadratic attenuation coefficients. */
attenuation?: Readonly<NumberArray3>;
/** Inner spotlight cone angle in radians. */
innerConeAngle?: number;
/** Outer spotlight cone angle in radians. */
outerConeAngle?: number;
};
/** Public JavaScript props accepted by the `lighting` shader module. */
export type LightingProps = {
/** Enables or disables lighting calculations for the module. */
enabled?: boolean;
/** When true, light colors are interpreted as byte-style 0-255 values. */
useByteColors?: boolean;
/** Preferred API for supplying mixed ambient, point, spot, and directional lights. */
lights?: Light[];
/**
* Legacy ambient-light prop.
* @deprecated Use `lights` with `{type: 'ambient', ...}` entries instead.
*/
ambientLight?: AmbientLight;
/**
* Legacy point-light prop.
* @deprecated Use `lights` with `{type: 'point', ...}` entries instead.
*/
pointLights?: PointLight[];
/**
* Legacy spot-light prop.
* @deprecated Use `lights` with `{type: 'spot', ...}` entries instead.
*/
spotLights?: SpotLight[];
/**
* Legacy directional-light prop.
* @deprecated Use `lights` with `{type: 'directional', ...}` entries instead.
*/
directionalLights?: DirectionalLight[];
};
/** Packed per-light data written into the module's fixed-size uniform array. */
export type LightingLightUniform = {
/** Light color converted to normalized shader-space RGB. */
color: Readonly<NumberArray3>;
/** World-space light position or a default placeholder for non-positional lights. */
position: Readonly<NumberArray3>;
/** World-space light direction or a default placeholder for positional lights. */
direction: Readonly<NumberArray3>;
/** Constant, linear, and quadratic attenuation coefficients. */
attenuation: Readonly<NumberArray3>;
/** Cosines of the inner and outer spotlight cone angles. */
coneCos: Readonly<NumberArray2>;
};
/** Fully normalized uniform values produced by the `lighting` shader module. */
export type LightingUniforms = {
/** `1` when lighting is enabled, otherwise `0`. */
enabled: number;
/** Number of packed directional lights in the `lights` array. */
directionalLightCount: number;
/** Number of packed point lights in the `lights` array. */
pointLightCount: number;
/** Number of packed spot lights in the `lights` array. */
spotLightCount: number;
/** Accumulated ambient color converted to normalized shader-space RGB. */
ambientColor: Readonly<NumberArray3>;
/** Packed trailing array of non-ambient light structs. */
lights: ReadonlyArray<LightingLightUniform>;
};
/**
* Portable lighting shader module shared by the Phong, Gouraud, and PBR material modules.
*
* The public JavaScript API accepts `lights: Light[]`, while the uniform buffer packs
* non-ambient lights into a fixed-size trailing array for portability across WebGL2 and WebGPU.
*/
export declare const lighting: {
readonly props: LightingProps;
readonly uniforms: LightingUniforms;
readonly name: "lighting";
readonly defines: {};
readonly uniformTypes: {
readonly enabled: "i32";
readonly directionalLightCount: "i32";
readonly pointLightCount: "i32";
readonly spotLightCount: "i32";
readonly ambientColor: "vec3<f32>";
readonly lights: readonly [{
readonly color: "vec3<f32>";
readonly position: "vec3<f32>";
readonly direction: "vec3<f32>";
readonly attenuation: "vec3<f32>";
readonly coneCos: "vec2<f32>";
}, 5];
};
readonly defaultUniforms: LightingUniforms;
readonly bindingLayout: readonly [{
readonly name: "lighting";
readonly group: 2;
}];
readonly firstBindingSlot: 0;
readonly source: "// #if (defined(SHADER_TYPE_FRAGMENT) && defined(LIGHTING_FRAGMENT)) || (defined(SHADER_TYPE_VERTEX) && defined(LIGHTING_VERTEX))\nconst MAX_LIGHTS: i32 = 5;\n\nstruct AmbientLight {\n color: vec3<f32>,\n};\n\nstruct PointLight {\n color: vec3<f32>,\n position: vec3<f32>,\n attenuation: vec3<f32>, // 2nd order x:Constant-y:Linear-z:Exponential\n};\n\nstruct SpotLight {\n color: vec3<f32>,\n position: vec3<f32>,\n direction: vec3<f32>,\n attenuation: vec3<f32>,\n coneCos: vec2<f32>,\n};\n\nstruct DirectionalLight {\n color: vec3<f32>,\n direction: vec3<f32>,\n};\n\nstruct UniformLight {\n color: vec3<f32>,\n position: vec3<f32>,\n direction: vec3<f32>,\n attenuation: vec3<f32>,\n coneCos: vec2<f32>,\n};\n\nstruct lightingUniforms {\n enabled: i32,\n directionalLightCount: i32,\n pointLightCount: i32,\n spotLightCount: i32,\n ambientColor: vec3<f32>,\n lights: array<UniformLight, 5>,\n};\n\n@group(2) @binding(auto) var<uniform> lighting : lightingUniforms;\n\nfn lighting_getPointLight(index: i32) -> PointLight {\n let light = lighting.lights[index];\n return PointLight(light.color, light.position, light.attenuation);\n}\n\nfn lighting_getSpotLight(index: i32) -> SpotLight {\n let light = lighting.lights[lighting.pointLightCount + index];\n return SpotLight(light.color, light.position, light.direction, light.attenuation, light.coneCos);\n}\n\nfn lighting_getDirectionalLight(index: i32) -> DirectionalLight {\n let light = lighting.lights[lighting.pointLightCount + lighting.spotLightCount + index];\n return DirectionalLight(light.color, light.direction);\n}\n\nfn getPointLightAttenuation(pointLight: PointLight, distance: f32) -> f32 {\n return pointLight.attenuation.x\n + pointLight.attenuation.y * distance\n + pointLight.attenuation.z * distance * distance;\n}\n\nfn getSpotLightAttenuation(spotLight: SpotLight, positionWorldspace: vec3<f32>) -> f32 {\n let lightDirection = normalize(positionWorldspace - spotLight.position);\n let coneFactor = smoothstep(\n spotLight.coneCos.y,\n spotLight.coneCos.x,\n dot(normalize(spotLight.direction), lightDirection)\n );\n let distanceAttenuation = getPointLightAttenuation(\n PointLight(spotLight.color, spotLight.position, spotLight.attenuation),\n distance(spotLight.position, positionWorldspace)\n );\n return distanceAttenuation / max(coneFactor, 0.0001);\n}\n";
readonly vs: "precision highp int;\n\n// #if (defined(SHADER_TYPE_FRAGMENT) && defined(LIGHTING_FRAGMENT)) || (defined(SHADER_TYPE_VERTEX) && defined(LIGHTING_VERTEX))\nstruct AmbientLight {\n vec3 color;\n};\n\nstruct PointLight {\n vec3 color;\n vec3 position;\n vec3 attenuation; // 2nd order x:Constant-y:Linear-z:Exponential\n};\n\nstruct SpotLight {\n vec3 color;\n vec3 position;\n vec3 direction;\n vec3 attenuation;\n vec2 coneCos;\n};\n\nstruct DirectionalLight {\n vec3 color;\n vec3 direction;\n};\n\nstruct UniformLight {\n vec3 color;\n vec3 position;\n vec3 direction;\n vec3 attenuation;\n vec2 coneCos;\n};\n\nlayout(std140) uniform lightingUniforms {\n int enabled;\n int directionalLightCount;\n int pointLightCount;\n int spotLightCount;\n vec3 ambientColor;\n UniformLight lights[5];\n} lighting;\n\nPointLight lighting_getPointLight(int index) {\n UniformLight light = lighting.lights[index];\n return PointLight(light.color, light.position, light.attenuation);\n}\n\nSpotLight lighting_getSpotLight(int index) {\n UniformLight light = lighting.lights[lighting.pointLightCount + index];\n return SpotLight(light.color, light.position, light.direction, light.attenuation, light.coneCos);\n}\n\nDirectionalLight lighting_getDirectionalLight(int index) {\n UniformLight light =\n lighting.lights[lighting.pointLightCount + lighting.spotLightCount + index];\n return DirectionalLight(light.color, light.direction);\n}\n\nfloat getPointLightAttenuation(PointLight pointLight, float distance) {\n return pointLight.attenuation.x\n + pointLight.attenuation.y * distance\n + pointLight.attenuation.z * distance * distance;\n}\n\nfloat getSpotLightAttenuation(SpotLight spotLight, vec3 positionWorldspace) {\n vec3 light_direction = normalize(positionWorldspace - spotLight.position);\n float coneFactor = smoothstep(\n spotLight.coneCos.y,\n spotLight.coneCos.x,\n dot(normalize(spotLight.direction), light_direction)\n );\n float distanceAttenuation = getPointLightAttenuation(\n PointLight(spotLight.color, spotLight.position, spotLight.attenuation),\n distance(spotLight.position, positionWorldspace)\n );\n return distanceAttenuation / max(coneFactor, 0.0001);\n}\n\n// #endif\n";
readonly fs: "precision highp int;\n\n// #if (defined(SHADER_TYPE_FRAGMENT) && defined(LIGHTING_FRAGMENT)) || (defined(SHADER_TYPE_VERTEX) && defined(LIGHTING_VERTEX))\nstruct AmbientLight {\n vec3 color;\n};\n\nstruct PointLight {\n vec3 color;\n vec3 position;\n vec3 attenuation; // 2nd order x:Constant-y:Linear-z:Exponential\n};\n\nstruct SpotLight {\n vec3 color;\n vec3 position;\n vec3 direction;\n vec3 attenuation;\n vec2 coneCos;\n};\n\nstruct DirectionalLight {\n vec3 color;\n vec3 direction;\n};\n\nstruct UniformLight {\n vec3 color;\n vec3 position;\n vec3 direction;\n vec3 attenuation;\n vec2 coneCos;\n};\n\nlayout(std140) uniform lightingUniforms {\n int enabled;\n int directionalLightCount;\n int pointLightCount;\n int spotLightCount;\n vec3 ambientColor;\n UniformLight lights[5];\n} lighting;\n\nPointLight lighting_getPointLight(int index) {\n UniformLight light = lighting.lights[index];\n return PointLight(light.color, light.position, light.attenuation);\n}\n\nSpotLight lighting_getSpotLight(int index) {\n UniformLight light = lighting.lights[lighting.pointLightCount + index];\n return SpotLight(light.color, light.position, light.direction, light.attenuation, light.coneCos);\n}\n\nDirectionalLight lighting_getDirectionalLight(int index) {\n UniformLight light =\n lighting.lights[lighting.pointLightCount + lighting.spotLightCount + index];\n return DirectionalLight(light.color, light.direction);\n}\n\nfloat getPointLightAttenuation(PointLight pointLight, float distance) {\n return pointLight.attenuation.x\n + pointLight.attenuation.y * distance\n + pointLight.attenuation.z * distance * distance;\n}\n\nfloat getSpotLightAttenuation(SpotLight spotLight, vec3 positionWorldspace) {\n vec3 light_direction = normalize(positionWorldspace - spotLight.position);\n float coneFactor = smoothstep(\n spotLight.coneCos.y,\n spotLight.coneCos.x,\n dot(normalize(spotLight.direction), light_direction)\n );\n float distanceAttenuation = getPointLightAttenuation(\n PointLight(spotLight.color, spotLight.position, spotLight.attenuation),\n distance(spotLight.position, positionWorldspace)\n );\n return distanceAttenuation / max(coneFactor, 0.0001);\n}\n\n// #endif\n";
readonly getUniforms: typeof getUniforms;
};
declare function getUniforms(props?: LightingProps, _prevUniforms?: Partial<LightingUniforms>): LightingUniforms;
export {};
//# sourceMappingURL=lighting.d.ts.map