The recently pushed demos crash on macOS.
Demos crashing: boxmulti2, physical_helmet.
08:49:14.510738:I:G3ND:OpenGL version: 4.1 ATI-1.66.31
08:49:14.511004:I:G3ND:G3N Demo v0.5 starting
08:49:14.511067:I:G3ND:Using data directory:/Users/dobegor/go/src/github.com/g3n/g3nd/data
08:49:14.562467:I:G3ND:OpenAL Community version: 1.1 ALSOFT 1.18.2
08:49:14.562766:I:G3ND:Xiph.Org libVorbis 1.3.6
panic: Error compiling Fragment Shader: ERROR: 0:185: Invalid call of undeclared identifier 'texture2D'
ERROR: 0:186: Use of undeclared identifier 'n'
ERROR: 0:186: Use of undeclared identifier 'n'
ERROR: 0:192: Use of undeclared identifier 'n'
ERROR: 0:306: Invalid call of undeclared identifier 'texture2D'
ERROR: 0:307: Use of undeclared identifier 'mrSample'
ERROR: 0:308: Use of undeclared identifier 'mrSample'
ERROR: 0:319: Invalid call of undeclared identifier 'texture2D'
ERROR: 0:325: Use of undeclared identifier 'baseColor'
ERROR: 0:326: Use of undeclared identifier 'diffuseColor'
ERROR: 0:328: Use of undeclared identifier 'baseColor'
ERROR: 0:331: Use of undeclared identifier 'specularColor'
ERROR: 0:331: Use of undeclared identifier 'specularColor'
ERROR: 0:331: Use of undeclared identifier 'specularColor'
ERROR: 0:335: Use of undeclared identifier 'reflectance'
ERROR: 0:336: Use of undeclared identifier 'specularColor'
ERROR: 0:337: Use of undeclared identifier 'reflectance90'
ERROR: 0:342: Use of undeclared identifier 'specularEnvironmentR0'
ERROR: 0:343: Use of undeclared identifier 'specularEnvironmentR90'
ERROR: 0:345: Use of undeclared identifier 'diffuseColor'
ERROR: 0:346: Use of undeclared identifier 'specularColor'
ERROR: 0:355: Use of undeclared identifier 'pbrInputs'
ERROR: 0:366: Use of undeclared identifier 'pbrInputs'
ERROR: 0:384: Use of undeclared identifier 'pbrInputs'
ERROR: 0:422: Invalid call of undeclared identifier 'texture2D'
ERROR: 0:423: Use of undeclared identifier 'ao'
ERROR: 0:427: Invalid call of undeclared identifier 'texture2D'
ERROR: 0:431: Use of undeclared identifier 'emissive'
ERROR: 0:464: Use of undeclared identifier 'baseColor'
001:#version 330 core
002:#define AMB_LIGHTS 1
003:#define POINT_LIGHTS 2
004:#define SPOT_LIGHTS 0
005:#define HAS_OCCLUSIONMAP
006:#define HAS_METALROUGHNESSMAP
007:#define DIR_LIGHTS 3
008:#define MAT_TEXTURES 5
009:#define HAS_NORMALMAP
010:#define HAS_EMISSIVEMAP
011:#define HAS_BASECOLORMAP
012://
013:// Physically Based Shading of a microfacet surface material - Fragment Shader
014:// Modified from reference implementation at https://github.com/KhronosGroup/glTF-WebGL-PBR
015://
016:// References:
017:// [1] Real Shading in Unreal Engine 4
018:// http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
019:// [2] Physically Based Shading at Disney
020:// http://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf
021:// [3] README.md - Environment Maps
022:// https://github.com/KhronosGroup/glTF-WebGL-PBR/#environment-maps
023:// [4] "An Inexpensive BRDF Model for Physically based Rendering" by Christophe Schlick
024:// https://www.cs.virginia.edu/~jdl/bib/appearance/analytic%20models/schlick94b.pdf
025:
026://#extension GL_EXT_shader_texture_lod: enable
027://#extension GL_OES_standard_derivatives : enable
028:
029:precision highp float;
030:
031://uniform vec3 u_LightDirection;
032://uniform vec3 u_LightColor;
033:
034://#ifdef USE_IBL
035://uniform samplerCube u_DiffuseEnvSampler;
036://uniform samplerCube u_SpecularEnvSampler;
037://uniform sampler2D u_brdfLUT;
038://#endif
039:
040:#ifdef HAS_BASECOLORMAP
041:uniform sampler2D uBaseColorSampler;
042:#endif
043:#ifdef HAS_METALROUGHNESSMAP
044:uniform sampler2D uMetallicRoughnessSampler;
045:#endif
046:#ifdef HAS_NORMALMAP
047:uniform sampler2D uNormalSampler;
048://uniform float uNormalScale;
049:#endif
050:#ifdef HAS_EMISSIVEMAP
051:uniform sampler2D uEmissiveSampler;
052:#endif
053:#ifdef HAS_OCCLUSIONMAP
054:uniform sampler2D uOcclusionSampler;
055:uniform float uOcclusionStrength;
056:#endif
057:
058:// Material parameters uniform array
059:uniform vec4 Material[3];
060:// Macros to access elements inside the Material array
061:#define uBaseColor Material[0]
062:#define uEmissiveColor Material[1]
063:#define uMetallicFactor Material[2].x
064:#define uRoughnessFactor Material[2].y
065:
066://
067:// Lights uniforms
068://
069:
070:// Ambient lights uniforms
071:#if AMB_LIGHTS>0
072: uniform vec3 AmbientLightColor[AMB_LIGHTS];
073:#endif
074:
075:// Directional lights uniform array. Each directional light uses 2 elements
076:#if DIR_LIGHTS>0
077: uniform vec3 DirLight[2*DIR_LIGHTS];
078: // Macros to access elements inside the DirectionalLight uniform array
079: #define DirLightColor(a) DirLight[2*a]
080: #define DirLightPosition(a) DirLight[2*a+1]
081:#endif
082:
083:// Point lights uniform array. Each point light uses 3 elements
084:#if POINT_LIGHTS>0
085: uniform vec3 PointLight[3*POINT_LIGHTS];
086: // Macros to access elements inside the PointLight uniform array
087: #define PointLightColor(a) PointLight[3*a]
088: #define PointLightPosition(a) PointLight[3*a+1]
089: #define PointLightLinearDecay(a) PointLight[3*a+2].x
090: #define PointLightQuadraticDecay(a) PointLight[3*a+2].y
091:#endif
092:
093:#if SPOT_LIGHTS>0
094: // Spot lights uniforms. Each spot light uses 5 elements
095: uniform vec3 SpotLight[5*SPOT_LIGHTS];
096:
097: // Macros to access elements inside the PointLight uniform array
098: #define SpotLightColor(a) SpotLight[5*a]
099: #define SpotLightPosition(a) SpotLight[5*a+1]
100: #define SpotLightDirection(a) SpotLight[5*a+2]
101: #define SpotLightAngularDecay(a) SpotLight[5*a+3].x
102: #define SpotLightCutoffAngle(a) SpotLight[5*a+3].y
103: #define SpotLightLinearDecay(a) SpotLight[5*a+3].z
104: #define SpotLightQuadraticDecay(a) SpotLight[5*a+4].x
105:#endif
106:
107:
108:
109:// Inputs from vertex shader
110:in vec3 Position; // Vertex position in camera coordinates.
111:in vec3 Normal; // Vertex normal in camera coordinates.
112:in vec3 CamDir; // Direction from vertex to camera
113:in vec2 FragTexcoord;
114:
115:// Final fragment color
116:out vec4 FragColor;
117:
118:// Encapsulate the various inputs used by the various functions in the shading equation
119:// We store values in this struct to simplify the integration of alternative implementations
120:// of the shading terms, outlined in the Readme.MD Appendix.
121:struct PBRLightInfo
122:{
123: float NdotL; // cos angle between normal and light direction
124: float NdotV; // cos angle between normal and view direction
125: float NdotH; // cos angle between normal and half vector
126: float LdotH; // cos angle between light direction and half vector
127: float VdotH; // cos angle between view direction and half vector
128:};
129:
130:struct PBRInfo
131:{
132: float perceptualRoughness; // roughness value, as authored by the model creator (input to shader)
133: float metalness; // metallic value at the surface
134: vec3 reflectance0; // full reflectance color (normal incidence angle)
135: vec3 reflectance90; // reflectance color at grazing angle
136: float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2])
137: vec3 diffuseColor; // color contribution from diffuse lighting
138: vec3 specularColor; // color contribution from specular lighting
139:};
140:
141:const float M_PI = 3.141592653589793;
142:const float c_MinRoughness = 0.04;
143:
144:vec4 SRGBtoLINEAR(vec4 srgbIn) {
145://#ifdef MANUAL_SRGB
146:// #ifdef SRGB_FAST_APPROXIMATION
147:// vec3 linOut = pow(srgbIn.xyz,vec3(2.2));
148:// #else //SRGB_FAST_APPROXIMATION
149: vec3 bLess = step(vec3(0.04045),srgbIn.xyz);
150: vec3 linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );
151:// #endif //SRGB_FAST_APPROXIMATION
152: return vec4(linOut,srgbIn.w);
153://#else //MANUAL_SRGB
154:// return srgbIn;
155://#endif //MANUAL_SRGB
156:}
157:
158:// Find the normal for this fragment, pulling either from a predefined normal map
159:// or from the interpolated mesh normal and tangent attributes.
160:vec3 getNormal()
161:{
162: // Retrieve the tangent space matrix
163://#ifndef HAS_TANGENTS
164: vec3 pos_dx = dFdx(Position);
165: vec3 pos_dy = dFdy(Position);
166: vec3 tex_dx = dFdx(vec3(FragTexcoord, 0.0));
167: vec3 tex_dy = dFdy(vec3(FragTexcoord, 0.0));
168: vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);
169:
170://#ifdef HAS_NORMALS
171: vec3 ng = normalize(Normal);
172://#else
173:// vec3 ng = cross(pos_dx, pos_dy);
174://#endif
175:
176: t = normalize(t - ng * dot(ng, t));
177: vec3 b = normalize(cross(ng, t));
178: mat3 tbn = mat3(t, b, ng);
179://#else // HAS_TANGENTS
180:// mat3 tbn = v_TBN;
181://#endif
182:
183:#ifdef HAS_NORMALMAP
184: float uNormalScale = 1.0;
185: vec3 n = texture2D(uNormalSampler, FragTexcoord).rgb;
186: n = normalize(tbn * ((2.0 * n - 1.0) * vec3(uNormalScale, uNormalScale, 1.0)));
187:#else
188: // The tbn matrix is linearly interpolated, so we need to re-normalize
189: vec3 n = normalize(tbn[2].xyz);
190:#endif
191:
192: return n;
193:}
194:
195:// Calculation of the lighting contribution from an optional Image Based Light source.
196:// Precomputed Environment Maps are required uniform inputs and are computed as outlined in [1].
197:// See our README.md on Environment Maps [3] for additional discussion.
198:vec3 getIBLContribution(PBRInfo pbrInputs, PBRLightInfo pbrLight, vec3 n, vec3 reflection)
199:{
200: float mipCount = 9.0; // resolution of 512x512
201: float lod = (pbrInputs.perceptualRoughness * mipCount);
202: // retrieve a scale and bias to F0. See [1], Figure 3
203: vec3 brdf = vec3(0.5,0.5,0.5);//SRGBtoLINEAR(texture2D(u_brdfLUT, vec2(pbrLight.NdotV, 1.0 - pbrInputs.perceptualRoughness))).rgb;
204: vec3 diffuseLight = vec3(0.5,0.5,0.5);//SRGBtoLINEAR(textureCube(u_DiffuseEnvSampler, n)).rgb;
205:
206://#ifdef USE_TEX_LOD
207:// vec3 specularLight = SRGBtoLINEAR(textureCubeLodEXT(u_SpecularEnvSampler, reflection, lod)).rgb;
208://#else
209: vec3 specularLight = vec3(0.5,0.5,0.5);//SRGBtoLINEAR(textureCube(u_SpecularEnvSampler, reflection)).rgb;
210://#endif
211:
212: vec3 diffuse = diffuseLight * pbrInputs.diffuseColor;
213: vec3 specular = specularLight * (pbrInputs.specularColor * brdf.x + brdf.y);
214:
215: // For presentation, this allows us to disable IBL terms
216:// diffuse *= u_ScaleIBLAmbient.x;
217:// specular *= u_ScaleIBLAmbient.y;
218:
219: return diffuse + specular;
220:}
221:
222:// Basic Lambertian diffuse
223:// Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog
224:// See also [1], Equation 1
225:vec3 diffuse(PBRInfo pbrInputs)
226:{
227: return pbrInputs.diffuseColor / M_PI;
228:}
229:
230:// The following equation models the Fresnel reflectance term of the spec equation (aka F())
231:// Implementation of fresnel from [4], Equation 15
232:vec3 specularReflection(PBRInfo pbrInputs, PBRLightInfo pbrLight)
233:{
234: return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance0) * pow(clamp(1.0 - pbrLight.VdotH, 0.0, 1.0), 5.0);
235:}
236:
237:// This calculates the specular geometric attenuation (aka G()),
238:// where rougher material will reflect less light back to the viewer.
239:// This implementation is based on [1] Equation 4, and we adopt their modifications to
240:// alphaRoughness as input as originally proposed in [2].
241:float geometricOcclusion(PBRInfo pbrInputs, PBRLightInfo pbrLight)
242:{
243: float NdotL = pbrLight.NdotL;
244: float NdotV = pbrLight.NdotV;
245: float r = pbrInputs.alphaRoughness;
246:
247: float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));
248: float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));
249: return attenuationL * attenuationV;
250:}
251:
252:// The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D())
253:// Implementation from "Average Irregularity Representation of a Roughened Surface for Ray Reflection" by T. S. Trowbridge, and K. P. Reitz
254:// Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3.
255:float microfacetDistribution(PBRInfo pbrInputs, PBRLightInfo pbrLight)
256:{
257: float roughnessSq = pbrInputs.alphaRoughness * pbrInputs.alphaRoughness;
258: float f = (pbrLight.NdotH * roughnessSq - pbrLight.NdotH) * pbrLight.NdotH + 1.0;
259: return roughnessSq / (M_PI * f * f);
260:}
261:
262:vec3 pbrModel(PBRInfo pbrInputs, vec3 lightColor, vec3 lightDir) {
263:
264: vec3 n = getNormal(); // normal at surface point
265: vec3 v = normalize(CamDir); // Vector from surface point to camera
266: vec3 l = normalize(lightDir); // Vector from surface point to light
267: vec3 h = normalize(l+v); // Half vector between both l and v
268: vec3 reflection = -normalize(reflect(v, n));
269:
270: float NdotL = clamp(dot(n, l), 0.001, 1.0);
271: float NdotV = abs(dot(n, v)) + 0.001;
272: float NdotH = clamp(dot(n, h), 0.0, 1.0);
273: float LdotH = clamp(dot(l, h), 0.0, 1.0);
274: float VdotH = clamp(dot(v, h), 0.0, 1.0);
275:
276: PBRLightInfo pbrLight = PBRLightInfo(
277: NdotL,
278: NdotV,
279: NdotH,
280: LdotH,
281: VdotH
282: );
283:
284: // Calculate the shading terms for the microfacet specular shading model
285: vec3 F = specularReflection(pbrInputs, pbrLight);
286: float G = geometricOcclusion(pbrInputs, pbrLight);
287: float D = microfacetDistribution(pbrInputs, pbrLight);
288:
289: // Calculation of analytical lighting contribution
290: vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInputs);
291: vec3 specContrib = F * G * D / (4.0 * NdotL * NdotV);
292: // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)
293: vec3 color = NdotL * lightColor * (diffuseContrib + specContrib);
294:
295: return color;
296:}
297:
298:void main() {
299:
300: float perceptualRoughness = uRoughnessFactor;
301: float metallic = uMetallicFactor;
302:
303:#ifdef HAS_METALROUGHNESSMAP
304: // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.
305: // This layout intentionally reserves the 'r' channel for (optional) occlusion map data
306: vec4 mrSample = texture2D(uMetallicRoughnessSampler, FragTexcoord);
307: perceptualRoughness = mrSample.g * perceptualRoughness;
308: metallic = mrSample.b * metallic;
309:#endif
310:
311: perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);
312: metallic = clamp(metallic, 0.0, 1.0);
313: // Roughness is authored as perceptual roughness; as is convention,
314: // convert to material roughness by squaring the perceptual roughness [2].
315: float alphaRoughness = perceptualRoughness * perceptualRoughness;
316:
317: // The albedo may be defined from a base texture or a flat color
318:#ifdef HAS_BASECOLORMAP
319: vec4 baseColor = SRGBtoLINEAR(texture2D(uBaseColorSampler, FragTexcoord)) * uBaseColor;
320:#else
321: vec4 baseColor = uBaseColor;
322:#endif
323:
324: vec3 f0 = vec3(0.04);
325: vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0);
326: diffuseColor *= 1.0 - metallic;
327:
328: vec3 specularColor = mix(f0, baseColor.rgb, uMetallicFactor);
329:
330: // Compute reflectance.
331: float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);
332:
333: // For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.
334: // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflectance to 0%.
335: float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);
336: vec3 specularEnvironmentR0 = specularColor.rgb;
337: vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
338:
339: PBRInfo pbrInputs = PBRInfo(
340: perceptualRoughness,
341: metallic,
342: specularEnvironmentR0,
343: specularEnvironmentR90,
344: alphaRoughness,
345: diffuseColor,
346: specularColor
347: );
348:
349:// vec3 normal = getNormal();
350: vec3 color = vec3(0.0);
351:
352:#if AMB_LIGHTS>0
353: // Ambient lights
354: for (int i = 0; i < AMB_LIGHTS; i++) {
355: color += AmbientLightColor[i] * pbrInputs.diffuseColor;
356: }
357:#endif
358:
359:#if DIR_LIGHTS>0
360: // Directional lights
361: for (int i = 0; i < DIR_LIGHTS; i++) {
362: // Diffuse reflection
363: // DirLightPosition is the direction of the current light
364: vec3 lightDirection = normalize(DirLightPosition(i));
365: // PBR
366: color += pbrModel(pbrInputs, DirLightColor(i), lightDirection);
367: }
368:#endif
369:
370:#if POINT_LIGHTS>0
371: // Point lights
372: for (int i = 0; i < POINT_LIGHTS; i++) {
373: // Common calculations
374: // Calculates the direction and distance from the current vertex to this point light.
375: vec3 lightDirection = PointLightPosition(i) - vec3(Position);
376: float lightDistance = length(lightDirection);
377: // Normalizes the lightDirection
378: lightDirection = lightDirection / lightDistance;
379: // Calculates the attenuation due to the distance of the light
380: float attenuation = 1.0 / (1.0 + PointLightLinearDecay(i) * lightDistance +
381: PointLightQuadraticDecay(i) * lightDistance * lightDistance);
382: vec3 attenuatedColor = PointLightColor(i) * attenuation;
383: // PBR
384: color += pbrModel(pbrInputs, attenuatedColor, lightDirection);
385: }
386:#endif
387:
388:#if SPOT_LIGHTS>0
389: for (int i = 0; i < SPOT_LIGHTS; i++) {
390:
391: // Calculates the direction and distance from the current vertex to this spot light.
392: vec3 lightDirection = SpotLightPosition(i) - vec3(Position);
393: float lightDistance = length(lightDirection);
394: lightDirection = lightDirection / lightDistance;
395:
396: // Calculates the attenuation due to the distance of the light
397: float attenuation = 1.0 / (1.0 + SpotLightLinearDecay(i) * lightDistance +
398: SpotLightQuadraticDecay(i) * lightDistance * lightDistance);
399:
400: // Calculates the angle between the vertex direction and spot direction
401: // If this angle is greater than the cutoff the spotlight will not contribute
402: // to the final color.
403: float angle = acos(dot(-lightDirection, SpotLightDirection(i)));
404: float cutoff = radians(clamp(SpotLightCutoffAngle(i), 0.0, 90.0));
405:
406: if (angle < cutoff) {
407: float spotFactor = pow(dot(-lightDirection, SpotLightDirection(i)), SpotLightAngularDecay(i));
408: vec3 attenuatedColor = SpotLightColor(i) * attenuation * spotFactor;
409: // PBR
410: color += pbrModel(pbrInputs, attenuatedColor, lightDirection);
411: }
412: }
413:#endif
414:
415: // Calculate lighting contribution from image based lighting source (IBL)
416://#ifdef USE_IBL
417:// color += getIBLContribution(pbrInputs, n, reflection);
418://#endif
419:
420: // Apply optional PBR terms for additional (optional) shading
421:#ifdef HAS_OCCLUSIONMAP
422: float ao = texture2D(uOcclusionSampler, FragTexcoord).r;
423: color = mix(color, color * ao, 1.0);//, uOcclusionStrength);
424:#endif
425:
426:#ifdef HAS_EMISSIVEMAP
427: vec3 emissive = SRGBtoLINEAR(texture2D(uEmissiveSampler, FragTexcoord)).rgb * vec3(uEmissiveColor);
428:#else
429: vec3 emissive = vec3(uEmissiveColor);
430:#endif
431: color += emissive;
432:
433: // Base Color
434:// FragColor = baseColor;
435:
436: // Normal
437:// FragColor = vec4(n, 1.0);
438:
439: // Emissive Color
440:// FragColor = vec4(emissive, 1.0);
441:
442: // F
443:// color = F;
444:
445: // G
446:// color = vec3(G);
447:
448: // D
449:// color = vec3(D);
450:
451: // Specular
452:// color = specContrib;
453:
454: // Diffuse
455:// color = diffuseContrib;
456:
457: // Roughness
458:// color = vec3(perceptualRoughness);
459:
460: // Metallic
461:// color = vec3(metallic);
462:
463: // Final fragment color
464: FragColor = vec4(pow(color,vec3(1.0/2.2)), baseColor.a);
465:}
466:
467:
468:
goroutine 1 [running, locked to thread]:
main.main()
/Users/dobegor/go/src/github.com/g3n/g3nd/main.go:29 +0x70
exit status 2```