untiy 3d ShaderLab_第9章_3_球体阴影(三) 点光源对球体的投影
2016-10-26 11:25
260 查看
如图所示,淡黄色的是阴影的淡入淡出,淡紫色的是点光源的阴影。在接受阴影的Shader 中计算了平行光和点光源两种阴影。平行光的:点击,原理是一样的,这里就不多说了。
具体实现的是:
具体实现的是:
Shader "Tut/Shadow/SphereShadow_3" { Properties { _spPos ("Sphere Position", vector) = (0,0,0,1) _spR ("Sphere Radius", float) = 1 _Intensity("Intensity Of Shadow",range(0,1))=0.5 } SubShader { pass{ Tags{"LightMode"="ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" float4 _spPos; float _spR; float _Intensity; float4 _LightColor0; struct v2f{ float4 pos:SV_POSITION; float3 litDir:TEXCOORD0; float3 spDir:TEXCOORD1; float4 vc:TEXCOORD2; }; v2f vert(appdata_base v) { v2f o; o.pos=mul(UNITY_MATRIX_MVP,v.vertex); o.litDir=WorldSpaceLightDir(v.vertex); o.spDir=(_spPos-mul(_Object2World,v.vertex)).xyz; float3 ldir=ObjSpaceLightDir(v.vertex); ldir=normalize(ldir); o.vc=_LightColor0*max(0,dot(ldir,v.normal)); return o; } float4 frag(v2f i):COLOR { float3 litDir=normalize(i.litDir); float3 spDir=i.spDir; float spDistance=length(spDir); spDir=normalize(spDir); float cosV=dot(spDir,litDir); float sinV=sin(acos(max(0,cosV))); float D=sinV*spDistance; float shadow=step(_spR,D);//spR>D 0,else 1 float atten=pow((D/_spR),4); float c=lerp(1-_Intensity,1,min(1,shadow+atten));//0 is dark //*step(0,dot(i.N,litDir)) return i.vc*c; } ENDCG }//endpass pass{ Tags{"LightMode"="ForwardAdd"} Blend One One CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" float4 _spPos; float _spR; float _Intensity; float4 _LightColor0; struct v2f{ float4 pos:SV_POSITION; float3 litDir:TEXCOORD0; float3 spDir:TEXCOORD1; float4 vc:TEXCOORD2; }; v2f vert(appdata_base v) { v2f o; o.pos=mul(UNITY_MATRIX_MVP,v.vertex); o.litDir=WorldSpaceLightDir(v.vertex); o.spDir=(_spPos-mul(_Object2World,v.vertex)).xyz; float3 ldir=ObjSpaceLightDir(v.vertex); float atten=1/(1+length(ldir)); ldir=normalize(ldir); o.vc=_LightColor0*max(0,dot(ldir,v.normal))*atten; return o; } float4 frag(v2f i):COLOR { float litAtten=length(i.litDir); float3 litDir=normalize(i.litDir); float3 spDir=i.spDir; float spDistance=length(spDir); spDir=normalize(spDir); float cosV=dot(spDir,litDir); float sinV=sin(acos(max(0,cosV))); float D=sinV*spDistance; float shadow=step(0,D-_spR);//spR>D 0,else 1 float atten=pow((D/_spR),4); float c=lerp(1-_Intensity,1,min(1,shadow+atten));//0 is dark //*step(0,dot(i.N,litDir))// return c*i.vc; } ENDCG }//endpass } }
相关文章推荐
- untiy 3d ShaderLab_第9章_1_平面阴影(二) 点光源对平面的投影
- untiy 3d ShaderLab_第9章_1_平面阴影(三) 点光源对平面的投影
- untiy 3d ShaderLab_第9章_2_球体阴影(一) 平行光对球体的投影
- untiy 3d ShaderLab_第9章_2_球体阴影(二) 阴影的淡入/淡出
- untiy 3d ShaderLab_第9章_1_平面阴影(一)
- untiy 3d ShaderLab_第6章_VertexLit渲染路径_4_顶点照明和Unity存放光源的第三种方式
- untiy 3d ShaderLab_第7章_ Forward渲染路径_2_Forward渲染路径下的重要光源
- untiy 3d ShaderLab_第6章_VertexLit渲染路径_2_顶点照明和Unity存放光源的第一种方式
- untiy 3d ShaderLab_第6章_VertexLit渲染路径_3_顶点照明和Unity存放光源的第二种方式
- untiy 3d ShaderLab_第7章_ Forward渲染路径_3_重要光源在ForwardAdd内的执行
- untiy 3d ShaderLab_第8章_4_ 单光照贴图在Deferred 渲染路径下的实时阴影
- untiy 3d ShaderLab_第6章_VertexLit渲染路径_4_顶点照明和Unity存放光源的第三种方式
- untiy 3d ShaderLab_第 2 章Unity中Shader(着色器)的形态_2_Unity中Shader的3种形态
- 摄像机,光源和阴影 -----OGRE 3D 1.7 Beginner‘s Guide中文版 第四章
- untiy 3d ShaderLab_第 2 章Unity中Shader(着色器)的形态_3_Shader的数据接口:属性和 uniform变量
- untiy 3d ShaderLab_第4章_基本的光照模型
- untiy 3d ShaderLab_第7章_ Forward渲染路径_1_ForwardBase和ForwardAdd
- untiy 3d ShaderLab_第5章_第一个被执行的Pass_1_不同LightMode 被选择的顺序
- untiy 3d ShaderLab_第5章_第一个被执行的Pass_2_3个渲染路径之外
- untiy 3d ShaderLab_第6章_VertexLit渲染路径_1_顶点照明