rapid-render 0.0.1 → 0.0.2
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 -149
- package/dist/index.d.ts +3 -6
- package/dist/interface.d.ts +0 -7
- package/dist/math.d.ts +57 -58
- package/dist/rapid.global.js +1 -0
- package/dist/rapid.js +1 -784
- package/dist/rapid.umd.cjs +1 -42
- package/dist/regions/graphic_region.d.ts +10 -0
- package/dist/regions/region.d.ts +24 -0
- package/dist/regions/sprite_region.d.ts +16 -0
- package/dist/render.d.ts +33 -0
- package/dist/texture.d.ts +1 -1
- package/dist/webgl/glshader.d.ts +13 -0
- package/package.json +10 -21
- package/dist/consts.d.ts +0 -5
- package/dist/region/graphic_region.d.ts +0 -41
- package/dist/region/region.d.ts +0 -44
- package/dist/region/sprite_region.d.ts +0 -47
- package/dist/renderer.d.ts +0 -109
- /package/dist/{utils/webgl.d.ts → webgl/utils.d.ts} +0 -0
package/dist/rapid.umd.cjs
CHANGED
|
@@ -1,42 +1 @@
|
|
|
1
|
-
(function(n,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(n=typeof globalThis<"u"?globalThis:n||self,o(n.rapid={}))})(this,function(n){"use strict";var L=Object.defineProperty;var j=(n,o,c)=>o in n?L(n,o,{enumerable:!0,configurable:!0,writable:!0,value:c}):n[o]=c;var s=(n,o,c)=>(j(n,typeof o!="symbol"?o+"":o,c),c);class f{constructor(e){s(this,"usedElemNum");s(this,"typedArray");s(this,"arrayType");s(this,"maxElemNum");s(this,"bytePerElem");this.usedElemNum=0,this.maxElemNum=64,this.bytePerElem=e.BYTES_PER_ELEMENT,this.arrayType=e,this.typedArray=new e(this.maxElemNum)}clear(){this.usedElemNum=0}resize(e=0){if(e+=this.usedElemNum,e>this.maxElemNum){for(;e>this.maxElemNum;)this.maxElemNum<<=1;const t=this.typedArray;return this.typedArray=new this.arrayType(this.maxElemNum),this.typedArray.set(t),this}}push(e){this.typedArray[this.usedElemNum]=e,this.usedElemNum+=1}pop(e){this.usedElemNum-=e}getArray(e=0,t){return t==null?this.typedArray:this.typedArray.subarray(e,t)}get length(){return this.typedArray.length}}class g extends f{constructor(){super(Float32Array)}pushMat(){const e=this.usedElemNum-6,t=this.typedArray;this.resize(6),this.push(t[e+0]),this.push(t[e+1]),this.push(t[e+2]),this.push(t[e+3]),this.push(t[e+4]),this.push(t[e+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(e,t){const r=this.usedElemNum-6,i=this.typedArray;i[r+4]=i[r+0]*e+i[r+2]*t+i[r+4],i[r+5]=i[r+1]*e+i[r+3]*t+i[r+5]}rotate(e){const t=this.usedElemNum-6,r=this.typedArray,i=Math.cos(e),h=Math.sin(e),u=r[t+0],v=r[t+1],P=r[t+2],x=r[t+3];r[t+0]=u*i-v*h,r[t+1]=u*h+v*i,r[t+2]=P*i-x*h,r[t+3]=P*h+x*i}scale(e,t=e){const r=this.usedElemNum-6,i=this.typedArray;i[r+0]=i[r+0]*e,i[r+1]=i[r+1]*e,i[r+2]=i[r+2]*t,i[r+3]=i[r+3]*t}apply(e,t){const r=this.usedElemNum-6,i=this.typedArray;return[i[r+0]*e+i[r+2]*t+i[r+4],i[r+1]*e+i[r+3]*t+i[r+5]]}}class d{constructor(e,t,r,i){s(this,"r");s(this,"g");s(this,"b");s(this,"a");this.r=e,this.g=t,this.b=r,this.a=i}equals(e){return e.r==this.r&&e.g==this.g&&e.b==this.b&&e.a==this.a}static fromHex(e){e.startsWith("#")&&(e=e.slice(1));const t=parseInt(e.slice(0,2),16)/255,r=parseInt(e.slice(2,4),16)/255,i=parseInt(e.slice(4,6),16)/255;let h=1;return e.length>=8&&(h=parseInt(e.slice(6,8),16)/255),new d(t,r,i,h)}}class T extends f{constructor(t,r,i=t.ARRAY_BUFFER){super(r);s(this,"buffer");s(this,"gl");s(this,"type");s(this,"bufferSize",0);this.gl=t,this.buffer=t.createBuffer(),this.type=i}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){const t=this.gl;this.usedElemNum>this.bufferSize?(t.bufferData(this.type,this.getArray(0,this.usedElemNum),t.STATIC_DRAW),this.bufferSize=this.usedElemNum):t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum))}clear(){super.clear(),this.bufferSize=0}}class b extends T{constructor(t,r,i){super(t,Uint16Array,t.ELEMENT_ARRAY_BUFFER);s(this,"elemVertPerObj");s(this,"objects",0);s(this,"vertexPerObject");s(this,"dirty",!0);this.elemVertPerObj=r,this.vertexPerObject=i}addObject(t){this.objects++,this.resize(this.elemVertPerObj)}setObjects(t){if(t>this.objects){for(;t>this.objects;)this.addObject(this.objects*this.vertexPerObject);this.dirty=!0}}bufferData(){this.dirty&&super.bufferData(),this.dirty=!1}clear(){super.clear(),this.objects=0}}const _=a=>{const e=a.getContext("webgl2")||a.getContext("webgl");if(!e)throw new Error("TODO");return e},p=(a,e,t)=>{const r=a.createShader(t);if(!r)throw new Error("Unable to create webgl shader");if(a.shaderSource(r,e),a.compileShader(r),!a.getShaderParameter(r,a.COMPILE_STATUS)){const h=a.getShaderInfoLog(r);throw console.error("Shader compilation failed:",h),new Error("Unable to compile shader: "+h+e)}return r},m=(a,e,t)=>{var r=a.createProgram(),i=p(a,e,35633),h=p(a,t,35632);if(!r)throw new Error("Unable to create program shader");return a.attachShader(r,i),a.attachShader(r,h),a.linkProgram(r),r};function A(a,e,t){const r=a.createTexture();if(a.bindTexture(a.TEXTURE_2D,r),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MIN_FILTER,t?a.LINEAR:a.NEAREST),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_MAG_FILTER,t?a.LINEAR:a.NEAREST),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_S,a.CLAMP_TO_EDGE),a.texParameteri(a.TEXTURE_2D,a.TEXTURE_WRAP_T,a.CLAMP_TO_EDGE),a.texImage2D(a.TEXTURE_2D,0,a.RGBA,a.RGBA,a.UNSIGNED_BYTE,e),!r)throw new Error("unable to create texture");return r}class R{constructor(e){s(this,"shader");s(this,"defaultShader");s(this,"render");s(this,"gl");s(this,"attributeBuffer");s(this,"attribute");this.render=e,this.gl=e.gl,this.attributeBuffer=new T(this.gl,Float32Array)}createDefaultShader(e){this.defaultShader=m(this.render.gl,e.vs,e.fs),this.attribute=e.attribute}addVertex(e,t,...r){const[i,h]=this.render.matrix.apply(e,t);this.attributeBuffer.push(i),this.attributeBuffer.push(h)}flush(){this.doFlush(),this.reset()}doFlush(){this.attributeBuffer.bufferData()}reset(){this.attributeBuffer.clear()}enter(e){this.shader=e||this.defaultShader;const t=this.gl;t.useProgram(this.shader),this.attributeBuffer.bindBuffer(),this.attribute.forEach(i=>{const h=t.getAttribLocation(this.shader,i.name);t.enableVertexAttribArray(h),t.vertexAttribPointer(h,i.size,i.type,i.normalized||!1,i.stride,i.offset||0)});const r=t.getUniformLocation(this.shader,"uProjectionMatrix");t.uniformMatrix4fv(r,!1,this.render.projection)}exit(){}}const w=`precision mediump float;\r
|
|
2
|
-
\r
|
|
3
|
-
uniform vec4 uColor;\r
|
|
4
|
-
\r
|
|
5
|
-
void main(void) {\r
|
|
6
|
-
gl_FragColor = uColor;\r
|
|
7
|
-
}\r
|
|
8
|
-
`,M=`precision mediump float;\r
|
|
9
|
-
\r
|
|
10
|
-
attribute vec2 aPosition;\r
|
|
11
|
-
uniform mat4 uProjectionMatrix;\r
|
|
12
|
-
uniform vec4 uColor;\r
|
|
13
|
-
\r
|
|
14
|
-
void main(void) {\r
|
|
15
|
-
gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r
|
|
16
|
-
}\r
|
|
17
|
-
`;class y extends R{constructor(t){super(t);s(this,"color",new d(0,0,0,1));s(this,"colorDirty",!0);s(this,"count",0);const r=this.gl,i=2*Float32Array.BYTES_PER_ELEMENT;this.createDefaultShader({fs:w,vs:M,attribute:[{name:"aPosition",size:2,type:r.FLOAT,stride:i}]})}setColor(t){t.equals(this.color)||(this.color=t,this.colorDirty=!0)}drawShape(t,r=new d(0,0,0,1)){this.setColor(r);for(let i=0;i<t.length;i+=2)this.addVertex(t[i],t[i+1]),this.count++;this.flush()}doFlush(){if(this.count>0){super.doFlush();const t=this.gl;if(this.colorDirty){const r=this.color,i=this.gl,h=i.getUniformLocation(this.shader,"uColor");i.uniform4f(h,r.r,r.g,r.b,r.a),this.colorDirty=!1}t.drawArrays(t.TRIANGLE_FAN,0,this.count)}}reset(){super.reset(),this.count=0}enter(t){super.enter(t),this.colorDirty=!0}}const N=`precision lowp float;\r
|
|
18
|
-
uniform sampler2D uTextures[%TEXTURE_NUM%];\r
|
|
19
|
-
\r
|
|
20
|
-
uniform vec4 uColor;\r
|
|
21
|
-
varying vec2 vRegion;\r
|
|
22
|
-
varying float vTextureId;\r
|
|
23
|
-
\r
|
|
24
|
-
void main(void) {\r
|
|
25
|
-
vec4 color;\r
|
|
26
|
-
%GET_COLOR%\r
|
|
27
|
-
\r
|
|
28
|
-
gl_FragColor = color;\r
|
|
29
|
-
}`,U=`precision lowp float;\r
|
|
30
|
-
attribute vec2 aPosition;\r
|
|
31
|
-
attribute vec2 aRegion;\r
|
|
32
|
-
attribute float aTextureId;\r
|
|
33
|
-
\r
|
|
34
|
-
uniform mat4 uProjectionMatrix;\r
|
|
35
|
-
varying vec2 vRegion;\r
|
|
36
|
-
varying float vTextureId;\r
|
|
37
|
-
\r
|
|
38
|
-
void main(void) {\r
|
|
39
|
-
gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r
|
|
40
|
-
vRegion = aRegion;\r
|
|
41
|
-
vTextureId = aTextureId;\r
|
|
42
|
-
}`;class X extends b{constructor(e){super(e,6,4)}addObject(e){super.addObject(),this.push(e),this.push(e+3),this.push(e+2),this.push(e),this.push(e+1),this.push(e+2)}}class S extends R{constructor(t){super(t);s(this,"objects",0);s(this,"usedTextures",[]);s(this,"elementArray",new X(this.gl));s(this,"textureUnits");const r=this.gl,i=5*Float32Array.BYTES_PER_ELEMENT;this.createDefaultShader({fs:this.generateFragShader(N,t.MAX_TEXTURE_IMAGE_UNITS),vs:U,attribute:[{name:"aPosition",size:2,type:r.FLOAT,stride:i},{name:"aRegion",size:2,type:r.FLOAT,stride:i,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:r.FLOAT,stride:i,offset:4*Float32Array.BYTES_PER_ELEMENT}]}),this.textureUnits=Array.from({length:t.MAX_TEXTURE_IMAGE_UNITS},(h,u)=>u)}addVertex(t,r,i,h,u){super.addVertex(t,r),this.attributeBuffer.push(i),this.attributeBuffer.push(h),this.attributeBuffer.push(u)}drawSprite(t,r,i){this.objects>10922&&this.flush();const h=t.base,u=this.useTexture(h.texture);this.objects++,this.elementArray.setObjects(this.objects),this.attributeBuffer.resize(2*6),this.addVertex(r,i,t.clipX,t.clipY,u),this.addVertex(r+t.width,i,t.clipW,t.clipY,u),this.addVertex(r+t.width,i+t.height,t.clipW,t.clipH,u),this.addVertex(r,i+t.height,t.clipX,t.clipH,u)}useTexture(t){let r=this.usedTextures.indexOf(t);return r===-1&&(this.usedTextures.length>=this.render.MAX_TEXTURE_IMAGE_UNITS&&this.flush(),this.usedTextures.push(t),r=this.usedTextures.length-1),r}doFlush(){if(this.objects>0){super.doFlush();const t=this.gl;for(let r=0;r<this.usedTextures.length;r++){const i=this.usedTextures[r];t.activeTexture(t.TEXTURE0+r),t.bindTexture(t.TEXTURE_2D,i)}this.elementArray.bufferData(),t.drawElements(t.TRIANGLES,this.objects*6,t.UNSIGNED_SHORT,0)}}reset(){super.reset(),this.elementArray.clear(),this.objects=0,this.usedTextures=[]}enter(t){super.enter(t);const r=this.gl,i=r.getUniformLocation(this.shader,"uTextures");r.uniform1iv(i,this.textureUnits),this.elementArray.bindBuffer()}generateFragShader(t,r){let i="";t=t.replace("%TEXTURE_NUM%",r.toString());for(let h=0;h<r;h++)h==0?i+=`if(vTextureId == ${h}.0)`:h==r-1?i+="else":i+=`else if(vTextureId == ${h}.0)`,i+=`{color = texture2D(uTextures[${h}], vRegion);}`;return t=t.replace("%GET_COLOR%",i),t}}class I{constructor(e){s(this,"render");s(this,"cache",new Map);this.render=e}async textureFromUrl(e,t=!1){let r=this.cache.get(e);if(!r){const i=await this.loadImage(e);r=l.fromImageSource(this.render,i,t),this.cache.set(e,r)}return new E(r)}async loadImage(e){return new Promise(t=>{const r=new Image;r.onload=()=>{t(r)},r.src=e})}}class l{constructor(e,t,r){s(this,"texture");s(this,"width");s(this,"height");this.texture=e,this.width=t,this.height=r}static fromImageSource(e,t,r=!1){return new l(A(e.gl,t,r),t.width,t.height)}}class E{constructor(e){s(this,"base");s(this,"clipX");s(this,"clipY");s(this,"clipW");s(this,"clipH");s(this,"width");s(this,"height");this.base=e,this.setClipRegion(0,0,e.width,e.height),console.log(this)}setClipRegion(e,t,r,i){this.clipX=e/this.base.width,this.clipY=t/this.base.width,this.clipW=this.clipX+r/this.base.width,this.clipH=this.clipY+i/this.base.width,this.width=r,this.height=i}static fromImageSource(e,t,r=!1){return new E(l.fromImageSource(e,t,r))}static fromUrl(e,t){return e.texture.textureFromUrl(t)}}class C{constructor(e){s(this,"canvas");s(this,"gl");s(this,"projection");s(this,"matrix",new g);s(this,"texture",new I(this));s(this,"devicePixelRatio");s(this,"nativeWdith");s(this,"nativeHeight");s(this,"backgroundColor");s(this,"currentRegion");s(this,"regions",new Map);s(this,"MAX_TEXTURE_IMAGE_UNITS");this.canvas=e.canvas;const t=_(this.canvas);this.gl=t,this.backgroundColor=e.backgroundColor||new d(.2,.2,.2,1),this.devicePixelRatio=e.pixelDensity||window.devicePixelRatio||1,this.projection=this.createOrthMatrix(0,this.canvas.width,this.canvas.height,0),this.nativeHeight=e.height||500,this.nativeWdith=e.width||500,this.updateNativeSize(),this.resize(),t.enable(t.BLEND),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),this.MAX_TEXTURE_IMAGE_UNITS=this.gl.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.registerRegion("sprite",S),this.registerRegion("graphic",y)}registerRegion(e,t){this.regions.set(e,new t(this))}setRegion(e,t){var i,h;const r=this.regions.get(e);(r!=this.currentRegion||t&&t!==this.currentRegion.shader)&&((i=this.currentRegion)==null||i.flush(),(h=this.currentRegion)==null||h.exit(),this.currentRegion=r,r.enter(t))}drawSprite(e,t=0,r=0,i){this.setRegion("sprite",i),this.currentRegion.drawSprite(e,t,r)}drawShape(e,t,r){this.setRegion("graphic",r),this.currentRegion.drawShape(e,t)}flush(){this.currentRegion&&this.currentRegion.flush()}save(){this.matrix.pushMat()}start(){this.matrix.clear(),this.matrix.pushIdentity(),this.currentRegion=void 0,this.clear()}end(){var e;(e=this.currentRegion)==null||e.flush()}restore(){this.matrix.popMat()}clear(){const e=this.gl,t=this.backgroundColor;e.clearColor(t.r,t.g,t.b,t.a),e.clear(e.COLOR_BUFFER_BIT)}resize(){const e=this.canvas,t=this.devicePixelRatio;e.width=this.nativeWdith*t,e.height=this.nativeHeight*t,this.gl.viewport(0,0,e.width,e.height)}updateNativeSize(){this.projection=this.createOrthMatrix(0,this.nativeWdith,this.nativeHeight,0)}setNativeSize(e,t){this.nativeWdith=e/this.devicePixelRatio,this.nativeHeight=t/this.devicePixelRatio,this.updateNativeSize()}createOrthMatrix(e,t,r,i){return new Float32Array([2/(t-e),0,0,0,0,2/(i-r),0,0,0,0,-1,0,-(t+e)/(t-e),-(i+r)/(i-r),0,1])}createShader(e,t){return m(this.gl,e,t)}}n.BaseTexture=l,n.BufferArray=T,n.Color=d,n.DynamicArrayBuffer=f,n.ElementArray=b,n.GraphicRegion=y,n.MatrixStack=g,n.Rapid=C,n.RenderRegion=R,n.SpriteRegion=S,n.Texture=E,n.TextureCache=I,n.compileShader=p,n.createShaderProgram=m,n.createTexture=A,n.getContext=_,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
"use strict";class e{constructor(e){Object.defineProperty(this,"usedElemNum",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"typedArray",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"arrayType",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"maxElemNum",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"bytePerElem",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"arraybuffer",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"uint32",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=e.BYTES_PER_ELEMENT,this.arrayType=e,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.typedArray=new e(this.arraybuffer),e==Float32Array&&(this.uint32=new Uint32Array(this.arraybuffer))}clear(){this.usedElemNum=0}resize(e=0){if((e+=this.usedElemNum)>this.maxElemNum){for(;e>this.maxElemNum;)this.maxElemNum<<=1;this.setMaxSize(this.maxElemNum)}}setMaxSize(e=this.maxElemNum){const t=this.typedArray;this.maxElemNum=e,this.arraybuffer=new ArrayBuffer(e*this.bytePerElem),this.typedArray=new this.arrayType(this.arraybuffer),this.uint32&&(this.uint32=new Uint32Array(this.arraybuffer)),this.typedArray.set(t)}push(e){this.typedArray[this.usedElemNum++]=e}pushUint(e){this.uint32[this.usedElemNum++]=e}pop(e){this.usedElemNum-=e}getArray(e=0,t){return null==t?this.typedArray:this.typedArray.subarray(e,t)}get length(){return this.typedArray.length}}class t extends e{constructor(e,t,r=e.ARRAY_BUFFER){super(t),Object.defineProperty(this,"buffer",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"gl",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"dirty",{enumerable:!0,configurable:!0,writable:!0,value:!0}),Object.defineProperty(this,"type",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"webglBufferSize",{enumerable:!0,configurable:!0,writable:!0,value:0}),this.gl=e,this.buffer=e.createBuffer(),this.type=r}push(e){super.push(e),this.dirty=!0}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const e=this.gl;this.maxElemNum>this.webglBufferSize?(e.bufferData(this.type,this.getArray(),e.STATIC_DRAW),this.webglBufferSize=this.maxElemNum):e.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class r extends e{constructor(){super(Float32Array)}pushMat(){const e=this.usedElemNum-6,t=this.typedArray;this.resize(6),this.push(t[e+0]),this.push(t[e+1]),this.push(t[e+2]),this.push(t[e+3]),this.push(t[e+4]),this.push(t[e+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(e,t){const r=this.usedElemNum-6,i=this.typedArray;i[r+4]=i[r+0]*e+i[r+2]*t+i[r+4],i[r+5]=i[r+1]*e+i[r+3]*t+i[r+5]}rotate(e){const t=this.usedElemNum-6,r=this.typedArray,i=Math.cos(e),a=Math.sin(e),s=r[t+0],n=r[t+1],u=r[t+2],o=r[t+3];r[t+0]=s*i-n*a,r[t+1]=s*a+n*i,r[t+2]=u*i-o*a,r[t+3]=u*a+o*i}scale(e,t=e){const r=this.usedElemNum-6,i=this.typedArray;i[r+0]=i[r+0]*e,i[r+1]=i[r+1]*e,i[r+2]=i[r+2]*t,i[r+3]=i[r+3]*t}apply(e,t){const r=this.usedElemNum-6,i=this.typedArray;return[i[r+0]*e+i[r+2]*t+i[r+4],i[r+1]*e+i[r+3]*t+i[r+5]]}getInverse(){const e=this.usedElemNum-6,t=this.typedArray,r=t[e+0],i=t[e+1],a=t[e+2],s=t[e+3],n=t[e+4],u=t[e+5],o=r*s-i*a;return new Float32Array([s/o,-i/o,-a/o,r/o,(a*u-s*n)/o,(i*n-r*u)/o])}getTransform(){const e=this.usedElemNum-6,t=this.typedArray;return new Float32Array([t[e+0],t[e+1],t[e+2],t[e+3],t[e+4],t[e+5]])}setTransform(e){const t=this.usedElemNum-6,r=this.typedArray;r[t+0]=e[0],r[t+1]=e[1],r[t+2]=e[2],r[t+3]=e[3],r[t+4]=e[4],r[t+5]=e[5]}}class i extends t{constructor(e,t,r,i){super(e,Uint16Array,e.ELEMENT_ARRAY_BUFFER),this.setMaxSize(t*i);for(let e=0;e<i;e++)this.addObject(e*r);this.bindBuffer(),this.bufferData()}addObject(e){}}class a{constructor(e,t,r,i){Object.defineProperty(this,"_r",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_g",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_b",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_a",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"uint32",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this._r=e,this._g=t,this._b=r,this._a=i,this.updateUint()}get r(){return this._r}set r(e){this._r=e,this.updateUint()}get g(){return this._g}set g(e){this._g=e,this.updateUint()}get b(){return this._b}set b(e){this._b=e,this.updateUint()}get a(){return this._a}set a(e){this._a=e,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0}setRGBA(e,t,r,i){this.r=e,this.g=t,this.b=r,this.a=i,this.updateUint()}copy(e){this.setRGBA(e.r,e.g,e.b,e.a)}equals(e){return e.r==this.r&&e.g==this.g&&e.b==this.b&&e.a==this.a}static fromHex(e){e.startsWith("#")&&(e=e.slice(1));const t=parseInt(e.slice(0,2),16),r=parseInt(e.slice(2,4),16),i=parseInt(e.slice(4,6),16);let s=255;return e.length>=8&&(s=parseInt(e.slice(6,8),16)),new a(t,r,i,s)}add(e){return new a(Math.min(this.r+e.r,255),Math.min(this.g+e.g,255),Math.min(this.b+e.b,255),Math.min(this.a+e.a,255))}subtract(e){return new a(Math.max(this.r-e.r,0),Math.max(this.g-e.g,0),Math.max(this.b-e.b,0),Math.max(this.a-e.a,0))}}const s=(e,t,r)=>{const i=e.createShader(r);if(!i)throw new Error("Unable to create webgl shader");e.shaderSource(i,t),e.compileShader(i);if(!e.getShaderParameter(i,e.COMPILE_STATUS)){const r=e.getShaderInfoLog(i);throw console.error("Shader compilation failed:",r),new Error("Unable to compile shader: "+r+t)}return i};class n{constructor(e,t,r){Object.defineProperty(this,"attributeLoc",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"unifromLoc",{enumerable:!0,configurable:!0,writable:!0,value:{}}),Object.defineProperty(this,"program",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"gl",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.program=((e,t,r)=>{var i=e.createProgram(),a=s(e,t,35633),n=s(e,r,35632);if(!i)throw new Error("Unable to create program shader");return e.attachShader(i,a),e.attachShader(i,n),e.linkProgram(i),i})(e.gl,t,r),this.gl=e.gl,this.parseShader(t),this.parseShader(r)}use(){this.gl.useProgram(this.program)}parseShader(e){const t=this.gl,r=e.match(/attribute\s+\w+\s+(\w+)/g);if(r)for(const e of r){const r=e.split(" ")[2];this.attributeLoc[r]=t.getAttribLocation(this.program,r)}const i=e.match(/uniform\s+\w+\s+(\w+)/g);if(i)for(const e of i){const r=e.split(" ")[2];this.unifromLoc[r]=t.getUniformLocation(this.program,r)}}setAttribute(e){const t=this.gl;t.vertexAttribPointer(this.attributeLoc[e.name],e.size,e.type,e.normalized||!1,e.stride,e.offset||0),t.enableVertexAttribArray(this.attributeLoc[e.name])}}class u{constructor(e,r){Object.defineProperty(this,"currentShader",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"defaultShader",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"attribute",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"webglArrayBuffer",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"rapid",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"gl",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"usedTextures",{enumerable:!0,configurable:!0,writable:!0,value:[]}),Object.defineProperty(this,"TEXTURE_UNITS_ARRAY",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.attribute=r,this.rapid=e,this.gl=e.gl,this.webglArrayBuffer=new t(e.gl,Float32Array,e.gl.ARRAY_BUFFER),this.TEXTURE_UNITS_ARRAY=Array.from({length:e.MAX_TEXTURE_UNITS},((e,t)=>t))}addVertex(e,t,...r){const[i,a]=this.rapid.transformPoint(e,t);this.webglArrayBuffer.push(i),this.webglArrayBuffer.push(a)}useTexture(e){let t=this.usedTextures.indexOf(e);return-1===t&&(this.usedTextures.length>=this.rapid.MAX_TEXTURE_UNITS&&this.render(),this.usedTextures.push(e),t=this.usedTextures.length-1),t}enterRegion(e){this.currentShader=e??this.defaultShader,this.currentShader.use(),this.initializeForNextRender(),this.webglArrayBuffer.bindBuffer(),this.attribute.forEach((e=>{this.currentShader?.setAttribute(e)})),this.gl.uniformMatrix4fv(this.currentShader.unifromLoc.uProjectionMatrix,!1,this.rapid.projection)}exitRegion(){}initDefaultShader(e,t){this.webglArrayBuffer.bindBuffer(),this.defaultShader=new n(this.rapid,e,t)}render(){this.executeRender(),this.initializeForNextRender()}executeRender(){this.webglArrayBuffer.bufferData();const e=this.gl;for(let t=0;t<this.usedTextures.length;t++)e.activeTexture(e.TEXTURE0+t),e.bindTexture(e.TEXTURE_2D,this.usedTextures[t])}initializeForNextRender(){this.webglArrayBuffer.clear(),this.usedTextures=[]}}class o extends u{constructor(e){const t=e.gl;super(e,[{name:"aPosition",size:2,type:t.FLOAT,stride:12},{name:"aColor",size:4,type:t.UNSIGNED_BYTE,stride:12,offset:2*Float32Array.BYTES_PER_ELEMENT,normalized:!0}]),Object.defineProperty(this,"vertex",{enumerable:!0,configurable:!0,writable:!0,value:0}),this.initDefaultShader("precision mediump float;\r\n\r\nattribute vec2 aPosition;\r\nattribute vec4 aColor;\r\nvarying vec4 vColor;\r\nuniform mat4 uProjectionMatrix;\r\nuniform vec4 uColor;\r\n\r\nvoid main(void) {\r\n gl_Position = uProjectionMatrix * vec4(aPosition, 0.0, 1.0);\r\n vColor = aColor;\r\n}\r\n","precision mediump float;\r\n\r\nvarying vec4 vColor;\r\nvoid main(void) {\r\n gl_FragColor = vColor;//vColor;\r\n}\r\n")}startRender(){this.vertex=0,this.webglArrayBuffer.clear()}addVertex(e,t,r){this.webglArrayBuffer.resize(3),super.addVertex(e,t),this.webglArrayBuffer.pushUint(r),this.vertex+=1}executeRender(){super.executeRender();const e=this.gl;e.drawArrays(e.TRIANGLE_FAN,0,this.vertex),this.vertex=0}}class h extends i{constructor(e,t){super(e,6,4,t)}addObject(e){super.addObject(),this.push(e),this.push(e+3),this.push(e+2),this.push(e),this.push(e+1),this.push(e+2)}}class l extends u{constructor(e){const t=e.gl;super(e,[{name:"aPosition",size:2,type:t.FLOAT,stride:24},{name:"aRegion",size:2,type:t.FLOAT,stride:24,offset:2*Float32Array.BYTES_PER_ELEMENT},{name:"aTextureId",size:1,type:t.FLOAT,stride:24,offset:4*Float32Array.BYTES_PER_ELEMENT},{name:"aColor",size:4,type:t.UNSIGNED_BYTE,stride:24,offset:5*Float32Array.BYTES_PER_ELEMENT,normalized:!0}]),Object.defineProperty(this,"batchSprite",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"indexBuffer",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"MAX_BATCH",{enumerable:!0,configurable:!0,writable:!0,value:void 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}",this.generateFragShader("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}",e.MAX_TEXTURE_UNITS)),this.MAX_BATCH=Math.floor(16384),this.indexBuffer=new h(t,this.MAX_BATCH)}addVertex(e,t,r,i,a,s){super.addVertex(e,t),this.webglArrayBuffer.push(r),this.webglArrayBuffer.push(i),this.webglArrayBuffer.push(a),this.webglArrayBuffer.pushUint(s)}renderSprite(e,t,r,i,a,s,n,u,o,h){this.batchSprite>=this.MAX_BATCH&&this.render(),this.batchSprite++,this.webglArrayBuffer.resize(24);const l=this.useTexture(e),c=u+t,b=o+r,d=i+s,f=a+n;this.addVertex(u,o,i,a,l,h),this.addVertex(c,o,d,a,l,h),this.addVertex(c,b,d,f,l,h),this.addVertex(u,b,i,f,l,h)}executeRender(){super.executeRender();const e=this.gl;this.indexBuffer.bufferData(),e.drawElements(e.TRIANGLES,6*this.batchSprite,e.UNSIGNED_SHORT,0)}enterRegion(e){super.enterRegion(e),this.indexBuffer.bindBuffer(),this.gl.uniform1iv(this.currentShader.unifromLoc.uTextures,this.TEXTURE_UNITS_ARRAY)}initializeForNextRender(){super.initializeForNextRender(),this.batchSprite=0}generateFragShader(e,t){let r="";e=e.replace("%TEXTURE_NUM%",t.toString());for(let e=0;e<t;e++)r+=0==e?`if(vTextureId == ${e}.0)`:e==t-1?"else":`else if(vTextureId == ${e}.0)`,r+=`{color = texture2D(uTextures[${e}], vRegion);}`;return e=e.replace("%GET_COLOR%",r)}}class c{constructor(e){Object.defineProperty(this,"render",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"cache",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),this.render=e}async textureFromUrl(e,t=!1){let r=this.cache.get(e);if(!r){const i=await this.loadImage(e);r=b.fromImageSource(this.render,i,t),this.cache.set(e,r)}return new d(r)}async loadImage(e){return new Promise((t=>{const r=new Image;r.onload=()=>{t(r)},r.src=e}))}}class b{constructor(e,t,r){Object.defineProperty(this,"texture",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"width",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"height",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.texture=e,this.width=t,this.height=r}static fromImageSource(e,t,r=!1){return new b(function(e,t,r){const i=e.createTexture();if(e.bindTexture(e.TEXTURE_2D,i),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,r?e.LINEAR:e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,r?e.LINEAR:e.NEAREST),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,t),!i)throw new Error("unable to create texture");return i}(e.gl,t,r),t.width,t.height)}}class d{constructor(e){Object.defineProperty(this,"base",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"clipX",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"clipY",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"clipW",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"clipH",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"width",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"height",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.base=e,this.setClipRegion(0,0,e.width,e.height)}setClipRegion(e,t,r,i){this.clipX=e/this.base.width,this.clipY=t/this.base.width,this.clipW=this.clipX+r/this.base.width,this.clipH=this.clipY+i/this.base.width,this.width=r,this.height=i}static fromImageSource(e,t,r=!1){return new d(b.fromImageSource(e,t,r))}static fromUrl(e,t){return e.texture.textureFromUrl(t)}}exports.BaseTexture=b,exports.Color=a,exports.DynamicArrayBuffer=e,exports.MatrixStack=r,exports.Rapid=class{constructor(e){Object.defineProperty(this,"gl",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"canvas",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"projection",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"matrixStack",{enumerable:!0,configurable:!0,writable:!0,value:new r}),Object.defineProperty(this,"texture",{enumerable:!0,configurable:!0,writable:!0,value:new c(this)}),Object.defineProperty(this,"currentRegion",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"currentRegionName",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"regions",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"MAX_TEXTURE_UNITS",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"defaultColor",{enumerable:!0,configurable:!0,writable:!0,value:new a(255,255,255,255)});const t=(e=>{const t=e.getContext("webgl2")||e.getContext("webgl");if(!t)throw new Error("TODO");return t})(e.canvas);this.gl=t,this.canvas=e.canvas,this.MAX_TEXTURE_UNITS=t.getParameter(this.gl.MAX_TEXTURE_IMAGE_UNITS),this.projection=this.createOrthMatrix(0,this.canvas.width,this.canvas.height,0),this.registerBuildInRegion(),t.enable(t.BLEND),t.disable(t.DEPTH_TEST),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA)}registerBuildInRegion(){this.registerRegion("sprite",l),this.registerRegion("graphic",o)}registerRegion(e,t){this.regions.set(e,new t(this))}setRegion(e,t){if(e!=this.currentRegionName||t&&t!==this.currentRegion.currentShader){const r=this.regions.get(e);this.currentRegion&&(this.currentRegion.render(),this.currentRegion.exitRegion()),this.currentRegion=r,this.currentRegionName=e,r.enterRegion(t)}}save(){this.matrixStack.pushMat()}restore(){this.matrixStack.popMat()}startRender(e=!0){e&&this.matrixStack.clear(),this.matrixStack.pushIdentity(),this.currentRegion=void 0,this.currentRegionName=void 0}endRender(){this.currentRegion?.render()}renderSprite(e,t=0,r=0,i=this.defaultColor,a){this.setRegion("sprite",a),this.currentRegion.renderSprite(e.base.texture,e.width,e.height,e.clipX,e.clipY,e.clipW,e.clipH,t,r,i.uint32)}startGraphicDraw(e){this.setRegion("graphic",e),this.currentRegion.startRender()}addGraphicVertex(e,t,r){this.currentRegion.addVertex(e,t,r.uint32)}endGraphicDraw(){this.currentRegion.render()}clear(){const e=this.gl;e.clearColor(.2,.2,.2,1),e.clear(e.COLOR_BUFFER_BIT)}createOrthMatrix(e,t,r,i){return new Float32Array([2/(t-e),0,0,0,0,2/(i-r),0,0,0,0,-1,0,-(t+e)/(t-e),-(i+r)/(i-r),0,1])}transformPoint(e,t){return this.matrixStack.apply(e,t)}},exports.Texture=d,exports.TextureCache=c,exports.WebglBufferArray=t,exports.WebglElementBufferArray=i;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Rapid } from "..";
|
|
2
|
+
import RenderRegion from "./region";
|
|
3
|
+
declare class GraphicRegion extends RenderRegion {
|
|
4
|
+
private vertex;
|
|
5
|
+
constructor(rapid: Rapid);
|
|
6
|
+
startRender(): void;
|
|
7
|
+
addVertex(x: number, y: number, color: number): void;
|
|
8
|
+
protected executeRender(): void;
|
|
9
|
+
}
|
|
10
|
+
export default GraphicRegion;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { IAttribute, WebGLContext } from "../interface";
|
|
2
|
+
import { WebglBufferArray } from "../math";
|
|
3
|
+
import Rapid from "../render";
|
|
4
|
+
import GLShader from "../webgl/glshader";
|
|
5
|
+
declare class RenderRegion {
|
|
6
|
+
currentShader?: GLShader;
|
|
7
|
+
protected defaultShader: GLShader;
|
|
8
|
+
protected attribute: IAttribute[];
|
|
9
|
+
protected webglArrayBuffer: WebglBufferArray;
|
|
10
|
+
protected rapid: Rapid;
|
|
11
|
+
protected gl: WebGLContext;
|
|
12
|
+
protected usedTextures: WebGLTexture[];
|
|
13
|
+
protected readonly TEXTURE_UNITS_ARRAY: number[];
|
|
14
|
+
constructor(rapid: Rapid, attributes?: IAttribute[]);
|
|
15
|
+
protected addVertex(x: number, y: number, ..._: unknown[]): void;
|
|
16
|
+
protected useTexture(texture: WebGLTexture): number;
|
|
17
|
+
enterRegion(customShader?: GLShader): void;
|
|
18
|
+
exitRegion(): void;
|
|
19
|
+
protected initDefaultShader(vs: string, fs: string): void;
|
|
20
|
+
render(): void;
|
|
21
|
+
protected executeRender(): void;
|
|
22
|
+
protected initializeForNextRender(): void;
|
|
23
|
+
}
|
|
24
|
+
export default RenderRegion;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import RenderRegion from "./region";
|
|
2
|
+
import Rapid from "../render";
|
|
3
|
+
import GLShader from "../webgl/glshader";
|
|
4
|
+
declare class SpriteRegion extends RenderRegion {
|
|
5
|
+
private batchSprite;
|
|
6
|
+
private indexBuffer;
|
|
7
|
+
protected readonly MAX_BATCH: number;
|
|
8
|
+
constructor(rapid: Rapid);
|
|
9
|
+
protected addVertex(x: number, y: number, u: number, v: number, textureUnit: number, color: number): void;
|
|
10
|
+
renderSprite(texture: WebGLTexture, width: number, height: number, u0: number, v0: number, u1: number, v1: number, offsetX: number, offsetY: number, color: number): void;
|
|
11
|
+
protected executeRender(): void;
|
|
12
|
+
enterRegion(customShader?: GLShader | undefined): void;
|
|
13
|
+
protected initializeForNextRender(): void;
|
|
14
|
+
private generateFragShader;
|
|
15
|
+
}
|
|
16
|
+
export default SpriteRegion;
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { IRapiadOptions, WebGLContext } from "./interface";
|
|
2
|
+
import { Color, MatrixStack } from "./math";
|
|
3
|
+
import RenderRegion from "./regions/region";
|
|
4
|
+
import { Texture, TextureCache } from "./texture";
|
|
5
|
+
import GLShader from "./webgl/glshader";
|
|
6
|
+
declare class Rapid {
|
|
7
|
+
gl: WebGLContext;
|
|
8
|
+
canvas: HTMLCanvasElement;
|
|
9
|
+
projection: Float32Array;
|
|
10
|
+
matrixStack: MatrixStack;
|
|
11
|
+
texture: TextureCache;
|
|
12
|
+
private currentRegion?;
|
|
13
|
+
private currentRegionName?;
|
|
14
|
+
private regions;
|
|
15
|
+
readonly MAX_TEXTURE_UNITS: number;
|
|
16
|
+
private readonly defaultColor;
|
|
17
|
+
constructor(options: IRapiadOptions);
|
|
18
|
+
private registerBuildInRegion;
|
|
19
|
+
registerRegion(name: string, regionClass: typeof RenderRegion): void;
|
|
20
|
+
setRegion(regionName: string, customShader?: GLShader): void;
|
|
21
|
+
save(): void;
|
|
22
|
+
restore(): void;
|
|
23
|
+
startRender(clear?: boolean): void;
|
|
24
|
+
endRender(): void;
|
|
25
|
+
renderSprite(texture: Texture, offsetX?: number, offsetY?: number, color?: Color, customShader?: GLShader): void;
|
|
26
|
+
startGraphicDraw(customShader?: GLShader): void;
|
|
27
|
+
addGraphicVertex(x: number, y: number, color: Color): void;
|
|
28
|
+
endGraphicDraw(): void;
|
|
29
|
+
clear(): void;
|
|
30
|
+
private createOrthMatrix;
|
|
31
|
+
transformPoint(x: number, y: number): number[];
|
|
32
|
+
}
|
|
33
|
+
export default Rapid;
|
package/dist/texture.d.ts
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IAttribute } from "../interface";
|
|
2
|
+
import Rapid from "../render";
|
|
3
|
+
declare class GLShader {
|
|
4
|
+
attributeLoc: Record<string, number>;
|
|
5
|
+
unifromLoc: Record<string, WebGLUniformLocation>;
|
|
6
|
+
program: WebGLProgram;
|
|
7
|
+
private gl;
|
|
8
|
+
constructor(rapid: Rapid, vs: string, fs: string);
|
|
9
|
+
use(): void;
|
|
10
|
+
parseShader(shader: string): void;
|
|
11
|
+
setAttribute(element: IAttribute): void;
|
|
12
|
+
}
|
|
13
|
+
export default GLShader;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rapid-render",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -14,27 +14,16 @@
|
|
|
14
14
|
"require": "./dist/rapid.umd.cjs"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
|
-
"dev": "
|
|
18
|
-
"build": "
|
|
17
|
+
"dev": "rollup -c rollup.config.dev.js -w",
|
|
18
|
+
"build": "rollup -c rollup.config.prod.js && tsc "
|
|
19
19
|
},
|
|
20
|
-
"keywords": [
|
|
21
|
-
"webgl",
|
|
22
|
-
"game",
|
|
23
|
-
"engine",
|
|
24
|
-
"game engine",
|
|
25
|
-
"canvas",
|
|
26
|
-
"game dev",
|
|
27
|
-
"renderer",
|
|
28
|
-
"render",
|
|
29
|
-
"fast"
|
|
30
|
-
],
|
|
31
20
|
"devDependencies": {
|
|
21
|
+
"rollup": "^4.12.0",
|
|
22
|
+
"rollup-plugin-server": "^0.7.0",
|
|
23
|
+
"rollup-plugin-typescript2": "^0.36.0",
|
|
32
24
|
"tslib": "^2.6.2",
|
|
33
|
-
"typedoc": "^0.25.7",
|
|
34
|
-
"typedoc-theme-category-nav": "^0.0.3",
|
|
35
25
|
"typescript": "^5.3.3",
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
26
|
+
"rollup-plugin-string": "^3.0.0",
|
|
27
|
+
"rollup-plugin-terser": "^7.0.2"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/dist/consts.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import RenderRegion from "./region";
|
|
2
|
-
import Rapid from "../renderer";
|
|
3
|
-
import { Color } from "../math";
|
|
4
|
-
/**
|
|
5
|
-
* draw quad texture sprites
|
|
6
|
-
*/
|
|
7
|
-
declare class GraphicRegion extends RenderRegion {
|
|
8
|
-
/** color to draw shapes */
|
|
9
|
-
protected color: Color;
|
|
10
|
-
/** is the color needs to be updated */
|
|
11
|
-
private colorDirty;
|
|
12
|
-
/** number of vertex to render */
|
|
13
|
-
private count;
|
|
14
|
-
constructor(render: Rapid);
|
|
15
|
-
/**
|
|
16
|
-
* set a color
|
|
17
|
-
* @param color
|
|
18
|
-
*/
|
|
19
|
-
private setColor;
|
|
20
|
-
/**
|
|
21
|
-
* draw a shape
|
|
22
|
-
* @param vertexs
|
|
23
|
-
* @param color
|
|
24
|
-
*/
|
|
25
|
-
drawShape(vertexs: number[], color?: Color): void;
|
|
26
|
-
/**
|
|
27
|
-
* render
|
|
28
|
-
*/
|
|
29
|
-
protected doFlush(): void;
|
|
30
|
-
/**
|
|
31
|
-
* reset this region
|
|
32
|
-
* called when flush or entering this region
|
|
33
|
-
*/
|
|
34
|
-
reset(): void;
|
|
35
|
-
/**
|
|
36
|
-
* enter this region
|
|
37
|
-
* @param shader costum shader
|
|
38
|
-
*/
|
|
39
|
-
enter(shader?: WebGLProgram | undefined): void;
|
|
40
|
-
}
|
|
41
|
-
export default GraphicRegion;
|
package/dist/region/region.d.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { IRenderRegionOptions, WebGLContext } from "../interface";
|
|
2
|
-
import { BufferArray as WebglBufferArray } from "../math";
|
|
3
|
-
import Rapid from "../renderer";
|
|
4
|
-
declare class RenderRegion {
|
|
5
|
-
/** shader to render */
|
|
6
|
-
shader?: WebGLProgram;
|
|
7
|
-
/** If there is no new shader, use the default shader */
|
|
8
|
-
protected defaultShader: WebGLProgram;
|
|
9
|
-
protected render: Rapid;
|
|
10
|
-
protected gl: WebGLContext;
|
|
11
|
-
/** buffer */
|
|
12
|
-
protected attributeBuffer: WebglBufferArray;
|
|
13
|
-
/** shader attribute */
|
|
14
|
-
private attribute;
|
|
15
|
-
constructor(render: Rapid);
|
|
16
|
-
/**
|
|
17
|
-
* Create a default shader that is called by subclasses
|
|
18
|
-
* @param options
|
|
19
|
-
*/
|
|
20
|
-
createDefaultShader(options: IRenderRegionOptions): void;
|
|
21
|
-
/**
|
|
22
|
-
* add a vertex
|
|
23
|
-
* @param x
|
|
24
|
-
* @param y
|
|
25
|
-
*/
|
|
26
|
-
addVertex(x: number, y: number, ..._args: unknown[]): void;
|
|
27
|
-
flush(): void;
|
|
28
|
-
protected doFlush(): void;
|
|
29
|
-
/**
|
|
30
|
-
* reset this region
|
|
31
|
-
* called when flush or entering this region
|
|
32
|
-
*/
|
|
33
|
-
reset(): void;
|
|
34
|
-
/**
|
|
35
|
-
* enter this region
|
|
36
|
-
* @param shader costum shader
|
|
37
|
-
*/
|
|
38
|
-
enter(shader?: WebGLProgram): void;
|
|
39
|
-
/**
|
|
40
|
-
* exit this reigon
|
|
41
|
-
*/
|
|
42
|
-
exit(): void;
|
|
43
|
-
}
|
|
44
|
-
export default RenderRegion;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import RenderRegion from "./region";
|
|
2
|
-
import Rapid from "../renderer";
|
|
3
|
-
import { Texture } from "../texture";
|
|
4
|
-
/**
|
|
5
|
-
* draw quad texture sprites
|
|
6
|
-
*/
|
|
7
|
-
declare class SpriteRegion extends RenderRegion {
|
|
8
|
-
private objects;
|
|
9
|
-
private usedTextures;
|
|
10
|
-
private elementArray;
|
|
11
|
-
readonly textureUnits: number[];
|
|
12
|
-
constructor(render: Rapid);
|
|
13
|
-
addVertex(x: number, y: number, v: number, u: number, t: number): void;
|
|
14
|
-
/**
|
|
15
|
-
* draw a sprite
|
|
16
|
-
* @param texture sprite texture
|
|
17
|
-
* @param offsetX
|
|
18
|
-
* @param offsetY
|
|
19
|
-
*/
|
|
20
|
-
drawSprite(texture: Texture, offsetX: number, offsetY: number): void;
|
|
21
|
-
/**
|
|
22
|
-
* use a texture
|
|
23
|
-
* @return texture unit id
|
|
24
|
-
*/
|
|
25
|
-
private useTexture;
|
|
26
|
-
/**
|
|
27
|
-
* render
|
|
28
|
-
*/
|
|
29
|
-
protected doFlush(): void;
|
|
30
|
-
/**
|
|
31
|
-
* reset this region
|
|
32
|
-
*/
|
|
33
|
-
reset(): void;
|
|
34
|
-
/**
|
|
35
|
-
* enter this region
|
|
36
|
-
* @param shader costum shader
|
|
37
|
-
*/
|
|
38
|
-
enter(shader?: WebGLProgram | undefined): void;
|
|
39
|
-
/**
|
|
40
|
-
* Generate shader based on the maximum texture units supported by the GPU
|
|
41
|
-
* @param fs
|
|
42
|
-
* @param max
|
|
43
|
-
* @returns
|
|
44
|
-
*/
|
|
45
|
-
generateFragShader(fs: string, max: number): string;
|
|
46
|
-
}
|
|
47
|
-
export default SpriteRegion;
|
package/dist/renderer.d.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { IRapiadOptions, WebGLContext } from "./interface";
|
|
2
|
-
import { Color, MatrixStack } from "./math";
|
|
3
|
-
import RenderRegion from "./region/region";
|
|
4
|
-
import { Texture, TextureCache } from "./texture";
|
|
5
|
-
/**
|
|
6
|
-
* Rapid
|
|
7
|
-
*⚡Rapid.js is a lightning-fast and lightweight 2D WebGL rendering for HTML5
|
|
8
|
-
* @example
|
|
9
|
-
* ```js
|
|
10
|
-
* let rap = new Rapid({
|
|
11
|
-
* canvas: document.getElementById("game"),
|
|
12
|
-
* width: 600,
|
|
13
|
-
* height: 600,
|
|
14
|
-
* backgroundColor: Color.fromHex("00FFFF")
|
|
15
|
-
* })
|
|
16
|
-
* rap.start() // Please call rap.start before rendering
|
|
17
|
-
* // draw..
|
|
18
|
-
* rap.end() // Please call rap.end after rendering
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
declare class Rapid {
|
|
22
|
-
/**
|
|
23
|
-
* canvas element
|
|
24
|
-
*/
|
|
25
|
-
canvas: HTMLCanvasElement;
|
|
26
|
-
/**
|
|
27
|
-
* WebGLContext
|
|
28
|
-
*/
|
|
29
|
-
gl: WebGLContext;
|
|
30
|
-
/**
|
|
31
|
-
* projection matrix
|
|
32
|
-
*/
|
|
33
|
-
projection: Float32Array;
|
|
34
|
-
/**
|
|
35
|
-
* matrix stack
|
|
36
|
-
*/
|
|
37
|
-
matrix: MatrixStack;
|
|
38
|
-
/**
|
|
39
|
-
* texture manager
|
|
40
|
-
*/
|
|
41
|
-
texture: TextureCache;
|
|
42
|
-
devicePixelRatio: number;
|
|
43
|
-
nativeWdith: number;
|
|
44
|
-
nativeHeight: number;
|
|
45
|
-
/** background color */
|
|
46
|
-
backgroundColor: Color;
|
|
47
|
-
private currentRegion?;
|
|
48
|
-
private regions;
|
|
49
|
-
readonly MAX_TEXTURE_IMAGE_UNITS: number;
|
|
50
|
-
constructor(options: IRapiadOptions);
|
|
51
|
-
/**
|
|
52
|
-
* register a region, use {@link setRegion} to use it
|
|
53
|
-
* @param name
|
|
54
|
-
* @param region
|
|
55
|
-
*/
|
|
56
|
-
registerRegion(name: string, region: typeof RenderRegion): void;
|
|
57
|
-
/**
|
|
58
|
-
* Sets the current render region and its associated shader.
|
|
59
|
-
*
|
|
60
|
-
* @param regionName - The name of the render region to use.
|
|
61
|
-
* @param shader - The shader to use with the render region, or undefined to use the default shader.
|
|
62
|
-
*/
|
|
63
|
-
setRegion(regionName: string, shader?: WebGLShader): void;
|
|
64
|
-
/**
|
|
65
|
-
* Draws a sprite on the screen.
|
|
66
|
-
*
|
|
67
|
-
* @param texture - The texture to use for the sprite.
|
|
68
|
-
* @param offsetX - The horizontal offset of the sprite from the origin.
|
|
69
|
-
* @param offsetY - The vertical offset of the sprite from the origin.
|
|
70
|
-
*/
|
|
71
|
-
drawSprite(texture: Texture, offsetX?: number, offsetY?: number, shader?: WebGLShader): void;
|
|
72
|
-
/**
|
|
73
|
-
* Draws a shape on the screen.
|
|
74
|
-
*
|
|
75
|
-
* @param vertexs - An array of vertex coordinates, where each vertex is a 2-element array of X and Y coordinates.
|
|
76
|
-
* @param color - The color of the shape
|
|
77
|
-
*/
|
|
78
|
-
drawShape(vertexs: number[], color?: Color, shader?: WebGLShader): void;
|
|
79
|
-
/**
|
|
80
|
-
* render
|
|
81
|
-
*/
|
|
82
|
-
flush(): void;
|
|
83
|
-
/**
|
|
84
|
-
* Saves the current state
|
|
85
|
-
*/
|
|
86
|
-
save(): void;
|
|
87
|
-
/**
|
|
88
|
-
* Call this before rendering
|
|
89
|
-
*/
|
|
90
|
-
start(): void;
|
|
91
|
-
/**
|
|
92
|
-
* Call this after rendering
|
|
93
|
-
*/
|
|
94
|
-
end(): void;
|
|
95
|
-
/**
|
|
96
|
-
* Restore to the previous saved state
|
|
97
|
-
*/
|
|
98
|
-
restore(): void;
|
|
99
|
-
/**
|
|
100
|
-
* clear the canvas
|
|
101
|
-
*/
|
|
102
|
-
clear(): void;
|
|
103
|
-
resize(): void;
|
|
104
|
-
private updateNativeSize;
|
|
105
|
-
setNativeSize(width: number, height: number): void;
|
|
106
|
-
private createOrthMatrix;
|
|
107
|
-
createShader(vs: string, fs: string): WebGLProgram;
|
|
108
|
-
}
|
|
109
|
-
export default Rapid;
|
|
File without changes
|