@luma.gl/shadertools
Version:
Shader module system for luma.gl
131 lines (118 loc) • 5.55 kB
JavaScript
// luma.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
export const PHONG_WGSL = /* wgsl */ `\
struct phongMaterialUniforms {
ambient: f32,
diffuse: f32,
shininess: f32,
specularColor: vec3<f32>,
};
@binding(2) @group(0) 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 * 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 (lighting.enabled == 0) {
return lightColor;
}
let view_direction: vec3<f32> = normalize(cameraPosition - position_worldspace);
lightColor = phongMaterial.ambient * surfaceColor * lighting.ambientColor;
if (lighting.lightType == 0) {
let pointLight: PointLight = lighting_getPointLight(0);
let light_position_worldspace: vec3<f32> = pointLight.position;
let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
} else if (lighting.lightType == 1) {
var directionalLight: DirectionalLight = lighting_getDirectionalLight(0);
lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
}
return lightColor;
/*
for (int i = 0; i < MAX_LIGHTS; i++) {
if (i >= lighting.pointLightCount) {
break;
}
PointLight pointLight = lighting.pointLight[i];
vec3 light_position_worldspace = pointLight.position;
vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
}
for (int i = 0; i < MAX_LIGHTS; i++) {
if (i >= lighting.directionalLightCount) {
break;
}
DirectionalLight directionalLight = lighting.directionalLight[i];
lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
}
*/
}
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);
switch (lighting.lightType) {
case 0, default: {
let pointLight: PointLight = lighting_getPointLight(0);
let light_position_worldspace: vec3<f32> = pointLight.position;
let light_direction: vec3<f32> = normalize(light_position_worldspace - position_worldspace);
lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
}
case 1: {
let directionalLight: DirectionalLight = lighting_getDirectionalLight(0);
lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
}
}
}
return lightColor;
}
`;
// TODO - handle multiple lights
/**
for (int i = 0; i < MAX_LIGHTS; i++) {
if (i >= lighting.pointLightCount) {
break;
}
PointLight pointLight = lighting_getPointLight(i);
vec3 light_position_worldspace = pointLight.position;
vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
}
for (int i = 0; i < MAX_LIGHTS; i++) {
if (i >= lighting.directionalLightCount) {
break;
}
PointLight pointLight = lighting_getDirectionalLight(i);
lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
}
}
/**
for (int i = 0; i < MAX_LIGHTS; i++) {
if (i >= lighting.pointLightCount) {
break;
}
PointLight pointLight = lighting_getPointLight(i);
vec3 light_position_worldspace = pointLight.position;
vec3 light_direction = normalize(light_position_worldspace - position_worldspace);
lightColor += lighting_getLightColor(surfaceColor, light_direction, view_direction, normal_worldspace, pointLight.color);
}
for (int i = 0; i < MAX_LIGHTS; i++) {
if (i >= lighting.directionalLightCount) {
break;
}
PointLight pointLight = lighting_getDirectionalLight(i);
lightColor += lighting_getLightColor(surfaceColor, -directionalLight.direction, view_direction, normal_worldspace, directionalLight.color);
}
}
*/
//# sourceMappingURL=phong-shaders-wgsl.js.map