sources.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. // TODO for alpha blending dont use mix use implementation below (similar to one in panel shader)
  91. //vec4 prevTexPre = texMixed; \
  92. //prevTexPre.rgb *= prevTexPre.a; \
  93. //vec4 currTexPre = texColor; \
  94. //currTexPre.rgb *= currTexPre.a; \
  95. //texMixed = currTexPre + prevTexPre * (1 - currTexPre.a); \
  96. //texMixed.rgb /= texMixed.a; \
  97. `
  98. const include_phong_model_source = `/***
  99. phong lighting model
  100. Parameters:
  101. position: input vertex position in camera coordinates
  102. normal: input vertex normal in camera coordinates
  103. camDir: input camera directions
  104. matAmbient: input material ambient color
  105. matDiffuse: input material diffuse color
  106. ambdiff: output ambient+diffuse color
  107. spec: output specular color
  108. Uniforms:
  109. AmbientLightColor[]
  110. DiffuseLightColor[]
  111. DiffuseLightPosition[]
  112. PointLightColor[]
  113. PointLightPosition[]
  114. PointLightLinearDecay[]
  115. PointLightQuadraticDecay[]
  116. MatSpecularColor
  117. MatShininess
  118. *****/
  119. void phongModel(vec4 position, vec3 normal, vec3 camDir, vec3 matAmbient, vec3 matDiffuse, out vec3 ambdiff, out vec3 spec) {
  120. vec3 ambientTotal = vec3(0.0);
  121. vec3 diffuseTotal = vec3(0.0);
  122. vec3 specularTotal = vec3(0.0);
  123. #if AMB_LIGHTS>0
  124. // Ambient lights
  125. for (int i = 0; i < AMB_LIGHTS; i++) {
  126. ambientTotal += AmbientLightColor[i] * matAmbient;
  127. }
  128. #endif
  129. #if DIR_LIGHTS>0
  130. // Directional lights
  131. for (int i = 0; i < DIR_LIGHTS; i++) {
  132. // Diffuse reflection
  133. // DirLightPosition is the direction of the current light
  134. vec3 lightDirection = normalize(DirLightPosition(i));
  135. // Calculates the dot product between the light direction and this vertex normal.
  136. float dotNormal = max(dot(lightDirection, normal), 0.0);
  137. diffuseTotal += DirLightColor(i) * matDiffuse * dotNormal;
  138. // Specular reflection
  139. // Calculates the light reflection vector
  140. vec3 ref = reflect(-lightDirection, normal);
  141. if (dotNormal > 0.0) {
  142. specularTotal += DirLightColor(i) * MatSpecularColor * pow(max(dot(ref, camDir), 0.0), MatShininess);
  143. }
  144. }
  145. #endif
  146. #if POINT_LIGHTS>0
  147. // Point lights
  148. for (int i = 0; i < POINT_LIGHTS; i++) {
  149. // Common calculations
  150. // Calculates the direction and distance from the current vertex to this point light.
  151. vec3 lightDirection = PointLightPosition(i) - vec3(position);
  152. float lightDistance = length(lightDirection);
  153. // Normalizes the lightDirection
  154. lightDirection = lightDirection / lightDistance;
  155. // Calculates the attenuation due to the distance of the light
  156. float attenuation = 1.0 / (1.0 + PointLightLinearDecay(i) * lightDistance +
  157. PointLightQuadraticDecay(i) * lightDistance * lightDistance);
  158. // Diffuse reflection
  159. float dotNormal = max(dot(lightDirection, normal), 0.0);
  160. diffuseTotal += PointLightColor(i) * matDiffuse * dotNormal * attenuation;
  161. // Specular reflection
  162. // Calculates the light reflection vector
  163. vec3 ref = reflect(-lightDirection, normal);
  164. if (dotNormal > 0.0) {
  165. specularTotal += PointLightColor(i) * MatSpecularColor *
  166. pow(max(dot(ref, camDir), 0.0), MatShininess) * attenuation;
  167. }
  168. }
  169. #endif
  170. #if SPOT_LIGHTS>0
  171. for (int i = 0; i < SPOT_LIGHTS; i++) {
  172. // Calculates the direction and distance from the current vertex to this spot light.
  173. vec3 lightDirection = SpotLightPosition(i) - vec3(position);
  174. float lightDistance = length(lightDirection);
  175. lightDirection = lightDirection / lightDistance;
  176. // Calculates the attenuation due to the distance of the light
  177. float attenuation = 1.0 / (1.0 + SpotLightLinearDecay(i) * lightDistance +
  178. SpotLightQuadraticDecay(i) * lightDistance * lightDistance);
  179. // Calculates the angle between the vertex direction and spot direction
  180. // If this angle is greater than the cutoff the spotlight will not contribute
  181. // to the final color.
  182. float angle = acos(dot(-lightDirection, SpotLightDirection(i)));
  183. float cutoff = radians(clamp(SpotLightCutoffAngle(i), 0.0, 90.0));
  184. if (angle < cutoff) {
  185. float spotFactor = pow(dot(-lightDirection, SpotLightDirection(i)), SpotLightAngularDecay(i));
  186. // Diffuse reflection
  187. float dotNormal = max(dot(lightDirection, normal), 0.0);
  188. diffuseTotal += SpotLightColor(i) * matDiffuse * dotNormal * attenuation * spotFactor;
  189. // Specular reflection
  190. vec3 ref = reflect(-lightDirection, normal);
  191. if (dotNormal > 0.0) {
  192. specularTotal += SpotLightColor(i) * MatSpecularColor * pow(max(dot(ref, camDir), 0.0), MatShininess) * attenuation * spotFactor;
  193. }
  194. }
  195. }
  196. #endif
  197. // Sets output colors
  198. ambdiff = ambientTotal + MatEmissiveColor + diffuseTotal;
  199. spec = specularTotal;
  200. }
  201. `
  202. const basic_fragment_source = `//
  203. // Fragment Shader template
  204. //
  205. in vec3 Color;
  206. out vec4 FragColor;
  207. void main() {
  208. FragColor = vec4(Color, 1.0);
  209. }
  210. `
  211. const basic_vertex_source = `//
  212. // Vertex shader basic
  213. //
  214. #include <attributes>
  215. // Model uniforms
  216. uniform mat4 MVP;
  217. // Final output color for fragment shader
  218. out vec3 Color;
  219. void main() {
  220. Color = VertexColor;
  221. gl_Position = MVP * vec4(VertexPosition, 1.0);
  222. }
  223. `
  224. const panel_fragment_source = `//
  225. // Fragment Shader template
  226. //
  227. // Texture uniforms
  228. uniform sampler2D MatTexture;
  229. uniform vec2 MatTexinfo[3];
  230. // Macros to access elements inside the MatTexinfo array
  231. #define MatTexOffset MatTexinfo[0]
  232. #define MatTexRepeat MatTexinfo[1]
  233. #define MatTexFlipY bool(MatTexinfo[2].x) // not used
  234. #define MatTexVisible bool(MatTexinfo[2].y) // not used
  235. // Inputs from vertex shader
  236. in vec2 FragTexcoord;
  237. // Input uniform
  238. uniform vec4 Panel[8];
  239. #define Bounds Panel[0] // panel bounds in texture coordinates
  240. #define Border Panel[1] // panel border in texture coordinates
  241. #define Padding Panel[2] // panel padding in texture coordinates
  242. #define Content Panel[3] // panel content area in texture coordinates
  243. #define BorderColor Panel[4] // panel border color
  244. #define PaddingColor Panel[5] // panel padding color
  245. #define ContentColor Panel[6] // panel content color
  246. #define TextureValid bool(Panel[7].x) // texture valid flag
  247. // Output
  248. out vec4 FragColor;
  249. /***
  250. * Checks if current fragment texture coordinate is inside the
  251. * supplied rectangle in texture coordinates:
  252. * rect[0] - position x [0,1]
  253. * rect[1] - position y [0,1]
  254. * rect[2] - width [0,1]
  255. * rect[3] - height [0,1]
  256. */
  257. bool checkRect(vec4 rect) {
  258. if (FragTexcoord.x < rect[0]) {
  259. return false;
  260. }
  261. if (FragTexcoord.x > rect[0] + rect[2]) {
  262. return false;
  263. }
  264. if (FragTexcoord.y < rect[1]) {
  265. return false;
  266. }
  267. if (FragTexcoord.y > rect[1] + rect[3]) {
  268. return false;
  269. }
  270. return true;
  271. }
  272. void main() {
  273. // Discard fragment outside of received bounds
  274. // Bounds[0] - xmin
  275. // Bounds[1] - ymin
  276. // Bounds[2] - xmax
  277. // Bounds[3] - ymax
  278. if (FragTexcoord.x <= Bounds[0] || FragTexcoord.x >= Bounds[2]) {
  279. discard;
  280. }
  281. if (FragTexcoord.y <= Bounds[1] || FragTexcoord.y >= Bounds[3]) {
  282. discard;
  283. }
  284. // Check if fragment is inside content area
  285. if (checkRect(Content)) {
  286. // If no texture, the color will be the material color.
  287. vec4 color = ContentColor;
  288. if (TextureValid) {
  289. // Adjust texture coordinates to fit texture inside the content area
  290. vec2 offset = vec2(-Content[0], -Content[1]);
  291. vec2 factor = vec2(1/Content[2], 1/Content[3]);
  292. vec2 texcoord = (FragTexcoord + offset) * factor;
  293. vec4 texColor = texture(MatTexture, texcoord * MatTexRepeat + MatTexOffset);
  294. // Mix content color with texture color.
  295. // Note that doing a simple linear interpolation (e.g. using mix()) is not correct!
  296. // The right formula can be found here: https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
  297. // For a more in-depth discussion: http://apoorvaj.io/alpha-compositing-opengl-blending-and-premultiplied-alpha.html#toc4
  298. // Pre-multiply the content color
  299. vec4 contentPre = ContentColor;
  300. contentPre.rgb *= contentPre.a;
  301. // Pre-multiply the texture color
  302. vec4 texPre = texColor;
  303. texPre.rgb *= texPre.a;
  304. // Combine colors the premultiplied final color
  305. color = texPre + contentPre * (1 - texPre.a);
  306. // Un-pre-multiply (pre-divide? :P)
  307. color.rgb /= color.a;
  308. }
  309. FragColor = color;
  310. return;
  311. }
  312. // Checks if fragment is inside paddings area
  313. if (checkRect(Padding)) {
  314. FragColor = PaddingColor;
  315. return;
  316. }
  317. // Checks if fragment is inside borders area
  318. if (checkRect(Border)) {
  319. FragColor = BorderColor;
  320. return;
  321. }
  322. // Fragment is in margins area (always transparent)
  323. FragColor = vec4(1,1,1,0);
  324. }
  325. `
  326. const panel_vertex_source = `//
  327. // Vertex shader panel
  328. //
  329. #include <attributes>
  330. // Model uniforms
  331. uniform mat4 ModelMatrix;
  332. // Outputs for fragment shader
  333. out vec2 FragTexcoord;
  334. void main() {
  335. // Always flip texture coordinates
  336. vec2 texcoord = VertexTexcoord;
  337. texcoord.y = 1 - texcoord.y;
  338. FragTexcoord = texcoord;
  339. // Set position
  340. vec4 pos = vec4(VertexPosition.xyz, 1);
  341. gl_Position = ModelMatrix * pos;
  342. }
  343. `
  344. const phong_fragment_source = `//
  345. // Fragment Shader template
  346. //
  347. // Inputs from vertex shader
  348. in vec4 Position; // Vertex position in camera coordinates.
  349. in vec3 Normal; // Vertex normal in camera coordinates.
  350. in vec3 CamDir; // Direction from vertex to camera
  351. in vec2 FragTexcoord;
  352. #include <lights>
  353. #include <material>
  354. #include <phong_model>
  355. // Final fragment color
  356. out vec4 FragColor;
  357. void main() {
  358. // Mix material color with textures colors
  359. vec4 texMixed = vec4(1);
  360. vec4 texColor;
  361. #if MAT_TEXTURES==1
  362. MIX_TEXTURE(0)
  363. #elif MAT_TEXTURES==2
  364. MIX_TEXTURE(0)
  365. MIX_TEXTURE(1)
  366. #elif MAT_TEXTURES==3
  367. MIX_TEXTURE(0)
  368. MIX_TEXTURE(1)
  369. MIX_TEXTURE(2)
  370. #endif
  371. // Combine material with texture colors
  372. vec4 matDiffuse = vec4(MatDiffuseColor, MatOpacity) * texMixed;
  373. vec4 matAmbient = vec4(MatAmbientColor, MatOpacity) * texMixed;
  374. // Inverts the fragment normal if not FrontFacing
  375. vec3 fragNormal = Normal;
  376. if (!gl_FrontFacing) {
  377. fragNormal = -fragNormal;
  378. }
  379. // Calculates the Ambient+Diffuse and Specular colors for this fragment using the Phong model.
  380. vec3 Ambdiff, Spec;
  381. phongModel(Position, fragNormal, CamDir, vec3(matAmbient), vec3(matDiffuse), Ambdiff, Spec);
  382. // Final fragment color
  383. FragColor = min(vec4(Ambdiff + Spec, matDiffuse.a), vec4(1.0));
  384. }
  385. `
  386. const phong_vertex_source = `//
  387. // Vertex Shader
  388. //
  389. #include <attributes>
  390. // Model uniforms
  391. uniform mat4 ModelViewMatrix;
  392. uniform mat3 NormalMatrix;
  393. uniform mat4 MVP;
  394. #include <material>
  395. // Output variables for Fragment shader
  396. out vec4 Position;
  397. out vec3 Normal;
  398. out vec3 CamDir;
  399. out vec2 FragTexcoord;
  400. void main() {
  401. // Transform this vertex position to camera coordinates.
  402. Position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  403. // Transform this vertex normal to camera coordinates.
  404. Normal = normalize(NormalMatrix * VertexNormal);
  405. // Calculate the direction vector from the vertex to the camera
  406. // The camera is at 0,0,0
  407. CamDir = normalize(-Position.xyz);
  408. // Flips texture coordinate Y if requested.
  409. vec2 texcoord = VertexTexcoord;
  410. #if MAT_TEXTURES>0
  411. if (MatTexFlipY(0)) {
  412. texcoord.y = 1 - texcoord.y;
  413. }
  414. #endif
  415. FragTexcoord = texcoord;
  416. gl_Position = MVP * vec4(VertexPosition, 1.0);
  417. }
  418. `
  419. const physical_fragment_source = `//
  420. // Physically Based Shading of a microfacet surface material - Fragment Shader
  421. // Modified from reference implementation at https://github.com/KhronosGroup/glTF-WebGL-PBR
  422. //
  423. // References:
  424. // [1] Real Shading in Unreal Engine 4
  425. // http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
  426. // [2] Physically Based Shading at Disney
  427. // http://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf
  428. // [3] README.md - Environment Maps
  429. // https://github.com/KhronosGroup/glTF-WebGL-PBR/#environment-maps
  430. // [4] "An Inexpensive BRDF Model for Physically based Rendering" by Christophe Schlick
  431. // https://www.cs.virginia.edu/~jdl/bib/appearance/analytic%20models/schlick94b.pdf
  432. //#extension GL_EXT_shader_texture_lod: enable
  433. //#extension GL_OES_standard_derivatives : enable
  434. precision highp float;
  435. //uniform vec3 u_LightDirection;
  436. //uniform vec3 u_LightColor;
  437. //#ifdef USE_IBL
  438. //uniform samplerCube u_DiffuseEnvSampler;
  439. //uniform samplerCube u_SpecularEnvSampler;
  440. //uniform sampler2D u_brdfLUT;
  441. //#endif
  442. #ifdef HAS_BASECOLORMAP
  443. uniform sampler2D uBaseColorSampler;
  444. #endif
  445. #ifdef HAS_METALROUGHNESSMAP
  446. uniform sampler2D uMetallicRoughnessSampler;
  447. #endif
  448. #ifdef HAS_NORMALMAP
  449. uniform sampler2D uNormalSampler;
  450. //uniform float uNormalScale;
  451. #endif
  452. #ifdef HAS_EMISSIVEMAP
  453. uniform sampler2D uEmissiveSampler;
  454. #endif
  455. #ifdef HAS_OCCLUSIONMAP
  456. uniform sampler2D uOcclusionSampler;
  457. uniform float uOcclusionStrength;
  458. #endif
  459. // Material parameters uniform array
  460. uniform vec4 Material[3];
  461. // Macros to access elements inside the Material array
  462. #define uBaseColor Material[0]
  463. #define uEmissiveColor Material[1]
  464. #define uMetallicFactor Material[2].x
  465. #define uRoughnessFactor Material[2].y
  466. #include <lights>
  467. // Inputs from vertex shader
  468. in vec3 Position; // Vertex position in camera coordinates.
  469. in vec3 Normal; // Vertex normal in camera coordinates.
  470. in vec3 CamDir; // Direction from vertex to camera
  471. in vec2 FragTexcoord;
  472. // Final fragment color
  473. out vec4 FragColor;
  474. // Encapsulate the various inputs used by the various functions in the shading equation
  475. // We store values in this struct to simplify the integration of alternative implementations
  476. // of the shading terms, outlined in the Readme.MD Appendix.
  477. struct PBRLightInfo
  478. {
  479. float NdotL; // cos angle between normal and light direction
  480. float NdotV; // cos angle between normal and view direction
  481. float NdotH; // cos angle between normal and half vector
  482. float LdotH; // cos angle between light direction and half vector
  483. float VdotH; // cos angle between view direction and half vector
  484. };
  485. struct PBRInfo
  486. {
  487. float perceptualRoughness; // roughness value, as authored by the model creator (input to shader)
  488. float metalness; // metallic value at the surface
  489. vec3 reflectance0; // full reflectance color (normal incidence angle)
  490. vec3 reflectance90; // reflectance color at grazing angle
  491. float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2])
  492. vec3 diffuseColor; // color contribution from diffuse lighting
  493. vec3 specularColor; // color contribution from specular lighting
  494. };
  495. const float M_PI = 3.141592653589793;
  496. const float c_MinRoughness = 0.04;
  497. vec4 SRGBtoLINEAR(vec4 srgbIn) {
  498. //#ifdef MANUAL_SRGB
  499. // #ifdef SRGB_FAST_APPROXIMATION
  500. // vec3 linOut = pow(srgbIn.xyz,vec3(2.2));
  501. // #else //SRGB_FAST_APPROXIMATION
  502. vec3 bLess = step(vec3(0.04045),srgbIn.xyz);
  503. vec3 linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );
  504. // #endif //SRGB_FAST_APPROXIMATION
  505. return vec4(linOut,srgbIn.w);
  506. //#else //MANUAL_SRGB
  507. // return srgbIn;
  508. //#endif //MANUAL_SRGB
  509. }
  510. // Find the normal for this fragment, pulling either from a predefined normal map
  511. // or from the interpolated mesh normal and tangent attributes.
  512. vec3 getNormal()
  513. {
  514. // Retrieve the tangent space matrix
  515. //#ifndef HAS_TANGENTS
  516. vec3 pos_dx = dFdx(Position);
  517. vec3 pos_dy = dFdy(Position);
  518. vec3 tex_dx = dFdx(vec3(FragTexcoord, 0.0));
  519. vec3 tex_dy = dFdy(vec3(FragTexcoord, 0.0));
  520. vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);
  521. //#ifdef HAS_NORMALS
  522. vec3 ng = normalize(Normal);
  523. //#else
  524. // vec3 ng = cross(pos_dx, pos_dy);
  525. //#endif
  526. t = normalize(t - ng * dot(ng, t));
  527. vec3 b = normalize(cross(ng, t));
  528. mat3 tbn = mat3(t, b, ng);
  529. //#else // HAS_TANGENTS
  530. // mat3 tbn = v_TBN;
  531. //#endif
  532. #ifdef HAS_NORMALMAP
  533. float uNormalScale = 1.0;
  534. vec3 n = texture(uNormalSampler, FragTexcoord).rgb;
  535. n = normalize(tbn * ((2.0 * n - 1.0) * vec3(uNormalScale, uNormalScale, 1.0)));
  536. #else
  537. // The tbn matrix is linearly interpolated, so we need to re-normalize
  538. vec3 n = normalize(tbn[2].xyz);
  539. #endif
  540. return n;
  541. }
  542. // Calculation of the lighting contribution from an optional Image Based Light source.
  543. // Precomputed Environment Maps are required uniform inputs and are computed as outlined in [1].
  544. // See our README.md on Environment Maps [3] for additional discussion.
  545. vec3 getIBLContribution(PBRInfo pbrInputs, PBRLightInfo pbrLight, vec3 n, vec3 reflection)
  546. {
  547. float mipCount = 9.0; // resolution of 512x512
  548. float lod = (pbrInputs.perceptualRoughness * mipCount);
  549. // retrieve a scale and bias to F0. See [1], Figure 3
  550. vec3 brdf = vec3(0.5,0.5,0.5);//SRGBtoLINEAR(texture(u_brdfLUT, vec2(pbrLight.NdotV, 1.0 - pbrInputs.perceptualRoughness))).rgb;
  551. vec3 diffuseLight = vec3(0.5,0.5,0.5);//SRGBtoLINEAR(textureCube(u_DiffuseEnvSampler, n)).rgb;
  552. //#ifdef USE_TEX_LOD
  553. // vec3 specularLight = SRGBtoLINEAR(textureCubeLodEXT(u_SpecularEnvSampler, reflection, lod)).rgb;
  554. //#else
  555. vec3 specularLight = vec3(0.5,0.5,0.5);//SRGBtoLINEAR(textureCube(u_SpecularEnvSampler, reflection)).rgb;
  556. //#endif
  557. vec3 diffuse = diffuseLight * pbrInputs.diffuseColor;
  558. vec3 specular = specularLight * (pbrInputs.specularColor * brdf.x + brdf.y);
  559. // For presentation, this allows us to disable IBL terms
  560. // diffuse *= u_ScaleIBLAmbient.x;
  561. // specular *= u_ScaleIBLAmbient.y;
  562. return diffuse + specular;
  563. }
  564. // Basic Lambertian diffuse
  565. // Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog
  566. // See also [1], Equation 1
  567. vec3 diffuse(PBRInfo pbrInputs)
  568. {
  569. return pbrInputs.diffuseColor / M_PI;
  570. }
  571. // The following equation models the Fresnel reflectance term of the spec equation (aka F())
  572. // Implementation of fresnel from [4], Equation 15
  573. vec3 specularReflection(PBRInfo pbrInputs, PBRLightInfo pbrLight)
  574. {
  575. return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance0) * pow(clamp(1.0 - pbrLight.VdotH, 0.0, 1.0), 5.0);
  576. }
  577. // This calculates the specular geometric attenuation (aka G()),
  578. // where rougher material will reflect less light back to the viewer.
  579. // This implementation is based on [1] Equation 4, and we adopt their modifications to
  580. // alphaRoughness as input as originally proposed in [2].
  581. float geometricOcclusion(PBRInfo pbrInputs, PBRLightInfo pbrLight)
  582. {
  583. float NdotL = pbrLight.NdotL;
  584. float NdotV = pbrLight.NdotV;
  585. float r = pbrInputs.alphaRoughness;
  586. float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));
  587. float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));
  588. return attenuationL * attenuationV;
  589. }
  590. // The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D())
  591. // Implementation from "Average Irregularity Representation of a Roughened Surface for Ray Reflection" by T. S. Trowbridge, and K. P. Reitz
  592. // Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3.
  593. float microfacetDistribution(PBRInfo pbrInputs, PBRLightInfo pbrLight)
  594. {
  595. float roughnessSq = pbrInputs.alphaRoughness * pbrInputs.alphaRoughness;
  596. float f = (pbrLight.NdotH * roughnessSq - pbrLight.NdotH) * pbrLight.NdotH + 1.0;
  597. return roughnessSq / (M_PI * f * f);
  598. }
  599. vec3 pbrModel(PBRInfo pbrInputs, vec3 lightColor, vec3 lightDir) {
  600. vec3 n = getNormal(); // normal at surface point
  601. vec3 v = normalize(CamDir); // Vector from surface point to camera
  602. vec3 l = normalize(lightDir); // Vector from surface point to light
  603. vec3 h = normalize(l+v); // Half vector between both l and v
  604. vec3 reflection = -normalize(reflect(v, n));
  605. float NdotL = clamp(dot(n, l), 0.001, 1.0);
  606. float NdotV = abs(dot(n, v)) + 0.001;
  607. float NdotH = clamp(dot(n, h), 0.0, 1.0);
  608. float LdotH = clamp(dot(l, h), 0.0, 1.0);
  609. float VdotH = clamp(dot(v, h), 0.0, 1.0);
  610. PBRLightInfo pbrLight = PBRLightInfo(
  611. NdotL,
  612. NdotV,
  613. NdotH,
  614. LdotH,
  615. VdotH
  616. );
  617. // Calculate the shading terms for the microfacet specular shading model
  618. vec3 F = specularReflection(pbrInputs, pbrLight);
  619. float G = geometricOcclusion(pbrInputs, pbrLight);
  620. float D = microfacetDistribution(pbrInputs, pbrLight);
  621. // Calculation of analytical lighting contribution
  622. vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInputs);
  623. vec3 specContrib = F * G * D / (4.0 * NdotL * NdotV);
  624. // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)
  625. vec3 color = NdotL * lightColor * (diffuseContrib + specContrib);
  626. return color;
  627. }
  628. void main() {
  629. float perceptualRoughness = uRoughnessFactor;
  630. float metallic = uMetallicFactor;
  631. #ifdef HAS_METALROUGHNESSMAP
  632. // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.
  633. // This layout intentionally reserves the 'r' channel for (optional) occlusion map data
  634. vec4 mrSample = texture(uMetallicRoughnessSampler, FragTexcoord);
  635. perceptualRoughness = mrSample.g * perceptualRoughness;
  636. metallic = mrSample.b * metallic;
  637. #endif
  638. perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);
  639. metallic = clamp(metallic, 0.0, 1.0);
  640. // Roughness is authored as perceptual roughness; as is convention,
  641. // convert to material roughness by squaring the perceptual roughness [2].
  642. float alphaRoughness = perceptualRoughness * perceptualRoughness;
  643. // The albedo may be defined from a base texture or a flat color
  644. #ifdef HAS_BASECOLORMAP
  645. vec4 baseColor = SRGBtoLINEAR(texture(uBaseColorSampler, FragTexcoord)) * uBaseColor;
  646. #else
  647. vec4 baseColor = uBaseColor;
  648. #endif
  649. vec3 f0 = vec3(0.04);
  650. vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0);
  651. diffuseColor *= 1.0 - metallic;
  652. vec3 specularColor = mix(f0, baseColor.rgb, uMetallicFactor);
  653. // Compute reflectance.
  654. float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);
  655. // For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.
  656. // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflectance to 0%.
  657. float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);
  658. vec3 specularEnvironmentR0 = specularColor.rgb;
  659. vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
  660. PBRInfo pbrInputs = PBRInfo(
  661. perceptualRoughness,
  662. metallic,
  663. specularEnvironmentR0,
  664. specularEnvironmentR90,
  665. alphaRoughness,
  666. diffuseColor,
  667. specularColor
  668. );
  669. // vec3 normal = getNormal();
  670. vec3 color = vec3(0.0);
  671. #if AMB_LIGHTS>0
  672. // Ambient lights
  673. for (int i = 0; i < AMB_LIGHTS; i++) {
  674. color += AmbientLightColor[i] * pbrInputs.diffuseColor;
  675. }
  676. #endif
  677. #if DIR_LIGHTS>0
  678. // Directional lights
  679. for (int i = 0; i < DIR_LIGHTS; i++) {
  680. // Diffuse reflection
  681. // DirLightPosition is the direction of the current light
  682. vec3 lightDirection = normalize(DirLightPosition(i));
  683. // PBR
  684. color += pbrModel(pbrInputs, DirLightColor(i), lightDirection);
  685. }
  686. #endif
  687. #if POINT_LIGHTS>0
  688. // Point lights
  689. for (int i = 0; i < POINT_LIGHTS; i++) {
  690. // Common calculations
  691. // Calculates the direction and distance from the current vertex to this point light.
  692. vec3 lightDirection = PointLightPosition(i) - vec3(Position);
  693. float lightDistance = length(lightDirection);
  694. // Normalizes the lightDirection
  695. lightDirection = lightDirection / lightDistance;
  696. // Calculates the attenuation due to the distance of the light
  697. float attenuation = 1.0 / (1.0 + PointLightLinearDecay(i) * lightDistance +
  698. PointLightQuadraticDecay(i) * lightDistance * lightDistance);
  699. vec3 attenuatedColor = PointLightColor(i) * attenuation;
  700. // PBR
  701. color += pbrModel(pbrInputs, attenuatedColor, lightDirection);
  702. }
  703. #endif
  704. #if SPOT_LIGHTS>0
  705. for (int i = 0; i < SPOT_LIGHTS; i++) {
  706. // Calculates the direction and distance from the current vertex to this spot light.
  707. vec3 lightDirection = SpotLightPosition(i) - vec3(Position);
  708. float lightDistance = length(lightDirection);
  709. lightDirection = lightDirection / lightDistance;
  710. // Calculates the attenuation due to the distance of the light
  711. float attenuation = 1.0 / (1.0 + SpotLightLinearDecay(i) * lightDistance +
  712. SpotLightQuadraticDecay(i) * lightDistance * lightDistance);
  713. // Calculates the angle between the vertex direction and spot direction
  714. // If this angle is greater than the cutoff the spotlight will not contribute
  715. // to the final color.
  716. float angle = acos(dot(-lightDirection, SpotLightDirection(i)));
  717. float cutoff = radians(clamp(SpotLightCutoffAngle(i), 0.0, 90.0));
  718. if (angle < cutoff) {
  719. float spotFactor = pow(dot(-lightDirection, SpotLightDirection(i)), SpotLightAngularDecay(i));
  720. vec3 attenuatedColor = SpotLightColor(i) * attenuation * spotFactor;
  721. // PBR
  722. color += pbrModel(pbrInputs, attenuatedColor, lightDirection);
  723. }
  724. }
  725. #endif
  726. // Calculate lighting contribution from image based lighting source (IBL)
  727. //#ifdef USE_IBL
  728. // color += getIBLContribution(pbrInputs, n, reflection);
  729. //#endif
  730. // Apply optional PBR terms for additional (optional) shading
  731. #ifdef HAS_OCCLUSIONMAP
  732. float ao = texture(uOcclusionSampler, FragTexcoord).r;
  733. color = mix(color, color * ao, 1.0);//, uOcclusionStrength);
  734. #endif
  735. #ifdef HAS_EMISSIVEMAP
  736. vec3 emissive = SRGBtoLINEAR(texture(uEmissiveSampler, FragTexcoord)).rgb * vec3(uEmissiveColor);
  737. #else
  738. vec3 emissive = vec3(uEmissiveColor);
  739. #endif
  740. color += emissive;
  741. // Base Color
  742. // FragColor = baseColor;
  743. // Normal
  744. // FragColor = vec4(n, 1.0);
  745. // Emissive Color
  746. // FragColor = vec4(emissive, 1.0);
  747. // F
  748. // color = F;
  749. // G
  750. // color = vec3(G);
  751. // D
  752. // color = vec3(D);
  753. // Specular
  754. // color = specContrib;
  755. // Diffuse
  756. // color = diffuseContrib;
  757. // Roughness
  758. // color = vec3(perceptualRoughness);
  759. // Metallic
  760. // color = vec3(metallic);
  761. // Final fragment color
  762. FragColor = vec4(pow(color,vec3(1.0/2.2)), baseColor.a);
  763. }
  764. `
  765. const physical_vertex_source = `//
  766. // Physically Based Shading of a microfacet surface material - Vertex Shader
  767. // Modified from reference implementation at https://github.com/KhronosGroup/glTF-WebGL-PBR
  768. //
  769. #include <attributes>
  770. // Model uniforms
  771. uniform mat4 ModelViewMatrix;
  772. uniform mat3 NormalMatrix;
  773. uniform mat4 MVP;
  774. // Output variables for Fragment shader
  775. out vec3 Position;
  776. out vec3 Normal;
  777. out vec3 CamDir;
  778. out vec2 FragTexcoord;
  779. void main() {
  780. // Transform this vertex position to camera coordinates.
  781. Position = vec3(ModelViewMatrix * vec4(VertexPosition, 1.0));
  782. // Transform this vertex normal to camera coordinates.
  783. Normal = normalize(NormalMatrix * VertexNormal);
  784. // Calculate the direction vector from the vertex to the camera
  785. // The camera is at 0,0,0
  786. CamDir = normalize(-Position.xyz);
  787. // Flips texture coordinate Y if requested.
  788. vec2 texcoord = VertexTexcoord;
  789. // #if MAT_TEXTURES>0
  790. // if (MatTexFlipY(0)) {
  791. // texcoord.y = 1 - texcoord.y;
  792. // }
  793. // #endif
  794. FragTexcoord = texcoord;
  795. gl_Position = MVP * vec4(VertexPosition, 1.0);
  796. }
  797. `
  798. const point_fragment_source = `#include <material>
  799. // GLSL 3.30 does not allow indexing texture sampler with non constant values.
  800. // This macro is used to mix the texture with the specified index with the material color.
  801. // It should be called for each texture index.
  802. #define MIX_POINT_TEXTURE(i) \
  803. if (MatTexVisible(i)) { \
  804. vec2 pt = gl_PointCoord - vec2(0.5); \
  805. vec4 texColor = texture(MatTexture[i], (Rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i)); \
  806. if (i == 0) { \
  807. texMixed = texColor; \
  808. } else { \
  809. texMixed = mix(texMixed, texColor, texColor.a); \
  810. } \
  811. }
  812. // Inputs from vertex shader
  813. in vec3 Color;
  814. flat in mat2 Rotation;
  815. // Output
  816. out vec4 FragColor;
  817. void main() {
  818. // Mix material color with textures colors
  819. vec4 texMixed = vec4(1);
  820. #if MAT_TEXTURES==1
  821. MIX_POINT_TEXTURE(0)
  822. #elif MAT_TEXTURES==2
  823. MIX_POINT_TEXTURE(0)
  824. MIX_POINT_TEXTURE(1)
  825. #elif MAT_TEXTURES==3
  826. MIX_POINT_TEXTURE(0)
  827. MIX_POINT_TEXTURE(1)
  828. MIX_POINT_TEXTURE(2)
  829. #endif
  830. // Generates final color
  831. FragColor = min(vec4(Color, MatOpacity) * texMixed, vec4(1));
  832. }
  833. `
  834. const point_vertex_source = `#include <attributes>
  835. // Model uniforms
  836. uniform mat4 MVP;
  837. // Material uniforms
  838. #include <material>
  839. // Outputs for fragment shader
  840. out vec3 Color;
  841. flat out mat2 Rotation;
  842. void main() {
  843. // Rotation matrix for fragment shader
  844. float rotSin = sin(MatPointRotationZ);
  845. float rotCos = cos(MatPointRotationZ);
  846. Rotation = mat2(rotCos, rotSin, - rotSin, rotCos);
  847. // Sets the vertex position
  848. vec4 pos = MVP * vec4(VertexPosition, 1.0);
  849. gl_Position = pos;
  850. // Sets the size of the rasterized point decreasing with distance
  851. gl_PointSize = (1.0 - pos.z / pos.w) * MatPointSize;
  852. // Outputs color
  853. Color = MatEmissiveColor;
  854. }
  855. `
  856. const sprite_fragment_source = `//
  857. // Fragment shader for sprite
  858. //
  859. #include <material>
  860. // Inputs from vertex shader
  861. in vec3 Color;
  862. in vec2 FragTexcoord;
  863. // Output
  864. out vec4 FragColor;
  865. void main() {
  866. // Combine all texture colors and opacity
  867. vec4 texCombined = vec4(1);
  868. #if MAT_TEXTURES>0
  869. for (int i = 0; i < {{.MatTexturesMax}}; i++) {
  870. vec4 texcolor = texture(MatTexture[i], FragTexcoord * MatTexRepeat(i) + MatTexOffset(i));
  871. if (i == 0) {
  872. texCombined = texcolor;
  873. } else {
  874. texCombined = mix(texCombined, texcolor, texcolor.a);
  875. }
  876. }
  877. #endif
  878. // Combine material color with texture
  879. FragColor = min(vec4(Color, MatOpacity) * texCombined, vec4(1));
  880. }
  881. `
  882. const sprite_vertex_source = `//
  883. // Vertex shader for sprites
  884. //
  885. #include <attributes>
  886. // Input uniforms
  887. uniform mat4 MVP;
  888. #include <material>
  889. // Outputs for fragment shader
  890. out vec3 Color;
  891. out vec2 FragTexcoord;
  892. void main() {
  893. // Applies transformation to vertex position
  894. gl_Position = MVP * vec4(VertexPosition, 1.0);
  895. // Outputs color
  896. Color = MatDiffuseColor;
  897. // Flips texture coordinate Y if requested.
  898. vec2 texcoord = VertexTexcoord;
  899. #if MAT_TEXTURES>0
  900. if (MatTexFlipY[0]) {
  901. texcoord.y = 1 - texcoord.y;
  902. }
  903. #endif
  904. FragTexcoord = texcoord;
  905. }
  906. `
  907. const standard_fragment_source = `//
  908. // Fragment Shader template
  909. //
  910. #include <material>
  911. // Inputs from Vertex shader
  912. in vec3 ColorFrontAmbdiff;
  913. in vec3 ColorFrontSpec;
  914. in vec3 ColorBackAmbdiff;
  915. in vec3 ColorBackSpec;
  916. in vec2 FragTexcoord;
  917. // Output
  918. out vec4 FragColor;
  919. void main() {
  920. // Mix material color with textures colors
  921. vec4 texMixed = vec4(1);
  922. vec4 texColor;
  923. #if MAT_TEXTURES==1
  924. MIX_TEXTURE(0)
  925. #elif MAT_TEXTURES==2
  926. MIX_TEXTURE(0)
  927. MIX_TEXTURE(1)
  928. #elif MAT_TEXTURES==3
  929. MIX_TEXTURE(0)
  930. MIX_TEXTURE(1)
  931. MIX_TEXTURE(2)
  932. #endif
  933. vec4 colorAmbDiff;
  934. vec4 colorSpec;
  935. if (gl_FrontFacing) {
  936. colorAmbDiff = vec4(ColorFrontAmbdiff, MatOpacity);
  937. colorSpec = vec4(ColorFrontSpec, 0);
  938. } else {
  939. colorAmbDiff = vec4(ColorBackAmbdiff, MatOpacity);
  940. colorSpec = vec4(ColorBackSpec, 0);
  941. }
  942. FragColor = min(colorAmbDiff * texMixed + colorSpec, vec4(1));
  943. }
  944. `
  945. const standard_vertex_source = `//
  946. // Vertex shader standard
  947. //
  948. #include <attributes>
  949. // Model uniforms
  950. uniform mat4 ModelViewMatrix;
  951. uniform mat3 NormalMatrix;
  952. uniform mat4 MVP;
  953. #include <lights>
  954. #include <material>
  955. #include <phong_model>
  956. // Outputs for the fragment shader.
  957. out vec3 ColorFrontAmbdiff;
  958. out vec3 ColorFrontSpec;
  959. out vec3 ColorBackAmbdiff;
  960. out vec3 ColorBackSpec;
  961. out vec2 FragTexcoord;
  962. void main() {
  963. // Transform this vertex normal to camera coordinates.
  964. vec3 normal = normalize(NormalMatrix * VertexNormal);
  965. // Calculate this vertex position in camera coordinates
  966. vec4 position = ModelViewMatrix * vec4(VertexPosition, 1.0);
  967. // Calculate the direction vector from the vertex to the camera
  968. // The camera is at 0,0,0
  969. vec3 camDir = normalize(-position.xyz);
  970. // Calculates the vertex Ambient+Diffuse and Specular colors using the Phong model
  971. // for the front and back
  972. phongModel(position, normal, camDir, MatAmbientColor, MatDiffuseColor, ColorFrontAmbdiff, ColorFrontSpec);
  973. phongModel(position, -normal, camDir, MatAmbientColor, MatDiffuseColor, ColorBackAmbdiff, ColorBackSpec);
  974. vec2 texcoord = VertexTexcoord;
  975. #if MAT_TEXTURES > 0
  976. // Flips texture coordinate Y if requested.
  977. if (MatTexFlipY(0)) {
  978. texcoord.y = 1 - texcoord.y;
  979. }
  980. #endif
  981. FragTexcoord = texcoord;
  982. gl_Position = MVP * vec4(VertexPosition, 1.0);
  983. }
  984. `
  985. // Maps include name with its source code
  986. var includeMap = map[string]string{
  987. "attributes": include_attributes_source,
  988. "lights": include_lights_source,
  989. "material": include_material_source,
  990. "phong_model": include_phong_model_source,
  991. }
  992. // Maps shader name with its source code
  993. var shaderMap = map[string]string{
  994. "basic_fragment": basic_fragment_source,
  995. "basic_vertex": basic_vertex_source,
  996. "panel_fragment": panel_fragment_source,
  997. "panel_vertex": panel_vertex_source,
  998. "phong_fragment": phong_fragment_source,
  999. "phong_vertex": phong_vertex_source,
  1000. "physical_fragment": physical_fragment_source,
  1001. "physical_vertex": physical_vertex_source,
  1002. "point_fragment": point_fragment_source,
  1003. "point_vertex": point_vertex_source,
  1004. "sprite_fragment": sprite_fragment_source,
  1005. "sprite_vertex": sprite_vertex_source,
  1006. "standard_fragment": standard_fragment_source,
  1007. "standard_vertex": standard_vertex_source,
  1008. }
  1009. // Maps program name with Proginfo struct with shaders names
  1010. var programMap = map[string]ProgramInfo{
  1011. "basic": {"basic_vertex", "basic_fragment", ""},
  1012. "panel": {"panel_vertex", "panel_fragment", ""},
  1013. "phong": {"phong_vertex", "phong_fragment", ""},
  1014. "physical": {"physical_vertex", "physical_fragment", ""},
  1015. "point": {"point_vertex", "point_fragment", ""},
  1016. "sprite": {"sprite_vertex", "sprite_fragment", ""},
  1017. "standard": {"standard_vertex", "standard_fragment", ""},
  1018. }