shadow.sha 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //Cg
  2. void vshader(float4 vtx_position : POSITION,
  3. float2 vtx_texcoord0 : TEXCOORD0,
  4. float3 vtx_normal : NORMAL,
  5. uniform float4x4 trans_model_to_clip_of_light,
  6. uniform float4x4 mat_modelproj,
  7. uniform float4 mspos_light,
  8. uniform float4 k_ambient,
  9. uniform float4 k_scale,
  10. uniform float k_push,
  11. out float4 l_position : POSITION,
  12. out float2 l_texcoord0 : TEXCOORD0,
  13. out float4 l_shadowcoord : TEXCOORD1,
  14. out float l_smooth : TEXCOORD2,
  15. out float4 l_lightclip : TEXCOORD3)
  16. {
  17. float4 position = vtx_position * k_scale;
  18. // vertex position
  19. l_position = mul(mat_modelproj, position);
  20. // Pass through texture coordinate for main texture.
  21. l_texcoord0 = vtx_texcoord0;
  22. // Calculate the surface lighting factor.
  23. l_smooth = saturate(dot(vtx_normal, normalize(mspos_light.xyz - position.xyz)));
  24. // Calculate light-space clip position.
  25. float4 pushed = position + float4(vtx_normal * k_push, 0);
  26. l_lightclip = mul(trans_model_to_clip_of_light, pushed);
  27. // Calculate shadow-map texture coordinates.
  28. l_shadowcoord = l_lightclip * float4(0.5, 0.5, 0.5, 1.0) +
  29. l_lightclip.w * float4(0.5, 0.5, 0.5, 0.0);
  30. }
  31. void fshader(in float2 l_texcoord0 : TEXCOORD0,
  32. in float4 l_shadowcoord : TEXCOORD1,
  33. in float l_smooth : TEXCOORD2,
  34. in float4 l_lightclip : TEXCOORD3,
  35. uniform sampler2D tex_0 : TEXUNIT0,
  36. uniform sampler2DShadow k_Ldepthmap : TEXUNIT1,
  37. uniform float4 k_ambient,
  38. uniform float4 k_texDisable,
  39. out float4 o_color : COLOR)
  40. {
  41. float3 circleoffs = float3(l_lightclip.xy / l_lightclip.w, 0);
  42. float falloff = saturate(1.0 - dot(circleoffs, circleoffs));
  43. float4 baseColor = saturate(tex2D(tex_0, l_texcoord0) + k_texDisable);
  44. float shade = shadow2DProj(k_Ldepthmap, l_shadowcoord).r;
  45. o_color = baseColor * (falloff * shade * l_smooth + k_ambient.x);
  46. }