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
1
Question by Antoft · Oct 04, 2010 at 11:00 AM · shadercustom-shaderdeferred

toonLighted shader to work with deferred rendered shadows

is it possible render shadows with deferred render, when I'm using this custom version of the toonLighted shader.

Shader "Toon/bumpLighted" { Properties { _Color ("Main Color", Color) = (0.5,0.5,0.5,1) _MainTex ("Base (RGB) Illumin (A)", 2D) = "white" {} _BumpMap ("Normalmap ", 2D) = "bump" {} _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {} }

 SubShader {
     Tags { "RenderType"="Opaque" }
     LOD 400

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; sampler2D _BumpMap; float4 _Color;

struct Input { float2 uv_MainTex : TEXCOORD0; float2 uv_BumpMap; };

void surf (Input IN, inout SurfaceOutput o) { half4 c = tex2D(_MainTex, IN.uv_MainTex) _Color; o.Albedo = c.rgb; // o.Alpha = c.a; o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); o.Emission = c.rgb tex2D(_MainTex, IN.uv_MainTex).a; } ENDCG

 } 

 Fallback "Diffuse"

}

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 3Duaun · Oct 04, 2010 at 01:36 PM 0
Share

I have thought about this as well. Hope an answer comes in some form.

2 Replies

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

Answer by skovacs1 · Oct 04, 2010 at 08:03 PM

Why it won't work the same

As per the docs, a deferred rendering shader's lighting function must have the form:

half4 LightingName_PrePass (SurfaceOutput s, half4 light);

Because the computation of toon lighting is dependent upon the dot product of the surface normal and the light direction (the cosine of the angle between the light and the surface) and pre-pass lighting doesn't provide us with the light direction, we cannot compute the same lighting exactly.

Approximation by clamping luminance (like a posterize)

What pre-pass lighting does provide us with is the pre-calculated lighting values as light.rgb. If we were to use the luminance of the lighting to index a different ramp, you could get something similar (but still not the same) with the addition of something like:

#include "UnityCG.cginc" inline half4 LightingToonRamp_PrePass (SurfaceOutput s, half4 light) { half spec = light.a * s.Gloss; half d = Luminance(light.rgb); half3 ramp = tex2D (_Ramp, float2(d,d)).rgb;

 half4 c;
 c.rgb = s.Albedo * light.rgb * ramp;
 c.a = 0;
 return c;

}

But the ramp used would have to account for luminance rather than cos(angle) *.5 + .5; or you'd have to multiply the luminance by 0.1 and add 0.9 to get a similar look. The problem comes in the fact that the light.rgb has falloff already applied and so you will get a different look around the edges of your light circles by using luminance to index the ramp.

Comment
Add comment · Show 1 · 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 3Duaun · Oct 04, 2010 at 09:26 PM 0
Share

Quite the answer, thank you very much! :-)

avatar image
0

Answer by Antoft · Dec 02, 2010 at 03:42 PM

Modified the code a bit, and got a more predictable solution.

CGPROGRAM #pragma surface surf ToonRamp

sampler2D _Ramp;

// custom lighting function that uses a texture ramp based // on angle between light direction and normal #include "UnityCG.cginc" inline half4 LightingToonRamp_PrePass (SurfaceOutput s, half4 light) {
half spec = light.a s.Gloss; half d = Luminance(light.rgb)0.5; half3 ramp = tex2D (_Ramp, float2(d,d)).rgb; half4 c; c.rgb = s.Albedo ramp (light.rgb * 2); c.a = 0; return c; }

sampler2D _MainTex; sampler2D _BumpMap; float4 _Color;

struct Input { float2 uv_MainTex : TEXCOORD0; float2 uv_BumpMap; };

void surf (Input IN, inout SurfaceOutput o) { half4 c = tex2D(_MainTex, IN.uv_MainTex) _Color; o.Albedo = c.rgb; o.Alpha = c.a; o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); o.Emission = c.rgb tex2D(_MainTex, IN.uv_MainTex).a; } ENDCG

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

No one has followed this question yet.

Related Questions

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

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

access to Lighting Pass in Surface Shader? 0 Answers

Tesselletion "bypassed" by Depth of Field 0 Answers

How can I pass world position to a custom lighting function for a surface shader? 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