Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 NateAGeek · Mar 02, 2015 at 12:02 PM · shaderlightingshadowsshadow

Toon Shader Not Working With Multiple Spotlight Shadows (Pro)

Hello all,

I've been working on making this toon shader from scratch. I have manage to get it to look great! However, it can't get shadows from multiple spotlights(it does work with directional), D:

What it looks like: Example

 Shader "Custom/ToonShader" {
     Properties {
         _MainTex ("Post Image", 2D) = "white" {}
         _RampTex ("Post Image", 2D) = "white" {}
         _Strength ( "Additive Strength", Float ) = 1.0 
         _TintColor ("Tint Color", Color) = (1.0, 1.0, 1.0, 1.0)
     }
     SubShader {
         Tags {"Queue" = "Geometry" "RenderType" = "Opaque"}
         Pass {
             Tags {"LightMode" = "ForwardBase"}
             CGPROGRAM
             #pragma multi_compile_fwdbase_fullforwardshadows
             #pragma vertex vertexShaderMain
             #pragma fragment fragmentShaderMain
             #pragma fragmentoption ARB_precision_hint_fastest
             #pragma target 3.0
             #include "UnityCG.cginc"
             #include "AutoLight.cginc"
             
             uniform sampler2D _MainTex;
             uniform sampler2D _RampTex;
             uniform float _Strength;
             uniform float4 _LightColor0;
             uniform float4 _TintColor;
             uniform float _TotalRamps;
             
             struct vertexInput {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float4 texcoord : TEXCOORD0;
             };
             
             struct vertexOutput {
                 float4 pos : SV_POSITION;
                 float4 tex : TEXCOORD0;
                 float4 posWorld : TEXCOORD1;
                 float3 normalDir : TEXCOORD2;
                 LIGHTING_COORDS(3,4)
             };
 
             vertexOutput vertexShaderMain(vertexInput input) {
                 vertexOutput output;
                 
                 output.posWorld = mul(_Object2World, input.vertex);
                 output.normalDir = normalize(mul(float4(input.normal, 0.0), _World2Object).xyz);
                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
                 output.tex = input.texcoord;
                 
                 TRANSFER_VERTEX_TO_FRAGMENT(output);
                 
                 return output;
             }
             
             float4 fragmentShaderMain(vertexOutput o) : COLOR {
                 float3 normalDir = o.normalDir;
                 float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - o.posWorld.xyz);
                 float atten;
                 float3 lightDir;
                 
                 if(_WorldSpaceLightPos0.w == 0.0){
                     //atten = 1.0;
                     lightDir = normalize(_WorldSpaceLightPos0.xyz);
                 }
                 else{
                     float3 fragToLightSource = _WorldSpaceLightPos0 - o.posWorld.xyz;
                     //atten = 1/length(fragToLightSource);
                     lightDir = normalize(fragToLightSource);
                 }
                 
                 atten = SHADOW_ATTENUATION(o) * 2;
                 
                 float3 diffuseRef = atten * _LightColor0.xyz * saturate(dot(normalDir, lightDir));
                 float intensity = dot(lightDir, normalDir);
                 
                 float3 rampColor = tex2D(_RampTex, float2(0.0, 0.0));
                 
                 if(intensity > 0.95){
                     rampColor = tex2D(_RampTex, float2(0.0, 0.0));
                 }else if(intensity > 0.5){
                     rampColor = tex2D(_RampTex, float2(0.25, 0.0));
                 }else if(intensity > 0.25){
                     rampColor = tex2D(_RampTex, float2(0.50, 0.0));
                 }else{
                     rampColor = tex2D(_RampTex, float2(0.75, 0.0));
                 }
                 
                 float3 lightFinal = rampColor * UNITY_LIGHTMODEL_AMBIENT.rgb;
                 
                 return tex2D(_MainTex, o.tex) * float4(lightFinal, 1.0) * float4(diffuseRef, 1.0) * _Strength;
             }
             ENDCG
         }
         Pass {
             Tags {"LightMode" = "ForwardAdd"}
             Blend One One 
             CGPROGRAM
             #pragma multi_compile_fwdbase
             #pragma vertex vertexShaderMain
             #pragma fragment fragmentShaderMain
             #pragma fragmentoption ARB_precision_hint_fastest
             #pragma target 3.0
             #include "UnityCG.cginc"
             #include "AutoLight.cginc"
             
             uniform sampler2D _MainTex;
             uniform sampler2D _RampTex;
             uniform float _Strength;
             uniform float4 _LightColor0;
             uniform float4 _TintColor;
             uniform float _TotalRamps;
             
             struct vertexInput {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float4 texcoord : TEXCOORD0;
             };
             
             struct vertexOutput {
                 float4 pos : SV_POSITION;
                 float4 tex : TEXCOORD0;
                 float4 posWorld : TEXCOORD1;
                 float3 normalDir : TEXCOORD2;
                 LIGHTING_COORDS(3,4)
             };
 
             vertexOutput vertexShaderMain(vertexInput input) {
                 vertexOutput output;
                 
                 output.posWorld = mul(_Object2World, input.vertex);
                 output.normalDir = normalize(mul(float4(input.normal, 0.0), _World2Object).xyz);
                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
                 output.tex = input.texcoord;
                 
                 TRANSFER_VERTEX_TO_FRAGMENT(output);
                 
                 return output;
             }
             
             float4 fragmentShaderMain(vertexOutput o) : COLOR {
                 float3 normalDir = o.normalDir;
                 float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - o.posWorld.xyz);
                 float atten;
                 float3 lightDir;
                 
                 if(_WorldSpaceLightPos0.w == 0.0){
                     //atten = 1.0;
                     lightDir = normalize(_WorldSpaceLightPos0.xyz);
                 }
                 else{
                     float3 fragToLightSource = _WorldSpaceLightPos0 - o.posWorld.xyz;
                     //atten = 1/length(fragToLightSource);
                     lightDir = normalize(fragToLightSource);
                 }
                 
                 atten = LIGHT_ATTENUATION(o) * 2;
                 
                 float3 diffuseRef = atten * _LightColor0.xyz * saturate(dot(normalDir, lightDir));
                 float intensity = dot(lightDir, normalDir);
                 
                 float3 rampColor = tex2D(_RampTex, float2(0.0, 0.0));
                 
                 if(intensity > 0.95){
                     rampColor = tex2D(_RampTex, float2(0.0, 0.0));
                 }else if(intensity > 0.5){
                     rampColor = tex2D(_RampTex, float2(0.25, 0.0));
                 }else if(intensity > 0.25){
                     rampColor = tex2D(_RampTex, float2(0.50, 0.0));
                 }else{
                     rampColor = tex2D(_RampTex, float2(0.75, 0.0));
                 }
                 
                 float3 lightFinal = rampColor * UNITY_LIGHTMODEL_AMBIENT.rgb;
                 
                 return tex2D(_MainTex, o.tex) * float4(lightFinal, 1.0) * float4(diffuseRef, 1.0) * _Strength;
             }
             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

2 People are following this question.

avatar image avatar image

Related Questions

Toon Shader Working With Multiple Spotlight Shadows (Pro) 0 Answers

Any way to get rid of squares (artifacts) in a semi transparent shadow of a fade shader? 0 Answers

Strange shadow I didn't expect 1 Answer

Want shadows from a semi-transparent receiver 0 Answers

Toon shader light culling shadow issue 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