sources.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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 physical_fragment_source = `//
  413. // Physical material fragment shader
  414. //
  415. // Inputs from vertex shader
  416. in vec4 Position; // Vertex position in camera coordinates.
  417. in vec3 Normal; // Vertex normal in camera coordinates.
  418. in vec3 CamDir; // Direction from vertex to camera
  419. in vec2 FragTexcoord;
  420. // Material parameters uniform array
  421. uniform vec4 Material[3];
  422. // Macros to access elements inside the Material array
  423. #define uBaseColor Material[0]
  424. #define uEmissiveColor Material[1]
  425. #define uMetallicFactor Material[2].x
  426. #define uRoughnessFactor Material[2].y
  427. #include <lights>
  428. // Final fragment color
  429. out vec4 FragColor;
  430. void main() {
  431. // Inverts the fragment normal if not FrontFacing
  432. vec3 fragNormal = Normal;
  433. if (!gl_FrontFacing) {
  434. fragNormal = -fragNormal;
  435. }
  436. // Final fragment color
  437. FragColor = uBaseColor;
  438. }
  439. `
  440. const physical_vertex_source = `//
  441. // Physical maiterial vertex shader
  442. //
  443. #include <attributes>
  444. // Model uniforms
  445. uniform mat4 ModelViewMatrix;
  446. uniform mat3 NormalMatrix;
  447. uniform mat4 MVP;
  448. // Output variables for Fragment shader
  449. out vec4 Position;
  450. out vec3 Normal;
  451. out vec3 CamDir;
  452. out vec2 FragTexcoord;
  453. void main() {
  454. // Transform this vertex position to camera coordinates.
  455. Position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  456. // Transform this vertex normal to camera coordinates.
  457. Normal = normalize(NormalMatrix * VertexNormal);
  458. // Calculate the direction vector from the vertex to the camera
  459. // The camera is at 0,0,0
  460. CamDir = normalize(-Position.xyz);
  461. // // Flips texture coordinate Y if requested.
  462. vec2 texcoord = VertexTexcoord;
  463. //#if MAT_TEXTURES>0
  464. // if (MatTexFlipY(0)) {
  465. // texcoord.y = 1 - texcoord.y;
  466. // }
  467. //#endif
  468. FragTexcoord = texcoord;
  469. gl_Position = MVP * vec4(VertexPosition, 1.0);
  470. }
  471. `
  472. const point_fragment_source = `#include <material>
  473. // GLSL 3.30 does not allow indexing texture sampler with non constant values.
  474. // This macro is used to mix the texture with the specified index with the material color.
  475. // It should be called for each texture index.
  476. #define MIX_POINT_TEXTURE(i) \
  477. if (MatTexVisible(i)) { \
  478. vec2 pt = gl_PointCoord - vec2(0.5); \
  479. vec4 texColor = texture(MatTexture[i], (Rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i)); \
  480. if (i == 0) { \
  481. texMixed = texColor; \
  482. } else { \
  483. texMixed = mix(texMixed, texColor, texColor.a); \
  484. } \
  485. }
  486. // Inputs from vertex shader
  487. in vec3 Color;
  488. flat in mat2 Rotation;
  489. // Output
  490. out vec4 FragColor;
  491. void main() {
  492. // Mix material color with textures colors
  493. vec4 texMixed = vec4(1);
  494. #if MAT_TEXTURES==1
  495. MIX_POINT_TEXTURE(0)
  496. #elif MAT_TEXTURES==2
  497. MIX_POINT_TEXTURE(0)
  498. MIX_POINT_TEXTURE(1)
  499. #elif MAT_TEXTURES==3
  500. MIX_POINT_TEXTURE(0)
  501. MIX_POINT_TEXTURE(1)
  502. MIX_POINT_TEXTURE(2)
  503. #endif
  504. // Generates final color
  505. FragColor = min(vec4(Color, MatOpacity) * texMixed, vec4(1));
  506. }
  507. `
  508. const point_vertex_source = `#include <attributes>
  509. // Model uniforms
  510. uniform mat4 MVP;
  511. // Material uniforms
  512. #include <material>
  513. // Outputs for fragment shader
  514. out vec3 Color;
  515. flat out mat2 Rotation;
  516. void main() {
  517. // Rotation matrix for fragment shader
  518. float rotSin = sin(MatPointRotationZ);
  519. float rotCos = cos(MatPointRotationZ);
  520. Rotation = mat2(rotCos, rotSin, - rotSin, rotCos);
  521. // Sets the vertex position
  522. vec4 pos = MVP * vec4(VertexPosition, 1.0);
  523. gl_Position = pos;
  524. // Sets the size of the rasterized point decreasing with distance
  525. gl_PointSize = (1.0 - pos.z / pos.w) * MatPointSize;
  526. // Outputs color
  527. Color = MatEmissiveColor;
  528. }
  529. `
  530. const sprite_fragment_source = `//
  531. // Fragment shader for sprite
  532. //
  533. #include <material>
  534. // Inputs from vertex shader
  535. in vec3 Color;
  536. in vec2 FragTexcoord;
  537. // Output
  538. out vec4 FragColor;
  539. void main() {
  540. // Combine all texture colors and opacity
  541. vec4 texCombined = vec4(1);
  542. #if MAT_TEXTURES>0
  543. for (int i = 0; i < {{.MatTexturesMax}}; i++) {
  544. vec4 texcolor = texture(MatTexture[i], FragTexcoord * MatTexRepeat(i) + MatTexOffset(i));
  545. if (i == 0) {
  546. texCombined = texcolor;
  547. } else {
  548. texCombined = mix(texCombined, texcolor, texcolor.a);
  549. }
  550. }
  551. #endif
  552. // Combine material color with texture
  553. FragColor = min(vec4(Color, MatOpacity) * texCombined, vec4(1));
  554. }
  555. `
  556. const sprite_vertex_source = `//
  557. // Vertex shader for sprites
  558. //
  559. #include <attributes>
  560. // Input uniforms
  561. uniform mat4 MVP;
  562. #include <material>
  563. // Outputs for fragment shader
  564. out vec3 Color;
  565. out vec2 FragTexcoord;
  566. void main() {
  567. // Applies transformation to vertex position
  568. gl_Position = MVP * vec4(VertexPosition, 1.0);
  569. // Outputs color
  570. Color = MatDiffuseColor;
  571. // Flips texture coordinate Y if requested.
  572. vec2 texcoord = VertexTexcoord;
  573. #if MAT_TEXTURES>0
  574. if (MatTexFlipY[0]) {
  575. texcoord.y = 1 - texcoord.y;
  576. }
  577. #endif
  578. FragTexcoord = texcoord;
  579. }
  580. `
  581. const standard_fragment_source = `//
  582. // Fragment Shader template
  583. //
  584. #include <material>
  585. // Inputs from Vertex shader
  586. in vec3 ColorFrontAmbdiff;
  587. in vec3 ColorFrontSpec;
  588. in vec3 ColorBackAmbdiff;
  589. in vec3 ColorBackSpec;
  590. in vec2 FragTexcoord;
  591. // Output
  592. out vec4 FragColor;
  593. void main() {
  594. // Mix material color with textures colors
  595. vec4 texMixed = vec4(1);
  596. vec4 texColor;
  597. #if MAT_TEXTURES==1
  598. MIX_TEXTURE(0)
  599. #elif MAT_TEXTURES==2
  600. MIX_TEXTURE(0)
  601. MIX_TEXTURE(1)
  602. #elif MAT_TEXTURES==3
  603. MIX_TEXTURE(0)
  604. MIX_TEXTURE(1)
  605. MIX_TEXTURE(2)
  606. #endif
  607. vec4 colorAmbDiff;
  608. vec4 colorSpec;
  609. if (gl_FrontFacing) {
  610. colorAmbDiff = vec4(ColorFrontAmbdiff, MatOpacity);
  611. colorSpec = vec4(ColorFrontSpec, 0);
  612. } else {
  613. colorAmbDiff = vec4(ColorBackAmbdiff, MatOpacity);
  614. colorSpec = vec4(ColorBackSpec, 0);
  615. }
  616. FragColor = min(colorAmbDiff * texMixed + colorSpec, vec4(1));
  617. }
  618. `
  619. const standard_vertex_source = `//
  620. // Vertex shader standard
  621. //
  622. #include <attributes>
  623. // Model uniforms
  624. uniform mat4 ModelViewMatrix;
  625. uniform mat3 NormalMatrix;
  626. uniform mat4 MVP;
  627. #include <lights>
  628. #include <material>
  629. #include <phong_model>
  630. // Outputs for the fragment shader.
  631. out vec3 ColorFrontAmbdiff;
  632. out vec3 ColorFrontSpec;
  633. out vec3 ColorBackAmbdiff;
  634. out vec3 ColorBackSpec;
  635. out vec2 FragTexcoord;
  636. void main() {
  637. // Transform this vertex normal to camera coordinates.
  638. vec3 normal = normalize(NormalMatrix * VertexNormal);
  639. // Calculate this vertex position in camera coordinates
  640. vec4 position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  641. // Calculate the direction vector from the vertex to the camera
  642. // The camera is at 0,0,0
  643. vec3 camDir = normalize(-position.xyz);
  644. // Calculates the vertex Ambient+Diffuse and Specular colors using the Phong model
  645. // for the front and back
  646. phongModel(position, normal, camDir, MatAmbientColor, MatDiffuseColor, ColorFrontAmbdiff, ColorFrontSpec);
  647. phongModel(position, -normal, camDir, MatAmbientColor, MatDiffuseColor, ColorBackAmbdiff, ColorBackSpec);
  648. vec2 texcoord = VertexTexcoord;
  649. #if MAT_TEXTURES > 0
  650. // Flips texture coordinate Y if requested.
  651. if (MatTexFlipY(0)) {
  652. texcoord.y = 1 - texcoord.y;
  653. }
  654. #endif
  655. FragTexcoord = texcoord;
  656. gl_Position = MVP * vec4(VertexPosition, 1.0);
  657. }
  658. `
  659. // Maps include name with its source code
  660. var includeMap = map[string]string{
  661. "attributes": include_attributes_source,
  662. "lights": include_lights_source,
  663. "material": include_material_source,
  664. "phong_model": include_phong_model_source,
  665. }
  666. // Maps shader name with its source code
  667. var shaderMap = map[string]string{
  668. "basic_fragment": basic_fragment_source,
  669. "basic_vertex": basic_vertex_source,
  670. "panel_fragment": panel_fragment_source,
  671. "panel_vertex": panel_vertex_source,
  672. "phong_fragment": phong_fragment_source,
  673. "phong_vertex": phong_vertex_source,
  674. "physical_fragment": physical_fragment_source,
  675. "physical_vertex": physical_vertex_source,
  676. "point_fragment": point_fragment_source,
  677. "point_vertex": point_vertex_source,
  678. "sprite_fragment": sprite_fragment_source,
  679. "sprite_vertex": sprite_vertex_source,
  680. "standard_fragment": standard_fragment_source,
  681. "standard_vertex": standard_vertex_source,
  682. }
  683. // Maps program name with Proginfo struct with shaders names
  684. var programMap = map[string]ProgramInfo{
  685. "basic": {"basic_vertex", "basic_fragment", ""},
  686. "panel": {"panel_vertex", "panel_fragment", ""},
  687. "phong": {"phong_vertex", "phong_fragment", ""},
  688. "physical": {"physical_vertex", "physical_fragment", ""},
  689. "point": {"point_vertex", "point_fragment", ""},
  690. "sprite": {"sprite_vertex", "sprite_fragment", ""},
  691. "standard": {"standard_vertex", "standard_fragment", ""},
  692. }