- Home /
Simple Unlit HLSL Shader with Rim Lighting?
Hi!
I'm looking for a way to create Rim Lighting effect on a simple Unlit shader. Everywhere I looked people are using the surface shaders and I'm not familiar with them yet.
Here is my current shader. Is simple Unlit that uses vertex colors.
struct appdata
{
float4 vertex : POSITION;
fixed4 color : COLOR;
};
struct v2f {
float4 pos : SV_POSITION;
fixed4 color : TEXCOORD2;
};
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.color = v.color;
UNITY_TRANSFER_FOG(o, o.pos);
return o;
}
half4 frag (v2f i) : COLOR
{
return i.color;
}
I would like to have this effect:
Where could I find an example for this using standard HLSL shaders instead of the Surf ones.
Thank you!
Answer by laurG · Sep 25, 2017 at 10:48 PM
I found this:
#version 330
uniform mat4 u_pm;
uniform mat4 u_vm;
layout( location = 0 ) out vec4 fragcolor;
in vec3 v_norm;
in vec4 v_pos;
void main() {
vec3 n = normalize(mat3(u_vm) * v_norm); // convert normal to view space, u_vm (view matrix), is a rigid body transform.
vec3 p = vec3(u_vm * v_pos); // position in view space
vec3 v = normalize(-p); // eye vector
float vdn = 1.0 - max(dot(v, n), 0.0); // the rim contribution
fragcolor.a = 1.0;
fragcolor.rgb = vec3(smoothstep(0.6, 1.0, vdn));
}
But I suck at shading languages, been struggling with them for a while and can't seem to get it working.
How can I apply this logic to my shader using Unity?
I finally cracked it:
struct appdata
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float3 normal : NOR$$anonymous$$AL;
};
struct v2f {
float4 pos : SV_POSITION;
float3 normal : NOR$$anonymous$$AL;
float4 posWorld : TEXCOORD0;
fixed4 color : COLOR;
};
v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
o.normal = normalize( mul ( float4(v.normal, 0.0), unity_WorldToObject).xyz);
o.color = v.color;
UNITY_TRANSFER_FOG(o, o.pos);
return o;
}
half4 frag(v2f i) : COLOR
{
float3 normalDir = i.normal;
float3 viewDir = normalize( _WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float rim = 1 - saturate ( dot(viewDir, normalDir) );
float3 rimLight = pow(rim, _Power) * _Color;
return float4( i.color.xyz + rimLight, 1.0f);
}
Your answer
Follow this Question
Related Questions
Set list in shader (uniform) 1 Answer
"Floor" function produces artifacts in shader 1 Answer
2D Water top surface profile 1 Answer
HLSL Post Process Shader Fixing UV/Texcoord 1 Answer
Shader float4 component values computing strangely 0 Answers