rapid-render 0.0.1
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/LICENSE +21 -0
- package/README.md +149 -0
- package/dist/consts.d.ts +5 -0
- package/dist/index.d.ts +9 -0
- package/dist/interface.d.ts +23 -0
- package/dist/math.d.ts +128 -0
- package/dist/rapid.js +784 -0
- package/dist/rapid.umd.cjs +42 -0
- package/dist/region/graphic_region.d.ts +41 -0
- package/dist/region/region.d.ts +44 -0
- package/dist/region/sprite_region.d.ts +47 -0
- package/dist/renderer.d.ts +109 -0
- package/dist/texture.d.ts +88 -0
- package/dist/utils/webgl.d.ts +24 -0
- package/package.json +40 -0
|
@@ -0,0 +1,42 @@
|
|
|
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"})});
|
|
@@ -0,0 +1,41 @@
|
|
|
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;
|
|
@@ -0,0 +1,44 @@
|
|
|
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;
|
|
@@ -0,0 +1,47 @@
|
|
|
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;
|
|
@@ -0,0 +1,109 @@
|
|
|
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;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Rapid } from ".";
|
|
2
|
+
import { Images } from "./interface";
|
|
3
|
+
/**
|
|
4
|
+
* texture manager
|
|
5
|
+
*/
|
|
6
|
+
declare class TextureCache {
|
|
7
|
+
private render;
|
|
8
|
+
private cache;
|
|
9
|
+
constructor(render: Rapid);
|
|
10
|
+
/**
|
|
11
|
+
* create texture from url
|
|
12
|
+
* Equivalent to {@link Texture.fromUrl} method
|
|
13
|
+
* @param url
|
|
14
|
+
* @param antialias
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
textureFromUrl(url: string, antialias?: boolean): Promise<Texture>;
|
|
18
|
+
loadImage(url: string): Promise<HTMLImageElement>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Each {@link Texture} references a baseTexture, and different textures may have the same baseTexture
|
|
22
|
+
*/
|
|
23
|
+
declare class BaseTexture {
|
|
24
|
+
texture: WebGLTexture;
|
|
25
|
+
width: number;
|
|
26
|
+
height: number;
|
|
27
|
+
constructor(texture: WebGLTexture, width: number, height: number);
|
|
28
|
+
static fromImageSource(r: Rapid, image: Images, antialias?: boolean): BaseTexture;
|
|
29
|
+
}
|
|
30
|
+
declare class Texture {
|
|
31
|
+
/**
|
|
32
|
+
* Reference to the {@link BaseTexture} used by this texture.
|
|
33
|
+
*/
|
|
34
|
+
base: BaseTexture;
|
|
35
|
+
/**
|
|
36
|
+
* The x-coordinate of the top-left corner of the clipped region.
|
|
37
|
+
*/
|
|
38
|
+
clipX: number;
|
|
39
|
+
/**
|
|
40
|
+
* The y-coordinate of the top-left corner of the clipped region.
|
|
41
|
+
*/
|
|
42
|
+
clipY: number;
|
|
43
|
+
/**
|
|
44
|
+
* The width of the clipped region.
|
|
45
|
+
*/
|
|
46
|
+
clipW: number;
|
|
47
|
+
/**
|
|
48
|
+
* The height of the clipped region.
|
|
49
|
+
*/
|
|
50
|
+
clipH: number;
|
|
51
|
+
/**
|
|
52
|
+
* The width of the texture.
|
|
53
|
+
*/
|
|
54
|
+
width: number;
|
|
55
|
+
/**
|
|
56
|
+
* The height of the texture.
|
|
57
|
+
*/
|
|
58
|
+
height: number;
|
|
59
|
+
/**
|
|
60
|
+
* Creates a new `Texture` instance with the specified base texture reference.
|
|
61
|
+
* @param base - The {@link BaseTexture} to be used by the texture.
|
|
62
|
+
*/
|
|
63
|
+
constructor(base: BaseTexture);
|
|
64
|
+
/**
|
|
65
|
+
* Sets the region of the texture to be used for rendering.
|
|
66
|
+
* @param x - The x-coordinate of the top-left corner of the region.
|
|
67
|
+
* @param y - The y-coordinate of the top-left corner of the region.
|
|
68
|
+
* @param w - The width of the region.
|
|
69
|
+
* @param h - The height of the region.
|
|
70
|
+
*/
|
|
71
|
+
setClipRegion(x: number, y: number, w: number, h: number): void;
|
|
72
|
+
/**
|
|
73
|
+
* Creates a new `Texture` instance from the specified image source.
|
|
74
|
+
* @param rapid - The Rapid instance to use.
|
|
75
|
+
* @param image - The image source to create the texture from.
|
|
76
|
+
* @param antialias - Whether to enable antialiasing for the texture. Default is `false`.
|
|
77
|
+
* @returns A new `Texture` instance created from the image source.
|
|
78
|
+
*/
|
|
79
|
+
static fromImageSource(rapid: Rapid, image: Images, antialias?: boolean): Texture;
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new `Texture` instance from the specified URL.
|
|
82
|
+
* @param rapid - The Rapid instance to use.
|
|
83
|
+
* @param url - The URL of the image to create the texture from.
|
|
84
|
+
* @returns A new `Texture` instance created from the specified URL.
|
|
85
|
+
*/
|
|
86
|
+
static fromUrl(rapid: Rapid, url: string): Promise<Texture>;
|
|
87
|
+
}
|
|
88
|
+
export { Texture, BaseTexture, TextureCache };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { WebGLContext } from "../interface";
|
|
2
|
+
/**
|
|
3
|
+
* get webgl rendering context
|
|
4
|
+
* @param canvas
|
|
5
|
+
* @returns webgl1 or webgl2
|
|
6
|
+
*/
|
|
7
|
+
export declare const getContext: (canvas: HTMLCanvasElement) => WebGLContext;
|
|
8
|
+
/**
|
|
9
|
+
* compile string to webgl shader
|
|
10
|
+
* @param gl
|
|
11
|
+
* @param source
|
|
12
|
+
* @param type
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
export declare const compileShader: (gl: WebGLContext, source: string, type: number) => WebGLShader;
|
|
16
|
+
/**
|
|
17
|
+
* create webgl program by shader string
|
|
18
|
+
* @param gl
|
|
19
|
+
* @param vsSource
|
|
20
|
+
* @param fsSource
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
export declare const createShaderProgram: (gl: WebGLContext, vsSource: string, fsSource: string) => WebGLProgram;
|
|
24
|
+
export declare function createTexture(gl: WebGLRenderingContext, image: TexImageSource, antialias: boolean): WebGLTexture;
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rapid-render",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"main": "./dist/rapid.umd.cjs",
|
|
9
|
+
"module": "./dist/rapid.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/rapid.js",
|
|
14
|
+
"require": "./dist/rapid.umd.cjs"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"dev": "vite",
|
|
18
|
+
"build": "vite build && tsc"
|
|
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
|
+
"devDependencies": {
|
|
32
|
+
"tslib": "^2.6.2",
|
|
33
|
+
"typedoc": "^0.25.7",
|
|
34
|
+
"typedoc-theme-category-nav": "^0.0.3",
|
|
35
|
+
"typescript": "^5.3.3",
|
|
36
|
+
"vite": "^5.0.10"
|
|
37
|
+
},
|
|
38
|
+
"author": "nightscra",
|
|
39
|
+
"license": "MIT"
|
|
40
|
+
}
|