- Home /
How can I make rim lighting appear in front of everything?
I'm experimenting with rim lighting using a shader from the Unity Surface Shader Examples page. I would like to be able to have the rim light appear in front of all other objects so that it works as a highlighting system like in the pic below:
I would like to add a separate pass to the below shader where I only want to have the rim lighting. This way I can turn the ZWrite "On" and the ZTest to "Always" only for the rim lighting, and have it in front of everything but not the entire character. To ensure I don't increase the drawcalls with the extra pass I'm going to have 2 versions of the shader; one with the extra rim lighting pass and one without, and switch between the two as necessary via script. I've looked at the Outline shader on the Unity Wiki but it's process is a little too performance heavy so I'm trying to simplify things via rim lighting and avoid the complex calculations used in that one. Here is the shader code I'm using:
Shader "Example/Rim" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)
_RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
}
SubShader {
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 viewDir;
};
sampler2D _MainTex;
sampler2D _BumpMap;
float4 _RimColor;
float _RimPower;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow (rim, _RimPower);
}
ENDCG
}
Fallback "Diffuse"
}
How can I add an extra pass for the rim lighting?
I ended up actually finding a way to do this. I used a second mesh parented to my character and that way I could focus on only the rim light part of the shader. I also added transparency controlled by the rim light intensity so anything without rim light on it is transparent, a very nice effect. To get the shader to appear "in front of everything" I simply added a pass with ZWrite "On" and the ZTest set to "Always". Also if you only want the shader to appear when its behind something use ZTest Greater ins$$anonymous$$d.
Answer by tanoshimi · Jan 02, 2014 at 09:15 AM
You can't separate the logic of surface shaders into separate passes; you can think of surface shaders as a bit like shader "wizards" - they provide a template and simplify the coding required to create many common shader effects, but at the expense of fine control over how the resulting code is structured.
If you want to create multiple pass shaders you'll have to learn vertex/fragment coding: http://docs.unity3d.com/Documentation/Components/SL-ShaderPrograms.html
Ah ok, well that's a bummer. Looks like I'm going to have to do things a different way then.
Your answer
Follow this Question
Related Questions
How can I have the camera forward in a vertex shader? 0 Answers
create a texture with shader code? ( without using a texture ) 1 Answer
vertex displacement shader and ssao 0 Answers
Depth mask with alpha / adding alpha to frag grabpass shader 0 Answers
How to make a 2d shader with edge waves 0 Answers