sources.go 41 KB

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