sources.go 25 KB

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