您的位置:首页 > 移动开发 > Unity3D

unity在定制的shader里面如何添加阴影

2014-05-22 21:30 573 查看
下面这段文字是在unity的官方论坛上发现的,下面的shader已经验证过,可以使用。

For the sake of anyone else who is trying to write a fragment shader that receives shadows, I figured it out.

You must do these things:

'#include "AutoLight.cginc"'
'#include "Lighting.cginc"'
Add "Tags {"LightMode" = "ForwardBase"}
'#pragma multi_compile_fwdbase'
Add the Unity macros to your VSOut struct, VS and PS: LIGHTING_COORDS, TRANSFER_VERTEX_TO_FRAGMENT, LIGHT_ATTENUATION.

None of this is documented in the Unity manual.

To access the primary directional light color, unity_LightColor[0] works as long as you don't add "Tags {"LightMode" = "ForwardBase"}". If you do add that line, then it doesn't work: use _LightColor0.rgb instead. Why? Who knows.. probably makes sense to
someone with access to the Unity source code. Which means no one.

Good luck!

-Peter

Shader "Custom/PeterShader2" {
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}

CGINCLUDE

#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "Lighting.cginc"

uniform sampler2D _MainTex;

ENDCG

SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200

Pass
{
Lighting On

Tags {"LightMode" = "ForwardBase"}

CGPROGRAM

#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase

struct VSOut
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD1;
LIGHTING_COORDS(3,4)
};

VSOut vert(appdata_tan v)
{
VSOut o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;

TRANSFER_VERTEX_TO_FRAGMENT(o);

return o;
}

float4 frag(VSOut i) : COLOR
{
float3 lightColor = _LightColor0.rgb;
float3 lightDir = _WorldSpaceLightPos0;
float4 colorTex = tex2D(_MainTex, i.uv.xy * float2(25.0f));
float atten = LIGHT_ATTENUATION(i);
float3 N = float3(0.0f, 1.0f, 0.0f);
float NL = saturate(dot(N, lightDir));

float3 color = colorTex.rgb * lightColor * NL * atten;
return float4(color, colorTex.a);
}

ENDCG
}
}
FallBack "Diffuse"
}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: