Unity里实现Sprite Renderer的阴影

  • 时间:
  • 浏览:2

float4 texcoord : TEXCOORD1;

Pass

return o;

};

Shader "Custom/SpriteDiffuse"

voidOnEnable(){

SubShader {

}

fixed4 color : COLOR;

}

};

float2 uv : TEXCOORD;

_MainTex ("Texture", 2D) = "white" {}

float4 frag(v2f i) : SV_Target

将以下脚本附到产生Shadow的物体上:

{

_MainTex ("Base (RGB)", 2D) = "white" {}

half4 c = tex2D (_MainTex, IN.uv_MainTex);

return o;

}

{

struct Input {

float4 vertex : SV_POSITION;

Tags { "RenderType"="Opaque" }

Shader "Custom/Diffuse2DTexture" {

fixed4 col = tex2D(_MainTex, i.texcoord) * i.color;

}

fixed4 frag (v2f i) : SV_Target

LOD 80

v2f vert(appdata_full v)

CGPROGRAM

{

SHADOW_CASTER_FRAGMENT(i)

void surf (Input IN, inout SurfaceOutput o) {

_AlphaCutOff ("AlphaCutOff", Range(0,1)) = 0.05

ENDCG

v2f o;

v2f vert (appdata v)

Tags {"LightMode"="ShadowCaster"}

}

fixed _AlphaCutOff;

sampler2D _MainTex;

{

}

fixed4 color : COLOR;

float4 vertex : POSITION;

}

o.color = v.color;

Pass

否则这是欠缺的,还不需要 Shader帮忙,下面的Shader请放满产生Shadow的物体上:

o.Alpha = c.a;

};

{

增加光源后来,运行Scene即可见阴影(不运行搞笑的话脚本的内容没人 运行会原应不开启阴影)。

GetComponent().castShadows =true;

sampler2D _MainTex;

}

fixed4 col = tex2D(_MainTex, i.uv) * i.color;

{

此外不需要 另一个Shader,来接受阴影:

GetComponent().receiveShadows =true;

struct appdata

clip(col.a - _AlphaCutOff);

v2f o;

更多unity2018的功能介绍请到paws3d爪爪学院查找。

TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)

struct v2f

FallBack "Diffuse"

CGPROGRAM

sampler2D _MainTex;

o.uv = v.uv;

V2F_SHADOW_CASTER;

ENDCG

{

}

fixed4 color : COLOR;

return col;

SubShader

clip(col.a - _AlphaCutOff);

{

ENDCG

o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);

Properties {

struct v2f {

o.Albedo = c.rgb;

float2 uv : TEXCOORD;

}

{

o.color = v.color;

}

}

}

Properties

};

CGPROGRAM

{

fixed _AlphaCutOff;

o.texcoord = v.texcoord;

{

}

float2 uv_MainTex;

half3 normal : NORMAL;

Tags {"LightMode"="ForwardBase"}