rapid-render 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -82
- package/dist/depth.d.ts +8 -0
- package/dist/index.d.ts +4 -3
- package/dist/interface.d.ts +83 -34
- package/dist/line.d.ts +8 -1
- package/dist/log.d.ts +2 -0
- package/dist/math.d.ts +218 -44
- package/dist/rapid.global.js +1 -1
- package/dist/rapid.js +2453 -1
- package/dist/rapid.umd.cjs +1 -1
- package/dist/regions/attributes.d.ts +44 -0
- package/dist/regions/graphic_region.d.ts +5 -25
- package/dist/regions/region.d.ts +12 -5
- package/dist/regions/sprite_region.d.ts +2 -26
- package/dist/render.d.ts +91 -49
- package/dist/texture.d.ts +30 -2
- package/dist/tilemap.d.ts +92 -19
- package/dist/utils.d.ts +1 -0
- package/dist/webgl/glshader.d.ts +14 -4
- package/dist/webgl/uniform.d.ts +13 -0
- package/package.json +1 -1
package/dist/rapid.umd.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const t=(t,e,r)=>{const s=t.createShader(r);if(!s)throw new Error("Unable to create webgl shader");t.shaderSource(s,e),t.compileShader(s);if(!t.getShaderParameter(s,t.COMPILE_STATUS)){const r=t.getShaderInfoLog(s);throw console.error("Shader compilation failed:",r),new Error("Unable to compile shader: "+r+e)}return s};const e=5126;class r{constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=t.BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.typedArray=new t(this.arraybuffer),t==Float32Array&&(this.uint32=new Uint32Array(this.arraybuffer))}clear(){this.usedElemNum=0}resize(t=0){if((t+=this.usedElemNum)>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum<<=1;this.setMaxSize(this.maxElemNum)}}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.typedArray=new this.arrayType(this.arraybuffer),this.uint32&&(this.uint32=new Uint32Array(this.arraybuffer)),this.typedArray.set(e)}push(t){this.typedArray[this.usedElemNum++]=t}pushUint(t){this.uint32[this.usedElemNum++]=t}pop(t){this.usedElemNum-=t}getArray(t=0,e){return null==e?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.typedArray.length}}class s extends r{constructor(t,e,r=t.ARRAY_BUFFER){super(e),this.dirty=!0,this.webglBufferSize=0,this.gl=t,this.buffer=t.createBuffer(),this.type=r}push(t){super.push(t),this.dirty=!0}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize?(t.bufferData(this.type,this.getArray(),t.STATIC_DRAW),this.webglBufferSize=this.maxElemNum):t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class i extends r{constructor(){super(Float32Array)}pushMat(){const t=this.usedElemNum-6,e=this.typedArray;this.resize(6),this.push(e[t+0]),this.push(e[t+1]),this.push(e[t+2]),this.push(e[t+3]),this.push(e[t+4]),this.push(e[t+5])}popMat(){this.pop(6)}pushIdentity(){this.resize(6),this.push(1),this.push(0),this.push(0),this.push(1),this.push(0),this.push(0)}translate(t,e){if(t instanceof h)return this.translate(t.x,t.y);const r=this.usedElemNum-6,s=this.typedArray;s[r+4]=s[r+0]*t+s[r+2]*e+s[r+4],s[r+5]=s[r+1]*t+s[r+3]*e+s[r+5]}rotate(t){const e=this.usedElemNum-6,r=this.typedArray,s=Math.cos(t),i=Math.sin(t),n=r[e+0],a=r[e+1],h=r[e+2],o=r[e+3];r[e+0]=n*s-a*i,r[e+1]=n*i+a*s,r[e+2]=h*s-o*i,r[e+3]=h*i+o*s}scale(t,e){if(t instanceof h)return this.scale(t.x,t.y);e||(e=t);const r=this.usedElemNum-6,s=this.typedArray;s[r+0]=s[r+0]*t,s[r+1]=s[r+1]*t,s[r+2]=s[r+2]*e,s[r+3]=s[r+3]*e}apply(t,e){if(t instanceof h)return new h(...this.apply(t.x,t.y));const r=this.usedElemNum-6,s=this.typedArray;return[s[r+0]*t+s[r+2]*e+s[r+4],s[r+1]*t+s[r+3]*e+s[r+5]]}getInverse(){const t=this.usedElemNum-6,e=this.typedArray,r=e[t+0],s=e[t+1],i=e[t+2],n=e[t+3],a=e[t+4],h=e[t+5],o=r*n-s*i;return new Float32Array([n/o,-s/o,-i/o,r/o,(i*h-n*a)/o,(s*a-r*h)/o])}getTransform(){const t=this.usedElemNum-6,e=this.typedArray;return new Float32Array([e[t+0],e[t+1],e[t+2],e[t+3],e[t+4],e[t+5]])}setTransform(t){const e=this.usedElemNum-6,r=this.typedArray;r[e+0]=t[0],r[e+1]=t[1],r[e+2]=t[2],r[e+3]=t[3],r[e+4]=t[4],r[e+5]=t[5]}}class n extends s{constructor(t,e,r,s){super(t,Uint16Array,t.ELEMENT_ARRAY_BUFFER),this.setMaxSize(e*s);for(let t=0;t<s;t++)this.addObject(t*r);this.bindBuffer(),this.bufferData()}addObject(t){}}class a{constructor(t,e,r,s){this._r=t,this._g=e,this._b=r,this._a=s,this.updateUint()}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0}setRGBA(t,e,r,s){this.r=t,this.g=e,this.b=r,this.a=s,this.updateUint()}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}equals(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),s=parseInt(t.slice(4,6),16);let i=255;return t.length>=8&&(i=parseInt(t.slice(6,8),16)),new a(e,r,s,i)}add(t){return new a(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new a(Math.max(this.r-t.r,0),Math.max(this.g-t.g,0),Math.max(this.b-t.b,0),Math.max(this.a-t.a,0))}}class h{constructor(t,e){this.x=void 0!==t?t:0,this.y=void 0!==e?e:0}scalarMult(t){return this.x*=t,this.y*=t,this}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}normalize(){const t=this.length();return this.x/=t,this.y/=t,this}angle(){return this.y/this.x}static Angle(t,e){return Math.atan2(e.y-t.y,e.x-t.x)}static Add(t,e){return new h(t.x+e.x,t.y+e.y)}static Sub(t,e){return new h(t.x-e.x,t.y-e.y)}static Middle(t,e){return h.Add(t,e).scalarMult(.5)}static FormArray(t){return t.map((t=>new h(t[0],t[1])))}}class o{constructor(t){this.cache=new Map,this.render=t}async textureFromUrl(t,e=!1){let r=this.cache.get(t);if(!r){const s=await this.loadImage(t);r=u.fromImageSource(this.render,s,e),this.cache.set(t,r)}return new l(r)}async loadImage(t){return new Promise((e=>{const r=new Image;r.onload=()=>{e(r)},r.src=t}))}createText(t){return new c(this.render,t)}}class u{constructor(t,e,r){this.texture=t,this.width=e,this.height=r}static fromImageSource(t,e,r=!1){return new u(function(t,e,r){const s=t.createTexture();if(t.bindTexture(t.TEXTURE_2D,s),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,r?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,r?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),!s)throw new Error("unable to create texture");return s}(t.gl,e,r),e.width,e.height)}}class l{constructor(t){this.scale=1,this.setBaseTextur(t)}setBaseTextur(t){t&&(this.base=t,this.setClipRegion(0,0,t.width,t.height))}setClipRegion(t,e,r,s){this.base&&(this.clipX=t/this.base.width,this.clipY=e/this.base.width,this.clipW=this.clipX+r/this.base.width,this.clipH=this.clipY+s/this.base.height,this.width=r*this.scale,this.height=s*this.scale)}static fromImageSource(t,e,r=!1){return new l(u.fromImageSource(t,e,r))}static fromUrl(t,e){return t.textures.textureFromUrl(e)}}class c extends l{constructor(t,e){super(),this.scale=.5,this.rapid=t,this.options=e,this.text=e.text||"",this.updateTextImage()}updateTextImage(){const t=this.createTextCanvas();this.setBaseTextur(u.fromImageSource(this.rapid,t,!0))}createTextCanvas(){const t=document.createElement("canvas"),e=t.getContext("2d");if(!e)throw new Error("Failed to get canvas context");e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";const r=this.text.split("\n");let s=0,i=0;for(const t of r){const r=e.measureText(t);s=Math.max(s,r.width),i+=this.options.fontSize||16}t.width=2*s,t.height=2*i,e.scale(2,2),e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";let n=0;for(const t of r)e.fillText(t,0,n),n+=this.options.fontSize||16;return t}setText(t){this.text!=t&&(this.text=t,this.updateTextImage())}}class d{constructor(e,r,s){this.attributeLoc={},this.uniformLoc={};const i=function(t,e){if(t.includes("%TEXTURE_NUM%")&&(t=t.replace("%TEXTURE_NUM%",e.toString())),t.includes("%GET_COLOR%")){let r="";for(let t=0;t<e;t++)r+=0==t?`if(vTextureId == ${t}.0)`:t==e-1?"else":`else if(vTextureId == ${t}.0)`,r+=`{color = texture2D(uTextures[${t}], vRegion);}`;t=t.replace("%GET_COLOR%",r)}return t}(s,e.maxTextureUnits);this.program=((e,r,s)=>{var i=e.createProgram(),n=t(e,r,35633),a=t(e,s,35632);if(!i)throw new Error("Unable to create program shader");if(e.attachShader(i,n),e.attachShader(i,a),e.linkProgram(i),!e.getProgramParameter(i,e.LINK_STATUS)){const t=e.getProgramInfoLog(i);throw new Error("Unable to link shader program: "+t)}return i})(e.gl,r,i),this.gl=e.gl,this.parseShader(r),this.parseShader(i)}setUniforms(t,e){var r;const s=this.gl;for(const i in t){const n=t[i],a=this.getUniform(i);if(n instanceof l&&(null===(r=n.base)||void 0===r?void 0:r.texture))s.activeTexture(s.TEXTURE0+e),s.bindTexture(s.TEXTURE_2D,n.base.texture),s.uniform1i(a,e),e+=1;else if("number"==typeof n)s.uniform1f(a,n);else if(Array.isArray(n))switch(n.length){case 1:Number.isInteger(n[0])?s.uniform1i(a,n[0]):s.uniform1f(a,n[0]);break;case 2:Number.isInteger(n[0])?s.uniform2iv(a,n):s.uniform2fv(a,n);break;case 3:Number.isInteger(n[0])?s.uniform3iv(a,n):s.uniform3fv(a,n);break;case 4:Number.isInteger(n[0])?s.uniform4iv(a,n):s.uniform4fv(a,n);break;case 9:s.uniformMatrix3fv(a,!1,n);break;case 16:s.uniformMatrix4fv(a,!1,n);break;default:console.error(`Unsupported uniform array length for ${i}:`,n.length)}else"boolean"==typeof n?s.uniform1i(a,n?1:0):console.error(`Unsupported uniform type for ${i}:`,typeof n)}}getUniform(t){return this.uniformLoc[t]}use(){this.gl.useProgram(this.program)}parseShader(t){const e=this.gl,r=t.match(/attribute\s+\w+\s+(\w+)/g);if(r)for(const t of r){const r=t.split(" ")[2],s=e.getAttribLocation(this.program,r);-1!=s&&(this.attributeLoc[r]=s)}const s=t.match(/uniform\s+\w+\s+(\w+)/g);if(s)for(const t of s){const r=t.split(" ")[2];this.uniformLoc[r]=e.getUniformLocation(this.program,r)}}setAttribute(t){const e=this.attributeLoc[t.name];if(void 0!==e){const r=this.gl;r.vertexAttribPointer(e,t.size,t.type,t.normalized||!1,t.stride,t.offset||0),r.enableVertexAttribArray(e)}}}class p{constructor(t,e){this.usedTextures=[],this.needBind=new Set,this.attribute=e,this.rapid=t,this.gl=t.gl,this.webglArrayBuffer=new s(t.gl,Float32Array,t.gl.ARRAY_BUFFER),this.TEXTURE_UNITS_ARRAY=Array.from({length:t.maxTextureUnits},((t,e)=>e))}addVertex(t,e,...r){const[s,i]=this.rapid.transformPoint(t,e);this.webglArrayBuffer.push(s),this.webglArrayBuffer.push(i)}useTexture(t){let e=this.usedTextures.indexOf(t);return-1===e&&(this.usedTextures.length>=this.rapid.maxTextureUnits&&this.render(),this.usedTextures.push(t),e=this.usedTextures.length-1,this.needBind.add(e)),e}enterRegion(t){this.currentShader=null!=t?t:this.defaultShader,this.currentShader.use(),this.initializeForNextRender(),this.webglArrayBuffer.bindBuffer();for(const t of this.attribute)this.currentShader.setAttribute(t);this.gl.uniformMatrix4fv(this.currentShader.uniformLoc.uProjectionMatrix,!1,this.rapid.projection)}exitRegion(){}initDefaultShader(t,e){this.webglArrayBuffer.bindBuffer(),this.defaultShader=new d(this.rapid,t,e)}render(){this.executeRender(),this.initializeForNextRender()}executeRender(){this.webglArrayBuffer.bufferData();const t=this.gl;for(const e of this.needBind)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,this.usedTextures[e]);this.needBind.clear()}initializeForNextRender(){this.webglArrayBuffer.clear(),this.usedTextures=[]}hasPendingContent(){return!1}}class f extends p{constructor(t){super(t,g),this.vertex=0,this.drawType=t.gl.TRIANGLE_FAN,this.initDefaultShader("precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec4 aColor;\r\nattribute vec2 aRegion;\r\n\r\nvarying vec4 vColor;\r\nuniform mat4 uProjectionMatrix;\r\nuniform vec4 uColor;\r\nvarying vec2 vRegion;\r\n\r\nvoid main(void) {\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n vColor = aColor;\r\n vRegion = aRegion;\r\n}\r\n","precision mediump float;\r\nvarying vec2 vRegion;\r\nvarying vec4 vColor;\r\n\r\nuniform sampler2D uTexture;\r\nuniform int uUseTexture;\r\n\r\nvoid main(void) {\r\n if(uUseTexture > 0){\r\n gl_FragColor = texture2D(uTexture, vRegion) * vColor;\r\n }else{\r\n gl_FragColor = vColor;\r\n }\r\n}\r\n")}startRender(t){this.vertex=0,this.webglArrayBuffer.clear(),t&&t.base&&(this.texture=this.useTexture(t.base.texture))}addVertex(t,e,r,s,i){this.webglArrayBuffer.resize(3),super.addVertex(t,e),this.webglArrayBuffer.pushUint(i),this.webglArrayBuffer.push(r),this.webglArrayBuffer.push(s),this.vertex+=1}executeRender(){super.executeRender();const t=this.gl;t.uniform1i(this.currentShader.uniformLoc.uUseTexture,void 0===this.texture?0:1),this.texture&&t.uniform1i(this.currentShader.uniformLoc.uTexture,this.texture),t.drawArrays(this.drawType,0,this.vertex),this.drawType=this.rapid.gl.TRIANGLE_FAN,this.vertex=0,this.texture=void 0}}const g=[{name:"aPosition",size:2,type:e,stride:20},{name:"aColor",size:4,type:5121,stride:20,offset:2*Float32Array.BYTES_PER_ELEMENT,normalized:!0},{name:"aRegion",size:2,type:e,stride:20,offset:3*Float32Array.BYTES_PER_ELEMENT}];class x extends n{constructor(t,e){super(t,6,4,e)}addObject(t){super.addObject(),this.push(t),this.push(t+3),this.push(t+2),this.push(t),this.push(t+1),this.push(t+2)}}class m extends p{constructor(t){const e=t.gl;super(t,y),this.batchSprite=0,this.initDefaultShader("precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec2 aRegion;\r\nattribute float aTextureId;\r\nattribute vec4 aColor;\r\n\r\nuniform mat4 uProjectionMatrix;\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vRegion = aRegion;\r\n vTextureId = aTextureId;\r\n vColor = aColor;\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n}","precision mediump float;\r\nuniform sampler2D uTextures[%TEXTURE_NUM%];\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n %GET_COLOR%\r\n\r\n gl_FragColor = color*vColor;\r\n}"),this.MAX_BATCH=Math.floor(16384),this.indexBuffer=new x(e,this.MAX_BATCH)}addVertex(t,e,r,s,i,n){super.addVertex(t,e),this.webglArrayBuffer.push(r),this.webglArrayBuffer.push(s),this.webglArrayBuffer.push(i),this.webglArrayBuffer.pushUint(n)}renderSprite(t,e,r,s,i,n,a,h,o,u,l){var c;l&&(null===(c=this.currentShader)||void 0===c||c.setUniforms(l,1)),this.batchSprite>=this.MAX_BATCH&&this.render(),this.batchSprite++,this.webglArrayBuffer.resize(24);const d=this.useTexture(t),p=h+e,f=o+r,g=s+n,x=i+a;this.addVertex(h,o,s,i,d,u),this.addVertex(p,o,g,i,d,u),this.addVertex(p,f,g,x,d,u),this.addVertex(h,f,s,x,d,u)}executeRender(){super.executeRender();const t=this.gl;t.drawElements(t.TRIANGLES,6*this.batchSprite,t.UNSIGNED_SHORT,0)}enterRegion(t){super.enterRegion(t),this.indexBuffer.bindBuffer(),this.gl.uniform1iv(this.currentShader.uniformLoc.uTextures,this.TEXTURE_UNITS_ARRAY)}initializeForNextRender(){super.initializeForNextRender(),this.batchSprite=0}hasPendingContent(){return this.batchSprite>0}}const y=[{name:"aPosition",size:2,type:e,stride:24},{name:"aRegion",size:2,type:e,stride:24,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:e,stride:24,offset:4*Float32Array.BYTES_PER_ELEMENT},{name:"aColor",size:4,type:5121,stride:24,offset:5*Float32Array.BYTES_PER_ELEMENT,normalized:!0}];var T,E,A,b;exports.JoinTyps=void 0,(T=exports.JoinTyps||(exports.JoinTyps={}))[T.BEVEL=0]="BEVEL",T[T.ROUND=1]="ROUND",T[T.MITER=2]="MITER",exports.CapTyps=void 0,(E=exports.CapTyps||(exports.CapTyps={}))[E.BUTT=0]="BUTT",E[E.ROUND=1]="ROUND",E[E.SQUARE=2]="SQUARE",exports.RenderStatus=void 0,(A=exports.RenderStatus||(exports.RenderStatus={}))[A.NORMAL=0]="NORMAL",A[A.MASK=1]="MASK",exports.MaskType=void 0,(b=exports.MaskType||(exports.MaskType={})).Normal="normal",b.Inverse="inverse";const R=1e-4,S=(t,e,r,s)=>{s.push(t),s.push(h.Add(t,r)),s.push(h.Add(e,r)),s.push(e),s.push(h.Add(e,r)),s.push(t)},v=(t,e,r,s,i)=>{const n=h.Sub(t,e).length();let a=Math.atan2(r.y-t.y,r.x-t.x),o=Math.atan2(e.y-t.y,e.x-t.x);const u=a;o>a?o-a>=Math.PI-R&&(o-=2*Math.PI):a-o>=Math.PI-R&&(a-=2*Math.PI);let l=o-a;if(Math.abs(l)>=Math.PI-R&&Math.abs(l)<=Math.PI+R){const e=h.Sub(t,s);0===e.x?e.y>0&&(l=-l):e.x>=-1e-4&&(l=-l)}const c=(Math.abs(l*n)/7|0)+1,d=l/c;for(let e=0;e<c;e++)i.push(new h(t.x,t.y)),i.push(new h(t.x+n*Math.cos(u+d*e),t.y+n*Math.sin(u+d*e))),i.push(new h(t.x+n*Math.cos(u+d*(1+e)),t.y+n*Math.sin(u+d*(1+e))))},w=(t,e,r,s,i,n,a)=>{const o=h.Sub(e,t).perpendicular(),u=h.Sub(r,e).perpendicular();((t,e,r)=>(e.x-t.x)*(r.y-t.y)-(r.x-t.x)*(e.y-t.y))(t,e,r)>0&&(o.invert(),u.invert()),o.normalize().scalarMult(i),u.normalize().scalarMult(i);const l=((t,e,r,s)=>{const i=e.y-t.y,n=t.x-e.x,a=s.y-r.y,o=r.x-s.x,u=i*o-a*n;if(u>-1e-4&&u<R)return null;{const e=i*t.x+n*t.y,s=a*r.x+o*r.y;return new h((o*e-n*s)/u,(i*s-a*e)/u)}})(h.Add(o,t),h.Add(o,e),h.Add(u,r),h.Add(u,e));let c=null,d=Number.MAX_VALUE;l&&(c=h.Sub(l,e),d=c.length());const p=d/i|0,f=h.Sub(t,e).length(),g=h.Sub(e,r).length();if(d>f||d>g)s.push(h.Add(t,o)),s.push(h.Sub(t,o)),s.push(h.Add(e,o)),s.push(h.Sub(t,o)),s.push(h.Add(e,o)),s.push(h.Sub(e,o)),n===exports.JoinTyps.ROUND?v(e,h.Add(e,o),h.Add(e,u),r,s):n===exports.JoinTyps.BEVEL||n===exports.JoinTyps.MITER&&p>=a?(s.push(e),s.push(h.Add(e,o)),s.push(h.Add(e,u))):n===exports.JoinTyps.MITER&&p<a&&l&&(s.push(h.Add(e,o)),s.push(e),s.push(l),s.push(h.Add(e,u)),s.push(e),s.push(l)),s.push(h.Add(r,u)),s.push(h.Sub(e,u)),s.push(h.Add(e,u)),s.push(h.Add(r,u)),s.push(h.Sub(e,u)),s.push(h.Sub(r,u));else{if(s.push(h.Add(t,o)),s.push(h.Sub(t,o)),s.push(h.Sub(e,c)),s.push(h.Add(t,o)),s.push(h.Sub(e,c)),s.push(h.Add(e,o)),n===exports.JoinTyps.ROUND){const t=h.Add(e,o),r=h.Add(e,u),i=h.Sub(e,c),n=e;s.push(t),s.push(n),s.push(i),v(n,t,r,i,s),s.push(n),s.push(r),s.push(i)}else(n===exports.JoinTyps.BEVEL||n===exports.JoinTyps.MITER&&p>=a)&&(s.push(h.Add(e,o)),s.push(h.Add(e,u)),s.push(h.Sub(e,c))),n===exports.JoinTyps.MITER&&p<a&&(s.push(l),s.push(h.Add(e,o)),s.push(h.Add(e,u)));s.push(h.Add(r,u)),s.push(h.Sub(e,c)),s.push(h.Add(e,u)),s.push(h.Add(r,u)),s.push(h.Sub(e,c)),s.push(h.Sub(r,u))}};exports.BaseTexture=u,exports.Color=a,exports.DynamicArrayBuffer=r,exports.GLShader=d,exports.MatrixStack=i,exports.Rapid=class{constructor(t){this.projectionDirty=!0,this.matrixStack=new i,this.textures=new o(this),this.devicePixelRatio=window.devicePixelRatio||1,this.defaultColor=new a(255,255,255,255),this.regions=new Map;const e=(t=>{const e={stencil:!0},r=t.getContext("webgl2",e)||t.getContext("webgl",e);if(!r)throw new Error("Unable to initialize WebGL. Your browser may not support it.");return r})(t.canvas);this.gl=e,this.canvas=t.canvas,this.maxTextureUnits=e.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.width=t.width||this.canvas.width,this.height=t.width||this.canvas.height,this.backgroundColor=t.backgroundColor||new a(255,255,255,255),this.registerBuildInRegion(),this.initWebgl(e)}initWebgl(t){this.resize(this.width,this.height),t.enable(t.BLEND),t.disable(t.DEPTH_TEST),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.enable(t.STENCIL_TEST)}registerBuildInRegion(){this.registerRegion("sprite",m),this.registerRegion("graphic",f)}registerRegion(t,e){this.regions.set(t,new e(this))}quitCurrentRegion(){this.currentRegion&&this.currentRegion.hasPendingContent()&&(this.currentRegion.render(),this.currentRegion.exitRegion())}setRegion(t,e){if(t!=this.currentRegionName||e&&e!==this.currentRegion.currentShader){const r=this.regions.get(t);this.quitCurrentRegion(),this.currentRegion=r,this.currentRegionName=t,r.enterRegion(e)}}save(){this.matrixStack.pushMat()}restore(){this.matrixStack.popMat()}startRender(t=!0){this.clear(),t&&this.matrixStack.clear(),this.matrixStack.pushIdentity(),this.currentRegion=void 0,this.currentRegionName=void 0}endRender(){var t;null===(t=this.currentRegion)||void 0===t||t.render(),this.projectionDirty=!1}renderSprite(t,e=0,r=0,s){if(t.base){if(s instanceof a)return this.renderSprite(t,e,r,{color:s});this.setRegion("sprite",null==s?void 0:s.shader),this.currentRegion.renderSprite(t.base.texture,t.width,t.height,t.clipX,t.clipY,t.clipW,t.clipH,e,r,((null==s?void 0:s.color)||this.defaultColor).uint32,null==s?void 0:s.uniforms)}}renderLine(t=0,e=0,r){const s=((t,e)=>{if(t.length<2)return[];let r=e.cap||exports.CapTyps.BUTT,s=e.join||exports.JoinTyps.BEVEL,i=(e.width||1)/2,n=e.miterLimit||10,a=[],o=[];if(2===t.length)s=exports.JoinTyps.BEVEL,w(t[0],h.Middle(t[0],t[1]),t[1],a,i,s,n);else{for(let e=0;e<t.length-1;e++)0===e?o.push(t[0]):e===t.length-2?o.push(t[t.length-1]):o.push(h.Middle(t[e],t[e+1]));for(let e=1;e<o.length;e++)w(o[e-1],t[e],o[e],a,i,s,n)}if(r===exports.CapTyps.ROUND){let e=a[0],r=a[1],s=t[1],i=a[a.length-1],n=a[a.length-3],h=t[t.length-2];v(t[0],e,r,s,a),v(t[t.length-1],i,n,h,a)}else if(r===exports.CapTyps.SQUARE){let e=a[a.length-1],r=a[a.length-3];S(a[0],a[1],h.Sub(t[0],t[1]).normalize().scalarMult(h.Sub(t[0],a[0]).length()),a),S(e,r,h.Sub(t[t.length-1],t[t.length-2]).normalize().scalarMult(h.Sub(r,t[t.length-1]).length()),a)}return a})(r.points,r);this.renderGraphic(t,e,{color:r.color,drawType:this.gl.TRIANGLES,points:s})}renderGraphic(t=0,e=0,r){if(r instanceof Array)return this.renderGraphic(t,e,{points:r});this.setRegion("graphic",r.shader);const s=this.currentRegion;s.startRender(r.texture),r.drawType&&(s.drawType=r.drawType),r.points.forEach(((i,n)=>{var a;const h=r.color instanceof Array?r.color[n]:r.color,o=null===(a=r.uv)||void 0===a?void 0:a[n];s.addVertex(i.x+t,i.y+e,null==o?void 0:o.x,null==o?void 0:o.y,(h||this.defaultColor).uint32)})),s.render()}renderRect(t,e,r,s,i=this.defaultColor){const n=[new h(0,0),new h(r,0),new h(r,s),new h(0,s)];this.renderGraphic(t,e,{points:n,color:i,drawType:this.gl.TRIANGLE_FAN})}renderCircle(t,e,r,s=this.defaultColor,i=32){const n=[];for(let t=0;t<=i;t++){const e=t/i*Math.PI*2,s=Math.cos(e)*r,a=Math.sin(e)*r;n.push(new h(s,a))}this.renderGraphic(t,e,{points:n,color:s,drawType:this.gl.TRIANGLE_FAN})}resize(t,e){this.width=t,this.height=e;const r=t*this.devicePixelRatio,s=e*this.devicePixelRatio;this.canvas.width=r,this.canvas.height=s,this.gl.viewport(0,0,r,s),this.projection=this.createOrthMatrix(0,t,e,0),this.projectionDirty=!0}clear(){const t=this.gl,e=this.backgroundColor;t.clearColor(e.r,e.g,e.b,e.a),t.clear(t.COLOR_BUFFER_BIT),this.clearMask()}createOrthMatrix(t,e,r,s){return new Float32Array([2/(e-t),0,0,0,0,2/(s-r),0,0,0,0,-1,0,-(e+t)/(e-t),-(s+r)/(s-r),0,1])}transformPoint(t,e){return this.matrixStack.apply(t,e)}startDrawMask(){const t=this.gl;this.quitCurrentRegion(),t.clearStencil(0),t.clear(t.STENCIL_BUFFER_BIT),t.stencilFunc(t.ALWAYS,1,255),t.stencilOp(t.KEEP,t.KEEP,t.REPLACE),t.colorMask(!1,!1,!1,!1)}endDrawMask(t=exports.MaskType.Normal){const e=this.gl;this.quitCurrentRegion(),this.setMaskType(t),e.stencilOp(e.KEEP,e.KEEP,e.KEEP),e.colorMask(!0,!0,!0,!0)}setMaskType(t){const e=this.gl;switch(this.quitCurrentRegion(),t){case exports.MaskType.Normal:e.stencilFunc(e.EQUAL,1,255);break;case exports.MaskType.Inverse:e.stencilFunc(e.NOTEQUAL,1,255)}}clearMask(){const t=this.gl;t.clear(t.STENCIL_BUFFER_BIT)}},exports.SCALEFACTOR=2,exports.Text=c,exports.Texture=l,exports.TextureCache=o,exports.Vec2=h,exports.WebglBufferArray=s,exports.WebglElementBufferArray=n,exports.graphicAttributes=g,exports.spriteAttributes=y;
|
|
1
|
+
"use strict";const t=(t,e,r)=>{const s=t.createShader(r);if(!s)throw new Error("Unable to create webgl shader");t.shaderSource(s,e),t.compileShader(s);if(!t.getShaderParameter(s,t.COMPILE_STATUS)){const r=t.getShaderInfoLog(s);throw console.error("Shader compilation failed:",r),new Error("Unable to compile shader: "+r+e)}return s};const e=5126;class r{constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=t.BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.typedArray=new t(this.arraybuffer),t==Float32Array&&(this.uint32=new Uint32Array(this.arraybuffer))}clear(){this.usedElemNum=0}resize(t=0){if((t+=this.usedElemNum)>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum<<=1;this.setMaxSize(this.maxElemNum)}}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.typedArray=new this.arrayType(this.arraybuffer),this.uint32&&(this.uint32=new Uint32Array(this.arraybuffer)),this.typedArray.set(e)}push(t){this.typedArray[this.usedElemNum++]=t}pushUint(t){this.uint32[this.usedElemNum++]=t}pop(t){this.usedElemNum-=t}getArray(t=0,e){return null==e?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.typedArray.length}}class s extends r{constructor(t,e,r=t.ARRAY_BUFFER){super(e),this.dirty=!0,this.webglBufferSize=0,this.gl=t,this.buffer=t.createBuffer(),this.type=r}push(t){super.push(t),this.dirty=!0}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize?(t.bufferData(this.type,this.getArray(),t.STATIC_DRAW),this.webglBufferSize=this.maxElemNum):t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class i extends r{constructor(){super(Float32Array)}pushMat(){const t=this.usedElemNum-6,e=this.typedArray;this.resize(6),this.push(e[t+0]),this.push(e[t+1]),this.push(e[t+2]),this.push(e[t+3]),this.push(e[t+4]),this.push(e[t+5])}popMat(){this.pop(6)}pushIdentity(){this.resize(6),this.push(1),this.push(0),this.push(0),this.push(1),this.push(0),this.push(0)}translate(t,e){if(t instanceof h)return this.translate(t.x,t.y);const r=this.usedElemNum-6,s=this.typedArray;s[r+4]=s[r+0]*t+s[r+2]*e+s[r+4],s[r+5]=s[r+1]*t+s[r+3]*e+s[r+5]}rotate(t){const e=this.usedElemNum-6,r=this.typedArray,s=Math.cos(t),i=Math.sin(t),n=r[e+0],a=r[e+1],h=r[e+2],o=r[e+3];r[e+0]=n*s-a*i,r[e+1]=n*i+a*s,r[e+2]=h*s-o*i,r[e+3]=h*i+o*s}scale(t,e){if(t instanceof h)return this.scale(t.x,t.y);e||(e=t);const r=this.usedElemNum-6,s=this.typedArray;s[r+0]=s[r+0]*t,s[r+1]=s[r+1]*t,s[r+2]=s[r+2]*e,s[r+3]=s[r+3]*e}apply(t,e){if(t instanceof h)return new h(...this.apply(t.x,t.y));const r=this.usedElemNum-6,s=this.typedArray;return[s[r+0]*t+s[r+2]*e+s[r+4],s[r+1]*t+s[r+3]*e+s[r+5]]}getInverse(){const t=this.usedElemNum-6,e=this.typedArray,r=e[t+0],s=e[t+1],i=e[t+2],n=e[t+3],a=e[t+4],h=e[t+5],o=r*n-s*i;return new Float32Array([n/o,-s/o,-i/o,r/o,(i*h-n*a)/o,(s*a-r*h)/o])}getTransform(){const t=this.usedElemNum-6,e=this.typedArray;return new Float32Array([e[t+0],e[t+1],e[t+2],e[t+3],e[t+4],e[t+5]])}setTransform(t){const e=this.usedElemNum-6,r=this.typedArray;r[e+0]=t[0],r[e+1]=t[1],r[e+2]=t[2],r[e+3]=t[3],r[e+4]=t[4],r[e+5]=t[5]}}class n extends s{constructor(t,e,r,s){super(t,Uint16Array,t.ELEMENT_ARRAY_BUFFER),this.setMaxSize(e*s);for(let t=0;t<s;t++)this.addObject(t*r);this.bindBuffer(),this.bufferData()}addObject(t){}}class a{constructor(t,e,r,s){this._r=t,this._g=e,this._b=r,this._a=s,this.updateUint()}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0}setRGBA(t,e,r,s){this.r=t,this.g=e,this.b=r,this.a=s,this.updateUint()}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}equals(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),s=parseInt(t.slice(4,6),16);let i=255;return t.length>=8&&(i=parseInt(t.slice(6,8),16)),new a(e,r,s,i)}add(t){return new a(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new a(Math.max(this.r-t.r,0),Math.max(this.g-t.g,0),Math.max(this.b-t.b,0),Math.max(this.a-t.a,0))}}class h{constructor(t,e){this.x=void 0!==t?t:0,this.y=void 0!==e?e:0}scalarMult(t){return this.x*=t,this.y*=t,this}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}normalize(){const t=this.length();return this.x/=t,this.y/=t,this}angle(){return this.y/this.x}static Angle(t,e){return Math.atan2(e.y-t.y,e.x-t.x)}static Add(t,e){return new h(t.x+e.x,t.y+e.y)}static Sub(t,e){return new h(t.x-e.x,t.y-e.y)}static Middle(t,e){return h.Add(t,e).scalarMult(.5)}static FromArray(t){return t.map((t=>new h(t[0],t[1])))}}class o{constructor(t){this.cache=new Map,this.render=t}async textureFromUrl(t,e=!1){let r=this.cache.get(t);if(!r){const s=await this.loadImage(t);r=u.fromImageSource(this.render,s,e),this.cache.set(t,r)}return new l(r)}async loadImage(t){return new Promise((e=>{const r=new Image;r.onload=()=>{e(r)},r.src=t}))}createText(t){return new c(this.render,t)}}class u{constructor(t,e,r){this.texture=t,this.width=e,this.height=r}static fromImageSource(t,e,r=!1){return new u(function(t,e,r){const s=t.createTexture();if(t.bindTexture(t.TEXTURE_2D,s),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MIN_FILTER,r?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_MAG_FILTER,r?t.LINEAR:t.NEAREST),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),!s)throw new Error("unable to create texture");return s}(t.gl,e,r),e.width,e.height)}}class l{constructor(t){this.scale=1,this.setBaseTextur(t)}setBaseTextur(t){t&&(this.base=t,this.setClipRegion(0,0,t.width,t.height))}setClipRegion(t,e,r,s){this.base&&(this.clipX=t/this.base.width,this.clipY=e/this.base.width,this.clipW=this.clipX+r/this.base.width,this.clipH=this.clipY+s/this.base.height,this.width=r*this.scale,this.height=s*this.scale)}static fromImageSource(t,e,r=!1){return new l(u.fromImageSource(t,e,r))}static fromUrl(t,e){return t.textures.textureFromUrl(e)}}class c extends l{constructor(t,e){super(),this.scale=.5,this.rapid=t,this.options=e,this.text=e.text||"",this.updateTextImage()}updateTextImage(){const t=this.createTextCanvas();this.setBaseTextur(u.fromImageSource(this.rapid,t,!0))}createTextCanvas(){const t=document.createElement("canvas"),e=t.getContext("2d");if(!e)throw new Error("Failed to get canvas context");e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";const r=this.text.split("\n");let s=0,i=0;for(const t of r){const r=e.measureText(t);s=Math.max(s,r.width),i+=this.options.fontSize||16}t.width=2*s,t.height=2*i,e.scale(2,2),e.font=`${this.options.fontSize||16}px ${this.options.fontFamily||"Arial"}`,e.fillStyle=this.options.color||"#000",e.textAlign=this.options.textAlign||"left",e.textBaseline=this.options.textBaseline||"top";let n=0;for(const t of r)e.fillText(t,0,n),n+=this.options.fontSize||16;return t}setText(t){this.text!=t&&(this.text=t,this.updateTextImage())}}class d{constructor(e,r,s){this.attributeLoc={},this.uniformLoc={};const i=function(t,e){if(t.includes("%TEXTURE_NUM%")&&(t=t.replace("%TEXTURE_NUM%",e.toString())),t.includes("%GET_COLOR%")){let r="";for(let t=0;t<e;t++)r+=0==t?`if(vTextureId == ${t}.0)`:t==e-1?"else":`else if(vTextureId == ${t}.0)`,r+=`{color = texture2D(uTextures[${t}], vRegion);}`;t=t.replace("%GET_COLOR%",r)}return t}(s,e.maxTextureUnits);this.program=((e,r,s)=>{var i=e.createProgram(),n=t(e,r,35633),a=t(e,s,35632);if(!i)throw new Error("Unable to create program shader");if(e.attachShader(i,n),e.attachShader(i,a),e.linkProgram(i),!e.getProgramParameter(i,e.LINK_STATUS)){const t=e.getProgramInfoLog(i);throw new Error("Unable to link shader program: "+t)}return i})(e.gl,r,i),this.gl=e.gl,this.parseShader(r),this.parseShader(i)}setUniforms(t,e){var r;const s=this.gl;for(const i in t){const n=t[i],a=this.getUniform(i);if(n instanceof l&&(null===(r=n.base)||void 0===r?void 0:r.texture))s.activeTexture(s.TEXTURE0+e),s.bindTexture(s.TEXTURE_2D,n.base.texture),s.uniform1i(a,e),e+=1;else if("number"==typeof n)s.uniform1f(a,n);else if(Array.isArray(n))switch(n.length){case 1:Number.isInteger(n[0])?s.uniform1i(a,n[0]):s.uniform1f(a,n[0]);break;case 2:Number.isInteger(n[0])?s.uniform2iv(a,n):s.uniform2fv(a,n);break;case 3:Number.isInteger(n[0])?s.uniform3iv(a,n):s.uniform3fv(a,n);break;case 4:Number.isInteger(n[0])?s.uniform4iv(a,n):s.uniform4fv(a,n);break;case 9:s.uniformMatrix3fv(a,!1,n);break;case 16:s.uniformMatrix4fv(a,!1,n);break;default:console.error(`Unsupported uniform array length for ${i}:`,n.length)}else"boolean"==typeof n?s.uniform1i(a,n?1:0):console.error(`Unsupported uniform type for ${i}:`,typeof n)}}getUniform(t){return this.uniformLoc[t]}use(){this.gl.useProgram(this.program)}parseShader(t){const e=this.gl,r=t.match(/attribute\s+\w+\s+(\w+)/g);if(r)for(const t of r){const r=t.split(" ")[2],s=e.getAttribLocation(this.program,r);-1!=s&&(this.attributeLoc[r]=s)}const s=t.match(/uniform\s+\w+\s+(\w+)/g);if(s)for(const t of s){const r=t.split(" ")[2];this.uniformLoc[r]=e.getUniformLocation(this.program,r)}}setAttribute(t){const e=this.attributeLoc[t.name];if(void 0!==e){const r=this.gl;r.vertexAttribPointer(e,t.size,t.type,t.normalized||!1,t.stride,t.offset||0),r.enableVertexAttribArray(e)}}}class p{constructor(t,e){this.usedTextures=[],this.needBind=new Set,this.attribute=e,this.rapid=t,this.gl=t.gl,this.webglArrayBuffer=new s(t.gl,Float32Array,t.gl.ARRAY_BUFFER),this.TEXTURE_UNITS_ARRAY=Array.from({length:t.maxTextureUnits},((t,e)=>e))}addVertex(t,e,...r){const[s,i]=this.rapid.transformPoint(t,e);this.webglArrayBuffer.push(s),this.webglArrayBuffer.push(i)}useTexture(t){let e=this.usedTextures.indexOf(t);return-1===e&&(this.usedTextures.length>=this.rapid.maxTextureUnits&&this.render(),this.usedTextures.push(t),e=this.usedTextures.length-1,this.needBind.add(e)),e}enterRegion(t){this.currentShader=null!=t?t:this.defaultShader,this.currentShader.use(),this.initializeForNextRender(),this.webglArrayBuffer.bindBuffer();for(const t of this.attribute)this.currentShader.setAttribute(t);this.gl.uniformMatrix4fv(this.currentShader.uniformLoc.uProjectionMatrix,!1,this.rapid.projection)}exitRegion(){}initDefaultShader(t,e){this.webglArrayBuffer.bindBuffer(),this.defaultShader=new d(this.rapid,t,e)}render(){this.executeRender(),this.initializeForNextRender()}executeRender(){this.webglArrayBuffer.bufferData();const t=this.gl;for(const e of this.needBind)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,this.usedTextures[e]);this.needBind.clear()}initializeForNextRender(){this.webglArrayBuffer.clear(),this.usedTextures=[]}hasPendingContent(){return!1}}class f extends p{constructor(t){super(t,g),this.vertex=0,this.drawType=t.gl.TRIANGLE_FAN,this.initDefaultShader("precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec4 aColor;\r\nattribute vec2 aRegion;\r\n\r\nvarying vec4 vColor;\r\nuniform mat4 uProjectionMatrix;\r\nuniform vec4 uColor;\r\nvarying vec2 vRegion;\r\n\r\nvoid main(void) {\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n vColor = aColor;\r\n vRegion = aRegion;\r\n}\r\n","precision mediump float;\r\nvarying vec2 vRegion;\r\nvarying vec4 vColor;\r\n\r\nuniform sampler2D uTexture;\r\nuniform int uUseTexture;\r\n\r\nvoid main(void) {\r\n if(uUseTexture > 0){\r\n gl_FragColor = texture2D(uTexture, vRegion) * vColor;\r\n }else{\r\n gl_FragColor = vColor;\r\n }\r\n}\r\n")}startRender(t){this.vertex=0,this.webglArrayBuffer.clear(),t&&t.base&&(this.texture=this.useTexture(t.base.texture))}addVertex(t,e,r,s,i){this.webglArrayBuffer.resize(3),super.addVertex(t,e),this.webglArrayBuffer.pushUint(i),this.webglArrayBuffer.push(r),this.webglArrayBuffer.push(s),this.vertex+=1}executeRender(){super.executeRender();const t=this.gl;t.uniform1i(this.currentShader.uniformLoc.uUseTexture,void 0===this.texture?0:1),this.texture&&t.uniform1i(this.currentShader.uniformLoc.uTexture,this.texture),t.drawArrays(this.drawType,0,this.vertex),this.drawType=this.rapid.gl.TRIANGLE_FAN,this.vertex=0,this.texture=void 0}}const g=[{name:"aPosition",size:2,type:e,stride:20},{name:"aColor",size:4,type:5121,stride:20,offset:2*Float32Array.BYTES_PER_ELEMENT,normalized:!0},{name:"aRegion",size:2,type:e,stride:20,offset:3*Float32Array.BYTES_PER_ELEMENT}];class x extends n{constructor(t,e){super(t,6,4,e)}addObject(t){super.addObject(),this.push(t),this.push(t+3),this.push(t+2),this.push(t),this.push(t+1),this.push(t+2)}}class m extends p{constructor(t){const e=t.gl;super(t,y),this.batchSprite=0,this.initDefaultShader("precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec2 aRegion;\r\nattribute float aTextureId;\r\nattribute vec4 aColor;\r\n\r\nuniform mat4 uProjectionMatrix;\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vRegion = aRegion;\r\n vTextureId = aTextureId;\r\n vColor = aColor;\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n}","precision mediump float;\r\nuniform sampler2D uTextures[%TEXTURE_NUM%];\r\n\r\nvarying vec2 vRegion;\r\nvarying float vTextureId;\r\nvarying vec4 vColor;\r\n\r\nvoid main(void) {\r\n vec4 color;\r\n %GET_COLOR%\r\n\r\n gl_FragColor = color*vColor;\r\n}"),this.MAX_BATCH=Math.floor(16384),this.indexBuffer=new x(e,this.MAX_BATCH)}addVertex(t,e,r,s,i,n){super.addVertex(t,e),this.webglArrayBuffer.push(r),this.webglArrayBuffer.push(s),this.webglArrayBuffer.push(i),this.webglArrayBuffer.pushUint(n)}renderSprite(t,e,r,s,i,n,a,h,o,u,l){var c;l&&(null===(c=this.currentShader)||void 0===c||c.setUniforms(l,1)),this.batchSprite>=this.MAX_BATCH&&this.render(),this.batchSprite++,this.webglArrayBuffer.resize(24);const d=this.useTexture(t),p=h+e,f=o+r,g=s+n,x=i+a;this.addVertex(h,o,s,i,d,u),this.addVertex(p,o,g,i,d,u),this.addVertex(p,f,g,x,d,u),this.addVertex(h,f,s,x,d,u)}executeRender(){super.executeRender();const t=this.gl;t.drawElements(t.TRIANGLES,6*this.batchSprite,t.UNSIGNED_SHORT,0)}enterRegion(t){super.enterRegion(t),this.indexBuffer.bindBuffer(),this.gl.uniform1iv(this.currentShader.uniformLoc.uTextures,this.TEXTURE_UNITS_ARRAY)}initializeForNextRender(){super.initializeForNextRender(),this.batchSprite=0}hasPendingContent(){return this.batchSprite>0}}const y=[{name:"aPosition",size:2,type:e,stride:24},{name:"aRegion",size:2,type:e,stride:24,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:e,stride:24,offset:4*Float32Array.BYTES_PER_ELEMENT},{name:"aColor",size:4,type:5121,stride:24,offset:5*Float32Array.BYTES_PER_ELEMENT,normalized:!0}];var T,E,A,b;exports.JoinTyps=void 0,(T=exports.JoinTyps||(exports.JoinTyps={}))[T.BEVEL=0]="BEVEL",T[T.ROUND=1]="ROUND",T[T.MITER=2]="MITER",exports.CapTyps=void 0,(E=exports.CapTyps||(exports.CapTyps={}))[E.BUTT=0]="BUTT",E[E.ROUND=1]="ROUND",E[E.SQUARE=2]="SQUARE",exports.RenderStatus=void 0,(A=exports.RenderStatus||(exports.RenderStatus={}))[A.NORMAL=0]="NORMAL",A[A.MASK=1]="MASK",exports.MaskType=void 0,(b=exports.MaskType||(exports.MaskType={})).Normal="normal",b.Inverse="inverse";const R=1e-4,S=(t,e,r,s)=>{s.push(t),s.push(h.Add(t,r)),s.push(h.Add(e,r)),s.push(e),s.push(h.Add(e,r)),s.push(t)},v=(t,e,r,s,i)=>{const n=h.Sub(t,e).length();let a=Math.atan2(r.y-t.y,r.x-t.x),o=Math.atan2(e.y-t.y,e.x-t.x);const u=a;o>a?o-a>=Math.PI-R&&(o-=2*Math.PI):a-o>=Math.PI-R&&(a-=2*Math.PI);let l=o-a;if(Math.abs(l)>=Math.PI-R&&Math.abs(l)<=Math.PI+R){const e=h.Sub(t,s);0===e.x?e.y>0&&(l=-l):e.x>=-1e-4&&(l=-l)}const c=(Math.abs(l*n)/7|0)+1,d=l/c;for(let e=0;e<c;e++)i.push(new h(t.x,t.y)),i.push(new h(t.x+n*Math.cos(u+d*e),t.y+n*Math.sin(u+d*e))),i.push(new h(t.x+n*Math.cos(u+d*(1+e)),t.y+n*Math.sin(u+d*(1+e))))},w=(t,e,r,s,i,n,a)=>{const o=h.Sub(e,t).perpendicular(),u=h.Sub(r,e).perpendicular();((t,e,r)=>(e.x-t.x)*(r.y-t.y)-(r.x-t.x)*(e.y-t.y))(t,e,r)>0&&(o.invert(),u.invert()),o.normalize().scalarMult(i),u.normalize().scalarMult(i);const l=((t,e,r,s)=>{const i=e.y-t.y,n=t.x-e.x,a=s.y-r.y,o=r.x-s.x,u=i*o-a*n;if(u>-1e-4&&u<R)return null;{const e=i*t.x+n*t.y,s=a*r.x+o*r.y;return new h((o*e-n*s)/u,(i*s-a*e)/u)}})(h.Add(o,t),h.Add(o,e),h.Add(u,r),h.Add(u,e));let c=null,d=Number.MAX_VALUE;l&&(c=h.Sub(l,e),d=c.length());const p=d/i|0,f=h.Sub(t,e).length(),g=h.Sub(e,r).length();if(d>f||d>g)s.push(h.Add(t,o)),s.push(h.Sub(t,o)),s.push(h.Add(e,o)),s.push(h.Sub(t,o)),s.push(h.Add(e,o)),s.push(h.Sub(e,o)),n===exports.JoinTyps.ROUND?v(e,h.Add(e,o),h.Add(e,u),r,s):n===exports.JoinTyps.BEVEL||n===exports.JoinTyps.MITER&&p>=a?(s.push(e),s.push(h.Add(e,o)),s.push(h.Add(e,u))):n===exports.JoinTyps.MITER&&p<a&&l&&(s.push(h.Add(e,o)),s.push(e),s.push(l),s.push(h.Add(e,u)),s.push(e),s.push(l)),s.push(h.Add(r,u)),s.push(h.Sub(e,u)),s.push(h.Add(e,u)),s.push(h.Add(r,u)),s.push(h.Sub(e,u)),s.push(h.Sub(r,u));else{if(s.push(h.Add(t,o)),s.push(h.Sub(t,o)),s.push(h.Sub(e,c)),s.push(h.Add(t,o)),s.push(h.Sub(e,c)),s.push(h.Add(e,o)),n===exports.JoinTyps.ROUND){const t=h.Add(e,o),r=h.Add(e,u),i=h.Sub(e,c),n=e;s.push(t),s.push(n),s.push(i),v(n,t,r,i,s),s.push(n),s.push(r),s.push(i)}else(n===exports.JoinTyps.BEVEL||n===exports.JoinTyps.MITER&&p>=a)&&(s.push(h.Add(e,o)),s.push(h.Add(e,u)),s.push(h.Sub(e,c))),n===exports.JoinTyps.MITER&&p<a&&(s.push(l),s.push(h.Add(e,o)),s.push(h.Add(e,u)));s.push(h.Add(r,u)),s.push(h.Sub(e,c)),s.push(h.Add(e,u)),s.push(h.Add(r,u)),s.push(h.Sub(e,c)),s.push(h.Sub(r,u))}};exports.BaseTexture=u,exports.Color=a,exports.DynamicArrayBuffer=r,exports.GLShader=d,exports.MatrixStack=i,exports.Rapid=class{constructor(t){this.projectionDirty=!0,this.matrixStack=new i,this.textures=new o(this),this.devicePixelRatio=window.devicePixelRatio||1,this.defaultColor=new a(255,255,255,255),this.regions=new Map;const e=(t=>{const e={stencil:!0},r=t.getContext("webgl2",e)||t.getContext("webgl",e);if(!r)throw new Error("Unable to initialize WebGL. Your browser may not support it.");return r})(t.canvas);this.gl=e,this.canvas=t.canvas,this.maxTextureUnits=e.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.width=t.width||this.canvas.width,this.height=t.width||this.canvas.height,this.backgroundColor=t.backgroundColor||new a(255,255,255,255),this.registerBuildInRegion(),this.initWebgl(e)}initWebgl(t){this.resize(this.width,this.height),t.enable(t.BLEND),t.disable(t.DEPTH_TEST),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),t.enable(t.STENCIL_TEST)}registerBuildInRegion(){this.registerRegion("sprite",m),this.registerRegion("graphic",f)}registerRegion(t,e){this.regions.set(t,new e(this))}quitCurrentRegion(){this.currentRegion&&this.currentRegion.hasPendingContent()&&(this.currentRegion.render(),this.currentRegion.exitRegion())}setRegion(t,e){if(t!=this.currentRegionName||e&&e!==this.currentRegion.currentShader){const r=this.regions.get(t);this.quitCurrentRegion(),this.currentRegion=r,this.currentRegionName=t,r.enterRegion(e)}}save(){this.matrixStack.pushMat()}restore(){this.matrixStack.popMat()}startRender(t=!0){this.clear(),t&&this.matrixStack.clear(),this.matrixStack.pushIdentity(),this.currentRegion=void 0,this.currentRegionName=void 0}endRender(){var t;null===(t=this.currentRegion)||void 0===t||t.render(),this.projectionDirty=!1}renderSprite(t,e=0,r=0,s){if(t.base){if(s instanceof a)return this.renderSprite(t,e,r,{color:s});this.setRegion("sprite",null==s?void 0:s.shader),this.currentRegion.renderSprite(t.base.texture,t.width,t.height,t.clipX,t.clipY,t.clipW,t.clipH,e,r,((null==s?void 0:s.color)||this.defaultColor).uint32,null==s?void 0:s.uniforms)}}renderLine(t=0,e=0,r){const s=((t,e)=>{if(t.length<2)return[];let r=e.cap||exports.CapTyps.BUTT,s=e.join||exports.JoinTyps.BEVEL,i=(e.width||1)/2,n=e.miterLimit||10,a=[],o=[];if(2===t.length)s=exports.JoinTyps.BEVEL,w(t[0],h.Middle(t[0],t[1]),t[1],a,i,s,n);else{for(let e=0;e<t.length-1;e++)0===e?o.push(t[0]):e===t.length-2?o.push(t[t.length-1]):o.push(h.Middle(t[e],t[e+1]));for(let e=1;e<o.length;e++)w(o[e-1],t[e],o[e],a,i,s,n)}if(r===exports.CapTyps.ROUND){let e=a[0],r=a[1],s=t[1],i=a[a.length-1],n=a[a.length-3],h=t[t.length-2];v(t[0],e,r,s,a),v(t[t.length-1],i,n,h,a)}else if(r===exports.CapTyps.SQUARE){let e=a[a.length-1],r=a[a.length-3];S(a[0],a[1],h.Sub(t[0],t[1]).normalize().scalarMult(h.Sub(t[0],a[0]).length()),a),S(e,r,h.Sub(t[t.length-1],t[t.length-2]).normalize().scalarMult(h.Sub(r,t[t.length-1]).length()),a)}return a})(r.points,r);this.renderGraphic(t,e,{color:r.color,drawType:this.gl.TRIANGLES,points:s})}renderGraphic(t=0,e=0,r){if(r instanceof Array)return this.renderGraphic(t,e,{points:r});this.setRegion("graphic",r.shader);const s=this.currentRegion;s.startRender(r.texture),r.drawType&&(s.drawType=r.drawType),r.points.forEach(((i,n)=>{var a;const h=r.color instanceof Array?r.color[n]:r.color,o=null===(a=r.uv)||void 0===a?void 0:a[n];s.addVertex(i.x+t,i.y+e,null==o?void 0:o.x,null==o?void 0:o.y,(h||this.defaultColor).uint32)})),s.render()}renderRect(t,e,r,s,i=this.defaultColor){const n=[new h(0,0),new h(r,0),new h(r,s),new h(0,s)];this.renderGraphic(t,e,{points:n,color:i,drawType:this.gl.TRIANGLE_FAN})}renderCircle(t,e,r,s=this.defaultColor,i=32){const n=[];for(let t=0;t<=i;t++){const e=t/i*Math.PI*2,s=Math.cos(e)*r,a=Math.sin(e)*r;n.push(new h(s,a))}this.renderGraphic(t,e,{points:n,color:s,drawType:this.gl.TRIANGLE_FAN})}resize(t,e){this.width=t,this.height=e;const r=t*this.devicePixelRatio,s=e*this.devicePixelRatio;this.canvas.width=r,this.canvas.height=s,this.gl.viewport(0,0,r,s),this.projection=this.createOrthMatrix(0,t,e,0),this.projectionDirty=!0}clear(){const t=this.gl,e=this.backgroundColor;t.clearColor(e.r,e.g,e.b,e.a),t.clear(t.COLOR_BUFFER_BIT),this.clearMask()}createOrthMatrix(t,e,r,s){return new Float32Array([2/(e-t),0,0,0,0,2/(s-r),0,0,0,0,-1,0,-(e+t)/(e-t),-(s+r)/(s-r),0,1])}transformPoint(t,e){return this.matrixStack.apply(t,e)}startDrawMask(){const t=this.gl;this.quitCurrentRegion(),t.clearStencil(0),t.clear(t.STENCIL_BUFFER_BIT),t.stencilFunc(t.ALWAYS,1,255),t.stencilOp(t.KEEP,t.KEEP,t.REPLACE),t.colorMask(!1,!1,!1,!1)}endDrawMask(t=exports.MaskType.Normal){const e=this.gl;this.quitCurrentRegion(),this.setMaskType(t),e.stencilOp(e.KEEP,e.KEEP,e.KEEP),e.colorMask(!0,!0,!0,!0)}setMaskType(t){const e=this.gl;switch(this.quitCurrentRegion(),t){case exports.MaskType.Normal:e.stencilFunc(e.EQUAL,1,255);break;case exports.MaskType.Inverse:e.stencilFunc(e.NOTEQUAL,1,255)}}clearMask(){const t=this.gl;t.clear(t.STENCIL_BUFFER_BIT)}},exports.SCALEFACTOR=2,exports.Text=c,exports.Texture=l,exports.TextureCache=o,exports.Vec2=h,exports.WebglBufferArray=s,exports.WebglElementBufferArray=n,exports.graphicAttributes=g,exports.spriteAttributes=y;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export declare const spriteAttributes: ({
|
|
2
|
+
name: string;
|
|
3
|
+
size: number;
|
|
4
|
+
type: number;
|
|
5
|
+
stride: number;
|
|
6
|
+
offset?: undefined;
|
|
7
|
+
normalized?: undefined;
|
|
8
|
+
} | {
|
|
9
|
+
name: string;
|
|
10
|
+
size: number;
|
|
11
|
+
type: number;
|
|
12
|
+
stride: number;
|
|
13
|
+
offset: number;
|
|
14
|
+
normalized?: undefined;
|
|
15
|
+
} | {
|
|
16
|
+
name: string;
|
|
17
|
+
size: number;
|
|
18
|
+
type: number;
|
|
19
|
+
stride: number;
|
|
20
|
+
offset: number;
|
|
21
|
+
normalized: boolean;
|
|
22
|
+
})[];
|
|
23
|
+
export declare const graphicAttributes: ({
|
|
24
|
+
name: string;
|
|
25
|
+
size: number;
|
|
26
|
+
type: number;
|
|
27
|
+
stride: number;
|
|
28
|
+
offset?: undefined;
|
|
29
|
+
normalized?: undefined;
|
|
30
|
+
} | {
|
|
31
|
+
name: string;
|
|
32
|
+
size: number;
|
|
33
|
+
type: number;
|
|
34
|
+
stride: number;
|
|
35
|
+
offset: number;
|
|
36
|
+
normalized: boolean;
|
|
37
|
+
} | {
|
|
38
|
+
name: string;
|
|
39
|
+
size: number;
|
|
40
|
+
type: number;
|
|
41
|
+
stride: number;
|
|
42
|
+
offset: number;
|
|
43
|
+
normalized?: undefined;
|
|
44
|
+
})[];
|
|
@@ -1,35 +1,15 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type Rapid from "../render";
|
|
2
|
+
import { Uniform } from "../webgl/uniform";
|
|
2
3
|
import RenderRegion from "./region";
|
|
4
|
+
import { Texture } from "../texture";
|
|
3
5
|
declare class GraphicRegion extends RenderRegion {
|
|
4
6
|
private vertex;
|
|
5
7
|
private texture?;
|
|
8
|
+
private offset;
|
|
6
9
|
drawType: number;
|
|
7
10
|
constructor(rapid: Rapid);
|
|
8
|
-
startRender(texture?: Texture): void;
|
|
11
|
+
startRender(offsetX: number, offsetY: number, texture?: Texture, uniforms?: Uniform): void;
|
|
9
12
|
addVertex(x: number, y: number, u: number, v: number, color: number): void;
|
|
10
13
|
protected executeRender(): void;
|
|
11
14
|
}
|
|
12
|
-
declare const graphicAttributes: ({
|
|
13
|
-
name: string;
|
|
14
|
-
size: number;
|
|
15
|
-
type: number;
|
|
16
|
-
stride: number;
|
|
17
|
-
offset?: undefined;
|
|
18
|
-
normalized?: undefined;
|
|
19
|
-
} | {
|
|
20
|
-
name: string;
|
|
21
|
-
size: number;
|
|
22
|
-
type: number;
|
|
23
|
-
stride: number;
|
|
24
|
-
offset: number;
|
|
25
|
-
normalized: boolean;
|
|
26
|
-
} | {
|
|
27
|
-
name: string;
|
|
28
|
-
size: number;
|
|
29
|
-
type: number;
|
|
30
|
-
stride: number;
|
|
31
|
-
offset: number;
|
|
32
|
-
normalized?: undefined;
|
|
33
|
-
})[];
|
|
34
|
-
export { graphicAttributes };
|
|
35
15
|
export default GraphicRegion;
|
package/dist/regions/region.d.ts
CHANGED
|
@@ -2,25 +2,32 @@ import { IAttribute, WebGLContext } from "../interface";
|
|
|
2
2
|
import { WebglBufferArray } from "../math";
|
|
3
3
|
import Rapid from "../render";
|
|
4
4
|
import GLShader from "../webgl/glshader";
|
|
5
|
+
import { Uniform } from "../webgl/uniform";
|
|
5
6
|
declare class RenderRegion {
|
|
6
7
|
currentShader?: GLShader;
|
|
7
|
-
protected defaultShader: GLShader;
|
|
8
|
-
protected attribute: IAttribute[];
|
|
9
8
|
protected webglArrayBuffer: WebglBufferArray;
|
|
10
9
|
protected rapid: Rapid;
|
|
11
10
|
protected gl: WebGLContext;
|
|
12
11
|
protected usedTextures: WebGLTexture[];
|
|
13
|
-
protected readonly TEXTURE_UNITS_ARRAY: number[];
|
|
14
12
|
protected needBind: Set<number>;
|
|
15
|
-
|
|
13
|
+
protected shaders: Map<string, GLShader>;
|
|
14
|
+
protected isCostumShader: boolean;
|
|
15
|
+
protected readonly MAX_TEXTURE_UNIT_ARRAY: number[];
|
|
16
|
+
private costumUnifrom?;
|
|
17
|
+
constructor(rapid: Rapid);
|
|
18
|
+
setTextureUnits(usedTexture: number): number[];
|
|
16
19
|
protected addVertex(x: number, y: number, ..._: unknown[]): void;
|
|
17
20
|
protected useTexture(texture: WebGLTexture): number;
|
|
18
21
|
enterRegion(customShader?: GLShader): void;
|
|
22
|
+
setCostumUnifrom(newCurrentUniform: Uniform): boolean;
|
|
19
23
|
exitRegion(): void;
|
|
20
|
-
protected initDefaultShader(vs: string, fs: string): void;
|
|
24
|
+
protected initDefaultShader(vs: string, fs: string, attributes?: IAttribute[]): void;
|
|
25
|
+
setShader(name: string, vs: string, fs: string, attributes?: IAttribute[]): void;
|
|
26
|
+
getShader(name: string): GLShader | undefined;
|
|
21
27
|
render(): void;
|
|
22
28
|
protected executeRender(): void;
|
|
23
29
|
protected initializeForNextRender(): void;
|
|
24
30
|
hasPendingContent(): boolean;
|
|
31
|
+
isShaderChanged(shader?: GLShader): boolean;
|
|
25
32
|
}
|
|
26
33
|
export default RenderRegion;
|
|
@@ -1,40 +1,16 @@
|
|
|
1
1
|
import RenderRegion from "./region";
|
|
2
2
|
import Rapid from "../render";
|
|
3
|
-
import { UniformType } from "../interface";
|
|
4
3
|
import GLShader from "../webgl/glshader";
|
|
4
|
+
import { Uniform } from "../webgl/uniform";
|
|
5
5
|
declare class SpriteRegion extends RenderRegion {
|
|
6
6
|
private batchSprite;
|
|
7
7
|
private indexBuffer;
|
|
8
|
-
protected readonly MAX_BATCH: number;
|
|
9
8
|
constructor(rapid: Rapid);
|
|
10
9
|
protected addVertex(x: number, y: number, u: number, v: number, textureUnit: number, color: number): void;
|
|
11
|
-
renderSprite(texture: WebGLTexture, width: number, height: number, u0: number, v0: number, u1: number, v1: number, offsetX: number, offsetY: number, color: number, uniforms?:
|
|
10
|
+
renderSprite(texture: WebGLTexture, width: number, height: number, u0: number, v0: number, u1: number, v1: number, offsetX: number, offsetY: number, color: number, uniforms?: Uniform): void;
|
|
12
11
|
protected executeRender(): void;
|
|
13
12
|
enterRegion(customShader?: GLShader | undefined): void;
|
|
14
13
|
protected initializeForNextRender(): void;
|
|
15
14
|
hasPendingContent(): boolean;
|
|
16
15
|
}
|
|
17
|
-
declare const spriteAttributes: ({
|
|
18
|
-
name: string;
|
|
19
|
-
size: number;
|
|
20
|
-
type: number;
|
|
21
|
-
stride: number;
|
|
22
|
-
offset?: undefined;
|
|
23
|
-
normalized?: undefined;
|
|
24
|
-
} | {
|
|
25
|
-
name: string;
|
|
26
|
-
size: number;
|
|
27
|
-
type: number;
|
|
28
|
-
stride: number;
|
|
29
|
-
offset: number;
|
|
30
|
-
normalized?: undefined;
|
|
31
|
-
} | {
|
|
32
|
-
name: string;
|
|
33
|
-
size: number;
|
|
34
|
-
type: number;
|
|
35
|
-
stride: number;
|
|
36
|
-
offset: number;
|
|
37
|
-
normalized: boolean;
|
|
38
|
-
})[];
|
|
39
|
-
export { spriteAttributes };
|
|
40
16
|
export default SpriteRegion;
|
package/dist/render.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { IGraphicOptions,
|
|
1
|
+
import { ICircleOptions, IGraphicOptions, ILayerRender, IRapidOptions, IRectOptions, IRenderLineOptions, IRenderSpriteOptions, ShaderType as ShaderType, MaskType, WebGLContext } from "./interface";
|
|
2
2
|
import { Color, MatrixStack, Vec2 } from "./math";
|
|
3
3
|
import RenderRegion from "./regions/region";
|
|
4
4
|
import { Texture, TextureCache } from "./texture";
|
|
5
|
+
import { TileMapRender, TileSet } from "./tilemap";
|
|
5
6
|
import GLShader from "./webgl/glshader";
|
|
6
7
|
/**
|
|
7
8
|
* The `Rapid` class provides a WebGL-based rendering engine.
|
|
@@ -15,6 +16,7 @@ declare class Rapid {
|
|
|
15
16
|
textures: TextureCache;
|
|
16
17
|
width: number;
|
|
17
18
|
height: number;
|
|
19
|
+
tileMap: TileMapRender;
|
|
18
20
|
backgroundColor: Color;
|
|
19
21
|
readonly devicePixelRatio: number;
|
|
20
22
|
readonly maxTextureUnits: number;
|
|
@@ -22,11 +24,19 @@ declare class Rapid {
|
|
|
22
24
|
private currentRegion?;
|
|
23
25
|
private currentRegionName?;
|
|
24
26
|
private regions;
|
|
27
|
+
private currentMaskType;
|
|
28
|
+
private currentTransformOptions?;
|
|
25
29
|
/**
|
|
26
30
|
* Constructs a new `Rapid` instance with the given options.
|
|
27
31
|
* @param options - Options for initializing the `Rapid` instance.
|
|
28
32
|
*/
|
|
29
|
-
constructor(options:
|
|
33
|
+
constructor(options: IRapidOptions);
|
|
34
|
+
/**
|
|
35
|
+
* Render a tile map layer.
|
|
36
|
+
* @param data - The map data to render.
|
|
37
|
+
* @param options - The options for rendering the tile map layer.
|
|
38
|
+
*/
|
|
39
|
+
renderTileMapLayer(data: (number | string)[][], options: ILayerRender | TileSet): void;
|
|
30
40
|
/**
|
|
31
41
|
* Initializes WebGL context settings.
|
|
32
42
|
* @param gl - The WebGL context.
|
|
@@ -47,6 +57,7 @@ declare class Rapid {
|
|
|
47
57
|
* Sets the current render region by name and optionally a custom shader.
|
|
48
58
|
* @param regionName - The name of the region to set as current.
|
|
49
59
|
* @param customShader - An optional custom shader to use with the region.
|
|
60
|
+
* @param hasUnifrom - have costum unifrom
|
|
50
61
|
*/
|
|
51
62
|
setRegion(regionName: string, customShader?: GLShader): void;
|
|
52
63
|
/**
|
|
@@ -57,6 +68,11 @@ declare class Rapid {
|
|
|
57
68
|
* Restores the matrix state from the stack.
|
|
58
69
|
*/
|
|
59
70
|
restore(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Executes a callback function within a saved and restored matrix state scope.
|
|
73
|
+
* @param cb - The callback function to execute within the saved and restored matrix state scope.
|
|
74
|
+
*/
|
|
75
|
+
withTransform(cb: () => void): void;
|
|
60
76
|
/**
|
|
61
77
|
* Starts the rendering process, resetting the matrix stack and clearing the current region.
|
|
62
78
|
* @param clear - Whether to clear the matrix stack. Defaults to true.
|
|
@@ -67,57 +83,74 @@ declare class Rapid {
|
|
|
67
83
|
*/
|
|
68
84
|
endRender(): void;
|
|
69
85
|
/**
|
|
70
|
-
*
|
|
71
|
-
* @param
|
|
72
|
-
* @param offsetX - The X offset for the sprite. Defaults to 0.
|
|
73
|
-
* @param offsetY - The Y offset for the sprite. Defaults to 0.
|
|
74
|
-
* @param options - Rendering options including color and custom shader.
|
|
86
|
+
* Render
|
|
87
|
+
* @param cb - The function to render.
|
|
75
88
|
*/
|
|
76
|
-
|
|
89
|
+
render(cb: () => void): void;
|
|
90
|
+
/**
|
|
91
|
+
* Renders a sprite with the specified options.
|
|
92
|
+
*
|
|
93
|
+
* @param options - The rendering options for the sprite, including texture, position, color, and shader.
|
|
94
|
+
*/
|
|
95
|
+
renderSprite(options: IRenderSpriteOptions): void;
|
|
96
|
+
/**
|
|
97
|
+
* Renders a texture directly without additional options.
|
|
98
|
+
* This is a convenience method that calls renderSprite with just the texture.
|
|
99
|
+
*
|
|
100
|
+
* @param texture - The texture to render at the current transformation position.
|
|
101
|
+
*/
|
|
102
|
+
renderTexture(texture: Texture): void;
|
|
77
103
|
/**
|
|
78
104
|
* Renders a line with the specified options.
|
|
79
105
|
*
|
|
80
|
-
* @param
|
|
81
|
-
* @param offsetY - The Y offset to apply when rendering the line. Defaults to 0.
|
|
82
|
-
* @param options - The options for rendering the line, including points and color.
|
|
106
|
+
* @param options - The options for rendering the line, including points, color, width, and join/cap types.
|
|
83
107
|
*/
|
|
84
|
-
renderLine(
|
|
108
|
+
renderLine(options: IRenderLineOptions): void;
|
|
85
109
|
/**
|
|
86
|
-
* Renders graphics based on the provided options
|
|
110
|
+
* Renders graphics based on the provided options.
|
|
87
111
|
*
|
|
88
|
-
* @param
|
|
89
|
-
|
|
90
|
-
|
|
112
|
+
* @param options - The options for rendering the graphic, including points, color, texture, and draw type.
|
|
113
|
+
*/
|
|
114
|
+
renderGraphic(options: IGraphicOptions): void;
|
|
115
|
+
/**
|
|
116
|
+
* Starts the graphic drawing process.
|
|
91
117
|
*
|
|
92
|
-
* @
|
|
93
|
-
|
|
94
|
-
|
|
118
|
+
* @param options - The options for the graphic drawing, including shader, texture, and draw type.
|
|
119
|
+
*/
|
|
120
|
+
startGraphicDraw(options: IGraphicOptions): void;
|
|
121
|
+
/**
|
|
122
|
+
* Adds a vertex to the current graphic being drawn.
|
|
123
|
+
*
|
|
124
|
+
* @param offsetX - The X coordinate of the vertex.
|
|
125
|
+
* @param offsetY - The Y coordinate of the vertex.
|
|
126
|
+
* @param uv - The texture UV coordinates for the vertex.
|
|
127
|
+
* @param color - The color of the vertex. Defaults to the renderer's default color.
|
|
128
|
+
*/
|
|
129
|
+
addGraphicVertex(offsetX: number, offsetY: number, uv: Vec2, color?: Color): void;
|
|
130
|
+
/**
|
|
131
|
+
* Completes the graphic drawing process and renders the result.
|
|
95
132
|
*/
|
|
96
|
-
|
|
133
|
+
endGraphicDraw(): void;
|
|
134
|
+
private startDraw;
|
|
135
|
+
private afterDraw;
|
|
97
136
|
/**
|
|
98
|
-
* Renders a rectangle
|
|
99
|
-
*
|
|
100
|
-
* @param
|
|
101
|
-
* @param width - The width of the rectangle
|
|
102
|
-
* @param height - The height of the rectangle
|
|
103
|
-
* @param color - The color of the rectangle
|
|
137
|
+
* Renders a rectangle with the specified options.
|
|
138
|
+
*
|
|
139
|
+
* @param options - The options for rendering the rectangle, including width, height, position, and color.
|
|
104
140
|
*/
|
|
105
|
-
renderRect(
|
|
141
|
+
renderRect(options: IRectOptions): void;
|
|
106
142
|
/**
|
|
107
|
-
* Renders a circle
|
|
108
|
-
*
|
|
109
|
-
* @param
|
|
110
|
-
* @param radius - The radius of the circle
|
|
111
|
-
* @param color - The color of the circle
|
|
112
|
-
* @param segments - The number of segments to use when rendering the circle, default is 32
|
|
143
|
+
* Renders a circle with the specified options.
|
|
144
|
+
*
|
|
145
|
+
* @param options - The options for rendering the circle, including radius, position, color, and segment count.
|
|
113
146
|
*/
|
|
114
|
-
renderCircle(
|
|
147
|
+
renderCircle(options: ICircleOptions): void;
|
|
115
148
|
/**
|
|
116
149
|
* Resizes the canvas and updates the viewport and projection matrix.
|
|
117
150
|
* @param width - The new width of the canvas.
|
|
118
151
|
* @param height - The new height of the canvas.
|
|
119
152
|
*/
|
|
120
|
-
resize(
|
|
153
|
+
resize(logicalWidth: number, logicalHeight: number): void;
|
|
121
154
|
/**
|
|
122
155
|
* Clears the canvas with the background color.
|
|
123
156
|
*/
|
|
@@ -132,31 +165,40 @@ declare class Rapid {
|
|
|
132
165
|
*/
|
|
133
166
|
private createOrthMatrix;
|
|
134
167
|
/**
|
|
135
|
-
*
|
|
136
|
-
* @param
|
|
137
|
-
* @param
|
|
138
|
-
* @returns The transformed point as an array `[newX, newY]`.
|
|
168
|
+
* Draw a mask. Automatically calls startDrawMask.
|
|
169
|
+
* @param type - The type of mask to draw.
|
|
170
|
+
* @param cb - The callback function to execute.
|
|
139
171
|
*/
|
|
140
|
-
|
|
172
|
+
drawMask(type: MaskType | undefined, cb: () => void): void;
|
|
141
173
|
/**
|
|
142
|
-
*
|
|
143
|
-
* This method
|
|
174
|
+
* Start drawing a mask using the stencil buffer.
|
|
175
|
+
* This method configures the WebGL context to begin defining a mask area.
|
|
144
176
|
*/
|
|
145
|
-
startDrawMask(): void;
|
|
177
|
+
startDrawMask(type?: MaskType): void;
|
|
146
178
|
/**
|
|
147
|
-
*
|
|
179
|
+
* End the mask drawing process.
|
|
148
180
|
* This method configures the WebGL context to use the defined mask for subsequent rendering.
|
|
149
181
|
*/
|
|
150
|
-
endDrawMask(
|
|
182
|
+
endDrawMask(): void;
|
|
151
183
|
/**
|
|
152
|
-
*
|
|
153
|
-
* @param type - The type
|
|
184
|
+
* Set the mask type for rendering
|
|
185
|
+
* @param type - The mask type to apply
|
|
186
|
+
* @param start - Whether this is the start of mask drawing
|
|
154
187
|
*/
|
|
155
|
-
setMaskType
|
|
188
|
+
private setMaskType;
|
|
156
189
|
/**
|
|
157
|
-
*
|
|
190
|
+
* Clear the current mask by clearing the stencil buffer.
|
|
158
191
|
* This effectively removes any previously defined mask.
|
|
159
192
|
*/
|
|
160
193
|
clearMask(): void;
|
|
194
|
+
/**
|
|
195
|
+
* Creates a custom shader.
|
|
196
|
+
* @param vs - Vertex shader code.
|
|
197
|
+
* @param fs - Fragment shader code.
|
|
198
|
+
* @param type - Shader type.
|
|
199
|
+
* @param textureUnit - The number of textures used by the shader
|
|
200
|
+
* @returns The created shader object.
|
|
201
|
+
*/
|
|
202
|
+
createCostumShader(vs: string, fs: string, type: ShaderType, textureUnit?: number): GLShader;
|
|
161
203
|
}
|
|
162
204
|
export default Rapid;
|
package/dist/texture.d.ts
CHANGED
|
@@ -2,11 +2,13 @@ import Rapid from "./render";
|
|
|
2
2
|
import { Images, ITextOptions } from "./interface";
|
|
3
3
|
/**
|
|
4
4
|
* texture manager
|
|
5
|
+
* @ignore
|
|
5
6
|
*/
|
|
6
7
|
declare class TextureCache {
|
|
7
8
|
private render;
|
|
8
9
|
private cache;
|
|
9
|
-
|
|
10
|
+
private antialias;
|
|
11
|
+
constructor(render: Rapid, antialias: boolean);
|
|
10
12
|
/**
|
|
11
13
|
* create texture from url
|
|
12
14
|
* Equivalent to {@link Texture.fromUrl} method
|
|
@@ -15,7 +17,13 @@ declare class TextureCache {
|
|
|
15
17
|
* @returns
|
|
16
18
|
*/
|
|
17
19
|
textureFromUrl(url: string, antialias?: boolean): Promise<Texture>;
|
|
20
|
+
textureFromSource(source: Images, antialias?: boolean): Promise<Texture>;
|
|
18
21
|
loadImage(url: string): Promise<HTMLImageElement>;
|
|
22
|
+
/**
|
|
23
|
+
* Create a new `Text` instance.
|
|
24
|
+
* @param options - The options for rendering the text, such as font, size, color, etc.
|
|
25
|
+
* @returns A new `Text` instance.
|
|
26
|
+
*/
|
|
19
27
|
createText(options: ITextOptions): Text;
|
|
20
28
|
}
|
|
21
29
|
/**
|
|
@@ -79,7 +87,7 @@ declare class Texture {
|
|
|
79
87
|
* @param w - The width of the region.
|
|
80
88
|
* @param h - The height of the region.
|
|
81
89
|
*/
|
|
82
|
-
setClipRegion(x: number, y: number, w: number, h: number):
|
|
90
|
+
setClipRegion(x: number, y: number, w: number, h: number): this | undefined;
|
|
83
91
|
/**
|
|
84
92
|
* Creates a new `Texture` instance from the specified image source.
|
|
85
93
|
* @param rapid - The Rapid instance to use.
|
|
@@ -95,7 +103,23 @@ declare class Texture {
|
|
|
95
103
|
* @returns A new `Texture` instance created from the specified URL.
|
|
96
104
|
*/
|
|
97
105
|
static fromUrl(rapid: Rapid, url: string): Promise<Texture>;
|
|
106
|
+
/**
|
|
107
|
+
* Converts the current texture into a spritesheet.
|
|
108
|
+
* @param rapid - The Rapid instance to use.
|
|
109
|
+
* @param spriteWidth - The width of each sprite in the spritesheet.
|
|
110
|
+
* @param spriteHeight - The height of each sprite in the spritesheet.
|
|
111
|
+
* @returns An array of `Texture` instances representing the sprites in the spritesheet.
|
|
112
|
+
*/
|
|
113
|
+
createSpritesHeet(spriteWidth: number, spriteHeight: number): Texture[];
|
|
114
|
+
/**
|
|
115
|
+
* Clone the current texture
|
|
116
|
+
* @returns A new `Texture` instance with the same base texture reference.
|
|
117
|
+
*/
|
|
118
|
+
clone(): Texture;
|
|
98
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* @ignore
|
|
122
|
+
*/
|
|
99
123
|
export declare const SCALEFACTOR = 2;
|
|
100
124
|
declare class Text extends Texture {
|
|
101
125
|
private options;
|
|
@@ -108,6 +132,10 @@ declare class Text extends Texture {
|
|
|
108
132
|
*/
|
|
109
133
|
constructor(rapid: Rapid, options: ITextOptions);
|
|
110
134
|
private updateTextImage;
|
|
135
|
+
/**
|
|
136
|
+
* Creates a canvas element for rendering text.
|
|
137
|
+
* @returns HTMLCanvasElement - The created canvas element.
|
|
138
|
+
*/
|
|
111
139
|
private createTextCanvas;
|
|
112
140
|
/**
|
|
113
141
|
* Update the displayed text
|