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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Nightre
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# <center>rapid.js</center>
|
|
2
|
+
|
|
3
|
+
⚡Lightning-fast, Lightweight 2D WebGL Rendering for HTML5.
|
|
4
|
+
gzip only 5kb ! [Why is Rapid so fast?](#Why-is-Rapid-so-fast?)
|
|
5
|
+
|
|
6
|
+
10000 Sprites 60FPS ! [Stress Test Demo](https://nightre.github.io/rapid.js/demo//stress-test/)
|
|
7
|
+
|
|
8
|
+
gzip only 5kb!
|
|
9
|
+
# Demo
|
|
10
|
+
* [Stress Test Demo](https://nightre.github.io/rapid.js/demo//stress-test/)
|
|
11
|
+
* [Graphics Demo](https://nightre.github.io/rapid.js/demo/graphic/)
|
|
12
|
+
* [MartixStack Demo](https://nightre.github.io/rapid.js/demo/matrix-stack)
|
|
13
|
+
|
|
14
|
+
# Quick Start
|
|
15
|
+
|
|
16
|
+
Installation
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
npm i rapid-render
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Or use unpkg
|
|
23
|
+
|
|
24
|
+
```html
|
|
25
|
+
<script src="https://unpkg.com/rapid-render/dist/rapid.umd.cjs"></script>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Import
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import { Rapid } from "rapid-render";
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
# Useage
|
|
35
|
+
Create Rapid.js renderer
|
|
36
|
+
```js
|
|
37
|
+
import { Rapid } from "../../dist/rapid";
|
|
38
|
+
let rap = new Rapid({
|
|
39
|
+
canvas: document.getElementById("game"),
|
|
40
|
+
width: 600,
|
|
41
|
+
height: 600,
|
|
42
|
+
backgroundColor: Color.fromHex("00FFFF")
|
|
43
|
+
})
|
|
44
|
+
rap.start() // Call rap.start() before rendering to initialize the rendering context.
|
|
45
|
+
// draw..
|
|
46
|
+
rap.end() // Call rap.end() after rendering to finalize the rendering process.
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
# Texture
|
|
50
|
+
|
|
51
|
+
create textures
|
|
52
|
+
load a texture from a URL using the `rap.texture.textureFromUrl` method or `Texture.fromUrl(rapid,"./cat")`. If you want to use ImageSource to create textures,use `Texture.fromImageSource(rapid,image)`
|
|
53
|
+
```js
|
|
54
|
+
const catTexture = await rap.texture.textureFromUrl("../cat.png") // Load a texture from a URL.
|
|
55
|
+
// or...
|
|
56
|
+
// await Texture.fromUrl(rapid,"./cat")
|
|
57
|
+
// Texture.fromImageSource(rapid,image)
|
|
58
|
+
```
|
|
59
|
+
If you only want to display a portion of the texture, you can set the clip region
|
|
60
|
+
```js
|
|
61
|
+
catTexture.setClipRegion(
|
|
62
|
+
0, // The x-coordinate of the top-left corner of the region.
|
|
63
|
+
0, // The y-coordinate of the top-left corner of the region.
|
|
64
|
+
50, // The width of the region.
|
|
65
|
+
50 // The height of the region.
|
|
66
|
+
)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
# Draw Sprite
|
|
70
|
+
Sprites are used to display textures
|
|
71
|
+
|
|
72
|
+
demo : [Sprites Demo](https://nightre.github.io/rapid.js/demo/stress-test/)
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
rap.start() // Call rap.start() before rendering to initialize the rendering context.
|
|
76
|
+
|
|
77
|
+
rap.drawSprite(texture, offsetX, offsetY)
|
|
78
|
+
|
|
79
|
+
rap.end() // Call rap.end() after rendering to finalize the rendering process.
|
|
80
|
+
```
|
|
81
|
+
# Draw Graphic
|
|
82
|
+
Graphics can be used to draw polygons
|
|
83
|
+
|
|
84
|
+
demo : [Graphics Demo](https://nightre.github.io/rapid.js/demo/graphic/)
|
|
85
|
+
```js
|
|
86
|
+
rap.start() // Call rap.start() before rendering to initialize the rendering context.
|
|
87
|
+
|
|
88
|
+
rap.drawShape([
|
|
89
|
+
0 , 0 ,
|
|
90
|
+
100, 0 ,
|
|
91
|
+
100, 50 ,
|
|
92
|
+
// The vertex of a triangle
|
|
93
|
+
], Color.fromHex("00FFFF"))
|
|
94
|
+
|
|
95
|
+
rap.end() // Call rap.end() after rendering to finalize the rendering process.
|
|
96
|
+
```
|
|
97
|
+
# MartixStack
|
|
98
|
+
|
|
99
|
+
Matrix stacking is very powerful, you can use it to implement stretching, rotation, translation, and hierarchical transformation.
|
|
100
|
+
|
|
101
|
+
demo : [MartixStack Demo](https://nightre.github.io/rapid.js/demo/matrix-stack)
|
|
102
|
+
```js
|
|
103
|
+
rap.save() // Save the current state of the transformation matrix.
|
|
104
|
+
rap.drawSprite(catTexture)
|
|
105
|
+
rap.matrix.translate(32, 32)
|
|
106
|
+
rap.matrix.rotate(1)
|
|
107
|
+
rap.save()
|
|
108
|
+
rap.matrix.scale(1, 1) // or rap.matrix.scale(1)
|
|
109
|
+
rap.restore() // Restore the transformation matrix to the previously saved state.
|
|
110
|
+
rap.drawSprite(catTexture)
|
|
111
|
+
rap.restore() // Restore the transformation matrix to the previously saved state.
|
|
112
|
+
rap.drawSprite(catTexture)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
# Custom Shader
|
|
116
|
+
|
|
117
|
+
```js
|
|
118
|
+
const shader = rap.createShader("vert shader", "frag shader")
|
|
119
|
+
rap.drawSprite(texture, offsetX, offsetY, shader)
|
|
120
|
+
rap.drawShape([
|
|
121
|
+
0 , 0 ,
|
|
122
|
+
100, 0 ,
|
|
123
|
+
100, 50 ,
|
|
124
|
+
// The vertex of a triangle
|
|
125
|
+
], Color.fromHex("00FFFF"), shader)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Customization
|
|
129
|
+
If the built-in Sprite and Graphics classes don’t meet your needs, you can create your own rendering mode with direct control over WebGL. Here’s how:
|
|
130
|
+
1. **Create a Subtype**: Start by creating a subtype that inherits from RenderRegion. This will be your custom rendering region.
|
|
131
|
+
```js
|
|
132
|
+
class YourRenderRegion extends RenderRegion {
|
|
133
|
+
// Your custom methods and properties here
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
2. **Register Your Region**: Once your custom rendering region is defined, register it with the renderer.
|
|
137
|
+
```js
|
|
138
|
+
this.registerRegion("YourRegionName", YourRenderRegion)
|
|
139
|
+
```
|
|
140
|
+
3. **Use Your Region**: Now you can set your custom region as the current region. You can also pass a custom shader if needed.
|
|
141
|
+
```js
|
|
142
|
+
this.setRegion("YourRegionName", customShader?);
|
|
143
|
+
rap.currentRegion
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
For more details, please check: [RenderRegions](./src/region)
|
|
147
|
+
|
|
148
|
+
# Why is Rapid so fast?
|
|
149
|
+
Rapid-Renderer is an efficient WebGL renderer that can process multiple sprites at once and intelligently group them based on the texture units they use and the maximum texture units supported by the GPU. This means that even if there are 1000 sprites, as long as the total texture units they use do not exceed the maximum supported by the GPU, Rapid-Renderer can group them together and render them in batches. This approach fully leverages the parallel processing capabilities of the GPU, resulting in efficient rendering. This greatly improves the rendering efficiency and allows Rapid-Renderer to maintain high rendering speed while handling a large number of sprites
|
package/dist/consts.d.ts
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import GraphicRegion from "./region/graphic_region";
|
|
2
|
+
import RenderRegion from "./region/region";
|
|
3
|
+
import SpriteRegion from "./region/sprite_region";
|
|
4
|
+
import Rapid from "./renderer";
|
|
5
|
+
export * from "./math";
|
|
6
|
+
export * from "./interface";
|
|
7
|
+
export * from "./texture";
|
|
8
|
+
export { RenderRegion, SpriteRegion, GraphicRegion, Rapid };
|
|
9
|
+
export * from "./utils/webgl";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Color } from "./math";
|
|
2
|
+
export type WebGLContext = WebGL2RenderingContext | WebGLRenderingContext;
|
|
3
|
+
export interface IRapiadOptions {
|
|
4
|
+
canvas: HTMLCanvasElement;
|
|
5
|
+
pixelDensity?: number;
|
|
6
|
+
width?: number;
|
|
7
|
+
height?: number;
|
|
8
|
+
backgroundColor?: Color;
|
|
9
|
+
}
|
|
10
|
+
export interface IRenderRegionOptions {
|
|
11
|
+
vs: string;
|
|
12
|
+
fs: string;
|
|
13
|
+
attribute: IAttribute[];
|
|
14
|
+
}
|
|
15
|
+
export interface IAttribute {
|
|
16
|
+
name: string;
|
|
17
|
+
size: number;
|
|
18
|
+
type: number;
|
|
19
|
+
normalized?: boolean;
|
|
20
|
+
stride: number;
|
|
21
|
+
offset?: number;
|
|
22
|
+
}
|
|
23
|
+
export type Images = ImageBitmap | ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | OffscreenCanvas;
|
package/dist/math.d.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { WebGLContext } from "./interface";
|
|
2
|
+
type ArrayType = typeof Float32Array | typeof Uint16Array;
|
|
3
|
+
declare class DynamicArrayBuffer {
|
|
4
|
+
protected usedElemNum: number;
|
|
5
|
+
protected typedArray: Float32Array | Uint16Array;
|
|
6
|
+
private arrayType;
|
|
7
|
+
private maxElemNum;
|
|
8
|
+
readonly bytePerElem: number;
|
|
9
|
+
constructor(arrayType: ArrayType);
|
|
10
|
+
clear(): void;
|
|
11
|
+
/**
|
|
12
|
+
* resize the array
|
|
13
|
+
* @param size
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
resize(size?: number): this | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* push a new element
|
|
19
|
+
* @param value
|
|
20
|
+
*/
|
|
21
|
+
push(value: number): void;
|
|
22
|
+
/**
|
|
23
|
+
* pop a element
|
|
24
|
+
* @param num
|
|
25
|
+
*/
|
|
26
|
+
pop(num: number): void;
|
|
27
|
+
/**
|
|
28
|
+
* get the array
|
|
29
|
+
* @param begin
|
|
30
|
+
* @param end
|
|
31
|
+
* @returns
|
|
32
|
+
*/
|
|
33
|
+
getArray(begin?: number, end?: number): Float32Array | Uint16Array;
|
|
34
|
+
/**
|
|
35
|
+
* length of the array
|
|
36
|
+
*/
|
|
37
|
+
get length(): number;
|
|
38
|
+
}
|
|
39
|
+
declare class MatrixStack extends DynamicArrayBuffer {
|
|
40
|
+
constructor();
|
|
41
|
+
/**
|
|
42
|
+
* push a matrix to the stack
|
|
43
|
+
*/
|
|
44
|
+
pushMat(): void;
|
|
45
|
+
/**
|
|
46
|
+
* pop a matrix from the stack
|
|
47
|
+
*/
|
|
48
|
+
popMat(): void;
|
|
49
|
+
/**
|
|
50
|
+
* push a matrix and indentiy it
|
|
51
|
+
*/
|
|
52
|
+
pushIdentity(): void;
|
|
53
|
+
/**
|
|
54
|
+
* Translates the current matrix by the specified x and y values.
|
|
55
|
+
* @param x - The amount to translate horizontally.
|
|
56
|
+
* @param y - The amount to translate vertically.
|
|
57
|
+
*/
|
|
58
|
+
translate(x: number, y: number): void;
|
|
59
|
+
/**
|
|
60
|
+
* Rotates the current matrix by the specified angle.
|
|
61
|
+
* @param angle - The angle, in radians, to rotate the matrix by.
|
|
62
|
+
*/
|
|
63
|
+
rotate(angle: number): void;
|
|
64
|
+
/**
|
|
65
|
+
* Multiplies the current matrix on the top of the stack by a scaling transformation.
|
|
66
|
+
* @param x - The amount to scale the matrix horizontally.
|
|
67
|
+
* @param y - The amount to scale the matrix vertically. If not specified, x is used for both horizontal and vertical scaling.
|
|
68
|
+
*/
|
|
69
|
+
scale(x: number, y?: number): void;
|
|
70
|
+
apply(x: number, y: number): number[];
|
|
71
|
+
}
|
|
72
|
+
declare class Color {
|
|
73
|
+
r: number;
|
|
74
|
+
g: number;
|
|
75
|
+
b: number;
|
|
76
|
+
a: number;
|
|
77
|
+
constructor(r: number, g: number, b: number, a: number);
|
|
78
|
+
/**
|
|
79
|
+
* is equal to another color instance
|
|
80
|
+
* @param color
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
equals(color: Color): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* create a new color by hex string
|
|
86
|
+
* @param hexString
|
|
87
|
+
* @returns
|
|
88
|
+
*/
|
|
89
|
+
static fromHex(hexString: string): Color;
|
|
90
|
+
}
|
|
91
|
+
declare class BufferArray extends DynamicArrayBuffer {
|
|
92
|
+
buffer: WebGLBuffer;
|
|
93
|
+
gl: WebGLContext;
|
|
94
|
+
readonly type: number;
|
|
95
|
+
private bufferSize;
|
|
96
|
+
constructor(gl: WebGLContext, arrayType: ArrayType, type?: number);
|
|
97
|
+
/**
|
|
98
|
+
* bind buffer to gpu
|
|
99
|
+
*/
|
|
100
|
+
bindBuffer(): void;
|
|
101
|
+
/**
|
|
102
|
+
* array data to gpu
|
|
103
|
+
*/
|
|
104
|
+
bufferData(): void;
|
|
105
|
+
/**
|
|
106
|
+
* clear
|
|
107
|
+
*/
|
|
108
|
+
clear(): void;
|
|
109
|
+
}
|
|
110
|
+
declare class ElementArray extends BufferArray {
|
|
111
|
+
readonly elemVertPerObj: number;
|
|
112
|
+
private objects;
|
|
113
|
+
readonly vertexPerObject: number;
|
|
114
|
+
private dirty;
|
|
115
|
+
constructor(gl: WebGLContext, elemVertPerObj: number, vertexPerObject: number);
|
|
116
|
+
protected addObject(_vertex?: number): void;
|
|
117
|
+
/**
|
|
118
|
+
* set number of objects
|
|
119
|
+
* @param obj
|
|
120
|
+
*/
|
|
121
|
+
setObjects(obj: number): void;
|
|
122
|
+
/**
|
|
123
|
+
* array data to gpu
|
|
124
|
+
*/
|
|
125
|
+
bufferData(): void;
|
|
126
|
+
clear(): void;
|
|
127
|
+
}
|
|
128
|
+
export { MatrixStack, DynamicArrayBuffer, Color, ElementArray, BufferArray };
|