|
28 | 28 | <script src="../src/test/js/show-demo-source.js"></script>
|
29 | 29 | <script>
|
30 | 30 | // -- https://xemantic.github.io/shader-web-background/
|
31 |
| -const shaderWebBackground={};(()=>{'use strict';const p=(a,b)=>{b.initHalfFloatRGBATexture(b.width,b.height);a.texParameteri(a.TEXTURE_2D, |
| 31 | +const shaderWebBackground={};(()=>{'use strict';const r=(a,b)=>{b.initHalfFloatRGBATexture(b.width,b.height);a.texParameteri(a.TEXTURE_2D, |
32 | 32 | a.TEXTURE_MIN_FILTER,a.LINEAR);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MAG_FILTER,
|
33 | 33 | a.LINEAR);a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_S,a.CLAMP_TO_EDGE);a.texParameteri(a.TEXTURE_2D,
|
34 |
| -a.TEXTURE_WRAP_T,a.CLAMP_TO_EDGE)},u=(a,b)=>{document.documentElement.classList.add("shader-web-background-fallback"); |
35 |
| -a.classList.add("fallback");if(b instanceof shaderWebBackground.GlError)console.log("Could not shade, adding fallback CSS classes:", |
| 34 | +a.TEXTURE_WRAP_T,a.CLAMP_TO_EDGE)},x=(a,b)=>{console.warn("shader-web-background cannot shade, adding fallback CSS classes"); |
| 35 | +document.documentElement.classList.add("shader-web-background-fallback");a.classList.add("fallback"); |
| 36 | +if(b instanceof shaderWebBackground.GlError)console.warn("Not sufficient WebGL support:", |
36 | 37 | b);else throw b;};
|
37 |
| -function v(a,b){if(!a)throw new shaderWebBackground.ConfigError(b);} |
38 |
| -function w(a){v(a instanceof HTMLCanvasElement,"config.canvas must be instance of canvas"); |
| 38 | +function y(a,b){if(!a)throw new shaderWebBackground.ConfigError(b);} |
| 39 | +function z(a){y(a instanceof HTMLCanvasElement,"config.canvas must be instance of canvas"); |
39 | 40 | return a}
|
40 |
| -function x(){const a=document.createElement("canvas"),b=a.style;a.id="shader-web-background";b.width= |
| 41 | +function A(){const a=document.createElement("canvas"),b=a.style;a.id="shader-web-background";b.width= |
41 | 42 | "100vw";b.height="100vh";b.position="fixed";b.top="0";b.left="0";b.zIndex=-9999;return a}
|
42 |
| -const z=a=>'<script type="x-shader/x-fragment" id="'+a+'">'; |
43 |
| -function A(a){const b=document.getElementById(a);v(b,"Missing shader source: "+z(a)); |
44 |
| -v(b instanceof HTMLScriptElement&&"x-shader/x-fragment"===b.type,'Shader source element of id "'+ |
45 |
| -a+'" should be of type: '+z(a));return b.text} |
46 |
| -function B(a,b){"loading"!==document.readyState?b():window.addEventListener(a,b)} |
47 |
| -class C{constructor(a,b,c,h){this.g=c;const d=a.g;this.h=()=>{for(const f of h)f.v(d, |
48 |
| -f.location,b)};this.j=()=>{var f=c.A,e=a.g;e.bindBuffer(e.ARRAY_BUFFER,a.o);e.enableVertexAttribArray(f); |
49 |
| -e.vertexAttribPointer(f,2,e.FLOAT,!1,0,0);e.drawArrays(e.TRIANGLE_STRIP,0,4);e.disableVertexAttribArray(f); |
50 |
| -e.bindBuffer(e.ARRAY_BUFFER,null);f=a.g;for(e=0;e<a.h;e++)f.activeTexture(f.TEXTURE0+ |
51 |
| -e),f.bindTexture(f.TEXTURE_2D,null);a.h=0}}} |
52 |
| -function D(a,b,c,h,d,f){const e=new E(a,g=>new shaderWebBackground.GlError(g)),K= |
53 |
| -e.g,t=[],k={canvas:a,width:0,height:0,cssPixelRatio:0,cssWidth:0,cssHeight:0,isOverShader:(g, |
54 |
| -l)=>{const m=a.getBoundingClientRect();return g>=m.left&&g<=m.right&&l>=m.top&&l<= |
55 |
| -m.bottom},getCoordinateX:g=>(g-a.getBoundingClientRect().left)*k.cssPixelRatio+.5, |
56 |
| -getCoordinateY:g=>a.height-(g-a.getBoundingClientRect().top)*k.cssPixelRatio-.5,u:()=> |
57 |
| -{k.cssWidth===a.clientWidth&&k.cssHeight===a.clientHeight||k.resize()},resize:()=> |
58 |
| -{const g=window.devicePixelRatio||1,l=a.clientWidth,m=a.clientHeight,n=Math.floor(l* |
59 |
| -g),q=Math.floor(m*g);a.width=n;a.height=q;k.width=n;k.height=q;k.cssPixelRatio=g; |
60 |
| -k.cssWidth=l;k.cssHeight=m;e.g.viewport(0,0,e.canvas.width,e.canvas.height);for(const L of t)L.g.m(n, |
61 |
| -q);h&&h(n,q,k)},texture:(g,l)=>{{const m=e.g;l=l instanceof F?l.g:l;m.activeTexture(m.TEXTURE0+ |
62 |
| -e.h);m.bindTexture(m.TEXTURE_2D,l);m.uniform1i(g,e.h++)}},buffers:{},initHalfFloatRGBATexture:(g, |
63 |
| -l)=>{e.i.h(g,l)}},M=Object.keys(b).length-1;let N=0;for(const g in b){if(N++<M){const n= |
64 |
| -b[g].texture||p;k.buffers[g]=G(e,()=>{n(K,k)})}const l=H(e,I(e,g,A(g)),k.buffers[g]), |
65 |
| -m=b[g].uniforms||{};var r=Object.keys(m);for(const n of l.s)v(m[n.name],'No configuration for uniform "'+ |
66 |
| -n.name+'" defined in shader "'+g+'"'),r=r.filter(q=>q!==n.name);0!==r.length&&console.log('Extra uniforms configured for shader "'+ |
67 |
| -g+'", which are not present in the shader code - might have been removed by GLSL compiler if not used: '+ |
68 |
| -r.join(", "));r=l.s.map(n=>({location:n.location,v:m[n.name]}));t.push(new C(e,k, |
69 |
| -l,r))}const y=()=>{k.u();d&&d(k);for(const g of t)g.g.j(g.h,g.j);f&&f();requestAnimationFrame(y)}; |
70 |
| -B("load",()=>{k.resize();c&&c(k);requestAnimationFrame(y)});return k} |
| 43 | +const B=a=>'<script type="x-shader/x-fragment" id="'+a+'">'; |
| 44 | +function D(a){const b=document.getElementById(a);y(b,"Missing shader source: "+B(a)); |
| 45 | +y(b instanceof HTMLScriptElement&&"x-shader/x-fragment"===b.type,'Shader source element of id "'+ |
| 46 | +a+'" should be of type: '+B(a));return b.text} |
| 47 | +function E(a,b){"loading"!==document.readyState?b():window.addEventListener(a,b)} |
| 48 | +class F{constructor(a,b,c,d){this.g=c;const k=a.g;this.h=()=>{for(const f of d)f.v(k, |
| 49 | +f.location,b)};this.j=()=>{var f=c.A,g=a.g;g.bindBuffer(g.ARRAY_BUFFER,a.l);g.enableVertexAttribArray(f); |
| 50 | +g.vertexAttribPointer(f,2,g.FLOAT,!1,0,0);g.drawArrays(g.TRIANGLE_STRIP,0,4);g.disableVertexAttribArray(f); |
| 51 | +g.bindBuffer(g.ARRAY_BUFFER,null);f=a.g;for(g=0;g<a.h;g++)f.activeTexture(f.TEXTURE0+ |
| 52 | +g),f.bindTexture(f.TEXTURE_2D,null);a.h=0}}} |
| 53 | +function G(a){var b={antialias:!1,depth:!1,alpha:!1};try{return new H(a,b)}catch(c){throw new shaderWebBackground.GlError(c.message); |
| 54 | +}} |
| 55 | +function I(a,b,c,d,k,f){function g(e,m){try{{var n=q;const p=n.g,v=J(n,e,p.VERTEX_SHADER,"attribute vec2 V;void main(){gl_Position=vec4(V,0,1);}"), |
| 56 | +O=J(n,e,p.FRAGMENT_SHADER,m),u=p.createProgram();p.attachShader(u,v);p.attachShader(u, |
| 57 | +O);p.linkProgram(u);var h=u}return h}catch(p){throw new shaderWebBackground.ConfigError(p.message); |
| 58 | +}}const q=G(a),P=q.g,w=[],l={canvas:a,width:0,height:0,cssPixelRatio:0,cssWidth:0, |
| 59 | +cssHeight:0,isOverShader:(e,m)=>{const n=a.getBoundingClientRect();return e>=n.left&& |
| 60 | +e<=n.right&&m>=n.top&&m<=n.bottom},getCoordinateX:e=>(e-a.getBoundingClientRect().left)* |
| 61 | +l.cssPixelRatio+.5,getCoordinateY:e=>a.height-(e-a.getBoundingClientRect().top)*l.cssPixelRatio- |
| 62 | +.5,u:()=>{l.cssWidth===a.clientWidth&&l.cssHeight===a.clientHeight||l.resize()},resize:()=> |
| 63 | +{const e=window.devicePixelRatio||1,m=a.clientWidth,n=a.clientHeight,h=Math.floor(m* |
| 64 | +e),p=Math.floor(n*e);a.width=h;a.height=p;l.width=h;l.height=p;l.cssPixelRatio=e; |
| 65 | +l.cssWidth=m;l.cssHeight=n;q.g.viewport(0,0,q.canvas.width,q.canvas.height);for(const v of w)v.g.m(h, |
| 66 | +p);d&&d(h,p,l)},texture:(e,m)=>{{var n=q;const h=n.g;m=m instanceof K?m.g:m;h.activeTexture(h.TEXTURE0+ |
| 67 | +n.h);h.bindTexture(h.TEXTURE_2D,m);h.uniform1i(e,n.h++)}},buffers:{},initHalfFloatRGBATexture:(e, |
| 68 | +m)=>{q.i.h(e,m)}},Q=Object.keys(b).length-1;let R=0;for(const e in b){if(R++<Q){const h= |
| 69 | +b[e].texture||r;l.buffers[e]=L(q,()=>{h(P,l)})}const m=M(q,g(e,D(e)),l.buffers[e]), |
| 70 | +n=b[e].uniforms||{};var t=Object.keys(n);for(const h of m.o)y(n[h.name],'No configuration for uniform "'+ |
| 71 | +h.name+'" defined in shader "'+e+'"'),t=t.filter(p=>p!==h.name);0!==t.length&&console.warn('Extra uniforms configured for shader "'+ |
| 72 | +e+'", which are not present in the shader code - might have been removed by GLSL compiler if not used: '+ |
| 73 | +t.join(", "));t=m.o.map(h=>({location:h.location,v:n[h.name]}));w.push(new F(q,l, |
| 74 | +m,t))}const C=()=>{l.u();k&&k(l);for(const e of w)e.g.j(e.h,e.j);f&&f();requestAnimationFrame(C)}; |
| 75 | +E("load",()=>{l.resize();c&&c(l);requestAnimationFrame(C)});return l} |
71 | 76 | shaderWebBackground.Error=class extends Error{constructor(a){super(a);this.name="shaderWebBackground.Error"}};
|
72 | 77 | shaderWebBackground.ConfigError=class extends shaderWebBackground.Error{constructor(a){super(a);
|
73 | 78 | this.name="shaderWebBackground.ConfigError"}};
|
74 | 79 | shaderWebBackground.GlError=class extends shaderWebBackground.Error{constructor(a){super(a);this.name=
|
75 | 80 | "shaderWebBackground.GlError"}};
|
76 |
| -shaderWebBackground.shade=function(a){v(a,"Missing config argument");const b=a.canvas? |
77 |
| -w(a.canvas):x();v(a.shaders,"No shaders specified in config");try{return a.canvas|| |
78 |
| -B("DOMContentLoaded",()=>document.body.appendChild(b)),D(b,a.shaders,a.onInit,a.onResize, |
79 |
| -a.onBeforeFrame,a.onFrameComplete)}catch(c){(a.onError||u)(b,c)}};const J=[-1,1,1,1,-1,-1,1,-1]; |
80 |
| -function O(a,b){return a.l(a.g.getExtension(b),b+" extension is not supported")} |
81 |
| -class P{constructor(a,b){this.g=a;this.l=b}h(){}} |
82 |
| -class Q extends P{constructor(a,b){super(a,b);this.i=O(this,"OES_texture_half_float"); |
83 |
| -O(this,"OES_texture_half_float_linear")}h(a,b){const c=this.g;c.texImage2D(c.TEXTURE_2D, |
| 81 | +shaderWebBackground.shade=function(a){y(a,"Missing config argument");const b=a.canvas? |
| 82 | +z(a.canvas):A();y(a.shaders,"No shaders specified in config");try{return a.canvas|| |
| 83 | +E("DOMContentLoaded",()=>document.body.appendChild(b)),I(b,a.shaders,a.onInit,a.onResize, |
| 84 | +a.onBeforeFrame,a.onFrameComplete)}catch(c){(a.onError||x)(b,c)}};const N=[-1,1,1,1,-1,-1,1,-1]; |
| 85 | +function S(a,b){return a.l(a.g.getExtension(b),b+" extension is not supported")} |
| 86 | +class T{constructor(a,b){this.g=a;this.l=b}h(){}} |
| 87 | +class U extends T{constructor(a,b){super(a,b);this.i=S(this,"OES_texture_half_float"); |
| 88 | +S(this,"OES_texture_half_float_linear")}h(a,b){const c=this.g;c.texImage2D(c.TEXTURE_2D, |
84 | 89 | 0,c.RGBA,a,b,0,c.RGBA,this.i.HALF_FLOAT_OES,null)}}
|
85 |
| -class R extends P{constructor(a,b){super(a,b);O(this,"EXT_color_buffer_float");this.g.getExtension("OES_texture_float_linear")}h(a, |
| 90 | +class V extends T{constructor(a,b){super(a,b);S(this,"EXT_color_buffer_float");this.g.getExtension("OES_texture_float_linear")}h(a, |
86 | 91 | b){const c=this.g;c.texImage2D(c.TEXTURE_2D,0,c.RGBA16F,a,b,0,c.RGBA,c.HALF_FLOAT,
|
87 |
| -null)}}function G(a,b){return new F(a.g,()=>{b(a.g)})} |
88 |
| -function H(a,b,c){const h=a.g;a=[];const d=h.getProgramParameter(b,h.ACTIVE_UNIFORMS); |
89 |
| -for(let f=0;f<d;f++){const e=h.getActiveUniform(b,f);a.push({name:e.name,location:h.getUniformLocation(b, |
90 |
| -e.name)})}return{A:h.getAttribLocation(b,"V"),s:a,m:c?(f,e)=>c.m(f,e):()=>{},j:(f, |
91 |
| -e)=>{h.useProgram(b);f();c?(f=c.g,c.g=c.h,c.h=f,c.j(e)):e()}}} |
92 |
| -function I(a,b,c){const h=a.g,d=S(a,b,h.VERTEX_SHADER,"attribute vec2 V;void main(){gl_Position=vec4(V,0,1);}"); |
93 |
| -a=S(a,b,h.FRAGMENT_SHADER,c);b=h.createProgram();h.attachShader(b,d);h.attachShader(b, |
94 |
| -a);h.linkProgram(b);return b} |
95 |
| -function T(a){a=a.split(/r?n/);const b=a.length.toString().length,c=[];a.forEach((h, |
96 |
| -d)=>{var f=c.push;d=""+(d+1);d=d.length>=b?d:" ".repeat(b-d.length)+d;f.call(c,d+ |
97 |
| -":");c.push(h);c.push("n")});return c.join("")} |
98 |
| -function S(a,b,c,h){const d=a.g;c=d.createShader(c);d.shaderSource(c,h);d.compileShader(c); |
99 |
| -if(!d.getShaderParameter(c,d.COMPILE_STATUS)){const f=String(d.getShaderInfoLog(c)); |
100 |
| -d.deleteShader(c);b="Cannot compile shader - "+b+": "+f;console.log(b);console.log(T(h)); |
101 |
| -throw a.l(b);}return c} |
102 |
| -class E{constructor(a,b){var c={antialias:!1,depth:!1,alpha:!1};this.canvas=a;this.l= |
103 |
| -b;const h=(f,e)=>{if(!f)throw b(e);return f};let d=a.getContext("webgl2",c);if(d)this.i= |
104 |
| -new R(d,h);else if(d=a.getContext("webgl",c))this.i=new Q(d,h);h(d,"webgl context not supported on supplied canvas element: "+ |
| 92 | +null)}}function L(a,b){return new K(a.g,()=>{b(a.g)})} |
| 93 | +function M(a,b,c){const d=a.g;a=[];const k=d.getProgramParameter(b,d.ACTIVE_UNIFORMS); |
| 94 | +for(let f=0;f<k;f++){const g=d.getActiveUniform(b,f);a.push({name:g.name,location:d.getUniformLocation(b, |
| 95 | +g.name)})}return{A:d.getAttribLocation(b,"V"),o:a,m:c?(f,g)=>c.m(f,g):()=>{},j:(f, |
| 96 | +g)=>{d.useProgram(b);f();c?(f=c.g,c.g=c.h,c.h=f,c.j(g)):g()}}} |
| 97 | +function W(a){a=a.split(/r?n/);const b=a.length.toString().length,c=[];a.forEach((d, |
| 98 | +k)=>{var f=c.push;k=""+(k+1);k=k.length>=b?k:" ".repeat(b-k.length)+k;f.call(c,k+ |
| 99 | +":");c.push(d);c.push("n")});return c.join("")} |
| 100 | +function J(a,b,c,d){a=a.g;c=a.createShader(c);a.shaderSource(c,d);a.compileShader(c); |
| 101 | +if(!a.getShaderParameter(c,a.COMPILE_STATUS)){const k=String(a.getShaderInfoLog(c)); |
| 102 | +a.deleteShader(c);b="Cannot compile shader - "+b+": "+k;console.log(b);console.log(W(d)); |
| 103 | +throw Error(b);}return c} |
| 104 | +class H{constructor(a,b){this.canvas=a;const c=(k,f)=>{if(!k)throw Error(f);return k}; |
| 105 | +let d=a.getContext("webgl2",b);if(d)this.i=new V(d,c);else if(d=a.getContext("webgl", |
| 106 | +b))this.i=new U(d,c);c(d,"webgl context not supported on supplied canvas element: "+ |
105 | 107 | a);this.g=d;a=d.createBuffer();d.bindBuffer(d.ARRAY_BUFFER,a);d.bufferData(d.ARRAY_BUFFER,
|
106 |
| -new Float32Array(J),d.STATIC_DRAW);d.bindBuffer(d.ARRAY_BUFFER,null);this.o=a;this.buffers= |
| 108 | +new Float32Array(N),d.STATIC_DRAW);d.bindBuffer(d.ARRAY_BUFFER,null);this.l=a;this.buffers= |
107 | 109 | {};this.h=0}}
|
108 |
| -function U(a){const b=a.i,c=b.createTexture();b.bindTexture(b.TEXTURE_2D,c);a.o(b); |
| 110 | +function X(a){const b=a.i,c=b.createTexture();b.bindTexture(b.TEXTURE_2D,c);a.s(b); |
109 | 111 | b.bindTexture(b.TEXTURE_2D,null);return c}
|
110 |
| -class F{constructor(a,b){this.l=a.createFramebuffer();this.i=a;this.o=b;this.g=this.h= |
| 112 | +class K{constructor(a,b){this.l=a.createFramebuffer();this.i=a;this.s=b;this.g=this.h= |
111 | 113 | null}m(){this.h&&this.i.deleteTexture(this.h);this.g&&this.i.deleteTexture(this.g);
|
112 |
| -this.h=U(this);this.g=U(this)}j(a){const b=this.i;b.bindFramebuffer(b.FRAMEBUFFER, |
| 114 | +this.h=X(this);this.g=X(this)}j(a){const b=this.i;b.bindFramebuffer(b.FRAMEBUFFER, |
113 | 115 | this.l);b.framebufferTexture2D(b.FRAMEBUFFER,b.COLOR_ATTACHMENT0,b.TEXTURE_2D,this.g,
|
114 | 116 | 0);a();b.framebufferTexture2D(b.FRAMEBUFFER,b.COLOR_ATTACHMENT0,b.TEXTURE_2D,null,
|
115 | 117 | 0);b.bindFramebuffer(b.FRAMEBUFFER,null)}};})()
|
|
0 commit comments