Advertisement
TimeDoctor

Untitled

Jun 10th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.15 KB | None | 0 0
  1. if(depth >= t)
  2. {
  3. bestDepth = depth;
  4. depth -= 2.0 * size;
  5. }
  6.  
  7. depth += size;
  8. }
  9. #endif
  10.  
  11. return bestDepth;
  12. }
  13. #endif
  14.  
  15. vec3 CalcDiffuse(vec3 diffuseAlbedo, vec3 N, vec3 L, vec3 E, float NE, float NL, float shininess)
  16. {
  17. #if defined(USE_OREN_NAYAR) || defined(USE_TRIACE_OREN_NAYAR)
  18. float gamma = dot(E, L) - NE * NL;
  19. float B = 2.22222 + 0.1 * shininess;
  20.  
  21. #if defined(USE_OREN_NAYAR)
  22. float A = 1.0 - 1.0 / (2.0 + 0.33 * shininess);
  23. gamma = clamp(gamma, 0.0, 1.0);
  24. #endif
  25.  
  26. #if defined(USE_TRIACE_OREN_NAYAR)
  27. float A = 1.0 - 1.0 / (2.0 + 0.65 * shininess);
  28.  
  29. if (gamma >= 0.0)
  30. #endif
  31. {
  32. B = max(B * max(NL, NE), EPSILON);
  33. }
  34.  
  35. return diffuseAlbedo * (A + gamma / B);
  36. #else
  37. return diffuseAlbedo;
  38. #endif
  39. }
  40.  
  41. vec3 EnvironmentBRDF(float gloss, float NE, vec3 specular)
  42. {
  43. #if 1
  44. // from http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
  45. v
  46. c4 t = vec4( 1.0/0.96, 0.475, (0.0275 - 0.25 * 0.04)/0.96,0.25 ) * gloss;
  47. t += vec4( 0.0, 0.0, (0.015 - 0.75 * 0.04)/0.96,0.75 );
  48. float a0 = t.x * min( t.y, exp2( -9.28 * NE ) ) + t.z;
  49. float a1 = t.w;
  50. return clamp( a0 + specular * ( a1 - a0 ), 0.0, 1.0 );
  51. #elif 0
  52. // from http://seblagarde.wordpress.com/2011/08/17/hello-world/
  53. return specular + CalcFresnel(NE) * clamp(vec3(gloss) - specular, 0.0, 1.0);
  54. #else
  55. // from http://advances.realtimerendering.com/s2011/Lazarov-Physically-Based-Lighting-in-Black-Ops%20%28Siggraph%202011%20Advances%20in%20Real-Time%20Rendering%20Course%29.pptx
  56. return mix(specular.rgb, vec3(1.0), CalcFresnel(NE) / (4.0 - 3.0 * gloss));
  57. #endif
  58. }
  59.  
  60. float CalcBlinn(float NH, float shininess)
  61. {
  62. #if defined(USE_BLINN) || defined(USE_BLINN_FRESNEL)
  63. // Normalized Blinn-Phong
  64. float norm = shininess * 0.125 + 1.0;
  65. #elif defined(USE_MCAULEY)
  66. // Cook-Torrance as done by Stephen McAuley
  67. // http://blog.selfshadow.com/publications/s2012-shading-course/mcauley/s2012_pbs_farcry3_notes
  68. v2.pdf
  69. float norm = shininess * 0.25 + 0.125;
  70. #elif defined(USE_GOTANDA)
  71. // Neumann-Neumann as done by Yoshiharu Gotanda
  72. // http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf
  73. float norm = shininess * 0.124858 + 0.269182;
  74. #elif defined(USE_LAZAROV)
  75. // Cook-Torrance as done by Dimitar Lazarov
  76. // http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
  77. float norm = shininess * 0.125 + 0.25;
  78. #else
  79. float norm = 1.0;
  80. #endif
  81.  
  82. #if 0
  83. // from http://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/
  84. float a = shininess + 0.775;
  85. return norm * exp(a * NH - a);
  86. #else
  87. return norm * pow(NH, shininess);
  88. #endif
  89. }
  90.  
  91. float CalcGGX(float NH, float gloss)
  92. {
  93. // from http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
  94. float a_sq = exp2(gloss * -13.0 + 1.0);
  95. float d = ((NH * NH) * (a_sq - 1.0) + 1.0);
  96. return a_sq / (d * d);
  97. }
  98.  
  99. float CalcFresnel(float EH)
  100. {
  101. #if 1
  102. // From http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
  103. // not accurate, but fast
  104. return exp2(-10.0 * EH);
  105. #elif 0
  106. // From http://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/
  107. return exp2((-5.55473 * EH - 6.98316) * EH);
  108. #elif 0
  109. float blend = 1.0 - EH;
  110. float blend2 = blend * blend;
  111. blend *= blend2 * blend2;
  112.  
  113. return blend;
  114. #else
  115. return pow(1.0 - EH, 5.0);
  116. #endif
  117. }
  118.  
  119. float CalcVisibility(float NH, float NL, float NE, float EH, float gloss)
  120. {
  121. #if defined(USE_GOTANDA)
  122. // Neumann-Neumann as done by Yoshiharu Gotanda
  123. // http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf
  124. return 1.0 / max(max(NL, NE), EPSILON);
  125. #elif defined(USE_LAZAROV)
  126. // Cook-Torrance as done by Dimitar Lazarov
  127. // http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
  128. float k = min(1.0, gloss + 0.545);
  129. return 1.0 / (k * (EH * EH - 1.0) + 1.0);
  130. #elif defined(USE_GGX
  131.  
  132. float roughness = exp2(gloss * -6.5);
  133.  
  134. // Modified from http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
  135. // NL, NE in numerator factored out from cook-torrance
  136. float k = roughness + 1.0;
  137. k *= k * 0.125;
  138.  
  139. float k2 = 1.0 - k;
  140.  
  141. float invGeo1 = NL * k2 + k;
  142. float invGeo2 = NE * k2 + k;
  143.  
  144. return 1.0 / (invGeo1 * invGeo2);
  145. #else
  146. return 1.0;
  147. #endif
  148. }
  149.  
  150.  
  151. vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float gloss, float shininess)
  152. {
  153. #if defined(USE_GGX)
  154. float distrib = CalcGGX(NH, gloss);
  155. #else
  156. float distrib = CalcBlinn(NH, shininess);
  157. #endif
  158.  
  159. #if defined(USE_BLINN)
  160. vec3 fSpecular = specular;
  161. #else
  162. vec3 fSpecular = mix(specular, vec3(1.0), CalcFresnel(EH));
  163. #endif
  164.  
  165. float vis = CalcVisibility(NH, NL, NE, EH, gloss);
  166.  
  167. return fSpecular * (distrib * vis);
  168. }
  169.  
  170.  
  171. float CalcLightAttenuation(float point, float normDist)
  172. {
  173. // zero light at 1.0, approximating q3 style
  174. // also don't attenuate directional light
  175. float attenuation = (0.5 * nor
  176. Dist - 1.5) * point + 1.0;
  177.  
  178. // clamp attenuation
  179. #if defined(NO_LIGHT_CLAMP)
  180. attenuation = max(attenuation, 0.0);
  181. #else
  182. attenuation = clamp(attenuation, 0.0, 1.0);
  183. #endif
  184.  
  185. return attenuation;
  186. }
  187.  
  188. // from http://www.thetenthplanet.de/archives/1180
  189. mat3 cotangent_frame( vec3 N, vec3 p, vec2 uv )
  190. {
  191. // get edge vectors of the pixel triangle
  192. vec3 dp1 = dFdx( p );
  193. vec3 dp2 = dFdy( p );
  194. vec2 duv1 = dFdx( uv );
  195. vec2 duv2 = dFdy( uv );
  196.  
  197. // solve the linear system
  198. vec3 dp2perp = cross( dp2, N );
  199. vec3 dp1perp = cross( N, dp1 );
  200. vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
  201. vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
  202.  
  203. // construct a scale-invariant frame
  204. float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );
  205. return mat3( T * invmax, B * invmax, N );
  206. }
  207.  
  208. void main()
  209. {
  210. vec3 viewDir, lightColor, ambientColor;
  211. vec3 L, N, E, H;
  212. float NL, NH, NE, EH, attenuation;
  213.  
  214. #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
  215. #if defined(USE_VERT_TANGENT_SPACE)
  216. mat3 tangentToWorld = mat3(var_Tangent.xyz, var_
  217. itangent.xyz, var_Normal.xyz);
  218. viewDir = vec3(var_Normal.w, var_Tangent.w, var_Bitangent.w);
  219. #else
  220. mat3 tangentToWorld = cotangent_frame(var_Normal, -var_ViewDir, var_TexCoords.xy);
  221. viewDir = var_ViewDir;
  222. #endif
  223.  
  224. E = normalize(viewDir);
  225.  
  226. L = var_LightDir.xyz;
  227. #if defined(USE_DELUXEMAP)
  228. L += (texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(0.5)) * u_EnableTextures.y;
  229. #endif
  230. float sqrLightDist = dot(L, L);
  231. #endif
  232.  
  233. #if defined(USE_LIGHTMAP)
  234. vec4 lightmapColor = texture2D(u_LightMap, var_TexCoords.zw);
  235. #if defined(RGBM_LIGHTMAP)
  236. lightmapColor.rgb *= lightmapColor.a;
  237. #endif
  238. #endif
  239.  
  240. vec2 texCoords = var_TexCoords.xy;
  241.  
  242. #if defined(USE_PARALLAXMAP)
  243. vec3 offsetDir = viewDir * tangentToWorld;
  244.  
  245. offsetDir.xy *= -u_NormalScale.a / offsetDir.z;
  246.  
  247. texCoords += offsetDir.xy * RayIntersectDisplaceMap(texCoords, offsetDir.xy, u_NormalMap);
  248. #endif
  249.  
  250. vec4 diffuse = texture2D(u_DiffuseMap, texCoords);
  251.  
  252. #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
  253. #if defined(USE_LIGHTMAP)
  254. lightColor = light
  255. apColor.rgb * var_Color.rgb;
  256. ambientColor = vec3(0.0);
  257. attenuation = 1.0;
  258. #elif defined(USE_LIGHT_VECTOR)
  259. lightColor = u_DirectedLight * var_Color.rgb;
  260. ambientColor = u_AmbientLight * var_Color.rgb;
  261. attenuation = CalcLightAttenuation(float(var_LightDir.w > 0.0), var_LightDir.w / sqrLightDist);
  262. #elif defined(USE_LIGHT_VERTEX)
  263. lightColor = var_Color.rgb;
  264. ambientColor = vec3(0.0);
  265. attenuation = 1.0;
  266. #endif
  267.  
  268. #if defined(USE_NORMALMAP)
  269. #if defined(SWIZZLE_NORMALMAP)
  270. N.xy = texture2D(u_NormalMap, texCoords).ag - vec2(0.5);
  271. #else
  272. N.xy = texture2D(u_NormalMap, texCoords).rg - vec2(0.5);
  273. #endif
  274. N.xy *= u_NormalScale.xy;
  275. N.z = sqrt(clamp((0.25 - N.x * N.x) - N.y * N.y, 0.0, 1.0));
  276. N = tangentToWorld * N;
  277. #else
  278. N = var_Normal.xyz;
  279. #endif
  280.  
  281. N = normalize(N);
  282. L /= sqrt(sqrLightDist);
  283.  
  284. #if defined(USE_SHADOWMAP)
  285. vec2 shadowTex = gl_FragCoord.xy * r_FBufScale;
  286. float shadowValue = texture2D(u_ShadowMap, shadowTex).r;
  287.  
  288. // surfaces not facing the light are always shadowed
  289. s
  290. adowValue *= float(dot(var_Normal.xyz, var_PrimaryLightDir.xyz) > 0.0);
  291.  
  292. #if defined(SHADOWMAP_MODULATE)
  293. //vec3 shadowColor = min(u_PrimaryLightAmbient, lightColor);
  294. vec3 shadowColor = u_PrimaryLightAmbient * lightColor;
  295.  
  296. #if 0
  297. // Only shadow when the world light is parallel to the primary light
  298. shadowValue = 1.0 + (shadowValue - 1.0) * clamp(dot(L, var_PrimaryLightDir.xyz), 0.0, 1.0);
  299. #endif
  300. lightColor = mix(shadowColor, lightColor, shadowValue);
  301. #endif
  302. #endif
  303.  
  304. #if defined(r_lightGamma)
  305. lightColor = pow(lightColor, vec3(r_lightGamma));
  306. ambientColor = pow(ambientColor, vec3(r_lightGamma));
  307. #endif
  308.  
  309. #if defined(USE_LIGHTMAP) || defined(USE_LIGHT_VERTEX)
  310. ambientColor = lightColor;
  311. float surfNL = clamp(dot(var_Normal.xyz, L), 0.0, 1.0);
  312.  
  313. // Scale the incoming light to compensate for the baked-in light angle
  314. // attenuation.
  315. lightColor /= max(surfNL, 0.25);
  316.  
  317. // Recover any unused light as ambient, in case attenuation is over 4x or
  318. // light is below the surface
  319. ambient
  320. olor = clamp(ambientColor - lightColor * surfNL, 0.0, 1.0);
  321. #endif
  322.  
  323. vec3 reflectance;
  324.  
  325. NL = clamp(dot(N, L), 0.0, 1.0);
  326. NE = clamp(dot(N, E), 0.0, 1.0);
  327.  
  328. #if defined(USE_SPECULARMAP)
  329. vec4 specular = texture2D(u_SpecularMap, texCoords);
  330. #else
  331. vec4 specular = vec4(1.0);
  332. #endif
  333.  
  334. specular *= u_SpecularScale;
  335.  
  336. #if defined(r_materialGamma)
  337. diffuse.rgb = pow(diffuse.rgb, vec3(r_materialGamma));
  338. specular.rgb = pow(specular.rgb, vec3(r_materialGamma));
  339. #endif
  340.  
  341. float gloss = specular.a;
  342. float shininess = exp2(gloss * 13.0);
  343.  
  344. #if defined(SPECULAR_IS_METALLIC)
  345. // diffuse is actually base color, and red of specular is metallicness
  346. float metallic = specular.r;
  347.  
  348. specular.rgb = (0.96 * metallic) * diffuse.rgb + vec3(0.04);
  349. diffuse.rgb *= 1.0 - metallic;
  350. #else
  351. // adjust diffuse by specular reflectance, to maintain energy conservation
  352. diffuse.rgb *= vec3(1.0) - specular.rgb;
  353. #endif
  354.  
  355. reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess);
  356.  
  357. #if defined(r_deluxeSpecular) ||
  358. efined(USE_LIGHT_VECTOR)
  359. float adjGloss = gloss;
  360. float adjShininess = shininess;
  361.  
  362. #if !defined(USE_LIGHT_VECTOR)
  363. adjGloss *= r_deluxeSpecular;
  364. adjShininess = exp2(adjGloss * 13.0);
  365. #endif
  366.  
  367. H = normalize(L + E);
  368.  
  369. EH = clamp(dot(E, H), 0.0, 1.0);
  370. NH = clamp(dot(N, H), 0.0, 1.0);
  371.  
  372. #if !defined(USE_LIGHT_VECTOR)
  373. reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, adjGloss, adjShininess) * r_deluxeSpecular;
  374. #else
  375. reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, adjGloss, adjShininess);
  376. #endif
  377. #endif
  378.  
  379. gl_FragColor.rgb = lightColor * reflectance * (attenuation * NL);
  380.  
  381. #if 0
  382. vec3 aSpecular = EnvironmentBRDF(gloss, NE, specular.rgb);
  383.  
  384. // do ambient as two hemisphere lights, one straight up one straight down
  385. float hemiDiffuseUp = N.z * 0.5 + 0.5;
  386. float hemiDiffuseDown = 1.0 - hemiDiffuseUp;
  387. float hemiSpecularUp = mix(hemiDiffuseUp, float(N.z >= 0.0), gloss);
  388. float hemiSpecularDown = 1.0 - hemiSpecularUp;
  389.  
  390. gl_FragColor.rgb += ambientColor * 0.75 * (diffuse.
  391. gb * hemiDiffuseUp + aSpecular * hemiSpecularUp);
  392. gl_FragColor.rgb += ambientColor * 0.25 * (diffuse.rgb * hemiDiffuseDown + aSpecular * hemiSpecularDown);
  393. #else
  394. gl_FragColor.rgb += ambientColor * (diffuse.rgb + specular.rgb);
  395. #endif
  396.  
  397. #if defined(USE_CUBEMAP)
  398. reflectance = EnvironmentBRDF(gloss, NE, specular.rgb);
  399.  
  400. vec3 R = reflect(E, N);
  401.  
  402. // parallax corrected cubemap (cheaper trick)
  403. // from http://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
  404. vec3 parallax = u_CubeMapInfo.xyz + u_CubeMapInfo.w * viewDir;
  405.  
  406. vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w;
  407.  
  408. // normalize cubemap based on lowest mip (~diffuse)
  409. // multiplying cubemap values by lighting below depends on either this or the cubemap being normalized at generation
  410. //vec3 cubeLightDiffuse = max(textureCubeLod(u_CubeMap, N, 6.0).rgb, 0.5 / 255.0);
  411. //cubeLightColor /= dot(cubeLightDiffuse, vec3(0.2125, 0.7154, 0.0721));
  412.  
  413. #i
  414. defined(r_framebufferGamma)
  415. cubeLightColor = pow(cubeLightColor, vec3(r_framebufferGamma));
  416. #endif
  417.  
  418. // multiply cubemap values by lighting
  419. // not technically correct, but helps make reflections look less unnatural
  420. //cubeLightColor *= lightColor * (attenuation * NL) + ambientColor;
  421.  
  422. gl_FragColor.rgb += cubeLightColor * reflectance;
  423. #endif
  424.  
  425. #if defined(USE_PRIMARY_LIGHT)
  426. vec3 L2, H2;
  427. float NL2, EH2, NH2;
  428.  
  429. L2 = var_PrimaryLightDir.xyz;
  430.  
  431. // enable when point lights are supported as primary lights
  432. //sqrLightDist = dot(L2, L2);
  433. //L2 /= sqrt(sqrLightDist);
  434.  
  435. NL2 = clamp(dot(N, L2), 0.0, 1.0);
  436.  
  437. H2 = normalize(L2 + E);
  438. EH2 = clamp(dot(E, H2), 0.0, 1.0);
  439. NH2 = clamp(dot(N, H2), 0.0, 1.0);
  440.  
  441. reflectance = CalcDiffuse(diffuse.rgb, N, L2, E, NE, NL2, shininess);
  442. reflectance += CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, gloss, shininess);
  443.  
  444. lightColor = u_PrimaryLightColor * var_Color.rgb;
  445.  
  446. #if defined(r_lightGamma)
  447. lightColor = pow(lightColor, vec3(r_lightGamma));
  448. #endif
  449.  
  450. #if defi
  451. ed(USE_SHADOWMAP)
  452. lightColor *= shadowValue;
  453. #endif
  454.  
  455. // enable when point lights are supported as primary lights
  456. //lightColor *= CalcLightAttenuation(float(u_PrimaryLightDir.w > 0.0), u_PrimaryLightDir.w / sqrLightDist);
  457.  
  458. gl_FragColor.rgb += lightColor * reflectance * NL2;
  459. #endif
  460. #else
  461. lightColor = var_Color.rgb;
  462.  
  463. #if defined(USE_LIGHTMAP)
  464. lightColor *= lightmapColor.rgb;
  465. #endif
  466.  
  467. #if defined(r_lightGamma)
  468. lightColor = pow(lightColor, vec3(r_lightGamma));
  469. #endif
  470.  
  471. #if defined(r_materialGamma)
  472. diffuse.rgb = pow(diffuse.rgb, vec3(r_materialGamma));
  473. #endif
  474.  
  475. gl_FragColor.rgb = diffuse.rgb * lightColor;
  476.  
  477. #endif
  478.  
  479. #if defined(r_framebufferGamma)
  480. gl_FragColor.rgb = pow(gl_FragColor.rgb, vec3(1.0 / r_framebufferGamma));
  481. #endif
  482.  
  483. gl_FragColor.a = diffuse.a * var_Color.a;
  484. }
  485.  
  486. compile log:
  487. ERROR: 0:534: Invalid call of undeclared identifier 'textureCubeLod'
  488. ERROR: 0:549: Use of undeclared identifier 'cubeLightColor'
  489.  
  490. ********************
  491. ERROR: Couldn't compile shader
  492. ********************
  493. RE_Shutdown( 0 )
  494. Hunk_Clear: reset the hunk ok
  495. ----- R_Init -----
  496. ------- FBO_Init -------
  497. ------- GLSL_InitGPUShaders -------
  498. #version 120
  499. #ifndef M_PI
  500. #define M_PI 3.14159265358979323846
  501. #endif
  502. #ifndef deformGen_t
  503. #define deformGen_t
  504. #define DGEN_WAVE_SIN 1
  505. #define DGEN_WAVE_SQUARE 2
  506. #define DGEN_WAVE_TRIANGLE 3
  507. #define DGEN_WAVE_SAWTOOTH 4
  508. #define DGEN_WAVE_INVERSE_SAWTOOTH 5
  509. #define DGEN_BULGE 7
  510. #define DGEN_MOVE 8
  511. #endif
  512. #ifndef tcGen_t
  513. #define tcGen_t
  514. #define TCGEN_LIGHTMAP 2
  515. #define TCGEN_TEXTURE 3
  516. #define TCGEN_ENVIRONMENT_MAPPED 4
  517. #define TCGEN_FOG 5
  518. #define TCGEN_VECTOR 6
  519. #endif
  520. #ifndef colorGen_t
  521. #define colorGen_t
  522. #define CGEN_LIGHTING_DIFFUSE 11
  523. #endif
  524. #ifndef alphaGen_t
  525. #define alphaGen_t
  526. #define AGEN_LIGHTING_SPECULAR 6
  527. #define AGEN_PORTAL 8
  528. #endif
  529. #ifndef texenv_t
  530. #define texenv_t
  531. #define TEXENV_MODULATE 8448
  532. #define TEXENV_ADD 260
  533. #define TEXENV_REPLACE 7681
  534. #endif
  535. #ifndef r_FBufScale
  536. #define r_FBufScale vec2(0.000521, 0.000926)
  537. #endif
  538. #define r_deluxeSpecular 0.300000
  539. #define SWIZZLE_NORMALMAP
  540. #define RGBM_LIGHTMAP
  541. #define USE_LIGHT
  542. #define USE_LIGHTMAP
  543. #define USE_DELUXEMAP
  544. #define USE_NORMALMAP
  545. #define USE_VERT_T
  546. NGENT_SPACE
  547. #define USE_SPECULARMAP
  548. #define USE_BLINN
  549. #define USE_CUBEMAP
  550. #line 0
  551. uniform sampler2D u_DiffuseMap;
  552.  
  553. #if defined(USE_LIGHTMAP)
  554. uniform sampler2D u_LightMap;
  555. #endif
  556.  
  557. #if defined(USE_NORMALMAP)
  558. uniform sampler2D u_NormalMap;
  559. #endif
  560.  
  561. #if defined(USE_DELUXEMAP)
  562. uniform sampler2D u_DeluxeMap;
  563. #endif
  564.  
  565. #if defined(USE_SPECULARMAP)
  566. uniform sampler2D u_SpecularMap;
  567. #endif
  568.  
  569. #if defined(USE_SHADOWMAP)
  570. uniform sampler2D u_ShadowMap;
  571. #endif
  572.  
  573. #if defined(USE_CUBEMAP)
  574. uniform samplerCube u_CubeMap;
  575. #endif
  576.  
  577. #if defined(USE_NORMALMAP) || defined(USE_DELUXEMAP) || defined(USE_SPECULARMAP) || defined(USE_CUBEMAP)
  578. // y = deluxe, w = cube
  579. uniform vec4 u_EnableTextures;
  580. #endif
  581.  
  582. #if defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
  583. uniform vec3 u_DirectedLight;
  584. uniform vec3 u_AmbientLight;
  585. #endif
  586.  
  587. #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
  588. uniform vec3 u_PrimaryLightColor;
  589. uniform vec3 u_PrimaryLightAmbient;
  590. #endif
  591.  
  592. #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
  593. uniform vec4
  594. u_NormalScale;
  595. uniform vec4 u_SpecularScale;
  596. #endif
  597.  
  598. #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
  599. #if defined(USE_CUBEMAP)
  600. uniform vec4 u_CubeMapInfo;
  601. #endif
  602. #endif
  603.  
  604. varying vec4 var_TexCoords;
  605.  
  606. varying vec4 var_Color;
  607.  
  608. #if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT))
  609. #if defined(USE_VERT_TANGENT_SPACE)
  610. varying vec4 var_Normal;
  611. varying vec4 var_Tangent;
  612. varying vec4 var_Bitangent;
  613. #else
  614. varying vec3 var_Normal;
  615. varying vec3 var_ViewDir;
  616. #endif
  617. #endif
  618.  
  619. #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
  620. varying vec4 var_LightDir;
  621. #endif
  622.  
  623. #if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
  624. varying vec4 var_PrimaryLightDir;
  625. #endif
  626.  
  627.  
  628. #define EPSILON 0.00000001
  629.  
  630. #if defined(USE_PARALLAXMAP)
  631. float SampleDepth(sampler2D normalMap, vec2 t)
  632. {
  633. #if defined(SWIZZLE_NORMALMAP)
  634. return 1.0 - texture2D(normalMap, t).r;
  635. #else
  636. return 1.0 - texture2D(normalMap, t).a;
  637. #endif
  638. }
  639.  
  640. float RayIntersectDisplaceMap(vec2 dp, vec2 ds, sampler2D normalMap)
  641. {
  642. const int
  643. inearSearchSteps = 16;
  644. const int binarySearchSteps = 6;
  645.  
  646. // current size of search window
  647. float size = 1.0 / float(linearSearchSteps);
  648.  
  649. // current depth position
  650. float depth = 0.0;
  651.  
  652. // best match found (starts with last position 1.0)
  653. float bestDepth = 1.0;
  654.  
  655. // texture depth at best depth
  656. float texDepth = 0.0;
  657.  
  658. float prevT = SampleDepth(normalMap, dp);
  659. float prevTexDepth = prevT;
  660.  
  661. // search front to back for first point inside object
  662. for(int i = 0; i < linearSearchSteps - 1; ++i)
  663. {
  664. depth += size;
  665.  
  666. float t = SampleDepth(normalMap, dp + ds * depth);
  667.  
  668. if(bestDepth > 0.996) // if no depth found yet
  669. if(depth >= t)
  670. {
  671. bestDepth = depth; // store best depth
  672. texDepth = t;
  673. prevTexDepth = prevT;
  674. }
  675. prevT = t;
  676. }
  677.  
  678. depth = bestDepth;
  679.  
  680. #if !defined (USE_RELIEFMAP)
  681. float div = 1.0 / (1.0 + (prevTexDepth - texDepth) * float(linearSearchSteps));
  682. bestDepth -= (depth - size - prevTexDepth) * div;
  683. #else
  684. // recurse around first point (depth) for closest match
  685. for(int i = 0; i <
  686. inarySearchSteps; ++i)
  687. {
  688. size *= 0.5;
  689.  
  690. float t = SampleDepth(normalMap, dp + ds * depth);
  691.  
  692. if(depth >= t)
  693. {
  694. bestDepth = depth;
  695. depth -= 2.0 * size;
  696. }
  697.  
  698. depth += size;
  699. }
  700. #endif
  701.  
  702. return bestDepth;
  703. }
  704. #endif
  705.  
  706. vec3 CalcDiffuse(vec3 diffuseAlbedo, vec3 N, vec3 L, vec3 E, float NE, float NL, float shininess)
  707. {
  708. #if defined(USE_OREN_NAYAR) || defined(USE_TRIACE_OREN_NAYAR)
  709. float gamma = dot(E, L) - NE * NL;
  710. float B = 2.22222 + 0.1 * shininess;
  711.  
  712. #if defined(USE_OREN_NAYAR)
  713. float A = 1.0 - 1.0 / (2.0 + 0.33 * shininess);
  714. gamma = clamp(gamma, 0.0, 1.0);
  715. #endif
  716.  
  717. #if defined(USE_TRIACE_OREN_NAYAR)
  718. float A = 1.0 - 1.0 / (2.0 + 0.65 * shininess);
  719.  
  720. if (gamma >= 0.0)
  721. #endif
  722. {
  723. B = max(B * max(NL, NE), EPSILON);
  724. }
  725.  
  726. return diffuseAlbedo * (A + gamma / B);
  727. #else
  728. return diffuseAlbedo;
  729. #endif
  730. }
  731.  
  732. vec3 EnvironmentBRDF(float gloss, float NE, vec3 specular)
  733. {
  734. #if 1
  735. // from http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
  736. v
  737. c4 t = vec4( 1.0/0.96, 0.475, (0.0275 - 0.25 * 0.04)/0.96,0.25 ) * gloss;
  738. t += vec4( 0.0, 0.0, (0.015 - 0.75 * 0.04)/0.96,0.75 );
  739. float a0 = t.x * min( t.y, exp2( -9.28 * NE ) ) + t.z;
  740. float a1 = t.w;
  741. return clamp( a0 + specular * ( a1 - a0 ), 0.0, 1.0 );
  742. #elif 0
  743. // from http://seblagarde.wordpress.com/2011/08/17/hello-world/
  744. return specular + CalcFresnel(NE) * clamp(vec3(gloss) - specular, 0.0, 1.0);
  745. #else
  746. // from http://advances.realtimerendering.com/s2011/Lazarov-Physically-Based-Lighting-in-Black-Ops%20%28Siggraph%202011%20Advances%20in%20Real-Time%20Rendering%20Course%29.pptx
  747. return mix(specular.rgb, vec3(1.0), CalcFresnel(NE) / (4.0 - 3.0 * gloss));
  748. #endif
  749. }
  750.  
  751. float CalcBlinn(float NH, float shininess)
  752. {
  753. #if defined(USE_BLINN) || defined(USE_BLINN_FRESNEL)
  754. // Normalized Blinn-Phong
  755. float norm = shininess * 0.125 + 1.0;
  756. #elif defined(USE_MCAULEY)
  757. // Cook-Torrance as done by Stephen McAuley
  758. // http://blog.selfshadow.com/publications/s2012-shading-course/mcauley/s2012_pbs_farcry3_notes
  759. v2.pdf
  760. float norm = shininess * 0.25 + 0.125;
  761. #elif defined(USE_GOTANDA)
  762. // Neumann-Neumann as done by Yoshiharu Gotanda
  763. // http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf
  764. float norm = shininess * 0.124858 + 0.269182;
  765. #elif defined(USE_LAZAROV)
  766. // Cook-Torrance as done by Dimitar Lazarov
  767. // http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
  768. float norm = shininess * 0.125 + 0.25;
  769. #else
  770. float norm = 1.0;
  771. #endif
  772.  
  773. #if 0
  774. // from http://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/
  775. float a = shininess + 0.775;
  776. return norm * exp(a * NH - a);
  777. #else
  778. return norm * pow(NH, shininess);
  779. #endif
  780. }
  781.  
  782. float CalcGGX(float NH, float gloss)
  783. {
  784. // from http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
  785. float a_sq = exp2(gloss * -13.0 + 1.0);
  786. float d = ((NH * NH) * (a_sq - 1.0) + 1.0);
  787. return a_sq / (d * d);
  788. }
  789.  
  790. float CalcFresnel(float EH)
  791. {
  792. #if 1
  793. // From http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
  794. // not accurate, but fast
  795. return exp2(-10.0 * EH);
  796. #elif 0
  797. // From http://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/
  798. return exp2((-5.55473 * EH - 6.98316) * EH);
  799. #elif 0
  800. float blend = 1.0 - EH;
  801. float blend2 = blend * blend;
  802. blend *= blend2 * blend2;
  803.  
  804. return blend;
  805. #else
  806. return pow(1.0 - EH, 5.0);
  807. #endif
  808. }
  809.  
  810. float CalcVisibility(float NH, float NL, float NE, float EH, float gloss)
  811. {
  812. #if defined(USE_GOTANDA)
  813. // Neumann-Neumann as done by Yoshiharu Gotanda
  814. // http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf
  815. return 1.0 / max(max(NL, NE), EPSILON);
  816. #elif defined(USE_LAZAROV)
  817. // Cook-Torrance as done by Dimitar Lazarov
  818. // http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
  819. float k = min(1.0, gloss + 0.545);
  820. return 1.0 / (k * (EH * EH - 1.0) + 1.0);
  821. #elif defined(USE_GGX
  822.  
  823. float roughness = exp2(gloss * -6.5);
  824.  
  825. // Modified from http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
  826. // NL, NE in numerator factored out from cook-torrance
  827. float k = roughness + 1.0;
  828. k *= k * 0.125;
  829.  
  830. float k2 = 1.0 - k;
  831.  
  832. float invGeo1 = NL * k2 + k;
  833. float invGeo2 = NE * k2 + k;
  834.  
  835. return 1.0 / (invGeo1 * invGeo2);
  836. #else
  837. return 1.0;
  838. #endif
  839. }
  840.  
  841.  
  842. vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float gloss, float shininess)
  843. {
  844. #if defined(USE_GGX)
  845. float distrib = CalcGGX(NH, gloss);
  846. #else
  847. float distrib = CalcBlinn(NH, shininess);
  848. #endif
  849.  
  850. #if defined(USE_BLINN)
  851. vec3 fSpecular = specular;
  852. #else
  853. vec3 fSpecular = mix(specular, vec3(1.0), CalcFresnel(EH));
  854. #endif
  855.  
  856. float vis = CalcVisibility(NH, NL, NE, EH, gloss);
  857.  
  858. return fSpecular * (distrib * vis);
  859. }
  860.  
  861.  
  862. float CalcLightAttenuation(float point, float normDist)
  863. {
  864. // zero light at 1.0, approximating q3 style
  865. // also don't attenuate directional light
  866. float attenuation = (0.5 * nor
  867. Dist - 1.5) * point + 1.0;
  868.  
  869. // clamp attenuation
  870. #if defined(NO_LIGHT_CLAMP)
  871. attenuation = max(attenuation, 0.0);
  872. #else
  873. attenuation = clamp(attenuation, 0.0, 1.0);
  874. #endif
  875.  
  876. return attenuation;
  877. }
  878.  
  879. // from http://www.thetenthplanet.de/archives/1180
  880. mat3 cotangent_frame( vec3 N, vec3 p, vec2 uv )
  881. {
  882. // get edge vectors of the pixel triangle
  883. vec3 dp1 = dFdx( p );
  884. vec3 dp2 = dFdy( p );
  885. vec2 duv1 = dFdx( uv );
  886. vec2 duv2 = dFdy( uv );
  887.  
  888. // solve the linear system
  889. vec3 dp2perp = cross( dp2, N );
  890. vec3 dp1perp = cross( N, dp1 );
  891. vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
  892. vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
  893.  
  894. // construct a scale-invariant frame
  895. float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );
  896. return mat3( T * invmax, B * invmax, N );
  897. }
  898.  
  899. void main()
  900. {
  901. vec3 viewDir, lightColor, ambientColor;
  902. vec3 L, N, E, H;
  903. float NL, NH, NE, EH, attenuation;
  904.  
  905. #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
  906. #if defined(USE_VERT_TANGENT_SPACE)
  907. mat3 tangentToWorld = mat3(var_Tangent.xyz, var_
  908. itangent.xyz, var_Normal.xyz);
  909. viewDir = vec3(var_Normal.w, var_Tangent.w, var_Bitangent.w);
  910. #else
  911. mat3 tangentToWorld = cotangent_frame(var_Normal, -var_ViewDir, var_TexCoords.xy);
  912. viewDir = var_ViewDir;
  913. #endif
  914.  
  915. E = normalize(viewDir);
  916.  
  917. L = var_LightDir.xyz;
  918. #if defined(USE_DELUXEMAP)
  919. L += (texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(0.5)) * u_EnableTextures.y;
  920. #endif
  921. float sqrLightDist = dot(L, L);
  922. #endif
  923.  
  924. #if defined(USE_LIGHTMAP)
  925. vec4 lightmapColor = texture2D(u_LightMap, var_TexCoords.zw);
  926. #if defined(RGBM_LIGHTMAP)
  927. lightmapColor.rgb *= lightmapColor.a;
  928. #endif
  929. #endif
  930.  
  931. vec2 texCoords = var_TexCoords.xy;
  932.  
  933. #if defined(USE_PARALLAXMAP)
  934. vec3 offsetDir = viewDir * tangentToWorld;
  935.  
  936. offsetDir.xy *= -u_NormalScale.a / offsetDir.z;
  937.  
  938. texCoords += offsetDir.xy * RayIntersectDisplaceMap(texCoords, offsetDir.xy, u_NormalMap);
  939. #endif
  940.  
  941. vec4 diffuse = texture2D(u_DiffuseMap, texCoords);
  942.  
  943. #if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
  944. #if defined(USE_LIGHTMAP)
  945. lightColor = light
  946. apAP)
  947. lightColor = light
  948. apangent.xyz, var_Normal.xyz);
  949. viewDir = vec3(var_Normal.w, var_Tangent.w, var_Bitangent.w);
  950. #else
  951. mat3 tangentToWorld = cotangent_frame(var_Normal, -var_ViewDir, var_TexCoords.xy);
  952. viewDir = var_ViewDir;
  953. #endif
  954.  
  955. E = normalize(viewDir);
  956.  
  957. L = var_LightDir.xyz;
  958. #if defined(USE_DELUXEMAP)
  959. L += (texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(0.5)) * u_EnableTextures.y;
  960. #endif
  961. float sqrLightDist = dot(L, L);
  962. #endif
  963.  
  964. #if defined(USE_LIGHTMAP)
  965. vec4 lightmapColor = texture2D(u_LightMap, var_TexCoords.zw);
  966. #if defined(RGBM_LIGHTMAP)
  967. lightmapColor.rgb *= lightmapColor.a;
  968. #endif
  969. #endif
  970.  
  971. vec2 texCoords = var_TexCoords.xy;
  972.  
  973. #if defined(USE_PARALLAXMAP)
  974. vec3 offsetDir = viewDir * tangentToWorld;
  975.  
  976. offsetDir.xy *= -u_NormalScale.a / offsetDir.z;
  977.  
  978. texCoords += offsetDir.xy * RayIntersectDisplaceMap(texCoords, offsetDir.xy, u_NormalMap);
  979. #endif
  980.  
  981. vec4 diffuse = texture2D(u_DiffuseMap, texCoords);
  982.  
  983. #if defined(USE_LIGHT) && !defined(USE_FAST_LIGht are always shadowed
  984. s
  985. adowValue *= float(dot(var_Normal.xyz, var_PrimaryLightDir.xyz) > 0.0);
  986.  
  987. #if defined(SHADOWMAP_MODULATE)
  988. //vec3 shadowColor = min(u_PrimaryLightAmbient, lightColor);
  989. vec3 shadowColor = u_PrimaryLightAmbient * lightColor;
  990.  
  991. #if 0
  992. // Only shadow when the world light is parallel to the primary light
  993. shadowValue = 1.0 + (shadowValue - 1.0) * clamp(dot(L, var_PrimaryLightDir.xyz), 0.0, 1.0);
  994. #endif
  995. lightColor = mix(shadowColor, lightColor, shadowValue);
  996. #endif
  997. #endif
  998.  
  999. #if defined(r_lightGamma)
  1000. lightColor = pow(lightColor, vec3(r_lightGamma));
  1001. ambientColor = pow(ambientColor, vec3(r_lightGamma));
  1002. #endif
  1003.  
  1004. #if defined(USE_LIGHTMAP) || defined(USE_LIGHT_VERTEX)
  1005. ambientColor = lightColor;
  1006. float surfNL = clamp(dot(var_Normal.xyz, L), 0.0, 1.0);
  1007.  
  1008. // Scale the incoming light to compensate for the baked-in light angle
  1009. // attenuation.
  1010. lightColor /= max(surfNL, 0.25);
  1011.  
  1012. // Recover any unused light as ambient, in case attenuation is over 4x or
  1013. // light is below the surface
  1014. ambient
  1015. olor = clamp(ambientColor - lightColor * surfNL, 0.0, 1.0);
  1016. #endif
  1017.  
  1018. vec3 reflectance;
  1019.  
  1020. NL = clamp(dot(N, L), 0.0, 1.0);
  1021. NE = clamp(dot(N, E), 0.0, 1.0);
  1022.  
  1023. #if defined(USE_SPECULARMAP)
  1024. vec4 specular = texture2D(u_SpecularMap, texCoords);
  1025. #else
  1026. vec4 specular = vec4(1.0);
  1027. #endif
  1028.  
  1029. specular *= u_SpecularScale;
  1030.  
  1031. #if defined(r_materialGamma)
  1032. diffuse.rgb = pow(diffuse.rgb, vec3(r_materialGamma));
  1033. specular.rgb = pow(specular.rgb, vec3(r_materialGamma));
  1034. #endif
  1035.  
  1036. float gloss = specular.a;
  1037. float shininess = exp2(gloss * 13.0);
  1038.  
  1039. #if defined(SPECULAR_IS_METALLIC)
  1040. // diffuse is actually base color, and red of specular is metallicness
  1041. float metallic = specular.r;
  1042.  
  1043. specular.rgb = (0.96 * metallic) * diffuse.rgb + vec3(0.04);
  1044. diffuse.rgb *= 1.0 - metallic;
  1045. #else
  1046. // adjust diffuse by specular reflectance, to maintain energy conservation
  1047. diffuse.rgb *= vec3(1.0) - specular.rgb;
  1048. #endif
  1049.  
  1050. reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess);
  1051.  
  1052. #if defined(r_deluxeSpecular) ||
  1053. efined(USE_LIGHT_VECTOR)
  1054. float adjGloss = gloss;
  1055. float adjShininess = shininess;
  1056.  
  1057. #if !defined(USE_LIGHT_VECTOR)
  1058. adjGloss *= r_deluxeSpecular;
  1059. adjShininess = exp2(adjGloss * 13.0);
  1060. #endif
  1061.  
  1062. H = normalize(L + E);
  1063.  
  1064. EH = clamp(dot(E, H), 0.0, 1.0);
  1065. NH = clamp(dot(N, H), 0.0, 1.0);
  1066.  
  1067. #if !defined(USE_LIGHT_VECTOR)
  1068. reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, adjGloss, adjShininess) * r_deluxeSpecular;
  1069. #else
  1070. reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, adjGloss, adjShininess);
  1071. #endif
  1072. #endif
  1073.  
  1074. gl_FragColor.rgb = lightColor * reflectance * (attenuation * NL);
  1075.  
  1076. #if 0
  1077. vec3 aSpecular = EnvironmentBRDF(gloss, NE, specular.rgb);
  1078.  
  1079. // do ambient as two hemisphere lights, one straight up one straight down
  1080. float hemiDiffuseUp = N.z * 0.5 + 0.5;
  1081. float hemiDiffuseDown = 1.0 - hemiDiffuseUp;
  1082. float hemiSpecularUp = mix(hemiDiffuseUp, float(N.z >= 0.0), gloss);
  1083. float hemiSpecularDown = 1.0 - hemiSpecularUp;
  1084.  
  1085. gl_FragColor.rgb += ambientColor * 0.75 * (diffuse.
  1086. gb * hemiDiffuseUp + aSpecular * hemiSpecularUp);
  1087. gl_FragColor.rgb += ambientColor * 0.25 * (diffuse.rgb * hemiDiffuseDown + aSpecular * hemiSpecularDown);
  1088. #else
  1089. gl_FragColor.rgb += ambientColor * (diffuse.rgb + specular.rgb);
  1090. #endif
  1091.  
  1092. #if defined(USE_CUBEMAP)
  1093. reflectance = EnvironmentBRDF(gloss, NE, specular.rgb);
  1094.  
  1095. vec3 R = reflect(E, N);
  1096.  
  1097. // parallax corrected cubemap (cheaper trick)
  1098. // from http://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
  1099. vec3 parallax = u_CubeMapInfo.xyz + u_CubeMapInfo.w * viewDir;
  1100.  
  1101. vec3 cubeLightColor = textureCubeLod(u_CubeMap, R + parallax, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w;
  1102.  
  1103. // normalize cubemap based on lowest mip (~diffuse)
  1104. // multiplying cubemap values by lighting below depends on either this or the cubemap being normalized at generation
  1105. //vec3 cubeLightDiffuse = max(textureCubeLod(u_CubeMap, N, 6.0).rgb, 0.5 / 255.0);
  1106. //cubeLightColor /= dot(cubeLightDiffuse, vec3(0.2125, 0.7154, 0.0721));
  1107.  
  1108. #i
  1109. defined(r_framebufferGamma)
  1110. cubeLightColor = pow(cubeLightColor, vec3(r_framebufferGamma));
  1111. #endif
  1112.  
  1113. // multiply cubemap values by lighting
  1114. // not technically correct, but helps make reflections look less unnatural
  1115. //cubeLightColor *= lightColor * (attenuation * NL) + ambientColor;
  1116.  
  1117. gl_FragColor.rgb += cubeLightColor * reflectance;
  1118. #endif
  1119.  
  1120. #if defined(USE_PRIMARY_LIGHT)
  1121. vec3 L2, H2;
  1122. float NL2, EH2, NH2;
  1123.  
  1124. L2 = var_PrimaryLightDir.xyz;
  1125.  
  1126. // enable when point lights are supported as primary lights
  1127. //sqrLightDist = dot(L2, L2);
  1128. //L2 /= sqrt(sqrLightDist);
  1129.  
  1130. NL2 = clamp(dot(N, L2), 0.0, 1.0);
  1131.  
  1132. H2 = normalize(L2 + E);
  1133. EH2 = clamp(dot(E, H2), 0.0, 1.0);
  1134. NH2 = clamp(dot(N, H2), 0.0, 1.0);
  1135.  
  1136. reflectance = CalcDiffuse(diffuse.rgb, N, L2, E, NE, NL2, shininess);
  1137. reflectance += CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, gloss, shininess);
  1138.  
  1139. lightColor = u_PrimaryLightColor * var_Color.rgb;
  1140.  
  1141. #if defined(r_lightGamma)
  1142. lightColor = pow(lightColor, vec3(r_lightGamma));
  1143. #endif
  1144.  
  1145. #if defi
  1146. ed(USE_SHADOWMAP)
  1147. lightColor *= shadowValue;
  1148. #endif
  1149.  
  1150. // enable when point lights are supported as primary lights
  1151. //lightColor *= CalcLightAttenuation(float(u_PrimaryLightDir.w > 0.0), u_PrimaryLightDir.w / sqrLightDist);
  1152.  
  1153. gl_FragColor.rgb += lightColor * reflectance * NL2;
  1154. #endif
  1155. #else
  1156. lightColor = var_Color.rgb;
  1157.  
  1158. #if defined(USE_LIGHTMAP)
  1159. lightColor *= lightmapColor.rgb;
  1160. #endif
  1161.  
  1162. #if defined(r_lightGamma)
  1163. lightColor = pow(lightColor, vec3(r_lightGamma));
  1164. #endif
  1165.  
  1166. #if defined(r_materialGamma)
  1167. diffuse.rgb = pow(diffuse.rgb, vec3(r_materialGamma));
  1168. #endif
  1169.  
  1170. gl_FragColor.rgb = diffuse.rgb * lightColor;
  1171.  
  1172. #endif
  1173.  
  1174. #if defined(r_framebufferGamma)
  1175. gl_FragColor.rgb = pow(gl_FragColor.rgb, vec3(1.0 / r_framebufferGamma));
  1176. #endif
  1177.  
  1178. gl_FragColor.a = diffuse.a * var_Color.a;
  1179. }
  1180.  
  1181. compile log:
  1182. ERROR: 0:534: Invalid call of undeclared identifier 'textureCubeLod'
  1183. ERROR: 0:549: Use of undeclared identifier 'cubeLightColor'
  1184.  
  1185. recursive error after: Couldn't compile shader
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement