Add initial support for normal mapping and other light types
This commit is contained in:
@@ -21,12 +21,15 @@ option BillboardSizeRotLocation: i32 = -1;
|
||||
option ColorLocation: i32 = -1;
|
||||
option NormalLocation: i32 = -1;
|
||||
option PosLocation: i32 = -1;
|
||||
option TangentLocation: i32 = -1;
|
||||
option UvLocation: i32 = -1;
|
||||
|
||||
const HasNormal = (NormalLocation >= 0);
|
||||
const HasVertexColor = (ColorLocation >= 0);
|
||||
const HasColor = (HasVertexColor || Billboard);
|
||||
const HasTangent = (TangentLocation >= 0);
|
||||
const HasUV = (UvLocation >= 0);
|
||||
const HasNormalMapping = HasNormalTexture && HasNormal && HasTangent;
|
||||
|
||||
[layout(std140)]
|
||||
struct MaterialSettings
|
||||
@@ -101,22 +104,24 @@ external
|
||||
[binding(10)] MaterialSpecularMap: sampler2D<f32>,
|
||||
}
|
||||
|
||||
// Fragment stage
|
||||
struct FragIn
|
||||
struct VertToFrag
|
||||
{
|
||||
[location(0)] worldPos: vec3<f32>,
|
||||
[location(1), cond(HasUV)] uv: vec2<f32>,
|
||||
[location(2), cond(HasColor)] color: vec4<f32>,
|
||||
[location(3), cond(HasNormal)] normal: vec3<f32>,
|
||||
[location(4), cond(HasNormalMapping)] tbnMatrix: mat3<f32>,
|
||||
[builtin(position)] position: vec4<f32>,
|
||||
}
|
||||
|
||||
// Fragment stage
|
||||
struct FragOut
|
||||
{
|
||||
[location(0)] RenderTarget0: vec4<f32>
|
||||
}
|
||||
|
||||
[entry(frag)]
|
||||
fn main(input: FragIn) -> FragOut
|
||||
fn main(input: VertToFrag) -> FragOut
|
||||
{
|
||||
let diffuseColor = settings.DiffuseColor;
|
||||
|
||||
@@ -146,6 +151,12 @@ fn main(input: FragIn) -> FragOut
|
||||
|
||||
let eyeVec = normalize(viewerData.eyePosition - input.worldPos);
|
||||
|
||||
let normal: vec3<f32>;
|
||||
const if (HasNormalMapping)
|
||||
normal = normalize(input.tbnMatrix * (MaterialNormalMap.Sample(input.uv).xyz * 2.0 - vec3<f32>(1.0, 1.0, 1.0)));
|
||||
else
|
||||
normal = normalize(input.normal);
|
||||
|
||||
for i in 0 -> lightData.lightCount
|
||||
{
|
||||
let light = lightData.lights[i];
|
||||
@@ -156,15 +167,15 @@ fn main(input: FragIn) -> FragOut
|
||||
// TODO: Add switch instruction
|
||||
if (light.type == DirectionalLight)
|
||||
{
|
||||
let lightDir = -(light.parameter1.xyz); //< FIXME
|
||||
let lightDir = light.parameter1.xyz;
|
||||
|
||||
lightAmbient += light.color.rgb * lightAmbientFactor * settings.AmbientColor;
|
||||
|
||||
let lambert = max(dot(input.normal, lightDir), 0.0);
|
||||
let lambert = max(dot(normal, -lightDir), 0.0);
|
||||
|
||||
lightDiffuse += lambert * light.color.rgb * lightDiffuseFactor;
|
||||
|
||||
let reflection = reflect(-lightDir, input.normal);
|
||||
let reflection = reflect(lightDir, normal);
|
||||
let specFactor = max(dot(reflection, eyeVec), 0.0);
|
||||
specFactor = pow(specFactor, settings.Shininess);
|
||||
|
||||
@@ -172,11 +183,56 @@ fn main(input: FragIn) -> FragOut
|
||||
}
|
||||
else if (light.type == PointLight)
|
||||
{
|
||||
|
||||
let lightPos = light.parameter1.xyz;
|
||||
let lightInvRadius = light.parameter1.w;
|
||||
|
||||
let lightToPos = input.worldPos - lightPos;
|
||||
let dist = length(lightToPos);
|
||||
let lightToPosNorm = lightToPos / max(dist, 0.0001);
|
||||
|
||||
let attenuationFactor = max(1.0 - dist * lightInvRadius, 0.0);
|
||||
|
||||
lightAmbient += attenuationFactor * light.color.rgb * lightAmbientFactor * settings.AmbientColor;
|
||||
|
||||
let lambert = max(dot(normal, -lightToPosNorm), 0.0);
|
||||
|
||||
lightDiffuse += attenuationFactor * lambert * light.color.rgb * lightDiffuseFactor;
|
||||
|
||||
let reflection = reflect(lightToPosNorm, normal);
|
||||
let specFactor = max(dot(reflection, eyeVec), 0.0);
|
||||
specFactor = pow(specFactor, settings.Shininess);
|
||||
|
||||
lightSpecular += attenuationFactor * specFactor * light.color.rgb;
|
||||
}
|
||||
else if (light.type == SpotLight)
|
||||
{
|
||||
|
||||
let lightPos = light.parameter1.xyz;
|
||||
let lightDir = light.parameter2.xyz;
|
||||
let lightInvRadius = light.parameter1.w;
|
||||
let lightInnerAngle = light.parameter3.x;
|
||||
let lightOuterAngle = light.parameter3.y;
|
||||
|
||||
let lightToPos = input.worldPos - lightPos;
|
||||
let dist = length(lightToPos);
|
||||
let lightToPosNorm = lightToPos / max(dist, 0.0001);
|
||||
|
||||
let curAngle = dot(lightDir, lightToPosNorm);
|
||||
let innerMinusOuterAngle = lightInnerAngle - lightOuterAngle;
|
||||
|
||||
let attenuationFactor = max(1.0 - dist * lightInvRadius, 0.0);
|
||||
attenuationFactor *= max((curAngle - lightOuterAngle) / innerMinusOuterAngle, 0.0);
|
||||
|
||||
lightAmbient += attenuationFactor * light.color.rgb * lightAmbientFactor * settings.AmbientColor;
|
||||
|
||||
let lambert = max(dot(normal, -lightToPosNorm), 0.0);
|
||||
|
||||
lightDiffuse += attenuationFactor * lambert * light.color.rgb * lightDiffuseFactor;
|
||||
|
||||
let reflection = reflect(lightToPosNorm, normal);
|
||||
let specFactor = max(dot(reflection, eyeVec), 0.0);
|
||||
specFactor = pow(specFactor, settings.Shininess);
|
||||
|
||||
lightSpecular += attenuationFactor * specFactor * light.color.rgb;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,6 +270,9 @@ struct VertIn
|
||||
[cond(HasNormal), location(NormalLocation)]
|
||||
normal: vec3<f32>,
|
||||
|
||||
[cond(HasTangent), location(TangentLocation)]
|
||||
tangent: vec3<f32>,
|
||||
|
||||
[cond(Billboard), location(BillboardCenterLocation)]
|
||||
billboardCenter: vec3<f32>,
|
||||
|
||||
@@ -224,15 +283,6 @@ struct VertIn
|
||||
billboardColor: vec4<f32>
|
||||
}
|
||||
|
||||
struct VertOut
|
||||
{
|
||||
[location(0)] worldPos: vec3<f32>,
|
||||
[location(1), cond(HasUV)] uv: vec2<f32>,
|
||||
[location(2), cond(HasColor)] color: vec4<f32>,
|
||||
[location(3), cond(HasNormal)] normal: vec3<f32>,
|
||||
[builtin(position)] position: vec4<f32>,
|
||||
}
|
||||
|
||||
[entry(vert), cond(Billboard)]
|
||||
fn billboardMain(input: VertIn) -> VertOut
|
||||
{
|
||||
@@ -252,7 +302,7 @@ fn billboardMain(input: VertIn) -> VertOut
|
||||
vertexPos += cameraRight * rotatedPosition.x;
|
||||
vertexPos += cameraUp * rotatedPosition.y;
|
||||
|
||||
let output: VertOut;
|
||||
let output: VertToFrag;
|
||||
output.position = viewerData.viewProjMatrix * instanceData.worldMatrix * vec4<f32>(vertexPos, 1.0);
|
||||
|
||||
const if (HasColor)
|
||||
@@ -265,22 +315,32 @@ fn billboardMain(input: VertIn) -> VertOut
|
||||
}
|
||||
|
||||
[entry(vert), cond(!Billboard)]
|
||||
fn main(input: VertIn) -> VertOut
|
||||
fn main(input: VertIn) -> VertToFrag
|
||||
{
|
||||
let worldPosition = instanceData.worldMatrix * vec4<f32>(input.pos, 1.0);
|
||||
|
||||
let output: VertOut;
|
||||
let output: VertToFrag;
|
||||
output.worldPos = worldPosition.xyz;
|
||||
output.position = viewerData.viewProjMatrix * worldPosition;
|
||||
|
||||
let rotationMatrix = mat3<f32>(instanceData.worldMatrix);
|
||||
|
||||
const if (HasColor)
|
||||
output.color = input.color;
|
||||
|
||||
const if (HasNormal)
|
||||
output.normal = input.normal;
|
||||
output.normal = rotationMatrix * input.normal;
|
||||
|
||||
const if (HasUV)
|
||||
output.uv = input.uv;
|
||||
|
||||
const if (HasNormalMapping)
|
||||
{
|
||||
let binormal = cross(input.normal, input.tangent);
|
||||
output.tbnMatrix[0] = normalize(rotationMatrix * input.tangent);
|
||||
output.tbnMatrix[1] = normalize(rotationMatrix * binormal);
|
||||
output.tbnMatrix[2] = normalize(rotationMatrix * input.normal);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user