WARNING: Bad compile of:
1 #version 110
2 #define FRAGMENT_SHADER 1
3 #define BASECOLORMAP 1
4 #define EMISSIVE 1
5 #define NORMAL_TYPE -1.0
6 #define SINGLE_PASS_LIGHTING 1
7 #define NB_LIGHTS 3
8 #define NB_PROBES 0
9 #extension GL_ARB_shader_texture_lod : enable
10 // -- begin import Common/ShaderLib/GLSLCompat.glsllib --
11 #if defined GL_ES
12 # define hfloat highp float
13 # define hvec2 highp vec2
14 # define hvec3 highp vec3
15 # define hvec4 highp vec4
16 # define lfloat lowp float
17 # define lvec2 lowp vec2
18 # define lvec3 lowp vec3
19 # define lvec4 lowp vec4
20 #else
21 # define hfloat float
22 # define hvec2 vec2
23 # define hvec3 vec3
24 # define hvec4 vec4
25 # define lfloat float
26 # define lvec2 vec2
27 # define lvec3 vec3
28 # define lvec4 vec4
29 #endif
30
31 #if __VERSION__ >= 130
32 # ifdef GL_ES
33 out highp vec4 outFragColor;
34 # else
35 out vec4 outFragColor;
36 #endif
37 # define texture1D texture
38 # define texture2D texture
39 # define texture3D texture
40 # define textureCube texture
41 # define texture2DLod textureLod
42 # define textureCubeLod textureLod
43 # define texture2DArray texture
44 # if defined VERTEX_SHADER
45 # define varying out
46 # define attribute in
47 # elif defined FRAGMENT_SHADER
48 # define varying in
49 # define gl_FragColor outFragColor
50 # endif
51 #else
52 # define isnan(val) !(val<0.0||val>0.0||val==0.0)
53 #endif
54
55
56 // -- end import Common/ShaderLib/GLSLCompat.glsllib --
57 // -- begin import Common/ShaderLib/PBR.glsllib --
58 #ifndef PI
59 #define PI 3.14159265358979323846264
60 #endif
61
62 //Specular fresnel computation
63 vec3 F_Shlick(float vh, vec3 F0){
64 float fresnelFact = pow(2.0, (-5.55473*vh - 6.98316) * vh);
65 return mix(F0, vec3(1.0, 1.0, 1.0), fresnelFact);
66 }
67
68 vec3 sphericalHarmonics( const in vec3 normal, const vec3 sph[9] ){
69 float x = normal.x;
70 float y = normal.y;
71 float z = normal.z;
72
73 vec3 result = (
74 sph[0] +
75
76 sph[1] * y +
77 sph[2] * z +
78 sph[3] * x +
79
80 sph[4] * y * x +
81 sph[5] * y * z +
82 sph[6] * (3.0 * z * z - 1.0) +
83 sph[7] * (z * x) +
84 sph[8] * (x*x - y*y)
85 );
86
87 return max(result, vec3(0.0));
88 }
89
90
91 float PBR_ComputeDirectLight(vec3 normal, vec3 lightDir, vec3 viewDir,
92 vec3 lightColor, vec3 fZero, float roughness, float ndotv,
93 out vec3 outDiffuse, out vec3 outSpecular){
94 // Compute halfway vector.
95 vec3 halfVec = normalize(lightDir + viewDir);
96
97 // Compute ndotl, ndoth, vdoth terms which are needed later.
98 float ndotl = max( dot(normal, lightDir), 0.0);
99 float ndoth = max( dot(normal, halfVec), 0.0);
100 float hdotv = max( dot(viewDir, halfVec), 0.0);
101
102 // Compute diffuse using energy-conserving Lambert.
103 // Alternatively, use Oren-Nayar for really rough
104 // materials or if you have lots of processing power ...
105 outDiffuse = vec3(ndotl) * lightColor;
106
107 //cook-torrence, microfacet BRDF : http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
108
109 float alpha = roughness * roughness;
110
111 //D, GGX normaal Distribution function
112 float alpha2 = alpha * alpha;
113 float sum = ((ndoth * ndoth) * (alpha2 - 1.0) + 1.0);
114 float denom = PI * sum * sum;
115 float D = alpha2 / denom;
116
117 // Compute Fresnel function via Schlick's approximation.
118 vec3 fresnel = F_Shlick(hdotv, fZero);
119
120 //G Shchlick GGX Gometry shadowing term, k = alpha/2
121 float k = alpha * 0.5;
122
123 /*
124 //classic Schlick ggx
125 float G_V = ndotv / (ndotv * (1.0 - k) + k);
126 float G_L = ndotl / (ndotl * (1.0 - k) + k);
127 float G = ( G_V * G_L );
128
129 float specular =(D* fresnel * G) /(4 * ndotv);
130 */
131
132 // UE4 way to optimise shlick GGX Gometry shadowing term
133 //http://graphicrants.blogspot.co.uk/2013/08/specular-brdf-reference.html
134 float G_V = ndotv + sqrt( (ndotv - ndotv * k) * ndotv + k );
135 float G_L = ndotl + sqrt( (ndotl - ndotl * k) * ndotl + k );
136 // the max here is to avoid division by 0 that may cause some small glitches.
137 float G = 1.0/max( G_V * G_L ,0.01);
138
139 float specular = D * G * ndotl;
140
141 outSpecular = vec3(specular) * fresnel * lightColor;
142 return hdotv;
143 }
144
145 vec3 integrateBRDFApprox( const in vec3 specular, float roughness, float NoV ){
146 const vec4 c0 = vec4( -1, -0.0275, -0.572, 0.022 );
147 const vec4 c1 = vec4( 1, 0.0425, 1.04, -0.04 );
148 vec4 r = roughness * c0 + c1;
149 float a004 = min( r.x * r.x, exp2( -9.28 * NoV ) ) * r.x + r.y;
150 vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw;
151 return specular * AB.x + AB.y;
152 }
153
154 // from Sebastien Lagarde https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf page 69
155 vec3 getSpecularDominantDir(const in vec3 N, const in vec3 R, const in float realRoughness){
156 vec3 dominant;
157
158 float smoothness = 1.0 - realRoughness;
159 float lerpFactor = smoothness * (sqrt(smoothness) + realRoughness);
160 // The result is not normalized as we fetch in a cubemap
161 dominant = mix(N, R, lerpFactor);
162
163 return dominant;
164 }
165
166 vec3 ApproximateSpecularIBL(samplerCube envMap,sampler2D integrateBRDF, vec3 SpecularColor , float Roughness, float ndotv, vec3 refVec, float nbMipMaps){
167 float Lod = sqrt( Roughness ) * (nbMipMaps - 1.0);
168 vec3 PrefilteredColor = textureCubeLod(envMap, refVec.xyz,Lod).rgb;
169 vec2 EnvBRDF = texture2D(integrateBRDF,vec2(Roughness, ndotv)).rg;
170 return PrefilteredColor * ( SpecularColor * EnvBRDF.x+ EnvBRDF.y );
171 }
172
173 vec3 ApproximateSpecularIBLPolynomial(samplerCube envMap, vec3 SpecularColor , float Roughness, float ndotv, vec3 refVec, float nbMipMaps){
174 float Lod = sqrt( Roughness ) * (nbMipMaps - 1.0);
175 vec3 PrefilteredColor = textureCubeLod(envMap, refVec.xyz, Lod).rgb;
176 return PrefilteredColor * integrateBRDFApprox(SpecularColor, Roughness, ndotv);
177 }
178
179
180 float renderProbe(vec3 viewDir, vec3 worldPos, vec3 normal, vec3 norm, float Roughness, vec4 diffuseColor, vec4 specularColor, float ndotv, vec3 ao, mat4 lightProbeData,vec3 shCoeffs[9],samplerCube prefEnvMap, inout vec3 color ){
181
182 // lightProbeData is a mat4 with this layout
183 // 3x3 rot mat|
184 // 0 1 2 | 3
185 // 0 | ax bx cx | px | )
186 // 1 | ay by cy | py | probe position
187 // 2 | az bz cz | pz | )
188 // --|----------|
189 // 3 | sx sy sz sp | -> 1/probe radius + nbMipMaps
190 // --scale--
191 // parallax fix for spherical / obb bounds and probe blending from
192 // from https://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
193 vec3 rv = reflect(-viewDir, normal);
194 vec4 probePos = lightProbeData[3];
195 float invRadius = fract( probePos.w);
196 float nbMipMaps = probePos.w - invRadius;
197 vec3 direction = worldPos - probePos.xyz;
198 float ndf = 0.0;
199
200 if(lightProbeData[0][3] != 0.0){
201 // oriented box probe
202 mat3 wToLocalRot = mat3(lightProbeData);
203 wToLocalRot = inverse(wToLocalRot);
204 vec3 scale = vec3(lightProbeData[0][3], lightProbeData[1][3], lightProbeData[2][3]);
205 #if NB_PROBES >= 2
206 // probe blending
207 // compute fragment position in probe local space
208 vec3 localPos = wToLocalRot * worldPos;
209 localPos -= probePos.xyz;
210 // compute normalized distance field
211 vec3 localDir = abs(localPos);
212 localDir /= scale;
213 ndf = max(max(localDir.x, localDir.y), localDir.z);
214 #endif
215 // parallax fix
216 vec3 rayLs = wToLocalRot * rv;
217 rayLs /= scale;
218
219 vec3 positionLs = worldPos - probePos.xyz;
220 positionLs = wToLocalRot * positionLs;
221 positionLs /= scale;
222
223 vec3 unit = vec3(1.0);
224 vec3 firstPlaneIntersect = (unit - positionLs) / rayLs;
225 vec3 secondPlaneIntersect = (-unit - positionLs) / rayLs;
226 vec3 furthestPlane = max(firstPlaneIntersect, secondPlaneIntersect);
227 float distance = min(min(furthestPlane.x, furthestPlane.y), furthestPlane.z);
228
229 vec3 intersectPositionWs = worldPos + rv * distance;
230 rv = intersectPositionWs - probePos.xyz;
231
232 } else {
233 // spherical probe
234 // paralax fix
235 rv = invRadius * direction + rv;
236
237 #if NB_PROBES >= 2
238 // probe blending
239 float dist = sqrt(dot(direction, direction));
240 ndf = dist * invRadius;
241 #endif
242 }
243
244 vec3 indirectDiffuse = vec3(0.0);
245 vec3 indirectSpecular = vec3(0.0);
246 indirectDiffuse = sphericalHarmonics(normal.xyz, shCoeffs) * diffuseColor.rgb;
247 vec3 dominantR = getSpecularDominantDir( normal, rv.xyz, Roughness * Roughness );
248 indirectSpecular = ApproximateSpecularIBLPolynomial(prefEnvMap, specularColor.rgb, Roughness, ndotv, dominantR, nbMipMaps);
249
250 #ifdef HORIZON_FADE
251 //horizon fade from http://marmosetco.tumblr.com/post/81245981087
252 float horiz = dot(rv, norm);
253 float horizFadePower = 1.0 - Roughness;
254 horiz = clamp( 1.0 + horizFadePower * horiz, 0.0, 1.0 );
255 horiz *= horiz;
256 indirectSpecular *= vec3(horiz);
257 #endif
258
259 vec3 indirectLighting = (indirectDiffuse + indirectSpecular) * ao;
260
261 color = indirectLighting * step( 0.0, probePos.w);
262 return ndf;
263 }
264
265
266
267
268
269 // -- end import Common/ShaderLib/PBR.glsllib --
270 // -- begin import Common/ShaderLib/Parallax.glsllib --
271 #if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP))) && !defined(VERTEX_LIGHTING)
272 vec2 steepParallaxOffset(sampler2D parallaxMap, vec3 vViewDir,vec2 texCoord,float parallaxScale){
273 vec2 vParallaxDirection = normalize( vViewDir.xy );
274
275 // The length of this vector determines the furthest amount of displacement: (Ati's comment)
276 float fLength = length( vViewDir );
277 float fParallaxLength = sqrt( fLength * fLength - vViewDir.z * vViewDir.z ) / vViewDir.z;
278
279 // Compute the actual reverse parallax displacement vector: (Ati's comment)
280 vec2 vParallaxOffsetTS = vParallaxDirection * fParallaxLength;
281
282 // Need to scale the amount of displacement to account for different height ranges
283 // in height maps. This is controlled by an artist-editable parameter: (Ati's comment)
284 parallaxScale *=0.3;
285 vParallaxOffsetTS *= parallaxScale;
286
287 vec3 eyeDir = normalize(vViewDir).xyz;
288
289 float nMinSamples = 6.0;
290 float nMaxSamples = 1000.0 * parallaxScale;
291 float nNumSamples = mix( nMinSamples, nMaxSamples, 1.0 - eyeDir.z ); //In reference shader: int nNumSamples = (int)(lerp( nMinSamples, nMaxSamples, dot( eyeDirWS, N ) ));
292 float fStepSize = 1.0 / nNumSamples;
293 float fCurrHeight = 0.0;
294 float fPrevHeight = 1.0;
295 float fNextHeight = 0.0;
296 float nStepIndex = 0.0;
297 vec2 vTexOffsetPerStep = fStepSize * vParallaxOffsetTS;
298 vec2 vTexCurrentOffset = texCoord;
299 float fCurrentBound = 1.0;
300 float fParallaxAmount = 0.0;
301
302 while ( nStepIndex < nNumSamples && fCurrHeight <= fCurrentBound ) {
303 vTexCurrentOffset -= vTexOffsetPerStep;
304 fPrevHeight = fCurrHeight;
305
306
307 #ifdef NORMALMAP_PARALLAX
308 //parallax map is stored in the alpha channel of the normal map
309 fCurrHeight = texture2D( parallaxMap, vTexCurrentOffset).a;
310 #else
311 //parallax map is a texture
312 fCurrHeight = texture2D( parallaxMap, vTexCurrentOffset).r;
313 #endif
314
315 fCurrentBound -= fStepSize;
316 nStepIndex+=1.0;
317 }
318 vec2 pt1 = vec2( fCurrentBound, fCurrHeight );
319 vec2 pt2 = vec2( fCurrentBound + fStepSize, fPrevHeight );
320
321 float fDelta2 = pt2.x - pt2.y;
322 float fDelta1 = pt1.x - pt1.y;
323
324 float fDenominator = fDelta2 - fDelta1;
325
326 fParallaxAmount = (pt1.x * fDelta2 - pt2.x * fDelta1 ) / fDenominator;
327
328 vec2 vParallaxOffset = vParallaxOffsetTS * (1.0 - fParallaxAmount );
329 return texCoord - vParallaxOffset;
330 }
331
332 vec2 classicParallaxOffset(sampler2D parallaxMap, vec3 vViewDir,vec2 texCoord,float parallaxScale){
333 float h;
334 #ifdef NORMALMAP_PARALLAX
335 //parallax map is stored in the alpha channel of the normal map
336 h = texture2D(parallaxMap, texCoord).a;
337 #else
338 //parallax map is a texture
339 h = texture2D(parallaxMap, texCoord).r;
340 #endif
341 float heightScale = parallaxScale;
342 float heightBias = heightScale* -0.6;
343 vec3 normView = normalize(vViewDir);
344 h = (h * heightScale + heightBias) * normView.z;
345 return texCoord + (h * normView.xy);
346 }
347 #endif
348 // -- end import Common/ShaderLib/Parallax.glsllib --
349 // -- begin import Common/ShaderLib/Lighting.glsllib --
350 /*Common function for light calculations*/
351
352
353 /*
354 * Computes light direction
355 * lightType should be 0.0,1.0,2.0, repectively for Directional, point and spot lights.
356 * Outputs the light direction and the light half vector.
357 */
358 void lightComputeDir(in vec3 worldPos, in float lightType, in vec4 position, out vec4 lightDir, out vec3 lightVec){
359 float posLight = step(0.5, lightType);
360 vec3 tempVec = position.xyz * sign(posLight - 0.5) - (worldPos * posLight);
361 lightVec = tempVec;
362 float dist = length(tempVec);
363 #ifdef SRGB
364 lightDir.w = (1.0 - position.w * dist) / (1.0 + position.w * dist * dist);
365 lightDir.w = clamp(lightDir.w, 1.0 - posLight, 1.0);
366 #else
367 lightDir.w = clamp(1.0 - position.w * dist * posLight, 0.0, 1.0);
368 #endif
369 lightDir.xyz = tempVec / vec3(dist);
370 }
371
372 /*
373 * Computes the spot falloff for a spotlight
374 */
375 float computeSpotFalloff(in vec4 lightDirection, in vec3 lightVector){
376 vec3 L=normalize(lightVector);
377 vec3 spotdir = normalize(lightDirection.xyz);
378 float curAngleCos = dot(-L, spotdir);
379 float innerAngleCos = floor(lightDirection.w) * 0.001;
380 float outerAngleCos = fract(lightDirection.w);
381 float innerMinusOuter = innerAngleCos - outerAngleCos;
382 float falloff = clamp((curAngleCos - outerAngleCos) / innerMinusOuter, step(lightDirection.w, 0.001), 1.0);
383
384 #ifdef SRGB
385 // Use quadratic falloff (notice the ^4)
386 return pow(clamp((curAngleCos - outerAngleCos) / innerMinusOuter, 0.0, 1.0), 4.0);
387 #else
388 // Use linear falloff
389 return falloff;
390 #endif
391 }
392
393 // -- end import Common/ShaderLib/Lighting.glsllib --
394
395 varying vec2 texCoord;
396 #ifdef SEPARATE_TEXCOORD
397 varying vec2 texCoord2;
398 #endif
399
400 varying vec4 Color;
401
402 uniform vec4 g_LightData[NB_LIGHTS];
403 uniform vec3 g_CameraPosition;
404 uniform vec4 g_AmbientLightColor;
405
406 uniform float m_Roughness;
407 uniform float m_Metallic;
408
409 varying vec3 wPosition;
410
411
412 #if NB_PROBES >= 1
413 uniform samplerCube g_PrefEnvMap;
414 uniform vec3 g_ShCoeffs[9];
415 uniform mat4 g_LightProbeData;
416 #endif
417 #if NB_PROBES >= 2
418 uniform samplerCube g_PrefEnvMap2;
419 uniform vec3 g_ShCoeffs2[9];
420 uniform mat4 g_LightProbeData2;
421 #endif
422 #if NB_PROBES == 3
423 uniform samplerCube g_PrefEnvMap3;
424 uniform vec3 g_ShCoeffs3[9];
425 uniform mat4 g_LightProbeData3;
426 #endif
427
428 #ifdef BASECOLORMAP
429 uniform sampler2D m_BaseColorMap;
430 #endif
431
432 #ifdef USE_PACKED_MR
433 uniform sampler2D m_MetallicRoughnessMap;
434 #else
435 #ifdef METALLICMAP
436 uniform sampler2D m_MetallicMap;
437 #endif
438 #ifdef ROUGHNESSMAP
439 uniform sampler2D m_RoughnessMap;
440 #endif
441 #endif
442
443 #ifdef EMISSIVE
444 uniform vec4 m_Emissive;
445 #endif
446 #ifdef EMISSIVEMAP
447 uniform sampler2D m_EmissiveMap;
448 #endif
449 #if defined(EMISSIVE) || defined(EMISSIVEMAP)
450 uniform float m_EmissivePower;
451 uniform float m_EmissiveIntensity;
452 #endif
453
454 #ifdef SPECGLOSSPIPELINE
455
456 uniform vec4 m_Specular;
457 uniform float m_Glossiness;
458 #ifdef USE_PACKED_SG
459 uniform sampler2D m_SpecularGlossinessMap;
460 #else
461 uniform sampler2D m_SpecularMap;
462 uniform sampler2D m_GlossinessMap;
463 #endif
464 #endif
465
466 #ifdef PARALLAXMAP
467 uniform sampler2D m_ParallaxMap;
468 #endif
469 #if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP)))
470 uniform float m_ParallaxHeight;
471 #endif
472
473 #ifdef LIGHTMAP
474 uniform sampler2D m_LightMap;
475 #endif
476
477 #if defined(NORMALMAP) || defined(PARALLAXMAP)
478 uniform sampler2D m_NormalMap;
479 varying vec4 wTangent;
480 #endif
481 varying vec3 wNormal;
482
483 #ifdef DISCARD_ALPHA
484 uniform float m_AlphaDiscardThreshold;
485 #endif
486
487 void main(){
488 vec2 newTexCoord;
489 vec3 viewDir = normalize(g_CameraPosition - wPosition);
490
491 vec3 norm = normalize(wNormal);
492 #if defined(NORMALMAP) || defined(PARALLAXMAP)
493 vec3 tan = normalize(wTangent.xyz);
494 mat3 tbnMat = mat3(tan, wTangent.w * cross( (norm), (tan)), norm);
495 #endif
496
497 #if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP)))
498 vec3 vViewDir = viewDir * tbnMat;
499 #ifdef STEEP_PARALLAX
500 #ifdef NORMALMAP_PARALLAX
501 //parallax map is stored in the alpha channel of the normal map
502 newTexCoord = steepParallaxOffset(m_NormalMap, vViewDir, texCoord, m_ParallaxHeight);
503 #else
504 //parallax map is a texture
505 newTexCoord = steepParallaxOffset(m_ParallaxMap, vViewDir, texCoord, m_ParallaxHeight);
506 #endif
507 #else
508 #ifdef NORMALMAP_PARALLAX
509 //parallax map is stored in the alpha channel of the normal map
510 newTexCoord = classicParallaxOffset(m_NormalMap, vViewDir, texCoord, m_ParallaxHeight);
511 #else
512 //parallax map is a texture
513 newTexCoord = classicParallaxOffset(m_ParallaxMap, vViewDir, texCoord, m_ParallaxHeight);
514 #endif
515 #endif
516 #else
517 newTexCoord = texCoord;
518 #endif
519
520 #ifdef BASECOLORMAP
521 vec4 albedo = texture2D(m_BaseColorMap, newTexCoord) * Color;
522 #else
523 vec4 albedo = Color;
524 #endif
525
526 #ifdef USE_PACKED_MR
527 vec2 rm = texture2D(m_MetallicRoughnessMap, newTexCoord).gb;
528 float Roughness = rm.x * max(m_Roughness, 1e-4);
529 float Metallic = rm.y * max(m_Metallic, 0.0);
530 #else
531 #ifdef ROUGHNESSMAP
532 float Roughness = texture2D(m_RoughnessMap, newTexCoord).r * max(m_Roughness, 1e-4);
533 #else
534 float Roughness = max(m_Roughness, 1e-4);
535 #endif
536 #ifdef METALLICMAP
537 float Metallic = texture2D(m_MetallicMap, newTexCoord).r * max(m_Metallic, 0.0);
538 #else
539 float Metallic = max(m_Metallic, 0.0);
540 #endif
541 #endif
542
543 float alpha = albedo.a;
544
545 #ifdef DISCARD_ALPHA
546 if(alpha < m_AlphaDiscardThreshold){
547 discard;
548 }
549 #endif
550
551 // ***********************
552 // Read from textures
553 // ***********************
554 #if defined(NORMALMAP)
555 vec4 normalHeight = texture2D(m_NormalMap, newTexCoord);
556 //Note the -2.0 and -1.0. We invert the green channel of the normal map,
557 //as it's complient with normal maps generated with blender.
558 //see http://hub.jmonkeyengine.org/forum/topic/parallax-mapping-fundamental-bug/#post-256898
559 //for more explanation.
560 vec3 normal = normalize((normalHeight.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0)));
561 normal = normalize(tbnMat * normal);
562 //normal = normalize(normal * inverse(tbnMat));
563 #else
564 vec3 normal = norm;
565 #endif
566
567 #ifdef SPECGLOSSPIPELINE
568
569 #ifdef USE_PACKED_SG
570 vec4 specularColor = texture2D(m_SpecularGlossinessMap, newTexCoord);
571 float glossiness = specularColor.a * m_Glossiness;
572 specularColor *= m_Specular;
573 #else
574 #ifdef SPECULARMAP
575 vec4 specularColor = texture2D(m_SpecularMap, newTexCoord);
576 #else
577 vec4 specularColor = vec4(1.0);
578 #endif
579 #ifdef GLOSSINESSMAP
580 float glossiness = texture2D(m_GlossinessMap, newTexCoord).r * m_Glossiness;
581 #else
582 float glossiness = m_Glossiness;
583 #endif
584 specularColor *= m_Specular;
585 #endif
586 vec4 diffuseColor = albedo;// * (1.0 - max(max(specularColor.r, specularColor.g), specularColor.b));
587 Roughness = 1.0 - glossiness;
588 vec3 fZero = specularColor.xyz;
589 #else
590 float specular = 0.5;
591 float nonMetalSpec = 0.08 * specular;
592 vec4 specularColor = (nonMetalSpec - nonMetalSpec * Metallic) + albedo * Metallic;
593 vec4 diffuseColor = albedo - albedo * Metallic;
594 vec3 fZero = vec3(specular);
595 #endif
596
597 gl_FragColor.rgb = vec3(0.0);
598 vec3 ao = vec3(1.0);
599
600 #ifdef LIGHTMAP
601 vec3 lightMapColor;
602 #ifdef SEPARATE_TEXCOORD
603 lightMapColor = texture2D(m_LightMap, texCoord2).rgb;
604 #else
605 lightMapColor = texture2D(m_LightMap, texCoord).rgb;
606 #endif
607 #ifdef AO_MAP
608 lightMapColor.gb = lightMapColor.rr;
609 ao = lightMapColor;
610 #else
611 gl_FragColor.rgb += diffuseColor.rgb * lightMapColor;
612 #endif
613 specularColor.rgb *= lightMapColor;
614 #endif
615
616
617 float ndotv = max( dot( normal, viewDir ),0.0);
618 for( int i = 0;i < NB_LIGHTS; i+=3){
619 vec4 lightColor = g_LightData[i];
620 vec4 lightData1 = g_LightData[i+1];
621 vec4 lightDir;
622 vec3 lightVec;
623 lightComputeDir(wPosition, lightColor.w, lightData1, lightDir, lightVec);
624
625 float fallOff = 1.0;
626 #if __VERSION__ >= 110
627 // allow use of control flow
628 if(lightColor.w > 1.0){
629 #endif
630 fallOff = computeSpotFalloff(g_LightData[i+2], lightVec);
631 #if __VERSION__ >= 110
632 }
633 #endif
634 //point light attenuation
635 fallOff *= lightDir.w;
636
637 lightDir.xyz = normalize(lightDir.xyz);
638 vec3 directDiffuse;
639 vec3 directSpecular;
640
641 float hdotv = PBR_ComputeDirectLight(normal, lightDir.xyz, viewDir,
642 lightColor.rgb, fZero, Roughness, ndotv,
643 directDiffuse, directSpecular);
644
645 vec3 directLighting = diffuseColor.rgb *directDiffuse + directSpecular;
646
647 gl_FragColor.rgb += directLighting * fallOff;
648 }
649
650 #if NB_PROBES >= 1
651 vec3 color1 = vec3(0.0);
652 vec3 color2 = vec3(0.0);
653 vec3 color3 = vec3(0.0);
654 float weight1 = 1.0;
655 float weight2 = 0.0;
656 float weight3 = 0.0;
657
658 float ndf = renderProbe(viewDir, wPosition, normal, norm, Roughness, diffuseColor, specularColor, ndotv, ao, g_LightProbeData, g_ShCoeffs, g_PrefEnvMap, color1);
659 #if NB_PROBES >= 2
660 float ndf2 = renderProbe(viewDir, wPosition, normal, norm, Roughness, diffuseColor, specularColor, ndotv, ao, g_LightProbeData2, g_ShCoeffs2, g_PrefEnvMap2, color2);
661 #endif
662 #if NB_PROBES == 3
663 float ndf3 = renderProbe(viewDir, wPosition, normal, norm, Roughness, diffuseColor, specularColor, ndotv, ao, g_LightProbeData3, g_ShCoeffs3, g_PrefEnvMap3, color3);
664 #endif
665
666 #if NB_PROBES >= 2
667 float invNdf = max(1.0 - ndf,0.0);
668 float invNdf2 = max(1.0 - ndf2,0.0);
669 float sumNdf = ndf + ndf2;
670 float sumInvNdf = invNdf + invNdf2;
671 #if NB_PROBES == 3
672 float invNdf3 = max(1.0 - ndf3,0.0);
673 sumNdf += ndf3;
674 sumInvNdf += invNdf3;
675 weight3 = ((1.0 - (ndf3 / sumNdf)) / (NB_PROBES - 1)) * (invNdf3 / sumInvNdf);
676 #endif
677
678 weight1 = ((1.0 - (ndf / sumNdf)) / (NB_PROBES - 1)) * (invNdf / sumInvNdf);
679 weight2 = ((1.0 - (ndf2 / sumNdf)) / (NB_PROBES - 1)) * (invNdf2 / sumInvNdf);
680
681 float weightSum = weight1 + weight2 + weight3;
682
683 weight1 /= weightSum;
684 weight2 /= weightSum;
685 weight3 /= weightSum;
686 #endif
687
688 #ifdef USE_AMBIENT_LIGHT
689 color1.rgb *= g_AmbientLightColor.rgb;
690 color2.rgb *= g_AmbientLightColor.rgb;
691 color3.rgb *= g_AmbientLightColor.rgb;
692 #endif
693 gl_FragColor.rgb += color1 * clamp(weight1,0.0,1.0) + color2 * clamp(weight2,0.0,1.0) + color3 * clamp(weight3,0.0,1.0);
694
695 #endif
696
697 #if defined(EMISSIVE) || defined (EMISSIVEMAP)
698 #ifdef EMISSIVEMAP
699 vec4 emissive = texture2D(m_EmissiveMap, newTexCoord);
700 #else
701 vec4 emissive = m_Emissive;
702 #endif
703 gl_FragColor += emissive * pow(emissive.a, m_EmissivePower) * m_EmissiveIntensity;
704 #endif
705 gl_FragColor.a = alpha;
706
707 }
мар. 27, 2022 9:04:31 PM com.jme3.app.LegacyApplication handleError
SEVERE: Uncaught exception thrown in Thread[jME3 Main,5,main]
com.jme3.renderer.RendererException: compile error in: ShaderSource[name=Common/MatDefs/Light/PBRLighting.frag, defines, type=Fragment, language=GLSL110]
0:202(21): error: cannot construct `mat3' from a matrix in GLSL 1.10 (GLSL 1.20 or GLSL ES 1.00 required)
0:203(16): error: no function with name 'inverse'
at com.jme3.renderer.opengl.GLRenderer.updateShaderSourceData(GLRenderer.java:1476)
at com.jme3.renderer.opengl.GLRenderer.updateShaderData(GLRenderer.java:1503)
at com.jme3.renderer.opengl.GLRenderer.setShader(GLRenderer.java:1567)
at com.jme3.material.logic.SinglePassAndImageBasedLightingLogic.render(SinglePassAndImageBasedLightingLogic.java:254)
at com.jme3.material.Technique.render(Technique.java:166)
at com.jme3.material.Material.render(Material.java:1026)
at com.jme3.renderer.RenderManager.renderGeometry(RenderManager.java:614)
at com.jme3.renderer.queue.RenderQueue.renderGeometryList(RenderQueue.java:266)
at com.jme3.renderer.queue.RenderQueue.renderQueue(RenderQueue.java:305)
at com.jme3.renderer.RenderManager.renderViewPortQueues(RenderManager.java:877)
at com.jme3.renderer.RenderManager.flushQueue(RenderManager.java:779)
at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:1108)
at com.jme3.renderer.RenderManager.render(RenderManager.java:1158)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:272)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:197)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:232)
at java.base/java.lang.Thread.run(Thread.java:834)