matrix-engine-wgpu 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,353 @@
1
+
2
+ export const vec3 = {
3
+ cross(a, b, dst) {
4
+ dst = dst || new Float32Array(3);
5
+
6
+ const t0 = a[1] * b[2] - a[2] * b[1];
7
+ const t1 = a[2] * b[0] - a[0] * b[2];
8
+ const t2 = a[0] * b[1] - a[1] * b[0];
9
+
10
+ dst[0] = t0;
11
+ dst[1] = t1;
12
+ dst[2] = t2;
13
+
14
+ return dst;
15
+ },
16
+
17
+ subtract(a, b, dst) {
18
+ dst = dst || new Float32Array(3);
19
+
20
+ dst[0] = a[0] - b[0];
21
+ dst[1] = a[1] - b[1];
22
+ dst[2] = a[2] - b[2];
23
+
24
+ return dst;
25
+ },
26
+
27
+ normalize(v, dst) {
28
+ dst = dst || new Float32Array(3);
29
+
30
+ const length = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
31
+ // make sure we don't divide by 0.
32
+ if (length > 0.00001) {
33
+ dst[0] = v[0] / length;
34
+ dst[1] = v[1] / length;
35
+ dst[2] = v[2] / length;
36
+ } else {
37
+ dst[0] = 0;
38
+ dst[1] = 0;
39
+ dst[2] = 0;
40
+ }
41
+
42
+ return dst;
43
+ },
44
+ };
45
+
46
+ export const mat4 = {
47
+ projection(width, height, depth, dst) {
48
+ // Note: This matrix flips the Y axis so that 0 is at the top.
49
+ return mat4.ortho(0, width, height, 0, depth, -depth, dst);
50
+ },
51
+
52
+ perspective(fieldOfViewYInRadians, aspect, zNear, zFar, dst) {
53
+ dst = dst || new Float32Array(16);
54
+
55
+ const f = Math.tan(Math.PI * 0.5 - 0.5 * fieldOfViewYInRadians);
56
+ const rangeInv = 1 / (zNear - zFar);
57
+
58
+ dst[0] = f / aspect;
59
+ dst[1] = 0;
60
+ dst[2] = 0;
61
+ dst[3] = 0;
62
+
63
+ dst[4] = 0;
64
+ dst[5] = f;
65
+ dst[6] = 0;
66
+ dst[7] = 0;
67
+
68
+ dst[8] = 0;
69
+ dst[9] = 0;
70
+ dst[10] = zFar * rangeInv;
71
+ dst[11] = -1;
72
+
73
+ dst[12] = 0;
74
+ dst[13] = 0;
75
+ dst[14] = zNear * zFar * rangeInv;
76
+ dst[15] = 0;
77
+
78
+ return dst;
79
+ },
80
+
81
+ ortho(left, right, bottom, top, near, far, dst) {
82
+ dst = dst || new Float32Array(16);
83
+
84
+ dst[0] = 2 / (right - left);
85
+ dst[1] = 0;
86
+ dst[2] = 0;
87
+ dst[3] = 0;
88
+
89
+ dst[4] = 0;
90
+ dst[5] = 2 / (top - bottom);
91
+ dst[6] = 0;
92
+ dst[7] = 0;
93
+
94
+ dst[8] = 0;
95
+ dst[9] = 0;
96
+ dst[10] = 1 / (near - far);
97
+ dst[11] = 0;
98
+
99
+ dst[12] = (right + left) / (left - right);
100
+ dst[13] = (top + bottom) / (bottom - top);
101
+ dst[14] = near / (near - far);
102
+ dst[15] = 1;
103
+
104
+ return dst;
105
+ },
106
+
107
+ identity(dst) {
108
+ dst = dst || new Float32Array(16);
109
+ dst[ 0] = 1; dst[ 1] = 0; dst[ 2] = 0; dst[ 3] = 0;
110
+ dst[ 4] = 0; dst[ 5] = 1; dst[ 6] = 0; dst[ 7] = 0;
111
+ dst[ 8] = 0; dst[ 9] = 0; dst[10] = 1; dst[11] = 0;
112
+ dst[12] = 0; dst[13] = 0; dst[14] = 0; dst[15] = 1;
113
+ return dst;
114
+ },
115
+
116
+ multiply(a, b, dst) {
117
+ dst = dst || new Float32Array(16);
118
+ const b00 = b[0 * 4 + 0];
119
+ const b01 = b[0 * 4 + 1];
120
+ const b02 = b[0 * 4 + 2];
121
+ const b03 = b[0 * 4 + 3];
122
+ const b10 = b[1 * 4 + 0];
123
+ const b11 = b[1 * 4 + 1];
124
+ const b12 = b[1 * 4 + 2];
125
+ const b13 = b[1 * 4 + 3];
126
+ const b20 = b[2 * 4 + 0];
127
+ const b21 = b[2 * 4 + 1];
128
+ const b22 = b[2 * 4 + 2];
129
+ const b23 = b[2 * 4 + 3];
130
+ const b30 = b[3 * 4 + 0];
131
+ const b31 = b[3 * 4 + 1];
132
+ const b32 = b[3 * 4 + 2];
133
+ const b33 = b[3 * 4 + 3];
134
+ const a00 = a[0 * 4 + 0];
135
+ const a01 = a[0 * 4 + 1];
136
+ const a02 = a[0 * 4 + 2];
137
+ const a03 = a[0 * 4 + 3];
138
+ const a10 = a[1 * 4 + 0];
139
+ const a11 = a[1 * 4 + 1];
140
+ const a12 = a[1 * 4 + 2];
141
+ const a13 = a[1 * 4 + 3];
142
+ const a20 = a[2 * 4 + 0];
143
+ const a21 = a[2 * 4 + 1];
144
+ const a22 = a[2 * 4 + 2];
145
+ const a23 = a[2 * 4 + 3];
146
+ const a30 = a[3 * 4 + 0];
147
+ const a31 = a[3 * 4 + 1];
148
+ const a32 = a[3 * 4 + 2];
149
+ const a33 = a[3 * 4 + 3];
150
+
151
+ dst[0] = b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30;
152
+ dst[1] = b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31;
153
+ dst[2] = b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32;
154
+ dst[3] = b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33;
155
+
156
+ dst[4] = b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30;
157
+ dst[5] = b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31;
158
+ dst[6] = b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32;
159
+ dst[7] = b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33;
160
+
161
+ dst[8] = b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30;
162
+ dst[9] = b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31;
163
+ dst[10] = b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32;
164
+ dst[11] = b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33;
165
+
166
+ dst[12] = b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30;
167
+ dst[13] = b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31;
168
+ dst[14] = b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32;
169
+ dst[15] = b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33;
170
+
171
+ return dst;
172
+ },
173
+
174
+ cameraAim(eye, target, up, dst) {
175
+ dst = dst || new Float32Array(16);
176
+
177
+ const zAxis = vec3.normalize(vec3.subtract(eye, target));
178
+ const xAxis = vec3.normalize(vec3.cross(up, zAxis));
179
+ const yAxis = vec3.normalize(vec3.cross(zAxis, xAxis));
180
+
181
+ dst[ 0] = xAxis[0]; dst[ 1] = xAxis[1]; dst[ 2] = xAxis[2]; dst[ 3] = 0;
182
+ dst[ 4] = yAxis[0]; dst[ 5] = yAxis[1]; dst[ 6] = yAxis[2]; dst[ 7] = 0;
183
+ dst[ 8] = zAxis[0]; dst[ 9] = zAxis[1]; dst[10] = zAxis[2]; dst[11] = 0;
184
+ dst[12] = eye[0]; dst[13] = eye[1]; dst[14] = eye[2]; dst[15] = 1;
185
+
186
+ return dst;
187
+ },
188
+
189
+ inverse(m, dst) {
190
+ dst = dst || new Float32Array(16);
191
+
192
+ const m00 = m[0 * 4 + 0];
193
+ const m01 = m[0 * 4 + 1];
194
+ const m02 = m[0 * 4 + 2];
195
+ const m03 = m[0 * 4 + 3];
196
+ const m10 = m[1 * 4 + 0];
197
+ const m11 = m[1 * 4 + 1];
198
+ const m12 = m[1 * 4 + 2];
199
+ const m13 = m[1 * 4 + 3];
200
+ const m20 = m[2 * 4 + 0];
201
+ const m21 = m[2 * 4 + 1];
202
+ const m22 = m[2 * 4 + 2];
203
+ const m23 = m[2 * 4 + 3];
204
+ const m30 = m[3 * 4 + 0];
205
+ const m31 = m[3 * 4 + 1];
206
+ const m32 = m[3 * 4 + 2];
207
+ const m33 = m[3 * 4 + 3];
208
+
209
+ const tmp0 = m22 * m33;
210
+ const tmp1 = m32 * m23;
211
+ const tmp2 = m12 * m33;
212
+ const tmp3 = m32 * m13;
213
+ const tmp4 = m12 * m23;
214
+ const tmp5 = m22 * m13;
215
+ const tmp6 = m02 * m33;
216
+ const tmp7 = m32 * m03;
217
+ const tmp8 = m02 * m23;
218
+ const tmp9 = m22 * m03;
219
+ const tmp10 = m02 * m13;
220
+ const tmp11 = m12 * m03;
221
+ const tmp12 = m20 * m31;
222
+ const tmp13 = m30 * m21;
223
+ const tmp14 = m10 * m31;
224
+ const tmp15 = m30 * m11;
225
+ const tmp16 = m10 * m21;
226
+ const tmp17 = m20 * m11;
227
+ const tmp18 = m00 * m31;
228
+ const tmp19 = m30 * m01;
229
+ const tmp20 = m00 * m21;
230
+ const tmp21 = m20 * m01;
231
+ const tmp22 = m00 * m11;
232
+ const tmp23 = m10 * m01;
233
+
234
+ const t0 = (tmp0 * m11 + tmp3 * m21 + tmp4 * m31) -
235
+ (tmp1 * m11 + tmp2 * m21 + tmp5 * m31);
236
+ const t1 = (tmp1 * m01 + tmp6 * m21 + tmp9 * m31) -
237
+ (tmp0 * m01 + tmp7 * m21 + tmp8 * m31);
238
+ const t2 = (tmp2 * m01 + tmp7 * m11 + tmp10 * m31) -
239
+ (tmp3 * m01 + tmp6 * m11 + tmp11 * m31);
240
+ const t3 = (tmp5 * m01 + tmp8 * m11 + tmp11 * m21) -
241
+ (tmp4 * m01 + tmp9 * m11 + tmp10 * m21);
242
+
243
+ const d = 1 / (m00 * t0 + m10 * t1 + m20 * t2 + m30 * t3);
244
+
245
+ dst[0] = d * t0;
246
+ dst[1] = d * t1;
247
+ dst[2] = d * t2;
248
+ dst[3] = d * t3;
249
+
250
+ dst[4] = d * ((tmp1 * m10 + tmp2 * m20 + tmp5 * m30) -
251
+ (tmp0 * m10 + tmp3 * m20 + tmp4 * m30));
252
+ dst[5] = d * ((tmp0 * m00 + tmp7 * m20 + tmp8 * m30) -
253
+ (tmp1 * m00 + tmp6 * m20 + tmp9 * m30));
254
+ dst[6] = d * ((tmp3 * m00 + tmp6 * m10 + tmp11 * m30) -
255
+ (tmp2 * m00 + tmp7 * m10 + tmp10 * m30));
256
+ dst[7] = d * ((tmp4 * m00 + tmp9 * m10 + tmp10 * m20) -
257
+ (tmp5 * m00 + tmp8 * m10 + tmp11 * m20));
258
+
259
+ dst[8] = d * ((tmp12 * m13 + tmp15 * m23 + tmp16 * m33) -
260
+ (tmp13 * m13 + tmp14 * m23 + tmp17 * m33));
261
+ dst[9] = d * ((tmp13 * m03 + tmp18 * m23 + tmp21 * m33) -
262
+ (tmp12 * m03 + tmp19 * m23 + tmp20 * m33));
263
+ dst[10] = d * ((tmp14 * m03 + tmp19 * m13 + tmp22 * m33) -
264
+ (tmp15 * m03 + tmp18 * m13 + tmp23 * m33));
265
+ dst[11] = d * ((tmp17 * m03 + tmp20 * m13 + tmp23 * m23) -
266
+ (tmp16 * m03 + tmp21 * m13 + tmp22 * m23));
267
+
268
+ dst[12] = d * ((tmp14 * m22 + tmp17 * m32 + tmp13 * m12) -
269
+ (tmp16 * m32 + tmp12 * m12 + tmp15 * m22));
270
+ dst[13] = d * ((tmp20 * m32 + tmp12 * m02 + tmp19 * m22) -
271
+ (tmp18 * m22 + tmp21 * m32 + tmp13 * m02));
272
+ dst[14] = d * ((tmp18 * m12 + tmp23 * m32 + tmp15 * m02) -
273
+ (tmp22 * m32 + tmp14 * m02 + tmp19 * m12));
274
+ dst[15] = d * ((tmp22 * m22 + tmp16 * m02 + tmp21 * m12) -
275
+ (tmp20 * m12 + tmp23 * m22 + tmp17 * m02));
276
+ return dst;
277
+ },
278
+
279
+ lookAt(eye, target, up, dst) {
280
+ return mat4.inverse(mat4.cameraAim(eye, target, up, dst), dst);
281
+ },
282
+
283
+ translation([tx, ty, tz], dst) {
284
+ dst = dst || new Float32Array(16);
285
+ dst[ 0] = 1; dst[ 1] = 0; dst[ 2] = 0; dst[ 3] = 0;
286
+ dst[ 4] = 0; dst[ 5] = 1; dst[ 6] = 0; dst[ 7] = 0;
287
+ dst[ 8] = 0; dst[ 9] = 0; dst[10] = 1; dst[11] = 0;
288
+ dst[12] = tx; dst[13] = ty; dst[14] = tz; dst[15] = 1;
289
+ return dst;
290
+ },
291
+
292
+ rotationX(angleInRadians, dst) {
293
+ const c = Math.cos(angleInRadians);
294
+ const s = Math.sin(angleInRadians);
295
+ dst = dst || new Float32Array(16);
296
+ dst[ 0] = 1; dst[ 1] = 0; dst[ 2] = 0; dst[ 3] = 0;
297
+ dst[ 4] = 0; dst[ 5] = c; dst[ 6] = s; dst[ 7] = 0;
298
+ dst[ 8] = 0; dst[ 9] = -s; dst[10] = c; dst[11] = 0;
299
+ dst[12] = 0; dst[13] = 0; dst[14] = 0; dst[15] = 1;
300
+ return dst;
301
+ },
302
+
303
+ rotationY(angleInRadians, dst) {
304
+ const c = Math.cos(angleInRadians);
305
+ const s = Math.sin(angleInRadians);
306
+ dst = dst || new Float32Array(16);
307
+ dst[ 0] = c; dst[ 1] = 0; dst[ 2] = -s; dst[ 3] = 0;
308
+ dst[ 4] = 0; dst[ 5] = 1; dst[ 6] = 0; dst[ 7] = 0;
309
+ dst[ 8] = s; dst[ 9] = 0; dst[10] = c; dst[11] = 0;
310
+ dst[12] = 0; dst[13] = 0; dst[14] = 0; dst[15] = 1;
311
+ return dst;
312
+ },
313
+
314
+ rotationZ(angleInRadians, dst) {
315
+ const c = Math.cos(angleInRadians);
316
+ const s = Math.sin(angleInRadians);
317
+ dst = dst || new Float32Array(16);
318
+ dst[ 0] = c; dst[ 1] = s; dst[ 2] = 0; dst[ 3] = 0;
319
+ dst[ 4] = -s; dst[ 5] = c; dst[ 6] = 0; dst[ 7] = 0;
320
+ dst[ 8] = 0; dst[ 9] = 0; dst[10] = 1; dst[11] = 0;
321
+ dst[12] = 0; dst[13] = 0; dst[14] = 0; dst[15] = 1;
322
+ return dst;
323
+ },
324
+
325
+ scaling([sx, sy, sz], dst) {
326
+ dst = dst || new Float32Array(16);
327
+ dst[ 0] = sx; dst[ 1] = 0; dst[ 2] = 0; dst[ 3] = 0;
328
+ dst[ 4] = 0; dst[ 5] = sy; dst[ 6] = 0; dst[ 7] = 0;
329
+ dst[ 8] = 0; dst[ 9] = 0; dst[10] = sz; dst[11] = 0;
330
+ dst[12] = 0; dst[13] = 0; dst[14] = 0; dst[15] = 1;
331
+ return dst;
332
+ },
333
+
334
+ translate(m, translation, dst) {
335
+ return mat4.multiply(m, mat4.translation(translation), dst);
336
+ },
337
+
338
+ rotateX(m, angleInRadians, dst) {
339
+ return mat4.multiply(m, mat4.rotationX(angleInRadians), dst);
340
+ },
341
+
342
+ rotateY(m, angleInRadians, dst) {
343
+ return mat4.multiply(m, mat4.rotationY(angleInRadians), dst);
344
+ },
345
+
346
+ rotateZ(m, angleInRadians, dst) {
347
+ return mat4.multiply(m, mat4.rotationZ(angleInRadians), dst);
348
+ },
349
+
350
+ scale(m, scale, dst) {
351
+ return mat4.multiply(m, mat4.scaling(scale), dst);
352
+ },
353
+ };
File without changes
package/src/meWGPU.js ADDED
@@ -0,0 +1,92 @@
1
+ import MEBall from "./engine/ball.js";
2
+ import MECube from './engine/cube.js';
3
+ // import { mat4, vec3 } from 'wgpu-matrix';
4
+ // import { createSphereMesh, SphereLayout } from './src/engine/ballsBuffer.js';
5
+
6
+ export default class MatrixEngineWGPU {
7
+
8
+ mainRenderBundle = [];
9
+ rbContainer = [];
10
+
11
+ constructor(callback) {
12
+ var canvas = document.createElement('canvas')
13
+ canvas.width = window.innerWidth;
14
+ canvas.height = window.innerHeight;
15
+ document.body.append(canvas)
16
+
17
+ this.init({canvas, callback})
18
+ }
19
+
20
+ init = async ({canvas, callback}) => {
21
+ this.canvas = canvas;
22
+ this.adapter = await navigator.gpu.requestAdapter();
23
+ this.device = await this.adapter.requestDevice();
24
+ this.context = canvas.getContext('webgpu');
25
+
26
+ const devicePixelRatio = window.devicePixelRatio;
27
+ canvas.width = canvas.clientWidth * devicePixelRatio;
28
+ canvas.height = canvas.clientHeight * devicePixelRatio;
29
+ const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
30
+
31
+ this.context.configure({
32
+ device: this.device,
33
+ format: presentationFormat,
34
+ alphaMode: 'premultiplied',
35
+ });
36
+
37
+ this.run(callback)
38
+ };
39
+
40
+ addCube = (o) => {
41
+ if(typeof o === 'undefined') {
42
+ var o = {
43
+ position: {x: 0, y: 0, z: -4},
44
+ texturesPaths: ['./res/textures/default.png']
45
+ }
46
+ } else {
47
+ if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
48
+ if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
49
+ }
50
+ let myCube1 = new MECube(this.canvas, this.device, this.context, o)
51
+ this.mainRenderBundle.push(myCube1);
52
+ }
53
+
54
+ addBall = (o) => {
55
+ if(typeof o === 'undefined') {
56
+ var o = {
57
+ position: {x: 0, y: 0, z: -4},
58
+ texturesPaths: ['./res/textures/default.png']
59
+ }
60
+ } else {
61
+ if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
62
+ if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
63
+ }
64
+ let myBall1 = new MEBall(this.canvas, this.device, this.context, o)
65
+ this.mainRenderBundle.push(myBall1);
66
+ }
67
+
68
+ run(callback) {
69
+ setTimeout(() => {requestAnimationFrame(this.frame)}, 1000)
70
+ setTimeout(() => {callback()}, 10)
71
+ }
72
+
73
+ frame = () => {
74
+ let commandEncoder = this.device.createCommandEncoder();
75
+ this.rbContainer = [];
76
+
77
+ let passEncoder;
78
+
79
+ this.mainRenderBundle.forEach((meItem, index) => {
80
+ meItem.draw();
81
+ this.rbContainer.push(meItem.renderBundle)
82
+ if(index == 0) passEncoder = commandEncoder.beginRenderPass(meItem.renderPassDescriptor);
83
+ })
84
+
85
+ // passEncoder.executeBundles([NIK.renderBundle, NIK2.renderBundle]);
86
+ passEncoder.executeBundles(this.rbContainer);
87
+ passEncoder.end();
88
+ this.device.queue.submit([commandEncoder.finish()]);
89
+
90
+ requestAnimationFrame(this.frame);
91
+ }
92
+ }
@@ -0,0 +1,171 @@
1
+
2
+ /**
3
+ * @description
4
+ * For microdraw pixel cube texture
5
+ */
6
+ export const shaderSrc = `struct VSUniforms {
7
+ worldViewProjection: mat4x4f,
8
+ worldInverseTranspose: mat4x4f,
9
+ };
10
+ @group(0) @binding(0) var<uniform> vsUniforms: VSUniforms;
11
+
12
+ struct MyVSInput {
13
+ @location(0) position: vec4f,
14
+ @location(1) normal: vec3f,
15
+ @location(2) texcoord: vec2f,
16
+ };
17
+
18
+ struct MyVSOutput {
19
+ @builtin(position) position: vec4f,
20
+ @location(0) normal: vec3f,
21
+ @location(1) texcoord: vec2f,
22
+ };
23
+
24
+ @vertex
25
+ fn myVSMain(v: MyVSInput) -> MyVSOutput {
26
+ var vsOut: MyVSOutput;
27
+ vsOut.position = vsUniforms.worldViewProjection * v.position;
28
+ vsOut.normal = (vsUniforms.worldInverseTranspose * vec4f(v.normal, 0.0)).xyz;
29
+ vsOut.texcoord = v.texcoord;
30
+ return vsOut;
31
+ }
32
+
33
+ struct FSUniforms {
34
+ lightDirection: vec3f,
35
+ };
36
+
37
+ @group(0) @binding(1) var<uniform> fsUniforms: FSUniforms;
38
+ @group(0) @binding(2) var diffuseSampler: sampler;
39
+ @group(0) @binding(3) var diffuseTexture: texture_2d<f32>;
40
+
41
+ @fragment
42
+ fn myFSMain(v: MyVSOutput) -> @location(0) vec4f {
43
+ var diffuseColor = textureSample(diffuseTexture, diffuseSampler, v.texcoord);
44
+ var a_normal = normalize(v.normal);
45
+ var l = dot(a_normal, fsUniforms.lightDirection) * 0.5 + 0.5;
46
+ return vec4f(diffuseColor.rgb * l, diffuseColor.a);
47
+ }
48
+ `;
49
+
50
+ /**
51
+ * @description
52
+ * For Cube with images
53
+ */
54
+ export const cubeTexShader = `struct Uniforms {
55
+ matrix: mat4x4f,
56
+ };
57
+
58
+ struct Vertex {
59
+ @location(0) position: vec4f,
60
+ @location(1) texcoord: vec2f,
61
+ };
62
+
63
+ struct VSOutput {
64
+ @builtin(position) position: vec4f,
65
+ @location(0) texcoord: vec2f,
66
+ };
67
+
68
+ @group(0) @binding(0) var<uniform> uni: Uniforms;
69
+ @group(0) @binding(1) var ourSampler: sampler;
70
+ @group(0) @binding(2) var ourTexture: texture_2d<f32>;
71
+
72
+ @vertex fn vs(vert: Vertex) -> VSOutput {
73
+ var vsOut: VSOutput;
74
+ vsOut.position = uni.matrix * vert.position;
75
+ vsOut.texcoord = vert.texcoord;
76
+ return vsOut;
77
+ }
78
+
79
+ @fragment fn fs(vsOut: VSOutput) -> @location(0) vec4f {
80
+ return textureSample(ourTexture, ourSampler, vsOut.texcoord);
81
+ }
82
+ `;
83
+
84
+ export const basicVertWGSL = `struct Uniforms {
85
+ modelViewProjectionMatrix : mat4x4<f32>,
86
+ }
87
+ @binding(0) @group(0) var<uniform> uniforms : Uniforms;
88
+
89
+ struct VertexOutput {
90
+ @builtin(position) Position : vec4<f32>,
91
+ @location(0) fragUV : vec2<f32>,
92
+ @location(1) fragPosition: vec4<f32>,
93
+ }
94
+
95
+ @vertex
96
+ fn main(
97
+ @location(0) position : vec4<f32>,
98
+ @location(1) uv : vec2<f32>
99
+ ) -> VertexOutput {
100
+ var output : VertexOutput;
101
+ output.Position = uniforms.modelViewProjectionMatrix * position;
102
+ output.fragUV = uv;
103
+ output.fragPosition = 0.5 * (position + vec4(1.0, 1.0, 1.0, 1.0));
104
+ return output;
105
+ }
106
+ `;
107
+
108
+ export const basicFragWGSL = `@group(0) @binding(1) var mySampler: sampler;
109
+ @group(0) @binding(2) var myTexture: texture_2d<f32>;
110
+
111
+ @fragment
112
+ fn main(
113
+ @location(0) fragUV: vec2<f32>,
114
+ @location(1) fragPosition: vec4<f32>
115
+ ) -> @location(0) vec4<f32> {
116
+ return textureSample(myTexture, mySampler, fragUV) * fragPosition;
117
+ }
118
+ `;
119
+ export const vertexPositionColorWGSL = `@fragment
120
+ fn main(
121
+ @location(0) fragUV: vec2<f32>,
122
+ @location(1) fragPosition: vec4<f32>
123
+ ) -> @location(0) vec4<f32> {
124
+ return fragPosition;
125
+ }`
126
+
127
+ export const BALL_SHADER = `struct Uniforms {
128
+ viewProjectionMatrix : mat4x4f
129
+ }
130
+ @group(0) @binding(0) var<uniform> uniforms : Uniforms;
131
+
132
+ @group(1) @binding(0) var<uniform> modelMatrix : mat4x4f;
133
+
134
+ struct VertexInput {
135
+ @location(0) position : vec4f,
136
+ @location(1) normal : vec3f,
137
+ @location(2) uv : vec2f
138
+ }
139
+
140
+ struct VertexOutput {
141
+ @builtin(position) position : vec4f,
142
+ @location(0) normal: vec3f,
143
+ @location(1) uv : vec2f,
144
+ }
145
+
146
+ @vertex
147
+ fn vertexMain(input: VertexInput) -> VertexOutput {
148
+ var output : VertexOutput;
149
+ output.position = uniforms.viewProjectionMatrix * modelMatrix * input.position;
150
+ output.normal = normalize((modelMatrix * vec4(input.normal, 0)).xyz);
151
+ output.uv = input.uv;
152
+ return output;
153
+ }
154
+
155
+ @group(1) @binding(1) var meshSampler: sampler;
156
+ @group(1) @binding(2) var meshTexture: texture_2d<f32>;
157
+
158
+ // Static directional lighting
159
+ const lightDir = vec3f(1, 1, 1);
160
+ const dirColor = vec3(1);
161
+ const ambientColor = vec3f(0.05);
162
+
163
+ @fragment
164
+ fn fragmentMain(input: VertexOutput) -> @location(0) vec4f {
165
+ let textureColor = textureSample(meshTexture, meshSampler, input.uv);
166
+
167
+ // Very simplified lighting algorithm.
168
+ let lightColor = saturate(ambientColor + max(dot(input.normal, lightDir), 0.0) * dirColor);
169
+
170
+ return vec4f(textureColor.rgb * lightColor, textureColor.a);
171
+ }`;
@@ -0,0 +1,56 @@
1
+ #version 300 es
2
+ precision mediump float;
3
+ in vec2 vTextureCoord;
4
+ in vec3 vLightWeighting;
5
+ uniform sampler2D uSampler;
6
+ uniform sampler2D uSampler1;
7
+ uniform sampler2D uSampler2;
8
+ uniform sampler2D uSampler3;
9
+ uniform sampler2D uSampler4;
10
+ uniform sampler2D uSampler5;
11
+ // The CubeMap texture.
12
+ uniform samplerCube u_texture;
13
+ // cube map
14
+ // in vec3 v_normal_cubemap;
15
+ uniform float numberOfsamplers;
16
+
17
+ // Spot
18
+ // Passed in from the vertex shader.
19
+ in vec3 v_normal;
20
+ in vec3 v_surfaceToLight;
21
+ in vec3 v_surfaceToView;
22
+ uniform vec4 u_color;
23
+ uniform float u_shininess;
24
+ uniform vec3 u_lightDirection;
25
+ uniform float u_innerLimit;
26
+ uniform float u_outerLimit;
27
+
28
+ out vec4 outColor;
29
+
30
+ void main(void) {
31
+ // because v_normal is a varying it's interpolated
32
+ // so it will not be a unit vector. Normalizing it
33
+ // will make it a unit vector again
34
+ vec3 normal = normalize(v_normal);
35
+
36
+ vec3 surfaceToLightDirection = normalize(v_surfaceToLight);
37
+ vec3 surfaceToViewDirection = normalize(v_surfaceToView);
38
+ vec3 halfVector = normalize(surfaceToLightDirection + surfaceToViewDirection);
39
+
40
+ float dotFromDirection = dot(surfaceToLightDirection, -u_lightDirection);
41
+ float limitRange = u_innerLimit - u_outerLimit;
42
+ float inLight = clamp((dotFromDirection - u_outerLimit) / limitRange, 0.0f, 1.0f);
43
+ float light = inLight * dot(normal, surfaceToLightDirection);
44
+ float specular = inLight * pow(dot(normal, halfVector), u_shininess);
45
+
46
+ // Directioin vs uAmbientColor
47
+ vec4 textureColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
48
+ vec4 textureColor1 = texture2D(uSampler1, vec2(vTextureCoord.s, vTextureCoord.t));
49
+ vec4 textureColor2 = texture2D(uSampler2, vec2(vTextureCoord.s, vTextureCoord.t));
50
+ vec4 testUnused = texture2D(u_texture, vec2(vTextureCoord.s, vTextureCoord.t));
51
+
52
+ outColor = vec4(textureColor.rgb * vLightWeighting, textureColor.a);
53
+ // Lets multiply just the color portion (not the alpha)
54
+ outColor.rgb *= light;
55
+ outColor.rgb += specular;
56
+ }