matrix-engine-wgpu 1.3.12 → 1.3.16

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.
@@ -1,273 +0,0 @@
1
- /**
2
- * @description
3
- * Created for matrix-engine-wgpu project.
4
- * MeshObj class estends Materials.
5
- * @author Nikola Lukic
6
- * @email zlatnaspirala@gmail.com
7
- */
8
-
9
- export default class Materials {
10
- constructor(device) {
11
- this.device = device;
12
- this.isVideo = false;
13
- // For shadow comparison
14
- this.compareSampler = this.device.createSampler({compare: 'less'});
15
- // For image textures (standard sampler)
16
- this.imageSampler = this.device.createSampler({
17
- magFilter: 'linear',
18
- minFilter: 'linear',
19
- });
20
- // For external video textures (needs to be filtering sampler too!)
21
- this.videoSampler = this.device.createSampler({
22
- magFilter: 'linear',
23
- minFilter: 'linear',
24
- });
25
- }
26
-
27
- async loadTex0(texturesPaths) {
28
- this.sampler = this.device.createSampler({
29
- magFilter: 'linear',
30
- minFilter: 'linear',
31
- });
32
- return new Promise(async (resolve) => {
33
- const response = await fetch(texturesPaths[0]);
34
- const imageBitmap = await createImageBitmap(await response.blob());
35
- this.texture0 = this.device.createTexture({
36
- size: [imageBitmap.width, imageBitmap.height, 1], // REMOVED 1
37
- format: 'rgba8unorm',
38
- usage:
39
- GPUTextureUsage.TEXTURE_BINDING |
40
- GPUTextureUsage.COPY_DST |
41
- GPUTextureUsage.RENDER_ATTACHMENT,
42
- });
43
- this.device.queue.copyExternalImageToTexture(
44
- {source: imageBitmap},
45
- {texture: this.texture0},
46
- [imageBitmap.width, imageBitmap.height]
47
- );
48
- resolve()
49
- })
50
- }
51
-
52
- async loadVideoTexture(arg) {
53
- this.isVideo = true;
54
- if(arg.type === 'video') {
55
- this.video = document.createElement('video');
56
- this.video.src = arg.src || 'res/videos/tunel.mp4';
57
- this.video.crossOrigin = 'anonymous';
58
- this.video.autoplay = true;
59
- this.video.loop = true;
60
- document.body.append(this.video);
61
- this.video.style.display = 'none';
62
- await this.video.play();
63
- } else if(arg.type === 'videoElement') {
64
- this.video = arg.el;
65
- await this.video.play();
66
- } else if(arg.type === 'camera') {
67
- this.video = document.createElement('video');
68
- this.video.autoplay = true;
69
- this.video.muted = true;
70
- this.video.playsInline = true;
71
- this.video.style.display = 'none';
72
- document.body.append(this.video);
73
-
74
- try {
75
- const stream = await (navigator.mediaDevices?.getUserMedia?.({
76
- video: {
77
- width: {ideal: 1280},
78
- height: {ideal: 720},
79
- },
80
- audio: false
81
- }));
82
-
83
- this.video.srcObject = stream;
84
- await this.video.play();
85
- } catch(err) {
86
- console.error("❌ Failed to access camera:", err);
87
- return;
88
- }
89
- } else if(arg.type === 'canvas2d') {
90
- // Existing canvas (arg.el) — assume it's actively drawing
91
- this.video = document.createElement('video');
92
- this.video.autoplay = true;
93
- this.video.muted = true;
94
- this.video.playsInline = true;
95
- this.video.style.display = 'none';
96
- document.body.append(this.video);
97
-
98
- // Create stream from existing canvas
99
- const stream = arg.el.captureStream?.() || arg.el.mozCaptureStream?.();
100
- if(!stream) {
101
- console.error('❌ Cannot capture stream from canvas2d');
102
- return;
103
- }
104
-
105
- this.video.srcObject = stream;
106
- await this.video.play();
107
-
108
- } else if(arg.type === 'canvas2d-inline') {
109
- // Miniature inline-drawn canvas created dynamically
110
- const canvas = document.createElement('canvas');
111
- canvas.width = arg.width || 256;
112
- canvas.height = arg.height || 256;
113
- const ctx = canvas.getContext('2d');
114
-
115
- if(typeof arg.canvaInlineProgram === 'function') {
116
- // Start drawing loop
117
- const drawLoop = () => {
118
- arg.canvaInlineProgram(ctx, canvas);
119
- requestAnimationFrame(drawLoop);
120
- };
121
- drawLoop();
122
- }
123
-
124
- this.video = document.createElement('video');
125
- this.video.autoplay = true;
126
- this.video.muted = true;
127
- this.video.playsInline = true;
128
- this.video.style.display = 'none';
129
- document.body.append(this.video);
130
-
131
- const stream = canvas.captureStream?.() || canvas.mozCaptureStream?.();
132
- if(!stream) {
133
- console.error('❌ Cannot capture stream from inline canvas');
134
- return;
135
- }
136
-
137
- this.video.srcObject = stream;
138
- await this.video.play();
139
- }
140
-
141
- this.sampler = this.device.createSampler({
142
- magFilter: 'linear',
143
- minFilter: 'linear',
144
- });
145
-
146
- // ✅ Now
147
- // includes externalTexture type
148
- this.createLayoutForRender();
149
- this.setupPipeline();
150
- setTimeout(() => this.createBindGroupForRender(), 1500);
151
- }
152
-
153
- updateVideoTexture() {
154
- if(!this.video || this.video.readyState < 2) return;
155
- this.externalTexture = this.device.importExternalTexture({source: this.video});
156
- this.createBindGroupForRender();
157
- }
158
-
159
- createBindGroupForRender() {
160
- const textureResource = this.isVideo
161
- ? this.externalTexture // must be set via updateVideoTexture
162
- : this.texture0.createView();
163
- // Log all bindings to debug
164
- if(!textureResource || !this.sceneUniformBuffer || !this.shadowDepthTextureView || !this.sampler) {
165
- console.warn("❗Missing res skipping...");
166
- return;
167
- }
168
-
169
- if(this.isVideo == true) {
170
- this.sceneBindGroupForRender = this.device.createBindGroup({
171
- layout: this.bglForRender,
172
- entries: [
173
- {
174
- binding: 0,
175
- resource: {buffer: this.sceneUniformBuffer},
176
- },
177
- {
178
- binding: 1,
179
- resource: this.shadowDepthTextureView,
180
- },
181
- {
182
- binding: 2,
183
- resource: this.compareSampler,
184
- },
185
- {
186
- binding: 3,
187
- resource: textureResource,
188
- },
189
- {
190
- binding: 4,
191
- resource: this.videoSampler,
192
- },
193
- ],
194
- });
195
- } else {
196
- this.sceneBindGroupForRender = this.device.createBindGroup({
197
- layout: this.bglForRender,
198
- entries: [
199
- {
200
- binding: 0,
201
- resource: {buffer: this.sceneUniformBuffer},
202
- },
203
- {
204
- binding: 1,
205
- resource: this.shadowDepthTextureView,
206
- },
207
- {
208
- binding: 2,
209
- resource: this.compareSampler,
210
- },
211
- {
212
- binding: 3,
213
- resource: textureResource,
214
- },
215
- {
216
- binding: 4,
217
- resource: this.imageSampler,
218
- },
219
- ],
220
- });
221
- }
222
- }
223
-
224
- createLayoutForRender() {
225
- this.bglForRender = this.device.createBindGroupLayout({
226
- entries: [
227
- {
228
- binding: 0,
229
- visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
230
- buffer: {type: 'uniform'},
231
- },
232
- {
233
- binding: 1,
234
- visibility: GPUShaderStage.FRAGMENT,
235
- texture: {sampleType: 'depth'},
236
- },
237
- {
238
- binding: 2,
239
- visibility: GPUShaderStage.FRAGMENT,
240
- sampler: {type: 'comparison'},
241
- },
242
- ...(this.isVideo
243
- ? [ // VIDEO
244
- {
245
- binding: 3,
246
- visibility: GPUShaderStage.FRAGMENT,
247
- externalTexture: {},
248
- },
249
- {
250
- binding: 4,
251
- visibility: GPUShaderStage.FRAGMENT,
252
- sampler: {type: 'filtering'}, // for video sampling
253
- },
254
- ]
255
- : [ // IMAGE
256
- {
257
- binding: 3,
258
- visibility: GPUShaderStage.FRAGMENT,
259
- texture: {
260
- sampleType: 'float',
261
- viewDimension: '2d',
262
- },
263
- },
264
- {
265
- binding: 4,
266
- visibility: GPUShaderStage.FRAGMENT,
267
- sampler: {type: 'filtering'},
268
- },
269
- ])
270
- ],
271
- });
272
- }
273
- }
@@ -1,252 +0,0 @@
1
- import {degToRad, radToDeg} from "./utils";
2
-
3
- /**
4
- * @description
5
- * Sub classes for matrix-wgpu
6
- * Base class
7
- * Position { x, y, z }
8
- */
9
-
10
- export class Position {
11
- constructor(x, y, z) {
12
- // console.log('TEST TYTPOF ', x)
13
- // Not in use for nwo this is from matrix-engine project [nameUniq]
14
- this.nameUniq = null;
15
-
16
- if(typeof x == 'undefined') x = 0;
17
- if(typeof y == 'undefined') y = 0;
18
- if(typeof z == 'undefined') z = 0;
19
-
20
- this.x = parseFloat(x);
21
- this.y = parseFloat(y);
22
- this.z = parseFloat(z);
23
-
24
- this.velY = 0;
25
- this.velX = 0;
26
- this.velZ = 0;
27
- this.inMove = false;
28
- this.targetX = parseFloat(x);
29
- this.targetY = parseFloat(y);
30
- this.targetZ = parseFloat(z);
31
- this.thrust = 0.01;
32
-
33
- return this;
34
- }
35
-
36
- setSpeed(n) {
37
- if(typeof n === 'number') {
38
- this.thrust = n;
39
- } else {
40
- console.log('Description: arguments (w, h) must be type of number.');
41
- }
42
- }
43
-
44
- translateByX(x) {
45
- this.inMove = true;
46
- this.targetX = parseFloat(x);
47
- };
48
-
49
- translateByY(y) {
50
- this.inMove = true;
51
- this.targetY = parseFloat(y);
52
- }
53
-
54
- translateByZ(z) {
55
- this.inMove = true;
56
- this.targetZ = parseFloat(z);
57
- }
58
-
59
- translateByXY(x, y) {
60
- this.inMove = true;
61
- this.targetX = parseFloat(x);
62
- this.targetY = parseFloat(y);
63
- }
64
-
65
- translateByXZ(x, z) {
66
- this.inMove = true;
67
- this.targetX = parseFloat(x);
68
- this.targetZ = parseFloat(z);
69
- }
70
-
71
- translateByYZ(y, z) {
72
- this.inMove = true;
73
- this.targetY = parseFloat(y);
74
- this.targetZ = parseFloat(z);
75
- }
76
-
77
- onTargetPositionReach() {}
78
-
79
- update() {
80
- var tx = parseFloat(this.targetX) - parseFloat(this.x),
81
- ty = parseFloat(this.targetY) - parseFloat(this.y),
82
- tz = parseFloat(this.targetZ) - parseFloat(this.z),
83
- dist = Math.sqrt(tx * tx + ty * ty + tz * tz);
84
- this.velX = (tx / dist) * this.thrust;
85
- this.velY = (ty / dist) * this.thrust;
86
- this.velZ = (tz / dist) * this.thrust;
87
- if(this.inMove == true) {
88
- if(dist > this.thrust) {
89
- this.x += this.velX;
90
- this.y += this.velY;
91
- this.z += this.velZ;
92
-
93
- // // from me
94
- // if(net && net.connection && typeof em === 'undefined' && App.scene[this.nameUniq].net.enable == true) net.connection.send({
95
- // netPos: {x: this.x, y: this.y, z: this.z},
96
- // netObjId: this.nameUniq,
97
- // });
98
-
99
- } else {
100
- this.x = this.targetX;
101
- this.y = this.targetY;
102
- this.z = this.targetZ;
103
- this.inMove = false;
104
- this.onTargetPositionReach();
105
-
106
- // // from me
107
- // if(net && net.connection && typeof em === 'undefined' && App.scene[this.nameUniq].net.enable == true) net.connection.send({
108
- // netPos: {x: this.x, y: this.y, z: this.z},
109
- // netObjId: this.nameUniq,
110
- // });
111
- }
112
- }
113
- }
114
-
115
- get worldLocation() {
116
- return [parseFloat(this.x), parseFloat(this.y), parseFloat(this.z)];
117
- }
118
-
119
- SetX(newx, em) {
120
- this.x = newx;
121
- this.targetX = newx;
122
- this.inMove = false;
123
-
124
- // if(net && net.connection && typeof em === 'undefined' &&
125
- // App.scene[this.nameUniq].net && App.scene[this.nameUniq].net.enable == true) {
126
- // net.connection.send({
127
- // netPos: {x: this.x, y: this.y, z: this.z},
128
- // netObjId: this.nameUniq,
129
- // });
130
- // }
131
- }
132
-
133
- SetY(newy, em) {
134
- this.y = newy;
135
- this.targetY = newy;
136
- this.inMove = false;
137
- // if(net && net.connection && typeof em === 'undefined' &&
138
- // App.scene[this.nameUniq].net && App.scene[this.nameUniq].net.enable == true) net.connection.send({
139
- // netPos: {x: this.x, y: this.y, z: this.z},
140
- // netObjId: this.nameUniq,
141
- // });
142
- }
143
-
144
- SetZ(newz, em) {
145
- this.z = newz;
146
- this.targetZ = newz;
147
- this.inMove = false;
148
- // if(net && net.connection && typeof em === 'undefined' &&
149
- // App.scene[this.nameUniq].net && App.scene[this.nameUniq].net.enable == true) net.connection.send({
150
- // netPos: {x: this.x, y: this.y, z: this.z},
151
- // netObjId: this.nameUniq,
152
- // });
153
- }
154
-
155
- get X() {
156
- return parseFloat(this.x)
157
- }
158
-
159
- get Y() {
160
- return parseFloat(this.y)
161
- }
162
-
163
- get Z() {
164
- return parseFloat(this.z)
165
- }
166
-
167
- setPosition(newx, newy, newz) {
168
- this.x = newx;
169
- this.y = newy;
170
- this.z = newz;
171
- this.targetX = newx;
172
- this.targetY = newy;
173
- this.targetZ = newz;
174
- this.inMove = false;
175
-
176
- // from me
177
- // if(App.scene[this.nameUniq] && net && net.connection && typeof em === 'undefined' &&
178
- // App.scene[this.nameUniq].net && App.scene[this.nameUniq].net.enable == true) net.connection.send({
179
- // netPos: {x: this.x, y: this.y, z: this.z},
180
- // netObjId: this.nameUniq,
181
- // });
182
- }
183
- }
184
-
185
- export class Rotation {
186
-
187
- constructor(x, y, z) {
188
- // Not in use for nwo this is from matrix-engine project [nameUniq]
189
- this.nameUniq = null;
190
- if(typeof x == 'undefined') x = 0;
191
- if(typeof y == 'undefined') y = 0;
192
- if(typeof z == 'undefined') z = 0;
193
- this.x = x;
194
- this.y = y;
195
- this.z = z;
196
- this.rotationSpeed = {x: 0, y: 0, z: 0};
197
- this.angle = 0;
198
- this.axis = {x: 0, y: 0, z: 0};
199
- // not in use good for exstend logic
200
- this.matrixRotation = null;
201
- }
202
-
203
- toDegree() {
204
-
205
- /*
206
- heading = atan2(y * sin(angle)- x * z * (1 - cos(angle)) , 1 - (y2 + z2 ) * (1 - cos(angle)))
207
- attitude = asin(x * y * (1 - cos(angle)) + z * sin(angle))
208
- bank = atan2(x * sin(angle)-y * z * (1 - cos(angle)) , 1 - (x2 + z2) * (1 - cos(angle)))
209
- */
210
- return [radToDeg(this.axis.x), radToDeg(this.axis.y), radToDeg(this.axis.z)];
211
- }
212
-
213
- toDegreeX() {
214
- return Math.cos(radToDeg(this.axis.x) / 2)
215
- }
216
-
217
- toDegreeY() {
218
- return Math.cos(radToDeg(this.axis.z) / 2)
219
- }
220
-
221
- toDegreeZ() {
222
- return Math.cos(radToDeg(this.axis.y) / 2)
223
- }
224
-
225
- getRotX() {
226
- if(this.rotationSpeed.x == 0) {
227
- return degToRad(this.x);
228
- } else {
229
- this.x = this.x + this.rotationSpeed.x * 0.001;
230
- return degToRad(this.x);
231
- }
232
- }
233
-
234
- getRotY() {
235
- if(this.rotationSpeed.y == 0) {
236
- return degToRad(this.y);
237
- } else {
238
- this.y = this.y + this.rotationSpeed.y * 0.001;
239
- return degToRad(this.y);
240
- }
241
- }
242
-
243
- getRotZ() {
244
- if(this.rotationSpeed.z == 0) {
245
- return degToRad(this.z);
246
- } else {
247
- this.z = this.z + this.rotationSpeed.z * 0.001;
248
- return degToRad(this.z);
249
- }
250
- }
251
-
252
- }