pex-renderer
Version:
Physically Based Renderer for Pex
38 lines (32 loc) • 1.26 kB
JavaScript
module.exports = /* glsl */ `
struct DirectionalLight {
vec3 direction;
vec4 color;
mat4 projectionMatrix;
mat4 viewMatrix;
bool castShadows;
float near;
float far;
float bias;
vec2 shadowMapSize;
};
uniform DirectionalLight uDirectionalLights[NUM_DIRECTIONAL_LIGHTS];
uniform sampler2D uDirectionalLightShadowMaps[NUM_DIRECTIONAL_LIGHTS]; //TODO: is it ok to sample depth texture as sampler2D?
void EvaluateDirectionalLight(inout PBRData data, DirectionalLight light, sampler2D shadowMap) {
vec4 lightViewPosition = light.viewMatrix * vec4(vPositionWorld, 1.0);
float lightDistView = -lightViewPosition.z;
vec4 lightDeviceCoordsPosition = light.projectionMatrix * lightViewPosition;
vec3 lightDeviceCoordsPositionNormalized = lightDeviceCoordsPosition.xyz / lightDeviceCoordsPosition.w;
vec2 lightUV = lightDeviceCoordsPositionNormalized.xy * 0.5 + 0.5;
float illuminated = bool(light.castShadows) ? getShadow(shadowMap, light.shadowMapSize, lightUV, lightDistView - light.bias, light.near, light.far) : 1.0;
if (illuminated > 0.0) {
Light l;
l.l = -light.direction;
l.color = light.color;
l.attenuation = 1.0;
getSurfaceShading(data, l, illuminated);
}
}
`