Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.53 KB | None | 0 0
  1. #include "Uniforms.glsl"
  2. #include "Samplers.glsl"
  3. #include "Transform.glsl"
  4. #include "ScreenPos.glsl"
  5. #include "Lighting.glsl"
  6. #include "Fog.glsl"
  7.  
  8. #ifdef BUMPMAP
  9. varying vec4 vTexCoord;
  10. varying vec4 vTangent;
  11. #else
  12. varying vec2 vTexCoord;
  13. #endif
  14.  
  15. //varying vec2 vTexCoord;
  16. varying vec3 vDetailTexCoord;
  17. varying vec3 vNormal;
  18. varying vec4 vWorldPos;
  19. #ifdef PERPIXEL
  20. #ifdef SHADOW
  21. varying vec4 vShadowPos[NUMCASCADES];
  22. #endif
  23. #ifdef SPOTLIGHT
  24. varying vec4 vSpotPos;
  25. #endif
  26. #ifdef POINTLIGHT
  27. varying vec3 vCubeMaskVec;
  28. #endif
  29. #else
  30. varying vec3 vVertexLight;
  31. varying vec4 vScreenPos;
  32. #ifdef ENVCUBEMAP
  33. varying vec3 vReflectionVec;
  34. #endif
  35. #if defined(LIGHTMAP) || defined(AO)
  36. varying vec2 vTexCoord2;
  37. #endif
  38. #endif
  39.  
  40. uniform sampler2D sWeightMap0;
  41. uniform sampler2D sWeightMap1;
  42. uniform sampler2DArray sDetailMap2;
  43.  
  44. #ifdef BUMPMAP
  45. uniform sampler2DArray sNormal3;
  46. #endif
  47.  
  48. #ifdef USEMASKTEXTURE
  49. uniform sampler2D sMask4;
  50. #endif
  51.  
  52. uniform vec3 cDetailTiling;
  53.  
  54. void VS()
  55. {
  56. mat4 modelMatrix = iModelMatrix;
  57. vec3 worldPos = GetWorldPos(modelMatrix);
  58. gl_Position = GetClipPos(worldPos);
  59. vNormal = GetWorldNormal(modelMatrix);
  60. vWorldPos = vec4(worldPos, GetDepth(gl_Position));
  61.  
  62. #ifdef BUMPMAP
  63. vec4 tangent = GetWorldTangent(modelMatrix);
  64. vec3 bitangent = cross(tangent.xyz, vNormal) * tangent.w;
  65. vTexCoord = vec4(GetTexCoord(iTexCoord), bitangent.xy);
  66. vTangent = vec4(tangent.xyz, bitangent.z);
  67. #else
  68. vTexCoord = GetTexCoord(iTexCoord);
  69. #endif
  70.  
  71. vDetailTexCoord = worldPos.xyz / cDetailTiling;
  72.  
  73. #ifdef PERPIXEL
  74. // Per-pixel forward lighting
  75. vec4 projWorldPos = vec4(worldPos, 1.0);
  76.  
  77. #ifdef SHADOW
  78. // Shadow projection: transform from world space to shadow space
  79. for (int i = 0; i < NUMCASCADES; i++)
  80. vShadowPos[i] = GetShadowPos(i, vNormal, projWorldPos);
  81. #endif
  82.  
  83. #ifdef SPOTLIGHT
  84. // Spotlight projection: transform from world space to projector texture coordinates
  85. vSpotPos = cLightMatrices[0] * projWorldPos;
  86. #endif
  87.  
  88. #ifdef POINTLIGHT
  89. vCubeMaskVec = mat3(cLightMatrices[0][0].xyz, cLightMatrices[0][1].xyz, cLightMatrices[0][2].xyz) * (worldPos - cLightPos.xyz);
  90. #endif
  91. #else
  92. // Ambient & per-vertex lighting
  93. #if defined(LIGHTMAP) || defined(AO)
  94. // If using lightmap, disregard zone ambient light
  95. // If using AO, calculate ambient in the PS
  96. vVertexLight = vec3(0.0, 0.0, 0.0);
  97. vTexCoord2 = iTexCoord2;
  98. #else
  99. vVertexLight = GetAmbient(GetZonePos(worldPos));
  100. #endif
  101.  
  102. #ifdef NUMVERTEXLIGHTS
  103. for (int i = 0; i < NUMVERTEXLIGHTS; ++i)
  104. vVertexLight += GetVertexLight(i, worldPos, vNormal) * cVertexLights[i * 3].rgb;
  105. #endif
  106.  
  107. vScreenPos = GetScreenPos(gl_Position);
  108.  
  109. #ifdef ENVCUBEMAP
  110. vReflectionVec = worldPos - cCameraPos;
  111. #endif
  112. #endif
  113. }
  114.  
  115. void PS()
  116. {
  117. // Get material diffuse albedo
  118. vec4 weights0 = texture(sWeightMap0, vTexCoord.xy);
  119. vec4 weights1 = texture(sWeightMap1, vTexCoord.xy);
  120.  
  121. #ifdef USEMASKTEXTURE
  122. float mask=texture(sMask4, vTexCoord.xy).r;
  123. #endif
  124.  
  125. vec3 nrm = normalize(vNormal);
  126. vec3 blending=abs(nrm);
  127. blending = normalize(max(blending, 0.00001));
  128. float b=blending.x+blending.y+blending.z;
  129. blending=blending/b;
  130.  
  131. vec4 tex1=texture(sDetailMap2, vec3(vDetailTexCoord.zy, 0))*blending.x +
  132. texture(sDetailMap2, vec3(vDetailTexCoord.xy, 0))*blending.z +
  133. texture(sDetailMap2, vec3(vDetailTexCoord.xz, 0))*blending.y;
  134. vec4 tex2=texture(sDetailMap2, vec3(vDetailTexCoord.zy, 1))*blending.x +
  135. texture(sDetailMap2, vec3(vDetailTexCoord.xy, 1))*blending.z +
  136. texture(sDetailMap2, vec3(vDetailTexCoord.xz, 1))*blending.y;
  137. vec4 tex3=texture(sDetailMap2, vec3(vDetailTexCoord.zy, 2))*blending.x +
  138. texture(sDetailMap2, vec3(vDetailTexCoord.xy, 2))*blending.z +
  139. texture(sDetailMap2, vec3(vDetailTexCoord.xz, 2))*blending.y;
  140. vec4 tex4=texture(sDetailMap2, vec3(vDetailTexCoord.zy, 3))*blending.x +
  141. texture(sDetailMap2, vec3(vDetailTexCoord.xy, 3))*blending.z +
  142. texture(sDetailMap2, vec3(vDetailTexCoord.xz, 3))*blending.y;
  143. vec4 tex5=texture(sDetailMap2, vec3(vDetailTexCoord.zy, 4))*blending.x +
  144. texture(sDetailMap2, vec3(vDetailTexCoord.xy, 4))*blending.z +
  145. texture(sDetailMap2, vec3(vDetailTexCoord.xz, 4))*blending.y;
  146. vec4 tex6=texture(sDetailMap2, vec3(vDetailTexCoord.zy, 5))*blending.x +
  147. texture(sDetailMap2, vec3(vDetailTexCoord.xy, 5))*blending.z +
  148. texture(sDetailMap2, vec3(vDetailTexCoord.xz, 5))*blending.y;
  149. vec4 tex7=texture(sDetailMap2, vec3(vDetailTexCoord.zy, 6))*blending.x +
  150. texture(sDetailMap2, vec3(vDetailTexCoord.xy, 6))*blending.z +
  151. texture(sDetailMap2, vec3(vDetailTexCoord.xz, 6))*blending.y;
  152. vec4 tex8=texture(sDetailMap2, vec3(vDetailTexCoord.zy, 7))*blending.x +
  153. texture(sDetailMap2, vec3(vDetailTexCoord.xy, 7))*blending.z +
  154. texture(sDetailMap2, vec3(vDetailTexCoord.xz, 7))*blending.y;
  155.  
  156. float ma=max(tex1.a+weights0.r, max(tex2.a+weights0.g, max(tex3.a+weights0.b, max(tex4.a+weights0.a, max(tex5.a+weights1.r, max(tex6.a+weights1.g, max(tex7.a+weights1.b, tex8.a+weights1.a)))))))-0.2;
  157. float b1=max(0, tex1.a+weights0.r-ma);
  158. float b2=max(0, tex2.a+weights0.g-ma);
  159. float b3=max(0, tex3.a+weights0.b-ma);
  160. float b4=max(0, tex4.a+weights0.a-ma);
  161. float b5=max(0, tex5.a+weights1.r-ma);
  162. float b6=max(0, tex6.a+weights1.g-ma);
  163. float b7=max(0, tex7.a+weights1.b-ma);
  164. float b8=max(0, tex8.a+weights1.a-ma);
  165. float bsum=b1+b2+b3+b4+b5+b6+b7+b8;
  166. vec4 diffColor=(tex1*b1+tex2*b2+tex3*b3+tex4*b4+tex5*b5+tex6*b6+tex7*b7+tex8*b8)/bsum;
  167. //vec4 diffColor=tex1;
  168.  
  169. #ifdef USEMASKTEXTURE
  170. diffColor=mix(vec4(1,0.5,0.3, diffColor.a), diffColor, mask);
  171. #endif
  172.  
  173. // Get material specular albedo
  174. vec3 specColor = cMatSpecColor.rgb;
  175.  
  176. // Get normal
  177. #ifdef BUMPMAP
  178. mat3 tbn = mat3(vTangent.xyz, vec3(vTexCoord.zw, vTangent.w), vNormal);
  179.  
  180. vec3 bump1=DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.zy, 0)))*blending.x+
  181. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xy, 0)))*blending.z+
  182. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xz,0)))*blending.y;
  183. vec3 bump2=DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.zy, 1)))*blending.x+
  184. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xy, 1)))*blending.z+
  185. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xz,1)))*blending.y;
  186. vec3 bump3=DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.zy, 2)))*blending.x+
  187. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xy, 2)))*blending.z+
  188. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xz,2)))*blending.y;
  189. vec3 bump4=DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.zy, 3)))*blending.x+
  190. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xy, 3)))*blending.z+
  191. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xz,3)))*blending.y;
  192. vec3 bump5=DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.zy, 4)))*blending.x+
  193. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xy, 4)))*blending.z+
  194. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xz,4)))*blending.y;
  195. vec3 bump6=DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.zy, 5)))*blending.x+
  196. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xy, 5)))*blending.z+
  197. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xz,5)))*blending.y;
  198. vec3 bump7=DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.zy, 6)))*blending.x+
  199. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xy, 6)))*blending.z+
  200. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xz,6)))*blending.y;
  201. vec3 bump8=DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.zy, 7)))*blending.x+
  202. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xy, 7)))*blending.z+
  203. DecodeNormal(texture(sNormal3, vec3(vDetailTexCoord.xz,7)))*blending.y;
  204.  
  205.  
  206. vec3 normal=tbn*normalize(((bump1*b1+bump2*b2+bump3*b3+bump4*b4+bump5*b5+bump6*b6+bump7*b7+bump8*b8)/bsum));
  207.  
  208. #else
  209. vec3 normal = normalize(vNormal);
  210. #endif
  211. //vec3 normal = normalize(vNormal);
  212.  
  213.  
  214. #ifdef HEIGHTFOG
  215. float fogFactor = GetHeightFogFactor(vWorldPos.w, vWorldPos.y);
  216. #else
  217. float fogFactor = GetFogFactor(vWorldPos.w);
  218. #endif
  219.  
  220. #if defined(PERPIXEL)
  221. // Per-pixel forward lighting
  222. // Per-pixel forward lighting
  223. vec3 lightColor;
  224. vec3 lightDir;
  225. vec3 finalColor;
  226.  
  227. float diff = GetDiffuse(normal, vWorldPos.xyz, lightDir);
  228.  
  229. #ifdef SHADOW
  230. diff *= GetShadow(vShadowPos, vWorldPos.w);
  231. #endif
  232.  
  233. #if defined(SPOTLIGHT)
  234. lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
  235. #elif defined(CUBEMASK)
  236. lightColor = textureCube(sLightCubeMap, vCubeMaskVec).rgb * cLightColor.rgb;
  237. #else
  238. lightColor = cLightColor.rgb;
  239. #endif
  240.  
  241. #ifdef SPECULAR
  242. float spec = GetSpecular(normal, cCameraPosPS - vWorldPos.xyz, lightDir, cMatSpecColor.a);
  243. finalColor = diff * lightColor * (diffColor.rgb + spec * specColor * cLightColor.a);
  244. #else
  245. finalColor = diff * lightColor * diffColor.rgb;
  246. #endif
  247.  
  248. #ifdef AMBIENT
  249. finalColor += cAmbientColor.rgb * diffColor.rgb;
  250. finalColor += cMatEmissiveColor.rgb;
  251. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  252. #else
  253. gl_FragColor = vec4(GetLitFog(finalColor, fogFactor), diffColor.a);
  254. #endif
  255. #elif defined(PREPASS)
  256. // Fill light pre-pass G-Buffer
  257. float specPower = cMatSpecColor.a / 255.0;
  258.  
  259. gl_FragData[0] = vec4(normal * 0.5 + 0.5, specPower);
  260. gl_FragData[1] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  261. #elif defined(DEFERRED)
  262. // Fill deferred G-buffer
  263. float specIntensity = specColor.g;
  264. float specPower = cMatSpecColor.a / 255.0;
  265.  
  266. vec3 finalColor = vVertexLight * diffColor.rgb;
  267. #ifdef AO
  268. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  269. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor * diffColor.rgb;
  270. #endif
  271.  
  272. #ifdef ENVCUBEMAP
  273. finalColor += cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  274. #endif
  275. #ifdef LIGHTMAP
  276. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  277. #endif
  278. #ifdef EMISSIVEMAP
  279. finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord.xy).rgb;
  280. #else
  281. finalColor += cMatEmissiveColor;
  282. #endif
  283.  
  284. gl_FragData[0] = vec4(GetFog(finalColor, fogFactor), 1.0);
  285. gl_FragData[1] = fogFactor * vec4(diffColor.rgb, specIntensity);
  286. gl_FragData[2] = vec4(normal * 0.5 + 0.5, specPower);
  287. gl_FragData[3] = vec4(EncodeDepth(vWorldPos.w), 0.0);
  288. #else
  289. // Ambient & per-vertex lighting
  290. vec3 finalColor = vVertexLight * diffColor.rgb;
  291. #ifdef AO
  292. // If using AO, the vertex light ambient is black, calculate occluded ambient here
  293. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * cAmbientColor.rgb * diffColor.rgb;
  294. #endif
  295.  
  296. #ifdef MATERIAL
  297. // Add light pre-pass accumulation result
  298. // Lights are accumulated at half intensity. Bring back to full intensity now
  299. vec4 lightInput = 2.0 * texture2DProj(sLightBuffer, vScreenPos);
  300. vec3 lightSpecColor = lightInput.a * lightInput.rgb / max(GetIntensity(lightInput.rgb), 0.001);
  301.  
  302. finalColor += lightInput.rgb * diffColor.rgb + lightSpecColor * specColor;
  303. #endif
  304.  
  305. #ifdef ENVCUBEMAP
  306. finalColor += cMatEnvMapColor * textureCube(sEnvCubeMap, reflect(vReflectionVec, normal)).rgb;
  307. #endif
  308. #ifdef LIGHTMAP
  309. finalColor += texture2D(sEmissiveMap, vTexCoord2).rgb * diffColor.rgb;
  310. #endif
  311. #ifdef EMISSIVEMAP
  312. finalColor += cMatEmissiveColor * texture2D(sEmissiveMap, vTexCoord.xy).rgb;
  313. #else
  314. finalColor += cMatEmissiveColor;
  315. #endif
  316.  
  317. gl_FragColor = vec4(GetFog(finalColor, fogFactor), diffColor.a);
  318. #endif
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement