Reworking tangent lighting
This commit is contained in:
@@ -14,7 +14,7 @@ struct DirectionalLight : ILightEnv
|
||||
|
||||
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
|
||||
{
|
||||
float3 dir_TS = mul(params.tbn, -normalize(direction.xyz));
|
||||
float3 dir_TS = mul(params.worldToTangent, -normalize(direction.xyz));
|
||||
return brdf.evaluate(params.viewDir_TS, dir_TS, color.xyz);
|
||||
}
|
||||
};
|
||||
@@ -26,7 +26,7 @@ struct PointLight : ILightEnv
|
||||
|
||||
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
|
||||
{
|
||||
float3 pos_TS = mul(params.tbn, position_WS.xyz);
|
||||
float3 pos_TS = mul(params.worldToTangent, position_WS.xyz);
|
||||
float3 lightDir_TS = pos_TS.xyz - params.position_TS;
|
||||
float d = length(lightDir_TS);
|
||||
float illuminance = max(1 - d / colorRange.w, 0);
|
||||
|
||||
@@ -7,34 +7,40 @@ struct MaterialParameter
|
||||
float3 vertexColor;
|
||||
};
|
||||
|
||||
// data used by light environment
|
||||
struct LightingParameter
|
||||
{
|
||||
float3x3 tbn;
|
||||
float3 normal_TS;
|
||||
float3 position_TS;
|
||||
float3 viewDir_TS;
|
||||
};
|
||||
float3x3 worldToTangent;
|
||||
float3 viewDir_TS;
|
||||
float3 position_TS;
|
||||
}
|
||||
|
||||
// Constructs a TBN matrix from a unpacked qtangent for example for after vertex interpolation in the fragment shader
|
||||
float3x3 constructTBNFromQTangent(float4 q){
|
||||
q = normalize(q); // Ensure that the quaternion is normalized in case it is not, for example after interpolation and so on
|
||||
float3 t2 = q.xyz * 2.0, tx = q.xxx * t2.xyz, ty = q.yyy * t2.xyz, tz = q.www * t2.xyz;
|
||||
float3 tangent = float3(1.0 - (ty.y + (q.z * t2.z)), tx.y + tz.z, tx.z - tz.y);
|
||||
float3 normal = float3(tx.z + tz.y, ty.z - tz.x, 1.0 - (tx.x + ty.y));
|
||||
return float3x3(tangent, cross(tangent, normal) * ((q.w < 0.0) ? -1.0 : 1.0), normal);
|
||||
}
|
||||
|
||||
// data passed to fragment shader
|
||||
struct FragmentParameter
|
||||
{
|
||||
float4 position_CS : SV_Position;
|
||||
#ifndef POS_ONLY
|
||||
float3 normal_WS : NORMALWS;
|
||||
float3 tangent_WS : TANGENTWS;
|
||||
float3 biTangent_WS : BITANGENTWS;
|
||||
float4 qTangent : QTANGENT;
|
||||
float3 position_WS : POSITIONWS;
|
||||
float3 position_TS : POSITIONTS;
|
||||
float3 viewDir_TS : VIEWDIRTS;
|
||||
float3 vertexColor : COLOR;
|
||||
float4 texCoords0 : TEXCOORDS0;
|
||||
float4 texCoords1 : TEXCOORDS1;
|
||||
float4 texCoords2 : TEXCOORDS2;
|
||||
float4 texCoords3 : TEXCOORDS3;
|
||||
MaterialParameter getMaterialParameter()
|
||||
{
|
||||
MaterialParameter result;
|
||||
result.position_WS = position_WS;
|
||||
result.vertexColor = vertexColor;
|
||||
MaterialParameter getMaterialParameter()
|
||||
{
|
||||
MaterialParameter result;
|
||||
result.position_WS = position_WS;
|
||||
result.texCoords[0] = texCoords0.xy;
|
||||
result.texCoords[1] = texCoords0.zw;
|
||||
result.texCoords[2] = texCoords1.xy;
|
||||
@@ -43,22 +49,18 @@ struct FragmentParameter
|
||||
result.texCoords[5] = texCoords2.zw;
|
||||
result.texCoords[6] = texCoords3.xy;
|
||||
result.texCoords[7] = texCoords3.zw;
|
||||
return result;
|
||||
}
|
||||
LightingParameter getLightingParameter()
|
||||
{
|
||||
LightingParameter result;
|
||||
float3x3 tbn = float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS));
|
||||
result.tbn = tbn;
|
||||
result.position_TS = mul(tbn, position_WS);
|
||||
result.viewDir_TS = mul(tbn, normalize(pViewParams.cameraPosition_WS.xyz - position_WS));
|
||||
result.normal_TS = mul(tbn, normal_WS);
|
||||
return result;
|
||||
}
|
||||
float3x3 getTangentToWorld()
|
||||
result.vertexColor = vertexColor;
|
||||
return result;
|
||||
}
|
||||
|
||||
LightingParameter getLightingParameter()
|
||||
{
|
||||
float3x3 tbn = float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS));
|
||||
return transpose(tbn);
|
||||
float3x3 worldToTangent = transpose(constructTBNFromQTangent(qTangent));
|
||||
LightingParameter result;
|
||||
result.worldToTangent = worldToTangent;
|
||||
result.viewDir_TS = viewDir_TS;
|
||||
result.position_TS = position_TS;
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
static FragmentParameter interpolate(FragmentParameter f0, FragmentParameter f1, FragmentParameter f2, float3 barycentricCoords)
|
||||
@@ -66,9 +68,7 @@ struct FragmentParameter
|
||||
FragmentParameter result;
|
||||
result.position_CS = f0.position_CS * barycentricCoords.x + f1.position_CS * barycentricCoords.y + f2.position_CS * barycentricCoords.z;
|
||||
#ifndef POS_ONLY
|
||||
result.normal_WS = f0.normal_WS * barycentricCoords.x + f1.normal_WS * barycentricCoords.y + f2.normal_WS * barycentricCoords.z;
|
||||
result.tangent_WS = f0.tangent_WS * barycentricCoords.x + f1.tangent_WS * barycentricCoords.y + f2.tangent_WS * barycentricCoords.z;
|
||||
result.biTangent_WS = f0.biTangent_WS * barycentricCoords.x + f1.biTangent_WS * barycentricCoords.y + f2.biTangent_WS * barycentricCoords.z;
|
||||
result.qTangent = f0.qTangent * barycentricCoords.x + f1.qTangent * barycentricCoords.y + f2.qTangent * barycentricCoords.z;
|
||||
result.position_WS = f0.position_WS * barycentricCoords.x + f1.position_WS * barycentricCoords.y + f2.position_WS * barycentricCoords.z;
|
||||
result.vertexColor = f0.vertexColor * barycentricCoords.x + f1.vertexColor * barycentricCoords.y + f2.vertexColor * barycentricCoords.z;
|
||||
//for(uint i = 0; i < MAX_TEXCOORDS; ++i)
|
||||
@@ -85,9 +85,7 @@ struct VertexAttributes
|
||||
{
|
||||
float3 position_MS;
|
||||
#ifndef POS_ONLY
|
||||
float3 normal_MS;
|
||||
float3 tangent_MS;
|
||||
float3 biTangent_MS;
|
||||
float4 qTangent;
|
||||
float3 vertexColor;
|
||||
float2 texCoords[MAX_TEXCOORDS];
|
||||
#endif
|
||||
@@ -100,15 +98,12 @@ struct VertexAttributes
|
||||
FragmentParameter result;
|
||||
result.position_CS = clipPos;
|
||||
#ifndef POS_ONLY
|
||||
float3x3 normalMatrix = float3x3(transformMatrix);
|
||||
float3 T = mul(normalMatrix, tangent_MS);
|
||||
float3 N = mul(normalMatrix, normal_MS);
|
||||
float3 B = mul(normalMatrix, biTangent_MS);
|
||||
result.normal_WS = N;
|
||||
result.tangent_WS = T;
|
||||
result.biTangent_WS = B;
|
||||
result.position_WS = worldPos.xyz;
|
||||
result.qTangent = qTangent;
|
||||
float3x3 tbn = transpose(constructTBNFromQTangent(qTangent));
|
||||
result.position_TS = mul(tbn, worldPos.xyz);
|
||||
result.vertexColor = vertexColor;
|
||||
result.position_WS = worldPos.xyz;
|
||||
result.viewDir_TS = mul(tbn, pViewParams.cameraPosition_WS.xyz - worldPos.xyz);
|
||||
result.texCoords0 = float4(texCoords[0], texCoords[1]);
|
||||
result.texCoords1 = float4(texCoords[2], texCoords[3]);
|
||||
result.texCoords2 = float4(texCoords[4], texCoords[5]);
|
||||
|
||||
@@ -8,33 +8,22 @@ struct StaticMeshVertexData
|
||||
{
|
||||
return value / 65535.0f;
|
||||
}
|
||||
float3 xAxis( float4 qQuat )
|
||||
{
|
||||
float fTy = 2.0 * qQuat.y;
|
||||
float fTz = 2.0 * qQuat.z;
|
||||
float fTwy = fTy * qQuat.w;
|
||||
float fTwz = fTz * qQuat.w;
|
||||
float fTxy = fTy * qQuat.x;
|
||||
float fTxz = fTz * qQuat.x;
|
||||
float fTyy = fTy * qQuat.y;
|
||||
float fTzz = fTz * qQuat.z;
|
||||
|
||||
return float3( 1.0-(fTyy+fTzz), fTxy+fTwz, fTxz-fTwy );
|
||||
|
||||
float3x3 decodeQTangentUI32(uint v){
|
||||
float4 q = float4(((float3(int3(uint3((uint3(v) >> uint3(0u, 10u, 20u)) & uint2(0x3ffu, 0x1ffu).xxy)) - int2(512, 256).xxy)) / float2(511.0, 255.0).xxy) * 0.7071067811865475, 0.0);
|
||||
q.w = sqrt(1.0 - clamp(dot(q.xyz, q.xyz), 0.0, 1.0));
|
||||
q = normalize(float4[4](q.wxyz, q.xwyz, q.xywz, q.xyzw)[uint((v >> 30u) & 0x3u)]);
|
||||
float3 t2 = q.xyz * 2.0, tx = q.xxx * t2.xyz, ty = q.yyy * t2.xyz, tz = q.www * t2.xyz;
|
||||
float3 tangent = float3(1.0 - (ty.y + (q.z * t2.z)), tx.y + tz.z, tx.z - tz.y);
|
||||
float3 normal = float3(tx.z + tz.y, ty.z - tz.x, 1.0 - (tx.x + ty.y));
|
||||
return float3x3(tangent, cross(tangent, normal) * (((v & (1u << 29u)) != 0u) ? -1.0 : 1.0), normal);
|
||||
}
|
||||
|
||||
float3 yAxis( float4 qQuat )
|
||||
{
|
||||
float fTx = 2.0 * qQuat.x;
|
||||
float fTy = 2.0 * qQuat.y;
|
||||
float fTz = 2.0 * qQuat.z;
|
||||
float fTwx = fTx * qQuat.w;
|
||||
float fTwz = fTz * qQuat.w;
|
||||
float fTxx = fTx * qQuat.x;
|
||||
float fTxy = fTy * qQuat.x;
|
||||
float fTyz = fTz * qQuat.y;
|
||||
float fTzz = fTz * qQuat.z;
|
||||
|
||||
return float3( fTxy-fTwz, 1.0-(fTxx+fTzz), fTyz+fTwx );
|
||||
|
||||
// Decodes the UI32 encoded qtangent into a unpacked qtangent for further processing like vertex interpolation and so on
|
||||
float4 decodeQTangentUI32Raw(uint v){
|
||||
float4 q = float4(((float3(int3(uint3((uint3(v) >> uint3(0u, 10u, 20u)) & uint2(0x3ffu, 0x1ffu).xxy)) - int2(512, 256).xxy)) / float2(511.0, 255.0).xxy) * 0.7071067811865475, 0.0);
|
||||
q.w = sqrt(1.0 - clamp(dot(q.xyz, q.xyz), 0.0, 1.0));
|
||||
return normalize(float4[4](q.wxyz, q.xwyz, q.xywz, q.xyzw)[uint((v >> 30u) & 0x3u)]) * (((v & (1u << 29u)) != 0u) ? -1.0 : 1.0);
|
||||
}
|
||||
|
||||
VertexAttributes getAttributes(uint index)
|
||||
@@ -42,11 +31,7 @@ struct StaticMeshVertexData
|
||||
VertexAttributes attributes;
|
||||
attributes.position_MS = float3(positions[index * 3 + 0], positions[index * 3 + 1], positions[index * 3 + 2]);
|
||||
#ifndef POS_ONLY
|
||||
float4 qtangent = normalize(qtangents[index]);
|
||||
attributes.normal_MS = xAxis(qtangent);
|
||||
attributes.tangent_MS = yAxis(qtangent);
|
||||
float biNormalReflection = sign(qtangents[index].w);
|
||||
attributes.biTangent_MS = cross(attributes.normal_MS, attributes.tangent_MS) * biNormalReflection;
|
||||
attributes.qTangent = decodeQTangentUI32Raw(qtangents[index]);
|
||||
for(uint i = 0; i < MAX_TEXCOORDS; ++i)
|
||||
{
|
||||
attributes.texCoords[i] = float2(uint16ToFloat(texCoords[i][index * 2 + 0]), uint16ToFloat(texCoords[i][index * 2 + 1]));
|
||||
@@ -56,7 +41,7 @@ struct StaticMeshVertexData
|
||||
return attributes;
|
||||
}
|
||||
StructuredBuffer<float> positions;
|
||||
StructuredBuffer<float4> qtangents;
|
||||
StructuredBuffer<uint> qtangents;
|
||||
StructuredBuffer<uint16_t> color;
|
||||
StructuredBuffer<uint16_t> texCoords[MAX_TEXCOORDS];
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user