1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Unity] Shaderで四角い穴を開ける

Posted at

UnityでShaderを使って四角い穴を開けたい…。
丸い穴を開ける記事は見つけたけど、四角が見当たらなかったので作りました。
べた塗りしか対応してないので、テクスチャ込みなのはまた今度。

環境

Unity 2017.2

Shader

BoxMask.shader
Shader "Custom/BoxMask" {
	Properties {
		_BoxSize("Box Size", Vector) = (1.0, 1.0, 0.0, 0.0)
		_BoxPos("Box Pos", Vector) = (0.5, 0.5, 1.0, 1.0)
		_Color ("Color", Color) = (0.0,0.0,0.0,1.0)
	}
	
	SubShader {
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
		
		ZWrite Off
		Blend SrcAlpha OneMinusSrcAlpha 
	
		Pass {
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
				
			#include "UnityCG.cginc"
			
			fixed4 _BoxSize;
			fixed4 _BoxPos;
			fixed4 _Color;
		
			struct v2f {
				float4 pos : POSITION;
				float2 uv : TEXCOORD0;
			};
			
			v2f vert (appdata_base v)
			{
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.uv = float2(v.texcoord.x, v.texcoord.y);
				return o;
			}
			
			half4  frag (v2f i) : COLOR
			{
				half4 col = _Color;
				fixed4 pos = i.pos;

				if(	pos.x < _BoxPos.x + (_BoxSize.x / 2) && 
					pos.x > _BoxPos.x - (_BoxSize.x / 2) && 
					pos.y < _BoxPos.y + (_BoxSize.y / 2) && 
					pos.y > _BoxPos.y - (_BoxSize.y / 2))
					{
						clip(-1.0);
					}

				return col;
			}
			ENDCG
		}
	}
}

結果

背景が白で、黒い半透明の真ん中に四角い穴を開けてみる。
image.png

スマホゲームのチュートリアルとかで「ここに注目して!」みたいな時に使えるんじゃないかな?

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?