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 TSCSimulation_AH · May 23, 2019 at 03:08 PM · shadertransparencyshader programming

Help Adding Transparency to Shader

Hello,

I've got a shader here that I'm making edits to. I'd like to add transparency, and I've added something that I thought would work, but it appears it doesn't. Any help would be greatly appreciated.

Code:

 // Upgrade NOTE: upgraded instancing buffer 'Props' to new syntax.
 
 Shader "Smkgames/Iridescence/DistortionView" {
     Properties{
         [Space(20)][Header(MainTex and ColorRamp)][Space(20)]
         _Color("Colour", Color) = (1,1,1,1)
         _Transparency("Transparency", Range(0, 1.0)) = 0.5
         _MainTex ("Albedo (RGB)", 2D) = "white" {}
         _ColorRamp("ColorRamp",2D) = "white"{}
         _Blend("Blend",Range(0,1)) = 0.5
         [Space(20)][Header(Mask)][Space(20)]
         _Mask("Mask",2D) = "white"{}
         [Space(20)][Header(Adding Distortion)][Space(20)]
         _Noise ("Noise", 2D) = "white" {}
         _Distortion("Distortion",Float) = 6
 
         [Space(20)][Header(BumpMap and BumpPower)][Space(20)]
 
         _BumpMap ("Bumpmap", 2D) = "bump" {}
         _BumpPower("BumpPower",Range(0.1,1)) = 0.1
         
         [Space(20)][Header(Change ColorRamp)][Space(20)]
         _Hue ("Hue", Range(0, 1.0)) = 0
         _Saturation ("Saturation", Range(0, 1.0)) = 0.5
         _Brightness ("Brightness", Range(0, 1.0)) = 0.5
         _Contrast ("Contrast", Range(0, 1.0)) = 0.5
 
         [Space(20)][Header(Smoothness and Metallic)][Space(20)]
         _Glossiness ("Smoothness", Range(0,1)) = 0.5
         _Metallic ("Metallic", Range(0,1)) = 0.0
     }
     SubShader {
         Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
         ZWrite Off
         Blend SrcAlpha OneMinusSrcAlpha
         LOD 100
         
         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard fullforwardshadows
 
         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0
 
     #include "UnityCG.cginc"
 
       sampler2D _MainTex,_ColorRamp,_Noise,_Mask;
       sampler2D _BumpMap;
       float4 _RimColor;
       float _BumpIntensity;
 
         struct Input {
           float2 uv_MainTex;
           float2 uv_Mask;
           float2 uv_Noise;
           float2 uv_BumpMap;
           float2 uv_ColorRamp;
           float3 viewDir;
           float3 worldPos;
         };
 
       float4 _ColorRamp_ST;
         half _Glossiness;
         half _Metallic;
         half _Transparency;
         float _Blend;
         float _BumpPower;
         float _Distortion;
 
         UNITY_INSTANCING_BUFFER_START(Props)
             // put more per-instance properties here
         UNITY_INSTANCING_BUFFER_END(Props)
 
         fixed _Hue, _Saturation, _Brightness, _Contrast;
 
 
                     inline float3 applyHue(float3 aColor, float aHue)
             {
                 float angle = radians(aHue);
                 float3 k = float3(0.57735, 0.57735, 0.57735);
                 float cosAngle = cos(angle);
                 
                 return aColor * cosAngle + cross(k, aColor) * sin(angle) + k * dot(k, aColor) * (1 - cosAngle);
             }
             
             inline float4 applyHSBCEffect(float4 startColor, fixed4 hsbc)
             {
                 float hue = 360 * hsbc.r;
                 float saturation = hsbc.g * 2;
                 float brightness = hsbc.b * 2 - 1;
                 float contrast = hsbc.a * 2;
  
                 float4 outputColor = startColor;
                 outputColor.rgb = applyHue(outputColor.rgb, hue);
                 outputColor.rgb = (outputColor.rgb - 0.5f) * contrast + 0.5f;
                 outputColor.rgb = outputColor.rgb + brightness;
                 float3 intensity = dot(outputColor.rgb, float3(0.39, 0.59, 0.11));
                 outputColor.rgb = lerp(intensity, outputColor.rgb, saturation);
                  
                 return outputColor;
             }
         
             fixed4 _Color;
 
         void surf (Input IN, inout SurfaceOutputStandard o) 
         {
             fixed4 col = _Color;
             // Albedo comes from a texture tinted by color
             float4 c = tex2D (_MainTex, IN.uv_MainTex);
             float noise = tex2D(_Noise,IN.uv_Noise);
             fixed3 normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
             normal.z /= _BumpPower;
             o.Normal = normalize(normal); 
             float2 rim = dot (normalize(IN.viewDir), o.Normal);
             //float2 rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
             float2 noiseRim = rim*(noise*_Distortion);
             float4 mask = tex2D(_Mask,IN.uv_Mask);
             float4 colorRamp = tex2D(_ColorRamp,TRANSFORM_TEX(noiseRim+IN.viewDir.xyz, _ColorRamp))*mask;
             colorRamp = max(colorRamp,(1-mask)* c);
             fixed4 hsbc = fixed4(_Hue, _Saturation, _Brightness, _Contrast);
             float4 colorRampHSBC = applyHSBCEffect(colorRamp, hsbc);
             o.Albedo = lerp(c,colorRampHSBC,_Blend) * col;
             o.Metallic = _Metallic;
             o.Smoothness = _Glossiness;
             o.Alpha = _Transparency; //This line!!!
         }
         ENDCG
     }
     FallBack "Diffuse"
 }

Thanks a lot in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Namey5 · Jun 16, 2019 at 10:55 AM

Surface shaders work a little differently when it comes to compiling with transparency; blend modes seem to only work some of the time. Instead, to make a surface shader transparent you need to make an addition to the shader declaration (line 40);

 #pragma surface surf Standard alpha:fade
Comment
Add comment · Share
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

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

174 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

Related Questions

Receiving Shadows On a Tranparent Shader 1 Answer

How can i add transparency to this shader? 1 Answer

Visual Artifacts from my Custom Transparent Shader 0 Answers

Shader to turn materials transparent based on y axis 0 Answers

See backside of a transparent emissive shader? 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