Jelajahi Sumber

pointlight conversion rev1- filled in creatrsurface, used pre-exisitng calcs for nDotL to aproximate the light direction needed by CreateSurfaceToLight(surface, dot( lightVec, normDepth.xyz )); NOTE: compiles, but artifacts badly.

Azaezel 6 tahun lalu
induk
melakukan
7c05e3d33d

+ 29 - 64
Templates/Full/game/shaders/common/lighting/advanced/pointLightP.hlsl

@@ -136,53 +136,37 @@ uniform float2 lightAttenuation;
 uniform float3x3 viewToLightProj;
 uniform float3x3 dynamicViewToLightProj;
 
-struct PS_OUTPUT
-{
-   float4 diffuse: TORQUE_TARGET0;
-   float4 spec: TORQUE_TARGET1;
-};
+uniform float3 eyePosWorld;
+uniform float4x4 cameraToWorld;
 
-PS_OUTPUT main(ConvexConnectP IN)
-{
-   PS_OUTPUT Output = (PS_OUTPUT)0;
+LightTargetOutput main(   ConvexConnectP IN )
+{   
+   LightTargetOutput Output = (LightTargetOutput)0;
 
    // Compute scene UV
    float3 ssPos = IN.ssPos.xyz / IN.ssPos.w;
    float2 uvScene = getUVFromSSPos(ssPos, rtParams0);
 
-   // Matinfo flags
-   float4 matInfo = TORQUE_TEX2D(matInfoBuffer, uvScene);
-   //early out if emissive
-   bool emissive = getFlag(matInfo.r, 0);
-   if (emissive)
-   {
+   //sky and editor background check
+   float4 normDepth = UnpackDepthNormal(TORQUE_SAMPLER2D_MAKEARG(deferredBuffer), uvScene);
+   if (normDepth.w>0.9999)
       return Output;
-   }
-
-   float4 colorSample = TORQUE_TEX2D(colorBuffer, uvScene);
-   float3 subsurface = float3(0.0, 0.0, 0.0);
-   if (getFlag(matInfo.r, 1))
-   {
-      subsurface = colorSample.rgb;
-      if (colorSample.r > colorSample.g)
-         subsurface = float3(0.772549, 0.337255, 0.262745);
-	  else
-         subsurface = float3(0.337255, 0.772549, 0.262745);
-	}
+      
+   // Eye ray - Eye -> Pixel
+   float3 eyeRay = getDistanceVectorToPlane( -vsFarPlane.w, IN.vsEyeDir.xyz, vsFarPlane );
+   float3 viewSpacePos = eyeRay * normDepth.w;
    
-   // Sample/unpack the normal/z data
-   float4 deferredSample = TORQUE_DEFERRED_UNCONDITION(deferredBuffer, uvScene);
-   float3 normal = deferredSample.rgb;
-   float depth = deferredSample.a;
+   //create surface
+   Surface surface = CreateSurface( normDepth, TORQUE_SAMPLER2D_MAKEARG(colorBuffer),TORQUE_SAMPLER2D_MAKEARG(matInfoBuffer),
+                                    uvScene, eyePosWorld, eyeRay, cameraToWorld);   
+   //early out if emissive
+   if (getFlag(surface.matFlag, 0))
+      return Output;      
    
-   // Eye ray - Eye -> Pixel
-   float3 eyeRay = getDistanceVectorToPlane(-vsFarPlane.w, IN.vsEyeDir.xyz, vsFarPlane);
-   float3 viewSpacePos = eyeRay * depth;
-      
    // Build light vec, get length, clip pixel if needed
    float3 lightVec = lightPosition - viewSpacePos;
    float lenLightV = length(lightVec);
-   clip(lightRange - lenLightV);
+   clip(lightRange - lenLightV);   
 
    // Get the attenuated falloff.
    float atten = attenuate(lightColor, lightAttenuation, lenLightV);
@@ -191,10 +175,13 @@ PS_OUTPUT main(ConvexConnectP IN)
    // Normalize lightVec
    lightVec /= lenLightV;
    
+   
    // If we can do dynamic branching then avoid wasting
    // fillrate on pixels that are backfacing to the light.
-   float nDotL = dot( lightVec, normal );
+   float nDotL = dot( lightVec, normDepth.xyz );
    //DB_CLIP( nDotL < 0 );
+   //create surface to light
+   SurfaceToLight surfaceToLight = CreateSurfaceToLight(surface, dot( lightVec, normDepth.xyz ));
 
 #ifdef NO_SHADOW
 
@@ -255,34 +242,12 @@ PS_OUTPUT main(ConvexConnectP IN)
 
 #endif
 
-   // NOTE: Do not clip on fully shadowed pixels as it would
-   // cause the hardware occlusion query to disable the shadow.
-
-   float3 l = lightVec;// normalize(-lightDirection);
-   float3 v = eyeRay;// normalize(eyePosWorld - worldPos.xyz);
-
-   float3 h = normalize(v + l);
-   float dotNLa = clamp(dot(normal, l), 0.0, 1.0);
-   float dotNVa = clamp(dot(normal, v), 0.0, 1.0);
-   float dotNHa = clamp(dot(normal, h), 0.0, 1.0);
-   float dotHVa = clamp(dot(normal, v), 0.0, 1.0);
-   float dotLHa = clamp(dot(l, h), 0.0, 1.0);
-
-   float roughness = 1.0001-matInfo.b;
-   float metalness = matInfo.a;
-
-   //diffuse
-   float disDiff = Fr_DisneyDiffuse(dotNVa, dotNLa, dotLHa, roughness);
-   float3 diffuse = float3(disDiff, disDiff, disDiff) / M_PI_F;
-   //specular
-   float3 specular = directSpecular(normal, v, l, roughness, 1.0) * lightColor.rgb;
-
-   
-   if (nDotL<0) shadowed = 0;
-   float Sat_NL_Att = saturate( nDotL * shadowed ) * lightBrightness;
+   //get directional light contribution   
+   LightResult result = GetDirectionalLight(surface, surfaceToLight, lightColor.rgb, lightBrightness, shadowed);
+      
    //output
-   Output.diffuse = float4(diffuse * lightBrightness*Sat_NL_Att*shadowed,1.0);
-   Output.spec = float4(specular * lightBrightness*Sat_NL_Att*shadowed,1.0);
-
+   Output.diffuse = float4(result.diffuse, 0);
+   Output.spec = float4(result.spec, 0);
+   
    return Output;
 }