sources.go 41 KB

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