Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Oct 19, 2014 at 03:29 PM · shaderalphacgblending

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:

alt text


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:

alt text

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! :)

ocean_only.png (268.1 kB)
ocean_waves.png (257.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges