Skip to content

Instantly share code, notes, and snippets.

@onotchi
Created August 1, 2018 08:20
Show Gist options
  • Save onotchi/32a5bc37df89d8bd9a38ddc0e8bedeef to your computer and use it in GitHub Desktop.
Save onotchi/32a5bc37df89d8bd9a38ddc0e8bedeef to your computer and use it in GitHub Desktop.
Planar手法でデカールを表示するシェーダー
Shader "Onoty3D/Unlit/PlanarDecal"
{
Properties
{
_Color("Color", Color) = (1, 1, 1, 1)
_MainTex("MainTex", 2D) = "white" {}
_DecalColor("Decal Color", Color) = (1, 1, 1, 1)
_DecalTex("Decal", 2D) = "white" {}
[KeywordEnum(X, Y, Z)] _Planar ("Decal Planar", int) = 0
[KeywordEnum(Front, Back, Both)] _Side ("Decal Side", int) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#pragma multi_compile _PLANAR_X _PLANAR_Y _PLANAR_Z
#pragma multi_compile _SIDE_FRONT _SIDE_BACK _SIDE_BOTH
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float2 decalUV : TEXCOORD2;
float drawDecal : TEXCOORD3;
};
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _DecalTex;
float4 _DecalTex_ST;
float4 _Color;
float4 _DecalColor;
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
float coord;
#ifdef _PLANAR_X
o.decalUV = (v.vertex.yz - _DecalTex_ST.zw) * _DecalTex_ST.xy;
coord = v.vertex.x;
#elif _PLANAR_Y
o.decalUV = (v.vertex.xz - _DecalTex_ST.zw) * _DecalTex_ST.xy;
coord = v.vertex.y;
#else
o.decalUV = (v.vertex.xy - _DecalTex_ST.zw) * _DecalTex_ST.xy;
coord = v.vertex.z;
#endif
#ifdef _SIDE_FRONT
o.drawDecal = step(0, coord);
#elif _SIDE_BACK
o.drawDecal = step(coord, 0);
#else
o.drawDecal = 1;
#endif
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float4 col = tex2D(_MainTex, i.uv) * _Color;
float4 decal = tex2D(_DecalTex, i.decalUV) * _DecalColor;
col = lerp(col, lerp(col, decal, decal.a), i.drawDecal);
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment