sources.go 43 KB

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