• 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 xeophin · May 24, 2010 at 08:24 AM · shadercustom-shader

Turning Rim Light Shader into a Rim Line Shader

I'd like to create a toon-like look for my project and figured I could modify the rim light shader found on the wiki so it darkened the rims instead of lighting them.

However, since I haven't worked with ShaderLab yet, I seem to be unable to make the necessary changes to the shader. Maybe someone could hive me some hints how to change the mode the rim light is factored in?

I guess the change should occur around here:

half4 frag (v2f i) : COLOR { half3 tangentSpaceNormal = (tex2D(_BumpMap, i.uv).rgb * 2.0) - 1.0;

        half4 result = float4(0, 0, 0, 1);

        //You might want to normalize tangentSpaceNormal and i.tangentSpaceLightDir,
        //but for most meshes this will most likely have minimal, if any, impact on quality.
        float rampSample = dot(tangentSpaceNormal, i.tangentSpaceLightDir);
        float intensity = tex2D(_RimLightRamp, rampSample.xx).r;


        result.rgb = intensity * _RimLightColor.rgb;
        result.rgb += tex2D(_MainTex, i.uv).rgb * _PPLAmbient.rgb;

        return result;
     }

Just where? And how?

Comment
Add comment · Show 1
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 xeophin · May 31, 2010 at 04:31 PM 0
Share

If the answer should be clear and obvious, maybe someone could just point me to the appropriate page of the documentation?

1 Reply

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

Answer by JonManatee · Jun 11, 2010 at 12:33 AM

While I'm not familiar with that particular shader, it appears that the lighting contribution for the rim lighting is factored into the output color in the line:

result.rgb = intensity * _RimLightColor.rgb;

The following line:

result.rgb += tex2D(_MainTex, i.uv).rgb * _PPLAmbient.rgb;

is the texture color + a constant ambient contribution

Consider modifying the two lines to be something more like this:

result.rgb = tex2D(_MainTex, i.uv).rgb * _PPLAmbient.rgb;
result.rgb -= intensity * _RimLightColor.rgb;

I'm not sure if CG will complain if any component of result.rgb is < 0, but you could include a check for that before returning result at the end

For more information on CG shaders, use NVIDIA's official reference and tutorial book (free online): http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter01.html

Comment
Add comment · Show 4 · 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 xeophin · Jun 16, 2010 at 03:11 PM 0
Share

Thank you for answering. I'll work through the tutorial and try to get a hang of it.

As for your solution, it is a step into the right direction, though it lacks the intensity I would wish for. But I guess I can figure it out from here on.

avatar image JonManatee · Jun 16, 2010 at 03:53 PM 0
Share

I suppose a quick description of a fragment shader should have been in order. The input parameter in the above code, v2f i, is the information passed from the vertex shader. The output is the color (RBGA), that will be written to the zbuffer. Shader code can often be decomposed as different components/contributions of the overall lighting calculation, such as the ambient, diffuse, and specular. Which is why I identified that one line as the rim component. The tutorial is a great resource for learning more about shaders in general, as it is a very easy read compared to many graphics texts.

avatar image xeophin · Aug 13, 2010 at 09:23 AM 0
Share

I tried your solution out quite some time ago, but was never really happy with it the rim was only very barely visible.

Turns out I didn't find the right switch: upon pumping up the Ambient Light, the outlines are now clearly visible. Exactly what I wanted! Check it out: https://www.dropbox.com/gallery/824021/1/TrustIn$$anonymous$$e?h=9e6a4d#gallery:0 Awesome! Thanks!

avatar image JonManatee · Aug 13, 2010 at 04:45 PM 0
Share

Looks great! might have to try it out myself sometime

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

No one has followed this question yet.

Related Questions

How can I pass world position to a custom lighting function for a surface shader? 1 Answer

Creating a fogless version of a built-in shader 1 Answer

Strange, triangle shaped holes appearing in my applied shader. 1 Answer

Custom shader dissappear on mobile 0 Answers

What's faster? Two draw calls or a black/white alpha blend? 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