sources.go 44 KB

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