- Home /
Normal length? Outline shader
Hi Im trying to create myself an outline shader based on the one provided in the SteamVR - plugin. (Even if I kind of modified the entire shader by now :)).. Anyhow.. Im having some issue with retaining the "scale"/width of the outline between different meshes/objects.. and I cant really get why this is happening..
As you can see.. both objects/meshes uses the very same material with the very same shader.. however.. they get different "width" of the outline..
Any help would be very much appreciated!
Oh.. guess I should mention that the shader is currently doing some stencil-magic to be able to use "layered"-materials (multiple materials on one object, and yes.. Im aware of the performance hit this might end up with :)).
Shader:
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
Shader "Unlit/Outline"
{
Properties
{
_OutlineColor("Color", Color) = (0,0,0,0)
_OutlineWidth("Width", Range(.0001, 0.1)) = .0005
}
SubShader
{
Tags
{
"RenderType" = "Transparent"
"Queue" = "Transparent"
}
LOD 100
////////////////////
// Base
////////////////////
Pass
{
Tags
{
"LightMode" = "Always"
"Queue" = "Transparent"
}
ColorMask A
Cull Off
ZWrite On
Stencil
{
Ref 1
Comp LEqual
Pass replace
Fail replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _OutlineWidth;
float4 _OutlineColor;
v2f vert(appdata_full v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
UNITY_APPLY_FOG(i.fogCoord, col);
return _OutlineColor;
}
ENDCG
}
////////////////////
// Outline
////////////////////
Pass
{
Tags
{
"LightMode" = "Always"
"Queue" = "Transparent"
}
Cull Back
Blend SrcAlpha OneMinusSrcAlpha
ZWrite On
Stencil
{
Ref 1
Comp notequal
Pass replace
Fail replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _OutlineWidth;
float4 _OutlineColor;
v2f vert(appdata_full v)
{
v2f o;
float3 n = v.normal;
v.vertex.xyz += (n.xyz * _OutlineWidth);
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
UNITY_APPLY_FOG(i.fogCoord, col);
return _OutlineColor;
}
ENDCG
}
}
}
Br, Inx
capture.png
(201.9 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612110939im_/https://answers.unity.com/themes/thub/images/avi.jpg)