Hi Chingwa,
Thank you for your reply,
Did you have a chance to look at the code? I am still trying to make Tenkoku compatible with LuxWater, but I am not good with shaders:(
Could you please advise me what part of the code is wrong?
Should I call TenkokuFogCore.cginc from LuxWaterCore.cginc with some parameters? Now I use
c = ComputeFog ();
but I get the following error: Shader error in 'Lux Water/WaterSurface': unrecognized identifier 'appdata_img' at SKY/SHADERS/TenkokuFogCore.cginc(60) (on d3d11)
Here is TenkokuFogCore.cginc that is based on Tenkoku_FX_Fog.shader:
uniform sampler2D _MainTex, _SkyTex;
uniform sampler2D_float _CameraDepthTexture;
sampler2D _CameraDepthNormalsTexture;
uniform float4 _HeightParams;
// x = start distance
uniform float4 _DistanceParams;
int4 _SceneFogMode;
float4 _SceneFogParams;
float _Tenkoku_FogStart;
float _Tenkoku_FogEnd;
uniform float4 _MainTex_TexelSize;
// for fast world space reconstruction
uniform float4x4 _FrustumCornersWS;
uniform float4 _CameraWS;
half4 _Tenkoku_FogColor;
float _fogSkybox;
float _fogHorizon;
float _FogStart;
float _FogDistance;
float _camDistance;
float _Tenkoku_AmbientGI;
float _Tenkoku_AtmosphereDensity;
float _Tenkoku_FogDensity;
float _tenkokufogFull;
float4 Tenkoku_Vec_SunFwd;
float4 Tenkoku_Vec_MoonFwd;
float4 Tenkoku_Vec_LightningFwd;
float4x4 _Tenkoku_CameraMV;
samplerCUBE _Tenkoku_EnvironmentCube;
samplerCUBE _Tenkoku_SkyCube;
samplerCUBE _Tenkoku_SnowCube;
sampler2D _Tenkoku_TexFX;
sampler2D _Tenkoku_ParticleTex;
float Tenkoku_LightningLightIntensity;
float _Tenkoku_shaderDepth;
float _Tenkoku_FadeDistance;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uv_depth : TEXCOORD1;
float4 interpolatedRay : TEXCOORD2;
float4 screenPos: TEXCOORD3;
};
v2f vert (appdata_img v)
{
v2f o;
half index = v.vertex.z;
v.vertex.z = 0.1;
o.pos = UnityObjectToClipPos(v.vertex);
o.screenPos=ComputeScreenPos(o.pos);
o.uv = v.texcoord.xy;
o.uv_depth = v.texcoord.xy;
#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0)
o.uv.y = 1-o.uv.y;
#endif
//set frustrum indexes specifically
// this fixes fog/effect errors under webGL
if (0 == (int)index)
o.interpolatedRay = _FrustumCornersWS[0];
else if (1 == (int)index)
o.interpolatedRay = _FrustumCornersWS[1];
else if (2 == (int)index)
o.interpolatedRay = _FrustumCornersWS[2];
else
o.interpolatedRay = _FrustumCornersWS[3];
return o;
}
// Distance-based fog
float ComputeDistance (float3 camDir, float zdepth)
{
float dist;
if (_SceneFogMode.y == 1)
dist = length(camDir);
else
dist = zdepth;// * _ProjectionParams.z;
return dist;
}
float ComputeHalfSpace (float3 wsDir)
{
float3 wpos = _CameraWS + wsDir;
float FH = _HeightParams.x;
float3 C = _CameraWS;
float3 V = wsDir;
float3 P = wpos;
float3 aV = _HeightParams.w * V;
float FdotC = _HeightParams.y;
float k = _HeightParams.z;
float FdotP = P.y-FH;
float FdotV = wsDir.y;
float c1 = k * (FdotP + FdotC);
float c2 = (1-2*k) * FdotP;
float g = min(c2, 0.0);
g = -length(aV) * (c1 - g * g / abs(FdotV+1.0e-5f));
return g;
}
half4 ComputeFog ()
{
// Reconstruct world space position & direction
// towards this screen pixel.
float rawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture,i.uv_depth);
float dpth = Linear01Depth(rawDepth);
float4 wsDir = dpth * i.interpolatedRay;
//CALCULATE FOG
float diff = _Tenkoku_FogEnd - _Tenkoku_FogStart;
float invDiff = abs(diff) > 0.0001f ? 1.0 / diff : 0.0;
_SceneFogParams.z = -invDiff;
_SceneFogParams.w = _Tenkoku_FogEnd * invDiff;
half usedpth = _DistanceParams.z + ComputeDistance(wsDir, dpth) + ComputeHalfSpace (wsDir);
half fogFac = (saturate(max(0.0,usedpth) * _SceneFogParams.z + _SceneFogParams.w));
//Get Scene Color Info
half4 sceneColor = tex2D(_MainTex, i.uv);
//Recalculate fog with heat distortion
diff = _Tenkoku_FogEnd - _Tenkoku_FogStart;
invDiff = abs(diff) > 0.0001f ? 1.0 / diff : 0.0;
_SceneFogParams.z = -invDiff;
_SceneFogParams.w = _Tenkoku_FogEnd * invDiff;
usedpth = _DistanceParams.z + ComputeDistance(wsDir, dpth) + ComputeHalfSpace (wsDir);
fogFac = (saturate(max(0.0,usedpth) * _SceneFogParams.z + _SceneFogParams.w));
// Handle skybox fog
if (rawDepth == 1.0)
{
if (_fogSkybox == 1.0)
{
fogFac = 1.0;
}
if (_fogSkybox == 0.0)
{
fogFac = 0.0;
}
}
fogFac = saturate(fogFac+saturate(lerp(1.0,0.0,_Tenkoku_AtmosphereDensity*2.0)));
//Handle Horizon Fog
float diff2 = _tenkokufogFull - (10.0);
float invDiff2 = abs(diff2) > 0.0001f ? 1.0 / diff2 : 0.0;
half fogFac3 = saturate(max(0.0,usedpth) * -invDiff2 + (_tenkokufogFull * invDiff2));
if (_fogHorizon == 1.0)
{
fogFac *= saturate((wsDir.y/min(_tenkokufogFull,250.0)) + fogFac3);
}
//read texture mipmap based on depth
half4 skyColor = tex2Dlod(_SkyTex, float4(i.uv.x,i.uv.y,0,0));
half4 fCol = lerp(skyColor, sceneColor, fogFac);
if (rawDepth >= 1.0)
{
fCol = lerp(fCol, sceneColor, saturate(lerp(-0.1,1.0, ((dot(half3(0,1,0), i.interpolatedRay.xyz)*0.0005)))));
}
//tint fog
half hVec = saturate(lerp(-4.0,3.0,saturate(dot(half3(0,1,0), normalize( i.interpolatedRay.xyz))+0.6)));
return fCol;
}
Thank you.