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