sources.go 43 KB

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