LoginSignup
12
8

More than 5 years have passed since last update.

[Unity] ステンシルバッファをImageEffectで利用するシンプルなサンプル

Last updated at Posted at 2017-08-29

unity.jpg

ImageEffectで、ステンシルバッファを取り出して利用するシンプルなサンプルを掲載します。

ステンシルを書き込むシェーダー

このシェーダー(マテリアル)をアタッチしたオブジェクトの描画部分をステンシルバッファに書き込みます。
フラグメントシェーダーで赤色を出力していますが、
後ほどImageEffectで上書きされるので、discardしない限り何色でも大丈夫です。

WriteStencil.shader
Shader "Stencil/WriteStencil" {
    Properties{
        _StencilRef("Stencil Reference", Int) = 1
    }
    SubShader{
        Tags{ "RenderType" = "Opaque" "Queue" = "Geometry" }

        Stencil{
            Ref [_StencilRef]
            Comp Always
            Pass Replace
        }

        Pass{

            CGPROGRAM

            #pragma target 3.0
            #pragma vertex vert
            #pragma fragment frag



            struct appdata {
                float4 vertex : POSITION;
            };

            struct v2f {
                float4 pos : SV_POSITION;
            };

            v2f vert(appdata v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                return o;
            }

            half4 frag(v2f i) : SV_Target{
                return half4(1,0,0,1);
            }
            ENDCG
        }
    }
}

ステンシルバッファを利用するImageEffect

スクリプト

[ImageEffectOpaque]を指定しなければ、ステンシルバッファを取り出せませんので注意してください。

StencilPicker.cs
using UnityEngine;

public class StencilPicker : MonoBehaviour {

    [SerializeField] private Shader shader;

    private Material material;

    void Awake() {
        if (material == null) {
            material = new Material(shader);
        }
    }

    [ImageEffectOpaque] // !!!!!!!!!!!!!!
    void OnRenderImage(RenderTexture source, RenderTexture destination) {
        Graphics.Blit(source, destination, material);
    }
}

シェーダー

市松模様を描くシェーダーです。
最終的に、ステンシルの書き込まれている領域でくりぬかれます。

StencilPicker.shader
Shader "ImageEffect/StencilPicker" {
    Properties {
        _MainTex ("Texture", 2D) = "white" {}
        _StencilRef("Stencil Reference", Int) = 1
    }
    SubShader {

        Cull Off
        ZWrite Off
        ZTest Always

        Stencil{
            Ref [_StencilRef]
            Comp Equal
        }

        Pass {

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v) {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            sampler2D _MainTex;

            fixed4 frag (v2f i) : SV_Target {
                fixed4 col = fixed4(frac(i.uv * float2(16, 9)),1,1); // 市松模様
                return col;
            }
            ENDCG
        }
    }
}

結果

ステンシルのリファレンス値を合わせてレンダリングを行えば、以下のような結果になります。

12
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
8