// VsLighting.hlsl // ------------------------------------------------------------------------------------------------ // DEFINTIONS // ------------------------------------------------------------------------------------------------ #define MAX_GROUND_OFFSET 2 // Max ground offset from position // ------------------------------------------------------------------------------------------------ // STRUCTURES // ------------------------------------------------------------------------------------------------ struct TangentSpaceBasis { float3 Tangent; float3 BiNormal; float3 Normal; }; #if defined(INPUT_NORMAL) && defined(INPUT_TANGENT) // ------------------------------------------------------------------------------------------------ // Name : ComputeTangentSpaceBasis // Descr. : Compute the tangent space basis for a given vertex // ------------------------------------------------------------------------------------------------ TangentSpaceBasis ComputeTangentSpaceBasis(in VSIN Input, in bool bTexTransform) { TangentSpaceBasis Basis; Basis.Normal = normalize(Input.Normal.xyz); Basis.Tangent = normalize(Input.Tangent.xyz); Basis.BiNormal = cross(Basis.Normal, Basis.Tangent); // Invert binormal if necessary Basis.BiNormal *= Input.Tangent.w; return Basis; } #endif // defined(INPUT_NORMAL) && defined(INPUT_TANGENT) #if (LIGHT_COUNT > 0) uniform float4 g_aLightColors[LIGHT_COUNT]; # if (DIR_LIGHT_COUNT > 0) uniform float4 g_aLightDirections[DIR_LIGHT_COUNT]; # endif # if (OMNI_LIGHT_COUNT > 0) uniform float4 g_aLightPositions[OMNI_LIGHT_COUNT]; uniform float4 g_aLightParams[OMNI_LIGHT_COUNT]; # endif # if (LIGHTING_TYPE == LIGHTING_VERTEX) // supports directional light and point light void ComputeVertexLighting(inout float4 diffuseColor, float3 vNormal, float3 vPosition) { # if (DIR_LIGHT_COUNT > 0) for (int i = 0; i < DIR_LIGHT_COUNT; ++i) { // directional light diffuseColor += max(dot(vNormal, g_aLightDirections[i].xyz), 0) * g_aLightColors[i]; } # endif # if (OMNI_LIGHT_COUNT > 0) for (int i = 0; i < OMNI_LIGHT_COUNT; ++i) { // point light float3 pToL = g_aLightPositions[i].xyz - vPosition; float angularAtten = max(dot(vNormal, normalize(pToL)), 0); // distanceAttenuation = saturate((Far - dist) / (Far - Near)) // konst1: Far / (Far - Near) // konst2: -1 / (Far - Near) float distanceAtten = saturate(g_aLightParams[i].x + length(pToL) * g_aLightParams[i].y); diffuseColor += angularAtten * distanceAtten * g_aLightColors[i + DIR_LIGHT_COUNT]; } # endif } # elif (LIGHTING_TYPE == LIGHTING_PIXEL && DIR_LIGHT_COUNT > 0) // only supports directional light when do lighting in tangent space void ComputePixelLighting(inout VSOUT stOutput, TangentSpaceBasis stBasis) { for (int i = 0; i < DIR_LIGHT_COUNT; ++i) { // transform direction to tangent space stOutput.LightDir[i].x = dot(stBasis.Tangent, g_aLightDirections[i].xyz); stOutput.LightDir[i].y = dot(stBasis.BiNormal, g_aLightDirections[i].xyz); stOutput.LightDir[i].z = dot(stBasis.Normal, g_aLightDirections[i].xyz); } } # endif #endif