Advertisement
Guest User

Untitled

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