sources.go 17 KB

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