- Home /
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.
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
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"
}
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"
}
Your answer
Follow this Question
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