Skip to content

Instantly share code, notes, and snippets.

@sugi-cho
Last active March 29, 2021 01:17
Show Gist options
  • Save sugi-cho/8a3d939427804a6184d1f4132c90c6ac to your computer and use it in GitHub Desktop.
Save sugi-cho/8a3d939427804a6184d1f4132c90c6ac to your computer and use it in GitHub Desktop.
ShadowCastの使い方

Assets/Create/Shader/Unlitで作った、vertex-fragment Shaderで、影を落とす方法。

  • ShadowCaster Passを追加。

    • Name "ShadowCaster"
    • Tags{ "LightMode" = "ShadowCaster" }
  • v2f構造体内でV2F_SHADOW_CASTWER;

    • 内部的には、float4 pos : SV_POSITION;を宣言している
  • vert関数内で、TRANSGER_SHADOW_CASTER(o)

    • opos:o.pos : SV_POSITION
    • hpos : TEXCOORD0;
    • opos = mul(UNITY_MATRIX_MVP, float4(v.vertex.xyz,1));
    • opos = UnityApplyLinearShadowBias(opos);
    • o.hpos = opos;
  • frag関数内で、SHADOW_CASTER_FRAGMENT(i)

    • return 0;するだけでも、影は落ちる。いろんなプラットフォームの差を埋めるやつ?
  • v.vertexは、事前に変形させておく。

  • 各処理のソースは、UnityCG.cginc内をSearchすると分かる

  • マクロの中身を展開してみたら、TRANSFER_SHADOW_CASTER(o)とかの中では、あんましなにもしてないのでは???てかんじになった(unlit-ShadowCast.shader)

    • Biasに対する処理とかをしてる!
Shader "Unlit/unlit-ShadowCast"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
}
CGINCLUDE
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 hpos : TEXCOORD0;
float2 uv : TEXCOORD1;
float4 pos : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert(appdata v)
{
v.vertex.y += 0.5;
v.vertex.xz *= v.vertex.y*v.vertex.y*2;
v.vertex.y -= 0.5;
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.hpos = o.pos;
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
v2f vert_shadow(appdata v){
v2f o;
float4 opos = mul(UNITY_MATRIX_MVP, v.vertex);
o.pos = opos;
opos.z += saturate(unity_LightShadowBias.x/opos.w);
float clamped = max(opos.z, opos.w * 0);
opos.z = lerp(opos.z, clamped, unity_LightShadowBias.y);
o.hpos = opos;
o.uv = v.uv;
return o;
}
fixed4 frag_shadow(v2f i) : SV_Target{
return 0;
}
ENDCG
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
Pass
{
Name "ShadowCaster"
Tags{"LightMode" = "ShadowCaster"}
ZWrite On ZTest LEqual
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
}
Shader "Unlit/UnlitCastShadow"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
}
CGINCLUDE
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float3 deformVertex(float3 original) {
original.y += 0.5;
original.xz *= original.y;
return original;
}
v2f vert(appdata v)
{
v.vertex.xyz = deformVertex(v.vertex.xyz);
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
struct v2f_shadow {
V2F_SHADOW_CASTER;
};
v2f_shadow vert_shadow(appdata v) {
v.vertex.xyz = deformVertex(v.vertex.xyz);
v2f_shadow o;
TRANSFER_SHADOW_CASTER(o)
return o;
}
fixed4 frag_shadow(v2f_shadow i) : SV_Target{
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
SubShader
{
Tags{ "RenderType" = "Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
Pass
{
Name "ShadowCaster"
Tags{ "LightMode" = "ShadowCaster" }
ZWrite On ZTest LEqual
CGPROGRAM
#pragma vertex vert_shadow
#pragma fragment frag_shadow
#pragma multi_compile_shadowcaster
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment