sources.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. //color = mix(color, texColor, texColor.a);
  289. color = texColor;
  290. }
  291. FragColor = color;
  292. return;
  293. }
  294. // Checks if fragment is inside paddings area
  295. if (checkRect(Padding)) {
  296. FragColor = PaddingColor;
  297. return;
  298. }
  299. // Checks if fragment is inside borders area
  300. if (checkRect(Border)) {
  301. FragColor = BorderColor;
  302. return;
  303. }
  304. // Fragment is in margins area (always transparent)
  305. FragColor = vec4(1,1,1,0);
  306. }
  307. `
  308. const panel_vertex_source = `//
  309. // Vertex shader panel
  310. //
  311. #include <attributes>
  312. // Model uniforms
  313. uniform mat4 ModelMatrix;
  314. // Outputs for fragment shader
  315. out vec2 FragTexcoord;
  316. void main() {
  317. // Always flip texture coordinates
  318. vec2 texcoord = VertexTexcoord;
  319. texcoord.y = 1 - texcoord.y;
  320. FragTexcoord = texcoord;
  321. // Set position
  322. vec4 pos = vec4(VertexPosition.xyz, 1);
  323. gl_Position = ModelMatrix * pos;
  324. }
  325. `
  326. const phong_fragment_source = `//
  327. // Fragment Shader template
  328. //
  329. // Inputs from vertex shader
  330. in vec4 Position; // Vertex position in camera coordinates.
  331. in vec3 Normal; // Vertex normal in camera coordinates.
  332. in vec3 CamDir; // Direction from vertex to camera
  333. in vec2 FragTexcoord;
  334. #include <lights>
  335. #include <material>
  336. #include <phong_model>
  337. // Final fragment color
  338. out vec4 FragColor;
  339. void main() {
  340. // Mix material color with textures colors
  341. vec4 texMixed = vec4(1);
  342. vec4 texColor;
  343. #if MAT_TEXTURES==1
  344. MIX_TEXTURE(0)
  345. #elif MAT_TEXTURES==2
  346. MIX_TEXTURE(0)
  347. MIX_TEXTURE(1)
  348. #elif MAT_TEXTURES==3
  349. MIX_TEXTURE(0)
  350. MIX_TEXTURE(1)
  351. MIX_TEXTURE(2)
  352. #endif
  353. // Combine material with texture colors
  354. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texMixed;
  355. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texMixed;
  356. // Inverts the fragment normal if not FrontFacing
  357. vec3 fragNormal = Normal;
  358. if (!gl_FrontFacing) {
  359. fragNormal = -fragNormal;
  360. }
  361. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  362. vec3 Ambdiff, Spec;
  363. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  364. // Final fragment color
  365. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  366. }
  367. `
  368. const phong_vertex_source = `//
  369. // Vertex Shader
  370. //
  371. #include <attributes>
  372. // Model uniforms
  373. uniform mat4 ModelViewMatrix;
  374. uniform mat3 NormalMatrix;
  375. uniform mat4 MVP;
  376. #include <material>
  377. // Output variables for Fragment shader
  378. out vec4 Position;
  379. out vec3 Normal;
  380. out vec3 CamDir;
  381. out vec2 FragTexcoord;
  382. void main() {
  383. // Transform this vertex position to camera coordinates.
  384. Position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  385. // Transform this vertex normal to camera coordinates.
  386. Normal = normalize(NormalMatrix * VertexNormal);
  387. // Calculate the direction vector from the vertex to the camera
  388. // The camera is at 0,0,0
  389. CamDir = normalize(-Position.xyz);
  390. // Flips texture coordinate Y if requested.
  391. vec2 texcoord = VertexTexcoord;
  392. #if MAT_TEXTURES>0
  393. if (MatTexFlipY(0)) {
  394. texcoord.y = 1 - texcoord.y;
  395. }
  396. #endif
  397. FragTexcoord = texcoord;
  398. gl_Position = MVP * vec4(VertexPosition, 1.0);
  399. }
  400. `
  401. const point_fragment_source = `#include <material>
  402. // GLSL 3.30 does not allow indexing texture sampler with non constant values.
  403. // This macro is used to mix the texture with the specified index with the material color.
  404. // It should be called for each texture index.
  405. #define MIX_POINT_TEXTURE(i) \
  406. if (MatTexVisible(i)) { \
  407. vec2 pt = gl_PointCoord - vec2(0.5); \
  408. vec4 texColor = texture(MatTexture[i], (Rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i)); \
  409. if (i == 0) { \
  410. texMixed = texColor; \
  411. } else { \
  412. texMixed = mix(texMixed, texColor, texColor.a); \
  413. } \
  414. }
  415. // Inputs from vertex shader
  416. in vec3 Color;
  417. flat in mat2 Rotation;
  418. // Output
  419. out vec4 FragColor;
  420. void main() {
  421. // Mix material color with textures colors
  422. vec4 texMixed = vec4(1);
  423. #if MAT_TEXTURES==1
  424. MIX_POINT_TEXTURE(0)
  425. #elif MAT_TEXTURES==2
  426. MIX_POINT_TEXTURE(0)
  427. MIX_POINT_TEXTURE(1)
  428. #elif MAT_TEXTURES==3
  429. MIX_POINT_TEXTURE(0)
  430. MIX_POINT_TEXTURE(1)
  431. MIX_POINT_TEXTURE(2)
  432. #endif
  433. // Generates final color
  434. FragColor = min(vec4(Color, MatOpacity) * texMixed, vec4(1));
  435. }
  436. `
  437. const point_vertex_source = `#include <attributes>
  438. // Model uniforms
  439. uniform mat4 MVP;
  440. // Material uniforms
  441. #include <material>
  442. // Outputs for fragment shader
  443. out vec3 Color;
  444. flat out mat2 Rotation;
  445. void main() {
  446. // Rotation matrix for fragment shader
  447. float rotSin = sin(MatPointRotationZ);
  448. float rotCos = cos(MatPointRotationZ);
  449. Rotation = mat2(rotCos, rotSin, - rotSin, rotCos);
  450. // Sets the vertex position
  451. vec4 pos = MVP * vec4(VertexPosition, 1.0);
  452. gl_Position = pos;
  453. // Sets the size of the rasterized point decreasing with distance
  454. gl_PointSize = (1.0 - pos.z / pos.w) * MatPointSize;
  455. // Outputs color
  456. Color = MatEmissiveColor;
  457. }
  458. `
  459. const sprite_fragment_source = `//
  460. // Fragment shader for sprite
  461. //
  462. #include <material>
  463. // Inputs from vertex shader
  464. in vec3 Color;
  465. in vec2 FragTexcoord;
  466. // Output
  467. out vec4 FragColor;
  468. void main() {
  469. // Combine all texture colors and opacity
  470. vec4 texCombined = vec4(1);
  471. #if MAT_TEXTURES>0
  472. for (int i = 0; i < {{.MatTexturesMax}}; i++) {
  473. vec4 texcolor = texture(MatTexture[i], FragTexcoord * MatTexRepeat(i) + MatTexOffset(i));
  474. if (i == 0) {
  475. texCombined = texcolor;
  476. } else {
  477. texCombined = mix(texCombined, texcolor, texcolor.a);
  478. }
  479. }
  480. #endif
  481. // Combine material color with texture
  482. FragColor = min(vec4(Color, MatOpacity) * texCombined, vec4(1));
  483. }
  484. `
  485. const sprite_vertex_source = `//
  486. // Vertex shader for sprites
  487. //
  488. #include <attributes>
  489. // Input uniforms
  490. uniform mat4 MVP;
  491. #include <material>
  492. // Outputs for fragment shader
  493. out vec3 Color;
  494. out vec2 FragTexcoord;
  495. void main() {
  496. // Applies transformation to vertex position
  497. gl_Position = MVP * vec4(VertexPosition, 1.0);
  498. // Outputs color
  499. Color = MatDiffuseColor;
  500. // Flips texture coordinate Y if requested.
  501. vec2 texcoord = VertexTexcoord;
  502. #if MAT_TEXTURES>0
  503. if (MatTexFlipY[0]) {
  504. texcoord.y = 1 - texcoord.y;
  505. }
  506. #endif
  507. FragTexcoord = texcoord;
  508. }
  509. `
  510. const standard_fragment_source = `//
  511. // Fragment Shader template
  512. //
  513. #include <material>
  514. // Inputs from Vertex shader
  515. in vec3 ColorFrontAmbdiff;
  516. in vec3 ColorFrontSpec;
  517. in vec3 ColorBackAmbdiff;
  518. in vec3 ColorBackSpec;
  519. in vec2 FragTexcoord;
  520. // Output
  521. out vec4 FragColor;
  522. void main() {
  523. // Mix material color with textures colors
  524. vec4 texMixed = vec4(1);
  525. vec4 texColor;
  526. #if MAT_TEXTURES==1
  527. MIX_TEXTURE(0)
  528. #elif MAT_TEXTURES==2
  529. MIX_TEXTURE(0)
  530. MIX_TEXTURE(1)
  531. #elif MAT_TEXTURES==3
  532. MIX_TEXTURE(0)
  533. MIX_TEXTURE(1)
  534. MIX_TEXTURE(2)
  535. #endif
  536. vec4 colorAmbDiff;
  537. vec4 colorSpec;
  538. if (gl_FrontFacing) {
  539. colorAmbDiff = vec4(ColorFrontAmbdiff, MatOpacity);
  540. colorSpec = vec4(ColorFrontSpec, 0);
  541. } else {
  542. colorAmbDiff = vec4(ColorBackAmbdiff, MatOpacity);
  543. colorSpec = vec4(ColorBackSpec, 0);
  544. }
  545. FragColor = min(colorAmbDiff * texMixed + colorSpec, vec4(1));
  546. }
  547. `
  548. const standard_vertex_source = `//
  549. // Vertex shader standard
  550. //
  551. #include <attributes>
  552. // Model uniforms
  553. uniform mat4 ModelViewMatrix;
  554. uniform mat3 NormalMatrix;
  555. uniform mat4 MVP;
  556. #include <lights>
  557. #include <material>
  558. #include <phong_model>
  559. // Outputs for the fragment shader.
  560. out vec3 ColorFrontAmbdiff;
  561. out vec3 ColorFrontSpec;
  562. out vec3 ColorBackAmbdiff;
  563. out vec3 ColorBackSpec;
  564. out vec2 FragTexcoord;
  565. void main() {
  566. // Transform this vertex normal to camera coordinates.
  567. vec3 normal = normalize(NormalMatrix * VertexNormal);
  568. // Calculate this vertex position in camera coordinates
  569. vec4 position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  570. // Calculate the direction vector from the vertex to the camera
  571. // The camera is at 0,0,0
  572. vec3 camDir = normalize(-position.xyz);
  573. // Calculates the vertex Ambient+Diffuse and Specular colors using the Phong model
  574. // for the front and back
  575. phongModel(position, normal, camDir, MatAmbientColor, MatDiffuseColor, ColorFrontAmbdiff, ColorFrontSpec);
  576. phongModel(position, -normal, camDir, MatAmbientColor, MatDiffuseColor, ColorBackAmbdiff, ColorBackSpec);
  577. vec2 texcoord = VertexTexcoord;
  578. #if MAT_TEXTURES > 0
  579. // Flips texture coordinate Y if requested.
  580. if (MatTexFlipY(0)) {
  581. texcoord.y = 1 - texcoord.y;
  582. }
  583. #endif
  584. FragTexcoord = texcoord;
  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. "lights": include_lights_source,
  592. "material": include_material_source,
  593. "phong_model": include_phong_model_source,
  594. }
  595. // Maps shader name with its source code
  596. var shaderMap = map[string]string{
  597. "basic_fragment": basic_fragment_source,
  598. "basic_vertex": basic_vertex_source,
  599. "panel_fragment": panel_fragment_source,
  600. "panel_vertex": panel_vertex_source,
  601. "phong_fragment": phong_fragment_source,
  602. "phong_vertex": phong_vertex_source,
  603. "point_fragment": point_fragment_source,
  604. "point_vertex": point_vertex_source,
  605. "sprite_fragment": sprite_fragment_source,
  606. "sprite_vertex": sprite_vertex_source,
  607. "standard_fragment": standard_fragment_source,
  608. "standard_vertex": standard_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. }