Guest User

Untitled

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