- Home /
Question by
ThatGuyMiniB · Jun 21, 2017 at 02:54 AM ·
shadershadersshader programmingoutline
Need help with outline shader
Hey, everyone, I am very new to shaders and just started learning last week.
Right now I am having troubles with creating an outline shader. This is what I have so far but it isn't working it basically creates a huge black blob around the model lol.
Shader "Custom/OutlineShader"
{
// Variables
Properties
{
_MainTex("Main Texture", 2D) = "white" {}
_AmbientOcclusion("Ambient Occlusion", 2D) = "white" {}
_OutlineWidth("Outline Width", float) = 2
_OutlineColor("Outline Color", color) = (1,1,1,1)
_ZValue("Z Value", float) = -0.4
}
SubShader
{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
Pass
{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vertexFunction
#pragma fragment fragmentFunction
#include "UnityCG.cginc"
struct appdata {
fixed4 vertex : POSITION;
fixed3 normal : NORMAL;
};
struct v2f {
float4 position : SV_POSITION;
};
float _OutlineWidth;
float4 _OutlineColor;
half _ZValue;
v2f vertexFunction(appdata IN)
{
v2f OUT;
OUT.position = UnityObjectToClipPos(IN.vertex);
float3 normal = mul((float3x3)UNITY_MATRIX_IT_MV, IN.normal);
normal.z = _ZValue;
OUT.position = OUT.position + float4(normalize(normal), 0) * _OutlineWidth / 2;
OUT.position = mul(UNITY_MATRIX_P, OUT.position);
return OUT;
}
fixed4 fragmentFunction(v2f IN) : COLOR
{
return _OutlineColor;
}
ENDCG
}
Pass
{
CGPROGRAM
#pragma vertex vertFunction
#pragma fragment frag
struct appdata {
fixed4 vertex : POSITION;
fixed2 uv : TEXCOORD0;
};
struct v2f {
fixed4 position : SV_POSITION;
fixed2 texcoord : TEXCOORD0;
};
sampler2D _MainTex;
sampler2D _AmbientOcclusion;
v2f vertFunction(appdata IN)
{
v2f OUT;
OUT.position = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.uv;
return OUT;
}
fixed4 frag(v2f IN) : SV_Target
{
fixed4 mainTextureColor = tex2D(_MainTex, IN.texcoord);
fixed4 endColor = mainTextureColor;
return endColor;
}
ENDCG
}
}
}
Comment