Skip to content

Instantly share code, notes, and snippets.

@mattatz
Created January 8, 2015 03:28

Revisions

  1. mattatz created this gist Jan 8, 2015.
    118 changes: 118 additions & 0 deletions StencilSample.shader
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,118 @@
    Shader "Mattatz/StencilSample" {

    Properties {

    _Outline ("Outline Length", Range(0.0, 1.0)) = 0.2

    _Color ("Color", Color) = (0.8, 0.8, 0.8, 1.0)
    _OutlineColor ("Outline Color", Color) = (0.2, 0.2, 0.2, 1.0)

    }

    SubShader {

    Tags {
    "RenderType"="Opaque"
    "Queue"="Transparent"
    }

    LOD 200

    // render model

    Pass {

    Stencil {
    Ref 128
    Comp always
    Pass replace
    }

    Blend SrcAlpha OneMinusSrcAlpha

    CGPROGRAM

    #pragma vertex vert
    #pragma fragment frag

    float4 _Color;

    struct appdata {
    float4 vertex : POSITION;
    float3 normal : NORMAL;
    };

    struct v2f {
    float4 pos : SV_POSITION;
    };

    v2f vert(appdata v) {
    v2f o;

    float4 vert = v.vertex;

    o.pos = mul(UNITY_MATRIX_MVP, vert);

    return o;
    }

    half4 frag(v2f i) : COLOR {
    return _Color;
    }

    ENDCG
    }

    // render outline

    Pass {

    Stencil {
    Ref 128
    Comp NotEqual
    }

    Cull Off
    ZWrite Off

    Blend SrcAlpha OneMinusSrcAlpha

    CGPROGRAM

    #pragma vertex vert
    #pragma fragment frag

    float _Outline;
    float4 _OutlineColor;

    struct appdata {
    float4 vertex : POSITION;
    float3 normal : NORMAL;
    };

    struct v2f {
    float4 pos : SV_POSITION;
    };

    v2f vert(appdata v) {
    v2f o;

    float4 vert = v.vertex;
    vert.xyz += v.normal * _Outline;

    o.pos = mul(UNITY_MATRIX_MVP, vert);

    return o;
    }

    half4 frag(v2f i) : COLOR {
    return _OutlineColor;
    }

    ENDCG
    }

    }

    FallBack "Diffuse"
    }