sources.go 41 KB

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