|
@@ -1,11 +1,30 @@
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
+// Copyright (c) 2012 GarageGames, LLC
|
|
|
+//
|
|
|
+// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
+// of this software and associated documentation files (the "Software"), to
|
|
|
+// deal in the Software without restriction, including without limitation the
|
|
|
+// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
+// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
+// furnished to do so, subject to the following conditions:
|
|
|
+//
|
|
|
+// The above copyright notice and this permission notice shall be included in
|
|
|
+// all copies or substantial portions of the Software.
|
|
|
+//
|
|
|
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
+// IN THE SOFTWARE.
|
|
|
+//-----------------------------------------------------------------------------
|
|
|
#include "../../../gl/hlslCompat.glsl"
|
|
|
#include "shadergen:/autogenConditioners.h"
|
|
|
-
|
|
|
#include "farFrustumQuad.glsl"
|
|
|
-#include "lightingUtils.glsl"
|
|
|
#include "../../../gl/lighting.glsl"
|
|
|
#include "../../../gl/torque.glsl"
|
|
|
-#line 8
|
|
|
+#line 27
|
|
|
|
|
|
in vec4 pos;
|
|
|
in vec4 wsEyeDir;
|
|
@@ -13,75 +32,51 @@ in vec4 ssPos;
|
|
|
in vec4 vsEyeDir;
|
|
|
|
|
|
uniform sampler2D deferredBuffer;
|
|
|
+uniform sampler2D colorBuffer;
|
|
|
uniform sampler2D matInfoBuffer;
|
|
|
uniform samplerCube cubeMap;
|
|
|
uniform samplerCube irradianceCubemap;
|
|
|
uniform sampler2D BRDFTexture;
|
|
|
-uniform float cubeMips;
|
|
|
-
|
|
|
|
|
|
uniform vec4 rtParams0;
|
|
|
|
|
|
uniform vec4 vsFarPlane;
|
|
|
-
|
|
|
-uniform float4x4 invViewMat;
|
|
|
-
|
|
|
+uniform mat4 cameraToWorld;
|
|
|
uniform vec3 eyePosWorld;
|
|
|
|
|
|
-vec3 iblSpecular(vec3 v, vec3 n, float roughness)
|
|
|
+vec3 iblSpecular(in Surface surface, vec3 F)
|
|
|
{
|
|
|
- vec3 R = reflect(v, n);
|
|
|
const float MAX_REFLECTION_LOD = 4.0;
|
|
|
- vec3 prefilteredColor = TORQUE_TEXCUBELOD(cubeMap, vec4(R, roughness * MAX_REFLECTION_LOD)).rgb;
|
|
|
- vec2 envBRDF = TORQUE_TEX2D(BRDFTexture, vec2(max(dot(n, v), 0.0), roughness)).rg;
|
|
|
- return prefilteredColor * (envBRDF.x + envBRDF.y);
|
|
|
- //return prefilteredColor;
|
|
|
+ vec3 prefilteredColor = textureLod(cubeMap, surface.R, surface.roughness * MAX_REFLECTION_LOD).rgb;
|
|
|
+ vec2 envBRDF = texture(BRDFTexture, vec2(surface.NdotV, surface.roughness)).rg;
|
|
|
+ return prefilteredColor * (F * envBRDF.x + envBRDF.y);
|
|
|
}
|
|
|
|
|
|
out vec4 OUT_col;
|
|
|
-out vec4 OUT_col1;
|
|
|
-
|
|
|
void main()
|
|
|
{
|
|
|
// Compute scene UV
|
|
|
- vec3 ssPos = IN.ssPos.xyz / IN.ssPos.w;
|
|
|
-
|
|
|
- //vec4 hardCodedRTParams0 = vec4(0,0.0277777780,1,0.972222209);
|
|
|
- vec2 uvScene = getUVFromSSPos( ssPos, rtParams0 );
|
|
|
-
|
|
|
- // Matinfo flags
|
|
|
- vec4 matInfo = TORQUE_TEX2D( matInfoBuffer, uvScene );
|
|
|
-
|
|
|
- // Sample/unpack the normal/z data
|
|
|
- vec4 deferredSample = TORQUE_DEFERRED_UNCONDITION( deferredBuffer, uvScene );
|
|
|
- vec3 normal = deferredSample.rgb;
|
|
|
- float depth = deferredSample.a;
|
|
|
- if (depth>0.9999)
|
|
|
- return Output;
|
|
|
-
|
|
|
- // Need world-space normal.
|
|
|
- vec3 wsNormal = mul(vec4(normal, 1), invViewMat).rgb;
|
|
|
-
|
|
|
- vec3 eyeRay = getDistanceVectorToPlane( -vsFarPlane.w, IN.vsEyeDir.xyz, vsFarPlane );
|
|
|
-
|
|
|
- vec3 wsEyeRay = mul(vec4(eyeRay, 1), invViewMat).rgb;
|
|
|
-
|
|
|
- // Use eye ray to get ws pos
|
|
|
- vec3 worldPos = vec3(eyePosWorld + wsEyeRay * depth);
|
|
|
-
|
|
|
- vec3 reflectionVec = reflect(IN.wsEyeDir, vec4(wsNormal,1)).xyz;
|
|
|
-
|
|
|
- float roughness = 1 - matInfo.b;
|
|
|
-
|
|
|
- vec3 v = normalize(eyePosWorld - worldPos);
|
|
|
-
|
|
|
- vec3 irradiance = TORQUE_TEXCUBE(irradianceCubemap, wsNormal).rgb;
|
|
|
-
|
|
|
- vec3 specular = iblSpecular(wsEyeRay, wsNormal, roughness);
|
|
|
-
|
|
|
-
|
|
|
- OUT_col = vec4(irradiance.rgb, 1);
|
|
|
- OUT_col1 = vec4(specular.rgb, 1);
|
|
|
- OUT_col *= matInfo.g;
|
|
|
- OUT_col1 *= matInfo.g;
|
|
|
+ vec2 uvScene = getUVFromSSPos( ssPos.xyz/ssPos.w, rtParams0 );
|
|
|
+
|
|
|
+ //eye ray WS/LS
|
|
|
+ vec3 vsEyeRay = getDistanceVectorToPlane( -vsFarPlane.w, vsEyeDir.xyz, vsFarPlane );
|
|
|
+ vec3 wsEyeRay = tMul(cameraToWorld, vec4(vsEyeRay, 0)).xyz;
|
|
|
+
|
|
|
+ //unpack normal and linear depth
|
|
|
+ vec4 normDepth = deferredUncondition(deferredBuffer, uvScene);
|
|
|
+
|
|
|
+ //create surface
|
|
|
+ Surface surface = createSurface( normDepth, colorBuffer, matInfoBuffer,
|
|
|
+ uvScene, eyePosWorld, wsEyeRay, cameraToWorld);
|
|
|
+
|
|
|
+ vec3 F = FresnelSchlickRoughness(surface.NdotV, surface.f0, surface.roughness);
|
|
|
+ vec3 irradiance = textureLod(irradianceCubemap, surface.N,0).rgb;
|
|
|
+ vec3 specular = iblSpecular(surface, F);
|
|
|
+ //energy conservation
|
|
|
+ vec3 kD = vec3(1.0) - F;
|
|
|
+ kD *= 1.0 - surface.metalness;
|
|
|
+ //final diffuse color
|
|
|
+ vec3 diffuse = kD * irradiance * surface.baseColor.rgb;
|
|
|
+
|
|
|
+ OUT_col = vec4(diffuse + specular * surface.ao, 0);
|
|
|
}
|