sources.go 43 KB

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