• 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 Fuzzzy · Aug 08, 2015 at 10:55 AM · shadowcgshader writing

Unity Shaders, CG color shadow glitch

Hi,

I am very new with cg and currently try to mix together some bits and pieces of code to get the results I want ... I found it a good way of learning new languages in general... So, what I am doing is : I have a cartoon shader on one side (just very slightly tweaked so far ...), and a bit of code allowing to change the color of shadows on the other side ... So of course I mixed them together and the results are pretty nice, I am starting to get this "early 90's" feel I am looking for..

BUT

There is a glitch.

The code will works well ... a part for a very annoying glitch ... Some square of color semi transparent, seeming following the camera, are showing up ... weirdly not everywhere... only certain angles (surface/camera) seems affected...

I don't have a clue what they are ...

I am not looking especially for someone to fix my code, but I would really appreciate if someone could at least tell me what are those glitch, where they usually come from (if they are a well known glitch.. as I said, I am very new with Cg ...), or point me to a few things to test so I can understand what is causing this ... (and I'll fix it myself then, or try too :) ).

So here's "my" code (some might recognize the most part ...).

 Shader "Toon/Cartoon Color Shadow" {
     Properties {
         _Color ("Main Color", Color) = (1,1,1,1)
         _ShadowColor ("Shadow Color", Color) = (0.97,0.88,1,0.75)
         _RimColor ("Rim Color", Color) = (0.97,0.88,1,0.75)
         _RimPower ("Rim Power", Float) = 2.5
         _MainTex ("Diffuse (RGB) Alpha (A)", 2D) = "white" {}
         _BumpMap ("Normal (Normal)", 2D) = "bump" {}
         _SpecularTex ("Specular Level (R) Gloss (G) Rim Mask (B)", 2D) = "gray" {}
         _RampTex ("Toon Ramp (RGB)", 2D) = "white" {}
         _Cutoff ("Alphatest Cutoff", Range(0, 1)) = 0.5
     }
 
     SubShader{
         Tags { "RenderType" = "Opaque" }
         
         CGPROGRAM
             #pragma surface surf TF2 alphatest:_Cutoff
             #pragma target 3.0
 
             struct Input
             {
                 float2 uv_MainTex;
                 float3 worldNormal;                
                 INTERNAL_DATA
             };
             
             sampler2D _MainTex, _SpecularTex, _BumpMap, _RampTex;
             float4 _RimColor;
             float _RimPower;
             float4 _ShadowColor;
             
             inline fixed4 LightingTF2 (SurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten)
             {
                 fixed3 h = normalize (lightDir + viewDir);
                 
                 fixed NdotL = dot(s.Normal, lightDir) * 0.5 + 0.5;                
                 float2 Test = NdotL * atten;
                 fixed3 ramp = tex2D(_RampTex,Test).rgb;
                 
                 float nh = max (0, dot (s.Normal, h));
                 float spec = pow (nh, s.Gloss * 128) * s.Specular;
                 
                 fixed4 c;
                 c.rgb = ((s.Albedo * ramp * _LightColor0.rgb + _LightColor0.rgb * spec) * (atten * 2));
                 
                 //shadow colorization    *IT IS HERE THE POOP SEEMS TO BE*                
                 c.rgb += _ShadowColor.xyz * max(0.0,(1.0-(NdotL*atten*2))) *ramp;
                                     
                 c.a = s.Alpha;
                 
                 return c;
             }
 
             void surf (Input IN, inout SurfaceOutput o)
             {
                 o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
                 o.Alpha = tex2D(_MainTex, IN.uv_MainTex).a;
                 o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
                 float3 specGloss = tex2D(_SpecularTex, IN.uv_MainTex).rgb;
                 o.Specular = specGloss.r;
                 o.Gloss = specGloss.g;
                 
                 half3 rim = pow(max(0, dot(float3(0, 1, 0), WorldNormalVector (IN, o.Normal))), _RimPower) * _RimColor.rgb * _RimColor.a * specGloss.b;
                 o.Emission = rim;
             }
         ENDCG
     }
     Fallback "Transparent/Cutout/Bumped Specular"
 }



Here's a screen of the glitch: A corner of the scene where it is especially visible. You notice all the colored squares ? That's bad. They appears as soon as I start putting some color in the shadows, when I set them as black, no glitch. But then again, for some reasons this glitch appears only on certain surfaces ... I noted this green textured surface is more prone to the glitch, other part using the same shader but with different textures/colors/angles from the cam won't have this issue or at least not as bad (see the pillar in the middle ? same shader, color in the shadow, no glitching...).

alt text

glitch.jpg (59.6 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.

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

2 People are following this question.

avatar image avatar image

Related Questions

Dissolve Shader Problem 1 Answer

Shader with zwrite, shadows and alpha (special alpha) 2 Answers

Linearising the inversion of camera depth effect 1 Answer

Obtaining relative speed of vertex in a shader 1 Answer

ShaderGraph for Projector 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