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 alessandro_88 · Sep 24, 2013 at 12:21 AM · shaderalphawateralpha-channel

Water shader with alpha

I have this water shader. I need a cutout, so I added an alpha-channel to the texture1 which I'd like use as this mentioned cutout, but I don't know how code and link this in the shader. Could anyone help me please? Thanks a lot!!

 Shader "Shaders/WaterShader"
 {
     Properties 
     {
         _Color("_Color", Color) = (1,1,1,1)
         _Texture1("_Texture1", 2D) = "black" {}
         _BumpMap1("_BumpMap1", 2D) = "black" {}
         _Texture2("_Texture2", 2D) = "black" {}
         _BumpMap2("_BumpMap2", 2D) = "black" {}
         
         _DistortionMap("_DistortionMap", 2D) = "black" {}
         _DistortionPower("_DistortionPower", Range(0,0.02) ) = 0
         _Specular("_Specular", Range(0,7) ) = 1
         _Gloss("_Gloss", Range(0.3,2) ) = 0.3
         _Opacity("_Opacity", Range(-0.2,1) ) = 0
         _Reflection("_Reflection", 2D) = "black" {}
         _ReflectPower("_ReflectPower", Range(0,0.8) ) = 0
     }
     
     SubShader 
     {
         Tags
         {
             "Queue"="Transparent"
             "IgnoreProjector"="True"
             "RenderType"="Transparent"
         }
 
         
         Cull Back
         ZWrite On
         ZTest LEqual
         ColorMask RGBA
         Blend SrcAlpha OneMinusSrcAlpha
         
         
         CGPROGRAM
         #pragma surface surf BlinnPhongEditor
         #pragma target 3.0
         
         
         fixed4 _Color;
         uniform sampler2D _Texture1;
         uniform sampler2D _BumpMap1;
         uniform sampler2D _Texture2;
         uniform sampler2D _BumpMap2;        
         uniform sampler2D _DistortionMap;
         half _DistortionPower;
         fixed _Specular;
         fixed _Gloss;
         float _Opacity;
         uniform sampler2D _Reflection;
         float _ReflectPower;
         
         inline fixed4 LightingBlinnPhongEditor_PrePass (SurfaceOutput s, fixed4 light)
         {
             fixed3 spec = light.a * s.Gloss;
             fixed4 c;
             c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
             c.a = s.Alpha;
             return c;
         }
 
         inline fixed4 LightingBlinnPhongEditor (SurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten)
         {
             fixed3 h = normalize (lightDir + viewDir);
             
             fixed diff = max (0, dot ( lightDir, s.Normal ));
             
             float nh = max (0, dot (s.Normal, h));
             float spec = pow (nh, s.Specular*128.0);
             
             fixed4 res;
             res.rgb = _LightColor0.rgb * diff;
             res.w = spec * Luminance (_LightColor0.rgb);
             res *= atten * 2.0;
 
             return LightingBlinnPhongEditor_PrePass( s, res );
         }
         
         struct Input {
             float3 viewDir;
             float2 uv_DistortionMap;
             float2 uv_Texture1;
             float2 uv_Texture2;
             float2 uv_BumpMap1;
             float2 uv_BumpMap2;
         };
 
         void surf (Input IN, inout SurfaceOutput o) {
             o.Normal = float3(0.0,0.0,1.0);
             o.Alpha = 1.0;
             o.Albedo = 0.0;
             o.Emission = 0.0;
             o.Gloss = 0.0;
             o.Specular = 0.0;
             
             float4 ViewDirection=float4( IN.viewDir.x, IN.viewDir.y,IN.viewDir.z *10,0 );
             float4 Normalize0=normalize(ViewDirection);            
             
             
             float2 ReflexUV= float2((Normalize0.x + 1) * 0.5, (Normalize0.y + 1) * 0.5);
             
             
             // Create Normal for DistorionMap
             float4 DistortNormal = tex2D(_DistortionMap,IN.uv_DistortionMap.xy);
             // Multiply Tex2DNormal effect by DistortionPower
             float2 FinalDistortion = DistortNormal.xy * _DistortionPower;
             
             // Apply DistorionMap in Reflection's UV
             float4 TexReflex=tex2D(_Reflection,ReflexUV + FinalDistortion);
             
             // Get Fresnel from viewDirection angle
             float3 Fresnel0_1_NoInput = float3(0,0,1);
             float Fresnel0=(1.0 - dot( normalize( float3( IN.viewDir.x, IN.viewDir.y,IN.viewDir.z ).xyz), normalize( Fresnel0_1_NoInput.xyz ) ));
             
             // Multiply reflection by fresnel so it's stronger when it's far
             float Multiply12 =_ReflectPower * Fresnel0;
             float4 FinalReflex = TexReflex * Multiply12;
             
             
             // Apply Distorion in MainTex
             float4 Tex2D0=tex2D(_Texture1,IN.uv_Texture1.xy + FinalDistortion);
             
             float3 alphaByView =  dot(normalize(IN.viewDir),o.Normal) ;
             
             
             // Apply Distorion in Texture2
             // float2 Add1=Tex2UV + FinalDistortion;
             float4 Tex2D1=tex2D(_Texture2,IN.uv_Texture2.xy - FinalDistortion); 
             
             // Merge MainTex and Texture2
             float4 TextureMix=Tex2D0 * Tex2D1;
             
             // Add TextureMix with Reflection
             // float4 TexNReflex = lerp(TextureMix, FinalReflex, _ReflectPower);
             
             // Merge Textures, Reflection and Color
             float4 FinalDiffuse=_Color * TextureMix;                 
             
             // Apply Distortion to BumpMap            
             half4 Tex2D3=tex2D(_BumpMap1,IN.uv_BumpMap1.xy + FinalDistortion);            
                         
             // Apply Distortion to BumpMap2                        
             fixed4 Tex2D4=tex2D(_BumpMap2,IN.uv_BumpMap2.xy - FinalDistortion);
             
             // Get Average from BumpMap1 and BumpMap2
             fixed4 AvgBump= (Tex2D3 + Tex2D4) / 2;
             
             // Unpack Normals
             fixed4 UnpackNormal1=float4(UnpackNormal(AvgBump).xyz, 1.0);
             
                         
             o.Albedo = FinalDiffuse;
             o.Normal = UnpackNormal1;
             o.Emission = FinalReflex;
             o.Specular = _Gloss;
             o.Gloss = _Specular;
             o.Alpha = (2 * _Opacity) - alphaByView;
 
             o.Normal = normalize(o.Normal);
         }
     ENDCG
     }
     Fallback "Diffuse"
 }
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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Water foam 1 Answer

Shader question 1 Answer

How to access alpha value of transparent shader. 3 Answers

Change alpha in runtime of gameObject with mobile shader 1 Answer

sprite.associatedAlphaSplitTexture with RGB Crunched ETC1 0 Answers


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