sources.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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 physical_vertex_source = `//
  365. // Physical maiterial vertex shader
  366. //
  367. #include <attributes>
  368. // Model uniforms
  369. uniform mat4 ModelViewMatrix;
  370. uniform mat3 NormalMatrix;
  371. uniform mat4 MVP;
  372. #include <material>
  373. // Output variables for Fragment shader
  374. out vec4 Position;
  375. out vec3 Normal;
  376. out vec3 CamDir;
  377. out vec2 FragTexcoord;
  378. void main() {
  379. // Transform this vertex position to camera coordinates.
  380. Position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  381. // Transform this vertex normal to camera coordinates.
  382. Normal = normalize(NormalMatrix * VertexNormal);
  383. // Calculate the direction vector from the vertex to the camera
  384. // The camera is at 0,0,0
  385. CamDir = normalize(-Position.xyz);
  386. // Flips texture coordinate Y if requested.
  387. vec2 texcoord = VertexTexcoord;
  388. #if MAT_TEXTURES>0
  389. if (MatTexFlipY(0)) {
  390. texcoord.y = 1 - texcoord.y;
  391. }
  392. #endif
  393. FragTexcoord = texcoord;
  394. gl_Position = MVP * vec4(VertexPosition, 1.0);
  395. }
  396. `
  397. const point_fragment_source = `#include <material>
  398. // GLSL 3.30 does not allow indexing texture sampler with non constant values.
  399. // This macro is used to mix the texture with the specified index with the material color.
  400. // It should be called for each texture index.
  401. #define MIX_POINT_TEXTURE(i) \
  402. if (MatTexVisible(i)) { \
  403. vec2 pt = gl_PointCoord - vec2(0.5); \
  404. vec4 texColor = texture(MatTexture[i], (Rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i)); \
  405. if (i == 0) { \
  406. texMixed = texColor; \
  407. } else { \
  408. texMixed = mix(texMixed, texColor, texColor.a); \
  409. } \
  410. }
  411. // Inputs from vertex shader
  412. in vec3 Color;
  413. flat in mat2 Rotation;
  414. // Output
  415. out vec4 FragColor;
  416. void main() {
  417. // Mix material color with textures colors
  418. vec4 texMixed = vec4(1);
  419. #if MAT_TEXTURES==1
  420. MIX_POINT_TEXTURE(0)
  421. #elif MAT_TEXTURES==2
  422. MIX_POINT_TEXTURE(0)
  423. MIX_POINT_TEXTURE(1)
  424. #elif MAT_TEXTURES==3
  425. MIX_POINT_TEXTURE(0)
  426. MIX_POINT_TEXTURE(1)
  427. MIX_POINT_TEXTURE(2)
  428. #endif
  429. // Generates final color
  430. FragColor = min(vec4(Color, MatOpacity) * texMixed, vec4(1));
  431. }
  432. `
  433. const basic_fragment_source = `//
  434. // Fragment Shader template
  435. //
  436. in vec3 Color;
  437. out vec4 FragColor;
  438. void main() {
  439. FragColor = vec4(Color, 1.0);
  440. }
  441. `
  442. const phong_vertex_source = `//
  443. // Vertex Shader
  444. //
  445. #include <attributes>
  446. // Model uniforms
  447. uniform mat4 ModelViewMatrix;
  448. uniform mat3 NormalMatrix;
  449. uniform mat4 MVP;
  450. #include <material>
  451. // Output variables for Fragment shader
  452. out vec4 Position;
  453. out vec3 Normal;
  454. out vec3 CamDir;
  455. out vec2 FragTexcoord;
  456. void main() {
  457. // Transform this vertex position to camera coordinates.
  458. Position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  459. // Transform this vertex normal to camera coordinates.
  460. Normal = normalize(NormalMatrix * VertexNormal);
  461. // Calculate the direction vector from the vertex to the camera
  462. // The camera is at 0,0,0
  463. CamDir = normalize(-Position.xyz);
  464. // Flips texture coordinate Y if requested.
  465. vec2 texcoord = VertexTexcoord;
  466. #if MAT_TEXTURES>0
  467. if (MatTexFlipY(0)) {
  468. texcoord.y = 1 - texcoord.y;
  469. }
  470. #endif
  471. FragTexcoord = texcoord;
  472. gl_Position = MVP * vec4(VertexPosition, 1.0);
  473. }
  474. `
  475. const panel_fragment_source = `//
  476. // Fragment Shader template
  477. //
  478. // Texture uniforms
  479. uniform sampler2D MatTexture;
  480. uniform vec2 MatTexinfo[3];
  481. // Macros to access elements inside the MatTexinfo array
  482. #define MatTexOffset MatTexinfo[0]
  483. #define MatTexRepeat MatTexinfo[1]
  484. #define MatTexFlipY bool(MatTexinfo[2].x) // not used
  485. #define MatTexVisible bool(MatTexinfo[2].y) // not used
  486. // Inputs from vertex shader
  487. in vec2 FragTexcoord;
  488. // Input uniform
  489. uniform vec4 Panel[8];
  490. #define Bounds Panel[0] // panel bounds in texture coordinates
  491. #define Border Panel[1] // panel border in texture coordinates
  492. #define Padding Panel[2] // panel padding in texture coordinates
  493. #define Content Panel[3] // panel content area in texture coordinates
  494. #define BorderColor Panel[4] // panel border color
  495. #define PaddingColor Panel[5] // panel padding color
  496. #define ContentColor Panel[6] // panel content color
  497. #define TextureValid bool(Panel[7].x) // texture valid flag
  498. // Output
  499. out vec4 FragColor;
  500. /***
  501. * Checks if current fragment texture coordinate is inside the
  502. * supplied rectangle in texture coordinates:
  503. * rect[0] - position x [0,1]
  504. * rect[1] - position y [0,1]
  505. * rect[2] - width [0,1]
  506. * rect[3] - height [0,1]
  507. */
  508. bool checkRect(vec4 rect) {
  509. if (FragTexcoord.x < rect[0]) {
  510. return false;
  511. }
  512. if (FragTexcoord.x > rect[0] + rect[2]) {
  513. return false;
  514. }
  515. if (FragTexcoord.y < rect[1]) {
  516. return false;
  517. }
  518. if (FragTexcoord.y > rect[1] + rect[3]) {
  519. return false;
  520. }
  521. return true;
  522. }
  523. void main() {
  524. // Discard fragment outside of received bounds
  525. // Bounds[0] - xmin
  526. // Bounds[1] - ymin
  527. // Bounds[2] - xmax
  528. // Bounds[3] - ymax
  529. if (FragTexcoord.x <= Bounds[0] || FragTexcoord.x >= Bounds[2]) {
  530. discard;
  531. }
  532. if (FragTexcoord.y <= Bounds[1] || FragTexcoord.y >= Bounds[3]) {
  533. discard;
  534. }
  535. // Check if fragment is inside content area
  536. if (checkRect(Content)) {
  537. // If no texture, the color will be the material color.
  538. vec4 color = ContentColor;
  539. if (TextureValid) {
  540. // Adjust texture coordinates to fit texture inside the content area
  541. vec2 offset = vec2(-Content[0], -Content[1]);
  542. vec2 factor = vec2(1/Content[2], 1/Content[3]);
  543. vec2 texcoord = (FragTexcoord + offset) * factor;
  544. vec4 texColor = texture(MatTexture, texcoord * MatTexRepeat + MatTexOffset);
  545. // Mix content color with texture color ???
  546. //color = mix(color, texColor, texColor.a);
  547. color = texColor;
  548. }
  549. FragColor = color;
  550. return;
  551. }
  552. // Checks if fragment is inside paddings area
  553. if (checkRect(Padding)) {
  554. FragColor = PaddingColor;
  555. return;
  556. }
  557. // Checks if fragment is inside borders area
  558. if (checkRect(Border)) {
  559. FragColor = BorderColor;
  560. return;
  561. }
  562. // Fragment is in margins area (always transparent)
  563. FragColor = vec4(1,1,1,0);
  564. }
  565. `
  566. const phong_fragment_source = `//
  567. // Fragment Shader template
  568. //
  569. // Inputs from vertex shader
  570. in vec4 Position; // Vertex position in camera coordinates.
  571. in vec3 Normal; // Vertex normal in camera coordinates.
  572. in vec3 CamDir; // Direction from vertex to camera
  573. in vec2 FragTexcoord;
  574. #include <lights>
  575. #include <material>
  576. #include <phong_model>
  577. // Final fragment color
  578. out vec4 FragColor;
  579. void main() {
  580. // Mix material color with textures colors
  581. vec4 texMixed = vec4(1);
  582. vec4 texColor;
  583. #if MAT_TEXTURES==1
  584. MIX_TEXTURE(0)
  585. #elif MAT_TEXTURES==2
  586. MIX_TEXTURE(0)
  587. MIX_TEXTURE(1)
  588. #elif MAT_TEXTURES==3
  589. MIX_TEXTURE(0)
  590. MIX_TEXTURE(1)
  591. MIX_TEXTURE(2)
  592. #endif
  593. // Combine material with texture colors
  594. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texMixed;
  595. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texMixed;
  596. // Inverts the fragment normal if not FrontFacing
  597. vec3 fragNormal = Normal;
  598. if (!gl_FrontFacing) {
  599. fragNormal = -fragNormal;
  600. }
  601. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  602. vec3 Ambdiff, Spec;
  603. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  604. // Final fragment color
  605. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  606. }
  607. `
  608. const basic_vertex_source = `//
  609. // Vertex shader basic
  610. //
  611. #include <attributes>
  612. // Model uniforms
  613. uniform mat4 MVP;
  614. // Final output color for fragment shader
  615. out vec3 Color;
  616. void main() {
  617. Color = VertexColor;
  618. gl_Position = MVP * vec4(VertexPosition, 1.0);
  619. }
  620. `
  621. const physical_fragment_source = `//
  622. // Physical material fragment shader
  623. //
  624. // Inputs from vertex shader
  625. in vec4 Position; // Vertex position in camera coordinates.
  626. in vec3 Normal; // Vertex normal in camera coordinates.
  627. in vec3 CamDir; // Direction from vertex to camera
  628. in vec2 FragTexcoord;
  629. #include <lights>
  630. #include <material>
  631. #include <phong_model>
  632. // Final fragment color
  633. out vec4 FragColor;
  634. void main() {
  635. // Mix material color with textures colors
  636. vec4 texMixed = vec4(1);
  637. vec4 texColor;
  638. #if MAT_TEXTURES==1
  639. MIX_TEXTURE(0)
  640. #elif MAT_TEXTURES==2
  641. MIX_TEXTURE(0)
  642. MIX_TEXTURE(1)
  643. #elif MAT_TEXTURES==3
  644. MIX_TEXTURE(0)
  645. MIX_TEXTURE(1)
  646. MIX_TEXTURE(2)
  647. #endif
  648. // Combine material with texture colors
  649. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texMixed;
  650. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texMixed;
  651. // Inverts the fragment normal if not FrontFacing
  652. vec3 fragNormal = Normal;
  653. if (!gl_FrontFacing) {
  654. fragNormal = -fragNormal;
  655. }
  656. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  657. vec3 Ambdiff, Spec;
  658. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  659. // Final fragment color
  660. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  661. }
  662. `
  663. // Maps include name with its source code
  664. var includeMap = map[string]string{
  665. "attributes": include_attributes_source,
  666. "phong_model": include_phong_model_source,
  667. "lights": include_lights_source,
  668. "material": include_material_source,
  669. }
  670. // Maps shader name with its source code
  671. var shaderMap = map[string]string{
  672. "sprite_fragment": sprite_fragment_source,
  673. "sprite_vertex": sprite_vertex_source,
  674. "point_vertex": point_vertex_source,
  675. "panel_vertex": panel_vertex_source,
  676. "standard_fragment": standard_fragment_source,
  677. "standard_vertex": standard_vertex_source,
  678. "physical_vertex": physical_vertex_source,
  679. "point_fragment": point_fragment_source,
  680. "basic_fragment": basic_fragment_source,
  681. "phong_vertex": phong_vertex_source,
  682. "panel_fragment": panel_fragment_source,
  683. "phong_fragment": phong_fragment_source,
  684. "basic_vertex": basic_vertex_source,
  685. "physical_fragment": physical_fragment_source,
  686. }
  687. // Maps program name with Proginfo struct with shaders names
  688. var programMap = map[string]ProgramInfo{
  689. "basic": {"basic_vertex", "basic_fragment", ""},
  690. "panel": {"panel_vertex", "panel_fragment", ""},
  691. "phong": {"phong_vertex", "phong_fragment", ""},
  692. "physical": {"physical_vertex", "physical_fragment", ""},
  693. "point": {"point_vertex", "point_fragment", ""},
  694. "sprite": {"sprite_vertex", "sprite_fragment", ""},
  695. "standard": {"standard_vertex", "standard_fragment", ""},
  696. }