sources.go 41 KB

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