sources.go 41 KB

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