Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Unity) Add Outline to Alpha Cutout Shader

I have a very simple Cutout Shader for displaying Icons in 3D space (see below).

I want to 'programatically' add an outline/stroke which follows the alpha contours, with a user defined thickness and colour.

Visual description of desired effect

(Left): What I currently have - an alpha cutout shader (Right): What I want - An outline to go around the cutout Please Note: These are not sprites, they are 3D planes

How can I go about doing this, please?

Shader "Custom/Transparent/CutoutEmissive" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    _EmissiveAmount ("Emissive Amount", Range(0,1)) = 0.5
    _Outline ("Outline Thickness", Range(0,10)) = 0.0
}

SubShader {
    Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    LOD 200

CGPROGRAM
#pragma surface surf Lambert alphatest:_Cutoff

sampler2D _MainTex;
fixed4 _Color;
float _EmissiveAmount;
float _Outline;
struct Input {
    float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    o.Alpha = c.a;

    o.Emission = c.rgb*_EmissiveAmount;
}
ENDCG
}

Fallback "Legacy Shaders/Transparent/Cutout/VertexLit"
}
like image 722
Ben Hayward Avatar asked Oct 18 '22 21:10

Ben Hayward


1 Answers

I know its very late but here it is anyways. I came across this solution where the script applies outline near the cutoff. It is hosted on git hub by José Guerreiro and it did solve my problem with cutoff meshes. The only thing I had to do is add the cutoff meshes to the list and set the color of the line. It has its thickness, intensity and cutoff. The usage is written on page itself. Downside is you will have to have the game in play mode to test this but otherwise it is really nice

Note: This outline works only with cutoff on 3d meshes and sprites; normal meshes it does not work.

Hope this helps.

like image 55
killer_mech Avatar answered Oct 24 '22 15:10

killer_mech