Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ROMgame · Jun 05, 2015 at 06:06 AM · shadershaderlabtoontoonshading

Use texture to determine shading?

Currently I am procedurally generating a "ramp texture" containing the four shades that I wish to colour my object with. The idea being that the shader would pick one of the four shades depending upon the intensity of the light.

I hoped that the Toon shader would work for this but unfortunately it seem that it applies additional lighting over the top of the ramp's colours. If the light is bright then the objects colours will be very white and washed out - not great!

The code for the current shader is below:

 Shader "Toon/Lit" {
     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 { "RenderType"="Opaque" }
         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 = 0;
     return c;
 }
 
 
 sampler2D _MainTex;
 
 struct Input {
     float2 uv_MainTex : TEXCOORD0;
 };
 
 void surf (Input IN, inout SurfaceOutput o) {
     half4 c = tex2D(_MainTex, IN.uv_MainTex);
     o.Albedo = c.rgb;
     o.Alpha = c.a;
 }
 ENDCG
 
     } 
 
     Fallback "Diffuse"
 }
 

I've only just started learning ShaderLab so any pointers would be much appreciated - I'm finding it pretty tough so far!

Thanks!

Comment
Add comment · Show 3
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 Glurth · Jun 05, 2015 at 01:25 PM 0
Share

half3 ramp = tex2D (_Ramp, float2(d,d)).rgb;

Hmm, so you have a texture which I will assume is a greyscale, and you want to get a particular pixel on the diagonal line across it's center to effect the color of the object. Is that correct? Note by using float2(d,d) as the coordinate, you will ONLY use the diagonal line across the center of the texture, all other colors in the texture will be ignored.

Simple workaround solution: It is possible you may wish to adjust your _Ramp texture to be non-linear (ramp/curve up to white more slowly). Alternative: have the computed value of "d" ramp up more slowly by squaring it (d=d*d). I would test/play with different options for computing d, e.g. what happens if you don't add 0.5? or if you multiply the final d=d*0.5? Sometimes you end up finding effects that are even more interesting than intended, or useful in other situations.

avatar image ROMgame · Jun 05, 2015 at 03:20 PM 0
Share

So when you use the above shader the colours of the object are not actually the colours of the ramp but the light mixed with the colour of the ramp. So for example if I have a ramp texture which goes from dark green to black and I set my directional light's intensity to "10" then the colour of the object wouldn't be dark green (the lightest shade of the ramp) but would be white.

What I would like is to have a ramp with several set colours which the application can choose from depending upon the intensity of the light. I guess if you think of something like $$anonymous$$onument Valley - (http://cdn0.sbnation.com/assets/4155547/monumentvalley_gdc_long.png) - then it wouldn't be too far off :)

avatar image Glurth · Jun 05, 2015 at 04:01 PM 0
Share

ok, I understood that part, but perhaps I misunderstood the parameters to your shader that define the inco$$anonymous$$g light you are talking about. lightDir and atten I interpreted as a 3d vector, and a light intensity value between 0 and 1, respectively.

What I would like is to have a ramp with several set colours which the application can choose from depending upon the intensity of the light.

I would think that if you made your ramp texture look like a dozen stripes each a distinct shade of grey, rather than a gradient of hundreds of greys, this shader would work as is. If you like particular ramp shade better than another, simply make THAT stripe wider in the texture.

Edit: Ah! I see what I was missing, the atten value is NOT a factor when computing "d". This is why the intensity of the light is not effecting which ramp color is chosen. Perhaps something like this will help.

  half d = dot (s.Normal, lightDir)*0.5 + 0.5;
  d=d*atten; //or is it (atten*2)? like lower in the shader  not sure
  half3 ramp = tex2D (_Ramp, float2(d,d)).rgb;

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Cel Shading Possible In Unity Free Version? 2 Answers

How to get toon Shader to work with terrains - Answered 1 Answer

Does AlphaToMask works in unity ? 0 Answers

Shader mesh not being effected by directional lights? 1 Answer

Toon shader for a cube - outline problem 2 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