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,353 @@
|
|
|
1
|
+
import { loadVertexShaders } from "./Shaders/Vertex/vertex";
|
|
2
|
+
import { loadFragmentShaders } from "./Shaders/Fragment/fragment";
|
|
3
|
+
import type { ShaderProgram } from "./Shaders/shader";
|
|
4
|
+
import * as Shader from "./Shaders/custom";
|
|
5
|
+
|
|
6
|
+
import * as Shapes from "./Shapes/Shapes"
|
|
7
|
+
import * as Matrix from "./Matrix/matrix";
|
|
8
|
+
import * as Line from "./Shapes/Line";
|
|
9
|
+
import * as Colour from "./colour";
|
|
10
|
+
import * as Texture from "./Texture/texture"
|
|
11
|
+
|
|
12
|
+
type Float = number;
|
|
13
|
+
type Int32 = number;
|
|
14
|
+
|
|
15
|
+
export * as Colour from "./colour";
|
|
16
|
+
export * as Shapes from "./Shapes/Shapes";
|
|
17
|
+
export * as Matrix from "./Matrix/matrix";
|
|
18
|
+
export * as Texture from "./Texture/texture";
|
|
19
|
+
export * as Line from "./Shapes/Line";
|
|
20
|
+
export * as Shader from "./Shaders/custom";
|
|
21
|
+
export * as App from "./app";
|
|
22
|
+
|
|
23
|
+
type VoidFunction = () => void;
|
|
24
|
+
|
|
25
|
+
interface MVPColourShader{
|
|
26
|
+
use: () => void;
|
|
27
|
+
setMvp:(mat: Matrix.TransformationMatrix3x3) => void;
|
|
28
|
+
setColourFromColourRGB: (colour: Colour.ColourRGB) => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class WebGL{
|
|
32
|
+
static gl: WebGL2RenderingContext | null;
|
|
33
|
+
static active_shader_program: ShaderProgram | null;
|
|
34
|
+
private static initialised: boolean = false;
|
|
35
|
+
//static buffer: WebGLBuffer | null; for testing
|
|
36
|
+
static defaultError(){
|
|
37
|
+
throw new Error("WebGL not initialised or null");
|
|
38
|
+
}
|
|
39
|
+
static initialise(canvas: HTMLCanvasElement){
|
|
40
|
+
this.gl = canvas.getContext("webgl2", {alpha: false});
|
|
41
|
+
this.resetViewport(canvas);
|
|
42
|
+
if(this.gl && !this.initialised){
|
|
43
|
+
loadVertexShaders();
|
|
44
|
+
loadFragmentShaders();
|
|
45
|
+
this.initialised = true;
|
|
46
|
+
//this.buffer = this.gl.createBuffer();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static resetViewport(canvas: HTMLCanvasElement){
|
|
51
|
+
this.gl!.viewport(0, 0, canvas.width, canvas.height);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static rectangleModel(x: Float, y: Float, width: number, height: number): Matrix.TransformationMatrix3x3{
|
|
55
|
+
let model = Matrix.TransformationMatrix3x3.translate(x, y);
|
|
56
|
+
model.scale(width, height);
|
|
57
|
+
return model;
|
|
58
|
+
}
|
|
59
|
+
static lineModel(x1: Float, y1: Float, x2: Float, y2: Float, lt: Float){
|
|
60
|
+
const line = new Line.Line(x1, y1, x2, y2);
|
|
61
|
+
|
|
62
|
+
let model = Matrix.TransformationMatrix3x3.identity();
|
|
63
|
+
model.translate(x1, y1);
|
|
64
|
+
model.rotate(-line.angleInRadians());
|
|
65
|
+
model.scale(line.length(), lt);
|
|
66
|
+
model.translate(0, -0.5);
|
|
67
|
+
|
|
68
|
+
return model;
|
|
69
|
+
}
|
|
70
|
+
static drawColourRect(vp: Matrix.TransformationMatrix3x3,
|
|
71
|
+
shader: MVPColourShader, x: Int32, y: Int32,
|
|
72
|
+
width: Int32, height: Int32,
|
|
73
|
+
colour: Colour.ColourRGB=Colour.ColourUtils.black())
|
|
74
|
+
{
|
|
75
|
+
const model = WebGL.rectangleModel(x, y, width, height);
|
|
76
|
+
shader.use();
|
|
77
|
+
shader.setColourFromColourRGB(colour);
|
|
78
|
+
shader.setMvp(vp.multiplyCopy(model));
|
|
79
|
+
Shapes.Quad.draw();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
//can only draw rects
|
|
85
|
+
export class BasicModel{
|
|
86
|
+
static colour_shader: Shader.MVPColourProgram;
|
|
87
|
+
|
|
88
|
+
static init(){
|
|
89
|
+
this.colour_shader = new Shader.MVPColourProgram();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
parts: BasicModelItem2D[];
|
|
93
|
+
constructor(){
|
|
94
|
+
this.parts = [];
|
|
95
|
+
}
|
|
96
|
+
addPart(part: BasicModelItem2D){
|
|
97
|
+
this.parts.push(part);
|
|
98
|
+
}
|
|
99
|
+
draw(p: Matrix.TransformationMatrix3x3){
|
|
100
|
+
const shader = BasicModel.colour_shader;
|
|
101
|
+
shader.use();
|
|
102
|
+
for(const model of this.parts){
|
|
103
|
+
shader.setMvp(p.multiplyCopy(model.transformation));
|
|
104
|
+
shader.setColour(model.colour.red, model.colour.green, model.colour.blue);
|
|
105
|
+
Shapes.Quad.drawRelative();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
static drawItem(vp: Matrix.TransformationMatrix3x3, item: BasicModelItem2D){
|
|
109
|
+
const shader = BasicModel.colour_shader;
|
|
110
|
+
shader.use();
|
|
111
|
+
shader.setColour(item.colour.red, item.colour.green, item.colour.blue);
|
|
112
|
+
shader.setMvp(vp.multiplyCopy(item.transformation));
|
|
113
|
+
Shapes.Quad.draw();
|
|
114
|
+
}
|
|
115
|
+
static defaultItem(): BasicModelItem2D{
|
|
116
|
+
return {colour: Colour.ColourUtils.white(), transformation: Matrix.TransformationMatrix3x3.identity()};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export type BasicModelItem2D = {
|
|
121
|
+
colour: Colour.ColourRGB;
|
|
122
|
+
transformation: Matrix.TransformationMatrix3x3;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
export function testBasicModel(){
|
|
127
|
+
const pers = Matrix.TransformationMatrix3x3.orthographic(0, 10, 10, 0);
|
|
128
|
+
const s1 = WebGL.rectangleModel(0, 0, 5, 5);
|
|
129
|
+
const s2 = WebGL.rectangleModel(5, 5, 5, 5);
|
|
130
|
+
const bm = new BasicModel();
|
|
131
|
+
BasicModel.init();
|
|
132
|
+
|
|
133
|
+
const white = {red: 1, green: 1, blue: 1};
|
|
134
|
+
const blue = {red: 0, green: 0, blue: 1};
|
|
135
|
+
bm.addPart({colour: white, transformation: s1});
|
|
136
|
+
bm.addPart({colour: white, transformation: s2});
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
const s3 = WebGL.rectangleModel(3, 5, 2, 2);
|
|
140
|
+
const s4 = WebGL.rectangleModel(5,3, 2, 2);
|
|
141
|
+
bm.addPart({colour: blue, transformation: s3});
|
|
142
|
+
bm.addPart({colour: blue, transformation: s4});
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
bm.draw(pers);
|
|
146
|
+
//Shapes.Quad.draw();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
export function loadTextureTest(){
|
|
151
|
+
if(WebGL.gl){
|
|
152
|
+
const gl = WebGL.gl;
|
|
153
|
+
|
|
154
|
+
const texture_shader = new Shader.MVPTextureProgram();
|
|
155
|
+
|
|
156
|
+
const image = new Image();
|
|
157
|
+
image.src = "/base.png";
|
|
158
|
+
image.onload = () => {
|
|
159
|
+
console.log(`${image.src} loaded`);
|
|
160
|
+
const texture = gl.createTexture();
|
|
161
|
+
const tex_id = 1;
|
|
162
|
+
gl.activeTexture(gl.TEXTURE0+tex_id);
|
|
163
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
164
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
165
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
166
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
167
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
168
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
|
|
169
|
+
texture_shader.use();
|
|
170
|
+
texture_shader.setTextureId(tex_id);
|
|
171
|
+
texture_shader.setTextureId(0);
|
|
172
|
+
//texture_shader.setTextureId(0);
|
|
173
|
+
const perspective = Matrix.TransformationMatrix3x3.orthographic(0, 1, 1, 0);
|
|
174
|
+
|
|
175
|
+
texture_shader.setMvp(perspective);
|
|
176
|
+
Shapes.Quad.drawRelative();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export class FontLoader{
|
|
182
|
+
private fonts: Map<string, Texture.CustomFont>;
|
|
183
|
+
loaded: Int32;
|
|
184
|
+
loading: boolean; // prevention measure for loading while another load is active
|
|
185
|
+
to_load: Texture.CustomFont[];
|
|
186
|
+
finished_loading: Int32;
|
|
187
|
+
constructor(){
|
|
188
|
+
this.fonts = new Map();
|
|
189
|
+
this.loaded = 0;
|
|
190
|
+
this.loading = false;
|
|
191
|
+
this.to_load = [];
|
|
192
|
+
this.finished_loading = 0;
|
|
193
|
+
}
|
|
194
|
+
addFont(name: string): boolean{
|
|
195
|
+
if(this.loading) return false;
|
|
196
|
+
const font = new Texture.CustomFont(name);
|
|
197
|
+
this.fonts.set(name, font);
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
loadFonts(onAllLoaded: () => void) {
|
|
201
|
+
function finishLoading(fl: FontLoader){
|
|
202
|
+
if(fl.finished_loading == fl.to_load.length){
|
|
203
|
+
console.log("end loading");
|
|
204
|
+
onAllLoaded();
|
|
205
|
+
fl.loading = false;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if(!this.loading){
|
|
209
|
+
console.log("start loading fonts");
|
|
210
|
+
this.loading = true;
|
|
211
|
+
this.to_load = [];
|
|
212
|
+
for(const [name, font] of this.fonts){
|
|
213
|
+
this.to_load.push(font);
|
|
214
|
+
}
|
|
215
|
+
console.log(this.to_load);
|
|
216
|
+
if(this.to_load.length == 0){
|
|
217
|
+
finishLoading(this);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
for(const font of this.to_load){
|
|
221
|
+
console.log(font);
|
|
222
|
+
font.load(() => {
|
|
223
|
+
this.loaded++;
|
|
224
|
+
this.finished_loading++;
|
|
225
|
+
console.log(`finished ${font.font_name}`);
|
|
226
|
+
finishLoading(this);
|
|
227
|
+
},
|
|
228
|
+
(e) => {
|
|
229
|
+
console.log(`error loading ${font.font_name} - ${e}`);
|
|
230
|
+
this.finished_loading++;
|
|
231
|
+
finishLoading(this);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
getFont(name: string): Texture.CustomFont | undefined{
|
|
237
|
+
return this.fonts.get(name);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export class TextDrawer{
|
|
242
|
+
sprite_sheet_shader: Shader.MVPSpriteSheetProgram;
|
|
243
|
+
colour_sheet_shader: Shader.MVPSpriteSheetColourProgram;
|
|
244
|
+
font?: Texture.CustomFont;
|
|
245
|
+
constructor(){
|
|
246
|
+
this.sprite_sheet_shader = new Shader.MVPSpriteSheetProgram();
|
|
247
|
+
this.colour_sheet_shader = new Shader.MVPSpriteSheetColourProgram();
|
|
248
|
+
}
|
|
249
|
+
setFont(font: Texture.CustomFont){
|
|
250
|
+
this.font = font;
|
|
251
|
+
}
|
|
252
|
+
loadFont(onLoaded:()=>void=()=>{}){
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
drawTextModelColour(mat: Matrix.TransformationMatrix3x3, text: string, size: Float, colour: Colour.ColourRGB){
|
|
256
|
+
if(this.font){
|
|
257
|
+
this.colour_sheet_shader.use();
|
|
258
|
+
this.colour_sheet_shader.setColourFromColourRGB(colour);
|
|
259
|
+
this.font.active(0);
|
|
260
|
+
this.colour_sheet_shader.setTextureId(0);
|
|
261
|
+
const scale = Matrix.TransformationMatrix3x3.scale(size, size);
|
|
262
|
+
const gl = WebGL.gl!;
|
|
263
|
+
gl.enable(gl.BLEND);
|
|
264
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
265
|
+
for(let i = 0; i < text.length; i++){
|
|
266
|
+
if(text[i] == ' ') continue;
|
|
267
|
+
const tr = Matrix.TransformationMatrix3x3.translate(size*i,0);
|
|
268
|
+
const matrix = mat.multiplyCopy(tr.multiplyCopy(scale));
|
|
269
|
+
this.colour_sheet_shader.setMvp(matrix);
|
|
270
|
+
this.font.setChar(this.colour_sheet_shader, text[i]);
|
|
271
|
+
Shapes.Quad.drawRelative();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
gl.disable(gl.BLEND);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
}
|
|
278
|
+
drawText(vp: Matrix.TransformationMatrix3x3, x: Float, y: Float, text: string, size: Float){
|
|
279
|
+
if(this.font){
|
|
280
|
+
//console.log(this.font);
|
|
281
|
+
const gl = WebGL.gl!;
|
|
282
|
+
this.sprite_sheet_shader.use();
|
|
283
|
+
this.font.active(1);
|
|
284
|
+
this.sprite_sheet_shader.setTextureId(1);
|
|
285
|
+
const scale = Matrix.TransformationMatrix3x3.scale(size, size);
|
|
286
|
+
gl.enable(gl.BLEND);
|
|
287
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
288
|
+
for(let i = 0; i < text.length; i++){
|
|
289
|
+
if(text[i] == ' ') continue;
|
|
290
|
+
const cx = x+i*size;
|
|
291
|
+
const tr = Matrix.TransformationMatrix3x3.translate(cx, y);
|
|
292
|
+
const model = tr.multiplyCopy(scale);
|
|
293
|
+
this.sprite_sheet_shader.setMvp(vp.multiplyCopy(model));
|
|
294
|
+
this.font.setChar(this.sprite_sheet_shader, text[i]);
|
|
295
|
+
Shapes.Quad.drawRelative();
|
|
296
|
+
}
|
|
297
|
+
gl.disable(gl.BLEND);
|
|
298
|
+
}else{
|
|
299
|
+
throw "TextDrawer: No font set";
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
drawTextModel(mat: Matrix.TransformationMatrix3x3, text: string, size: Float){
|
|
303
|
+
if(this.font){
|
|
304
|
+
const gl = WebGL.gl!;
|
|
305
|
+
this.sprite_sheet_shader.use();
|
|
306
|
+
this.font.active(1);
|
|
307
|
+
this.sprite_sheet_shader.setTextureId(1);
|
|
308
|
+
const scale = Matrix.TransformationMatrix3x3.scale(size, size);
|
|
309
|
+
gl.enable(gl.BLEND);
|
|
310
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
311
|
+
for(let i = 0; i < text.length; i++){
|
|
312
|
+
if(text[i] == ' ') continue;
|
|
313
|
+
const tr = Matrix.TransformationMatrix3x3.translate(size*i,0);
|
|
314
|
+
|
|
315
|
+
const matrix = mat.multiplyCopy(tr.multiplyCopy(scale));
|
|
316
|
+
this.sprite_sheet_shader.setMvp(matrix);
|
|
317
|
+
this.font.setChar(this.sprite_sheet_shader, text[i]);
|
|
318
|
+
Shapes.Quad.drawRelative();
|
|
319
|
+
}
|
|
320
|
+
gl.disable(gl.BLEND);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
drawTextColour(vp: Matrix.TransformationMatrix3x3, x: Float, y: Float, text: string, size: Float, colour: Colour.ColourRGB){
|
|
324
|
+
if(this.font){
|
|
325
|
+
const gl = WebGL.gl!;
|
|
326
|
+
this.colour_sheet_shader.use();
|
|
327
|
+
this.colour_sheet_shader.setColourFromColourRGB(colour);
|
|
328
|
+
this.font.active(1);
|
|
329
|
+
this.colour_sheet_shader.setTextureId(1);
|
|
330
|
+
const scale = Matrix.TransformationMatrix3x3.scale(size, size);
|
|
331
|
+
gl.enable(gl.BLEND);
|
|
332
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
333
|
+
for(let i = 0; i < text.length; i++){
|
|
334
|
+
if(text[i] == ' ') continue;
|
|
335
|
+
const cx = x+i*size;
|
|
336
|
+
const tr = Matrix.TransformationMatrix3x3.translate(cx, y);
|
|
337
|
+
const model = tr.multiplyCopy(scale);
|
|
338
|
+
this.colour_sheet_shader.setMvp(vp.multiplyCopy(model));
|
|
339
|
+
this.font.setChar(this.colour_sheet_shader, text[i]);
|
|
340
|
+
Shapes.Quad.drawRelative();
|
|
341
|
+
}
|
|
342
|
+
gl.disable(gl.BLEND);
|
|
343
|
+
}else{
|
|
344
|
+
throw "TextDrawer: No font set";
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
//expects that all characters are of same width
|
|
348
|
+
getTextWidth(text: string, size: Float): Float{
|
|
349
|
+
return text.length*size;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export default WebGL;
|
package/src/global.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/global.d.ts
|
|
2
|
+
declare module '*.txt' {
|
|
3
|
+
const content: string;
|
|
4
|
+
export default content;
|
|
5
|
+
}
|
|
6
|
+
declare module '*.frag' {
|
|
7
|
+
const content: string;
|
|
8
|
+
export default content;
|
|
9
|
+
}
|
|
10
|
+
declare module '*.vert' {
|
|
11
|
+
const content: string;
|
|
12
|
+
export default content;
|
|
13
|
+
}
|
package/src/index.ts
ADDED
package/src/main.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
|
|
2
|
+
import WebGL from './WebGL/globals';
|
|
3
|
+
import * as Shapes from './WebGL/Shapes/Shapes';
|
|
4
|
+
import * as CustomShader from './WebGL/Shaders/custom';
|
|
5
|
+
import * as Matrix from "./WebGL/Matrix/matrix";
|
|
6
|
+
|
|
7
|
+
import * as Mixin from './utils/mixin';
|
|
8
|
+
import * as App from './WebGL/app';
|
|
9
|
+
import * as Grid from './App/ResourceSim/grid_app';
|
|
10
|
+
import * as WebGLGlobals from './WebGL/globals';
|
|
11
|
+
|
|
12
|
+
import { newPuzzleApp } from './App/puzzle_box/app';
|
|
13
|
+
|
|
14
|
+
import * as Water from './App/water/water'
|
|
15
|
+
import * as Card from "./App/card/card";
|
|
16
|
+
//import * as CustomShaders from './shaders/custom';
|
|
17
|
+
|
|
18
|
+
const canvas: HTMLCanvasElement = document.getElementById("app") as HTMLCanvasElement;
|
|
19
|
+
|
|
20
|
+
const overlay: HTMLDivElement = document.getElementById("overlay") as HTMLDivElement;
|
|
21
|
+
|
|
22
|
+
overlay.style.position = "absolute";
|
|
23
|
+
overlay.style.left = "50%";
|
|
24
|
+
overlay.style.top = "50%";
|
|
25
|
+
overlay.style.transform = "translate(-50%, -50%)";
|
|
26
|
+
overlay.style.color = "white";
|
|
27
|
+
overlay.textContent = "Loading";
|
|
28
|
+
|
|
29
|
+
canvas.width = window.innerWidth; // record this! TODO important for consistant resizing
|
|
30
|
+
canvas.height = window.innerHeight;
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
//console.log(txt.);
|
|
35
|
+
//const gl: WebGL2RenderingContext = canvas.getContext("webgl2")!;
|
|
36
|
+
WebGL.initialise(canvas);
|
|
37
|
+
|
|
38
|
+
const gl = WebGL.gl;
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
//const engine = new App.MyEngine();
|
|
42
|
+
const engine = new Grid.WallEngine();
|
|
43
|
+
engine.addOverlayElement(overlay);
|
|
44
|
+
//const renderer = new App.MyRenderer();
|
|
45
|
+
const renderer = new Grid.WallRenderer(canvas.width, canvas.height);
|
|
46
|
+
renderer.addOverlayElement(overlay);
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
const water_engine = new Water.WaterEngine();
|
|
50
|
+
const water_renderer = new Water.WaterRenderer(canvas.width, canvas.height);
|
|
51
|
+
|
|
52
|
+
const card_engine = new Card.CardEngine(canvas.width, canvas.height);
|
|
53
|
+
const card_renderer = new Card.CardRenderer(canvas.width, canvas.height);
|
|
54
|
+
|
|
55
|
+
//const app = new App.App(card_engine, card_renderer);
|
|
56
|
+
//const app = new App.App(engine, renderer);
|
|
57
|
+
const app = newPuzzleApp(canvas.width,canvas.height);
|
|
58
|
+
|
|
59
|
+
app.loadResources(() => {
|
|
60
|
+
console.log("running app");
|
|
61
|
+
app.initApp();
|
|
62
|
+
engine.onFinishLoading();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
window.addEventListener("resize", (e: Event) => {
|
|
66
|
+
canvas.width = window.innerWidth;
|
|
67
|
+
canvas.height = window.innerHeight;
|
|
68
|
+
app.resize(canvas.width, canvas.height, canvas);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
if(gl){
|
|
73
|
+
//WebGLGlobals.loadTexture();
|
|
74
|
+
//app.addEvents();
|
|
75
|
+
//app.update();
|
|
76
|
+
//app.draw();
|
|
77
|
+
|
|
78
|
+
//water_engine.addEvents();
|
|
79
|
+
//water_renderer.render(water_engine);
|
|
80
|
+
|
|
81
|
+
}else{
|
|
82
|
+
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/*
|
|
86
|
+
function draw2(){
|
|
87
|
+
//const transformColour = new CustomShader.TransformColourProgram();
|
|
88
|
+
//const translateColour = new CustomShader.TranslateColourMixin();
|
|
89
|
+
|
|
90
|
+
const t = Date.now();
|
|
91
|
+
|
|
92
|
+
const matrix = Matrix.TransformationMatrix3x3.identity();
|
|
93
|
+
//matrix.rotate(0.5);
|
|
94
|
+
//const rot = Matrix.TransformationMatrix3x3.rotate(t/360);
|
|
95
|
+
//matrix.multiply(rot);
|
|
96
|
+
//const sc = Matrix.TransformationMatrix3x3.scale(1.0, 1.0);
|
|
97
|
+
//matrix.multiply(sc);
|
|
98
|
+
|
|
99
|
+
transformColour.use();
|
|
100
|
+
transformColour.setTransform(matrix);
|
|
101
|
+
transformColour.setColour(0.5, 0.8, 0.5);
|
|
102
|
+
|
|
103
|
+
Shapes.RightTriangle.draw();
|
|
104
|
+
requestAnimationFrame(draw2);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function drawScene(){
|
|
108
|
+
|
|
109
|
+
gl?.clear(gl.COLOR_BUFFER_BIT);
|
|
110
|
+
|
|
111
|
+
const transformColour = new CustomShader.TransformColourProgram();
|
|
112
|
+
const translateColour = new CustomShader.TranslateColourProgram();
|
|
113
|
+
|
|
114
|
+
const t = Date.now();
|
|
115
|
+
|
|
116
|
+
const matrix = Matrix.TransformationMatrix3x3.translate(Math.sin(t/400)/1.5, Math.cos(t/600)/3);
|
|
117
|
+
//matrix.rotate(0.5);
|
|
118
|
+
const rot = Matrix.TransformationMatrix3x3.rotate(t/360);
|
|
119
|
+
matrix.multiply(rot);
|
|
120
|
+
const sc = Matrix.TransformationMatrix3x3.scale(1.0, 1.0);
|
|
121
|
+
matrix.multiply(sc);
|
|
122
|
+
|
|
123
|
+
transformColour.use();
|
|
124
|
+
transformColour.setTransform(matrix);
|
|
125
|
+
transformColour.setColour(0.5, 0.8, 0.5);
|
|
126
|
+
|
|
127
|
+
//Shapes.RightTriangle.draw();
|
|
128
|
+
Shapes.RightTriangle.draw();
|
|
129
|
+
//Shapes.Quad.draw();
|
|
130
|
+
|
|
131
|
+
//program.use();
|
|
132
|
+
//program.setTranslate(-0.8, -0.8);
|
|
133
|
+
translateColour.use();
|
|
134
|
+
translateColour.setColour(0.4, 0.2, 0.5);
|
|
135
|
+
translateColour.setTranslate(-0.4, -0.9);
|
|
136
|
+
|
|
137
|
+
requestAnimationFrame(drawScene);
|
|
138
|
+
}
|
|
139
|
+
*/
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
type Equals = 0;
|
|
2
|
+
type LessThan = -1;
|
|
3
|
+
type GreaterThan = 1;
|
|
4
|
+
|
|
5
|
+
type Comparator = LessThan | Equals | GreaterThan;
|
|
6
|
+
|
|
7
|
+
type Int32 = number;
|
|
8
|
+
type Float = number;
|
|
9
|
+
|
|
10
|
+
export function flatten<T>(arr: T[][]): T[]{
|
|
11
|
+
const flat = [];
|
|
12
|
+
for(const a of arr){
|
|
13
|
+
for(const e of a){
|
|
14
|
+
flat.push(e);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return flat;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function reverse<T>(arr: T[]){
|
|
21
|
+
const h = Math.floor(arr.length/2);
|
|
22
|
+
for(let i = 0; i < h; i++){
|
|
23
|
+
[arr[i], arr[arr.length-i-1]] = [arr[arr.length-i-1], arr[i]];
|
|
24
|
+
}
|
|
25
|
+
return arr;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function binarySearch<T>(arr: T[], compare: (t:T) => Float): Int32{
|
|
29
|
+
let i = 0;
|
|
30
|
+
let j = arr.length-1;
|
|
31
|
+
while(i <= j){
|
|
32
|
+
const m = Math.trunc((i+j)/2);
|
|
33
|
+
const comp = compare(arr[m]);
|
|
34
|
+
if(comp == 0){
|
|
35
|
+
return m;
|
|
36
|
+
}else if(comp > 0){
|
|
37
|
+
i = m+1;
|
|
38
|
+
}else{ // less
|
|
39
|
+
j = m-1;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return -1;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
export function binarySearchUpperBound<T>(arr: T[], compare: (t:T) => Float): Int32{
|
|
48
|
+
let i = 0;
|
|
49
|
+
let j = arr.length-1;
|
|
50
|
+
while(i <= j){
|
|
51
|
+
const m = Math.trunc((i+j)/2);
|
|
52
|
+
const comp = compare(arr[m]);
|
|
53
|
+
if(comp <= 0){
|
|
54
|
+
j = m - 1;
|
|
55
|
+
}else{
|
|
56
|
+
i = m + 1;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return i;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function binarySearchLowerBound<T>(arr: T[], compare: (t:T) => Float): Int32{
|
|
63
|
+
let i = 0;
|
|
64
|
+
let j = arr.length-1;
|
|
65
|
+
while(i <= j){
|
|
66
|
+
const m = Math.trunc((i+j)/2);
|
|
67
|
+
const comp = compare(arr[m]);
|
|
68
|
+
if(comp >= 0){
|
|
69
|
+
i = m + 1;
|
|
70
|
+
}else{
|
|
71
|
+
j = m - 1;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return j;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function random0ToN(size: Int32): Int32[]{
|
|
78
|
+
const arr = Array.from({length: size}, (_, i) => i);
|
|
79
|
+
for(let i = arr.length-1; i > 0; i--){
|
|
80
|
+
const j = Math.floor(Math.random()*(i+1));
|
|
81
|
+
//new_cards[n] = this.cards[j];
|
|
82
|
+
|
|
83
|
+
[arr[j], arr[i]] = [arr[i], arr[j]];
|
|
84
|
+
}
|
|
85
|
+
return arr;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function testBinarySearch(){
|
|
89
|
+
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export class SortedArray<T>{
|
|
93
|
+
private array: T[];
|
|
94
|
+
private cmp: (a: T, b: T) => Int32;
|
|
95
|
+
constructor(arr: T[]=[], cmp: (a: T, b: T) => Int32){
|
|
96
|
+
this.array = [...arr];
|
|
97
|
+
this.array = this.array.sort(cmp);
|
|
98
|
+
this.cmp = cmp;
|
|
99
|
+
}
|
|
100
|
+
size(): Int32{
|
|
101
|
+
return this.array.length;
|
|
102
|
+
}
|
|
103
|
+
add(ele: T){
|
|
104
|
+
const index = this.lowerBound(ele);
|
|
105
|
+
this.array.splice(index, 0, ele);
|
|
106
|
+
}
|
|
107
|
+
remove(i: Int32){
|
|
108
|
+
if(i < this.array.length && i >= 0){
|
|
109
|
+
this.array.splice(i, 1);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
getArray(): T[]{
|
|
113
|
+
return this.array;
|
|
114
|
+
}
|
|
115
|
+
get(i: Int32): T | undefined{
|
|
116
|
+
if(i < this.array.length && i >= 0){
|
|
117
|
+
return this.array[i];
|
|
118
|
+
}
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
lowerBound(ele: T): Int32{
|
|
122
|
+
let i = 0;
|
|
123
|
+
let j = this.array.length-1;
|
|
124
|
+
while(i <= j){
|
|
125
|
+
let m = Math.floor((i+j)*0.5);
|
|
126
|
+
const c = this.cmp(ele, this.array[m]);
|
|
127
|
+
if(c == 0){
|
|
128
|
+
return m;
|
|
129
|
+
}else if(c < 0){
|
|
130
|
+
j = m - 1;
|
|
131
|
+
}else{
|
|
132
|
+
i = m + 1;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return i;
|
|
136
|
+
}
|
|
137
|
+
//returns any element
|
|
138
|
+
search(ele: T): Int32 | undefined{
|
|
139
|
+
let i = 0;
|
|
140
|
+
let j = this.array.length-1;
|
|
141
|
+
while(i <= j){
|
|
142
|
+
let m = Math.floor((i+j)*0.5);
|
|
143
|
+
const c = this.cmp(ele, this.array[m])
|
|
144
|
+
if(c == 0){
|
|
145
|
+
return m;
|
|
146
|
+
}else if(c < 0){
|
|
147
|
+
j = m - 1;
|
|
148
|
+
}else{
|
|
149
|
+
i = m + 1;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type Constructor = new (...args: any[]) => {};
|
|
2
|
+
export type GConstructor<T = {}> = new (...args: any[]) => T;
|
|
3
|
+
|
|
4
|
+
class V{
|
|
5
|
+
protected val: number;
|
|
6
|
+
constructor(){
|
|
7
|
+
this.val = 0;
|
|
8
|
+
}
|
|
9
|
+
get v(){
|
|
10
|
+
return this.val;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type Vable = GConstructor<V>;
|
|
15
|
+
|
|
16
|
+
function Add1<TBase extends Vable>(Base: TBase){
|
|
17
|
+
return class Add extends Base{
|
|
18
|
+
add1(){
|
|
19
|
+
this.val += 1;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|