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]
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]
相关文章推荐
- Unity里面为Sprite添加边框写的的shader
- 如何在Windows Server 2008 Core里面添加Role~~~
- VC++里面radio button如何添加成员变量
- 如何在alertiew里面添加进度条
- 如何在WES 7的开发工具ICE里面添加OEM客户信息
- [Unity3D]如何给物体添加阴影效果
- 在DataGrid里面添加Label后,如何给Label赋值
- 如何在Android.mk里面添加大量的源文件
- 如何通过代码向 User 类型的字段里面添加 多个用户
- 牛腩购物18 : 添加商品页面2 。泛型List<T>的使用,linq 的使用, 如何在 DropDownList 里面绑定二级分类(运用linq),fileupload和图片控件的配合使用用来上传图片,try catch
- 如何在visio里面添加数学函数图形
- [VB.NET]我在dataset里新建了一个datatable表,不知道如何把表添加到数据库里面,请指点一下!
- Flex中如何利用seriesFilters属性,添加/删除LineChart图表曲线阴影
- C# 添加Excel 批注、如何在Excel的单元格里面增加批注
- 在装好的xp系统里面如何添加新的硬件设备
- 在GridView里面添加CheckBox,如何实现哪一行被选中
- 在GridView里面添加CheckBox,如何实现哪一行被选中
- 如何往自己编译的opencore里面添加自己的项目。
- NSPredicate里面如何添加NSDate进行筛选?
- [Drupal教程] 如何添加CSS和Javascript定制Drupal7表单