본문 바로가기

개발/Graphics, Shader

Unity Matcap Shader

Shader "MatCap"
{
	Properties
	{
		_MainTex("Base (RGB)", 2D) = "white" {}
		_MatCap("MatCap (RGB)", 2D) = "white" {}
	}

	Subshader
	{
		Tags { "RenderType" = "Opaque" }

		Pass
		{
			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#include "UnityCG.cginc"

				struct v2f
				{
					float4 pos	: SV_POSITION;
					float2 uv 	: TEXCOORD0;
					float2 cap	: TEXCOORD1;
				};

				uniform float4 _MainTex_ST;

				v2f vert(appdata_base v)
				{
					v2f o;
					o.pos = UnityObjectToClipPos(v.vertex);
					o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

					float3 worldNorm = UnityObjectToWorldNormal(v.normal);
					worldNorm = mul((float3x3)UNITY_MATRIX_V, worldNorm);
					o.cap.xy = worldNorm.xy * 0.5 + 0.5;

					return o;
				}

				uniform sampler2D _MainTex;
				uniform sampler2D _MatCap;

				float4 frag(v2f i) : COLOR
				{
					float4 tex = tex2D(_MainTex, i.uv);
					float4 mc = tex2D(_MatCap, i.cap);

					mc.rgb = tex.rgb * mc.rgb;
					return mc;
				}
			ENDCG
		}
	}

	Fallback "Diffuse"
}

'개발 > Graphics, Shader' 카테고리의 다른 글

Phong vs Blinn-Phong  (0) 2021.04.22
초간단 흑백쉐이더 (GrayScale)  (0) 2020.10.25
노말맵이 파란이유  (0) 2020.09.17
난반사, 정반사, 주변광 (Diffuse, Specular, Ambient)  (0) 2020.08.12
공간 변환  (0) 2020.08.03