Advertisement
Guest User

Untitled

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