// 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, }; @group(3) @binding(auto) var phongMaterial : phongMaterialUniforms; fn lighting_getLightColor(surfaceColor: vec3, light_direction: vec3, view_direction: vec3, normal_worldspace: vec3, color: vec3) -> vec3 { let halfway_direction: vec3 = 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, cameraPosition: vec3, position_worldspace: vec3, normal_worldspace: vec3) -> vec3 { var lightColor: vec3 = surfaceColor; if (phongMaterial.unlit != 0u) { return surfaceColor; } if (lighting.enabled == 0) { return lightColor; } let view_direction: vec3 = 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 = pointLight.position; let light_direction: vec3 = 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 = spotLight.position; let light_direction: vec3 = 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, position_worldspace: vec3, normal_worldspace: vec3) -> vec3{ var lightColor = vec3(0, 0, 0); let surfaceColor = vec3(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 = pointLight.position; let light_direction: vec3 = 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 = spotLight.position; let light_direction: vec3 = 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; } `;