- Home /
Shader works only on Cubes properly (Alpha problem)
I made a water shader for Indie Unity which works quite good, but it seems that it works only on the cubes from the Gameobjects-menu properly. The problem is that the "fog"-effect which fades out alpha in the distance is not working on non-cubes, the remaining effects work properly.
I would made the fadeout-effect with an extra (alpha)-texture, but then I get a warning that the input limit is reached (I need all the other inputs because I control both bumpmaps with a scrolling-texture script).
Here's the shader:
Shader "Custom/Water" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
_Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
_BumpMap1 ("Bumpmap (RGB Trans)", 2D) = "bump" {}
_BumpMap2 ("Bumpmap (RGB Trans)", 2D) = "bump" {}
_FogFar ("Fog Near", range (0,1000)) = 5
_FogNear ("Fog Far", range (0,1000)) = 10
_FresnelPower ("_FresnelPower", Range(0.05,5.0)) = 0.75
}
SubShader {
Tags { "RenderType"="Transparent" }
CGPROGRAM
#pragma surface surf BlinnPhong alpha
#pragma target 3.0
samplerCUBE _RtReflection;
sampler2D _BumpMap1, _BumpMap2;
samplerCUBE _Cube;
float4 _Color, _ReflectColor;
float _FresnelPower, _FogNear, _FogFar;
struct Input {
float2 uv_BumpMap1;
float2 uv_BumpMap2;
float3 worldRefl;
float3 viewDir;
INTERNAL_DATA
};
void surf (Input IN, inout SurfaceOutput o)
{
half4 c = _Color;
half3 bump1 = UnpackNormal(tex2D( _BumpMap1, IN.uv_BumpMap1)).rgb;
half3 bump2 = UnpackNormal(tex2D( _BumpMap2, IN.uv_BumpMap2)).rgb;
half3 bump = (bump1 + bump2) * 0.5;
o.Normal = bump;
half3 worldReflVec = WorldReflectionVector(IN, o.Normal);
half4 reflcol = texCUBE(_Cube, worldReflVec);
// FRESNEL CALCS
float fcbias = 0.20373;
float facing = saturate(1.0 - max(dot( normalize(IN.viewDir.xyz), normalize(o.Normal)), 0.0));
float refl2Refr = max(fcbias + (1.0-fcbias) * pow(facing, _FresnelPower), 0);
o.Albedo = reflcol.rgb * _ReflectColor.rgb + c.rgb;
o.Emission = o.Albedo * 0.25;
float d = length(IN.viewDir);
float l = saturate((d - _FogNear) / (_FogFar - _FogNear));
o.Alpha = refl2Refr * l;
}
ENDCG
}
FallBack "Reflective/VertexLit"
}
Any ideas how I could solve the issue?
Answer by Owen-Reynolds · Apr 02, 2013 at 01:42 AM
If it only works on a cube, it has to have something to do with cubes always having a back face. I'd guess there's something wrong with the face=
calculation and viewDir, giving 0 alpha on front faces, but working on backs. A flipped sign(?)
Take a look at the back of a plane and see if it works there.
No, the cubes from Unity are also backface culled. On the other side from the plane there's nothing like it should.
Even If I delete the "refl2Refr" in the o.Alpha... line, the non-cubes have the issue.
Just tried it -- looks the same on a plane and cube to me, just looking at alpha. But, the alpha fades IN in the distance. One $$anonymous$$us EL would fade out.
The refl2Refr doesn't seem to do anything (just uniform less opaque.)
Can be tricky to adjust _FogNear/Far, since you can't see the numbers. Sometimes easier to make them not sliders.
That's strange. For me, alpha fades out and refl2refr works. I use a mac, an open gl issue?
Look at el = (distance-near)/(far-near)
, which sets the alpha. That number is 0 at the near value and 1 at the far. So increases alpha as you move away. If it doesn't, something funny is going on.
I made a new scene and now the shader works perfect on both cube and plane. The names for "fog near" and "fog far" are choosen this way, that the slider in the editor makes bigger values in right direction than left, so your "fog far" slider must be on the right hand from the "fog near" slider value. For me it looks this: http://www.imagebanana.com/view/jnvn01xs/WaterShader.jpg (can't embed pictures here some reason, the "accept" button does nothing)