Advertisement
Guest User

Untitled

a guest
Apr 16th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.81 KB | None | 0 0
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Transform.hlsl"
  4. #include "ScreenPos.hlsl"
  5. #include "Lighting.hlsl"
  6. #include "Fog.hlsl"
  7.  
  8. // When rendering a shadowed point light, disable specular calculations on Shader Model 2 to avoid exceeding the instruction limit
  9. #if !defined(SM3) && defined(SHADOW) && defined(POINTLIGHT)
  10. #undef SPECULAR
  11. #endif
  12.  
  13. #ifdef COMPILEPS
  14. Texture2D tWeightMap0 : register(t0);
  15. Texture2DArray tDetailMap : register(t2);
  16. SamplerState sWeightMap0 : register(s0);
  17. SamplerState sDetailMap : register(s2);
  18. #ifdef BUMPMAP
  19. #ifdef MODELNORMAL
  20. Texture2D tModelNormal : register(t1);
  21. SamplerState sModelNormal : register(s1);
  22. #endif
  23. Texture2DArray tNormal : register(t3);
  24. SamplerState sNormal : register(s3);
  25. #endif
  26.  
  27. #endif
  28.  
  29. #ifdef COMPILEVS
  30.  
  31. cbuffer CustomVS : register(b6)
  32. {
  33. float2 cTilingFactors;
  34. };
  35.  
  36.  
  37. #endif
  38.  
  39. float3 combineNormals(float3 n1, float3 n2)
  40. {
  41. float3 t=n1*float3(2,2,2)+float3(-1,-1,0);
  42. float3 u=n2*float3(-2,-2,2)+float3(1,1,-1);
  43. float3 r=t*dot(t,u)/t.z-u;
  44. return r;
  45. }
  46.  
  47. struct VSIn
  48. {
  49. float4 Pos : POSITION;
  50. float2 TexCoord : TEXCOORD0;
  51. #ifdef BUMPMAP
  52. float4 Tangent : TANGENT;
  53. #endif
  54. #ifdef INSTANCED
  55. float4x3 ModelInstance : TEXCOORD2;
  56. #endif
  57. };
  58.  
  59. struct PSIn
  60. {
  61. #ifndef BUMPMAP
  62. float2 TexCoord : TEXCOORD0;
  63. #else
  64. float4 TexCoord : TEXCOORD0;
  65. float4 Tangent : TEXCOORD3;
  66. #ifdef MODELNORMAL
  67. float2 ModelTexCoord : TEXCOORD8;
  68. #endif
  69. #endif
  70. float3 Normal : TEXCOORD1;
  71. float4 WorldPos : TEXCOORD2;
  72. float3 DetailTexCoord : TEXCOORD10;
  73. #ifdef PERPIXEL
  74. #ifdef SHADOW
  75. float4 ShadowPos[NUMCASCADES] : TEXCOORD4;
  76. #endif
  77. #ifdef SPOTLIGHT
  78. float4 SpotPos : TEXCOORD5;
  79. #endif
  80. #ifdef POINTLIGHT
  81. float3 CubeMaskVec : TEXCOORD5;
  82. #endif
  83. #else
  84. float3 VertexLight : TEXCOORD4;
  85. float4 ScreenPos : TEXCOORD5;
  86. #endif
  87. #if defined(D3D11) && defined(CLIPPLANE)
  88. float Clip : SV_CLIPDISTANCE0;
  89. #endif
  90. float4 Pos : SV_POSITION;
  91. };
  92.  
  93. struct PSOut
  94. {
  95. float4 oColor : SV_TARGET0;
  96. float4 oAlbedo : SV_TARGET1;
  97. float4 oNormal : SV_TARGET2;
  98. float oDepth : SV_TARGET3;
  99. };
  100.  
  101.  
  102. #ifdef COMPILEVS
  103. PSIn VS(VSIn vsin)
  104. {
  105. PSIn vsout;
  106.  
  107. #ifdef INSTANCED
  108. float4x3 modelMatrix = in.ModelInstance;
  109. #else
  110. float4x3 modelMatrix = cModel;
  111. #endif
  112. float3 worldPos = mul(vsin.Pos, modelMatrix);
  113. out.Pos = GetClipPos(worldPos);
  114. out.Normal = normalize(mul(in.Normal, (float3x3)modelMatrix));
  115. out.WorldPos = float4(worldPos, GetDepth(out.Pos));
  116. #ifdef BUMPMAP
  117. float3 tangent = normalize(mul(vsin.Tangent.xyz, (float3x3)modelMatrix));
  118. float3 bitangent = cross(tangent, vsout.Normal) * vsin.Tangent.w;
  119. vsout.TexCoord = float4(worldPos.xz*float2(1.0/cTilingFactors.x, 1.0/cTilingFactors.y), bitangent.xy);
  120. vsout.Tangent = float4(tangent, bitangent.z);
  121. #ifdef MODELNORMAL
  122. vsout.ModelTexCoord = vsin.TexCoord;
  123. #endif
  124.  
  125. #else
  126. vsout.TexCoord = GetTexCoord(worldPos.xz*float2(1.0/cTilingFactors.x, 1.0/cTilingFactors.y));
  127. #endif
  128. vsout.DetailTexCoord=worldPos.xyz*0.8;
  129.  
  130. #if defined(D3D11) && defined(CLIPPLANE)
  131. vsout.Clip = dot(vsout.Pos, cClipPlane);
  132. #endif
  133.  
  134. #ifdef PERPIXEL
  135. // Per-pixel forward lighting
  136. float4 projWorldPos = float4(worldPos.xyz, 1.0);
  137.  
  138. #ifdef SHADOW
  139. // Shadow projection: transform from world space to shadow space
  140. GetShadowPos(projWorldPos, oShadowPos);
  141. #endif
  142.  
  143. #ifdef SPOTLIGHT
  144. // Spotlight projection: transform from world space to projector texture coordinates
  145. vsout.SpotPos = mul(projWorldPos, cLightMatrices[0]);
  146. #endif
  147.  
  148. #ifdef POINTLIGHT
  149. vsout.CubeMaskVec = mul(worldPos - cLightPos.xyz, (float3x3)cLightMatrices[0]);
  150. #endif
  151. #else
  152. // Ambient & per-vertex lighting
  153. vsout.VertexLight = GetAmbient(GetZonePos(worldPos));
  154.  
  155. #ifdef NUMVERTEXLIGHTS
  156. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  157. vsout.VertexLight += GetVertexLight(i, worldPos, out.Normal) * cVertexLights[i * 3].rgb;
  158. #endif
  159.  
  160. vsout.ScreenPos = GetScreenPos(out.Pos);
  161. #endif
  162.  
  163. return out;
  164. };
  165. #endif
  166.  
  167. #ifdef COMPILEPS
  168.  
  169. PSOut PS(PSIn psin)
  170. {
  171. PSOut psout;
  172. // Get material diffuse albedo
  173.  
  174. float4 weights0=tWeightMap0.Sample(sWeightMap0, psin.TexCoord.xy);
  175.  
  176.  
  177. float3 nrm = normalize(psin.Normal);
  178. float3 blending=abs(nrm);
  179. blending = normalize(max(blending, 0.00001));
  180. float b=blending.x+blending.y+blending.z;
  181. blending=blending/b;
  182.  
  183.  
  184.  
  185.  
  186. float4 tex1=tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.zy, 4))*blending.x +
  187. tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xy, 4))*blending.z +
  188. tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xz, 0))*blending.y;
  189. float4 tex2=tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.zy, 5))*blending.x +
  190. tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xy, 5))*blending.z +
  191. tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xz, 1))*blending.y;
  192. float4 tex3=tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.zy, 6))*blending.x +
  193. tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xy, 6))*blending.z +
  194. tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xz, 2))*blending.y;
  195. float4 tex4=tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.zy, 7))*blending.x +
  196. tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xy, 7))*blending.z +
  197. tDetailMap.Sample(sDetailMap, float3(psin.DetailTexCoord.xz, 3))*blending.y;
  198.  
  199.  
  200. float ma=max(tex1.a+weights0.r, max(tex2.a+weights0.g, max(tex3.a+weights0.b, tex4.a+weights0.a)))-0.2;
  201. float b1=max(0, tex1.a+weights0.r-ma);
  202. float b2=max(0, tex2.a+weights0.g-ma);
  203. float b3=max(0, tex3.a+weights0.b-ma);
  204. float b4=max(0, tex4.a+weights0.a-ma);
  205.  
  206. float bsum=b1+b2+b3+b4;
  207. float4 diffColor=((tex1*b1+tex2*b2+tex3*b3+tex4*b4)/bsum);
  208.  
  209. float grad=clamp(psin.WorldPos.y*0.125,0,1);
  210. diffColor*=grad;
  211.  
  212. // Get normal
  213. #ifdef BUMPMAP
  214.  
  215.  
  216. float3 bump1=DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.zy, 4)))*blending.x +
  217. DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xy, 4)))*blending.z +
  218. DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xz, 0)))*blending.y;
  219. float3 bump2=DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.zy, 5)))*blending.x +
  220. DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xy, 5)))*blending.z +
  221. DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xz, 1)))*blending.y;
  222. float3 bump3=DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.zy, 6)))*blending.x +
  223. DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xy, 6)))*blending.z +
  224. DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xz, 2)))*blending.y;
  225. float3 bump4=DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.zy, 7)))*blending.x +
  226. DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xy, 7)))*blending.z +
  227. DecodeNormal(tNormal.Sample(sNormal, float3(psin.DetailTexCoord.xz, 3)))*blending.y;
  228.  
  229. float3x3 tbn = float3x3(psin.Tangent.xyz, float3(psin.TexCoord.zw, psin.Tangent.w), psin.Normal);
  230.  
  231. #ifdef MODELNORMAL
  232. float3 texnormal=normalize((bump1*b1+bump2*b2+bump3*b3+bump4*b4)/bsum)*0.5+0.5;
  233. float3 modelnormal=tModelNormal.Sample(sModelNormal, psin.ModelTexCoord.xy).rgb;
  234. float3 normal=normalize(combineNormals(modelnormal, texnormal));
  235. #else
  236. float3 normal=normalize((bump1*b1+bump2*b2+bump3*b3+bump4*b4)/bsum);
  237. #endif
  238.  
  239. normal=normalize(mul(normal, tbn));
  240.  
  241. #else
  242. float3 normal = normalize(iNormal);
  243. #endif
  244.  
  245. float3 specColor = cMatSpecColor.rgb;
  246.  
  247. #ifdef HEIGHTFOG
  248. float fogFactor = GetHeightFogFactor(psin.WorldPos.w, psin.WorldPos.y);
  249. #else
  250. float fogFactor = GetFogFactor(psin.WorldPos.w);
  251. #endif
  252.  
  253. //diffColor=float4(fogFactor,fogFactor,fogFactor,1);
  254. //diffColor=float4(1,1,1,1);
  255.  
  256. #if defined(PERPIXEL)
  257. // Per-pixel forward lighting
  258. float3 lightDir;
  259. float3 lightColor;
  260. float3 finalColor;
  261.  
  262. float diff = GetDiffuse(normal, psin.WorldPos.xyz, lightDir);
  263.  
  264. #ifdef SHADOW
  265. diff *= GetShadow(psin.ShadowPos, psin.WorldPos.w);
  266. #endif
  267.  
  268. #if defined(SPOTLIGHT)
  269. lightColor = psin.SpotPos.w > 0.0 ? Sample2DProj(LightSpotMap, psin.SpotPos).rgb * cLightColor.rgb : 0.0;
  270. #elif defined(CUBEMASK)
  271. lightColor = SampleCube(LightCubeMap, psin.CubeMaskVec).rgb * cLightColor.rgb;
  272. #else
  273. lightColor = cLightColor.rgb;
  274. #endif
  275.  
  276. #ifdef SPECULAR
  277. float spec = GetSpecular(normal, cCameraPosPS - psin.WorldPos.xyz, lightDir, cMatSpecColor.a);
  278. finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  279. #else
  280. finalColor = diff * lightColor * diffColor.rgb;
  281. #endif
  282.  
  283. #ifdef AMBIENT
  284. finalColor += cAmbientColor * diffColor.rgb;
  285. finalColor += cMatEmissiveColor;
  286. psout.oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  287.  
  288. #else
  289. psout.oColor = float4(GetLitFog(finalColor, fogFactor), diffColor.a);
  290. #endif
  291. #elif defined(PREPASS)
  292. // Fill light pre-pass G-Buffer
  293. float specPower = cMatSpecColor.a / 255.0;
  294.  
  295. psout.oColor = float4(normal * 0.5 + 0.5, specPower);
  296. psout.oDepth = psin.WorldPos.w;
  297. #elif defined(DEFERRED)
  298. // Fill deferred G-buffer
  299. float specIntensity = specColor.g;
  300. float specPower = cMatSpecColor.a / 255.0;
  301.  
  302. float3 finalColor = psin.VertexLight * diffColor.rgb;
  303.  
  304. psout.oColor = float4(GetFog(finalColor, fogFactor), 1.0);
  305. psout.oAlbedo = fogFactor * float4(diffColor.rgb, specIntensity);
  306. psout.oNormal = float4(normal * 0.5 + 0.5, specPower);
  307. psout.oDepth = iWorldPos.w;
  308. #else
  309. // Ambient & per-vertex lighting
  310. float3 finalColor = psin.VertexLight * diffColor.rgb;
  311.  
  312. #ifdef MATERIAL
  313. // Add light pre-pass accumulation result
  314. // Lights are accumulated at half intensity. Bring back to full intensity now
  315. float4 lightInput = 2.0 * Sample2DProj(LightBuffer, psin.ScreenPos);
  316. float3 lightSpecColor = lightInput.a * (lightInput.rgb / GetIntensity(lightInput.rgb));
  317.  
  318. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  319. #endif
  320.  
  321. psout.oColor = float4(GetFog(finalColor, fogFactor), diffColor.a);
  322.  
  323. #endif
  324. return psout;
  325. };
  326. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement