@luma.gl/shadertools
Version:
Shader module system for luma.gl
130 lines (113 loc) • 4.88 kB
text/typescript
// luma.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
export const PHONG_WGSL = /* wgsl */ `\
struct phongMaterialUniforms {
unlit: u32,
ambient: f32,
diffuse: f32,
shininess: f32,
specularColor: vec3<f32>,
};
@group(3) @binding(auto) var<uniform> phongMaterial : phongMaterialUniforms;
fn lighting_getLightColor(surfaceColor: vec3<f32>, light_direction: vec3<f32>, view_direction: vec3<f32>, normal_worldspace: vec3<f32>, color: vec3<f32>) -> vec3<f32> {
let halfway_direction: vec3<f32> = normalize(light_direction + view_direction);
var lambertian: f32 = dot(light_direction, normal_worldspace);
var specular: f32 = 0.0;
if (lambertian > 0.0) {
let specular_angle = max(dot(normal_worldspace, halfway_direction), 0.0);
specular = pow(specular_angle, phongMaterial.shininess);
}
lambertian = max(lambertian, 0.0);
return (
lambertian * phongMaterial.diffuse * surfaceColor +
specular * floatColors_normalize(phongMaterial.specularColor)
) * color;
}
fn lighting_getLightColor2(surfaceColor: vec3<f32>, cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32> {
var lightColor: vec3<f32> = surfaceColor;
if (phongMaterial.unlit != 0u) {
return surfaceColor;
}
if (lighting.enabled == 0) {
return lightColor;
}
let view_direction: vec3<f32> = normalize(cameraPosition - position_worldspace);
lightColor = phongMaterial.ambient * surfaceColor * lighting.ambientColor;
for (var i: i32 = 0; i < lighting.pointLightCount; i++) {
let pointLight: PointLight = lighting_getPointLight(i);
let light_position_worldspace: vec3<f32> = pointLight.position;
let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
let light_attenuation = getPointLightAttenuation(
pointLight,
distance(light_position_worldspace, position_worldspace)
);
lightColor += lighting_getLightColor(
surfaceColor,
light_direction,
view_direction,
normal_worldspace,
pointLight.color / light_attenuation
);
}
for (var i: i32 = 0; i < lighting.spotLightCount; i++) {
let spotLight: SpotLight = lighting_getSpotLight(i);
let light_position_worldspace: vec3<f32> = spotLight.position;
let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
let light_attenuation = getSpotLightAttenuation(spotLight, position_worldspace);
lightColor += lighting_getLightColor(
surfaceColor,
light_direction,
view_direction,
normal_worldspace,
spotLight.color / light_attenuation
);
}
for (var i: i32 = 0; i < lighting.directionalLightCount; i++) {
let directionalLight: DirectionalLight = lighting_getDirectionalLight(i);
lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
}
return lightColor;
}
fn lighting_getSpecularLightColor(cameraPosition: vec3<f32>, position_worldspace: vec3<f32>, normal_worldspace: vec3<f32>) -> vec3<f32>{
var lightColor = vec3<f32>(0, 0, 0);
let surfaceColor = vec3<f32>(0, 0, 0);
if (lighting.enabled != 0) {
let view_direction = normalize(cameraPosition - position_worldspace);
for (var i: i32 = 0; i < lighting.pointLightCount; i++) {
let pointLight: PointLight = lighting_getPointLight(i);
let light_position_worldspace: vec3<f32> = pointLight.position;
let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
let light_attenuation = getPointLightAttenuation(
pointLight,
distance(light_position_worldspace, position_worldspace)
);
lightColor += lighting_getLightColor(
surfaceColor,
light_direction,
view_direction,
normal_worldspace,
pointLight.color / light_attenuation
);
}
for (var i: i32 = 0; i < lighting.spotLightCount; i++) {
let spotLight: SpotLight = lighting_getSpotLight(i);
let light_position_worldspace: vec3<f32> = spotLight.position;
let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
let light_attenuation = getSpotLightAttenuation(spotLight, position_worldspace);
lightColor += lighting_getLightColor(
surfaceColor,
light_direction,
view_direction,
normal_worldspace,
spotLight.color / light_attenuation
);
}
for (var i: i32 = 0; i < lighting.directionalLightCount; i++) {
let directionalLight: DirectionalLight = lighting_getDirectionalLight(i);
lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
}
}
return lightColor;
}
`;