zincjs 1.0.16 → 1.1.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.
@@ -1,3 +1,7 @@
1
+
2
+ /**
3
+ * Provide additional shaders to render time dependent color.
4
+ */
1
5
  exports.augmentMorphColor = function() {
2
6
  return function(shader) {
3
7
  shader.vertexShader = shader.vertexShader.replace(
@@ -7,7 +7,7 @@ const THREE = require('three');
7
7
  *
8
8
  * @class
9
9
  * @author Alan Wu
10
- * @return {Zinc.Geometry}
10
+ * @return {Geometry}
11
11
  */
12
12
  const Geometry = function () {
13
13
  (require('./zincObject').ZincObject).call(this);
@@ -15,6 +15,19 @@ const Geometry = function () {
15
15
  this.videoHandler = undefined;
16
16
  this.isGeometry = true;
17
17
 
18
+ /**
19
+ * Create the mesh for rendering
20
+ *
21
+ * @param {THREE.Geomtry} geometryIn - Geometry to be rendered.
22
+ * @param {THREE.Material} materialIn - Material to be set for the geometry.
23
+ * @param {Object} options - Provide various options
24
+ * @param {THREE.Color} options.colour - colour to be set for the geometry
25
+ * @param {Boolean} options.localTimeEnabled - A flag to indicate either the geometry is
26
+ * time dependent.
27
+ * @param {Boolean} options.localMorphColour - A flag to indicate either the colour is
28
+ * time dependent.
29
+ * @param {Number} options.opacity - Opacity to be set for the geometry
30
+ */
18
31
  this.createMesh = (geometryIn, materialIn, options) => {
19
32
  if (this.geometry && this.morph && (geometryIn != undefined))
20
33
  return;
@@ -73,7 +86,10 @@ const Geometry = function () {
73
86
  let mesh = new THREE.Mesh(geometry, material);
74
87
  this.setMesh(mesh, options.localTimeEnabled, options.localMorphColour);
75
88
  }
76
-
89
+
90
+ /**
91
+ * Calculate the UV for texture rendering.
92
+ */
77
93
  this.calculateUVs = () => {
78
94
  this.geometry.computeBoundingBox();
79
95
  const max = this.geometry.boundingBox.max, min = this.geometry.boundingBox.min;
@@ -4,107 +4,127 @@ const THREE = require('three');
4
4
  * Zinc representation of glyph graphic, it contains the colours,
5
5
  * geometry and transformation of the glyph.
6
6
  *
7
- * @param {THREE.Geometry} geometry - Geometry of the glyph.
8
- * @param {THREE.material} materialIn - Material of the glyph.
7
+ * @param {THREE.Geometry} geometry - Geometry of the glyph .
8
+ * @param {THREE.Material} materialIn - Material of the glyph.
9
9
  * @param {Number} idIn - Id of the glyph.
10
10
  *
11
11
  * @class
12
12
  * @author Alan Wu
13
- * @return {Zinc.Glyph}
13
+ * @return {Glyph}
14
14
  */
15
- const Glyph = function(geometry, materialIn, idIn, glyphsetIn) {
15
+ const Glyph = function (geometry, materialIn, idIn, glyphsetIn) {
16
16
  (require('./zincObject').ZincObject).call(this);
17
- let material = undefined;
18
- if (materialIn) {
19
- material = materialIn.clone();
20
- material.vertexColors = THREE.FaceColors;
21
- }
22
- const parent = glyphsetIn;
23
- this.id = idIn;
24
- let label = undefined;
25
- let labelString = undefined;
26
- const group = new THREE.Group();
27
- this.isGlyph = true;
28
-
29
- this.fromMesh = meshIn => {
30
- if (meshIn && meshIn.isMesh) {
31
- this.morph = meshIn.clone();
32
- this.morph.userData = this;
33
- group.add(this.morph);
34
- return true;
35
- }
36
- return false;
37
- }
38
-
39
- if (geometry && material) {
40
- this.fromMesh(new THREE.Mesh( geometry, material ));
41
- }
42
-
43
- this.getGlyphset = function() {
44
- return parent;
45
- }
46
-
47
- this.setLabel = text => {
48
- if (text && (typeof text === 'string' || text instanceof String)) {
49
- labelString = text;
50
- if (this.morph)
51
- this.morph.name = text;
52
- }
53
- if (label)
54
- this.showLabel();
55
- }
56
-
57
- this.showLabel = (colour) => {
58
- if (label) {
59
- position = label.getPosition();
60
- group.remove(label.getSprite());
61
- label.dispose();
62
- label = undefined;
17
+ let material = undefined;
18
+ if (materialIn) {
19
+ material = materialIn.clone();
20
+ material.vertexColors = THREE.FaceColors;
21
+ }
22
+ const parent = glyphsetIn;
23
+ this.id = idIn;
24
+ let label = undefined;
25
+ let labelString = undefined;
26
+ const group = new THREE.Group();
27
+ this.isGlyph = true;
28
+
29
+ /**
30
+ * Create a glyph using mesh
31
+ * @param {THREE.Mesh} meshIn - Mesh to create the glyph from
32
+ *
33
+ * @returns {Boolean} true if successful
34
+ */
35
+ this.fromMesh = meshIn => {
36
+ if (meshIn && meshIn.isMesh) {
37
+ this.morph = meshIn.clone();
38
+ this.morph.userData = this;
39
+ group.add(this.morph);
40
+ return true;
41
+ }
42
+ return false;
43
+ }
44
+
45
+ if (geometry && material) {
46
+ this.fromMesh(new THREE.Mesh(geometry, material));
47
+ }
48
+
49
+ /**
50
+ * Get the {Glyphset} containing this glyph.
51
+ *
52
+ * @returns {Boolean} true if successful
53
+ */
54
+ this.getGlyphset = function () {
55
+ return parent;
56
+ }
57
+
58
+ /**
59
+ * Set and update the text containing this glyph.
60
+ * @param {String} text - Label to be set for this instance
61
+ */
62
+ this.setLabel = text => {
63
+ if (text && (typeof text === 'string' || text instanceof String)) {
64
+ labelString = text;
65
+ if (this.morph)
66
+ this.morph.name = text;
67
+ }
68
+ if (label)
69
+ this.showLabel();
70
+ }
71
+
72
+ /**
73
+ * Display label with the choosen colour. It will replace the current
74
+ * label.
75
+ * @param {THREE.Color} colour - Colour for the label.
76
+ */
77
+ this.showLabel = (colour) => {
78
+ if (label) {
79
+ position = label.getPosition();
80
+ group.remove(label.getSprite());
81
+ label.dispose();
82
+ label = undefined;
83
+ }
84
+ if (labelString && (typeof labelString === 'string' || labelString instanceof String)) {
85
+ let position = [0, 0, 0];
86
+ label = new (require('./label').Label)(labelString, colour);
87
+ label.setPosition(position[0], position[1], position[2]);
88
+ group.add(label.getSprite());
63
89
  }
64
- if (labelString && (typeof labelString === 'string' || labelString instanceof String)) {
65
- let position = [0, 0, 0];
66
- label = new (require('./label').Label)(labelString, colour);
67
- label.setPosition(position[0], position[1], position[2]);
68
- group.add(label.getSprite());
69
- }
70
- }
71
-
72
- /**
73
- * Get the group containing the label and mesh.
74
- * @return {THREE.Group}
75
- */
76
- this.getGroup = () => {
77
- return group;
78
- }
79
-
80
- /**
81
- * Get the label of this glyph
82
- * @return {Label}
83
- */
84
- this.getLabel = () => {
85
- return labelString;
86
- }
87
-
88
- /**
89
- * Get the mesh of this glyph.
90
- * @return {THREE.Mesh}
91
- */
92
- this.getMesh = () => {
93
- return this.morph;
94
90
  }
95
91
 
96
- /**
97
- * Set the transformation of this glyph.
98
- * @param {Array} position - Three components vectors containing position of the
99
- * transformation.
100
- * @param {Array} axis1 - Three components vectors containing axis1 rotation of the
101
- * transformation.
102
- * @param {Array} axis2 - Three components vectors containing axis2 rotation of the
103
- * transformation.
104
- * @param {Array} position - Three components vectors containing axis3 rotation of the
105
- * transformation.
106
- */
107
- this.setTransformation = (position, axis1, axis2, axis3) => {
92
+ /**
93
+ * Get the group containing the label and mesh.
94
+ * @return {THREE.Group}
95
+ */
96
+ this.getGroup = () => {
97
+ return group;
98
+ }
99
+
100
+ /**
101
+ * Get the label of this glyph
102
+ * @return {Label}
103
+ */
104
+ this.getLabel = () => {
105
+ return labelString;
106
+ }
107
+
108
+ /**
109
+ * Get the mesh of this glyph.
110
+ * @return {THREE.Mesh}
111
+ */
112
+ this.getMesh = () => {
113
+ return this.morph;
114
+ }
115
+
116
+ /**
117
+ * Set the transformation of this glyph.
118
+ * @param {Array} position - Three components vectors containing position of the
119
+ * transformation.
120
+ * @param {Array} axis1 - Three components vectors containing axis1 rotation of the
121
+ * transformation.
122
+ * @param {Array} axis2 - Three components vectors containing axis2 rotation of the
123
+ * transformation.
124
+ * @param {Array} position - Three components vectors containing axis3 rotation of the
125
+ * transformation.
126
+ */
127
+ this.setTransformation = (position, axis1, axis2, axis3) => {
108
128
  if (this.morph) {
109
129
  this.morph.matrix.elements[0] = axis1[0];
110
130
  this.morph.matrix.elements[1] = axis1[1];
@@ -124,10 +144,15 @@ const Glyph = function(geometry, materialIn, idIn, glyphsetIn) {
124
144
  this.morph.matrix.elements[15] = 1.0;
125
145
  this.morph.matrixAutoUpdate = false;
126
146
  }
127
- if (label)
128
- label.setPosition(position[0], position[1], position[2]);
129
- }
130
-
147
+ if (label)
148
+ label.setPosition(position[0], position[1], position[2]);
149
+ }
150
+
151
+ /**
152
+ * Set the color of the glyph and its label.
153
+ *
154
+ * @param {THREE.Color} color - Colour to be set.
155
+ */
131
156
  this.setColour = (color) => {
132
157
  if (label)
133
158
  label.setColour(color);
@@ -136,14 +161,14 @@ const Glyph = function(geometry, materialIn, idIn, glyphsetIn) {
136
161
  this.geometry.colorsNeedUpdate = true;
137
162
  }
138
163
 
139
- /**
140
- * Clear and free its memory.
141
- */
142
- this.dispose = () => {
143
- if (this.material)
144
- this.material.dispose();
145
- this.morph = undefined;
146
- }
164
+ /**
165
+ * Clear and free its memory.
166
+ */
167
+ this.dispose = () => {
168
+ if (this.material)
169
+ this.material.dispose();
170
+ this.morph = undefined;
171
+ }
147
172
  }
148
173
 
149
174
  Glyph.prototype = Object.create((require('./zincObject').ZincObject).prototype);
@@ -2,14 +2,14 @@ const THREE = require('three');
2
2
  const JSONLoader = require('../loaders/JSONLoader').JSONLoader;
3
3
 
4
4
  /**
5
- * This is a container of {@link Zinc.Glyph} and their graphical properties
5
+ * This is a container of {@link Glyph} and their graphical properties
6
6
  * including transformations, colors, number of time steps, duration of animations
7
7
  * and group name. Please note that all glyphs in the glyphset share the same geometry
8
8
  * however they may have different transformations.
9
9
  *
10
10
  * @class
11
11
  * @author Alan Wu
12
- * @return {Zinc.Glyphset}
12
+ * @return {Glyphset}
13
13
  */
14
14
  const Glyphset = function () {
15
15
  (require('./zincObject').ZincObject).call(this);
@@ -49,25 +49,25 @@ const Glyphset = function () {
49
49
  _points[i] = new THREE.Vector3();
50
50
  }
51
51
 
52
- /**
53
- * Get the {@link Three.Group} containing all of the glyphs' meshes.
54
- * @returns {Three.Group}
55
- */
52
+ /**
53
+ * Get the {@link Three.Group} containing all of the glyphs' meshes.
54
+ * @returns {Three.Group}
55
+ */
56
56
  this.getGroup = () => {
57
57
  return this.morph;
58
58
  }
59
59
 
60
- /**
61
- * Copy glyphset data into this glyphset then load the glyph's geoemtry
62
- * with the provided glyphURL. FinishCallback will be called once
63
- * glyph is loaded.
64
- *
65
- * @param {Array} glyphsetData - contains the informations about the glyphs.
66
- * @param {String} glyphURL - URL to the geometry which will be applied to all
67
- * all the glyphs in the glyphset once loaded.
68
- * @param {Function} finishCallback - User's function to be called once glyph's
69
- * geometry is loaded.
70
- */
60
+ /**
61
+ * Copy glyphset data into this glyphset then load the glyph's geoemtry
62
+ * with the provided glyphURL. FinishCallback will be called once
63
+ * glyph is loaded.
64
+ *
65
+ * @param {Array} glyphsetData - contains the informations about the glyphs.
66
+ * @param {String} glyphURL - URL to the geometry which will be applied to all
67
+ * all the glyphs in the glyphset once loaded.
68
+ * @param {Function} finishCallback - User's function to be called once glyph's
69
+ * geometry is loaded.
70
+ */
71
71
  this.load = (glyphsetData, glyphURL, finishCallback, isInline, displayLabels) => {
72
72
  axis1s = glyphsetData.axis1;
73
73
  axis2s = glyphsetData.axis2;
@@ -101,11 +101,12 @@ const Glyphset = function () {
101
101
  }
102
102
  }
103
103
 
104
- /**
105
- * Calculate the actual transformation value that can be applied
106
- * to the transformation matrix.
107
- * @returns {Array}
108
- */
104
+ /**
105
+ * Calculate the actual transformation value that can be applied
106
+ * to the transformation matrix.
107
+ *
108
+ * @returns {Array}
109
+ */
109
110
  const resolve_glyph_axes = (point, axis1, axis2, axis3, scale, return_arrays) => {
110
111
  if (repeat_mode == "NONE" || repeat_mode == "MIRROR") {
111
112
  let axis_scale = [0.0, 0.0, 0.0];
@@ -166,7 +167,7 @@ const Glyphset = function () {
166
167
  mirrored_axis3[1] = -mirrored_axis3[1];
167
168
  mirrored_axis3[2] = -mirrored_axis3[2];
168
169
  }
169
- return_arrays[1] =[mirrored_point, mirrored_axis1, mirrored_axis2, mirrored_axis3];
170
+ return_arrays[1] = [mirrored_point, mirrored_axis1, mirrored_axis2, mirrored_axis3];
170
171
  }
171
172
  }
172
173
  else if (repeat_mode == "AXES_2D" || repeat_mode == "AXES_3D") {
@@ -236,9 +237,9 @@ const Glyphset = function () {
236
237
  return return_arrays;
237
238
  };
238
239
 
239
- /**
240
- * Update transformation for each of the glyph in this glyphset.
241
- */
240
+ /**
241
+ * Update transformation for each of the glyph in this glyphset.
242
+ */
242
243
  const updateGlyphsetTransformation = (
243
244
  current_positions,
244
245
  current_axis1s,
@@ -298,9 +299,9 @@ const Glyphset = function () {
298
299
  this.morph.instanceMatrix.needsUpdate = true;
299
300
  };
300
301
 
301
- /**
302
- * Update colour for each of the glyph in this glyphset.
303
- */
302
+ /**
303
+ * Update colour for each of the glyph in this glyphset.
304
+ */
304
305
  const updateGlyphsetHexColors = current_colors => {
305
306
  let numberOfGlyphs = 1;
306
307
  if (repeat_mode == "AXES_2D" || repeat_mode == "MIRROR")
@@ -323,11 +324,11 @@ const Glyphset = function () {
323
324
  this.morph.instanceColor.needsUpdate = true;
324
325
  };
325
326
 
326
- /**
327
- * Update the current states of the glyphs in this glyphset, this includes transformation and
328
- * colour for each of them. This is called when glyphset and glyphs are initialised and whenever
329
- * the internal time has been updated.
330
- */
327
+ /**
328
+ * Update the current states of the glyphs in this glyphset, this includes transformation and
329
+ * colour for each of them. This is called when glyphset and glyphs are initialised and whenever
330
+ * the internal time has been updated.
331
+ */
331
332
  const updateMorphGlyphsets = () => {
332
333
  const current_positions = _current_positions;
333
334
  const current_axis1s = _current_axis1s;
@@ -387,11 +388,11 @@ const Glyphset = function () {
387
388
  _bot_colour.b * proportion + _top_colour.b * (1 - proportion));
388
389
  current_colors[i] = _bot_colour.getHex();
389
390
  }
390
- /*
391
- for (var i = 0; i < bottom_colors.length; i++) {
392
- current_colors.push(proportion * bottom_colors[i] + (1.0 - proportion) * top_colors[i]);
393
- }
394
- */
391
+ /*
392
+ for (var i = 0; i < bottom_colors.length; i++) {
393
+ current_colors.push(proportion * bottom_colors[i] + (1.0 - proportion) * top_colors[i]);
394
+ }
395
+ */
395
396
  } else {
396
397
  current_colors = colors["0"];
397
398
  }
@@ -399,12 +400,20 @@ const Glyphset = function () {
399
400
  }
400
401
  };
401
402
 
403
+ /**
404
+ * Display the label of the glyphs in the glyphset.
405
+ */
402
406
  this.showLabel = () => {
403
407
  for (let i = 0; i < glyphList.length; i++) {
404
408
  glyphList[i].showLabel(this.morph.material ? this.morph.material.color : undefined);
405
409
  }
406
410
  }
407
411
 
412
+ /**
413
+ * Create the glyphs in the glyphset.
414
+ *
415
+ * @param {Boolean} displayLabels -Flag to determine either the labels should be display or not.
416
+ */
408
417
  const createGlyphs = (displayLabels) => {
409
418
  if ((labels != undefined) && displayLabels) {
410
419
  for (let i = 0; i < numberOfVertices; i++) {
@@ -433,6 +442,11 @@ const Glyphset = function () {
433
442
  this.boundingBoxUpdateRequired = true;
434
443
  };
435
444
 
445
+ /**
446
+ * Add a custom {@link Glyph} to this {@link Glyphset}.
447
+ *
448
+ * @param {Glyph} Glyph to be added.
449
+ */
436
450
  this.addCustomGlyph = glyph => {
437
451
  if (glyph.isGlyph)
438
452
  glyphList.push(glyph);
@@ -440,6 +454,12 @@ const Glyphset = function () {
440
454
  this.boundingBoxUpdateRequired = true;
441
455
  }
442
456
 
457
+ /**
458
+ * Add a THREE.Mesh object to be displayed as glyph in this {@link Glyphset}.
459
+ *
460
+ * @param {THREE.Mesh} Mesh to be added.
461
+ * @param {Number} id of the mesh.
462
+ */
443
463
  this.addMeshAsGlyph = (mesh, id) => {
444
464
  if (mesh.isMesh) {
445
465
  const glyph = new (require('./glyph').Glyph)(undefined, undefined, id, this);
@@ -453,12 +473,13 @@ const Glyphset = function () {
453
473
  return undefined;
454
474
  }
455
475
 
456
- /**
457
- * A function which iterates through the list of glyphs and call the callback
458
- * function with the glyph as the argument.
459
- * @param {Function} callbackFunction - Callback function with the glyph
460
- * as an argument.
461
- */
476
+ /**
477
+ * A function which iterates through the list of glyphs and call the callback
478
+ * function with the glyph as the argument.
479
+ *
480
+ * @param {Function} callbackFunction - Callback function with the glyph
481
+ * as an argument.
482
+ */
462
483
  this.forEachGlyph = callbackFunction => {
463
484
  for (let i = 0; i < glyphList.length; i++) {
464
485
  callbackFunction(glyphList[i]);
@@ -539,24 +560,11 @@ const Glyphset = function () {
539
560
  return undefined;
540
561
  }
541
562
 
542
- this.applyMatrix4ToBox = (box1, box2, matrix) => {
543
- _points[0].set(box1.min.x, box1.min.y, box1.min.z).applyMatrix4(matrix); // 000
544
- _points[1].set(box1.min.x, box1.min.y, box1.max.z).applyMatrix4(matrix); // 001
545
- _points[2].set(box1.min.x, box1.max.y, box1.min.z).applyMatrix4(matrix); // 010
546
- _points[3].set(box1.min.x, box1.max.y, box1.max.z).applyMatrix4(matrix); // 011
547
- _points[4].set(box1.max.x, box1.min.y, box1.min.z).applyMatrix4(matrix); // 100
548
- _points[5].set(box1.max.x, box1.min.y, box1.max.z).applyMatrix4(matrix); // 101
549
- _points[6].set(box1.max.x, box1.max.y, box1.min.z).applyMatrix4(matrix); // 110
550
- _points[7].set(box1.max.x, box1.max.y, box1.max.z).applyMatrix4(matrix); // 111
551
- box2.setFromPoints(_points);
552
- }
553
-
554
-
555
- /**
556
- * Get the bounding box for the whole set of glyphs.
557
- *
558
- * @return {Three.Box3};
559
- */
563
+ /**
564
+ * Get the bounding box for the whole set of glyphs.
565
+ *
566
+ * @return {Three.Box3};
567
+ */
560
568
  this.getBoundingBox = () => {
561
569
  if (this.morph && this.ready && this.morph.visible) {
562
570
  if (this.boundingBoxUpdateRequired) {
@@ -564,7 +572,7 @@ const Glyphset = function () {
564
572
  this.morph.geometry.attributes.position);
565
573
  for (let i = 0; i < numberOfVertices; i++) {
566
574
  this.morph.getMatrixAt(i, _transformMatrix);
567
- this.applyMatrix4ToBox(_boundingBox1, _boundingBox2, _transformMatrix);
575
+ _boundingBox2.copy(_boundingBox1).applyMatrix4(_transformMatrix);
568
576
  if (i == 0) {
569
577
  _boundingBox3.copy(_boundingBox2);
570
578
  } else {
@@ -573,6 +581,8 @@ const Glyphset = function () {
573
581
  }
574
582
  if (_boundingBox3) {
575
583
  this.cachedBoundingBox.copy(_boundingBox3);
584
+ this.morph.updateWorldMatrix();
585
+ this.cachedBoundingBox.applyMatrix4(this.morph.matrixWorld);
576
586
  this.boundingBoxUpdateRequired = false;
577
587
  } else
578
588
  return undefined;
@@ -582,11 +592,11 @@ const Glyphset = function () {
582
592
  return undefined;
583
593
  }
584
594
 
585
- /**
586
- * Set the local time of this glyphset.
587
- *
588
- * @param {Number} time - Can be any value between 0 to duration.
589
- */
595
+ /**
596
+ * Set the local time of this glyphset.
597
+ *
598
+ * @param {Number} time - Can be any value between 0 to duration.
599
+ */
590
600
  this.setMorphTime = time => {
591
601
  if (time > this.duration)
592
602
  this.inbuildTime = this.duration;
@@ -612,14 +622,19 @@ const Glyphset = function () {
612
622
  return false;
613
623
  }
614
624
 
625
+ /**
626
+ * Get the current inbuild time of the
627
+ *
628
+ * @return {Number}
629
+ */
615
630
  this.getCurrentTime = () => {
616
631
  return this.inbuildTime;
617
632
  }
618
633
 
619
634
 
620
- /**
621
- * Clear this glyphset and its list of glyphs which will release them from the memory.
622
- */
635
+ /**
636
+ * Clear this glyphset and its list of glyphs which will release them from the memory.
637
+ */
623
638
  this.dispose = () => {
624
639
  for (let i = glyphList.length - 1; i >= 0; i--) {
625
640
  glyphList[i].dispose();
@@ -638,12 +653,14 @@ const Glyphset = function () {
638
653
  this.groupName = undefined;
639
654
  }
640
655
 
641
- //Update the geometry and colours depending on the morph.
656
+ /**
657
+ * Update the glyphsets if required the render.
658
+ */
642
659
  this.render = (delta, playAnimation, options) => {
643
660
  if (playAnimation == true) {
644
661
  let targetTime = this.inbuildTime + delta;
645
662
  if (targetTime > this.duration)
646
- targetTime = targetTime - this.duration
663
+ targetTime = targetTime - this.duration;
647
664
  this.inbuildTime = targetTime;
648
665
  if (morphColours || morphVertices) {
649
666
  updateMorphGlyphsets();