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 Santolin · Jul 07, 2012 at 10:07 PM · shadercubemaptoonreflective

Reflective toon (outline+ramp) shader

Hi!

I'm trying to make a reflective toon shader by combining the default toon and reflective shaders. The outline is always rendered, but the shading is only applied once. The code looks like this:

 Shader "Custom/Toon Reflective" {
  Properties {
  _Color ("Main Color", Color) = (0.5,0.5,0.5,1)
  _OutlineColor ("Outline Color", Color) = (0,0,0,1)
  _Outline ("Outline width", Range (.002, 0.03)) = .005
  _MainTex ("Base (RGB)", 2D) = "white" {}
         _BumpMap ("Normal map", 2D) = "bump" {}
  _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
         _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
         _Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
  }
 
  SubShader {
  Tags { "RenderType"="Opaque" }
  UsePass "Toon/Lighted/FORWARD"
  UsePass "Toon/Basic Outline/OUTLINE"
         UsePass "Reflective/Bumped/FORWARD"
  } 
  
  Fallback "Toon/Lighted"
 }

This would only apply the bumped reflective shader. If I changed the order of the first and third passes, it would only apply the toon ramp.

Is it possible to get a reflective, toonramped shader by using the default shaders or would I have to write my own? If it is the case, could anyone point me in the right direction, please?

Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ScroodgeM · Jul 14, 2012 at 09:28 AM

first, "Toon/Lighted/FORWARD" and "Reflective/Bumped/FORWARD" excludes each other. you'll never get a combination of both this way. only one of them will be rendered or you possibly can get an effect of z-fighting. blend options inside passes can describe how to blend them, but by default both draws on top of all.

second, to get both effects you need (and it's much better for shader perfomance) to calculate them in a single pass and draw final color on screen.

toon ramp is calculates in "Toon/Lighted/FORWARD" pass here:

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

and then applied:

c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);

bumped reflection is calculated in "Reflective/Bumped/FORWARD" and applied here:

o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
float3 worldRefl = WorldReflectionVector (IN, o.Normal);
fixed4 reflcol = texCUBE (_Cube, worldRefl);
reflcol *= tex.a;
o.Emission = reflcol.rgb * _ReflectColor.rgb;
o.Alpha = reflcol.a * _ReflectColor.a;

so, to apply both effects you need just write you own pass with both calculations. don't try just copy-paste one to others, usually this don't work.

really you need just to re-write you own lighting model (copy existing for begin) for standard bumped reflective shader, adding toonramp calculations there

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
avatar image
0

Answer by Santolin · Aug 29, 2012 at 02:03 PM

I'm not sure why but I can't upvote you. Anyways, I got it to work:

 Shader "Custom/Reflective toon" {
     Properties {
         _Color ("Main Color", Color) = (1,1,1,1)
         _MainTex ("Texture", 2D) = "white" {}
         _BumpMap ("Normalmap", 2D) = "bump" {}
         _Ramp ("Shading Ramp", 2D) = "gray" {}
         _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
         _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }       
     }
    
     SubShader {
         Tags { "RenderType" = "Opaque" }
         CGPROGRAM
         #pragma surface surf Ramp
 
         sampler2D _Ramp;
 
         half4 LightingRamp (SurfaceOutput s, half3 lightDir, half atten) {
             half NdotL = dot (s.Normal, lightDir);
             half diff = NdotL * 0.5 + 0.5;
             half3 ramp = tex2D (_Ramp, float2(diff)).rgb;
             half4 c;
             c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);
             c.a = s.Alpha;
             return c;
         }
 
         fixed4 _Color;
         fixed4 _ReflectColor;
         
         struct Input {
             float2 uv_MainTex;
             float2 uv_BumpMap;
             float3 worldRefl;
             INTERNAL_DATA
         };
         
         sampler2D _MainTex;
         sampler2D _BumpMap;
         samplerCUBE _Cube;
    
         void surf (Input IN, inout SurfaceOutput o) {
             fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
             fixed4 c = tex * _Color;
             o.Albedo = c.rgb;
     
             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
             
             float3 worldRefl = WorldReflectionVector (IN, o.Normal);
             fixed4 reflcol = texCUBE (_Cube, worldRefl);
             reflcol *= tex.a;
             o.Emission = reflcol.rgb * _ReflectColor.rgb;
             o.Alpha = reflcol.a * _ReflectColor.a;
         }
    
         ENDCG
    
     }
    
     Fallback "Diffuse Bumped"
 }
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
avatar image
0

Answer by o0_ICE_0o · Mar 22, 2016 at 02:05 PM

Refined your code :) @Santolin

  Shader "Custom/Reflective toon" {
        Properties {
      _Color ("Main Color", Color) = (1,1,1,1)
      _MainTex ("Texture", 2D) = "white" {}
      _BumpMap ("Normalmap", 2D) = "bump" {}
      _Ramp ("Shading Ramp", 2D) = "gray" {}
      _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
      _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }       
  }
 
  SubShader {
      Tags { "RenderType" = "Opaque" }
 //Added Double sided normal support
      Cull off
      CGPROGRAM
      #pragma surface surf Ramp
 
      sampler2D _Ramp;
 
      half4 LightingRamp (SurfaceOutput s, half3 lightDir, half atten) {
 //Reduced some lines of code and added better shadow support
           half d = (dot (s.Normal, lightDir)*0.5 + 0.5) * atten;
           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;
      }
 
      fixed4 _Color;
      fixed4 _ReflectColor;
      
      struct Input {
          float2 uv_MainTex;
          float2 uv_BumpMap;
          float3 worldRefl;
          INTERNAL_DATA
      };
      
      sampler2D _MainTex;
      sampler2D _BumpMap;
      samplerCUBE _Cube;
 
      void surf (Input IN, inout SurfaceOutput o) {
          fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
          fixed4 c = tex * _Color;
          o.Albedo = c.rgb;
  
          o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
          
          float3 worldRefl = WorldReflectionVector (IN, o.Normal);
          fixed4 reflcol = texCUBE (_Cube, worldRefl);
          reflcol *= tex.a;
          o.Emission = reflcol.rgb * _ReflectColor.rgb;
          o.Alpha = reflcol.a * _ReflectColor.a;
      }
 
      ENDCG
 
  }
 
  Fallback "Diffuse Bumped"
  }
 
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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

why realtime reflective cubemap code don't work anymore? 1 Answer

Adding Shadows to a Shader 1 Answer

Rendering a G-Buffer for a cubemap 0 Answers

Cubemap on Android 1 Answer

How to get toon Shader to work with terrains - Answered 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