Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Darkio · Jul 01, 2019 at 03:14 PM · shadershadersshader programmingshaderlabshader writing

How to adapt this shader into a transparent one (alpha blend)

Hey guys im very new at shaders so ive no idea what im doing here, may someone help me and say whats missing to this shader gets transparent? i dont know much the syntax only the render pipeline so i really cant do this alone :/ In advance i really apreciate the help thanks here is also a link to the code for better visualization: https://hastebin.com/fivedusaqu.cs

 Shader "Custom/Water/Depth/DiffuseWater"
 {
     Properties
     {
         _Color ("Color", Color) = (1, 1, 1, 1)
         _MainTex ("Texture", 2D) = "white" {}
 
         [Space(20)]
         _WaterColor ("Water color", Color) = (1, 1, 1, 1)
         _WaterTex("Water texture", 2D) = "white" {}
         _Tiling ("Water tiling", Vector) = (1, 1, 1, 1)
         _TextureVisibility ("Texture visibility", Range(0, 1)) = 1
 
         [Space(20)]
         _DistTex ("Distortion", 2D) = "white" {}
         _DistTiling ("Distortion tiling", Vector) = (1, 1, 1, 1)
 
         [Space(20)]
         //_DeepColor ("Water deep color", Color) = (1, 1, 1, 1)
         _WaterHeight ("Water height", Float) = 0
         _WaterDeep ("Water deep", Float) = 0
         _WaterDepth ("Water depth param", Range(0, 0.1)) = 0
         _WaterMinAlpha ("Water min alpha", Range(0, 1)) = 0
         
         [Space(20)]
         _BorderColor ("Border color", Color) = (1, 1, 1, 1)
         _BorderWidth ("Border width", Range(0, 1)) = 0
 
         [Space(20)]
         _MoveDirection ("Direction", Vector) = (0, 0, 0, 0)
     }
     SubShader
     {
         Tags{ "Queue" = "Transparent"  "BW" = "TrueProbes" "LightMode" = "ForwardBase" "RenderType" = "Transparent" "IgnoreProjector" = "True"}//{ "RenderType" = "Opaque" "BW" = "TrueProbes" "LightMode" = "ForwardBase" }
         LOD 100
         ZWrite On
         ColorMask 0
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
             #include "UnityLightingCommon.cginc"
             #pragma multi_compile_fog
             #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
             //Blend One OneMinusSrcAlpha
             //SetTexture[_MainTex]{ combine texture }
             //AlphaToMask On
             struct appdata
             {
                 float4 vertex : POSITION;
                 float4 normal : NORMAL;
                 float2 uv : TEXCOORD0;
 #if LIGHTMAP_ON
                 float2 lightmap_uv : TEXCOORD1;
 #endif
         };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 fixed4 worldPos : TEXCOORD1;
                 fixed camHeightOverWater : TEXCOORD2;
                 fixed waterDepth : TEXCOORD3;
                 UNITY_FOG_COORDS(4)
 #if LIGHTMAP_ON
                 fixed2 lightmap_uv : TEXCOORD5;
 #else
                 fixed4 diffuseLight : TEXCOORD5;
 #endif
                 float4 vertex : SV_POSITION;
             };
 
             fixed4 _Color;
             sampler2D _MainTex;
             float4 _MainTex_ST;
 
             sampler2D _WaterTex;
             fixed2 _Tiling;
             fixed4 _WaterColor;
 
             sampler2D _DistTex;
             fixed2 _DistTiling;
 
             fixed4 _DeepColor;
             fixed _WaterHeight;
             fixed _TextureVisibility;
             fixed _WaterDeep;
             fixed _WaterDepth;
             fixed _WaterMinAlpha;
 
             fixed4 _BorderColor;
             fixed _BorderWidth;
             fixed _BorderVisibility;
 
             fixed3 _MoveDirection;
 
 
             fixed4 DiffuseLight(fixed3 worldNormal)
             {
                 half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
 
                 fixed4 diff = nl * _LightColor0;
                 diff.rgb += ShadeSH9(half4(worldNormal, 1));
 
                 return diff;
             }
 
             fixed2 WaterPlaneUV(fixed3 worldPos, fixed camHeightOverWater)
             {
                 fixed3 camToWorldRay = worldPos - _WorldSpaceCameraPos;
                 fixed3 rayToWaterPlane = (camHeightOverWater / camToWorldRay.y * camToWorldRay);
                 return rayToWaterPlane.xz - _WorldSpaceCameraPos.xz;
             }
 
             fixed3 LightmapColor(fixed2 lightmap_uv)
             {
                 fixed4 lightmapCol = UNITY_SAMPLE_TEX2D(unity_Lightmap, lightmap_uv);
                 return DecodeLightmap(lightmapCol);
             }
 
             fixed4 MainColor(v2f i)
             {
                 fixed4 mainCol = tex2D(_MainTex, i.uv) * _Color; 
 #if LIGHTMAP_ON
                 mainCol.rgb *= LightmapColor(i.lightmap_uv);
 #else
                 mainCol.rgb *= i.diffuseLight;
 #endif
 
                 return mainCol;
             }
 
             v2f vert (appdata v)
             {
                 v2f o;
 
                 o.worldPos = mul(UNITY_MATRIX_M, v.vertex);
                 o.vertex = mul(UNITY_MATRIX_VP, o.worldPos);
                 
                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
 
                 fixed3 camToWorldRay = o.worldPos - _WorldSpaceCameraPos;
                 o.camHeightOverWater = _WorldSpaceCameraPos.y - _WaterHeight;
 
                 fixed3 rayToWaterPlane = o.camHeightOverWater / (-camToWorldRay.y) * camToWorldRay;
                 fixed depth = length(camToWorldRay - rayToWaterPlane);
                 o.waterDepth = depth * _WaterDepth * saturate(rayToWaterPlane.y - camToWorldRay.y);;
 
 #if LIGHTMAP_ON
                 o.lightmap_uv = v.lightmap_uv.xy * unity_LightmapST.xy + unity_LightmapST.zw;
 #else
                 fixed4 worldNormal = normalize(mul(UNITY_MATRIX_M, float4(v.normal.xyz, 0)));
                 o.diffuseLight = DiffuseLight(worldNormal.xyz);
 #endif
 
 #if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
                 fixed3 worldPosOnPlane = _WorldSpaceCameraPos + rayToWaterPlane;
                 fixed3 positionForFog = lerp(worldPosOnPlane, o.worldPos.xyz, o.worldPos.y > _WaterHeight);
                 fixed4 waterVertex = mul(UNITY_MATRIX_VP, fixed4(positionForFog, 1));
                 UNITY_TRANSFER_FOG(o, waterVertex);
 #endif
 
                 return o;
             }
             
             fixed4 frag (v2f i) : SV_Target
             {
                 fixed lengthUnderWater = max(0, _WaterHeight - i.worldPos.y);
                 fixed underWater = lerp(0, 1, lengthUnderWater > 0);
                 fixed borderAlpha = lerp(underWater * _BorderColor.a, 0, saturate(lengthUnderWater / _BorderWidth));
                 fixed waterAlpha = saturate(lengthUnderWater / _WaterDeep + _WaterMinAlpha + i.waterDepth);
 
                 fixed4 mainCol = MainColor(i);
 
                 fixed2 water_uv = i.worldPos.xz;//WaterPlaneUV(i.worldPos, i.camHeightOverWater);
                 fixed4 distortion = tex2D(_DistTex, water_uv * _DistTiling) * 2 - 1;
                 fixed2 distorted_uv = ((water_uv + distortion.rg) - _Time.y * _MoveDirection.xz) * _Tiling;
 
                 fixed4 waterCol = tex2D(_WaterTex, distorted_uv);
                 waterCol = lerp(_WaterColor, fixed4(1, 1, 1, 1), waterCol.r * _TextureVisibility);
 
                 fixed4 finalCol = lerp(mainCol, waterCol, _WaterColor.a * waterAlpha * underWater);
                 finalCol.rgb = lerp(finalCol.rgb, _BorderColor.rgb, borderAlpha);
 
                 UNITY_APPLY_FOG(i.fogCoord, finalCol);
 
                 //return fixed4(reflection, 0, 0, 1);
                 return finalCol;
             }
 
             
 
             ENDCG
         }
     }
 }
 
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

180 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 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 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 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 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 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 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 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 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 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 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 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

Shader inverts uv map only once, how to fix it? 0 Answers

Fog not working in my Shader~ 0 Answers

Header for material properties in inspector? 2 Answers

Addition to Standard Shader stopping it from batching? 0 Answers

"Stencil Operation" property setup? 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