matrix-engine-wgpu 1.0.1 → 1.0.4

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.
Files changed (46) hide show
  1. package/LICENSE +22 -17
  2. package/REFERENCE.md +48 -2
  3. package/app-worker.js +45 -0
  4. package/empty.js +12 -0
  5. package/examples/load-obj-file.js +48 -0
  6. package/examples/unlit-textures.js +27 -0
  7. package/examples.js +7 -0
  8. package/index.js +18 -5
  9. package/main.js +29 -12
  10. package/package.json +11 -2
  11. package/public/app-worker.js +47 -0
  12. package/public/app.js +2833 -279
  13. package/public/css/style.css +1 -2
  14. package/public/empty.html +25 -0
  15. package/public/empty.js +9107 -0
  16. package/public/examples.html +25 -0
  17. package/public/examples.js +9180 -0
  18. package/public/res/meshes/blender/piramyd.blend +0 -0
  19. package/public/res/meshes/blender/piramyd.blend1 +0 -0
  20. package/public/res/meshes/blender/piramyd.js +42 -0
  21. package/public/res/meshes/blender/piramyd.mtl +10 -0
  22. package/public/res/meshes/blender/piramyd.obj +18696 -0
  23. package/public/res/meshes/blender/piramyd1.js +42 -0
  24. package/public/res/meshes/blender/welcomeTextblend.blend +0 -0
  25. package/public/res/meshes/dragon/stanfordDragonData.js +5 -0
  26. package/public/res/meshes/obj/armor.obj +319 -0
  27. package/public/res/meshes/obj/armor.png +0 -0
  28. package/public/worker.html +25 -0
  29. package/readme.md +77 -36
  30. package/src/engine/ball.js +43 -23
  31. package/src/engine/cube.js +112 -87
  32. package/src/engine/engine.js +470 -0
  33. package/src/engine/final/adaptJSON1.js +53 -0
  34. package/src/engine/final/utils2.js +63 -0
  35. package/src/engine/loader-obj.js +469 -0
  36. package/src/engine/matrix-class.js +44 -1
  37. package/src/engine/matrix-mesh.js +49 -0
  38. package/src/engine/mesh-obj.js +526 -0
  39. package/src/engine/mesh.js +477 -0
  40. package/src/engine/utils.js +2 -0
  41. package/src/shaders/fragment.wgsl.js +48 -0
  42. package/src/shaders/shaders.js +4 -124
  43. package/src/shaders/vertex.wgsl.js +49 -0
  44. package/src/shaders/vertexShadow.wgsl.js +20 -0
  45. package/src/world.js +263 -0
  46. package/src/meWGPU.js +0 -92
@@ -0,0 +1,469 @@
1
+ /**
2
+ * The main Mesh class. The constructor will parse through the OBJ file data
3
+ * and collect the vertex, vertex normal, texture, and face information. This
4
+ * information can then be used later on when creating your VBOs. See
5
+ * OBJ.initMeshBuffers for an example of how to use the newly created Mesh
6
+ *
7
+ * @class Mesh
8
+ * @constructor
9
+ *
10
+ * @param {String} objectData a string representation of an OBJ file with newlines preserved.
11
+ */
12
+
13
+ export class constructMesh {
14
+
15
+ constructor (objectData, inputArg) {
16
+ this.inputArg = inputArg;
17
+ this.objectData = objectData;
18
+ this.create(objectData, inputArg);
19
+ this.setScale = (s) => {
20
+ this.inputArg.scale = s;
21
+ this.create(this.objectData, this.inputArg)
22
+ };
23
+ this.updateBuffers = () => {
24
+ this.inputArg.scale = 1;
25
+ this.create(this.objectData, this.inputArg);
26
+ };
27
+ }
28
+
29
+ create = (objectData, inputArg, callback) => {
30
+
31
+ if (typeof callback === 'undefined') callback = function() {};
32
+
33
+ let initOrientation = [0,1,2];
34
+ /*
35
+ The OBJ file format does a sort of compression when saving a model in a
36
+ program like Blender. There are at least 3 sections (4 including textures)
37
+ within the file. Each line in a section begins with the same string:
38
+ * 'v': indicates vertex section
39
+ * 'vn': indicates vertex normal section
40
+ * 'f': indicates the faces section
41
+ * 'vt': indicates vertex texture section (if textures were used on the model)
42
+ Each of the above sections (except for the faces section) is a list/set of
43
+ unique vertices.
44
+ Each line of the faces section contains a list of
45
+ (vertex, [texture], normal) groups
46
+ Some examples:
47
+ // the texture index is optional, both formats are possible for models
48
+ // without a texture applied
49
+ f 1/25 18/46 12/31
50
+ f 1//25 18//46 12//31
51
+ // A 3 vertex face with texture indices
52
+ f 16/92/11 14/101/22 1/69/1
53
+ // A 4 vertex face
54
+ f 16/92/11 40/109/40 38/114/38 14/101/22
55
+ The first two lines are examples of a 3 vertex face without a texture applied.
56
+ The second is an example of a 3 vertex face with a texture applied.
57
+ The third is an example of a 4 vertex face. Note: a face can contain N
58
+ number of vertices.
59
+ Each number that appears in one of the groups is a 1-based index
60
+ corresponding to an item from the other sections (meaning that indexing
61
+ starts at one and *not* zero).
62
+ For example:
63
+ `f 16/92/11` is saying to
64
+ - take the 16th element from the [v] vertex array
65
+ - take the 92nd element from the [vt] texture array
66
+ - take the 11th element from the [vn] normal array
67
+ and together they make a unique vertex.
68
+ Using all 3+ unique Vertices from the face line will produce a polygon.
69
+ Now, you could just go through the OBJ file and create a new vertex for
70
+ each face line and WebGL will draw what appears to be the same model.
71
+ However, vertices will be overlapped and duplicated all over the place.
72
+ Consider a cube in 3D space centered about the origin and each side is
73
+ 2 units long. The front face (with the positive Z-axis pointing towards
74
+ you) would have a Top Right vertex (looking orthogonal to its normal)
75
+ mapped at (1,1,1) The right face would have a Top Left vertex (looking
76
+ orthogonal to its normal) at (1,1,1) and the top face would have a Bottom
77
+ Right vertex (looking orthogonal to its normal) at (1,1,1). Each face
78
+ has a vertex at the same coordinates, however, three distinct vertices
79
+ will be drawn at the same spot.
80
+ To solve the issue of duplicate Vertices (the `(vertex, [texture], normal)`
81
+ groups), while iterating through the face lines, when a group is encountered
82
+ the whole group string ('16/92/11') is checked to see if it exists in the
83
+ packed.hashindices object, and if it doesn't, the indices it specifies
84
+ are used to look up each attribute in the corresponding attribute arrays
85
+ already created. The values are then copied to the corresponding unpacked
86
+ array (flattened to play nice with WebGL's ELEMENT_ARRAY_BUFFER indexing),
87
+ the group string is added to the hashindices set and the current unpacked
88
+ index is used as this hashindices value so that the group of elements can
89
+ be reused. The unpacked index is incremented. If the group string already
90
+ exists in the hashindices object, its corresponding value is the index of
91
+ that group and is appended to the unpacked indices array.
92
+ */
93
+ var verts = [],
94
+ vertNormals = [],
95
+ textures = [],
96
+ unpacked = {};
97
+ // unpacking stuff
98
+ unpacked.verts = [];
99
+ unpacked.norms = [];
100
+ unpacked.textures = [];
101
+ unpacked.hashindices = {};
102
+ unpacked.indices = [];
103
+ unpacked.index = 0;
104
+ // array of lines separated by the newline
105
+ var lines = objectData.split('\n');
106
+
107
+ // update swap orientation
108
+ if (inputArg.swap[0] !== null) {
109
+ swap(inputArg.swap[0], inputArg.swap[1], initOrientation);
110
+ }
111
+
112
+ var VERTEX_RE = /^v\s/;
113
+ var NORMAL_RE = /^vn\s/;
114
+ var TEXTURE_RE = /^vt\s/;
115
+ var FACE_RE = /^f\s/;
116
+ var WHITESPACE_RE = /\s+/;
117
+
118
+ for (var i = 0; i < lines.length; i++) {
119
+ var line = lines[i].trim();
120
+ var elements = line.split(WHITESPACE_RE);
121
+ elements.shift();
122
+
123
+ if (VERTEX_RE.test(line)) {
124
+ // if this is a vertex
125
+ verts.push.apply(verts, elements);
126
+ } else if (NORMAL_RE.test(line)) {
127
+ // if this is a vertex normal
128
+ vertNormals.push.apply(vertNormals, elements);
129
+ } else if (TEXTURE_RE.test(line)) {
130
+ // if this is a texture
131
+ textures.push.apply(textures, elements);
132
+ } else if (FACE_RE.test(line)) {
133
+ // if this is a face
134
+ /*
135
+ split this face into an array of vertex groups
136
+ for example:
137
+ f 16/92/11 14/101/22 1/69/1
138
+ becomes:
139
+ ['16/92/11', '14/101/22', '1/69/1'];
140
+ */
141
+ var quad = false;
142
+ for (var j = 0, eleLen = elements.length; j < eleLen; j++) {
143
+ // Triangulating quads
144
+ // quad: 'f v0/t0/vn0 v1/t1/vn1 v2/t2/vn2 v3/t3/vn3/'
145
+ // corresponding triangles:
146
+ // 'f v0/t0/vn0 v1/t1/vn1 v2/t2/vn2'
147
+ // 'f v2/t2/vn2 v3/t3/vn3 v0/t0/vn0'
148
+ if (j === 3 && !quad) {
149
+ // add v2/t2/vn2 in again before continuing to 3
150
+ j = 2;
151
+ quad = true;
152
+ }
153
+ if (elements[j] in unpacked.hashindices) {
154
+ unpacked.indices.push(unpacked.hashindices[elements[j]]);
155
+ } else {
156
+ /*
157
+ Each element of the face line array is a vertex which has its
158
+ attributes delimited by a forward slash. This will separate
159
+ each attribute into another array:
160
+ '19/92/11'
161
+ becomes:
162
+ vertex = ['19', '92', '11'];
163
+ where
164
+ vertex[0] is the vertex index
165
+ vertex[1] is the texture index
166
+ vertex[2] is the normal index
167
+ Think of faces having Vertices which are comprised of the
168
+ attributes location (v), texture (vt), and normal (vn).
169
+ */
170
+ var vertex = elements[j].split('/');
171
+ /*
172
+ The verts, textures, and vertNormals arrays each contain a
173
+ flattend array of coordinates.
174
+ Because it gets confusing by referring to vertex and then
175
+ vertex (both are different in my descriptions) I will explain
176
+ what's going on using the vertexNormals array:
177
+ vertex[2] will contain the one-based index of the vertexNormals
178
+ section (vn). One is subtracted from this index number to play
179
+ nice with javascript's zero-based array indexing.
180
+ Because vertexNormal is a flattened array of x, y, z values,
181
+ simple pointer arithmetic is used to skip to the start of the
182
+ vertexNormal, then the offset is added to get the correct
183
+ component: +0 is x, +1 is y, +2 is z.
184
+ This same process is repeated for verts and textures.
185
+ */
186
+ // vertex position
187
+ unpacked.verts.push(+verts[(vertex[0] - 1) * 3 + initOrientation[0]] * inputArg.scale);
188
+ unpacked.verts.push(+verts[(vertex[0] - 1) * 3 + initOrientation[1]] * inputArg.scale);
189
+ unpacked.verts.push(+verts[(vertex[0] - 1) * 3 + initOrientation[2]] * inputArg.scale);
190
+
191
+ // vertex textures
192
+ if (textures.length) {
193
+ unpacked.textures.push(+textures[(vertex[1] - 1) * 2 + 0]);
194
+ unpacked.textures.push(+textures[(vertex[1] - 1) * 2 + 1]);
195
+ }
196
+ // vertex normals
197
+ unpacked.norms.push(+vertNormals[(vertex[2] - 1) * 3 + 0]);
198
+ unpacked.norms.push(+vertNormals[(vertex[2] - 1) * 3 + 1]);
199
+ unpacked.norms.push(+vertNormals[(vertex[2] - 1) * 3 + 2]);
200
+ // add the newly created vertex to the list of indices
201
+ unpacked.hashindices[elements[j]] = unpacked.index;
202
+ unpacked.indices.push(unpacked.index);
203
+ // increment the counter
204
+ unpacked.index += 1;
205
+ }
206
+ if (j === 3 && quad) {
207
+ // add v0/t0/vn0 onto the second triangle
208
+ unpacked.indices.push(unpacked.hashindices[elements[0]]);
209
+ }
210
+ }
211
+ }
212
+ }
213
+ this.vertices = unpacked.verts;
214
+ this.vertexNormals = unpacked.norms;
215
+ this.textures = unpacked.textures;
216
+ this.indices = unpacked.indices;
217
+ callback();
218
+ return this;
219
+ };
220
+
221
+ }
222
+
223
+ var Ajax = function () {
224
+ // this is just a helper class to ease ajax calls
225
+ var _this = this;
226
+ this.xmlhttp = new XMLHttpRequest();
227
+
228
+ this.get = function (url, callback) {
229
+ _this.xmlhttp.onreadystatechange = function () {
230
+ if (_this.xmlhttp.readyState === 4) {
231
+ callback(_this.xmlhttp.responseText, _this.xmlhttp.status);
232
+ }
233
+ };
234
+ _this.xmlhttp.open('GET', url, true);
235
+ _this.xmlhttp.send();
236
+ };
237
+ };
238
+
239
+ /**
240
+ * Takes in an object of `mesh_name`, `'/url/to/OBJ/file'` pairs and a callback
241
+ * function. Each OBJ file will be ajaxed in and automatically converted to
242
+ * an OBJ.Mesh. When all files have successfully downloaded the callback
243
+ * function provided will be called and passed in an object containing
244
+ * the newly created meshes.
245
+ *
246
+ * **Note:** In order to use this function as a way to download meshes, a
247
+ * webserver of some sort must be used.
248
+ *
249
+ * @param {Object} nameAndURLs an object where the key is the name of the mesh and the value is the url to that mesh's OBJ file
250
+ *
251
+ * @param {Function} completionCallback should contain a function that will take one parameter: an object array where the keys will be the unique object name and the value will be a Mesh object
252
+ *
253
+ * @param {Object} meshes In case other meshes are loaded separately or if a previously declared variable is desired to be used, pass in a (possibly empty) json object of the pattern: { '<mesh_name>': OBJ.Mesh }
254
+ *
255
+ */
256
+ export var downloadMeshes = function (nameAndURLs, completionCallback, inputArg) {
257
+ // the total number of meshes. this is used to implement "blocking"
258
+ var semaphore = Object.keys(nameAndURLs).length;
259
+ // if error is true, an alert will given
260
+ var error = false;
261
+ // this is used to check if all meshes have been downloaded
262
+ // if meshes is supplied, then it will be populated, otherwise
263
+ // a new object is created. this will be passed into the completionCallback
264
+ if (typeof inputArg === 'undefined') {
265
+ var inputArg = {
266
+ scale: 1,
267
+ swap: [null]
268
+ };
269
+ }
270
+ if (typeof inputArg.scale === 'undefined') inputArg.scale = 0.1;
271
+ if (typeof inputArg.swap === 'undefined') inputArg.swap = [null];
272
+
273
+ var meshes = {};
274
+
275
+ // loop over the mesh_name,url key,value pairs
276
+ for (var mesh_name in nameAndURLs) {
277
+ if (nameAndURLs.hasOwnProperty(mesh_name)) {
278
+ new Ajax().get(
279
+ nameAndURLs[mesh_name],
280
+ (function (name) {
281
+ return function (data, status) {
282
+ if (status === 200) {
283
+ meshes[name] = new constructMesh(data, inputArg);
284
+ } else {
285
+ error = true;
286
+ console.error('An error has occurred and the mesh "' + name + '" could not be downloaded.');
287
+ }
288
+ // the request has finished, decrement the counter
289
+ semaphore--;
290
+ if (semaphore === 0) {
291
+ if (error) {
292
+ // if an error has occurred, the user is notified here and the
293
+ // callback is not called
294
+ console.error('An error has occurred and one or meshes has not been ' + 'downloaded. The execution of the script has terminated.');
295
+ throw '';
296
+ }
297
+ // there haven't been any errors in retrieving the meshes
298
+ // call the callback
299
+ completionCallback(meshes);
300
+ }
301
+ };
302
+ })(mesh_name)
303
+ );
304
+ }
305
+ }
306
+ };
307
+
308
+ /**
309
+ * Takes in the WebGL context and a Mesh, then creates and appends the buffers
310
+ * to the mesh object as attributes.
311
+ *
312
+ * @param {WebGLRenderingContext} gl the `canvas.getContext('webgl')` context instance
313
+ * @param {Mesh} mesh a single `OBJ.Mesh` instance
314
+ *
315
+ * The newly created mesh attributes are:
316
+ *
317
+ * Attrbute | Description
318
+ * :--- | ---
319
+ * **normalBuffer** |contains the model&#39;s Vertex Normals
320
+ * normalBuffer.itemSize |set to 3 items
321
+ * normalBuffer.numItems |the total number of vertex normals
322
+ * |
323
+ * **textureBuffer** |contains the model&#39;s Texture Coordinates
324
+ * textureBuffer.itemSize |set to 2 items
325
+ * textureBuffer.numItems |the number of texture coordinates
326
+ * |
327
+ * **vertexBuffer** |contains the model&#39;s Vertex Position Coordinates (does not include w)
328
+ * vertexBuffer.itemSize |set to 3 items
329
+ * vertexBuffer.numItems |the total number of vertices
330
+ * |
331
+ * **indexBuffer** |contains the indices of the faces
332
+ * indexBuffer.itemSize |is set to 1
333
+ * indexBuffer.numItems |the total number of indices
334
+ *
335
+ * A simple example (a lot of steps are missing, so don't copy and paste):
336
+ *
337
+ * var gl = canvas.getContext('webgl'),
338
+ * mesh = OBJ.Mesh(obj_file_data);
339
+ * // compile the shaders and create a shader program
340
+ * var shaderProgram = gl.createProgram();
341
+ * // compilation stuff here
342
+ * ...
343
+ * // make sure you have vertex, vertex normal, and texture coordinate
344
+ * // attributes located in your shaders and attach them to the shader program
345
+ * shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
346
+ * gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
347
+ *
348
+ * shaderProgram.vertexNormalAttribute = gl.getAttribLocation(shaderProgram, "aVertexNormal");
349
+ * gl.enableVertexAttribArray(shaderProgram.vertexNormalAttribute);
350
+ *
351
+ * shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
352
+ * gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
353
+ *
354
+ * // create and initialize the vertex, vertex normal, and texture coordinate buffers
355
+ * // and save on to the mesh object
356
+ * OBJ.initMeshBuffers(gl, mesh);
357
+ *
358
+ * // now to render the mesh
359
+ * gl.bindBuffer(gl.ARRAY_BUFFER, mesh.vertexBuffer);
360
+ * gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, mesh.vertexBuffer.itemSize, gl.FLOAT, false, 0, 0);
361
+ * // it's possible that the mesh doesn't contain
362
+ * // any texture coordinates (e.g. suzanne.obj in the development branch).
363
+ * // in this case, the texture vertexAttribArray will need to be disabled
364
+ * // before the call to drawElements
365
+ * if(!mesh.textures.length){
366
+ * gl.disableVertexAttribArray(shaderProgram.textureCoordAttribute);
367
+ * }
368
+ * else{
369
+ * // if the texture vertexAttribArray has been previously
370
+ * // disabled, then it needs to be re-enabled
371
+ * gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
372
+ * gl.bindBuffer(gl.ARRAY_BUFFER, mesh.textureBuffer);
373
+ * gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, mesh.textureBuffer.itemSize, gl.FLOAT, false, 0, 0);
374
+ * }
375
+ *
376
+ * gl.bindBuffer(gl.ARRAY_BUFFER, mesh.normalBuffer);
377
+ * gl.vertexAttribPointer(shaderProgram.vertexNormalAttribute, mesh.normalBuffer.itemSize, gl.FLOAT, false, 0, 0);
378
+ *
379
+ * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, model.mesh.indexBuffer);
380
+ * gl.drawElements(gl.TRIANGLES, model.mesh.indexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
381
+ */
382
+ export var initMeshBuffers = function (gl, mesh) {
383
+ // mesh.vertexNormals
384
+ // mesh.textures
385
+ // mesh.vertices
386
+ // mesh.indices
387
+ };
388
+
389
+ /**
390
+ * @description
391
+ * Construct sequence list argument for downloadMeshes.
392
+ * This is adaptation for blender obj animation export.
393
+ * For example:
394
+ * matrixEngine.objLoader.downloadMeshes(
395
+ matrixEngine.objLoader.makeObjSeqArg(
396
+ {
397
+ id: objName,
398
+ joinMultiPahts: [
399
+ {
400
+ path: "res/bvh-skeletal-base/swat-guy/seq-walk/low/swat",
401
+ id: objName,
402
+ from: 1, to: 34
403
+ },
404
+ {
405
+ path: "res/bvh-skeletal-base/swat-guy/seq-walk-pistol/low/swat-walk-pistol",
406
+ id: objName,
407
+ from: 35, to: 54
408
+ }
409
+ ]
410
+ }),
411
+ onLoadObj
412
+ );
413
+ */
414
+ export const makeObjSeqArg = (arg) => {
415
+ // Adaptation for blender (animation) obj exporter.
416
+ var local = {};
417
+
418
+ function localCalc(arg, noInitial = false) {
419
+ var zeros = '00000';
420
+ var l = {};
421
+ var helper = arg.from;
422
+ for (let j = arg.from, z=1; j <= arg.to;j++ ) {
423
+
424
+ if (z > 9 && z < 99) {
425
+ zeros = '0000';
426
+ } else if (z > 99 && z < 999) {
427
+ zeros = '000';
428
+ } // no need more then 999
429
+
430
+ if (helper == arg.from && noInitial === false) {
431
+ l[arg.id] = arg.path + '_' + zeros + z + '.obj';
432
+ } else {
433
+ l[arg.id + (helper-1)] = arg.path + '_' + zeros + z + '.obj';
434
+ }
435
+ helper++;
436
+ z++;
437
+ }
438
+ return l;
439
+ }
440
+
441
+ if (typeof arg.path === 'string' ) {
442
+ local = localCalc(arg);
443
+ } else if (typeof arg.path === 'undefined') {
444
+ if (typeof arg.joinMultiPahts !== 'undefined') {
445
+ console.log("ITS joinMultiPahts!");
446
+ var localFinal = {};
447
+ arg.joinMultiPahts.forEach((arg, index)=> {
448
+ if (index === 0) {
449
+ localFinal = Object.assign(local, localCalc(arg));
450
+ } else {
451
+ localFinal = Object.assign(local, localCalc(arg, true));
452
+ }
453
+ });
454
+ console.log("joinMultiPahts LOCAL => ", localFinal);
455
+ return localFinal;
456
+ }
457
+ }
458
+
459
+ return local;
460
+ }
461
+
462
+ /**
463
+ * @description
464
+ * Switching obj seq animations frames range.
465
+ */
466
+ export function play (nameAni) {
467
+ this.animation.anims.active = nameAni;
468
+ this.animation.currentAni = this.animation.anims[this.animation.anims.active].from;
469
+ }
@@ -1,5 +1,6 @@
1
- // Sub classes for matrix-wgpu
1
+ import {degToRad} from "./utils";
2
2
 
3
+ // Sub classes for matrix-wgpu
3
4
  /**
4
5
  * @description Base class
5
6
  * Position { x, y, z }
@@ -169,3 +170,45 @@ export class Position {
169
170
  }
170
171
  }
171
172
 
173
+ export class Rotation {
174
+
175
+ constructor(x, y, z) {
176
+ // Not in use for nwo this is from matrix-engine project [nameUniq]
177
+ this.nameUniq = null;
178
+ if(typeof x == 'undefined') x = 0;
179
+ if(typeof y == 'undefined') y = 0;
180
+ if(typeof z == 'undefined') z = 0;
181
+ this.x = x;
182
+ this.y = y;
183
+ this.z = z;
184
+ this.rotationSpeed = {x: 0, y: 0, z: 0}
185
+ }
186
+
187
+ getRotX () {
188
+ if (this.rotationSpeed.x == 0) {
189
+ return degToRad(this.x);
190
+ } else {
191
+ this.x = this.x + this.rotationSpeed.x* 0.001;
192
+ return this.x;
193
+ }
194
+ }
195
+
196
+ getRotY () {
197
+ if (this.rotationSpeed.y == 0) {
198
+ return degToRad(this.y);
199
+ } else {
200
+ this.y = this.y + this.rotationSpeed.y * 0.001;
201
+ return this.y;
202
+ }
203
+ }
204
+
205
+ getRotZ () {
206
+ if (this.rotationSpeed.z == 0) {
207
+ return degToRad(this.z);
208
+ } else {
209
+ this.z = this.z + this.rotationSpeed.z* 0.001;
210
+ return this.z;
211
+ }
212
+ }
213
+
214
+ }
@@ -0,0 +1,49 @@
1
+ import {computeSurfaceNormals, computeProjectedPlaneUVs} from './utils2.js';
2
+
3
+ export function makeMeshData1(m) {
4
+
5
+ let mesh = {
6
+ positions: m.positions,
7
+ triangles: m.cells,
8
+ normals: [],
9
+ uvs: [],
10
+ };
11
+
12
+ // Compute surface normals
13
+ mesh.normals = computeSurfaceNormals(mesh.positions, mesh.triangles);
14
+
15
+ // Compute some easy uvs for testing
16
+ mesh.uvs = computeProjectedPlaneUVs(mesh.positions, 'xy');
17
+
18
+ // // Push indices for an additional ground plane
19
+ mesh.triangles.push(
20
+ [mesh.positions.length, mesh.positions.length + 2, mesh.positions.length + 1],
21
+ // [mesh.positions.length, mesh.positions.length + 1, mesh.positions.length + 3]
22
+ );
23
+
24
+ // // Push vertex attributes for an additional ground plane
25
+ // // prettier-ignore
26
+ // mesh.positions.push(
27
+ // [-100, 20, -100], //
28
+ // [100, 20, 100], //
29
+ // [-100, 20, 100], //
30
+ // [100, 20, -100]
31
+ // );
32
+ // mesh.normals.push(
33
+ // [0, 1, 0], //
34
+ // [0, 1, 0], //
35
+ // [0, 1, 0], //
36
+ // [0, 1, 0]
37
+ // );
38
+ // mesh.uvs.push(
39
+ // [0, 0], //
40
+ // [1, 1], //
41
+ // [0, 1], //
42
+ // [1, 0]
43
+ // );
44
+
45
+ // mesh.positions = new Float32Array(m.positions)
46
+ // mesh.triangles = new Float32Array(m.cells)
47
+
48
+ return mesh;
49
+ }