Skip to content

Instantly share code, notes, and snippets.

@bazhenovc
Created September 15, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bazhenovc/c07a109163c9ba79d619 to your computer and use it in GitHub Desktop.
Save bazhenovc/c07a109163c9ba79d619 to your computer and use it in GitHub Desktop.
#include "shaders/common/torque.hlsl"
struct VS_OUTPUT
{
float4 hpos : SV_POSITION;
float2 uv : TEXCOORD0;
};
struct PS_OUTPUT
{
float4 color0 : SV_Target0;
};
cbuffer cbDefault : register(b0)
{
float4x4 matProjInv;
float4x4 matViewInv;
float4x4 lightViewOnly;
float4x4 lightMatProj;
float2 targetSize;
float4 lightColor;
float4 lightAmbient;
float3 lightDirection;
float lightBrightness;
float3 eyePosWorld;
float3 rimColor;
float3 specularColor;
};
#ifdef TORQUE_VS
VS_OUTPUT vs_main(uint id: SV_VertexID)
{
VS_OUTPUT OUT;
OUT.uv = float2((id << 1) & 2, id & 2);
OUT.hpos = float4(mad(OUT.uv, float2(2, -2), float2(-1, 1)), 0, 1);
return OUT;
}
#endif
#ifdef TORQUE_PS
Texture2D prepassTex : register(t0);
Texture2D matInfoTex : register(t1);
Texture2D depthTex : register(t2);
Texture2D colorTex : register(t3);
Texture2D sunDepthMap : register( t4 );
StructuredBuffer<float2> warpMaps : register( t5 );
float getShadowTerm(float2 texCoord, float depth, float distToEye)
{
//--------------------------------------------------------------
// SHADOWS
#ifdef NO_SHADOWS
return 1;
#else
//reconstruction
float2 cspos = float2(texCoord.x * 2 - 1, (1-texCoord.y) * 2 - 1);
float4 depthCoord = float4(cspos.xy, depth, 1);
depthCoord = mul(matProjInv, depthCoord).xzyw;
depthCoord = mul(matViewInv, depthCoord);
depthCoord /= depthCoord.w;
//projection
float4 rpos = mul(lightViewOnly, depthCoord).xzyw;
rpos = mul(lightMatProj, rpos);
rpos /= rpos.w;
float2 warpStrength = float2(0,0);
#ifndef NO_WARPING
// per-pixel RTW - do warping
warpStrength = CalcShadowWarpsRecv( warpMaps, rpos.xy, RTW_WIDTH );
rpos.xy += warpStrength;
#endif
float shadow = 0;
float2 depthTexCoord = mad(rpos.xy, 0.5, 0.5);
float projDepth = saturate(rpos.z);
float2 radius = lerp(
float2(0.000001,0.00001),
float2(0.0005,0.0005),
saturate(warpStrength)
);
#ifdef TORQUE_GROUND_COVER
shadow = pcfFilterNo(sunDepthMap, depthTexCoord, projDepth);
#else
//TODO:: add soft shadows
shadow = pcfFilterNo(sunDepthMap, depthTexCoord, projDepth);
#endif
//TODO: add fading!
//float fadeCoord = saturate((distToEye - 200) / 200);
//shadow = saturate(max(shadow, fadeCoord) + 0.05);
return shadow;
#endif
}
PS_OUTPUT ps_main(VS_OUTPUT IN)
{
static const float PI = 3.14159265f;
static const float PI4 = PI / 4.0;
static const float PI2 = PI / 2.0;
PS_OUTPUT OUT;
float2 ssc = IN.hpos.xy / targetSize.xy;
float depth = depthTex.SampleLevel( samplerDefault, ssc, 0 ).r;
float4 colorSample = colorTex.Sample(samplerDefault, ssc);
float4 prepassSample = prepassTex.Sample(samplerDefault, ssc);
float4 matInfoSample = matInfoTex.Sample(samplerDefault, ssc);
float3 pos = reconstructPos(depth, ssc, matProjInv).xyz;
float distToEye = pos.z;
float3 eyeDir = -mul(matViewInv, float4(normalize(pos.xzy), 1.0)).xzy; //now it's eye dir
float3 V = normalize(eyeDir);
float3 N = unpackNormal(prepassSample.xy);
float3 L = -lightDirection.xzy;
float3 H = normalize(L + V);
float shadow = getShadowTerm(ssc, depth, distToEye);
float Rim = matInfoSample.a;
float dotNL = saturate(dot(N, L));
float dotNH = saturate(dot(N, H));
float dotLH = saturate(dot(L, H));
float dotNV = saturate(dot(N, V));
float3 lightAmount = lightAmbient.rgb + lightColor.rgb * min(dotNL, shadow);
float3 lightTerm = lightAmount * lightBrightness + Rim * rimColor;
float R = matInfoSample.y;
float S = exp(matInfoSample.x * R * 16.0 + 1.0);
float3 Cdiff = colorSample.rgb * lightTerm;
float3 Cspec = specularColor * shadow;
float3 Fschlick = Cspec + (max(R.xxx, Cspec) - Cspec) * pow(1.0 - dotNV, 5.0);
float Fnorm = ((S + 2.0) / 8.0) * pow(dotNH, S) * dotNL;
OUT.color0.rgb = Cdiff + Fschlick * Fnorm * lightColor.rgb;
//OUT.color0.rgb = float3(R, R, R);
OUT.color0.a = 1;
return OUT;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment