• 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 awplays49 · Jan 08, 2016 at 02:53 AM · shadercg

How to make diffuse shader that ignores position of directional light, light other shaders?

My code is for a borderlands shader. Works great, until I move the sphere away...

 Shader "SFX/Toon Outline" {
     
     Properties {
         _Color ("Main Color", Color) = (1, 1, 1, 1)
         _Shadow ("Shadow Strength", Range (0, 1)) = 0.5
         _Threshold ("Shadow Threshold", Range (0, 1)) = 0.5
         _Diffuse ("Diffuse Amount", Range (0, 1)) = 0.5
         _Color3 ("Outline Color", Color) = (0, 0, 0, 1)
         _Outline ("Outline width", Range (.002, 0.03)) = .005
         _MainTex ("Base (RGB)", 2D) = "white" { }
     }
     
     CGINCLUDE
     #include "UnityCG.cginc"
     
     float _Outline;
     float4 _Color;
     float4 _Color2;
     float4 _Color3;
 
 
     ENDCG
 
     SubShader
     {
         Tags
         {
             "RenderType" = "Opaque"
             "LightMode" = "ForwardBase"
         }
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             float _Shadow;
             float _Threshold;
             float _Diffuse;
             sampler2D _MainTex;
             float4 _MainTex_ST;
             float4 _LightColor0;
 
             struct vertexInput {
                 float4 pos : POSITION;
                 float3 norm : NORMAL;
                 float4 tex : TEXCOORD0;
             };
 
             struct vertexOutput {
                 float4 pos : POSITION;
                 float4 col : COLOR;
                 float4 tex : TEXCOORD0;
             };
 
             vertexOutput vert (vertexInput input) {
                 float3 normalDirection = normalize (mul (float4 (input.norm, 0), _World2Object)).xyz;
                 float3 lightDirection = normalize (_WorldSpaceLightPos0).xyz;
                 float3 diffuseReflection = dot (normalDirection, lightDirection);
                 float3 finalColor = diffuseReflection * _Color.rgb + UNITY_LIGHTMODEL_AMBIENT;
                 vertexOutput output;
                 output.pos = mul (UNITY_MATRIX_MVP, input.pos);
                 output.col = float4 (finalColor, 1);
                 output.tex = input.tex;
                 output.tex.xy = TRANSFORM_TEX (input.tex, _MainTex);
                 return output;
             }
 
             float4 frag (vertexOutput output) : COLOR {
                 float4 col = output.col;
                 if ((col.r + col.g + col.b) / 3 <= _Threshold)
                 {
                     col = float4 (saturate (_Color.x - _Shadow), saturate (_Color.y - _Shadow), saturate (_Color.z - _Shadow), 1);
                 }
                 else
                 {
                     col = _Color;
                 }
                 float4 finalCol = lerp (col, output.col, _Diffuse) * tex2D (_MainTex, output.tex);
                 return finalCol;
             }
             ENDCG
         }
 
         Pass
         {
             Cull Front
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             struct vertexInput {
                 float4 pos : POSITION;
                 float3 norm : NORMAL;
             };
 
             struct vertexOutput {
                 float4 pos : POSITION;
                 fixed4 color : COLOR;
             };
 
             vertexOutput vert (vertexInput input) {
                 vertexOutput output;
                 output.pos = mul (UNITY_MATRIX_MVP, input.pos);
                 float3 norm = normalize (mul ((float3x3) UNITY_MATRIX_MV, input.norm));
                 float2 offset = TransformViewToProjection (norm.xy);
                 output.pos.xy += offset * _Outline;
                 output.color = _Color3;
                 return output;
             }
 
             float4 frag (vertexOutput output) : COLOR {
                 return output.color;
             }
             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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Jan 08, 2016 at 04:49 AM

Taking a note from https://en.wikibooks.org/wiki/Cg_Programming/Unity/Lighting_of_Bumpy_Surfaces:

 if (0.0 == _WorldSpaceLightPos0.w) // directional light?
 // ...
 else // point or spot light

Your dilemma's a bit unclear, but if your main goal is to differentiate between directional lights and other lights, this might help get you going in the right direction.

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.

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

40 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

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

UnityObjectToClipPos is inverted? 0 Answers

Sizeof float in cg shader 1 Answer

Objects Go Into Shadow as Soon as They are Outside of a Point Light's Range 1 Answer

Selectively drawing textures on a mesh to represent damage using a 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