• 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
Question by Xatoku · Jan 21, 2012 at 08:19 PM · shaderlightingsizeintensityramp

Shader: Changing a Ramp Size

Hey, I'm trying to have the Toon Lighted Shader increase/decrease the size of the ramp depending on the main light's intensity. Any ideas how I'd go about this? I'm considering changing the line below to multiply by something, but I'm not sure what.

 half d = dot (s.Normal, lightDir)*0.5 + 0.5;

^ This line because when I mess with the 0.5 numbers, it increases/decreases the ramp size.

 Shader "Toon/Lighted" {
     Properties {
         _Color ("Main Color", Color) = (0.5,0.5,0.5,1)
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {} 
     }
 
     SubShader {
        Tags {"Queue"="Transparent" "RenderType"="Transparent"}
        Blend SrcAlpha OneMinusSrcAlpha
        LOD 200
 
 CGPROGRAM
 #pragma surface surf ToonRamp
 
 sampler2D _Ramp;
 
 // custom lighting function that uses a texture ramp based
 // on angle between light direction and normal
 #pragma lighting ToonRamp exclude_path:prepass
 inline half4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
 {
     #ifndef USING_DIRECTIONAL_LIGHT
     lightDir = normalize(lightDir);
     #endif
 
     half d = dot (s.Normal, lightDir)*0.5 + 0.5;
     half3 ramp = tex2D (_Ramp, float2(d,d)).rgb;
 
     half4 c;
     c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);
     c.a = s.Alpha;
     return c;
 }
 
 
 sampler2D _MainTex;
 float4 _Color;
 
 struct Input {
     float2 uv_MainTex : TEXCOORD0;
 };
 
 void surf (Input IN, inout SurfaceOutput o) {
     half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
     o.Albedo = c.rgb;
     o.Alpha = c.a;
 }
 ENDCG
 
     } 
 
     Fallback "Diffuse"
 }
Comment

People who like this

0 Show 0
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
Best Answer

Answer by Owen-Reynolds · Jan 22, 2012 at 05:39 PM

You're on the right track. If you know what the old code was doing, makes it easier to modify. The code above looks up a 0-1 ramp value from the White/Black Ramp texture. You're allowed to not look it up, and instead just compute the ramp. In the old code, these two lines compute ramp:

 half d = dot (s.Normal, lightDir)*0.5 + 0.5;
 half3 ramp = tex2D (_Ramp, float2(d,d)).rgb; // we can skip this

The dot-product of your normal and the lightDir computes the percent of light you should get: 1 if you are facing, 0.7 for 45 degree angled away, 0 for an edge, and -1 for facing away. The range is -1 to 1. It's a really common value in lighting math.

Texture lookups think 0 is the left side and 1 is the right. So, DP*0.5+0.5 scales the -1 to 1 dot-prod into a 0-1 for the lookup. Since we aren't doing a texture lookup, we can ignore that. In math, a ramp looks like this (imagine low is black and high is white, so this represents black on the left, a short ramp to white, then white on the right):

 1             ------------
              /
             /
 0 -----------
   -1                      1  <- dot prod

Assume we have the raw -1 to 1: half d = dot(s.Normal, lightDir); A general formula for a ramp is:

 ramp = (d-CENTER)*SPEED+0.5;
 ramp = Clamp(ramp, 0, 1); // this flattens the left/right sides

For CENTER, 0 means the lightwise edge, positive (up to one) brings it towards the light, negative (down to -1) brings it to the unlit side. Interesting numbers for SPEED are 1 (very wide) to huge. Of course, anything more than about 20 will be almost an instant change between colors. The 0.5 makes CENTER be the center (without it, CENTER is the left edge, and changing SPEED makes it grow to the right. With the 0.5, changing SPEED grows both ways.)

For example, ramp=(d - -0.25)*10 + 0.5; would make a very small/sharp ramp (the 10 makes it change quickly,) which is centered towards the backside of the model (0 is the edge, so -0.25 is towards the backside.) You could also incorporate the *2 (in atten*2) into here, saving a math step.

Comment
Xatoku
Lo0NuhtiK

People who like this

2 Show 5 · 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
avatar image Xatoku · Jan 22, 2012 at 10:09 PM 0
Share

Okay, that helps a bit. I'm guessing I'd do something like:

 half d = dot (s.Normal, lightDir)* ((0.4 + 0.4) * lightIntensity);

Now the problem is what to multiply it by. Any ideas?

avatar image Owen-Reynolds · Jan 23, 2012 at 06:48 AM 0
Share

Not that exactly -- that's just DOT*0.8*light. The trick is to compute a new value for the ramp variable, using dot(...) times something and plus something. Times grows/shrinks it, plus shifts it.

As to get the light intensity? There isn't nomrally such a thing. You'd have to set up zones (like in the smokey tavern, it's 1/2.)

avatar image Xatoku · Jan 23, 2012 at 12:25 PM 0
Share

How about the colour of the light? Would that be more conventional?

avatar image Owen-Reynolds · Jan 23, 2012 at 05:44 PM 0
Share

When you wrote "main light" I assumed you meant the one direction light. It's always got the same stats everywhere. It you want to check the closest point light...seems doable, but not my area.

The other thing you could do is simply make different ramp textures. One is for most everywhere. A few triggerboxes (the boss zone?) swap the texture in/out when you enter/leave.

avatar image Xatoku · Jan 23, 2012 at 05:54 PM 0
Share

Hah, again I overlook the simplest solution. That will more than suffice, and thanks for the information none the less!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Prevent light combining with other lights 0 Answers

Making shader's first pass light sensitive (2D) 0 Answers

Custom (Sprite-)Shader: Combine Sprite with light and original Sprite 0 Answers

Cel Shading using a ramp/reference texture 1 Answer

Triplanar surface shader not responding to Mesh.Colors 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