.[ ČeskéHry.cz ].

Laboratoř ČeskýchHer.cz - PasteBin

Vložit nový kód

ČeskéHry.cz - KOMUNITA HERNÍCH VÝVOJÁŘŮ

  1. 2025051110h04min28sec
    3 min
  2. 2025051109h53min29sec
    14 min
  3. SS Links
    19 min
  4. 2025051109h42min52sec
    24 min
  5. 2025051109h31min30sec
    36 min
  6. 2025051109h20min53sec
    46 min
  7. 2025051109h09min55sec
    57 min
  8. 2025051108h59min11sec
    1 hod
  9. 2025051108h48min32sec
    1 hod
  10. 2025051108h37min31sec
    1 hod
Link: http://nopaste.ceske-hry.cz/subdom/nopaste222914
Zaslal: PcMaster
Popis: HLSL .fx ShadowMap + PCF
Jazyk: C++
Vloženo: 28.2.2010, 18:09
Stáhnout jako soubor
  1. float Script : STANDARDSGLOBAL <
  2. string UIWidget = "none";
  3. string ScriptClass = "scene";
  4. string ScriptOrder = "postprocess";
  5. string ScriptOutput = "color";
  6. string Script = "Technique=Main;";
  7. > = 0.8;
  8.  
  9. float4 ClearColor <string UIWidget="None";> = {0,0,0,0.0};
  10. float ClearDepth <string UIWidget="None";> = 1.0;
  11. float4 ShadowClearColor <string UIWidget="None";> = {1.0,1.0,1.0,1.0};
  12.  
  13. float4x4 WorldXf : World <string UIWidget="None";>;
  14. float4x4 WorldViewProjXf : WorldViewProjection <string UIWidget="None";>;
  15. float4x4 WorldITXf : WorldInverseTranspose <string UIWidget="None";>;
  16. float4x4 LampViewXf : View <string frustum = "light0";>;
  17. float4x4 LampProjXf : Projection <string frustum = "light0";>;
  18.  
  19. ///////////////////////////////////////////////////////////////
  20. /// TWEAKABLES //////////////////////////////
  21. ///////////////////////////////////////////////////////////////
  22.  
  23. float ShadBias <
  24. string UIWidget = "slider";
  25. float UIMin = -0.1;
  26. float UIMax = +0.1;
  27. float UIStep = 0.00005;
  28. string UIName = "Shadow Bias";
  29. > = 0.0001;
  30.  
  31. float SpotLightCone <
  32. string UIWidget = "slider";
  33. float UIMin = 0.0;
  34. float UIMax = 90.0;
  35. float UIStep = 0.1;
  36. string UIName = "Cone Angle";
  37. > = 45.0f;
  38.  
  39. float3 SpotLightPos : POSITION <
  40. string UIName = "Light Posistion";
  41. string Object = "SpotLight";
  42. string Space = "World";
  43. > = {-1.0f, 1.0f, 0.0f};
  44.  
  45.  
  46. ////////////////////////////////////////////////////////
  47. /// TEXTURES //////////////////////////
  48. ////////////////////////////////////////////////////////
  49.  
  50. #define SHADOW_SIZE 1024
  51.  
  52. texture ShadMap : RENDERCOLORTARGET <
  53. float2 Dimensions = { SHADOW_SIZE, SHADOW_SIZE };
  54. string Format = ("r32f") ;
  55. string UIWidget = "None";
  56. >;
  57.  
  58. sampler ShadSampler = sampler_state {
  59. texture = <ShadMap>;
  60. AddressU = CLAMP;
  61. AddressV = CLAMP;
  62. MipFilter = NONE;
  63. MinFilter = POINT;
  64. MagFilter = POINT;
  65. };
  66.  
  67. texture ShadDepthTarget : RENDERDEPTHSTENCILTARGET <
  68. float2 Dimensions = { SHADOW_SIZE, SHADOW_SIZE };
  69. string format = "D24S8";
  70. string UIWidget = "None";
  71. >;
  72.  
  73. struct VertexIn1 {
  74. float4 Position : POSITION0;
  75. };
  76.  
  77. struct VertexIn2 {
  78. float4 Position : POSITION0;
  79. float3 Normal : NORMAL0;
  80. };
  81.  
  82.  
  83. struct VertexOut1 {
  84. float4 Position : POSITION;
  85. //float4 LightSpacePos : TEXCOORD0;
  86. float Depth : TEXCOORD0;
  87. };
  88.  
  89. struct VertexOut2 {
  90. float4 Position : POSITION;
  91. float4 LightSpacePos : TEXCOORD0;
  92. float4 WorldPos : TEXCOORD1;
  93. float3 Normal : TEXCOORD2;
  94. };
  95.  
  96. // Converts position into light view space
  97. float4 GetPositionFromLight(float4 position) {
  98. float4x4 LightWVP = mul(mul(WorldXf, LampViewXf), LampProjXf);
  99. return mul(position, LightWVP);
  100. }
  101.  
  102. VertexOut1 shadowVS(VertexIn1 IN) {
  103. VertexOut1 OUT = (VertexOut1)0;
  104. OUT.Position = GetPositionFromLight(IN.Position);
  105. //OUT.LightSpacePos = GetPositionFromLight(IN.Position);
  106. OUT.Depth = 1.0 - (OUT.Position.z / OUT.Position.w);
  107. return OUT;
  108. }
  109.  
  110. float4 shadowPS(VertexOut1 IN) : COLOR {
  111. // MS says that this will increase precision and can actually be done in VS
  112. // watch out for "1.0 -" in next pass too :)
  113. // if we do it HERE, the precision is terrible
  114. // if we do it in VS, it is OK :O
  115. // return 1.0 - (IN.LightSpacePos.z / IN.LightSpacePos.w);
  116. return float4(IN.Depth, 0, 0, 1);
  117. }
  118.  
  119. VertexOut2 mainCamVS(VertexIn2 IN) {
  120. VertexOut2 OUT = (VertexOut2)0;
  121. OUT.Position = mul(IN.Position, WorldViewProjXf);
  122. OUT.LightSpacePos = GetPositionFromLight(IN.Position);
  123. OUT.WorldPos = mul(IN.Position, WorldXf);
  124. OUT.Normal = mul(IN.Normal, WorldITXf);
  125. return OUT;
  126. }
  127.  
  128. float4 useShadowPS(VertexOut2 IN) : COLOR {
  129. // calculate the depth of the fragment in the lightspace
  130. float depthFromCurrentDepth = IN.LightSpacePos.z / IN.LightSpacePos.w;
  131.  
  132. // project the fragment into the lightspace and the shadowmap
  133. float sum = 0;
  134. float x, y;
  135. for (y = -1.5; y <= 1.5; y += 1.0) {
  136. for (x = -1.5; x <= 1.5; x += 1.0) {
  137. float2 proj = float2(0.5, -0.5) *
  138. IN.LightSpacePos.xy/IN.LightSpacePos.w +
  139. float2(0.5, 0.5) + float2(x,y) / SHADOW_SIZE;
  140. // get a depth sample from the shadowmap
  141. float s = 1.0 - tex2D(ShadSampler, proj).r;
  142. sum += 1-step(s, depthFromCurrentDepth - ShadBias);
  143. }
  144. }
  145.  
  146. float shadow = sum / 16.0;
  147.  
  148. float3 toLight = normalize(SpotLightPos - IN.WorldPos);
  149. // diffuse lighting
  150. float d = dot(toLight, normalize(IN.Normal));
  151. // spotlight cone fadeout
  152. float3 toLightLightSpace = normalize(-IN.LightSpacePos.xyz/IN.LightSpacePos.w);
  153. float c = dot(toLightLightSpace, float3(0, 0, -1));
  154. return shadow * d * sin(3.14159 * SpotLightCone / 180.0) * c;
  155. }
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164. ////////////////////////////////////////////////////////////////////
  165. /// TECHNIQUES /////////////////////////////////////////////////////
  166. ////////////////////////////////////////////////////////////////////
  167.  
  168. technique Main <
  169. string Script = "Pass=MakeShadow;"
  170. "Pass=UseShadow;";
  171. > {
  172. pass MakeShadow <
  173. string Script = "RenderColorTarget0=ShadMap;"
  174. "RenderDepthStencilTarget=ShadDepthTarget;"
  175. "RenderPort=light0;"
  176. "ClearSetColor=ShadowClearColor;"
  177. "ClearSetDepth=ClearDepth;"
  178. "Clear=Color;"
  179. "Clear=Depth;"
  180. "Draw=geometry;";
  181. > {
  182. VertexShader = compile vs_2_0 shadowVS();
  183. ZEnable = true;
  184. ZWriteEnable = true;
  185. ZFunc = LessEqual;
  186. CullMode = None;
  187. PixelShader = compile ps_2_0 shadowPS();
  188. }
  189. pass UseShadow <
  190. string Script = "RenderColorTarget0=;"
  191. "RenderDepthStencilTarget=;"
  192. "RenderPort=;"
  193. "ClearSetColor=ClearColor;"
  194. "ClearSetDepth=ClearDepth;"
  195. "Clear=Color;"
  196. "Clear=Depth;"
  197. "Draw=geometry;";
  198. > {
  199. VertexShader = compile vs_3_0 mainCamVS();
  200. ZEnable = true;
  201. ZWriteEnable = true;
  202. ZFunc = LessEqual;
  203. CullMode = None;
  204. PixelShader = compile ps_3_0 useShadowPS();
  205. }
  206. }
  207.