- Home /
How to enable Fog in vertex bending shader?
I recently found a Unity shader that bends the vertex to create a "Subway Surfers" game like effect. This is working beautifully, but it doesn't seem to support Fog.
Shader "Custom/Curved" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_QOffset ("Offset", Vector) = (0,0,0,0)
_Dist ("Distance", Float) = 100.0
}
SubShader {
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _QOffset;
float _Dist;
struct v2f {
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
};
v2f vert (appdata_base v)
{
v2f o;
float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
float zOff = vPos.z/_Dist;
vPos += _QOffset*zOff*zOff;
o.pos = mul (UNITY_MATRIX_P, vPos);
o.uv = v.texcoord;
return o;
}
half4 frag (v2f i) : COLOR
{
half4 col = tex2D(_MainTex, i.uv.xy);
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
I tried altering the "SubShader" section as shown below, but it has created a flickering Fog with no Alpha.
Tags { "RenderType"="transparent" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _QOffset;
float _Dist;
struct v2f {
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
};
v2f vert (appdata_base v)
{
v2f o;
float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
float zOff = vPos.z/_Dist;
vPos += _QOffset*zOff*zOff;
o.pos = mul (UNITY_MATRIX_P, vPos);
o.uv = v.texcoord;
UNITY_TRANSFER_FOG(o,v.vertex);
return o;
}
half4 frag (v2f i) : COLOR
{
half4 col = tex2D(_MainTex, i.uv.xy);
UNITY_APPLY_FOG(i.fogCoord, col);
UNITY_OPAQUE_ALPHA(col.a);
return col;
}
ENDCG
}
Can anyone tell me exactly what I'm doing wrong.
Answer by abhimanyu-singh · May 25, 2016 at 05:47 AM
Thanks @Jessespike. I tried those earlier, but they were giving some compilation errors. I merged some code from another shader, and it now supports fog (and hopefully lightmapping as well). Since I'm not a shader programmer, I'm not sure if this is the most optimised way to do it, but at least it's in a working state right now. I'd again like to thank you for taking out the time to help me.
Shader "Custom/Curved With Fog" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_QOffset ("Offset", Vector) = (0,0,0,0)
_Dist ("Distance", Float) = 100.0
}
SubShader {
Tags { "RenderType"="transparent" "IgnoreProjector"="True" }
Pass
{
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert addshadow
#pragma fragment frag
#pragma multi_compile_fog
#pragma multi_compile LIGHTMAP_ON LIGHTMAP_OFF
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _QOffset;
float _Dist;
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};
struct v2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
half2 uv2 : TEXCOORD1;
UNITY_FOG_COORDS(2)
};
v2f vert (appdata_t v)
{
v2f o;
float4 vPos = mul (UNITY_MATRIX_MV, v.vertex);
float zOff = vPos.z/_Dist;
vPos += _QOffset*zOff*zOff;
o.vertex = mul (UNITY_MATRIX_P, vPos);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv2 = v.texcoord;
#ifdef LIGHTMAP_ON
o.uv2 = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
#endif
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
half4 frag (v2f i) : COLOR
{
half4 col = tex2D(_MainTex, i.uv.xy);
#ifdef LIGHTMAP_ON
half4 lm = UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv2 );
col.rgb = col.rgb * DecodeLightmap(lm);
col *= UNITY_LIGHTMODEL_AMBIENT * 10;
#endif
UNITY_APPLY_FOG(i.fogCoord, col);
UNITY_OPAQUE_ALPHA(col.a);
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
Hi, I am facing the same problem, i am using curved shaders in endless game but it doesn't support Fog.....how can i manage to do this in single shader..?? help will be appreciated.
@$$anonymous$$ajid468 since it's a pretty old thread, I do not remember the specifics. However, I've added the last working shader code in my previous comment. As mentioned, I am not sure about the performance though.
Answer by Jessespike · May 24, 2016 at 09:20 PM
UNITY_TRANSFER_FOG(o,v.vertex);
is wrong, it should be UNITY_TRANSFER_FOG(o,o.pos);
As for the alpha problem, I don't know. I would guess it has something to do with UNITY_OPAQUE_ALPHA(col.a);
It wasn't in the original shader that you posted and it's not needed for fog, so it's probably not needed.