sources.go 22 KB

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