I am trying to port the Sky shader code to hlsl.
It works for the models but the sky does not show up (Black background even in day)
Also no lights show up.
Here is the hlsl shader code
#include "Uniforms.hlsl"
#include "Samplers.hlsl"
#include "Transform.hlsl"
#include "ScreenPos.hlsl"
#include "Lighting.hlsl"
#ifdef COMPILEPS
cbuffer CustomPS : register (b6)
{
float3 cSkyColor;
float3 cSunColor;
float3 cSunDir;
float cFogDist;
float cCamHeight;
}
#endif
void VS(
float4 iPos : POSITION,
out float2 oScreenPos : TEXCOORD0,
out float3 oFarRay : TEXCOORD1,
out float4 oPos : OUTPOSITION)
{
float4x3 modelMatrix = iModelMatrix;
float3 worldPos = GetWorldPos(modelMatrix);
oPos = GetClipPos(worldPos);
oScreenPos = GetScreenPosPreDiv(oPos);
oFarRay = GetFarRay(oPos);
}
void PS(
float2 iScreenPos : TEXCOORD0,
float3 iFarRay : TeXCOORD1,
out float4 oColor : OUTCOLOR0)
{
// oColor = float4 (1.0, 1.0, 1.0, 1.0);
// If rendering a directional light quad, optimize out the w divide
#ifdef HWDEPTH
float depth = ReconstructDepth(Sample2D (DepthBuffer, iScreenPos).r);
#else
float depth = DecodeDepth(Sample2D (DepthBuffer, iScreenPos).rgb);
#endif
float3 worldPos = iFarRay * depth;
float4 diffuseInput = Sample2D (DiffMap, iScreenPos);
float4 projWorldPos = float4 (worldPos, 1.0);
float depth2 = 1 - clamp (length(worldPos) / cFogDist, 0.0, 1.0);
float height = (worldPos.y + cCamHeight) * 0.01;
float fogFactor = clamp (exp (-height * depth2 + 0.7) * (1 - exp (depth2 * 2 - 2)), 0, 1);
float diffFactor = 1 - fogFactor;//(1-exp(-height*depth2)) * (exp(depth2*3-3));
//float skyfactor = (exp((1-depth2)*10-10));
//float skydiff = 0.5 * (normal.y + 1.0);
float3 DirRay = normalize (iFarRay);
// float layer = min(pow((1-abs(DirRay.y)),3.5),1);//*2*(1-diffFactor)-2*(1-diffFactor)
//float layer = pow(1-DirRay.y, 1-cCamHeight);
float layer = clamp (exp (((1 - DirRay.y) - 1) * (0.5 + cCamHeight * 0.001)), 0, 1);
float sunDot = max (dot (DirRay, -1 * cSunDir), 0);
float sunAmount = pow (exp ((sunDot - 1) * 5), 1 + (1 - layer) * 60); //exp(sunDot*20*(1-layer)-20*(1-layer)); //pow (sunDot, 1 + (1-layer) * 6);// * (1-diffFactor);//max (dot(DirRay ,-1 * cSunDir ),0);
//sunAmount *= 1+layer*2;
//
float3 fogColor = 1.0 * cSkyColor * layer + cSunColor * sunAmount; //mix( cSkyColor,cSunColor, sunAmount );// pow(sunAmount,8.0)
float3 result = diffuseInput.rgb * diffFactor + fogColor * fogFactor; //diffuseInput.rgb * (1-0.95*diffFactor) + fogcolor * fogFactor;
//result = pow( result ,float3( 1/2.2 ) );
oColor = float4 (result, 0.0);
// float camHeight = (500.0 - cCamHeight) / 500.0; // For testing if the cbuffer input is working
// oColor = float4 (camHeight, camHeight, camHeight, 1.0);
// oColor = float4 (fogcolor, 0.0); // FogColor is computed correctly
// oColor = float4 (depth2, depth2, depth2, 1.0);
}
Here is a screenshot for reference:
drive.google.com/file/d/0Bxa7g0 … sp=sharing
Note: The terrain in the image is using default terrain shader provided by Urho3D.
It might be a silly mistake somewhere due to my limited knowledge in hlsl (maybe depth in range [0,1] instead of [-1,1]?). Please help.