@n3rdw1z4rd/core 0.18.4 → 0.19.0
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 +72 -10
- package/dist/astar.d.ts +44 -0
- package/dist/astar.js +118 -0
- package/dist/canvas-renderer.d.ts +154 -0
- package/dist/canvas-renderer.js +200 -0
- package/dist/clock.d.ts +69 -0
- package/dist/clock.js +108 -0
- package/dist/color-palette.d.ts +25 -0
- package/dist/color-palette.js +112 -0
- package/dist/color.d.ts +90 -0
- package/dist/color.js +224 -0
- package/dist/css/main.css +1 -3
- package/dist/disposable.d.ts +27 -0
- package/dist/disposable.js +32 -0
- package/dist/dungeon-generator.d.ts +142 -0
- package/dist/dungeon-generator.js +288 -0
- package/dist/ecs.d.ts +87 -0
- package/dist/ecs.js +101 -0
- package/dist/emitter.d.ts +40 -0
- package/dist/emitter.js +54 -0
- package/dist/event-queue.d.ts +29 -0
- package/dist/event-queue.js +40 -0
- package/dist/index.cjs +3813 -0
- package/dist/index.d.cts +2655 -0
- package/dist/index.d.ts +35 -873
- package/dist/index.js +34 -3986
- package/dist/input/action-map.d.ts +79 -0
- package/dist/input/action-map.js +131 -0
- package/dist/input/bindings.d.ts +273 -0
- package/dist/input/bindings.js +250 -0
- package/dist/input/gestures.d.ts +108 -0
- package/dist/input/gestures.js +159 -0
- package/dist/input/index.d.ts +5 -0
- package/dist/input/index.js +5 -0
- package/dist/input/input.d.ts +297 -0
- package/dist/input/input.js +695 -0
- package/dist/log.d.ts +8 -0
- package/dist/log.js +9 -0
- package/dist/map2d.d.ts +83 -0
- package/dist/map2d.js +120 -0
- package/dist/map3d-CHDJiq5T.d.cts +236 -0
- package/dist/map3d.d.ts +94 -0
- package/dist/map3d.js +148 -0
- package/dist/math.d.ts +176 -0
- package/dist/math.js +206 -0
- package/dist/misc.d.ts +46 -0
- package/dist/misc.js +71 -0
- package/dist/observable.d.ts +43 -0
- package/dist/observable.js +53 -0
- package/dist/particle-renderer.d.ts +47 -0
- package/dist/particle-renderer.js +82 -0
- package/dist/particle-system-2d.d.ts +60 -0
- package/dist/particle-system-2d.js +147 -0
- package/dist/random.d.ts +46 -0
- package/dist/random.js +74 -0
- package/dist/rectangle.d.ts +44 -0
- package/dist/rectangle.js +69 -0
- package/dist/rng.d.ts +72 -0
- package/dist/rng.js +152 -0
- package/dist/signal.d.ts +34 -0
- package/dist/signal.js +41 -0
- package/dist/spatial-partition-2d.d.ts +42 -0
- package/dist/spatial-partition-2d.js +52 -0
- package/dist/spatial-partition-3d-Ba-VBveM.cjs +1324 -0
- package/dist/spatial-partition-3d.d.ts +44 -0
- package/dist/spatial-partition-3d.js +57 -0
- package/dist/three/asteroid-mesh.d.ts +41 -0
- package/dist/three/asteroid-mesh.js +44 -0
- package/dist/three/camera-rig.d.ts +140 -0
- package/dist/three/camera-rig.js +197 -0
- package/dist/three/cube-sphere-geometry.d.ts +55 -0
- package/dist/three/cube-sphere-geometry.js +239 -0
- package/dist/three/ecs.d.ts +75 -0
- package/dist/three/ecs.js +64 -0
- package/dist/three/index.d.ts +12 -0
- package/dist/three/index.js +12 -0
- package/dist/three/particle-system-3d.d.ts +53 -0
- package/dist/three/particle-system-3d.js +224 -0
- package/dist/three/renderer.d.ts +38 -0
- package/dist/three/renderer.js +57 -0
- package/dist/three/starfield.d.ts +23 -0
- package/dist/three/starfield.js +64 -0
- package/dist/three/texture-atlas.d.ts +76 -0
- package/dist/three/texture-atlas.js +84 -0
- package/dist/three/texture-utils.d.ts +31 -0
- package/dist/three/texture-utils.js +44 -0
- package/dist/three/voxel-geometry.d.ts +72 -0
- package/dist/three/voxel-geometry.js +231 -0
- package/dist/three/voxel-mesh.d.ts +67 -0
- package/dist/three/voxel-mesh.js +71 -0
- package/dist/three.cjs +1307 -0
- package/dist/three.d.cts +655 -0
- package/dist/tilemap.d.ts +17 -0
- package/dist/tilemap.js +26 -0
- package/dist/types.d.ts +34 -0
- package/dist/types.js +0 -0
- package/dist/vector.d.ts +78 -0
- package/dist/vector.js +169 -0
- package/dist/webgl-renderer.d.ts +89 -0
- package/dist/webgl-renderer.js +140 -0
- package/dist/webgl.d.ts +357 -0
- package/dist/webgl.js +628 -0
- package/dist/worker-interface.d.ts +30 -0
- package/dist/worker-interface.js +39 -0
- package/package.json +55 -13
- package/dist/css/index.ts +0 -1
- package/dist/index.css +0 -104
- package/dist/index.d.mts +0 -873
- package/dist/index.mjs +0 -3982
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Any object with numeric `x` and `y`, such as a {@link Vector}.
|
|
4
|
+
* @property x horizontal coordinate
|
|
5
|
+
* @property y vertical coordinate
|
|
6
|
+
* @category Math & Random
|
|
7
|
+
*/
|
|
8
|
+
export interface XY {
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Any object with numeric `x`, `y` and `z`.
|
|
14
|
+
* @property x x coordinate
|
|
15
|
+
* @property y y coordinate
|
|
16
|
+
* @property z z coordinate
|
|
17
|
+
* @category Math & Random
|
|
18
|
+
*/
|
|
19
|
+
export interface XYZ {
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
z: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Plain object with string keys and values of any type.
|
|
26
|
+
* @category Math & Random
|
|
27
|
+
*/
|
|
28
|
+
export type KeyValue = Record<string, any>;
|
|
29
|
+
/**
|
|
30
|
+
* matches simplex-noise's NoiseFunction3D, e.g. createNoise3D(() => rng.nextFloat())
|
|
31
|
+
* @category Math & Random
|
|
32
|
+
*/
|
|
33
|
+
export type NoiseFunction3D = (x: number, y: number, z: number) => number;
|
|
34
|
+
//#endregion
|
package/dist/types.js
ADDED
|
File without changes
|
package/dist/vector.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { XY } from "./types.js";
|
|
2
|
+
//#region src/vector.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Mutable 2D vector. Most methods modify the vector in place and return `this` for chaining;
|
|
5
|
+
* {@link Vector.clone} and {@link Vector.floored} return new vectors.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* const v = new Vector(3, 4);
|
|
9
|
+
* v.length(); // 5
|
|
10
|
+
* v.clone().normalize().times(10); // (6, 8), v unchanged
|
|
11
|
+
* v.plus(1).minus(new Vector(0, 2)); // v is now (4, 3)
|
|
12
|
+
* ```
|
|
13
|
+
* @category Math & Random
|
|
14
|
+
*/
|
|
15
|
+
export declare class Vector {
|
|
16
|
+
/** horizontal component */
|
|
17
|
+
x: number;
|
|
18
|
+
/** vertical component (down in screen coordinates) */
|
|
19
|
+
y: number;
|
|
20
|
+
constructor(x?: number, y?: number);
|
|
21
|
+
/** A new vector with the same components. */
|
|
22
|
+
clone(): Vector;
|
|
23
|
+
/** Copies `that`'s components into this vector. */
|
|
24
|
+
copy(that: Vector): this;
|
|
25
|
+
/** Writes this vector's components into `target`; returns this vector, not `target`. */
|
|
26
|
+
copyTo(target: {
|
|
27
|
+
x: number;
|
|
28
|
+
y: number;
|
|
29
|
+
}): this;
|
|
30
|
+
/** Sets both components. */
|
|
31
|
+
set(x: number, y: number): this;
|
|
32
|
+
/** Exact (`===`) component-wise equality. */
|
|
33
|
+
equals(that: Vector): boolean;
|
|
34
|
+
/** Euclidean distance to `that`. */
|
|
35
|
+
distanceTo(that: Vector): number;
|
|
36
|
+
/** Euclidean length (magnitude). */
|
|
37
|
+
length(): number;
|
|
38
|
+
/** Scales to unit length in place; a zero vector is left unchanged. */
|
|
39
|
+
normalize(): this;
|
|
40
|
+
/** Scales to the given length in place, keeping the direction; a zero vector stays zero. */
|
|
41
|
+
setLength(length: number): this;
|
|
42
|
+
/** Dot product with `that`. */
|
|
43
|
+
dot(that: Vector): number;
|
|
44
|
+
/** Direction in radians, `Math.atan2(y, x)`, in (-π, π]. */
|
|
45
|
+
angle(): number;
|
|
46
|
+
/** Unsigned angle between this vector and `that`, in radians in [0, π]; 0 if either is a zero vector. */
|
|
47
|
+
angleTo(that: Vector): number;
|
|
48
|
+
/** Rotates in place about the origin by `radians` (counter-clockwise with y up, clockwise on screen with y down). */
|
|
49
|
+
rotate(radians: number): this;
|
|
50
|
+
/** Rotates in place about `origin` by `radians`. */
|
|
51
|
+
rotateAround(origin: Vector, radians: number): this;
|
|
52
|
+
/** A new vector with both components floored; this vector is unchanged. */
|
|
53
|
+
floored(): Vector;
|
|
54
|
+
/** Adds in place: another vector, a scalar to both components (`plus(n)`), or separate `x` and `y` amounts. */
|
|
55
|
+
plus(x: number | Vector, y?: number): this;
|
|
56
|
+
/** Subtracts in place; arguments as for {@link Vector.plus}. */
|
|
57
|
+
minus(x: number | Vector, y?: number): this;
|
|
58
|
+
/** Multiplies component-wise in place; arguments as for {@link Vector.plus}. */
|
|
59
|
+
times(x: number | Vector, y?: number): this;
|
|
60
|
+
/**
|
|
61
|
+
* Divides component-wise in place; arguments as for {@link Vector.plus}. Plain division: a zero
|
|
62
|
+
* divisor gives `Infinity` (or `NaN` for 0 / 0), as with numbers.
|
|
63
|
+
*/
|
|
64
|
+
dividedBy(x: number | Vector, y?: number): this;
|
|
65
|
+
/** A new vector from any `{ x, y }` object. */
|
|
66
|
+
static from(value: XY): Vector;
|
|
67
|
+
/** new (0, 0) */
|
|
68
|
+
static get ZERO(): Vector;
|
|
69
|
+
/** new (0, -1): up in screen coordinates (y down) */
|
|
70
|
+
static get NORTH(): Vector;
|
|
71
|
+
/** new (0, 1): down in screen coordinates */
|
|
72
|
+
static get SOUTH(): Vector;
|
|
73
|
+
/** new (1, 0) */
|
|
74
|
+
static get EAST(): Vector;
|
|
75
|
+
/** new (-1, 0) */
|
|
76
|
+
static get WEST(): Vector;
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
package/dist/vector.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { distance2d } from "./math.js";
|
|
2
|
+
//#region src/vector.ts
|
|
3
|
+
/**
|
|
4
|
+
* Mutable 2D vector. Most methods modify the vector in place and return `this` for chaining;
|
|
5
|
+
* {@link Vector.clone} and {@link Vector.floored} return new vectors.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* const v = new Vector(3, 4);
|
|
9
|
+
* v.length(); // 5
|
|
10
|
+
* v.clone().normalize().times(10); // (6, 8), v unchanged
|
|
11
|
+
* v.plus(1).minus(new Vector(0, 2)); // v is now (4, 3)
|
|
12
|
+
* ```
|
|
13
|
+
* @category Math & Random
|
|
14
|
+
*/
|
|
15
|
+
var Vector = class Vector {
|
|
16
|
+
/** horizontal component */
|
|
17
|
+
x;
|
|
18
|
+
/** vertical component (down in screen coordinates) */
|
|
19
|
+
y;
|
|
20
|
+
constructor(x = 0, y = 0) {
|
|
21
|
+
this.x = x;
|
|
22
|
+
this.y = y;
|
|
23
|
+
}
|
|
24
|
+
/** A new vector with the same components. */
|
|
25
|
+
clone() {
|
|
26
|
+
return new Vector(this.x, this.y);
|
|
27
|
+
}
|
|
28
|
+
/** Copies `that`'s components into this vector. */
|
|
29
|
+
copy(that) {
|
|
30
|
+
this.x = that.x;
|
|
31
|
+
this.y = that.y;
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
/** Writes this vector's components into `target`; returns this vector, not `target`. */
|
|
35
|
+
copyTo(target) {
|
|
36
|
+
target.x = this.x;
|
|
37
|
+
target.y = this.y;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
/** Sets both components. */
|
|
41
|
+
set(x, y) {
|
|
42
|
+
this.x = x;
|
|
43
|
+
this.y = y;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/** Exact (`===`) component-wise equality. */
|
|
47
|
+
equals(that) {
|
|
48
|
+
return this.x === that.x && this.y === that.y;
|
|
49
|
+
}
|
|
50
|
+
/** Euclidean distance to `that`. */
|
|
51
|
+
distanceTo(that) {
|
|
52
|
+
return distance2d(this.x, this.y, that.x, that.y);
|
|
53
|
+
}
|
|
54
|
+
/** Euclidean length (magnitude). */
|
|
55
|
+
length() {
|
|
56
|
+
return Math.hypot(this.x, this.y);
|
|
57
|
+
}
|
|
58
|
+
/** Scales to unit length in place; a zero vector is left unchanged. */
|
|
59
|
+
normalize() {
|
|
60
|
+
const length = this.length();
|
|
61
|
+
if (length > 0) this.dividedBy(length);
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
/** Scales to the given length in place, keeping the direction; a zero vector stays zero. */
|
|
65
|
+
setLength(length) {
|
|
66
|
+
return this.normalize().times(length);
|
|
67
|
+
}
|
|
68
|
+
/** Dot product with `that`. */
|
|
69
|
+
dot(that) {
|
|
70
|
+
return this.x * that.x + this.y * that.y;
|
|
71
|
+
}
|
|
72
|
+
/** Direction in radians, `Math.atan2(y, x)`, in (-π, π]. */
|
|
73
|
+
angle() {
|
|
74
|
+
return Math.atan2(this.y, this.x);
|
|
75
|
+
}
|
|
76
|
+
/** Unsigned angle between this vector and `that`, in radians in [0, π]; 0 if either is a zero vector. */
|
|
77
|
+
angleTo(that) {
|
|
78
|
+
const denominator = this.length() * that.length();
|
|
79
|
+
if (denominator === 0) return 0;
|
|
80
|
+
const theta = this.dot(that) / denominator;
|
|
81
|
+
return Math.acos(Math.min(1, Math.max(-1, theta)));
|
|
82
|
+
}
|
|
83
|
+
/** Rotates in place about the origin by `radians` (counter-clockwise with y up, clockwise on screen with y down). */
|
|
84
|
+
rotate(radians) {
|
|
85
|
+
const cos = Math.cos(radians);
|
|
86
|
+
const sin = Math.sin(radians);
|
|
87
|
+
const { x, y } = this;
|
|
88
|
+
this.x = x * cos - y * sin;
|
|
89
|
+
this.y = x * sin + y * cos;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
/** Rotates in place about `origin` by `radians`. */
|
|
93
|
+
rotateAround(origin, radians) {
|
|
94
|
+
return this.minus(origin).rotate(radians).plus(origin);
|
|
95
|
+
}
|
|
96
|
+
/** A new vector with both components floored; this vector is unchanged. */
|
|
97
|
+
floored() {
|
|
98
|
+
return new Vector(Math.floor(this.x), Math.floor(this.y));
|
|
99
|
+
}
|
|
100
|
+
/** Adds in place: another vector, a scalar to both components (`plus(n)`), or separate `x` and `y` amounts. */
|
|
101
|
+
plus(x, y) {
|
|
102
|
+
if (x instanceof Vector) {
|
|
103
|
+
y = x.y;
|
|
104
|
+
x = x.x;
|
|
105
|
+
} else if (y === void 0) y = x;
|
|
106
|
+
this.x += x;
|
|
107
|
+
this.y += y;
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
/** Subtracts in place; arguments as for {@link Vector.plus}. */
|
|
111
|
+
minus(x, y) {
|
|
112
|
+
if (x instanceof Vector) {
|
|
113
|
+
y = x.y;
|
|
114
|
+
x = x.x;
|
|
115
|
+
} else if (y === void 0) y = x;
|
|
116
|
+
this.x -= x;
|
|
117
|
+
this.y -= y;
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
/** Multiplies component-wise in place; arguments as for {@link Vector.plus}. */
|
|
121
|
+
times(x, y) {
|
|
122
|
+
if (x instanceof Vector) {
|
|
123
|
+
y = x.y;
|
|
124
|
+
x = x.x;
|
|
125
|
+
} else if (y === void 0) y = x;
|
|
126
|
+
this.x *= x;
|
|
127
|
+
this.y *= y;
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Divides component-wise in place; arguments as for {@link Vector.plus}. Plain division: a zero
|
|
132
|
+
* divisor gives `Infinity` (or `NaN` for 0 / 0), as with numbers.
|
|
133
|
+
*/
|
|
134
|
+
dividedBy(x, y) {
|
|
135
|
+
if (x instanceof Vector) {
|
|
136
|
+
y = x.y;
|
|
137
|
+
x = x.x;
|
|
138
|
+
} else if (y === void 0) y = x;
|
|
139
|
+
this.x /= x;
|
|
140
|
+
this.y /= y;
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
/** A new vector from any `{ x, y }` object. */
|
|
144
|
+
static from(value) {
|
|
145
|
+
return new Vector(value.x, value.y);
|
|
146
|
+
}
|
|
147
|
+
/** new (0, 0) */
|
|
148
|
+
static get ZERO() {
|
|
149
|
+
return new Vector(0, 0);
|
|
150
|
+
}
|
|
151
|
+
/** new (0, -1): up in screen coordinates (y down) */
|
|
152
|
+
static get NORTH() {
|
|
153
|
+
return new Vector(0, -1);
|
|
154
|
+
}
|
|
155
|
+
/** new (0, 1): down in screen coordinates */
|
|
156
|
+
static get SOUTH() {
|
|
157
|
+
return new Vector(0, 1);
|
|
158
|
+
}
|
|
159
|
+
/** new (1, 0) */
|
|
160
|
+
static get EAST() {
|
|
161
|
+
return new Vector(1, 0);
|
|
162
|
+
}
|
|
163
|
+
/** new (-1, 0) */
|
|
164
|
+
static get WEST() {
|
|
165
|
+
return new Vector(-1, 0);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
//#endregion
|
|
169
|
+
export { Vector };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Observable } from "./observable.js";
|
|
2
|
+
import { DrawOptions, ProgramInfo, RenderTarget, VertexArrayInfo } from "./webgl.js";
|
|
3
|
+
//#region src/webgl-renderer.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Options for the {@link WebGlRenderer} constructor.
|
|
6
|
+
*
|
|
7
|
+
* @category WebGL
|
|
8
|
+
*/
|
|
9
|
+
export interface RendererParams {
|
|
10
|
+
/** default: a new canvas */
|
|
11
|
+
canvas?: HTMLCanvasElement;
|
|
12
|
+
/** append the canvas here (and size it to the parent) */
|
|
13
|
+
parent?: HTMLElement;
|
|
14
|
+
/** antialias, alpha, preserveDrawingBuffer, ... */
|
|
15
|
+
attributes?: WebGLContextAttributes;
|
|
16
|
+
/** default: window.devicePixelRatio */
|
|
17
|
+
pixelRatio?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A thin owner of a WebGL2 context: sizing (CSS pixels x pixel ratio), clearing, render targets,
|
|
21
|
+
* drawing, and context-loss notifications. Everything else is the webgl.ts wrappers on `gl`.
|
|
22
|
+
* There is no render loop and no resource tracking: run your own loop (e.g. with Clock) and
|
|
23
|
+
* delete what you create.
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* const renderer = new WebGlRenderer({ parent: document.body });
|
|
27
|
+
* const program = renderer.createProgram(vertexSource, fragmentSource);
|
|
28
|
+
* const quad = CreateVertexArray(renderer.gl, program, {
|
|
29
|
+
* aPosition: { buffer: CreateBuffer(renderer.gl, [-1, -1, 1, -1, -1, 1, 1, 1]), size: 2 },
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* new Clock().start((dt, time) => {
|
|
33
|
+
* renderer.clear();
|
|
34
|
+
* renderer.draw(program, quad, { mode: renderer.gl.TRIANGLE_STRIP, count: 4, uniforms: { uTime: time } });
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @category WebGL
|
|
39
|
+
*/
|
|
40
|
+
export declare class WebGlRenderer {
|
|
41
|
+
/** the WebGL2 context; use it with the {@link CreateBuffer}, {@link CreateTexture}, ... helpers */
|
|
42
|
+
readonly gl: WebGL2RenderingContext;
|
|
43
|
+
/** drawing buffer pixels per CSS pixel; applied on the next `resize()` */
|
|
44
|
+
pixelRatio: number;
|
|
45
|
+
/**
|
|
46
|
+
* The GPU context was lost (driver reset, too many contexts, ...): every program, buffer and
|
|
47
|
+
* texture is gone. Stop drawing until onContextRestored, then recreate your resources.
|
|
48
|
+
*/
|
|
49
|
+
readonly onContextLost: Observable<void>;
|
|
50
|
+
/** The GPU context is back after {@link WebGlRenderer.onContextLost}: recreate programs, buffers and textures. */
|
|
51
|
+
readonly onContextRestored: Observable<void>;
|
|
52
|
+
private _width;
|
|
53
|
+
private _height;
|
|
54
|
+
private readonly _listeners;
|
|
55
|
+
/** the canvas the context draws on */
|
|
56
|
+
get canvas(): HTMLCanvasElement;
|
|
57
|
+
/** Size in CSS pixels (the drawing buffer is this times `pixelRatio`). */
|
|
58
|
+
get width(): number;
|
|
59
|
+
/** Height in CSS pixels. */
|
|
60
|
+
get height(): number;
|
|
61
|
+
/** `width / height`, or 1 while the height is 0 */
|
|
62
|
+
get aspectRatio(): number;
|
|
63
|
+
/** whether the GPU context is currently lost (`draw()` does nothing meanwhile) */
|
|
64
|
+
get isContextLost(): boolean;
|
|
65
|
+
/** Creates the context (throws without WebGL2), enables depth testing, and appends to `params.parent` if given. */
|
|
66
|
+
constructor(params?: RendererParams);
|
|
67
|
+
/** Moves the canvas into `element` (or out of the page, without one) and resizes to fit. */
|
|
68
|
+
appendTo(element?: HTMLElement): void;
|
|
69
|
+
/**
|
|
70
|
+
* Sizes the canvas to `width` x `height` CSS pixels (default: its parent's size); the drawing
|
|
71
|
+
* buffer gets the pixel ratio applied. Returns whether anything changed.
|
|
72
|
+
*/
|
|
73
|
+
resize(width?: number, height?: number): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Clears the bound target to `color` (RGBA in 0..1, alpha default 1) and, with `depth`
|
|
76
|
+
* (default true), also the depth and stencil buffers.
|
|
77
|
+
*/
|
|
78
|
+
clear(color?: ArrayLike<number>, depth?: boolean): void;
|
|
79
|
+
/** Draws into `target`, or the canvas with null. */
|
|
80
|
+
setRenderTarget(target: RenderTarget | null): void;
|
|
81
|
+
/** Compiles and links a program and collects its attributes and uniforms ({@link CreateProgramInfo}). Throws on errors. */
|
|
82
|
+
createProgram(vertexShaderSource: string, fragmentShaderSource: string): ProgramInfo;
|
|
83
|
+
/** Draws with {@link Draw}; skipped while the context is lost. */
|
|
84
|
+
draw(info: ProgramInfo, vertexArray: VertexArrayInfo, options?: DrawOptions): void;
|
|
85
|
+
/** Removes the renderer's event listeners. GPU resources you created are yours to delete. */
|
|
86
|
+
dispose(): void;
|
|
87
|
+
private _listen;
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { Observable } from "./observable.js";
|
|
2
|
+
import { BindRenderTarget, CreateProgramInfo, CreateWebGlContext, Draw, ResizeWebGlContext } from "./webgl.js";
|
|
3
|
+
//#region src/webgl-renderer.ts
|
|
4
|
+
/**
|
|
5
|
+
* A thin owner of a WebGL2 context: sizing (CSS pixels x pixel ratio), clearing, render targets,
|
|
6
|
+
* drawing, and context-loss notifications. Everything else is the webgl.ts wrappers on `gl`.
|
|
7
|
+
* There is no render loop and no resource tracking: run your own loop (e.g. with Clock) and
|
|
8
|
+
* delete what you create.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const renderer = new WebGlRenderer({ parent: document.body });
|
|
12
|
+
* const program = renderer.createProgram(vertexSource, fragmentSource);
|
|
13
|
+
* const quad = CreateVertexArray(renderer.gl, program, {
|
|
14
|
+
* aPosition: { buffer: CreateBuffer(renderer.gl, [-1, -1, 1, -1, -1, 1, 1, 1]), size: 2 },
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* new Clock().start((dt, time) => {
|
|
18
|
+
* renderer.clear();
|
|
19
|
+
* renderer.draw(program, quad, { mode: renderer.gl.TRIANGLE_STRIP, count: 4, uniforms: { uTime: time } });
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @category WebGL
|
|
24
|
+
*/
|
|
25
|
+
var WebGlRenderer = class {
|
|
26
|
+
/** the WebGL2 context; use it with the {@link CreateBuffer}, {@link CreateTexture}, ... helpers */
|
|
27
|
+
gl;
|
|
28
|
+
/** drawing buffer pixels per CSS pixel; applied on the next `resize()` */
|
|
29
|
+
pixelRatio;
|
|
30
|
+
/**
|
|
31
|
+
* The GPU context was lost (driver reset, too many contexts, ...): every program, buffer and
|
|
32
|
+
* texture is gone. Stop drawing until onContextRestored, then recreate your resources.
|
|
33
|
+
*/
|
|
34
|
+
onContextLost = new Observable();
|
|
35
|
+
/** The GPU context is back after {@link WebGlRenderer.onContextLost}: recreate programs, buffers and textures. */
|
|
36
|
+
onContextRestored = new Observable();
|
|
37
|
+
_width = 0;
|
|
38
|
+
_height = 0;
|
|
39
|
+
_listeners = [];
|
|
40
|
+
/** the canvas the context draws on */
|
|
41
|
+
get canvas() {
|
|
42
|
+
return this.gl.canvas;
|
|
43
|
+
}
|
|
44
|
+
/** Size in CSS pixels (the drawing buffer is this times `pixelRatio`). */
|
|
45
|
+
get width() {
|
|
46
|
+
return this._width;
|
|
47
|
+
}
|
|
48
|
+
/** Height in CSS pixels. */
|
|
49
|
+
get height() {
|
|
50
|
+
return this._height;
|
|
51
|
+
}
|
|
52
|
+
/** `width / height`, or 1 while the height is 0 */
|
|
53
|
+
get aspectRatio() {
|
|
54
|
+
return this._height > 0 ? this._width / this._height : 1;
|
|
55
|
+
}
|
|
56
|
+
/** whether the GPU context is currently lost (`draw()` does nothing meanwhile) */
|
|
57
|
+
get isContextLost() {
|
|
58
|
+
return this.gl.isContextLost();
|
|
59
|
+
}
|
|
60
|
+
/** Creates the context (throws without WebGL2), enables depth testing, and appends to `params.parent` if given. */
|
|
61
|
+
constructor(params = {}) {
|
|
62
|
+
this.gl = CreateWebGlContext(params.canvas, params.attributes);
|
|
63
|
+
this.pixelRatio = params.pixelRatio ?? globalThis.devicePixelRatio ?? 1;
|
|
64
|
+
this._width = this.canvas.width / this.pixelRatio;
|
|
65
|
+
this._height = this.canvas.height / this.pixelRatio;
|
|
66
|
+
this.gl.enable(this.gl.DEPTH_TEST);
|
|
67
|
+
this._listen("webglcontextlost", (event) => {
|
|
68
|
+
event.preventDefault();
|
|
69
|
+
this.onContextLost.notify();
|
|
70
|
+
});
|
|
71
|
+
this._listen("webglcontextrestored", () => {
|
|
72
|
+
this.gl.enable(this.gl.DEPTH_TEST);
|
|
73
|
+
this.onContextRestored.notify();
|
|
74
|
+
});
|
|
75
|
+
if (params.parent) this.appendTo(params.parent);
|
|
76
|
+
}
|
|
77
|
+
/** Moves the canvas into `element` (or out of the page, without one) and resizes to fit. */
|
|
78
|
+
appendTo(element) {
|
|
79
|
+
this.canvas.remove();
|
|
80
|
+
if (element) {
|
|
81
|
+
element.appendChild(this.canvas);
|
|
82
|
+
this.resize();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Sizes the canvas to `width` x `height` CSS pixels (default: its parent's size); the drawing
|
|
87
|
+
* buffer gets the pixel ratio applied. Returns whether anything changed.
|
|
88
|
+
*/
|
|
89
|
+
resize(width, height) {
|
|
90
|
+
if (width === void 0 || height === void 0) {
|
|
91
|
+
const rect = this.canvas.parentElement?.getBoundingClientRect() ?? this.canvas.getBoundingClientRect();
|
|
92
|
+
width ??= rect.width;
|
|
93
|
+
height ??= rect.height;
|
|
94
|
+
}
|
|
95
|
+
const changed = width !== this._width || height !== this._height;
|
|
96
|
+
this._width = width;
|
|
97
|
+
this._height = height;
|
|
98
|
+
this.canvas.style.width = `${width}px`;
|
|
99
|
+
this.canvas.style.height = `${height}px`;
|
|
100
|
+
return ResizeWebGlContext(this.gl, width, height, this.pixelRatio) || changed;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Clears the bound target to `color` (RGBA in 0..1, alpha default 1) and, with `depth`
|
|
104
|
+
* (default true), also the depth and stencil buffers.
|
|
105
|
+
*/
|
|
106
|
+
clear(color = [
|
|
107
|
+
0,
|
|
108
|
+
0,
|
|
109
|
+
0,
|
|
110
|
+
1
|
|
111
|
+
], depth = true) {
|
|
112
|
+
const gl = this.gl;
|
|
113
|
+
gl.clearColor(color[0], color[1], color[2], color[3] ?? 1);
|
|
114
|
+
gl.clear(gl.COLOR_BUFFER_BIT | (depth ? gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT : 0));
|
|
115
|
+
}
|
|
116
|
+
/** Draws into `target`, or the canvas with null. */
|
|
117
|
+
setRenderTarget(target) {
|
|
118
|
+
BindRenderTarget(this.gl, target);
|
|
119
|
+
}
|
|
120
|
+
/** Compiles and links a program and collects its attributes and uniforms ({@link CreateProgramInfo}). Throws on errors. */
|
|
121
|
+
createProgram(vertexShaderSource, fragmentShaderSource) {
|
|
122
|
+
return CreateProgramInfo(this.gl, vertexShaderSource, fragmentShaderSource);
|
|
123
|
+
}
|
|
124
|
+
/** Draws with {@link Draw}; skipped while the context is lost. */
|
|
125
|
+
draw(info, vertexArray, options) {
|
|
126
|
+
if (this.isContextLost) return;
|
|
127
|
+
Draw(this.gl, info, vertexArray, options);
|
|
128
|
+
}
|
|
129
|
+
/** Removes the renderer's event listeners. GPU resources you created are yours to delete. */
|
|
130
|
+
dispose() {
|
|
131
|
+
for (const [type, listener] of this._listeners) this.canvas.removeEventListener(type, listener);
|
|
132
|
+
this._listeners.length = 0;
|
|
133
|
+
}
|
|
134
|
+
_listen(type, listener) {
|
|
135
|
+
this.canvas.addEventListener(type, listener);
|
|
136
|
+
this._listeners.push([type, listener]);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
//#endregion
|
|
140
|
+
export { WebGlRenderer };
|