|
@@ -21,6 +21,8 @@
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
#include "./torque.hlsl"
|
|
#include "./torque.hlsl"
|
|
|
|
+#include "./brdf.hlsl"
|
|
|
|
+#include "./shaderModelAutoGen.hlsl"
|
|
|
|
|
|
#ifndef TORQUE_SHADERGEN
|
|
#ifndef TORQUE_SHADERGEN
|
|
|
|
|
|
@@ -37,141 +39,20 @@ uniform float4 inLightColor[4];
|
|
#endif
|
|
#endif
|
|
|
|
|
|
uniform float4 ambient;
|
|
uniform float4 ambient;
|
|
-#define ambientCameraFactor 0.3
|
|
|
|
uniform float smoothness;
|
|
uniform float smoothness;
|
|
uniform float metalness;
|
|
uniform float metalness;
|
|
uniform float4 albedo;
|
|
uniform float4 albedo;
|
|
|
|
|
|
#endif // !TORQUE_SHADERGEN
|
|
#endif // !TORQUE_SHADERGEN
|
|
|
|
|
|
-
|
|
|
|
-float3 F_schlick( in float3 f0, in float3 f90, in float u )
|
|
|
|
-{
|
|
|
|
- //
|
|
|
|
- // F( v, h ) = F0 + ( 1.0 - F0 ) * pow( 1.0f - HdotV, 5.0f )
|
|
|
|
- //
|
|
|
|
- //
|
|
|
|
- // where
|
|
|
|
- //
|
|
|
|
- // F0 = BaseColor * nDotL
|
|
|
|
- //
|
|
|
|
- // Dielectric materials always have a range of 0.02 < F0 < 0.05 , use a stock value of 0.04 ( roughly plastics )
|
|
|
|
- //
|
|
|
|
-
|
|
|
|
- return f0 + ( f90 - f0 ) * pow( 1.0f - u , 5.0f );
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-float Fr_DisneyDiffuse ( float NdotV , float NdotL , float LdotH , float linearRoughness )
|
|
|
|
|
|
+//deferred lighting output
|
|
|
|
+struct LightTargetOutput
|
|
{
|
|
{
|
|
- float energyBias = lerp (0 , 0.5 , linearRoughness );
|
|
|
|
- float energyFactor = lerp (1.0 , 1.0 / 1.51 , linearRoughness );
|
|
|
|
- float fd90 = energyBias + 2.0 * LdotH * LdotH * linearRoughness ;
|
|
|
|
- float3 f0 = float3 ( 1.0f , 1.0f , 1.0f );
|
|
|
|
- float lightScatter = F_schlick( f0 , fd90 , NdotL ).r;
|
|
|
|
- float viewScatter = F_schlick(f0 , fd90 , NdotV ).r;
|
|
|
|
-
|
|
|
|
- return lightScatter * viewScatter * energyFactor;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-float SmithGGX( float NdotL, float NdotV, float alpha )
|
|
|
|
-{
|
|
|
|
- //
|
|
|
|
- // G( L, V, h ) = G( L ) G( V )
|
|
|
|
- //
|
|
|
|
- // nDotL
|
|
|
|
- // G( L ) = _________________________
|
|
|
|
- // nDotL ( 1 - k ) + k
|
|
|
|
- //
|
|
|
|
- //
|
|
|
|
- // NdotV
|
|
|
|
- // G( V ) = _________________________
|
|
|
|
- // NdotV ( 1 - k ) + k
|
|
|
|
- //
|
|
|
|
- //
|
|
|
|
- // pow( ( Roughness + 1 ), 2)
|
|
|
|
- // , Where k = __________________________ ( unreal 4 )
|
|
|
|
- // 8
|
|
|
|
- //
|
|
|
|
-
|
|
|
|
- float alphaSqr = alpha * alpha;
|
|
|
|
-
|
|
|
|
- //float GGX_V = NdotL * sqrt ( ( - NdotV * alphaSqr + NdotV ) * NdotV + alphaSqr );
|
|
|
|
- //float GGX_L = NdotV * sqrt ( ( - NdotL * alphaSqr + NdotL ) * NdotL + alphaSqr );
|
|
|
|
-
|
|
|
|
- float GGX_V = NdotL + sqrt ( ( - NdotV * alphaSqr + NdotV ) * NdotV + alphaSqr );
|
|
|
|
- float GGX_L = NdotV + sqrt ( ( - NdotL * alphaSqr + NdotL ) * NdotL + alphaSqr );
|
|
|
|
-
|
|
|
|
- return rcp( GGX_V + GGX_L );
|
|
|
|
- //return 0.5f / ( GGX_V + GGX_L );
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-float D_GGX( float NdotH , float alpha )
|
|
|
|
-{
|
|
|
|
- //
|
|
|
|
- // or GGX ( disney / unreal 4 )
|
|
|
|
- //
|
|
|
|
- // alpha = pow( roughness, 2 );
|
|
|
|
- //
|
|
|
|
- // pow( alpha, 2 )
|
|
|
|
- // D( h ) = ________________________________________________________________
|
|
|
|
- // PI pow( pow( NdotH , 2 ) ( pow( alpha, 2 ) - 1 ) + 1 ), 2 )
|
|
|
|
- //
|
|
|
|
-
|
|
|
|
- float alphaSqr = alpha*alpha;
|
|
|
|
- float f = ( NdotH * alphaSqr - NdotH ) * NdotH + 1;
|
|
|
|
- return alphaSqr / ( M_PI_F * (f * f) );
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-float4 EvalBDRF( float3 baseColor, float3 lightColor, float3 toLight, float3 position, float3 normal, float roughness, float metallic )
|
|
|
|
-{
|
|
|
|
- //
|
|
|
|
- // Microfacet Specular Cook-Torrance
|
|
|
|
- //
|
|
|
|
- // D( h ) F( v, h ) G( l, v, h )
|
|
|
|
- // f( l, v ) = ____________________________
|
|
|
|
- // 4 ( dot( n, l ) dot( n, v )
|
|
|
|
- //
|
|
|
|
- //
|
|
|
|
-
|
|
|
|
- float3 L = normalize( toLight );
|
|
|
|
- float3 V = normalize( -position );
|
|
|
|
- float3 H = normalize( L + V );
|
|
|
|
- float3 N = normal;
|
|
|
|
-
|
|
|
|
- float NdotV = abs( dot( N, V ) ) + 1e-5f;
|
|
|
|
- float NdotH = saturate( dot( N, H ) );
|
|
|
|
- float NdotL = saturate( dot( N, L ) );
|
|
|
|
- float LdotH = saturate( dot( L, H ) );
|
|
|
|
-
|
|
|
|
- float VdotH = saturate( dot( V, H ) );
|
|
|
|
-
|
|
|
|
- if ( NdotL == 0 )
|
|
|
|
- return float4( 0.0f, 0.0f, 0.0f, 0.0f );
|
|
|
|
-
|
|
|
|
- float alpha = roughness;
|
|
|
|
- float visLinAlpha = alpha * alpha;
|
|
|
|
-
|
|
|
|
- float3 f0 = baseColor;
|
|
|
|
- float metal = metallic;
|
|
|
|
-
|
|
|
|
- float3 F_conductor= F_schlick( f0, float3( 1.0, 1.0, 1.0 ), VdotH );
|
|
|
|
- float3 F_dielec = F_schlick( float3( 0.04, 0.04, 0.04 ), float3( 1.0, 1.0, 1.0 ), VdotH );
|
|
|
|
- float Vis = SmithGGX( NdotL, NdotV, visLinAlpha );
|
|
|
|
- float D = D_GGX( NdotH, visLinAlpha );
|
|
|
|
-
|
|
|
|
- float3 Fr_dielec = D * F_dielec * Vis;
|
|
|
|
- float3 Fr_conductor = D * F_conductor * Vis;
|
|
|
|
-
|
|
|
|
- float3 Fd = Fr_DisneyDiffuse( NdotV , NdotL , LdotH , visLinAlpha ) / M_PI_F ;
|
|
|
|
- float3 specular = ( 1.0f - metal ) * Fr_dielec + metal * Fr_conductor;
|
|
|
|
- float3 diffuse = ( 1.0f - metal ) * Fd * f0;
|
|
|
|
-
|
|
|
|
- float3 ret = ( diffuse + specular + lightColor) * NdotL;
|
|
|
|
-
|
|
|
|
- float FR = saturate(length(specular));
|
|
|
|
- return float4(ret,FR);
|
|
|
|
-}
|
|
|
|
|
|
+ float4 spec: TORQUE_TARGET0;
|
|
|
|
+ float4 diffuse: TORQUE_TARGET1;
|
|
|
|
+};
|
|
|
|
|
|
|
|
+//TODO fix compute 4 lights
|
|
void compute4Lights( float3 wsView,
|
|
void compute4Lights( float3 wsView,
|
|
float3 wsPosition,
|
|
float3 wsPosition,
|
|
float3 wsNormal,
|
|
float3 wsNormal,
|
|
@@ -194,115 +75,121 @@ void compute4Lights( float3 wsView,
|
|
out float4 outDiffuse,
|
|
out float4 outDiffuse,
|
|
out float4 outSpecular )
|
|
out float4 outSpecular )
|
|
{
|
|
{
|
|
- // NOTE: The light positions and spotlight directions
|
|
|
|
- // are stored in SoA order, so inLightPos[0] is the
|
|
|
|
- // x coord for all 4 lights... inLightPos[1] is y... etc.
|
|
|
|
- //
|
|
|
|
- // This is the key to fully utilizing the vector units and
|
|
|
|
- // saving a huge amount of instructions.
|
|
|
|
- //
|
|
|
|
- // For example this change saved more than 10 instructions
|
|
|
|
- // over a simple for loop for each light.
|
|
|
|
-
|
|
|
|
- int i;
|
|
|
|
-
|
|
|
|
- float4 lightVectors[3];
|
|
|
|
- for ( i = 0; i < 3; i++ )
|
|
|
|
- lightVectors[i] = wsPosition[i] - inLightPos[i];
|
|
|
|
-
|
|
|
|
- // Accumulate the dot product between the light
|
|
|
|
- // vector and the normal.
|
|
|
|
- //
|
|
|
|
- // The normal is negated because it faces away from
|
|
|
|
- // the surface and the light faces towards the
|
|
|
|
- // surface... this keeps us from needing to flip
|
|
|
|
- // the light vector direction which complicates
|
|
|
|
- // the spot light calculations.
|
|
|
|
- //
|
|
|
|
- // We normalize the result a little later.
|
|
|
|
- //
|
|
|
|
- float4 nDotL = 0;
|
|
|
|
- for ( i = 0; i < 3; i++ )
|
|
|
|
- nDotL += lightVectors[i] * -wsNormal[i];
|
|
|
|
-
|
|
|
|
- float4 squareDists = 0;
|
|
|
|
- for ( i = 0; i < 3; i++ )
|
|
|
|
- squareDists += lightVectors[i] * lightVectors[i];
|
|
|
|
- half4 correction = (half4)rsqrt( squareDists );
|
|
|
|
- nDotL = saturate( nDotL * correction );
|
|
|
|
-
|
|
|
|
- // First calculate a simple point light linear
|
|
|
|
- // attenuation factor.
|
|
|
|
- //
|
|
|
|
- // If this is a directional light the inverse
|
|
|
|
- // radius should be greater than the distance
|
|
|
|
- // causing the attenuation to have no affect.
|
|
|
|
- //
|
|
|
|
- float4 atten = saturate( 1.0 - ( squareDists * inLightInvRadiusSq ) );
|
|
|
|
|
|
|
|
- #ifndef TORQUE_BL_NOSPOTLIGHT
|
|
|
|
-
|
|
|
|
- // The spotlight attenuation factor. This is really
|
|
|
|
- // fast for what it does... 6 instructions for 4 spots.
|
|
|
|
-
|
|
|
|
- float4 spotAtten = 0;
|
|
|
|
- for ( i = 0; i < 3; i++ )
|
|
|
|
- spotAtten += lightVectors[i] * inLightSpotDir[i];
|
|
|
|
|
|
+ outDiffuse = float4(0,0,0,0);
|
|
|
|
+ outSpecular = float4(0,0,0,0);
|
|
|
|
+}
|
|
|
|
|
|
- float4 cosAngle = ( spotAtten * correction ) - inLightSpotAngle;
|
|
|
|
- atten *= saturate( cosAngle * inLightSpotFalloff );
|
|
|
|
|
|
+struct Surface
|
|
|
|
+{
|
|
|
|
+ float3 P; // world space position
|
|
|
|
+ float3 N; // world space normal
|
|
|
|
+ float3 V; // world space view vector
|
|
|
|
+ float4 baseColor; // base color [0 -> 1] (rgba)
|
|
|
|
+ float metalness; // metalness [0:dielectric -> 1:metal]
|
|
|
|
+ float roughness; // roughness: [0:smooth -> 1:rough] (linear)
|
|
|
|
+ float roughness_brdf; // roughness remapped from linear to BRDF
|
|
|
|
+ float depth; // depth: [0:near -> 1:far] (linear)
|
|
|
|
+ float ao; // ambient occlusion [0 -> 1]
|
|
|
|
+ float matFlag; // material flag - use getFlag to retreive
|
|
|
|
+
|
|
|
|
+ float NdotV; // cos(angle between normal and view vector)
|
|
|
|
+ float3 f0; // fresnel value (rgb)
|
|
|
|
+ float3 albedo; // diffuse light absorbtion value (rgb)
|
|
|
|
+ float3 R; // reflection vector
|
|
|
|
+ float3 F; // fresnel term computed from f0, N and V
|
|
|
|
+
|
|
|
|
+ inline void Update()
|
|
|
|
+ {
|
|
|
|
+ NdotV = abs(dot(N, V)) + 1e-5f; // avoid artifact
|
|
|
|
+
|
|
|
|
+ albedo = baseColor.rgb * (1.0 - metalness);
|
|
|
|
+ f0 = lerp(float3_splat(0.04), baseColor.rgb, metalness);
|
|
|
|
+
|
|
|
|
+ R = -reflect(V, N);
|
|
|
|
+ float f90 = saturate(50.0 * dot(f0, 0.33));
|
|
|
|
+ F = F_Schlick(f0, f90, NdotV);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+inline Surface CreateSurface(TORQUE_SAMPLER2D(gbufferTex0), TORQUE_SAMPLER2D(gbufferTex1), TORQUE_SAMPLER2D(gbufferTex2), in float2 uv, in float3 wsEyePos, in float3 wsEyeRay, in float4x4 invView)
|
|
|
|
+{
|
|
|
|
+ Surface surface;
|
|
|
|
+
|
|
|
|
+ float4 gbuffer0 = UnpackDepthNormal(TORQUE_SAMPLER2D_MAKEARG(gbufferTex0), uv);
|
|
|
|
+ float4 gbuffer1 = TORQUE_TEX2DLOD(gbufferTex1, float4(uv,0,0));
|
|
|
|
+ float4 gbuffer2 = TORQUE_TEX2DLOD(gbufferTex2, float4(uv,0,0));
|
|
|
|
+
|
|
|
|
+ surface.depth = gbuffer0.a;
|
|
|
|
+ surface.P = wsEyePos + wsEyeRay * surface.depth;
|
|
|
|
+ surface.N = mul(invView, float4(gbuffer0.xyz,0)).xyz;
|
|
|
|
+ surface.V = normalize(wsEyePos - surface.P);
|
|
|
|
+ surface.baseColor = gbuffer1;
|
|
|
|
+ const float minRoughness=1e-4;
|
|
|
|
+ surface.roughness = clamp(1.0 - gbuffer2.b, minRoughness, 1.0); //torque uses smoothness so we convert to roughness. Should really clamp during gbuffer pass though
|
|
|
|
+ surface.roughness_brdf = surface.roughness * surface.roughness;
|
|
|
|
+ surface.metalness = gbuffer2.a;
|
|
|
|
+ surface.ao = gbuffer2.g;
|
|
|
|
+ surface.matFlag = gbuffer2.r;
|
|
|
|
+
|
|
|
|
+ surface.Update();
|
|
|
|
+ return surface;
|
|
|
|
+}
|
|
|
|
|
|
- #endif
|
|
|
|
-
|
|
|
|
- // Get the final light intensity.
|
|
|
|
- float4 intensity = nDotL * atten;
|
|
|
|
|
|
+struct SurfaceToLight
|
|
|
|
+{
|
|
|
|
+ float3 L; // surface to light vector
|
|
|
|
+ float3 H; // half-vector between view vector and light vector
|
|
|
|
+ float NdotL; // cos(angle between N and L)
|
|
|
|
+ float HdotV; // cos(angle between H and V) = HdotL = cos(angle between H and L)
|
|
|
|
+ float NdotH; // cos(angle between N and H)
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+inline SurfaceToLight CreateSurfaceToLight(in Surface surface, in float3 L)
|
|
|
|
+{
|
|
|
|
+ SurfaceToLight surfaceToLight;
|
|
|
|
|
|
- // Combine the light colors for output.
|
|
|
|
- float4 lightColor = 0;
|
|
|
|
- for ( i = 0; i < 4; i++ )
|
|
|
|
- lightColor += intensity[i] * inLightColor[i];
|
|
|
|
-
|
|
|
|
- float3 toLight = 0;
|
|
|
|
- for ( i = 0; i < 3; i++ )
|
|
|
|
- toLight += lightVectors[i].rgb;
|
|
|
|
|
|
+ surfaceToLight.L = normalize(L);
|
|
|
|
+ surfaceToLight.H = normalize(surface.V + surfaceToLight.L);
|
|
|
|
+ surfaceToLight.NdotL = saturate(dot(surfaceToLight.L, surface.N));
|
|
|
|
+ surfaceToLight.HdotV = saturate(dot(surfaceToLight.H, surface.V));
|
|
|
|
+ surfaceToLight.NdotH = saturate(dot(surfaceToLight.H, surface.N));
|
|
|
|
|
|
- outDiffuse = float4(albedo.rgb*(1.0-metalness),albedo.a);
|
|
|
|
- outSpecular = EvalBDRF( float3( 1.0, 1.0, 1.0 ), lightColor.rgb, toLight, wsPosition, wsNormal, smoothness, metalness );
|
|
|
|
|
|
+ return surfaceToLight;
|
|
}
|
|
}
|
|
|
|
|
|
-float G1V(float dotNV, float k)
|
|
|
|
|
|
+float3 BRDF_GetSpecular(in Surface surface, in SurfaceToLight surfaceToLight)
|
|
{
|
|
{
|
|
- return 1.0f/(dotNV*(1.0f-k)+k);
|
|
|
|
|
|
+ float f90 = saturate(50.0 * dot(surface.f0, 0.33));
|
|
|
|
+ float3 F = F_Schlick(surface.f0, f90, surfaceToLight.HdotV);
|
|
|
|
+ float Vis = V_SmithGGXCorrelated(surface.NdotV, surfaceToLight.NdotL, surface.roughness_brdf);
|
|
|
|
+ float D = D_GGX(surfaceToLight.NdotH, surface.roughness_brdf);
|
|
|
|
+ float3 Fr = D * F * Vis / M_PI_F;
|
|
|
|
+ return Fr;
|
|
}
|
|
}
|
|
|
|
|
|
-float3 directSpecular(float3 N, float3 V, float3 L, float roughness, float F0)
|
|
|
|
|
|
+float BRDF_GetDiffuse(in Surface surface, in SurfaceToLight surfaceToLight)
|
|
{
|
|
{
|
|
- float alpha = roughness*roughness;
|
|
|
|
-
|
|
|
|
- //TODO don't need to calculate all this again timmy!!!!!!
|
|
|
|
- float3 H = normalize(V + L);
|
|
|
|
- float dotNL = clamp(dot(N,L), 0.0, 1.0);
|
|
|
|
- float dotNV = clamp(dot(N,V), 0.0, 1.0);
|
|
|
|
- float dotNH = clamp(dot(N,H), 0.0, 1.0);
|
|
|
|
- float dotHV = clamp(dot(H,V), 0.0, 1.0);
|
|
|
|
- float dotLH = clamp(dot(L,H), 0.0, 1.0);
|
|
|
|
-
|
|
|
|
- float F, D, vis;
|
|
|
|
|
|
+ //getting some banding with disney method, using lambert instead - todo do some futher testing
|
|
|
|
+ float Fd = 1.0 / M_PI_F;//Fr_DisneyDiffuse(surface.NdotV, surfaceToLight.NdotL, surfaceToLight.HdotV, surface.roughness) / M_PI_F;
|
|
|
|
+ return Fd;
|
|
|
|
+}
|
|
|
|
|
|
- // D
|
|
|
|
- float alphaSqr = alpha*alpha;
|
|
|
|
- float pi = 3.14159f;
|
|
|
|
- float denom = dotNH * dotNH *(alphaSqr-1.0) + 1.0f;
|
|
|
|
- D = alphaSqr/(pi * denom * denom);
|
|
|
|
|
|
+struct LightResult
|
|
|
|
+{
|
|
|
|
+ float3 diffuse;
|
|
|
|
+ float3 spec;
|
|
|
|
+};
|
|
|
|
|
|
- // F
|
|
|
|
- float dotLH5 = pow(1.0f-dotLH,5);
|
|
|
|
- F = F0 + (1.0-F0)*(dotLH5);
|
|
|
|
|
|
+inline LightResult GetDirectionalLight(in Surface surface, in SurfaceToLight surfaceToLight, float3 lightColor, float lightIntensity, float shadow)
|
|
|
|
+{
|
|
|
|
+ LightResult result;
|
|
|
|
+ float3 factor = lightColor * max(surfaceToLight.NdotL, 0) * shadow * lightIntensity;
|
|
|
|
+ result.diffuse = BRDF_GetDiffuse(surface,surfaceToLight) * factor;
|
|
|
|
+ result.spec = BRDF_GetSpecular(surface,surfaceToLight) * factor;
|
|
|
|
|
|
- // V
|
|
|
|
- float k = alpha/2.0f;
|
|
|
|
- vis = G1V(dotNL,k)*G1V(dotNV,k);
|
|
|
|
|
|
+ result.diffuse = max(0.0f, result.diffuse);
|
|
|
|
+ result.spec = max(0.0f, result.spec);
|
|
|
|
|
|
- float specular = dotNL * D * F * vis;
|
|
|
|
- return float3(specular,specular,specular);
|
|
|
|
-}
|
|
|
|
|
|
+ return result;
|
|
|
|
+}
|