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 muzza74 · Nov 27, 2013 at 07:30 AM · shaderlightingcel shading

Cel Shading using a ramp/reference texture

I'm looking to make a shader that lights objects using a reference position on a texture based on the value of the light.

I want to take the value of the the light then apply this to a texture containing blocks of black-to-white,so I can edit the threshold for the lighting by just changing the positions of the black/grey/white along the x axis of the lookup texture to create "bands" of light around the object instead of the usual gradient.

alt text

Currently I have the following code:

 Shader "Character/Banded Light Ramp Tex" {
     Properties {
         _MainTex ("Texture", 2D) = "white" {}
         _RampTex ("Ramp", 2D) = "white"{}
         _Color ("Color", Color) = (1,1,1,1)
         _LightCutoff("Maximum distance", Float) = 5.0
     }
     SubShader {
         Tags { "RenderType" = "Opaque" }
         CGPROGRAM
         #pragma surface surf WrapLambert
         
         uniform float _LightCutoff;
         sampler2D _RampTex;
         
         half4 LightingWrapLambert (SurfaceOutput s, half3 lightDir, half atten) {
             half NdotL = dot (s.Normal, lightDir);
             //atten = step(_LightCutoff, atten) * atten;
             atten = atten * NdotL;
             
             
             // Find position to lookup in ramp texture
             float2 lookUpPos = (0.5,saturate(atten));
             // Lookup value at position in the ramp texture
             atten = (tex2D(_RampTex, lookUpPos));
             
             atten = atten < 1 - _LightCutoff ? 0 : atten;
             
             if(atten <= 0.5)
             {
                 atten = 0;
             }
             
             half vMax = (max(max(s.Albedo.r, s.Albedo.g), s.Albedo.b));
             half3 colorAdjust = vMax > 0 ? s.Albedo / vMax : 1;
             half4 c;
 //            if(s.Normal.z > 0 && s.Normal.z < 0.15){
 //                atten = 0.5;
 //            }
             //c.rgb = _LightColor0.rgb * atten;
             c.rgb = _LightColor0.rgb * atten * colorAdjust;
             c.a = s.Alpha;
             return c;
         }
     
         struct Input {
             float2 uv_MainTex;
             float3 viewDir;
         };
         
         sampler2D _MainTex;
         half4 _Color;
         void surf (Input IN, inout SurfaceOutput o) {
             o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color;
             
         }
         ENDCG
     }
     Fallback "Diffuse"
 }

This code seems to work, but I end up with the light showing as a square under a point light.

alt text

I'm not sure if I'm using tex2D incorrectly here. The problem seems to be somewhere in the way I'm looking up the position on the ramp texture. This is my first foray into shaders so I may be missing something incredibly obvious.

EDIT: Updated code to represent re-written shader. I had to clamp the ramp texture, and change the filter settings in "reimport" and the shader now works mostly as I would like it to. The shader still gives me the weird square shape, which seems to be related to screen size/the facing of the object relative to the camera. I still don't know why the square is there.

EDIT 2: Figured out what was causing the square! Atten would be equal to the lowest value in the reference ramp texture if the dot of the normal and the light was zero. The atten should be equal to zero in this case. This caused the shader to use the wrong atten value and draw the strange square.

testramp.png (155 B)
lightexample.png (60.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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by muzza74 · Nov 27, 2013 at 04:18 PM

Figured it all out! See edits on main question for more info. Final code is as follows:

 Shader "Character/Banded Light Ramp Tex" {
     Properties {
         _MainTex ("Texture", 2D) = "white" {}
         _RampTex ("Ramp", 2D) = "white"{}
         _Color ("Color", Color) = (1,1,1,1)
         _LightCutoff("Maximum distance", Float) = 5.0
     }
     SubShader {
         Tags { "RenderType" = "Opaque" }
         CGPROGRAM
         #pragma surface surf WrapLambert
         
         uniform float _LightCutoff;
         sampler2D _RampTex;
         
         half4 LightingWrapLambert (SurfaceOutput s, half3 lightDir, half atten) {
             half NdotL = dot (s.Normal, lightDir);
             atten = atten * NdotL;
         
             // Find position to lookup in ramp texture
             float2 lookUpPos = (saturate(atten),saturate(atten));
             // Lookup value at position in the ramp texture
             atten = (tex2D(_RampTex, lookUpPos));
             
             // Get the lowest value in the ramp texture
             float lowVal = tex2D(_RampTex, float2(0,0));
             // Check to see if the attenuation is less than the lowest value in the
             // ramp texture
             if(atten < lowVal)
             {
                 atten = 0;
             }
             
             half vMax = (max(max(s.Albedo.r, s.Albedo.g), s.Albedo.b));
             half3 colorAdjust = vMax > 0 ? s.Albedo / vMax : 1;
             half4 c;
             c.rgb = _LightColor0.rgb * atten * colorAdjust;
             c.a = s.Alpha;
             return c;
         }
     
         struct Input {
             float2 uv_MainTex;
             float3 viewDir;
         };
         
         sampler2D _MainTex;
         half4 _Color;
         void surf (Input IN, inout SurfaceOutput o) {
             o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color;
             
         }
         ENDCG
     }
     Fallback "Diffuse"
 }
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.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Weird lighting with cel shader 1 Answer

How can I get the attenuation value of a spotlight in a shader? 0 Answers

Prevent light combining with other lights 0 Answers

Help with making a water shader receive light 1 Answer

Shining light for mobile? 1 Answer


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