Graphics/Shader: Add light shadow support to PBR
This commit is contained in:
parent
ceedfbabaf
commit
d92a307fb8
|
|
@ -6,10 +6,12 @@ import LightData from Engine.LightData;
|
||||||
import SkeletalData from Engine.SkeletalData;
|
import SkeletalData from Engine.SkeletalData;
|
||||||
import ViewerData from Engine.ViewerData;
|
import ViewerData from Engine.ViewerData;
|
||||||
|
|
||||||
|
import * from Engine.LightShadow;
|
||||||
import SkinLinearPosition, SkinLinearPositionNormal from Engine.SkinningLinear;
|
import SkinLinearPosition, SkinLinearPositionNormal from Engine.SkinningLinear;
|
||||||
|
|
||||||
// Pass-specific options
|
// Pass-specific options
|
||||||
option DepthPass: bool = false;
|
option DepthPass: bool = false;
|
||||||
|
option DistanceDepth: bool = false;
|
||||||
|
|
||||||
// Basic material options
|
// Basic material options
|
||||||
option HasBaseColorTexture: bool = false;
|
option HasBaseColorTexture: bool = false;
|
||||||
|
|
@ -40,6 +42,8 @@ option VertexUvLoc: i32 = -1;
|
||||||
option VertexJointIndicesLoc: i32 = -1;
|
option VertexJointIndicesLoc: i32 = -1;
|
||||||
option VertexJointWeightsLoc: i32 = -1;
|
option VertexJointWeightsLoc: i32 = -1;
|
||||||
|
|
||||||
|
option MaxLightCount: u32 = u32(3); //< FIXME: Fix integral value types
|
||||||
|
|
||||||
const HasNormal = (VertexNormalLoc >= 0);
|
const HasNormal = (VertexNormalLoc >= 0);
|
||||||
const HasVertexColor = (VertexColorLoc >= 0);
|
const HasVertexColor = (VertexColorLoc >= 0);
|
||||||
const HasColor = (HasVertexColor || Billboard);
|
const HasColor = (HasVertexColor || Billboard);
|
||||||
|
|
@ -87,7 +91,10 @@ external
|
||||||
[tag("InstanceData")] instanceData: uniform[InstanceData],
|
[tag("InstanceData")] instanceData: uniform[InstanceData],
|
||||||
[tag("ViewerData")] viewerData: uniform[ViewerData],
|
[tag("ViewerData")] viewerData: uniform[ViewerData],
|
||||||
[tag("SkeletalData")] skeletalData: uniform[SkeletalData],
|
[tag("SkeletalData")] skeletalData: uniform[SkeletalData],
|
||||||
[tag("LightData")] lightData: uniform[LightData]
|
[tag("LightData")] lightData: uniform[LightData],
|
||||||
|
[tag("ShadowMapsDirectional")] shadowMapsDirectional: array[depth_sampler2D_array[f32], MaxLightCount],
|
||||||
|
[tag("ShadowMapsPoint")] shadowMapsPoint: array[sampler_cube[f32], MaxLightCount],
|
||||||
|
[tag("ShadowMapsSpot")] shadowMapsSpot: array[depth_sampler2D[f32], MaxLightCount],
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VertToFrag
|
struct VertToFrag
|
||||||
|
|
@ -101,16 +108,16 @@ struct VertToFrag
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fragment stage
|
// Fragment stage
|
||||||
import DistributionGGX, GeometrySmith, FresnelSchlick from Math.CookTorrancePBR;
|
import ComputeLightRadiance, DistributionGGX, GeometrySmith, FresnelSchlick from Math.CookTorrancePBR;
|
||||||
import Pi from Math.Constants;
|
import Pi from Math.Constants;
|
||||||
|
|
||||||
struct FragOut
|
struct FragOut
|
||||||
{
|
{
|
||||||
[location(0)] RenderTarget0: vec4[f32]
|
[location(0)] RenderTarget0: vec4[f32],
|
||||||
|
[builtin(frag_depth), cond(DistanceDepth)] fragdepth: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
[entry(frag), cond(!DepthPass || AlphaTest)]
|
fn ComputeColor(input: VertToFrag) -> vec4[f32]
|
||||||
fn main(input: VertToFrag) -> FragOut
|
|
||||||
{
|
{
|
||||||
let color = settings.BaseColor;
|
let color = settings.BaseColor;
|
||||||
|
|
||||||
|
|
@ -126,6 +133,13 @@ fn main(input: VertToFrag) -> FragOut
|
||||||
const if (HasAlphaTexture)
|
const if (HasAlphaTexture)
|
||||||
color.w *= MaterialAlphaMap.Sample(input.uv).x;
|
color.w *= MaterialAlphaMap.Sample(input.uv).x;
|
||||||
|
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
[entry(frag), cond(!DepthPass)]
|
||||||
|
fn main(input: VertToFrag) -> FragOut
|
||||||
|
{
|
||||||
|
let color = ComputeColor(input);
|
||||||
const if (AlphaTest)
|
const if (AlphaTest)
|
||||||
{
|
{
|
||||||
if (color.w < settings.AlphaThreshold)
|
if (color.w < settings.AlphaThreshold)
|
||||||
|
|
@ -170,61 +184,55 @@ fn main(input: VertToFrag) -> FragOut
|
||||||
|
|
||||||
let albedoFactor = albedo / Pi;
|
let albedoFactor = albedo / Pi;
|
||||||
|
|
||||||
/*for i in u32(0) -> lightData.lightCount
|
for lightIndex in u32(0) -> lightData.directionalLightCount
|
||||||
{
|
{
|
||||||
let light = lightData.lights[i];
|
let light = lightData.directionalLights[lightIndex];
|
||||||
|
|
||||||
let attenuation = 1.0;
|
let lambert = max(dot(normal, -light.direction), 0.0);
|
||||||
|
|
||||||
// TODO: Add switch instruction
|
let shadowFactor = ComputeDirectionalLightShadow(light, shadowMapsDirectional[lightIndex], input.worldPos, lambert, viewerData.viewMatrix);
|
||||||
let lightToPosNorm: vec3[f32];
|
let radiance = ComputeLightRadiance(light.color.rgb, -light.direction, 1.0, albedoFactor, eyeVec, F0, normal, metallic, roughness);
|
||||||
if (light.type == DirectionalLight)
|
|
||||||
lightToPosNorm = -light.parameter1.xyz;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// PointLight | SpotLight
|
|
||||||
let lightPos = light.parameter1.xyz;
|
|
||||||
let lightInvRadius = light.parameter1.w;
|
|
||||||
|
|
||||||
let lightToPos = input.worldPos - lightPos;
|
lightRadiance += shadowFactor * radiance;
|
||||||
let dist = length(lightToPos);
|
}
|
||||||
|
|
||||||
attenuation = max(1.0 - dist * lightInvRadius, 0.0);
|
for lightIndex in u32(0) -> lightData.pointLightCount
|
||||||
lightToPosNorm = lightToPos / max(dist, 0.0001);
|
{
|
||||||
|
let light = lightData.pointLights[lightIndex];
|
||||||
|
|
||||||
if (light.type == SpotLight)
|
let lightToPos = input.worldPos - light.position;
|
||||||
{
|
let dist = length(lightToPos);
|
||||||
let lightDir = light.parameter2.xyz;
|
|
||||||
let lightInnerAngle = light.parameter3.x;
|
|
||||||
let lightOuterAngle = light.parameter3.y;
|
|
||||||
|
|
||||||
let curAngle = dot(lightDir, lightToPosNorm);
|
let attenuation = max(1.0 - dist * light.invRadius, 0.0);
|
||||||
let innerMinusOuterAngle = lightInnerAngle - lightOuterAngle;
|
let lightToPosNorm = lightToPos / max(dist, 0.0001);
|
||||||
|
|
||||||
attenuation *= max((curAngle - lightOuterAngle) / innerMinusOuterAngle, 0.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let radiance = light.color.rgb * attenuation;
|
let shadowFactor = ComputePointLightShadow(light, shadowMapsPoint[lightIndex], dist, lightToPosNorm);
|
||||||
|
let radiance = ComputeLightRadiance(light.color.rgb, lightToPosNorm, attenuation, albedoFactor, eyeVec, F0, normal, metallic, roughness);
|
||||||
|
|
||||||
let halfDir = normalize(lightToPosNorm + eyeVec);
|
lightRadiance += shadowFactor * radiance;
|
||||||
|
}
|
||||||
|
|
||||||
// Cook-Torrance BRDF
|
for lightIndex in u32(0) -> lightData.spotLightCount
|
||||||
let NDF = DistributionGGX(normal, halfDir, roughness);
|
{
|
||||||
let G = GeometrySmith(normal, eyeVec, lightToPosNorm, roughness);
|
let light = lightData.spotLights[lightIndex];
|
||||||
let F = FresnelSchlick(max(dot(halfDir, eyeVec), 0.0), F0);
|
|
||||||
|
|
||||||
let kS = F;
|
let lightToPos = input.worldPos - light.position;
|
||||||
let diffuse = vec3[f32](1.0, 1.0, 1.0) - kS;
|
let dist = length(lightToPos);
|
||||||
diffuse *= 1.0 - metallic;
|
let lightToPosNorm = lightToPos / max(dist, 0.0001);
|
||||||
|
|
||||||
let numerator = NDF * G * F;
|
let curAngle = dot(light.direction, lightToPosNorm);
|
||||||
let denominator = 4.0 * max(dot(normal, eyeVec), 0.0) * max(dot(normal, lightToPosNorm), 0.0);
|
let innerMinusOuterAngle = light.innerAngle - light.outerAngle;
|
||||||
let specular = numerator / max(denominator, 0.0001);
|
|
||||||
|
|
||||||
let NdotL = max(dot(normal, lightToPosNorm), 0.0);
|
let attenuation = max(1.0 - dist * light.invRadius, 0.0);
|
||||||
lightRadiance += (diffuse * albedoFactor + specular) * radiance * NdotL;
|
attenuation *= max((curAngle - light.outerAngle) / innerMinusOuterAngle, 0.0);
|
||||||
}*/
|
|
||||||
|
let lambert = clamp(dot(normal, -lightToPosNorm), 0.0, 1.0);
|
||||||
|
|
||||||
|
let shadowFactor = ComputeSpotLightShadow(light, shadowMapsSpot[lightIndex], input.worldPos, lambert);
|
||||||
|
let radiance = ComputeLightRadiance(light.color.rgb, lightToPosNorm, attenuation, albedoFactor, eyeVec, F0, normal, metallic, roughness);
|
||||||
|
|
||||||
|
lightRadiance += shadowFactor * radiance;
|
||||||
|
}
|
||||||
|
|
||||||
let ambient = (0.03).rrr * albedo;
|
let ambient = (0.03).rrr * albedo;
|
||||||
|
|
||||||
|
|
@ -244,9 +252,41 @@ fn main(input: VertToFrag) -> FragOut
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dummy fragment shader (TODO: Add a way to delete stage?)
|
// Shadow passes entries
|
||||||
[entry(frag), cond(DepthPass && !AlphaTest)]
|
[entry(frag), cond(DepthPass && DistanceDepth)]
|
||||||
fn main() {}
|
[depth_write(replace)]
|
||||||
|
fn main(input: VertToFrag) -> FragOut
|
||||||
|
{
|
||||||
|
let color = ComputeColor(input);
|
||||||
|
const if (AlphaTest)
|
||||||
|
{
|
||||||
|
if (color.w < settings.AlphaThreshold)
|
||||||
|
discard;
|
||||||
|
}
|
||||||
|
|
||||||
|
let output: FragOut;
|
||||||
|
output.RenderTarget0 = color;
|
||||||
|
|
||||||
|
let dist = distance(viewerData.eyePosition, input.worldPos);
|
||||||
|
output.fragdepth = dist / viewerData.farPlane;
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
[entry(frag), cond(DepthPass && AlphaTest && !DistanceDepth)]
|
||||||
|
fn main(input: VertToFrag) -> FragOut
|
||||||
|
{
|
||||||
|
let color = ComputeColor(input);
|
||||||
|
if (color.w < settings.AlphaThreshold)
|
||||||
|
discard;
|
||||||
|
|
||||||
|
let output: FragOut;
|
||||||
|
output.RenderTarget0 = color;
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
[entry(frag), cond(DepthPass && !AlphaTest && !DistanceDepth)]
|
||||||
|
fn main() {} //< dummy
|
||||||
|
|
||||||
// Vertex stage
|
// Vertex stage
|
||||||
struct VertIn
|
struct VertIn
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue