sources.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. // File generated by G3NSHADERS. Do not edit.
  2. // To regenerate this file install 'g3nshaders' and execute:
  3. // 'go generate' in this folder.
  4. package shaders
  5. const include_attributes_source = `//
  6. // Vertex attributes
  7. //
  8. layout(location = 0) in vec3 VertexPosition;
  9. layout(location = 1) in vec3 VertexNormal;
  10. layout(location = 2) in vec3 VertexColor;
  11. layout(location = 3) in vec2 VertexTexcoord;
  12. layout(location = 4) in float VertexDistance;
  13. layout(location = 5) in vec4 VertexTexoffsets;
  14. `
  15. const include_lights_source = `//
  16. // Lights uniforms
  17. //
  18. // Ambient lights uniforms
  19. #if AMB_LIGHTS>0
  20. uniform vec3 AmbientLightColor[AMB_LIGHTS];
  21. #endif
  22. // Directional lights uniform array. Each directional light uses 2 elements
  23. #if DIR_LIGHTS>0
  24. uniform vec3 DirLight[2*DIR_LIGHTS];
  25. // Macros to access elements inside the DirectionalLight uniform array
  26. #define DirLightColor(a) DirLight[2*a]
  27. #define DirLightPosition(a) DirLight[2*a+1]
  28. #endif
  29. // Point lights uniform array. Each point light uses 3 elements
  30. #if POINT_LIGHTS>0
  31. uniform vec3 PointLight[3*POINT_LIGHTS];
  32. // Macros to access elements inside the PointLight uniform array
  33. #define PointLightColor(a) PointLight[3*a]
  34. #define PointLightPosition(a) PointLight[3*a+1]
  35. #define PointLightLinearDecay(a) PointLight[3*a+2].x
  36. #define PointLightQuadraticDecay(a) PointLight[3*a+2].y
  37. #endif
  38. #if SPOT_LIGHTS>0
  39. // Spot lights uniforms. Each spot light uses 5 elements
  40. uniform vec3 SpotLight[5*SPOT_LIGHTS];
  41. // Macros to access elements inside the PointLight uniform array
  42. #define SpotLightColor(a) SpotLight[5*a]
  43. #define SpotLightPosition(a) SpotLight[5*a+1]
  44. #define SpotLightDirection(a) SpotLight[5*a+2]
  45. #define SpotLightAngularDecay(a) SpotLight[5*a+3].x
  46. #define SpotLightCutoffAngle(a) SpotLight[5*a+3].y
  47. #define SpotLightLinearDecay(a) SpotLight[5*a+3].z
  48. #define SpotLightQuadraticDecay(a) SpotLight[5*a+4].x
  49. #endif
  50. `
  51. const include_material_source = `//
  52. // Material properties uniform
  53. //
  54. // Material parameters uniform array
  55. uniform vec3 Material[6];
  56. // Macros to access elements inside the Material array
  57. #define MatAmbientColor Material[0]
  58. #define MatDiffuseColor Material[1]
  59. #define MatSpecularColor Material[2]
  60. #define MatEmissiveColor Material[3]
  61. #define MatShininess Material[4].x
  62. #define MatOpacity Material[4].y
  63. #define MatPointSize Material[4].z
  64. #define MatPointRotationZ Material[5].x
  65. #if MAT_TEXTURES > 0
  66. // Texture unit sampler array
  67. uniform sampler2D MatTexture[MAT_TEXTURES];
  68. // Texture parameters (3*vec2 per texture)
  69. uniform vec2 MatTexinfo[3*MAT_TEXTURES];
  70. // Macros to access elements inside the MatTexinfo array
  71. #define MatTexOffset(a) MatTexinfo[(3*a)]
  72. #define MatTexRepeat(a) MatTexinfo[(3*a)+1]
  73. #define MatTexFlipY(a) bool(MatTexinfo[(3*a)+2].x)
  74. #define MatTexVisible(a) bool(MatTexinfo[(3*a)+2].y)
  75. #endif
  76. // GLSL 3.30 does not allow indexing texture sampler with non constant values.
  77. // This macro is used to mix the texture with the specified index with the material color.
  78. // It should be called for each texture index. It uses two externally defined variables:
  79. // vec4 texColor
  80. // vec4 texMixed
  81. #define MIX_TEXTURE(i) \
  82. if (MatTexVisible(i)) { \
  83. texColor = texture(MatTexture[i], FragTexcoord * MatTexRepeat(i) + MatTexOffset(i)); \
  84. if (i == 0) { \
  85. texMixed = texColor; \
  86. } else { \
  87. texMixed = mix(texMixed, texColor, texColor.a); \
  88. } \
  89. }
  90. `
  91. const include_phong_model_source = `/***
  92. phong lighting model
  93. Parameters:
  94. position: input vertex position in camera coordinates
  95. normal: input vertex normal in camera coordinates
  96. camDir: input camera directions
  97. matAmbient: input material ambient color
  98. matDiffuse: input material diffuse color
  99. ambdiff: output ambient+diffuse color
  100. spec: output specular color
  101. Uniforms:
  102. AmbientLightColor[]
  103. DiffuseLightColor[]
  104. DiffuseLightPosition[]
  105. PointLightColor[]
  106. PointLightPosition[]
  107. PointLightLinearDecay[]
  108. PointLightQuadraticDecay[]
  109. MatSpecularColor
  110. MatShininess
  111. *****/
  112. void phongModel(vec4 position, vec3 normal, vec3 camDir, vec3 matAmbient, vec3 matDiffuse, out vec3 ambdiff, out vec3 spec) {
  113. vec3 ambientTotal = vec3(0.0);
  114. vec3 diffuseTotal = vec3(0.0);
  115. vec3 specularTotal = vec3(0.0);
  116. #if AMB_LIGHTS>0
  117. // Ambient lights
  118. for (int i = 0; i < AMB_LIGHTS; i++) {
  119. ambientTotal += AmbientLightColor[i] * matAmbient;
  120. }
  121. #endif
  122. #if DIR_LIGHTS>0
  123. // Directional lights
  124. for (int i = 0; i < DIR_LIGHTS; i++) {
  125. // Diffuse reflection
  126. // DirLightPosition is the direction of the current light
  127. vec3 lightDirection = normalize(DirLightPosition(i));
  128. // Calculates the dot product between the light direction and this vertex normal.
  129. float dotNormal = max(dot(lightDirection, normal), 0.0);
  130. diffuseTotal += DirLightColor(i) * matDiffuse * dotNormal;
  131. // Specular reflection
  132. // Calculates the light reflection vector
  133. vec3 ref = reflect(-lightDirection, normal);
  134. if (dotNormal > 0.0) {
  135. specularTotal += DirLightColor(i) * MatSpecularColor * pow(max(dot(ref, camDir), 0.0), MatShininess);
  136. }
  137. }
  138. #endif
  139. #if POINT_LIGHTS>0
  140. // Point lights
  141. for (int i = 0; i < POINT_LIGHTS; i++) {
  142. // Common calculations
  143. // Calculates the direction and distance from the current vertex to this point light.
  144. vec3 lightDirection = PointLightPosition(i) - vec3(position);
  145. float lightDistance = length(lightDirection);
  146. // Normalizes the lightDirection
  147. lightDirection = lightDirection / lightDistance;
  148. // Calculates the attenuation due to the distance of the light
  149. float attenuation = 1.0 / (1.0 + PointLightLinearDecay(i) * lightDistance +
  150. PointLightQuadraticDecay(i) * lightDistance * lightDistance);
  151. // Diffuse reflection
  152. float dotNormal = max(dot(lightDirection, normal), 0.0);
  153. diffuseTotal += PointLightColor(i) * matDiffuse * dotNormal * attenuation;
  154. // Specular reflection
  155. // Calculates the light reflection vector
  156. vec3 ref = reflect(-lightDirection, normal);
  157. if (dotNormal > 0.0) {
  158. specularTotal += PointLightColor(i) * MatSpecularColor *
  159. pow(max(dot(ref, camDir), 0.0), MatShininess) * attenuation;
  160. }
  161. }
  162. #endif
  163. #if SPOT_LIGHTS>0
  164. for (int i = 0; i < SPOT_LIGHTS; i++) {
  165. // Calculates the direction and distance from the current vertex to this spot light.
  166. vec3 lightDirection = SpotLightPosition(i) - vec3(position);
  167. float lightDistance = length(lightDirection);
  168. lightDirection = lightDirection / lightDistance;
  169. // Calculates the attenuation due to the distance of the light
  170. float attenuation = 1.0 / (1.0 + SpotLightLinearDecay(i) * lightDistance +
  171. SpotLightQuadraticDecay(i) * lightDistance * lightDistance);
  172. // Calculates the angle between the vertex direction and spot direction
  173. // If this angle is greater than the cutoff the spotlight will not contribute
  174. // to the final color.
  175. float angle = acos(dot(-lightDirection, SpotLightDirection(i)));
  176. float cutoff = radians(clamp(SpotLightCutoffAngle(i), 0.0, 90.0));
  177. if (angle < cutoff) {
  178. float spotFactor = pow(dot(-lightDirection, SpotLightDirection(i)), SpotLightAngularDecay(i));
  179. // Diffuse reflection
  180. float dotNormal = max(dot(lightDirection, normal), 0.0);
  181. diffuseTotal += SpotLightColor(i) * matDiffuse * dotNormal * attenuation * spotFactor;
  182. // Specular reflection
  183. vec3 ref = reflect(-lightDirection, normal);
  184. if (dotNormal > 0.0) {
  185. specularTotal += SpotLightColor(i) * MatSpecularColor * pow(max(dot(ref, camDir), 0.0), MatShininess) * attenuation * spotFactor;
  186. }
  187. }
  188. }
  189. #endif
  190. // Sets output colors
  191. ambdiff = ambientTotal + MatEmissiveColor + diffuseTotal;
  192. spec = specularTotal;
  193. }
  194. `
  195. const basic_fragment_source = `//
  196. // Fragment Shader template
  197. //
  198. in vec3 Color;
  199. out vec4 FragColor;
  200. void main() {
  201. FragColor = vec4(Color, 1.0);
  202. }
  203. `
  204. const basic_vertex_source = `//
  205. // Vertex shader basic
  206. //
  207. #include <attributes>
  208. // Model uniforms
  209. uniform mat4 MVP;
  210. // Final output color for fragment shader
  211. out vec3 Color;
  212. void main() {
  213. Color = VertexColor;
  214. gl_Position = MVP * vec4(VertexPosition, 1.0);
  215. }
  216. `
  217. const panel_fragment_source = `//
  218. // Fragment Shader template
  219. //
  220. // Texture uniforms
  221. uniform sampler2D MatTexture;
  222. uniform vec2 MatTexinfo[3];
  223. // Macros to access elements inside the MatTexinfo array
  224. #define MatTexOffset MatTexinfo[0]
  225. #define MatTexRepeat MatTexinfo[1]
  226. #define MatTexFlipY bool(MatTexinfo[2].x) // not used
  227. #define MatTexVisible bool(MatTexinfo[2].y) // not used
  228. // Inputs from vertex shader
  229. in vec2 FragTexcoord;
  230. // Input uniform
  231. uniform vec4 Panel[8];
  232. #define Bounds Panel[0] // panel bounds in texture coordinates
  233. #define Border Panel[1] // panel border in texture coordinates
  234. #define Padding Panel[2] // panel padding in texture coordinates
  235. #define Content Panel[3] // panel content area in texture coordinates
  236. #define BorderColor Panel[4] // panel border color
  237. #define PaddingColor Panel[5] // panel padding color
  238. #define ContentColor Panel[6] // panel content color
  239. #define TextureValid bool(Panel[7].x) // texture valid flag
  240. // Output
  241. out vec4 FragColor;
  242. /***
  243. * Checks if current fragment texture coordinate is inside the
  244. * supplied rectangle in texture coordinates:
  245. * rect[0] - position x [0,1]
  246. * rect[1] - position y [0,1]
  247. * rect[2] - width [0,1]
  248. * rect[3] - height [0,1]
  249. */
  250. bool checkRect(vec4 rect) {
  251. if (FragTexcoord.x < rect[0]) {
  252. return false;
  253. }
  254. if (FragTexcoord.x > rect[0] + rect[2]) {
  255. return false;
  256. }
  257. if (FragTexcoord.y < rect[1]) {
  258. return false;
  259. }
  260. if (FragTexcoord.y > rect[1] + rect[3]) {
  261. return false;
  262. }
  263. return true;
  264. }
  265. void main() {
  266. // Discard fragment outside of received bounds
  267. // Bounds[0] - xmin
  268. // Bounds[1] - ymin
  269. // Bounds[2] - xmax
  270. // Bounds[3] - ymax
  271. if (FragTexcoord.x <= Bounds[0] || FragTexcoord.x >= Bounds[2]) {
  272. discard;
  273. }
  274. if (FragTexcoord.y <= Bounds[1] || FragTexcoord.y >= Bounds[3]) {
  275. discard;
  276. }
  277. // Check if fragment is inside content area
  278. if (checkRect(Content)) {
  279. // If no texture, the color will be the material color.
  280. vec4 color = ContentColor;
  281. if (TextureValid) {
  282. // Adjust texture coordinates to fit texture inside the content area
  283. vec2 offset = vec2(-Content[0], -Content[1]);
  284. vec2 factor = vec2(1/Content[2], 1/Content[3]);
  285. vec2 texcoord = (FragTexcoord + offset) * factor;
  286. vec4 texColor = texture(MatTexture, texcoord * MatTexRepeat + MatTexOffset);
  287. // Mix content color with texture color.
  288. // Note that doing a simple linear interpolation (e.g. using mix()) is not correct!
  289. // The right formula can be found here: https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
  290. // For a more in-depth discussion: http://apoorvaj.io/alpha-compositing-opengl-blending-and-premultiplied-alpha.html#toc4
  291. // Pre-multiply the content color
  292. vec4 contentPre = ContentColor;
  293. contentPre.rgb *= contentPre.a;
  294. // Pre-multiply the texture color
  295. vec4 texPre = texColor;
  296. texPre.rgb *= texPre.a;
  297. // Combine colors the premultiplied final color
  298. color = texPre + contentPre * (1 - texPre.a);
  299. // Un-pre-multiply (pre-divide? :P)
  300. color.rgb /= color.a;
  301. }
  302. FragColor = color;
  303. return;
  304. }
  305. // Checks if fragment is inside paddings area
  306. if (checkRect(Padding)) {
  307. FragColor = PaddingColor;
  308. return;
  309. }
  310. // Checks if fragment is inside borders area
  311. if (checkRect(Border)) {
  312. FragColor = BorderColor;
  313. return;
  314. }
  315. // Fragment is in margins area (always transparent)
  316. FragColor = vec4(1,1,1,0);
  317. }
  318. `
  319. const panel_vertex_source = `//
  320. // Vertex shader panel
  321. //
  322. #include <attributes>
  323. // Model uniforms
  324. uniform mat4 ModelMatrix;
  325. // Outputs for fragment shader
  326. out vec2 FragTexcoord;
  327. void main() {
  328. // Always flip texture coordinates
  329. vec2 texcoord = VertexTexcoord;
  330. texcoord.y = 1 - texcoord.y;
  331. FragTexcoord = texcoord;
  332. // Set position
  333. vec4 pos = vec4(VertexPosition.xyz, 1);
  334. gl_Position = ModelMatrix * pos;
  335. }
  336. `
  337. const phong_fragment_source = `//
  338. // Fragment Shader template
  339. //
  340. // Inputs from vertex shader
  341. in vec4 Position; // Vertex position in camera coordinates.
  342. in vec3 Normal; // Vertex normal in camera coordinates.
  343. in vec3 CamDir; // Direction from vertex to camera
  344. in vec2 FragTexcoord;
  345. #include <lights>
  346. #include <material>
  347. #include <phong_model>
  348. // Final fragment color
  349. out vec4 FragColor;
  350. void main() {
  351. // Mix material color with textures colors
  352. vec4 texMixed = vec4(1);
  353. vec4 texColor;
  354. #if MAT_TEXTURES==1
  355. MIX_TEXTURE(0)
  356. #elif MAT_TEXTURES==2
  357. MIX_TEXTURE(0)
  358. MIX_TEXTURE(1)
  359. #elif MAT_TEXTURES==3
  360. MIX_TEXTURE(0)
  361. MIX_TEXTURE(1)
  362. MIX_TEXTURE(2)
  363. #endif
  364. // Combine material with texture colors
  365. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texMixed;
  366. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texMixed;
  367. // Inverts the fragment normal if not FrontFacing
  368. vec3 fragNormal = Normal;
  369. if (!gl_FrontFacing) {
  370. fragNormal = -fragNormal;
  371. }
  372. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  373. vec3 Ambdiff, Spec;
  374. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  375. // Final fragment color
  376. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  377. }
  378. `
  379. const phong_vertex_source = `//
  380. // Vertex Shader
  381. //
  382. #include <attributes>
  383. // Model uniforms
  384. uniform mat4 ModelViewMatrix;
  385. uniform mat3 NormalMatrix;
  386. uniform mat4 MVP;
  387. #include <material>
  388. // Output variables for Fragment shader
  389. out vec4 Position;
  390. out vec3 Normal;
  391. out vec3 CamDir;
  392. out vec2 FragTexcoord;
  393. void main() {
  394. // Transform this vertex position to camera coordinates.
  395. Position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  396. // Transform this vertex normal to camera coordinates.
  397. Normal = normalize(NormalMatrix * VertexNormal);
  398. // Calculate the direction vector from the vertex to the camera
  399. // The camera is at 0,0,0
  400. CamDir = normalize(-Position.xyz);
  401. // Flips texture coordinate Y if requested.
  402. vec2 texcoord = VertexTexcoord;
  403. #if MAT_TEXTURES>0
  404. if (MatTexFlipY(0)) {
  405. texcoord.y = 1 - texcoord.y;
  406. }
  407. #endif
  408. FragTexcoord = texcoord;
  409. gl_Position = MVP * vec4(VertexPosition, 1.0);
  410. }
  411. `
  412. const point_fragment_source = `#include <material>
  413. // GLSL 3.30 does not allow indexing texture sampler with non constant values.
  414. // This macro is used to mix the texture with the specified index with the material color.
  415. // It should be called for each texture index.
  416. #define MIX_POINT_TEXTURE(i) \
  417. if (MatTexVisible(i)) { \
  418. vec2 pt = gl_PointCoord - vec2(0.5); \
  419. vec4 texColor = texture(MatTexture[i], (Rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i)); \
  420. if (i == 0) { \
  421. texMixed = texColor; \
  422. } else { \
  423. texMixed = mix(texMixed, texColor, texColor.a); \
  424. } \
  425. }
  426. // Inputs from vertex shader
  427. in vec3 Color;
  428. flat in mat2 Rotation;
  429. // Output
  430. out vec4 FragColor;
  431. void main() {
  432. // Mix material color with textures colors
  433. vec4 texMixed = vec4(1);
  434. #if MAT_TEXTURES==1
  435. MIX_POINT_TEXTURE(0)
  436. #elif MAT_TEXTURES==2
  437. MIX_POINT_TEXTURE(0)
  438. MIX_POINT_TEXTURE(1)
  439. #elif MAT_TEXTURES==3
  440. MIX_POINT_TEXTURE(0)
  441. MIX_POINT_TEXTURE(1)
  442. MIX_POINT_TEXTURE(2)
  443. #endif
  444. // Generates final color
  445. FragColor = min(vec4(Color, MatOpacity) * texMixed, vec4(1));
  446. }
  447. `
  448. const point_vertex_source = `#include <attributes>
  449. // Model uniforms
  450. uniform mat4 MVP;
  451. // Material uniforms
  452. #include <material>
  453. // Outputs for fragment shader
  454. out vec3 Color;
  455. flat out mat2 Rotation;
  456. void main() {
  457. // Rotation matrix for fragment shader
  458. float rotSin = sin(MatPointRotationZ);
  459. float rotCos = cos(MatPointRotationZ);
  460. Rotation = mat2(rotCos, rotSin, - rotSin, rotCos);
  461. // Sets the vertex position
  462. vec4 pos = MVP * vec4(VertexPosition, 1.0);
  463. gl_Position = pos;
  464. // Sets the size of the rasterized point decreasing with distance
  465. gl_PointSize = (1.0 - pos.z / pos.w) * MatPointSize;
  466. // Outputs color
  467. Color = MatEmissiveColor;
  468. }
  469. `
  470. const sprite_fragment_source = `//
  471. // Fragment shader for sprite
  472. //
  473. #include <material>
  474. // Inputs from vertex shader
  475. in vec3 Color;
  476. in vec2 FragTexcoord;
  477. // Output
  478. out vec4 FragColor;
  479. void main() {
  480. // Combine all texture colors and opacity
  481. vec4 texCombined = vec4(1);
  482. #if MAT_TEXTURES>0
  483. for (int i = 0; i < {{.MatTexturesMax}}; i++) {
  484. vec4 texcolor = texture(MatTexture[i], FragTexcoord * MatTexRepeat(i) + MatTexOffset(i));
  485. if (i == 0) {
  486. texCombined = texcolor;
  487. } else {
  488. texCombined = mix(texCombined, texcolor, texcolor.a);
  489. }
  490. }
  491. #endif
  492. // Combine material color with texture
  493. FragColor = min(vec4(Color, MatOpacity) * texCombined, vec4(1));
  494. }
  495. `
  496. const sprite_vertex_source = `//
  497. // Vertex shader for sprites
  498. //
  499. #include <attributes>
  500. // Input uniforms
  501. uniform mat4 MVP;
  502. #include <material>
  503. // Outputs for fragment shader
  504. out vec3 Color;
  505. out vec2 FragTexcoord;
  506. void main() {
  507. // Applies transformation to vertex position
  508. gl_Position = MVP * vec4(VertexPosition, 1.0);
  509. // Outputs color
  510. Color = MatDiffuseColor;
  511. // Flips texture coordinate Y if requested.
  512. vec2 texcoord = VertexTexcoord;
  513. #if MAT_TEXTURES>0
  514. if (MatTexFlipY[0]) {
  515. texcoord.y = 1 - texcoord.y;
  516. }
  517. #endif
  518. FragTexcoord = texcoord;
  519. }
  520. `
  521. const standard_fragment_source = `//
  522. // Fragment Shader template
  523. //
  524. #include <material>
  525. // Inputs from Vertex shader
  526. in vec3 ColorFrontAmbdiff;
  527. in vec3 ColorFrontSpec;
  528. in vec3 ColorBackAmbdiff;
  529. in vec3 ColorBackSpec;
  530. in vec2 FragTexcoord;
  531. // Output
  532. out vec4 FragColor;
  533. void main() {
  534. // Mix material color with textures colors
  535. vec4 texMixed = vec4(1);
  536. vec4 texColor;
  537. #if MAT_TEXTURES==1
  538. MIX_TEXTURE(0)
  539. #elif MAT_TEXTURES==2
  540. MIX_TEXTURE(0)
  541. MIX_TEXTURE(1)
  542. #elif MAT_TEXTURES==3
  543. MIX_TEXTURE(0)
  544. MIX_TEXTURE(1)
  545. MIX_TEXTURE(2)
  546. #endif
  547. vec4 colorAmbDiff;
  548. vec4 colorSpec;
  549. if (gl_FrontFacing) {
  550. colorAmbDiff = vec4(ColorFrontAmbdiff, MatOpacity);
  551. colorSpec = vec4(ColorFrontSpec, 0);
  552. } else {
  553. colorAmbDiff = vec4(ColorBackAmbdiff, MatOpacity);
  554. colorSpec = vec4(ColorBackSpec, 0);
  555. }
  556. FragColor = min(colorAmbDiff * texMixed + colorSpec, vec4(1));
  557. }
  558. `
  559. const standard_vertex_source = `//
  560. // Vertex shader standard
  561. //
  562. #include <attributes>
  563. // Model uniforms
  564. uniform mat4 ModelViewMatrix;
  565. uniform mat3 NormalMatrix;
  566. uniform mat4 MVP;
  567. #include <lights>
  568. #include <material>
  569. #include <phong_model>
  570. // Outputs for the fragment shader.
  571. out vec3 ColorFrontAmbdiff;
  572. out vec3 ColorFrontSpec;
  573. out vec3 ColorBackAmbdiff;
  574. out vec3 ColorBackSpec;
  575. out vec2 FragTexcoord;
  576. void main() {
  577. // Transform this vertex normal to camera coordinates.
  578. vec3 normal = normalize(NormalMatrix * VertexNormal);
  579. // Calculate this vertex position in camera coordinates
  580. vec4 position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  581. // Calculate the direction vector from the vertex to the camera
  582. // The camera is at 0,0,0
  583. vec3 camDir = normalize(-position.xyz);
  584. // Calculates the vertex Ambient+Diffuse and Specular colors using the Phong model
  585. // for the front and back
  586. phongModel(position, normal, camDir, MatAmbientColor, MatDiffuseColor, ColorFrontAmbdiff, ColorFrontSpec);
  587. phongModel(position, -normal, camDir, MatAmbientColor, MatDiffuseColor, ColorBackAmbdiff, ColorBackSpec);
  588. vec2 texcoord = VertexTexcoord;
  589. #if MAT_TEXTURES > 0
  590. // Flips texture coordinate Y if requested.
  591. if (MatTexFlipY(0)) {
  592. texcoord.y = 1 - texcoord.y;
  593. }
  594. #endif
  595. FragTexcoord = texcoord;
  596. gl_Position = MVP * vec4(VertexPosition, 1.0);
  597. }
  598. `
  599. // Maps include name with its source code
  600. var includeMap = map[string]string{
  601. "attributes": include_attributes_source,
  602. "lights": include_lights_source,
  603. "material": include_material_source,
  604. "phong_model": include_phong_model_source,
  605. }
  606. // Maps shader name with its source code
  607. var shaderMap = map[string]string{
  608. "basic_fragment": basic_fragment_source,
  609. "basic_vertex": basic_vertex_source,
  610. "panel_fragment": panel_fragment_source,
  611. "panel_vertex": panel_vertex_source,
  612. "phong_fragment": phong_fragment_source,
  613. "phong_vertex": phong_vertex_source,
  614. "point_fragment": point_fragment_source,
  615. "point_vertex": point_vertex_source,
  616. "sprite_fragment": sprite_fragment_source,
  617. "sprite_vertex": sprite_vertex_source,
  618. "standard_fragment": standard_fragment_source,
  619. "standard_vertex": standard_vertex_source,
  620. }
  621. // Maps program name with Proginfo struct with shaders names
  622. var programMap = map[string]ProgramInfo{
  623. "basic": {"basic_vertex", "basic_fragment", ""},
  624. "panel": {"panel_vertex", "panel_fragment", ""},
  625. "phong": {"phong_vertex", "phong_fragment", ""},
  626. "point": {"point_vertex", "point_fragment", ""},
  627. "sprite": {"sprite_vertex", "sprite_fragment", ""},
  628. "standard": {"standard_vertex", "standard_fragment", ""},
  629. }