webglmusti 0.0.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/.gitattributes +2 -0
- package/idea_list.txt +13 -0
- package/index.html +17 -0
- package/package.json +20 -0
- package/public/apple.png +0 -0
- package/public/base.png +0 -0
- package/public/car.png +0 -0
- package/public/drop.png +0 -0
- package/public/font16-Sheet.png +0 -0
- package/public/font16-Sheet.txt +52 -0
- package/public/letters-Sheet.png +0 -0
- package/public/letters-Sheet.txt +27 -0
- package/public/letters_Sheet.png +0 -0
- package/public/letters_Sheet.txt +4 -0
- package/public/vite.svg +1 -0
- package/src/App/ResourceSim/car.ts +172 -0
- package/src/App/ResourceSim/grid.ts +993 -0
- package/src/App/ResourceSim/grid_app.ts +1326 -0
- package/src/App/ResourceSim/grid_test.test.ts +18 -0
- package/src/App/ResourceSim/node_graph.ts +293 -0
- package/src/App/ResourceSim/nodes.ts +151 -0
- package/src/App/ResourceSim/resource.ts +8 -0
- package/src/App/ResourceSim/texts.ts +6 -0
- package/src/App/card/card.test.ts +22 -0
- package/src/App/card/card.ts +763 -0
- package/src/App/puzzle_box/app.ts +10 -0
- package/src/App/puzzle_box/engine.ts +374 -0
- package/src/App/puzzle_box/renderer.ts +102 -0
- package/src/App/puzzle_box/test.test.ts +49 -0
- package/src/App/water/water.ts +206 -0
- package/src/Interface/button.ts +345 -0
- package/src/Interface/interface_element.ts +30 -0
- package/src/Interface/internal_window.ts +100 -0
- package/src/Interface/options.ts +332 -0
- package/src/Interface/text_input.ts +183 -0
- package/src/WebGL/Matrix/matrix.test.ts +30 -0
- package/src/WebGL/Matrix/matrix.ts +293 -0
- package/src/WebGL/Shaders/Fragment/Program/circle.ts +49 -0
- package/src/WebGL/Shaders/Fragment/Program/circle_only.ts +47 -0
- package/src/WebGL/Shaders/Fragment/Program/circle_outline.ts +54 -0
- package/src/WebGL/Shaders/Fragment/Program/colour.ts +39 -0
- package/src/WebGL/Shaders/Fragment/Program/colour_alpha.ts +37 -0
- package/src/WebGL/Shaders/Fragment/Program/line.ts +39 -0
- package/src/WebGL/Shaders/Fragment/Program/multi_colour_centre_circle_path.ts +69 -0
- package/src/WebGL/Shaders/Fragment/Program/multi_colour_path.ts +69 -0
- package/src/WebGL/Shaders/Fragment/Program/path_centre_circle.ts +69 -0
- package/src/WebGL/Shaders/Fragment/Program/rect_outline.ts +39 -0
- package/src/WebGL/Shaders/Fragment/Program/solid_path.ts +64 -0
- package/src/WebGL/Shaders/Fragment/Program/sprite_sheet.ts +54 -0
- package/src/WebGL/Shaders/Fragment/Program/sprite_sheet_colour.ts +62 -0
- package/src/WebGL/Shaders/Fragment/Program/texture.ts +34 -0
- package/src/WebGL/Shaders/Fragment/Source/circle.frag +19 -0
- package/src/WebGL/Shaders/Fragment/Source/circle_only.frag +15 -0
- package/src/WebGL/Shaders/Fragment/Source/circle_outline.frag +23 -0
- package/src/WebGL/Shaders/Fragment/Source/colour.frag +9 -0
- package/src/WebGL/Shaders/Fragment/Source/colour_alpha.frag +7 -0
- package/src/WebGL/Shaders/Fragment/Source/fragment.frag +5 -0
- package/src/WebGL/Shaders/Fragment/Source/fragment_source.ts +15 -0
- package/src/WebGL/Shaders/Fragment/Source/line.frag +12 -0
- package/src/WebGL/Shaders/Fragment/Source/multi_colour_centre_circle_path.frag +36 -0
- package/src/WebGL/Shaders/Fragment/Source/multi_colour_path.frag +34 -0
- package/src/WebGL/Shaders/Fragment/Source/path_centre_circle.frag +29 -0
- package/src/WebGL/Shaders/Fragment/Source/rect_outline.frag +20 -0
- package/src/WebGL/Shaders/Fragment/Source/solid_path.frag +24 -0
- package/src/WebGL/Shaders/Fragment/Source/sprite_sheet.frag +17 -0
- package/src/WebGL/Shaders/Fragment/Source/sprite_sheet_colour.frag +19 -0
- package/src/WebGL/Shaders/Fragment/Source/texture.frag +11 -0
- package/src/WebGL/Shaders/Fragment/fragment.ts +43 -0
- package/src/WebGL/Shaders/Fragment/fragment_old.ts +261 -0
- package/src/WebGL/Shaders/Vertex/Program/mvp2d.ts +41 -0
- package/src/WebGL/Shaders/Vertex/Program/transform2d.ts +39 -0
- package/src/WebGL/Shaders/Vertex/Source/mvp2d.vert +17 -0
- package/src/WebGL/Shaders/Vertex/Source/mvp_i_2d.vert +18 -0
- package/src/WebGL/Shaders/Vertex/Source/simple.vert +9 -0
- package/src/WebGL/Shaders/Vertex/Source/transform2d.vert +13 -0
- package/src/WebGL/Shaders/Vertex/Source/transform2d_rel.vert +16 -0
- package/src/WebGL/Shaders/Vertex/Source/translate2d.vert +7 -0
- package/src/WebGL/Shaders/Vertex/Source/vertex_source.ts +13 -0
- package/src/WebGL/Shaders/Vertex/vertex.ts +10 -0
- package/src/WebGL/Shaders/Vertex/vertex_old.ts +289 -0
- package/src/WebGL/Shaders/custom.ts +129 -0
- package/src/WebGL/Shaders/shader.ts +197 -0
- package/src/WebGL/Shapes/Line.ts +29 -0
- package/src/WebGL/Shapes/Shapes.ts +189 -0
- package/src/WebGL/Texture/texture.ts +214 -0
- package/src/WebGL/Util/file.ts +24 -0
- package/src/WebGL/app.ts +150 -0
- package/src/WebGL/colour.ts +71 -0
- package/src/WebGL/globals.ts +353 -0
- package/src/WebGL/index.ts +3 -0
- package/src/WebGL/mixin.ts +2 -0
- package/src/global.d.ts +13 -0
- package/src/index.ts +4 -0
- package/src/main.ts +139 -0
- package/src/utils/array.ts +154 -0
- package/src/utils/assert.ts +8 -0
- package/src/utils/file.ts +0 -0
- package/src/utils/mixin.ts +22 -0
- package/src/utils/numbers.ts +11 -0
- package/src/utils/utils.test.ts +23 -0
- package/src/vite-env.d.ts +1 -0
- package/tsconfig.json +25 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import WebGL from '../globals';
|
|
2
|
+
|
|
3
|
+
export class RightTriangle{
|
|
4
|
+
private static positions = new Float32Array([
|
|
5
|
+
0, 0,
|
|
6
|
+
0, 0.5,
|
|
7
|
+
0.5, 0
|
|
8
|
+
]);
|
|
9
|
+
private static positionBuffer: WebGLBuffer | undefined;
|
|
10
|
+
//private static p = new
|
|
11
|
+
static draw(){
|
|
12
|
+
if(!RightTriangle.positionBuffer) RightTriangle.setup();
|
|
13
|
+
if(WebGL.gl){
|
|
14
|
+
const gl = WebGL.gl;
|
|
15
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, RightTriangle.positionBuffer!);
|
|
16
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); // changes the bind buffers
|
|
17
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
private static setup(){
|
|
22
|
+
if(WebGL.gl){
|
|
23
|
+
const gl = WebGL.gl;
|
|
24
|
+
RightTriangle.positionBuffer = gl.createBuffer();
|
|
25
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, RightTriangle.positionBuffer);
|
|
26
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(RightTriangle.positions), gl.STATIC_DRAW);
|
|
27
|
+
gl.enableVertexAttribArray(0);
|
|
28
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
|
|
29
|
+
}else{
|
|
30
|
+
throw new Error("WebGL globals not initialised");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class Quad{
|
|
36
|
+
private static positions = new Float32Array([
|
|
37
|
+
0, 0,
|
|
38
|
+
1, 0,
|
|
39
|
+
0, 1,
|
|
40
|
+
1, 1
|
|
41
|
+
]);
|
|
42
|
+
private static positionBuffer: WebGLBuffer | undefined;
|
|
43
|
+
private static indices = new Uint16Array([0,1,2, 2,1,3]);
|
|
44
|
+
private static indicesBuffer: WebGLBuffer | undefined;
|
|
45
|
+
|
|
46
|
+
private static positionsArray = new Float32Array([
|
|
47
|
+
0, 0,
|
|
48
|
+
1, 0,
|
|
49
|
+
0, 1,
|
|
50
|
+
0, 1,
|
|
51
|
+
1, 0,
|
|
52
|
+
1, 1
|
|
53
|
+
]);
|
|
54
|
+
private static positionsArrayBuffer: WebGLBuffer | undefined;
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
//private static p = new
|
|
58
|
+
static draw(){
|
|
59
|
+
if(!Quad.positionBuffer) Quad.setup();
|
|
60
|
+
if(WebGL.gl){
|
|
61
|
+
const gl = WebGL.gl;
|
|
62
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Quad.indicesBuffer!);
|
|
63
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, Quad.positionBuffer!);
|
|
64
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); // changes the bind buffers
|
|
65
|
+
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static drawArrays(){
|
|
70
|
+
if(!Quad.positionsArrayBuffer) Quad.setup();
|
|
71
|
+
if(WebGL.gl){
|
|
72
|
+
const gl = WebGL.gl;
|
|
73
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, Quad.positionsArrayBuffer!);
|
|
74
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
|
|
75
|
+
gl.drawArrays(gl.TRIANGLES, 0, 6);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static drawRelative(){
|
|
80
|
+
if(!Quad.positionBuffer) Quad.setup();
|
|
81
|
+
if(WebGL.gl){
|
|
82
|
+
const gl = WebGL.gl;
|
|
83
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Quad.indicesBuffer!);
|
|
84
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, Quad.positionBuffer!);
|
|
85
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); // changes the bind buffers
|
|
86
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, Quad.positionBuffer!);
|
|
87
|
+
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0); // changes the bind buffers
|
|
88
|
+
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private static setup(){
|
|
93
|
+
if(WebGL.gl){
|
|
94
|
+
const gl = WebGL.gl;
|
|
95
|
+
Quad.positionBuffer = gl.createBuffer();
|
|
96
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, Quad.positionBuffer);
|
|
97
|
+
gl.bufferData(gl.ARRAY_BUFFER, Quad.positions, gl.STATIC_DRAW);
|
|
98
|
+
|
|
99
|
+
Quad.indicesBuffer = gl.createBuffer();
|
|
100
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Quad.indicesBuffer);
|
|
101
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, Quad.indices, gl.STATIC_DRAW);
|
|
102
|
+
|
|
103
|
+
Quad.positionsArrayBuffer = gl.createBuffer();
|
|
104
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, Quad.positionsArrayBuffer);
|
|
105
|
+
gl.bufferData(gl.ARRAY_BUFFER, Quad.positionsArray, gl.DYNAMIC_DRAW);
|
|
106
|
+
|
|
107
|
+
gl.enableVertexAttribArray(0);
|
|
108
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
|
|
109
|
+
gl.enableVertexAttribArray(1);
|
|
110
|
+
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
|
|
111
|
+
}else{
|
|
112
|
+
throw new Error("WebGL globals not initialised");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export class CenterQuad{
|
|
118
|
+
private static positions = new Float32Array([
|
|
119
|
+
0.5, 0.5,
|
|
120
|
+
-0.5, 0.5,
|
|
121
|
+
0.5, -0.5,
|
|
122
|
+
-0.5, -0.5,
|
|
123
|
+
/*0.5, 0.5,
|
|
124
|
+
-0.5, 0.5,
|
|
125
|
+
0.5, -0.5,
|
|
126
|
+
-0.5, -0.5*/
|
|
127
|
+
]);
|
|
128
|
+
private static positionBuffer: WebGLBuffer | undefined;
|
|
129
|
+
private static relativePositions = new Float32Array([
|
|
130
|
+
0, 0,
|
|
131
|
+
1, 0,
|
|
132
|
+
0, 1,
|
|
133
|
+
1, 1
|
|
134
|
+
/*0.5, 0.5,
|
|
135
|
+
-0.5, 0.5,
|
|
136
|
+
0.5, -0.5,
|
|
137
|
+
-0.5, -0.5*/
|
|
138
|
+
]);
|
|
139
|
+
private static relativeBuffer: WebGLBuffer | undefined;
|
|
140
|
+
private static indices = new Uint16Array([0,1,2,2,1,3]);
|
|
141
|
+
private static indicesBuffer: WebGLBuffer | undefined;
|
|
142
|
+
|
|
143
|
+
static draw(){
|
|
144
|
+
if(!CenterQuad.positionBuffer) CenterQuad.setup();
|
|
145
|
+
if(WebGL.gl){
|
|
146
|
+
const gl = WebGL.gl;
|
|
147
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, CenterQuad.indicesBuffer!);
|
|
148
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, CenterQuad.positionBuffer!);
|
|
149
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); // changes the bind buffers
|
|
150
|
+
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
static drawRelative(){
|
|
155
|
+
if(!CenterQuad.relativeBuffer) CenterQuad.setup();
|
|
156
|
+
if(WebGL.gl){
|
|
157
|
+
const gl = WebGL.gl;
|
|
158
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, CenterQuad.indicesBuffer!);
|
|
159
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, CenterQuad.positionBuffer!);
|
|
160
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); // changes the bind buffers
|
|
161
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, CenterQuad.relativeBuffer!);
|
|
162
|
+
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0); // changes the bind buffers
|
|
163
|
+
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private static setup(){
|
|
168
|
+
if(WebGL.gl){
|
|
169
|
+
const gl = WebGL.gl;
|
|
170
|
+
CenterQuad.positionBuffer = gl.createBuffer();
|
|
171
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, CenterQuad.positionBuffer);
|
|
172
|
+
gl.bufferData(gl.ARRAY_BUFFER, CenterQuad.positions, gl.STATIC_DRAW);
|
|
173
|
+
|
|
174
|
+
gl.enableVertexAttribArray(0);
|
|
175
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
|
|
176
|
+
|
|
177
|
+
CenterQuad.indicesBuffer = gl.createBuffer();
|
|
178
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, CenterQuad.indicesBuffer);
|
|
179
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, CenterQuad.indices, gl.STATIC_DRAW);
|
|
180
|
+
|
|
181
|
+
CenterQuad.relativeBuffer = gl.createBuffer();
|
|
182
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, CenterQuad.relativeBuffer);
|
|
183
|
+
gl.bufferData(gl.ARRAY_BUFFER, CenterQuad.relativePositions, gl.STATIC_DRAW);
|
|
184
|
+
|
|
185
|
+
gl.enableVertexAttribArray(1);
|
|
186
|
+
//gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import WebGL from "./../globals";
|
|
2
|
+
import * as File from "./../Util/file"
|
|
3
|
+
import * as Shader from "./../Shaders/custom"
|
|
4
|
+
|
|
5
|
+
type Int32 = number;
|
|
6
|
+
type VoidFunction = () => void;
|
|
7
|
+
type ErrorFunction = (e: any) => void;
|
|
8
|
+
const EmptyErrorFunction = (e: any) => {};
|
|
9
|
+
|
|
10
|
+
const EmptyFunction = () => {};
|
|
11
|
+
|
|
12
|
+
export class Texture{
|
|
13
|
+
|
|
14
|
+
static textures_loaded: Int32 = 0;
|
|
15
|
+
static textures_requested: Int32 = 0;
|
|
16
|
+
static textures_failed: Set<string> = new Set();
|
|
17
|
+
static path = "/";
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
texture: WebGLTexture | undefined;
|
|
21
|
+
is_loaded: boolean;
|
|
22
|
+
url: string
|
|
23
|
+
constructor(fn: string){
|
|
24
|
+
//const gl = WebGL.gl;
|
|
25
|
+
this.url = Texture.path+fn;
|
|
26
|
+
this.is_loaded = false;
|
|
27
|
+
}
|
|
28
|
+
load(onLoad:VoidFunction=EmptyFunction, onError:ErrorFunction=EmptyErrorFunction){
|
|
29
|
+
console.log(this.url);
|
|
30
|
+
if(!this.is_loaded){
|
|
31
|
+
Texture.textures_requested++;
|
|
32
|
+
const gl = WebGL.gl;
|
|
33
|
+
if(gl != undefined){
|
|
34
|
+
const img = new Image();
|
|
35
|
+
img.src = this.url;
|
|
36
|
+
img.onload = () => {
|
|
37
|
+
console.log(`Loaded texture ${this.url}`);
|
|
38
|
+
this.texture = gl.createTexture();
|
|
39
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
40
|
+
gl.bindTexture(gl.TEXTURE_2D, this.texture);
|
|
41
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
42
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
43
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
44
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
45
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
|
|
46
|
+
Texture.textures_loaded++;
|
|
47
|
+
this.is_loaded = true;
|
|
48
|
+
onLoad();
|
|
49
|
+
}
|
|
50
|
+
img.onerror = (e) => {
|
|
51
|
+
onError(e);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}else{
|
|
55
|
+
onLoad();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//static loadList
|
|
59
|
+
active(id: Int32){
|
|
60
|
+
const gl = WebGL.gl;
|
|
61
|
+
if(this.texture && this.is_loaded && gl != undefined){
|
|
62
|
+
gl.activeTexture(gl.TEXTURE0+id);
|
|
63
|
+
gl.bindTexture(gl.TEXTURE_2D, this.texture);
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export class TextureCollection{
|
|
72
|
+
textures: Map<string, Texture>;
|
|
73
|
+
loaded: Int32;
|
|
74
|
+
loading: boolean;
|
|
75
|
+
to_load: Texture[]; // active to loading for
|
|
76
|
+
finished_loading: Int32;
|
|
77
|
+
constructor(){
|
|
78
|
+
this.textures = new Map();
|
|
79
|
+
this.loaded = 0;
|
|
80
|
+
this.loading = false;
|
|
81
|
+
this.to_load = [];
|
|
82
|
+
this.finished_loading = 0;
|
|
83
|
+
}
|
|
84
|
+
active(key: string, id: Int32): boolean{
|
|
85
|
+
if(this.textures.has(key)){
|
|
86
|
+
const tex = this.textures.get(key)!;
|
|
87
|
+
if(!tex.is_loaded) return false;
|
|
88
|
+
tex.active(id);
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
getTexture(key: string): Texture | undefined{
|
|
94
|
+
return this.textures.get(key);
|
|
95
|
+
}
|
|
96
|
+
addTexture(key: string, texture: Texture){
|
|
97
|
+
this.textures.set(key, texture);
|
|
98
|
+
}
|
|
99
|
+
load(onAllLoaded: VoidFunction=EmptyFunction){
|
|
100
|
+
function finishLoading(fl: TextureCollection){
|
|
101
|
+
if(fl.finished_loading == fl.to_load.length){
|
|
102
|
+
console.log("end loading texture collection");
|
|
103
|
+
onAllLoaded();
|
|
104
|
+
fl.loading = false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if(!this.loading){
|
|
108
|
+
console.log("start loading textures");
|
|
109
|
+
this.loading = true;
|
|
110
|
+
this.to_load = [];
|
|
111
|
+
for(const [name, tex] of this.textures){
|
|
112
|
+
if(!tex.is_loaded) this.to_load.push(tex);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for(const tex of this.to_load){
|
|
116
|
+
tex.load(() => {
|
|
117
|
+
this.loaded++;
|
|
118
|
+
this.finished_loading++;
|
|
119
|
+
console.log(`finished ${tex.url}`);
|
|
120
|
+
finishLoading(this);
|
|
121
|
+
},
|
|
122
|
+
() => {
|
|
123
|
+
console.log(`error loading ${tex.url}`);
|
|
124
|
+
this.finished_loading++;
|
|
125
|
+
finishLoading(this);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
addFromUrl(key: string, file: string){
|
|
131
|
+
const texture = new Texture(file);
|
|
132
|
+
this.textures.set(key, texture);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
type SpriteSheetPosition = {
|
|
137
|
+
x: Int32;
|
|
138
|
+
y: Int32;
|
|
139
|
+
}
|
|
140
|
+
type Char = string;
|
|
141
|
+
|
|
142
|
+
interface FontSheetShader{
|
|
143
|
+
setWidth: (w: Int32) => void;
|
|
144
|
+
setHeight: (h: Int32) => void;
|
|
145
|
+
setX: (x: Int32) => void;
|
|
146
|
+
setY: (y: Int32) => void;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export class CustomFont{
|
|
150
|
+
coord_to_sheet_position: Map<Char, SpriteSheetPosition>
|
|
151
|
+
font_sheet: Texture;
|
|
152
|
+
font_name: string;
|
|
153
|
+
loaded: boolean;
|
|
154
|
+
width: Int32;
|
|
155
|
+
height: Int32;
|
|
156
|
+
|
|
157
|
+
//fn requires extension e.g. .png
|
|
158
|
+
constructor(font_sheet_fn: string){
|
|
159
|
+
this.font_sheet = new Texture(font_sheet_fn);
|
|
160
|
+
this.font_name = font_sheet_fn.split('.')[0];
|
|
161
|
+
this.loaded = false;
|
|
162
|
+
this.coord_to_sheet_position = new Map();
|
|
163
|
+
this.width = 0;
|
|
164
|
+
this.height = 0;
|
|
165
|
+
//test font settings
|
|
166
|
+
//this.coord_to_sheet_position.set("a", {x: 0, y: 0});
|
|
167
|
+
//this.coord_to_sheet_position.set("b", {x: 1, y: 0});
|
|
168
|
+
//this.coord_to_sheet_position.set("c", {x: 2, y: 0});
|
|
169
|
+
}
|
|
170
|
+
active(id: Int32){
|
|
171
|
+
this.font_sheet.active(id);
|
|
172
|
+
}
|
|
173
|
+
load(onLoaded:()=>void=()=>{}, onError?: (e: any) => void){
|
|
174
|
+
this.font_sheet.load(
|
|
175
|
+
() =>
|
|
176
|
+
File.fetchPublicFile(`${this.font_name}.txt`, (txt) => {
|
|
177
|
+
const sp = txt.split('\r\n');
|
|
178
|
+
const dims = sp[0].split(' ');
|
|
179
|
+
this.width = parseInt(dims[0]);
|
|
180
|
+
this.height = parseInt(dims[1]);
|
|
181
|
+
for(let i = 1; i < sp.length; i++){
|
|
182
|
+
const x = (i-1)%this.width;
|
|
183
|
+
const y = Math.floor((i-1)/this.width);
|
|
184
|
+
for(let j = 0; j < sp[i].length; j++){
|
|
185
|
+
this.coord_to_sheet_position.set(sp[i][j], {x, y});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
console.log(`Font: loaded success, ${this.font_name}`);
|
|
189
|
+
onLoaded();
|
|
190
|
+
this.loaded = true;
|
|
191
|
+
},
|
|
192
|
+
(e) => {
|
|
193
|
+
console.log(`${this.font_name}.txt - error`);
|
|
194
|
+
if(onError) onError(e);
|
|
195
|
+
}),
|
|
196
|
+
(e) => {
|
|
197
|
+
if(onError) onError(e);
|
|
198
|
+
}
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
setChar(shader: FontSheetShader, char: Char){
|
|
203
|
+
shader.setWidth(this.width);
|
|
204
|
+
shader.setHeight(this.height);
|
|
205
|
+
const coord = this.coord_to_sheet_position.get(char);
|
|
206
|
+
if(coord != undefined){
|
|
207
|
+
shader.setX(coord.x);
|
|
208
|
+
shader.setY(coord.y);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
//export class Texture
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function fetchPublicFile(fn: string, success:(text: string) => void,
|
|
2
|
+
error:(reason: any) => void=(e)=>{console.log(e)}){
|
|
3
|
+
fetch(`/${fn}`).then(async (res) => {
|
|
4
|
+
if(res.ok){
|
|
5
|
+
const text = await res.blob().then((b) => {
|
|
6
|
+
if(b.type == "text/plain"){
|
|
7
|
+
return b.text();
|
|
8
|
+
}
|
|
9
|
+
return undefined;
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
return text;
|
|
13
|
+
}
|
|
14
|
+
return res.text();
|
|
15
|
+
}).then((text) => {
|
|
16
|
+
if(text != undefined){
|
|
17
|
+
success(text);
|
|
18
|
+
}else{
|
|
19
|
+
error("public file is not a text file");
|
|
20
|
+
}
|
|
21
|
+
}).catch((e) => {
|
|
22
|
+
error(e);
|
|
23
|
+
});
|
|
24
|
+
}
|
package/src/WebGL/app.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import WebGL from "../WebGL/globals";
|
|
2
|
+
import * as WebGLGlobals from "../WebGL/globals";
|
|
3
|
+
|
|
4
|
+
type Int32 = number;
|
|
5
|
+
type Float = number;
|
|
6
|
+
|
|
7
|
+
type VoidFunction = () => void;
|
|
8
|
+
const EmptyFunction: VoidFunction = () => {};
|
|
9
|
+
type TimeTakenFunction = (t:Float) => void
|
|
10
|
+
type OnFinishFunction = (onFinish: VoidFunction) => void;
|
|
11
|
+
|
|
12
|
+
interface IEngine{
|
|
13
|
+
addEvents: () => void;
|
|
14
|
+
loadResources: () => void;
|
|
15
|
+
update: TimeTakenFunction;
|
|
16
|
+
resize: (w: Int32, h:Int32) => void
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class BaseEngine implements IEngine{
|
|
20
|
+
constructor(){};
|
|
21
|
+
|
|
22
|
+
//to override
|
|
23
|
+
loadResources(){};
|
|
24
|
+
|
|
25
|
+
//to override
|
|
26
|
+
update(t: Float){};
|
|
27
|
+
|
|
28
|
+
addEvents(){
|
|
29
|
+
this.addKeyEvents();
|
|
30
|
+
}
|
|
31
|
+
private addKeyEvents(){
|
|
32
|
+
window.addEventListener("keydown", (ev) => this.handleKeyDown(ev));
|
|
33
|
+
window.addEventListener("keyup", (ev) => this.handleKeyUp(ev));
|
|
34
|
+
window.addEventListener("mousemove", (ev) => this.handleMouseMove(ev));
|
|
35
|
+
window.addEventListener("mousedown", (ev) => this.handleMouseDown(ev));
|
|
36
|
+
window.addEventListener("mouseup", (ev) => this.handleMouseUp(ev));
|
|
37
|
+
}
|
|
38
|
+
resize(w: Int32, h:Int32){
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//to override
|
|
43
|
+
protected handleKeyDown(ev: KeyboardEvent){};
|
|
44
|
+
//to override
|
|
45
|
+
protected handleKeyUp(ev: KeyboardEvent){};
|
|
46
|
+
//to override
|
|
47
|
+
protected handleMouseMove(ev: MouseEvent){};
|
|
48
|
+
//to override
|
|
49
|
+
protected handleMouseDown(ev: MouseEvent){};
|
|
50
|
+
//to override
|
|
51
|
+
protected handleMouseUp(ev: MouseEvent){};
|
|
52
|
+
//to override
|
|
53
|
+
protected handleScrollWheel(){
|
|
54
|
+
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface IEngineRenderer<E extends IEngine>{
|
|
60
|
+
render?: (engine: E) => void;
|
|
61
|
+
renderUpdate?: (time: Int32, engine: E) => void;
|
|
62
|
+
loadTextures?: OnFinishFunction;
|
|
63
|
+
resize?: (w: Int32, h: Int32) => void;
|
|
64
|
+
//loadResources: () => void;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface IRenderer{
|
|
68
|
+
render: () => void;
|
|
69
|
+
loadShaders?: () => void; // shaders usually already preloaded (added by raw file and compiled on shader class construction)
|
|
70
|
+
loadTextures?: () => void;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class App<E extends IEngine>{
|
|
74
|
+
private engine: E;
|
|
75
|
+
private renderer: IEngineRenderer<E>;
|
|
76
|
+
|
|
77
|
+
constructor(engine: E, renderer: IEngineRenderer<E>){
|
|
78
|
+
this.engine = engine;
|
|
79
|
+
this.renderer = renderer;
|
|
80
|
+
}
|
|
81
|
+
addEvents(){
|
|
82
|
+
this.engine.addEvents();
|
|
83
|
+
}
|
|
84
|
+
getRenderer(): IEngineRenderer<E>{
|
|
85
|
+
return this.renderer;
|
|
86
|
+
}
|
|
87
|
+
resize(w: Int32, h: Int32, canvas: HTMLCanvasElement){
|
|
88
|
+
this.engine.resize(w, h);
|
|
89
|
+
if(this.renderer.resize) this.renderer.resize(w, h);
|
|
90
|
+
WebGL.resetViewport(canvas);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
//to override?
|
|
94
|
+
loadResources(onLoaded:VoidFunction=()=>{}){
|
|
95
|
+
//this.engine.loadTextures();
|
|
96
|
+
if(this.renderer.loadTextures){
|
|
97
|
+
this.renderer.loadTextures(onLoaded);
|
|
98
|
+
}else{
|
|
99
|
+
onLoaded();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
update(t: Float){
|
|
103
|
+
this.engine.update(t);
|
|
104
|
+
}
|
|
105
|
+
draw(){
|
|
106
|
+
//this.renderer.render(this.engine);
|
|
107
|
+
}
|
|
108
|
+
initApp(){
|
|
109
|
+
this.addEvents();
|
|
110
|
+
this.appCycle(0);
|
|
111
|
+
}
|
|
112
|
+
appCycle(t: Float){
|
|
113
|
+
this.update(t);
|
|
114
|
+
if(this.renderer.render) this.renderer.render(this.engine);
|
|
115
|
+
requestAnimationFrame((t) => this.appCycle(t));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
//includes text
|
|
121
|
+
export class SimpleAppRenderer<E extends BaseEngine> implements IEngineRenderer<E>{
|
|
122
|
+
fonts: WebGLGlobals.FontLoader;
|
|
123
|
+
text_drawer: WebGLGlobals.TextDrawer;
|
|
124
|
+
orthographic: WebGLGlobals.Matrix.TransformationMatrix3x3;
|
|
125
|
+
font_names: string[];
|
|
126
|
+
constructor(w: Int32, h: Int32){
|
|
127
|
+
this.fonts = new WebGLGlobals.FontLoader();
|
|
128
|
+
this.text_drawer = new WebGLGlobals.TextDrawer();
|
|
129
|
+
this.orthographic = WebGLGlobals.Matrix.TransformationMatrix3x3.orthographic(0, w, h, 0);
|
|
130
|
+
this.font_names = [];
|
|
131
|
+
}
|
|
132
|
+
loadTextures(onLoad: VoidFunction=EmptyFunction){
|
|
133
|
+
console.log("loading textures");
|
|
134
|
+
for(const fn of this.font_names){
|
|
135
|
+
this.fonts.addFont(fn);
|
|
136
|
+
}
|
|
137
|
+
this.fonts.loadFonts(() => {
|
|
138
|
+
if(this.font_names.length != 0){
|
|
139
|
+
this.text_drawer.setFont(this.fonts.getFont(this.font_names[0])!);
|
|
140
|
+
}
|
|
141
|
+
//this.text_drawer.loadFont(); //this does nothing
|
|
142
|
+
console.log("finished loading");
|
|
143
|
+
if(onLoad) onLoad();
|
|
144
|
+
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
resize(w: Int32, h: Int32){
|
|
148
|
+
this.orthographic = WebGLGlobals.Matrix.TransformationMatrix3x3.orthographic(0, w, h, 0);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
|
|
2
|
+
type Float = number;
|
|
3
|
+
type Int32 = number;
|
|
4
|
+
export type ColourRGB = {
|
|
5
|
+
red: Float;
|
|
6
|
+
green: Float;
|
|
7
|
+
blue: Float;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type ColourRGBA = ColourRGB & {
|
|
11
|
+
alpha: Float;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export class ColourUtils{
|
|
16
|
+
static mix(c1: ColourRGB, c2: ColourRGB): ColourRGB{
|
|
17
|
+
const red = (c1.red+c2.red)*0.5;
|
|
18
|
+
const green = (c1.green+c2.green)*0.5;
|
|
19
|
+
const blue = (c1.blue+c2.blue)*0.5;
|
|
20
|
+
return {red, green, blue};
|
|
21
|
+
}
|
|
22
|
+
static fromRGB(red: Float, green: Float, blue: Float): ColourRGB{
|
|
23
|
+
return {red, green, blue};
|
|
24
|
+
}
|
|
25
|
+
static toRGBA(colour: ColourRGB, alpha: Float): ColourRGBA{
|
|
26
|
+
return {...colour, alpha};
|
|
27
|
+
}
|
|
28
|
+
static black(): ColourRGB{
|
|
29
|
+
return this.fromRGB(0, 0, 0);
|
|
30
|
+
}
|
|
31
|
+
static white(): ColourRGB{
|
|
32
|
+
return this.fromRGB(1, 1, 1);
|
|
33
|
+
}
|
|
34
|
+
static red():ColourRGB{
|
|
35
|
+
return this.fromRGB(1, 0, 0);
|
|
36
|
+
}
|
|
37
|
+
static green():ColourRGB{
|
|
38
|
+
return this.fromRGB(0, 1, 0);
|
|
39
|
+
}
|
|
40
|
+
static blue():ColourRGB{
|
|
41
|
+
return this.fromRGB(0, 0, 1);
|
|
42
|
+
}
|
|
43
|
+
static yellow():ColourRGB{
|
|
44
|
+
return this.fromRGB(1, 1, 0);
|
|
45
|
+
}
|
|
46
|
+
static pink():ColourRGB{
|
|
47
|
+
return this.fromRGB(1, 0, 1);
|
|
48
|
+
}
|
|
49
|
+
static cyan():ColourRGB{
|
|
50
|
+
return this.fromRGB(0, 1, 1);
|
|
51
|
+
}
|
|
52
|
+
static grey(a: Float=0.5): ColourRGB{
|
|
53
|
+
return this.fromRGB(a, a, a);
|
|
54
|
+
}
|
|
55
|
+
static random(): ColourRGB{
|
|
56
|
+
return this.fromRGB(Math.random(), Math.random(), Math.random());
|
|
57
|
+
}
|
|
58
|
+
static linearTransitionColours(c1: ColourRGB, c2: ColourRGB, n: Int32): ColourRGB[]{
|
|
59
|
+
const colours: ColourRGB[] = [];
|
|
60
|
+
const step_r = (c2.red-c1.red)/(n+1);
|
|
61
|
+
const step_g = (c2.green-c1.green)/(n+1);
|
|
62
|
+
const step_b = (c2.blue-c1.blue)/(n+1);
|
|
63
|
+
|
|
64
|
+
for(let i = 1; i <= n; i++){
|
|
65
|
+
const col = ColourUtils.fromRGB(c1.red+step_r*i, c1.green+step_g*i, c1.blue+step_b*i);
|
|
66
|
+
colours.push(col);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return colours;
|
|
70
|
+
}
|
|
71
|
+
}
|