- Home /
Alpha Blending Problem
I know that there are many questions to alpha blending topic but I have a problem I can't fix:
In my scene is a sea which has attached a cg shader. This shader reads the alpha values from a heighmap which is procedural generated from the terrain.
The shader code:
Shader "Custom/Sea"
{
Properties
{
_Color("Diffuse Material Color", Color) = (1.0, 1.0, 1.0, 1.0)
_MainTex("Diffusemap", 2D) = "white" {}
_BumpTex("Normalmap", 2D) = "bump" {}
_CubeTex("Cubemap", Cube) = "" {}
_SpecColor("Specular Material Color", Color) = (1.0, 1.0, 1.0, 1.0)
_Shininess("Shininess", Range(1, 20)) = 2
_HeighTex("Heighmap (Script)", 2D) = "white" {}
}
SubShader
{
Pass
{
Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform sampler2D _BumpTex;
uniform float4 _BumpTex_ST;
uniform sampler2D _HeighTex;
uniform float4 _HeighTex_ST;
uniform samplerCUBE _CubeTex;
uniform float4 _Color;
uniform float4 _SpecColor;
uniform float _Shininess;
uniform float _Alpha;
uniform float4 _LightColor0;
struct vertexInput
{
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
float4 texcoord1 : TEXCOORD1;
float3 normal : NORMAL;
float4 tangent : TANGENT;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
float4 posWorld : TEXCOORD0;
float4 tex : TEXCOORD1;
float4 tex1 : TEXCOORD2;
float3 tangentWorld : TEXCOORD3;
float3 normalWorld : TEXCOORD4;
float3 binormalWorld : TEXCOORD5;
};
vertexOutput vert(vertexInput i)
{
vertexOutput o;
o.posWorld = mul(_Object2World, i.vertex);
o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
o.tangentWorld = normalize(mul(_Object2World, float4(i.tangent.xyz, 0.0)).xyz);
o.normalWorld = normalize(mul(float4(i.normal, 0.0), _World2Object).xyz);
o.binormalWorld = normalize(cross(o.normalWorld, o.tangentWorld) * i.tangent.w);
o.tex = i.texcoord;
o.tex1 = i.texcoord1;
return o;
}
float4 frag(vertexOutput o) : COLOR
{
float4 diffmap = tex2D(_MainTex, _MainTex_ST.xy * o.tex.xy + _MainTex_ST.zw);
float4 bumpmap = tex2D(_BumpTex, _BumpTex_ST.xy * o.tex.xy + _BumpTex_ST.zw);
float4 heighmap = tex2D(_HeighTex, _HeighTex_ST.xy * o.tex1.xy + _HeighTex_ST.zw);
float3 localCoords = 2.0 * bumpmap.xyz - float3(1.0, 1.0, 1.0);
float3x3 local2WorldTranspose = float3x3(o.tangentWorld, o.binormalWorld, o.normalWorld);
float3 normalDirection = normalize(mul(localCoords, local2WorldTranspose));
float3 viewDirection = normalize(_WorldSpaceCameraPos - o.posWorld.xyz);
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 ambient = UNITY_LIGHTMODEL_AMBIENT * _Color * diffmap;
float3 diffuse = _LightColor0 * _Color * diffmap * max(0.0, dot(normalDirection, lightDirection));
float4 specular = _LightColor0 * _SpecColor * pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)), _Shininess);
float3 cubemap = texCUBE(_CubeTex, normalize(reflect(normalize(o.posWorld.xyz - _WorldSpaceCameraPos), normalDirection)));
float alpha = 1.0 - heighmap.w;
return float4(ambient + diffuse + cubemap, alpha) + specular;
}
ENDCG
}
}
}
And that's producing the following result:
All right so far - but now I want to add some waves (plane with transparent shader) which should been blended by the sea and that should look like this:
The alpha value of the sea should blend the waves - thats the theory, but in practice the waves are either always complete visible or not visible (Zwrite On/Off). The wave-plane is placed under the sea and over the terrain;
The waves shader code:
Shader "Custom/Waves"
{
Properties
{
_MainTex("Diffusemap", 2D) = "white" {}
}
SubShader
{
Pass
{
Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
struct vertexInput
{
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
float3 normal : NORMAL;
float4 tangent : TANGENT;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
float4 tex : TEXCOORD0;
};
vertexOutput vert(vertexInput i)
{
vertexOutput o;
o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
o.tex = i.texcoord;
return o;
}
float4 frag(vertexOutput o) : COLOR
{
float4 diffmap = tex2D(_MainTex, _MainTex_ST.xy * o.tex.xy + _MainTex_ST.zw);
return diffmap;
}
ENDCG
}
}
}
My question is: What have I have to add in these two shaders (like ZWrite) that the ocean blends the waves the way I want?
Sorry for my bad english! :)
Your answer
Follow this Question
Related Questions
Alpha Blending for projectors on transparent/cutout materials 0 Answers
Is there a way to set an alpha color? 2 Answers
Shader with zwrite, shadows and alpha (special alpha) 2 Answers
shader blending 2 texture script 0 Answers
Use multiple materials? 1 Answer