sources.go 43 KB

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