sources.go 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. // File generated by G3NSHADERS. Do not edit.
  2. // To regenerate this file install 'g3nshaders' and execute:
  3. // 'go generate' in this folder.
  4. package shaders
  5. const include_attributes_source = `//
  6. // Vertex attributes
  7. //
  8. layout(location = 0) in vec3 VertexPosition;
  9. layout(location = 1) in vec3 VertexNormal;
  10. layout(location = 2) in vec3 VertexColor;
  11. layout(location = 3) in vec2 VertexTexcoord;
  12. layout(location = 4) in float VertexDistance;
  13. layout(location = 5) in vec4 VertexTexoffsets;
  14. `
  15. const include_lights_source = `//
  16. // Lights uniforms
  17. //
  18. // Ambient lights uniforms
  19. #if AMB_LIGHTS>0
  20. uniform vec3 AmbientLightColor[AMB_LIGHTS];
  21. #endif
  22. // Directional lights uniform array. Each directional light uses 2 elements
  23. #if DIR_LIGHTS>0
  24. uniform vec3 DirLight[2*DIR_LIGHTS];
  25. // Macros to access elements inside the DirectionalLight uniform array
  26. #define DirLightColor(a) DirLight[2*a]
  27. #define DirLightPosition(a) DirLight[2*a+1]
  28. #endif
  29. // Point lights uniform array. Each point light uses 3 elements
  30. #if POINT_LIGHTS>0
  31. uniform vec3 PointLight[3*POINT_LIGHTS];
  32. // Macros to access elements inside the PointLight uniform array
  33. #define PointLightColor(a) PointLight[3*a]
  34. #define PointLightPosition(a) PointLight[3*a+1]
  35. #define PointLightLinearDecay(a) PointLight[3*a+2].x
  36. #define PointLightQuadraticDecay(a) PointLight[3*a+2].y
  37. #endif
  38. #if SPOT_LIGHTS>0
  39. // Spot lights uniforms. Each spot light uses 5 elements
  40. uniform vec3 SpotLight[5*SPOT_LIGHTS];
  41. // Macros to access elements inside the PointLight uniform array
  42. #define SpotLightColor(a) SpotLight[5*a]
  43. #define SpotLightPosition(a) SpotLight[5*a+1]
  44. #define SpotLightDirection(a) SpotLight[5*a+2]
  45. #define SpotLightAngularDecay(a) SpotLight[5*a+3].x
  46. #define SpotLightCutoffAngle(a) SpotLight[5*a+3].y
  47. #define SpotLightLinearDecay(a) SpotLight[5*a+3].z
  48. #define SpotLightQuadraticDecay(a) SpotLight[5*a+4].x
  49. #endif
  50. `
  51. const include_material_source = `//
  52. // Material properties uniform
  53. //
  54. // Material parameters uniform array
  55. uniform vec3 Material[6];
  56. // Macros to access elements inside the Material array
  57. #define MatAmbientColor Material[0]
  58. #define MatDiffuseColor Material[1]
  59. #define MatSpecularColor Material[2]
  60. #define MatEmissiveColor Material[3]
  61. #define MatShininess Material[4].x
  62. #define MatOpacity Material[4].y
  63. #define MatPointSize Material[4].z
  64. #define MatPointRotationZ Material[5].x
  65. #if MAT_TEXTURES > 0
  66. // Texture unit sampler array
  67. uniform sampler2D MatTexture[MAT_TEXTURES];
  68. // Texture parameters (3*vec2 per texture)
  69. uniform vec2 MatTexinfo[3*MAT_TEXTURES];
  70. // Macros to access elements inside the MatTexinfo array
  71. #define MatTexOffset(a) MatTexinfo[(3*a)]
  72. #define MatTexRepeat(a) MatTexinfo[(3*a)+1]
  73. #define MatTexFlipY(a) bool(MatTexinfo[(3*a)+2].x)
  74. #define MatTexVisible(a) bool(MatTexinfo[(3*a)+2].y)
  75. #endif
  76. // GLSL 3.30 does not allow indexing texture sampler with non constant values.
  77. // This macro is used to mix the texture with the specified index with the material color.
  78. // It should be called for each texture index. It uses two externally defined variables:
  79. // vec4 texColor
  80. // vec4 texMixed
  81. #define MIX_TEXTURE(i) \
  82. if (MatTexVisible(i)) { \
  83. texColor = texture(MatTexture[i], FragTexcoord * MatTexRepeat(i) + MatTexOffset(i)); \
  84. if (i == 0) { \
  85. texMixed = texColor; \
  86. } else { \
  87. texMixed = mix(texMixed, texColor, texColor.a); \
  88. } \
  89. }
  90. `
  91. const include_phong_model_source = `/***
  92. phong lighting model
  93. Parameters:
  94. position: input vertex position in camera coordinates
  95. normal: input vertex normal in camera coordinates
  96. camDir: input camera directions
  97. matAmbient: input material ambient color
  98. matDiffuse: input material diffuse color
  99. ambdiff: output ambient+diffuse color
  100. spec: output specular color
  101. Uniforms:
  102. AmbientLightColor[]
  103. DiffuseLightColor[]
  104. DiffuseLightPosition[]
  105. PointLightColor[]
  106. PointLightPosition[]
  107. PointLightLinearDecay[]
  108. PointLightQuadraticDecay[]
  109. MatSpecularColor
  110. MatShininess
  111. *****/
  112. void phongModel(vec4 position, vec3 normal, vec3 camDir, vec3 matAmbient, vec3 matDiffuse, out vec3 ambdiff, out vec3 spec) {
  113. vec3 ambientTotal = vec3(0.0);
  114. vec3 diffuseTotal = vec3(0.0);
  115. vec3 specularTotal = vec3(0.0);
  116. #if AMB_LIGHTS>0
  117. // Ambient lights
  118. for (int i = 0; i < AMB_LIGHTS; i++) {
  119. ambientTotal += AmbientLightColor[i] * matAmbient;
  120. }
  121. #endif
  122. #if DIR_LIGHTS>0
  123. // Directional lights
  124. for (int i = 0; i < DIR_LIGHTS; i++) {
  125. // Diffuse reflection
  126. // DirLightPosition is the direction of the current light
  127. vec3 lightDirection = normalize(DirLightPosition(i));
  128. // Calculates the dot product between the light direction and this vertex normal.
  129. float dotNormal = max(dot(lightDirection, normal), 0.0);
  130. diffuseTotal += DirLightColor(i) * matDiffuse * dotNormal;
  131. // Specular reflection
  132. // Calculates the light reflection vector
  133. vec3 ref = reflect(-lightDirection, normal);
  134. if (dotNormal > 0.0) {
  135. specularTotal += DirLightColor(i) * MatSpecularColor * pow(max(dot(ref, camDir), 0.0), MatShininess);
  136. }
  137. }
  138. #endif
  139. #if POINT_LIGHTS>0
  140. // Point lights
  141. for (int i = 0; i < POINT_LIGHTS; i++) {
  142. // Common calculations
  143. // Calculates the direction and distance from the current vertex to this point light.
  144. vec3 lightDirection = PointLightPosition(i) - vec3(position);
  145. float lightDistance = length(lightDirection);
  146. // Normalizes the lightDirection
  147. lightDirection = lightDirection / lightDistance;
  148. // Calculates the attenuation due to the distance of the light
  149. float attenuation = 1.0 / (1.0 + PointLightLinearDecay(i) * lightDistance +
  150. PointLightQuadraticDecay(i) * lightDistance * lightDistance);
  151. // Diffuse reflection
  152. float dotNormal = max(dot(lightDirection, normal), 0.0);
  153. diffuseTotal += PointLightColor(i) * matDiffuse * dotNormal * attenuation;
  154. // Specular reflection
  155. // Calculates the light reflection vector
  156. vec3 ref = reflect(-lightDirection, normal);
  157. if (dotNormal > 0.0) {
  158. specularTotal += PointLightColor(i) * MatSpecularColor *
  159. pow(max(dot(ref, camDir), 0.0), MatShininess) * attenuation;
  160. }
  161. }
  162. #endif
  163. #if SPOT_LIGHTS>0
  164. for (int i = 0; i < SPOT_LIGHTS; i++) {
  165. // Calculates the direction and distance from the current vertex to this spot light.
  166. vec3 lightDirection = SpotLightPosition(i) - vec3(position);
  167. float lightDistance = length(lightDirection);
  168. lightDirection = lightDirection / lightDistance;
  169. // Calculates the attenuation due to the distance of the light
  170. float attenuation = 1.0 / (1.0 + SpotLightLinearDecay(i) * lightDistance +
  171. SpotLightQuadraticDecay(i) * lightDistance * lightDistance);
  172. // Calculates the angle between the vertex direction and spot direction
  173. // If this angle is greater than the cutoff the spotlight will not contribute
  174. // to the final color.
  175. float angle = acos(dot(-lightDirection, SpotLightDirection(i)));
  176. float cutoff = radians(clamp(SpotLightCutoffAngle(i), 0.0, 90.0));
  177. if (angle < cutoff) {
  178. float spotFactor = pow(dot(-lightDirection, SpotLightDirection(i)), SpotLightAngularDecay(i));
  179. // Diffuse reflection
  180. float dotNormal = max(dot(lightDirection, normal), 0.0);
  181. diffuseTotal += SpotLightColor(i) * matDiffuse * dotNormal * attenuation * spotFactor;
  182. // Specular reflection
  183. vec3 ref = reflect(-lightDirection, normal);
  184. if (dotNormal > 0.0) {
  185. specularTotal += SpotLightColor(i) * MatSpecularColor * pow(max(dot(ref, camDir), 0.0), MatShininess) * attenuation * spotFactor;
  186. }
  187. }
  188. }
  189. #endif
  190. // Sets output colors
  191. ambdiff = ambientTotal + MatEmissiveColor + diffuseTotal;
  192. spec = specularTotal;
  193. }
  194. `
  195. const basic_fragment_source = `//
  196. // Fragment Shader template
  197. //
  198. in vec3 Color;
  199. out vec4 FragColor;
  200. void main() {
  201. FragColor = vec4(Color, 1.0);
  202. }
  203. `
  204. const basic_vertex_source = `//
  205. // Vertex shader basic
  206. //
  207. #include <attributes>
  208. // Model uniforms
  209. uniform mat4 MVP;
  210. // Final output color for fragment shader
  211. out vec3 Color;
  212. void main() {
  213. Color = VertexColor;
  214. gl_Position = MVP * vec4(VertexPosition, 1.0);
  215. }
  216. `
  217. const panel_fragment_source = `//
  218. // Fragment Shader template
  219. //
  220. // Texture uniforms
  221. uniform sampler2D MatTexture;
  222. uniform vec2 MatTexinfo[3];
  223. // Macros to access elements inside the MatTexinfo array
  224. #define MatTexOffset MatTexinfo[0]
  225. #define MatTexRepeat MatTexinfo[1]
  226. #define MatTexFlipY bool(MatTexinfo[2].x) // not used
  227. #define MatTexVisible bool(MatTexinfo[2].y) // not used
  228. // Inputs from vertex shader
  229. in vec2 FragTexcoord;
  230. // Input uniform
  231. uniform vec4 Panel[8];
  232. #define Bounds Panel[0] // panel bounds in texture coordinates
  233. #define Border Panel[1] // panel border in texture coordinates
  234. #define Padding Panel[2] // panel padding in texture coordinates
  235. #define Content Panel[3] // panel content area in texture coordinates
  236. #define BorderColor Panel[4] // panel border color
  237. #define PaddingColor Panel[5] // panel padding color
  238. #define ContentColor Panel[6] // panel content color
  239. #define TextureValid bool(Panel[7].x) // texture valid flag
  240. // Output
  241. out vec4 FragColor;
  242. /***
  243. * Checks if current fragment texture coordinate is inside the
  244. * supplied rectangle in texture coordinates:
  245. * rect[0] - position x [0,1]
  246. * rect[1] - position y [0,1]
  247. * rect[2] - width [0,1]
  248. * rect[3] - height [0,1]
  249. */
  250. bool checkRect(vec4 rect) {
  251. if (FragTexcoord.x < rect[0]) {
  252. return false;
  253. }
  254. if (FragTexcoord.x > rect[0] + rect[2]) {
  255. return false;
  256. }
  257. if (FragTexcoord.y < rect[1]) {
  258. return false;
  259. }
  260. if (FragTexcoord.y > rect[1] + rect[3]) {
  261. return false;
  262. }
  263. return true;
  264. }
  265. void main() {
  266. // Discard fragment outside of received bounds
  267. // Bounds[0] - xmin
  268. // Bounds[1] - ymin
  269. // Bounds[2] - xmax
  270. // Bounds[3] - ymax
  271. if (FragTexcoord.x <= Bounds[0] || FragTexcoord.x >= Bounds[2]) {
  272. discard;
  273. }
  274. if (FragTexcoord.y <= Bounds[1] || FragTexcoord.y >= Bounds[3]) {
  275. discard;
  276. }
  277. // Check if fragment is inside content area
  278. if (checkRect(Content)) {
  279. // If no texture, the color will be the material color.
  280. vec4 color = ContentColor;
  281. if (TextureValid) {
  282. // Adjust texture coordinates to fit texture inside the content area
  283. vec2 offset = vec2(-Content[0], -Content[1]);
  284. vec2 factor = vec2(1/Content[2], 1/Content[3]);
  285. vec2 texcoord = (FragTexcoord + offset) * factor;
  286. vec4 texColor = texture(MatTexture, texcoord * MatTexRepeat + MatTexOffset);
  287. // Mix content color with texture color.
  288. // Note that doing a simple linear interpolation (e.g. using mix()) is not correct!
  289. // The right formula can be found here: https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
  290. // For a more in-depth discussion: http://apoorvaj.io/alpha-compositing-opengl-blending-and-premultiplied-alpha.html#toc4
  291. // Pre-multiply the content color
  292. vec4 contentPre = ContentColor;
  293. contentPre.rgb *= contentPre.a;
  294. // Pre-multiply the texture color
  295. vec4 texPre = texColor;
  296. texPre.rgb *= texPre.a;
  297. // Combine colors the premultiplied final color
  298. color = texPre + contentPre * (1 - texPre.a);
  299. // Un-pre-multiply (pre-divide? :P)
  300. color.rgb /= color.a;
  301. }
  302. FragColor = color;
  303. return;
  304. }
  305. // Checks if fragment is inside paddings area
  306. if (checkRect(Padding)) {
  307. FragColor = PaddingColor;
  308. return;
  309. }
  310. // Checks if fragment is inside borders area
  311. if (checkRect(Border)) {
  312. FragColor = BorderColor;
  313. return;
  314. }
  315. // Fragment is in margins area (always transparent)
  316. FragColor = vec4(1,1,1,0);
  317. }
  318. `
  319. const panel_vertex_source = `//
  320. // Vertex shader panel
  321. //
  322. #include <attributes>
  323. // Model uniforms
  324. uniform mat4 ModelMatrix;
  325. // Outputs for fragment shader
  326. out vec2 FragTexcoord;
  327. void main() {
  328. // Always flip texture coordinates
  329. vec2 texcoord = VertexTexcoord;
  330. texcoord.y = 1 - texcoord.y;
  331. FragTexcoord = texcoord;
  332. // Set position
  333. vec4 pos = vec4(VertexPosition.xyz, 1);
  334. gl_Position = ModelMatrix * pos;
  335. }
  336. `
  337. const phong_fragment_source = `//
  338. // Fragment Shader template
  339. //
  340. // Inputs from vertex shader
  341. in vec4 Position; // Vertex position in camera coordinates.
  342. in vec3 Normal; // Vertex normal in camera coordinates.
  343. in vec3 CamDir; // Direction from vertex to camera
  344. in vec2 FragTexcoord;
  345. #include <lights>
  346. #include <material>
  347. #include <phong_model>
  348. // Final fragment color
  349. out vec4 FragColor;
  350. void main() {
  351. // Mix material color with textures colors
  352. vec4 texMixed = vec4(1);
  353. vec4 texColor;
  354. #if MAT_TEXTURES==1
  355. MIX_TEXTURE(0)
  356. #elif MAT_TEXTURES==2
  357. MIX_TEXTURE(0)
  358. MIX_TEXTURE(1)
  359. #elif MAT_TEXTURES==3
  360. MIX_TEXTURE(0)
  361. MIX_TEXTURE(1)
  362. MIX_TEXTURE(2)
  363. #endif
  364. // Combine material with texture colors
  365. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texMixed;
  366. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texMixed;
  367. // Inverts the fragment normal if not FrontFacing
  368. vec3 fragNormal = Normal;
  369. if (!gl_FrontFacing) {
  370. fragNormal = -fragNormal;
  371. }
  372. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  373. vec3 Ambdiff, Spec;
  374. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  375. // Final fragment color
  376. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  377. }
  378. `
  379. const phong_vertex_source = `//
  380. // Vertex Shader
  381. //
  382. #include <attributes>
  383. // Model uniforms
  384. uniform mat4 ModelViewMatrix;
  385. uniform mat3 NormalMatrix;
  386. uniform mat4 MVP;
  387. #include <material>
  388. // Output variables for Fragment shader
  389. out vec4 Position;
  390. out vec3 Normal;
  391. out vec3 CamDir;
  392. out vec2 FragTexcoord;
  393. void main() {
  394. // Transform this vertex position to camera coordinates.
  395. Position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  396. // Transform this vertex normal to camera coordinates.
  397. Normal = normalize(NormalMatrix * VertexNormal);
  398. // Calculate the direction vector from the vertex to the camera
  399. // The camera is at 0,0,0
  400. CamDir = normalize(-Position.xyz);
  401. // Flips texture coordinate Y if requested.
  402. vec2 texcoord = VertexTexcoord;
  403. #if MAT_TEXTURES>0
  404. if (MatTexFlipY(0)) {
  405. texcoord.y = 1 - texcoord.y;
  406. }
  407. #endif
  408. FragTexcoord = texcoord;
  409. gl_Position = MVP * vec4(VertexPosition, 1.0);
  410. }
  411. `
  412. const physical_fragment_source = `//
  413. // Physically Based Shading of a microfacet surface material - Fragment Shader
  414. // Modified from reference implementation at https://github.com/KhronosGroup/glTF-WebGL-PBR
  415. //
  416. // References:
  417. // [1] Real Shading in Unreal Engine 4
  418. // http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
  419. // [2] Physically Based Shading at Disney
  420. // http://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf
  421. // [3] README.md - Environment Maps
  422. // https://github.com/KhronosGroup/glTF-WebGL-PBR/#environment-maps
  423. // [4] "An Inexpensive BRDF Model for Physically based Rendering" by Christophe Schlick
  424. // https://www.cs.virginia.edu/~jdl/bib/appearance/analytic%20models/schlick94b.pdf
  425. //#extension GL_EXT_shader_texture_lod: enable
  426. //#extension GL_OES_standard_derivatives : enable
  427. precision highp float;
  428. //uniform vec3 u_LightDirection;
  429. //uniform vec3 u_LightColor;
  430. //#ifdef USE_IBL
  431. //uniform samplerCube u_DiffuseEnvSampler;
  432. //uniform samplerCube u_SpecularEnvSampler;
  433. //uniform sampler2D u_brdfLUT;
  434. //#endif
  435. #ifdef HAS_BASECOLORMAP
  436. uniform sampler2D u_BaseColorSampler;
  437. #endif
  438. #ifdef HAS_NORMALMAP
  439. uniform sampler2D u_NormalSampler;
  440. uniform float u_NormalScale;
  441. #endif
  442. #ifdef HAS_EMISSIVEMAP
  443. uniform sampler2D u_EmissiveSampler;
  444. uniform vec3 u_EmissiveFactor;
  445. #endif
  446. #ifdef HAS_METALROUGHNESSMAP
  447. uniform sampler2D u_MetallicRoughnessSampler;
  448. #endif
  449. #ifdef HAS_OCCLUSIONMAP
  450. uniform sampler2D u_OcclusionSampler;
  451. uniform float u_OcclusionStrength;
  452. #endif
  453. // Material parameters uniform array
  454. uniform vec4 Material[3];
  455. // Macros to access elements inside the Material array
  456. #define uBaseColor Material[0]
  457. #define uEmissiveColor Material[1]
  458. #define uMetallicFactor Material[2].x
  459. #define uRoughnessFactor Material[2].y
  460. #include <lights>
  461. // Inputs from vertex shader
  462. in vec3 Position; // Vertex position in camera coordinates.
  463. in vec3 Normal; // Vertex normal in camera coordinates.
  464. in vec3 CamDir; // Direction from vertex to camera
  465. in vec2 FragTexcoord;
  466. // Final fragment color
  467. out vec4 FragColor;
  468. // Encapsulate the various inputs used by the various functions in the shading equation
  469. // We store values in this struct to simplify the integration of alternative implementations
  470. // of the shading terms, outlined in the Readme.MD Appendix.
  471. struct PBRInfo
  472. {
  473. float NdotL; // cos angle between normal and light direction
  474. float NdotV; // cos angle between normal and view direction
  475. float NdotH; // cos angle between normal and half vector
  476. float LdotH; // cos angle between light direction and half vector
  477. float VdotH; // cos angle between view direction and half vector
  478. float perceptualRoughness; // roughness value, as authored by the model creator (input to shader)
  479. float metalness; // metallic value at the surface
  480. vec3 reflectance0; // full reflectance color (normal incidence angle)
  481. vec3 reflectance90; // reflectance color at grazing angle
  482. float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2])
  483. vec3 diffuseColor; // color contribution from diffuse lighting
  484. vec3 specularColor; // color contribution from specular lighting
  485. };
  486. const float M_PI = 3.141592653589793;
  487. const float c_MinRoughness = 0.04;
  488. vec4 SRGBtoLINEAR(vec4 srgbIn) {
  489. //#ifdef MANUAL_SRGB
  490. // #ifdef SRGB_FAST_APPROXIMATION
  491. // vec3 linOut = pow(srgbIn.xyz,vec3(2.2));
  492. // #else //SRGB_FAST_APPROXIMATION
  493. // vec3 bLess = step(vec3(0.04045),srgbIn.xyz);
  494. // vec3 linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );
  495. // #endif //SRGB_FAST_APPROXIMATION
  496. // return vec4(linOut,srgbIn.w);
  497. //#else //MANUAL_SRGB
  498. return srgbIn;
  499. //#endif //MANUAL_SRGB
  500. }
  501. // Find the normal for this fragment, pulling either from a predefined normal map
  502. // or from the interpolated mesh normal and tangent attributes.
  503. vec3 getNormal()
  504. {
  505. // Retrieve the tangent space matrix
  506. //#ifndef HAS_TANGENTS
  507. vec3 pos_dx = dFdx(Position);
  508. vec3 pos_dy = dFdy(Position);
  509. vec3 tex_dx = dFdx(vec3(FragTexcoord, 0.0));
  510. vec3 tex_dy = dFdy(vec3(FragTexcoord, 0.0));
  511. vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);
  512. #ifdef HAS_NORMALS
  513. vec3 ng = normalize(Normal);
  514. #else
  515. vec3 ng = cross(pos_dx, pos_dy);
  516. #endif
  517. t = normalize(t - ng * dot(ng, t));
  518. vec3 b = normalize(cross(ng, t));
  519. mat3 tbn = mat3(t, b, ng);
  520. //#else // HAS_TANGENTS
  521. // mat3 tbn = v_TBN;
  522. //#endif
  523. #ifdef HAS_NORMALMAP
  524. vec3 n = texture2D(uNormalSampler, FragTexcoord).rgb;
  525. n = normalize(tbn * ((2.0 * n - 1.0) * vec3(uNormalScale, uNormalScale, 1.0)));
  526. #else
  527. // The tbn matrix is linearly interpolated, so we need to re-normalize
  528. vec3 n = normalize(tbn[2].xyz);
  529. #endif
  530. return n;
  531. }
  532. //// Calculation of the lighting contribution from an optional Image Based Light source.
  533. //// Precomputed Environment Maps are required uniform inputs and are computed as outlined in [1].
  534. //// See our README.md on Environment Maps [3] for additional discussion.
  535. //vec3 getIBLContribution(PBRInfo pbrInputs, vec3 n, vec3 reflection)
  536. //{
  537. // float mipCount = 9.0; // resolution of 512x512
  538. // float lod = (pbrInputs.perceptualRoughness * mipCount);
  539. // // retrieve a scale and bias to F0. See [1], Figure 3
  540. // vec3 brdf = SRGBtoLINEAR(texture2D(u_brdfLUT, vec2(pbrInputs.NdotV, 1.0 - pbrInputs.perceptualRoughness))).rgb;
  541. // vec3 diffuseLight = SRGBtoLINEAR(textureCube(u_DiffuseEnvSampler, n)).rgb;
  542. //
  543. ////#ifdef USE_TEX_LOD
  544. //// vec3 specularLight = SRGBtoLINEAR(textureCubeLodEXT(u_SpecularEnvSampler, reflection, lod)).rgb;
  545. ////#else
  546. // vec3 specularLight = SRGBtoLINEAR(textureCube(u_SpecularEnvSampler, reflection)).rgb;
  547. ////#endif
  548. //
  549. // vec3 diffuse = diffuseLight * pbrInputs.diffuseColor;
  550. // vec3 specular = specularLight * (pbrInputs.specularColor * brdf.x + brdf.y);
  551. //
  552. // // For presentation, this allows us to disable IBL terms
  553. // diffuse *= u_ScaleIBLAmbient.x;
  554. // specular *= u_ScaleIBLAmbient.y;
  555. //
  556. // return diffuse + specular;
  557. //}
  558. // Basic Lambertian diffuse
  559. // Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog
  560. // See also [1], Equation 1
  561. vec3 diffuse(PBRInfo pbrInputs)
  562. {
  563. return pbrInputs.diffuseColor / M_PI;
  564. }
  565. // The following equation models the Fresnel reflectance term of the spec equation (aka F())
  566. // Implementation of fresnel from [4], Equation 15
  567. vec3 specularReflection(PBRInfo pbrInputs)
  568. {
  569. return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance0) * pow(clamp(1.0 - pbrInputs.VdotH, 0.0, 1.0), 5.0);
  570. }
  571. // This calculates the specular geometric attenuation (aka G()),
  572. // where rougher material will reflect less light back to the viewer.
  573. // This implementation is based on [1] Equation 4, and we adopt their modifications to
  574. // alphaRoughness as input as originally proposed in [2].
  575. float geometricOcclusion(PBRInfo pbrInputs)
  576. {
  577. float NdotL = pbrInputs.NdotL;
  578. float NdotV = pbrInputs.NdotV;
  579. float r = pbrInputs.alphaRoughness;
  580. float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));
  581. float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));
  582. return attenuationL * attenuationV;
  583. }
  584. // The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D())
  585. // Implementation from "Average Irregularity Representation of a Roughened Surface for Ray Reflection" by T. S. Trowbridge, and K. P. Reitz
  586. // Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3.
  587. float microfacetDistribution(PBRInfo pbrInputs)
  588. {
  589. float roughnessSq = pbrInputs.alphaRoughness * pbrInputs.alphaRoughness;
  590. float f = (pbrInputs.NdotH * roughnessSq - pbrInputs.NdotH) * pbrInputs.NdotH + 1.0;
  591. return roughnessSq / (M_PI * f * f);
  592. }
  593. void main() {
  594. float perceptualRoughness = uRoughnessFactor;
  595. float metallic = uMetallicFactor;
  596. #ifdef HAS_METALROUGHNESSMAP
  597. // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.
  598. // This layout intentionally reserves the 'r' channel for (optional) occlusion map data
  599. vec4 mrSample = texture2D(uMetallicRoughnessSampler, FragTexcoord);
  600. perceptualRoughness = mrSample.g * perceptualRoughness;
  601. metallic = mrSample.b * metallic;
  602. #endif
  603. perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);
  604. metallic = clamp(metallic, 0.0, 1.0);
  605. // Roughness is authored as perceptual roughness; as is convention,
  606. // convert to material roughness by squaring the perceptual roughness [2].
  607. float alphaRoughness = perceptualRoughness * perceptualRoughness;
  608. // The albedo may be defined from a base texture or a flat color
  609. #ifdef HAS_BASECOLORMAP
  610. vec4 baseColor = SRGBtoLINEAR(texture2D(uBaseColorSampler, FragTexcoord)) * uBaseColor;
  611. #else
  612. vec4 baseColor = uBaseColor;
  613. #endif
  614. vec3 f0 = vec3(0.04);
  615. vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0);
  616. diffuseColor *= 1.0 - metallic;
  617. vec3 specularColor = mix(f0, baseColor.rgb, uMetallicFactor);
  618. // Compute reflectance.
  619. float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);
  620. // For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.
  621. // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%.
  622. float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);
  623. vec3 specularEnvironmentR0 = specularColor.rgb;
  624. vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
  625. // TODO These are not currently uniforms - and need to support all kinds of lights!
  626. vec3 u_LightColor = vec3(1,1,1);
  627. vec3 u_LightDirection = vec3(1,0,0);
  628. vec3 n = getNormal(); // normal at surface point
  629. vec3 v = normalize(CamDir); // Vector from surface point to camera
  630. vec3 l = normalize(u_LightDirection); // Vector from surface point to light
  631. vec3 h = normalize(l+v); // Half vector between both l and v
  632. vec3 reflection = -normalize(reflect(v, n));
  633. float NdotL = clamp(dot(n, l), 0.001, 1.0);
  634. float NdotV = abs(dot(n, v)) + 0.001;
  635. float NdotH = clamp(dot(n, h), 0.0, 1.0);
  636. float LdotH = clamp(dot(l, h), 0.0, 1.0);
  637. float VdotH = clamp(dot(v, h), 0.0, 1.0);
  638. PBRInfo pbrInputs = PBRInfo(
  639. NdotL,
  640. NdotV,
  641. NdotH,
  642. LdotH,
  643. VdotH,
  644. perceptualRoughness,
  645. metallic,
  646. specularEnvironmentR0,
  647. specularEnvironmentR90,
  648. alphaRoughness,
  649. diffuseColor,
  650. specularColor
  651. );
  652. // Calculate the shading terms for the microfacet specular shading model
  653. vec3 F = specularReflection(pbrInputs);
  654. float G = geometricOcclusion(pbrInputs);
  655. float D = microfacetDistribution(pbrInputs);
  656. // Calculation of analytical lighting contribution
  657. vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInputs);
  658. vec3 specContrib = F * G * D / (4.0 * NdotL * NdotV);
  659. // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)
  660. vec3 color = NdotL * u_LightColor * (diffuseContrib + specContrib);
  661. // Calculate lighting contribution from image based lighting source (IBL)
  662. //#ifdef USE_IBL
  663. // color += getIBLContribution(pbrInputs, n, reflection);
  664. //#endif
  665. // Apply optional PBR terms for additional (optional) shading
  666. #ifdef HAS_OCCLUSIONMAP
  667. float ao = texture2D(uOcclusionSampler, v_UV).r;
  668. color = mix(color, color * ao, u_OcclusionStrength);
  669. #endif
  670. #ifdef HAS_EMISSIVEMAP
  671. vec3 emissive = SRGBtoLINEAR(texture2D(u_EmissiveSampler, v_UV)).rgb * u_EmissiveFactor;
  672. color += emissive;
  673. #endif
  674. // Final fragment color
  675. FragColor = vec4(pow(color,vec3(1.0/2.2)), baseColor.a);
  676. }
  677. `
  678. const physical_vertex_source = `//
  679. // Physically Based Shading of a microfacet surface material - Vertex Shader
  680. // Modified from reference implementation at https://github.com/KhronosGroup/glTF-WebGL-PBR
  681. //
  682. #include <attributes>
  683. // Model uniforms
  684. uniform mat4 ModelViewMatrix;
  685. uniform mat3 NormalMatrix;
  686. uniform mat4 MVP;
  687. // Output variables for Fragment shader
  688. out vec3 Position;
  689. out vec3 Normal;
  690. out vec3 CamDir;
  691. out vec2 FragTexcoord;
  692. void main() {
  693. // Transform this vertex position to camera coordinates.
  694. Position = vec3(ModelViewMatrix * vec4(VertexPosition, 1.0));
  695. // Transform this vertex normal to camera coordinates.
  696. Normal = normalize(NormalMatrix * VertexNormal);
  697. // Calculate the direction vector from the vertex to the camera
  698. // The camera is at 0,0,0
  699. CamDir = normalize(-Position.xyz);
  700. // Flips texture coordinate Y if requested.
  701. vec2 texcoord = VertexTexcoord;
  702. // #if MAT_TEXTURES>0
  703. // if (MatTexFlipY(0)) {
  704. // texcoord.y = 1 - texcoord.y;
  705. // }
  706. // #endif
  707. FragTexcoord = texcoord;
  708. gl_Position = MVP * vec4(VertexPosition, 1.0);
  709. }
  710. `
  711. const point_fragment_source = `#include <material>
  712. // GLSL 3.30 does not allow indexing texture sampler with non constant values.
  713. // This macro is used to mix the texture with the specified index with the material color.
  714. // It should be called for each texture index.
  715. #define MIX_POINT_TEXTURE(i) \
  716. if (MatTexVisible(i)) { \
  717. vec2 pt = gl_PointCoord - vec2(0.5); \
  718. vec4 texColor = texture(MatTexture[i], (Rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i)); \
  719. if (i == 0) { \
  720. texMixed = texColor; \
  721. } else { \
  722. texMixed = mix(texMixed, texColor, texColor.a); \
  723. } \
  724. }
  725. // Inputs from vertex shader
  726. in vec3 Color;
  727. flat in mat2 Rotation;
  728. // Output
  729. out vec4 FragColor;
  730. void main() {
  731. // Mix material color with textures colors
  732. vec4 texMixed = vec4(1);
  733. #if MAT_TEXTURES==1
  734. MIX_POINT_TEXTURE(0)
  735. #elif MAT_TEXTURES==2
  736. MIX_POINT_TEXTURE(0)
  737. MIX_POINT_TEXTURE(1)
  738. #elif MAT_TEXTURES==3
  739. MIX_POINT_TEXTURE(0)
  740. MIX_POINT_TEXTURE(1)
  741. MIX_POINT_TEXTURE(2)
  742. #endif
  743. // Generates final color
  744. FragColor = min(vec4(Color, MatOpacity) * texMixed, vec4(1));
  745. }
  746. `
  747. const point_vertex_source = `#include <attributes>
  748. // Model uniforms
  749. uniform mat4 MVP;
  750. // Material uniforms
  751. #include <material>
  752. // Outputs for fragment shader
  753. out vec3 Color;
  754. flat out mat2 Rotation;
  755. void main() {
  756. // Rotation matrix for fragment shader
  757. float rotSin = sin(MatPointRotationZ);
  758. float rotCos = cos(MatPointRotationZ);
  759. Rotation = mat2(rotCos, rotSin, - rotSin, rotCos);
  760. // Sets the vertex position
  761. vec4 pos = MVP * vec4(VertexPosition, 1.0);
  762. gl_Position = pos;
  763. // Sets the size of the rasterized point decreasing with distance
  764. gl_PointSize = (1.0 - pos.z / pos.w) * MatPointSize;
  765. // Outputs color
  766. Color = MatEmissiveColor;
  767. }
  768. `
  769. const sprite_fragment_source = `//
  770. // Fragment shader for sprite
  771. //
  772. #include <material>
  773. // Inputs from vertex shader
  774. in vec3 Color;
  775. in vec2 FragTexcoord;
  776. // Output
  777. out vec4 FragColor;
  778. void main() {
  779. // Combine all texture colors and opacity
  780. vec4 texCombined = vec4(1);
  781. #if MAT_TEXTURES>0
  782. for (int i = 0; i < {{.MatTexturesMax}}; i++) {
  783. vec4 texcolor = texture(MatTexture[i], FragTexcoord * MatTexRepeat(i) + MatTexOffset(i));
  784. if (i == 0) {
  785. texCombined = texcolor;
  786. } else {
  787. texCombined = mix(texCombined, texcolor, texcolor.a);
  788. }
  789. }
  790. #endif
  791. // Combine material color with texture
  792. FragColor = min(vec4(Color, MatOpacity) * texCombined, vec4(1));
  793. }
  794. `
  795. const sprite_vertex_source = `//
  796. // Vertex shader for sprites
  797. //
  798. #include <attributes>
  799. // Input uniforms
  800. uniform mat4 MVP;
  801. #include <material>
  802. // Outputs for fragment shader
  803. out vec3 Color;
  804. out vec2 FragTexcoord;
  805. void main() {
  806. // Applies transformation to vertex position
  807. gl_Position = MVP * vec4(VertexPosition, 1.0);
  808. // Outputs color
  809. Color = MatDiffuseColor;
  810. // Flips texture coordinate Y if requested.
  811. vec2 texcoord = VertexTexcoord;
  812. #if MAT_TEXTURES>0
  813. if (MatTexFlipY[0]) {
  814. texcoord.y = 1 - texcoord.y;
  815. }
  816. #endif
  817. FragTexcoord = texcoord;
  818. }
  819. `
  820. const standard_fragment_source = `//
  821. // Fragment Shader template
  822. //
  823. #include <material>
  824. // Inputs from Vertex shader
  825. in vec3 ColorFrontAmbdiff;
  826. in vec3 ColorFrontSpec;
  827. in vec3 ColorBackAmbdiff;
  828. in vec3 ColorBackSpec;
  829. in vec2 FragTexcoord;
  830. // Output
  831. out vec4 FragColor;
  832. void main() {
  833. // Mix material color with textures colors
  834. vec4 texMixed = vec4(1);
  835. vec4 texColor;
  836. #if MAT_TEXTURES==1
  837. MIX_TEXTURE(0)
  838. #elif MAT_TEXTURES==2
  839. MIX_TEXTURE(0)
  840. MIX_TEXTURE(1)
  841. #elif MAT_TEXTURES==3
  842. MIX_TEXTURE(0)
  843. MIX_TEXTURE(1)
  844. MIX_TEXTURE(2)
  845. #endif
  846. vec4 colorAmbDiff;
  847. vec4 colorSpec;
  848. if (gl_FrontFacing) {
  849. colorAmbDiff = vec4(ColorFrontAmbdiff, MatOpacity);
  850. colorSpec = vec4(ColorFrontSpec, 0);
  851. } else {
  852. colorAmbDiff = vec4(ColorBackAmbdiff, MatOpacity);
  853. colorSpec = vec4(ColorBackSpec, 0);
  854. }
  855. FragColor = min(colorAmbDiff * texMixed + colorSpec, vec4(1));
  856. }
  857. `
  858. const standard_vertex_source = `//
  859. // Vertex shader standard
  860. //
  861. #include <attributes>
  862. // Model uniforms
  863. uniform mat4 ModelViewMatrix;
  864. uniform mat3 NormalMatrix;
  865. uniform mat4 MVP;
  866. #include <lights>
  867. #include <material>
  868. #include <phong_model>
  869. // Outputs for the fragment shader.
  870. out vec3 ColorFrontAmbdiff;
  871. out vec3 ColorFrontSpec;
  872. out vec3 ColorBackAmbdiff;
  873. out vec3 ColorBackSpec;
  874. out vec2 FragTexcoord;
  875. void main() {
  876. // Transform this vertex normal to camera coordinates.
  877. vec3 normal = normalize(NormalMatrix * VertexNormal);
  878. // Calculate this vertex position in camera coordinates
  879. vec4 position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  880. // Calculate the direction vector from the vertex to the camera
  881. // The camera is at 0,0,0
  882. vec3 camDir = normalize(-position.xyz);
  883. // Calculates the vertex Ambient+Diffuse and Specular colors using the Phong model
  884. // for the front and back
  885. phongModel(position, normal, camDir, MatAmbientColor, MatDiffuseColor, ColorFrontAmbdiff, ColorFrontSpec);
  886. phongModel(position, -normal, camDir, MatAmbientColor, MatDiffuseColor, ColorBackAmbdiff, ColorBackSpec);
  887. vec2 texcoord = VertexTexcoord;
  888. #if MAT_TEXTURES > 0
  889. // Flips texture coordinate Y if requested.
  890. if (MatTexFlipY(0)) {
  891. texcoord.y = 1 - texcoord.y;
  892. }
  893. #endif
  894. FragTexcoord = texcoord;
  895. gl_Position = MVP * vec4(VertexPosition, 1.0);
  896. }
  897. `
  898. // Maps include name with its source code
  899. var includeMap = map[string]string{
  900. "attributes": include_attributes_source,
  901. "lights": include_lights_source,
  902. "material": include_material_source,
  903. "phong_model": include_phong_model_source,
  904. }
  905. // Maps shader name with its source code
  906. var shaderMap = map[string]string{
  907. "basic_fragment": basic_fragment_source,
  908. "basic_vertex": basic_vertex_source,
  909. "panel_fragment": panel_fragment_source,
  910. "panel_vertex": panel_vertex_source,
  911. "phong_fragment": phong_fragment_source,
  912. "phong_vertex": phong_vertex_source,
  913. "physical_fragment": physical_fragment_source,
  914. "physical_vertex": physical_vertex_source,
  915. "point_fragment": point_fragment_source,
  916. "point_vertex": point_vertex_source,
  917. "sprite_fragment": sprite_fragment_source,
  918. "sprite_vertex": sprite_vertex_source,
  919. "standard_fragment": standard_fragment_source,
  920. "standard_vertex": standard_vertex_source,
  921. }
  922. // Maps program name with Proginfo struct with shaders names
  923. var programMap = map[string]ProgramInfo{
  924. "basic": {"basic_vertex", "basic_fragment", ""},
  925. "panel": {"panel_vertex", "panel_fragment", ""},
  926. "phong": {"phong_vertex", "phong_fragment", ""},
  927. "physical": {"physical_vertex", "physical_fragment", ""},
  928. "point": {"point_vertex", "point_fragment", ""},
  929. "sprite": {"sprite_vertex", "sprite_fragment", ""},
  930. "standard": {"standard_vertex", "standard_fragment", ""},
  931. }