• 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 artunn · Nov 24, 2020 at 07:00 PM · shadershadersshader programming

Help about adding transparency to Roystan's toon shader

Hi guys, I wanted to add cartoonish look to my game, and I find this amazing tutorial about it. https://roystan.net/articles/toon-shader.html

First of all I want to mention that I just started learning about shader development and computer graphics, so the whole concept is new for me. I searched other posts to learn how can I add transparency to custom shaders, but the things I tried(I'll explain in a second) end up with unexpected behaviour.

I tried to add Transparency related things to the tags, added 'alpha' keyword to #pragma definitions and blend with SrcAlpha and OneMinusSrcAlpha.

My modified version is like this, and colors change even when I'm in the editor mode of the Unity, definitely weird. I'm sure that something I've done is totally wrong :D

 Shader "Roystan/Toon Complete"
 {
     Properties
     {
         _Color("Color", Color) = (1,1,1,1)
         _MainTex("Main Texture", 2D) = "white" {}
         // Ambient light is applied uniformly to all surfaces on the object.
         [HDR]
         _AmbientColor("Ambient Color", Color) = (0.4,0.4,0.4,1)
         [HDR]
         _SpecularColor("Specular Color", Color) = (0.9,0.9,0.9,1)
         // Controls the size of the specular reflection.
         _Glossiness("Glossiness", Float) = 32
         [HDR]
         _RimColor("Rim Color", Color) = (1,1,1,1)
         _RimAmount("Rim Amount", Range(0, 1)) = 0.716
         // Control how smoothly the rim blends when approaching unlit
         // parts of the surface.
         _RimThreshold("Rim Threshold", Range(0, 1)) = 0.1        
     }
     SubShader
     {
         Pass
         {
              
             // Setup our pass to use Forward rendering, and only receive
             // data on the main directional light and ambient light.
             Tags
             {
                 "LightMode" = "ForwardBase"
                 "PassFlags" = "OnlyDirectional"
 
                 //I ADDED THE REST OF THE TAGS
                 "Queue"="Transparent"
                 "IgnoreProjector"="True"
                 "RenderType"="Transparent"
             }
 
             //I ADDED THE BLEND
             Blend SrcAlpha OneMinusSrcAlpha
 
             CGPROGRAM
             #pragma vertex vert alpha //I ADDED ALPHA
             #pragma fragment frag alpha //I ADDED ALPHA
             // Compile multiple versions of this shader depending on lighting settings.
             #pragma multi_compile_fwdbase
             
             #include "UnityCG.cginc"
             // Files below include macros and functions to assist
             // with lighting and shadows.
             #include "Lighting.cginc"
             #include "AutoLight.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;                
                 float4 uv : TEXCOORD0;
                 float3 normal : NORMAL;
             };
 
             struct v2f
             {
                 float4 pos : SV_POSITION;
                 float3 worldNormal : NORMAL;
                 float2 uv : TEXCOORD0;
                 float3 viewDir : TEXCOORD1;    
                 // Macro found in Autolight.cginc. Declares a vector4
                 // into the TEXCOORD2 semantic with varying precision 
                 // depending on platform target.
                 SHADOW_COORDS(2)
             };
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
             
             v2f vert (appdata v)
             {
                 v2f o;
                 o.pos = UnityObjectToClipPos(v.vertex);
                 o.worldNormal = UnityObjectToWorldNormal(v.normal);        
                 o.viewDir = WorldSpaceViewDir(v.vertex);
                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                 // Defined in Autolight.cginc. Assigns the above shadow coordinate
                 // by transforming the vertex from world space to shadow-map space.
                 TRANSFER_SHADOW(o)
                 return o;
             }
             
             float4 _Color;
 
             float4 _AmbientColor;
 
             float4 _SpecularColor;
             float _Glossiness;        
 
             float4 _RimColor;
             float _RimAmount;
             float _RimThreshold;    
 
             float4 frag (v2f i) : SV_Target
             {
                 float3 normal = normalize(i.worldNormal);
                 float3 viewDir = normalize(i.viewDir);
 
                 // Lighting below is calculated using Blinn-Phong,
                 // with values thresholded to creat the "toon" look.
                 // https://en.wikipedia.org/wiki/Blinn-Phong_shading_model
 
                 // Calculate illumination from directional light.
                 // _WorldSpaceLightPos0 is a vector pointing the OPPOSITE
                 // direction of the main directional light.
                 float NdotL = dot(_WorldSpaceLightPos0, normal);
 
                 // Samples the shadow map, returning a value in the 0...1 range,
                 // where 0 is in the shadow, and 1 is not.
                 float shadow = SHADOW_ATTENUATION(i);
                 // Partition the intensity into light and dark, smoothly interpolated
                 // between the two to avoid a jagged break.
                 float lightIntensity = smoothstep(0, 0.01, NdotL * shadow);    
                 // Multiply by the main directional light's intensity and color.
                 float4 light = lightIntensity * _LightColor0;
 
                 // Calculate specular reflection.
                 float3 halfVector = normalize(_WorldSpaceLightPos0 + viewDir);
                 float NdotH = dot(normal, halfVector);
                 // Multiply _Glossiness by itself to allow artist to use smaller
                 // glossiness values in the inspector.
                 float specularIntensity = pow(NdotH * lightIntensity, _Glossiness * _Glossiness);
                 float specularIntensitySmooth = smoothstep(0.005, 0.01, specularIntensity);
                 float4 specular = specularIntensitySmooth * _SpecularColor;                
 
                 // Calculate rim lighting.
                 float rimDot = 1 - dot(viewDir, normal);
                 // We only want rim to appear on the lit side of the surface,
                 // so multiply it by NdotL, raised to a power to smoothly blend it.
                 float rimIntensity = rimDot * pow(NdotL, _RimThreshold);
                 rimIntensity = smoothstep(_RimAmount - 0.01, _RimAmount + 0.01, rimIntensity);
                 float4 rim = rimIntensity * _RimColor;
 
                 float4 sample = tex2D(_MainTex, i.uv);
                 
                 return (light + _AmbientColor + specular + rim) * _Color * sample;
             }
             ENDCG
         }
 
         // Shadow casting support.
         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
     }
 }

I left the original comments on the code, and commented my changes with CAPITAL LATTERS to make them easily distinguishable.

I want to be able to edit transparency. Can you help me to achieve this? Thanks.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

222 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

My shader code is having an error with returns at lines 50 and 60. 1 Answer

Custom shader converting to urp need help 1 Answer

pixelated water shader 0 Answers

post shader: reconstruct worldspace plane coordinates 1 Answer

How do I get local object position param in particle system for 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