@ccp-nc/crystvis-js 0.4.13

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 (100) hide show
  1. package/.eslintrc.json +16 -0
  2. package/.github/workflows/test-mocha.yml +30 -0
  3. package/.vscode/settings.json +4 -0
  4. package/LICENSE +21 -0
  5. package/README.html +1127 -0
  6. package/README.md +76 -0
  7. package/demo/demo.css +30 -0
  8. package/demo/index.html +76 -0
  9. package/demo/main.js +143 -0
  10. package/docs/.nojekyll +0 -0
  11. package/docs-tutorials/Events.md +57 -0
  12. package/docs-tutorials/Queries.md +50 -0
  13. package/fonts/Rubik/OFL.txt +93 -0
  14. package/fonts/Rubik/README.txt +77 -0
  15. package/fonts/Rubik/Rubik-Italic-VariableFont_wght.ttf +0 -0
  16. package/fonts/Rubik/Rubik-VariableFont_wght.ttf +0 -0
  17. package/fonts/Rubik/static/Rubik-Black.ttf +0 -0
  18. package/fonts/Rubik/static/Rubik-BlackItalic.ttf +0 -0
  19. package/fonts/Rubik/static/Rubik-Bold.ttf +0 -0
  20. package/fonts/Rubik/static/Rubik-BoldItalic.ttf +0 -0
  21. package/fonts/Rubik/static/Rubik-ExtraBold.ttf +0 -0
  22. package/fonts/Rubik/static/Rubik-ExtraBoldItalic.ttf +0 -0
  23. package/fonts/Rubik/static/Rubik-Italic.ttf +0 -0
  24. package/fonts/Rubik/static/Rubik-Light.ttf +0 -0
  25. package/fonts/Rubik/static/Rubik-LightItalic.ttf +0 -0
  26. package/fonts/Rubik/static/Rubik-Medium.ttf +0 -0
  27. package/fonts/Rubik/static/Rubik-MediumItalic.ttf +0 -0
  28. package/fonts/Rubik/static/Rubik-Regular.ttf +0 -0
  29. package/fonts/Rubik/static/Rubik-SemiBold.ttf +0 -0
  30. package/fonts/Rubik/static/Rubik-SemiBoldItalic.ttf +0 -0
  31. package/index.html +25 -0
  32. package/index.js +11 -0
  33. package/jsconf.json +14 -0
  34. package/lib/assets/fonts/Rubik-Medium.fnt +297 -0
  35. package/lib/assets/fonts/Rubik-Medium.png +0 -0
  36. package/lib/assets/fonts/bmpfonts.in.js +16 -0
  37. package/lib/assets/fonts/bmpfonts.js +9 -0
  38. package/lib/assets/fonts/font.js +82 -0
  39. package/lib/assets/fonts/index.js +14 -0
  40. package/lib/assets/fonts/threebmfont.js +28 -0
  41. package/lib/data.js +125 -0
  42. package/lib/formats/cell.js +114 -0
  43. package/lib/formats/cif.js +22 -0
  44. package/lib/formats/magres.js +337 -0
  45. package/lib/formats/xyz.js +124 -0
  46. package/lib/loader.js +87 -0
  47. package/lib/model.js +2076 -0
  48. package/lib/modelview.js +382 -0
  49. package/lib/nmrdata.js +2898 -0
  50. package/lib/orbit.js +1233 -0
  51. package/lib/primitives/atoms.js +261 -0
  52. package/lib/primitives/cell.js +160 -0
  53. package/lib/primitives/dither.js +156 -0
  54. package/lib/primitives/ellipsoid.js +183 -0
  55. package/lib/primitives/geometries.js +20 -0
  56. package/lib/primitives/index.js +48 -0
  57. package/lib/primitives/isosurface.js +171 -0
  58. package/lib/primitives/shapes.js +100 -0
  59. package/lib/primitives/sprites.js +172 -0
  60. package/lib/query.js +158 -0
  61. package/lib/render.js +440 -0
  62. package/lib/selbox.js +361 -0
  63. package/lib/shaders/aura.frag +26 -0
  64. package/lib/shaders/aura.vert +37 -0
  65. package/lib/shaders/dither.frag +42 -0
  66. package/lib/shaders/dither.vert +8 -0
  67. package/lib/shaders/index.in.js +17 -0
  68. package/lib/shaders/index.js +25 -0
  69. package/lib/shaders/msdf300.frag +25 -0
  70. package/lib/shaders/msdf300.vert +45 -0
  71. package/lib/tensor.js +227 -0
  72. package/lib/utils.js +168 -0
  73. package/lib/visualizer.js +480 -0
  74. package/package.json +106 -0
  75. package/scripts/build-bundle.js +17 -0
  76. package/scripts/build-fonts.js +43 -0
  77. package/scripts/build-resources.js +46 -0
  78. package/scripts/plugins-shim.js +10 -0
  79. package/test/chemdata.js +69 -0
  80. package/test/data/CHA.cif +74 -0
  81. package/test/data/H2O.xyz +8 -0
  82. package/test/data/H2_bound.xyz +4 -0
  83. package/test/data/ethanol.cell +25 -0
  84. package/test/data/ethanol.magres +238 -0
  85. package/test/data/example_single.cif +789 -0
  86. package/test/data/frac.cell +8 -0
  87. package/test/data/org.cif +427 -0
  88. package/test/data/pyridine.xyz +13 -0
  89. package/test/data/si8.xyz +10 -0
  90. package/test/loader.js +107 -0
  91. package/test/model.js +368 -0
  92. package/test/query.js +135 -0
  93. package/test/tensor.js +133 -0
  94. package/test/test-html/examples.js +1485 -0
  95. package/test/test-html/index.html +33 -0
  96. package/test/test-html/index.js +279 -0
  97. package/tools/compile_colors.py +120 -0
  98. package/tools/compile_periodic.py +96 -0
  99. package/tools/ptable.json +497 -0
  100. package/tools/test +5844 -0
@@ -0,0 +1,183 @@
1
+ 'use strict';
2
+
3
+ import _ from 'lodash';
4
+ import * as mjs from 'mathjs';
5
+ import * as THREE from 'three';
6
+ import {
7
+ unitSphere,
8
+ } from './geometries.js';
9
+ import {
10
+ DitherMaterial
11
+ } from './dither.js';
12
+ import {
13
+ addStaticVar
14
+ } from '../utils.js';
15
+
16
+ // Basic materials
17
+ const _phong = new THREE.MeshPhongMaterial({});
18
+
19
+ class EllipsoidMesh extends THREE.Mesh {
20
+
21
+ constructor(parameters = {}) {
22
+
23
+ const defaults = {
24
+ center: [0, 0, 0],
25
+ eigenvalues: [1, 1, 1],
26
+ eigenvectors: [
27
+ [1, 0, 0],
28
+ [0, 1, 0],
29
+ [0, 0, 1]
30
+ ],
31
+ color: 0xff0000,
32
+ opacity: 0.5,
33
+ opacityMode: EllipsoidMesh.DITHER,
34
+ showCircles: true,
35
+ showAxes: true,
36
+ scalingFactor: 1.0
37
+ };
38
+
39
+ parameters = _.merge(defaults, parameters);
40
+
41
+ var geometry = unitSphere;
42
+ var material;
43
+
44
+ var c = new THREE.Color(parameters.color);
45
+
46
+ switch (parameters.opacityMode) {
47
+ case EllipsoidMesh.DITHER:
48
+ material = new DitherMaterial({
49
+ color: c,
50
+ opacity: parameters.opacity,
51
+ shiftSeed: parameters.ditherSeed
52
+ });
53
+ break;
54
+ case EllipsoidMesh.PHONG:
55
+ material = new THREE.MeshPhongMaterial({
56
+ transparent: true,
57
+ color: c,
58
+ opacity: parameters.opacity
59
+ });
60
+ break;
61
+ default:
62
+ throw new Error('Invalid opacityMode argument passed to EllipsoidMesh');
63
+ }
64
+
65
+ super(geometry, material);
66
+
67
+ if (parameters.showCircles) {
68
+
69
+ let matline = new THREE.LineBasicMaterial({
70
+ color: new THREE.Color(c),
71
+ });
72
+ let geoline = new THREE.CircleBufferGeometry(1.0, 32);
73
+ geoline = new THREE.EdgesGeometry(geoline);
74
+
75
+ let cseg = new THREE.LineSegments(geoline, matline);
76
+ this.add(cseg);
77
+ cseg = new THREE.LineSegments(geoline, matline);
78
+ cseg.rotateX(Math.PI / 2.0);
79
+ this.add(cseg);
80
+ cseg = new THREE.LineSegments(geoline, matline);
81
+ cseg.rotateY(Math.PI / 2.0);
82
+ this.add(cseg);
83
+
84
+ }
85
+
86
+ if (parameters.showAxes) {
87
+
88
+ let matline = new THREE.LineBasicMaterial({
89
+ color: new THREE.Color(c),
90
+ });
91
+
92
+ let geoline = new THREE.BufferGeometry().setFromPoints([new THREE.Vector3(-1, 0, 0),
93
+ new THREE.Vector3(1, 0, 0)
94
+ ]);
95
+
96
+ let cseg = new THREE.Line(geoline, matline);
97
+ this.add(cseg);
98
+ cseg = new THREE.Line(geoline, matline);
99
+ cseg.rotateZ(Math.PI / 2.0);
100
+ this.add(cseg);
101
+ cseg = new THREE.Line(geoline, matline);
102
+ cseg.rotateY(Math.PI / 2.0);
103
+ this.add(cseg);
104
+ }
105
+
106
+ let c0 = parameters.center;
107
+
108
+ if (!(c0 instanceof THREE.Vector3)) {
109
+ c0 = new THREE.Vector3(c0[0], c0[1], c0[2]);
110
+ }
111
+ this.position.copy(c0);
112
+
113
+ this._scalefactor = parameters.scalingFactor;
114
+
115
+ this.eigenvalues = parameters.eigenvalues;
116
+ this.eigenvectors = parameters.eigenvectors;
117
+
118
+ this.renderOrder = 0.5;
119
+ }
120
+
121
+ get eigenvalues() {
122
+ return Array.from(this._eigenvalues);
123
+ }
124
+
125
+ set eigenvalues(v) {
126
+ this._eigenvalues = v;
127
+ this.scalingFactor = this._scalefactor;
128
+ }
129
+
130
+ get eigenvectors() {
131
+ return JSON.parse(JSON.stringify(this._eigenvectors));
132
+ }
133
+
134
+ set eigenvectors(v) {
135
+ this._eigenvectors = v;
136
+
137
+ var basis = _.map(_.range(3), (i) => {
138
+ return new THREE.Vector3(v[0][i],
139
+ v[1][i],
140
+ v[2][i]).normalize();
141
+ });
142
+ var rotm = new THREE.Matrix4();
143
+ rotm.makeBasis(basis[0], basis[1], basis[2]);
144
+ this.setRotationFromMatrix(rotm);
145
+ }
146
+
147
+ get color() {
148
+ return this.material.color;
149
+ }
150
+
151
+ set color(c) {
152
+ // Change all colors
153
+ c = new THREE.Color(c);
154
+ this.material.color = c;
155
+ for (let i = 0; i < this.children.length; ++i) {
156
+ this.children[i].material.color = c;
157
+ }
158
+ }
159
+
160
+ get opacity() {
161
+ return this.material.opacity;
162
+ }
163
+
164
+ set opacity(o) {
165
+ this.material.opacity = o;
166
+ }
167
+
168
+ get scalingFactor() {
169
+ return this._scalefactor;
170
+ }
171
+
172
+ set scalingFactor(s) {
173
+ this._scalefactor = s;
174
+ this.scale.fromArray(mjs.multiply(this._eigenvalues, s));
175
+ }
176
+ }
177
+
178
+ addStaticVar(EllipsoidMesh, 'DITHER', 0);
179
+ addStaticVar(EllipsoidMesh, 'PHONG', 1);
180
+
181
+ export {
182
+ EllipsoidMesh
183
+ }
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * @fileoverview Geometries used multiple times, generated once for economy
5
+ */
6
+
7
+ import * as THREE from 'three';
8
+
9
+ const resolution = 16;
10
+
11
+ const unitSphere = new THREE.SphereBufferGeometry(1.0, resolution, resolution);
12
+ const unitCylinder = new THREE.CylinderBufferGeometry(1, 1, 1, resolution);
13
+ unitCylinder.rotateX(Math.PI / 2.0);
14
+ const unitCircle = new THREE.CircleBufferGeometry(1.0, resolution*2);
15
+
16
+ export {
17
+ unitSphere,
18
+ unitCylinder,
19
+ unitCircle
20
+ };
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * @fileoverview Basic objects rendered by Renderer, derived from THREE.js
5
+ * classes
6
+ * @module
7
+ */
8
+
9
+ import {
10
+ AtomMesh,
11
+ BondMesh,
12
+ AuraMesh
13
+ } from './atoms.js'
14
+
15
+ import {
16
+ BoxMesh,
17
+ AxesMesh
18
+ } from './cell.js'
19
+
20
+ import {
21
+ ImageSprite,
22
+ TextSprite
23
+ } from './sprites.js'
24
+
25
+ import {
26
+ EllipsoidMesh
27
+ } from './ellipsoid.js'
28
+
29
+ import {
30
+ LineMesh
31
+ } from './shapes.js'
32
+
33
+ import {
34
+ IsosurfaceMesh
35
+ } from './isosurface.js'
36
+
37
+ export {
38
+ AtomMesh,
39
+ BondMesh,
40
+ AuraMesh,
41
+ BoxMesh,
42
+ AxesMesh,
43
+ EllipsoidMesh,
44
+ LineMesh,
45
+ IsosurfaceMesh,
46
+ ImageSprite,
47
+ TextSprite
48
+ }
@@ -0,0 +1,171 @@
1
+ 'use strict';
2
+
3
+ import _ from 'lodash';
4
+ import * as THREE from 'three';
5
+ import IsoSurf from 'isosurface';
6
+ import {
7
+ DitherMaterial
8
+ } from './dither.js';
9
+ import { cellMatrix3, addStaticVar } from '../utils.js';
10
+
11
+ // Basic materials
12
+ const _phong = new THREE.MeshPhongMaterial({});
13
+
14
+ class IsosurfaceMesh extends THREE.Mesh {
15
+
16
+ constructor(field, threshold, lattice, parameters={}) {
17
+ /**
18
+ * Build an isosurface from the data found in field, using threshold
19
+ * as a cutoff. Field must be a triple nested array ordered in such a
20
+ * way that:
21
+ *
22
+ * field[x][y][z]
23
+ *
24
+ * is the value at x, y, z. Dimensions must be consistent. Field will
25
+ * be considered as spanning the orthorombic cell. If no cell is
26
+ * passed, field's own dimensions will be used.
27
+ *
28
+ * Three methods are available:
29
+ * 0 = surface nets
30
+ * 1 = marching cubes
31
+ * 2 = marching tetrahedra
32
+ *
33
+ * @param {Array} field Volumetric data
34
+ * @param {float} threshold Isosurface threshold
35
+ * @param {Array} lattice Unit cell on which the data is defined
36
+ * @param {Object} parameters Options:
37
+ * color
38
+ * opacity
39
+ * opacityMode [IsosurfaceMesh.RENDER_DITHER,
40
+ * IsosurfaceMesh.RENDER_PHONG,
41
+ * IsosurfaceMesh.RENDER_WFRAME]
42
+ * isoMethod [IsosurfaceMesh.ISO_SURFACE_NETS,
43
+ * IsosurfaceMesh.ISO_MARCHING_CUBES
44
+ * IsosurfaceMesh.ISO_MARCHING_TETRAHEDRA]
45
+ */
46
+
47
+ const defaults = {
48
+ color: 0x00ff00,
49
+ opacity: 0.5,
50
+ opacityMode: IsosurfaceMesh.RENDER_DITHER,
51
+ isoMethod: IsosurfaceMesh.ISO_SURFACE_NETS
52
+ }
53
+
54
+ parameters = _.merge(defaults, parameters);
55
+
56
+ // First compute the isosurface vertices and faces
57
+ var dims = [0, 0, 0];
58
+
59
+ try {
60
+ dims[0] = field.length;
61
+ dims[1] = field[0].length;
62
+ dims[2] = field[0][0].length;
63
+ } catch (e) {
64
+ // If we're here, something is wrong with field
65
+ throw Error('Invalid field for isosurface rendering');
66
+ }
67
+
68
+ if (lattice instanceof Array) {
69
+ lattice = cellMatrix3(lattice);
70
+ }
71
+
72
+ var isofunc = IsoSurf[parameters.isoMethod];
73
+
74
+ if (isofunc == null) {
75
+ throw Error('Invalid method for isosurface rendering');
76
+ }
77
+
78
+ var mesh = isofunc(dims, function(x, y, z) {
79
+ return field[x][y][z] - threshold;
80
+ });
81
+
82
+ // Convert positions to absolute coordinates
83
+ var abspos = mesh.positions.map(function(xyz) {
84
+ return (new THREE.Vector3(xyz[0] / dims[0],
85
+ xyz[1] / dims[1],
86
+ xyz[2] / dims[2])).applyMatrix3(lattice);
87
+ });
88
+
89
+ // Now generate a mesh geometry
90
+ var geometry = new THREE.BufferGeometry();
91
+
92
+ var verts = [];
93
+ for (let i = 0; i < mesh.cells.length; ++i) {
94
+ var face = mesh.cells[i];
95
+ for (var j = 0; j < 3; ++j) {
96
+ var v = abspos[face[j]];
97
+ verts.push(v.x);
98
+ verts.push(v.y);
99
+ verts.push(v.z);
100
+ }
101
+ }
102
+
103
+ verts = new Float32Array(verts);
104
+ geometry.setAttribute('position', new THREE.BufferAttribute(verts, 3));
105
+ geometry.computeVertexNormals();
106
+ geometry.computeFaceNormals();
107
+
108
+ var c = new THREE.Color(parameters.color);
109
+ var material;
110
+
111
+ switch (parameters.opacityMode) {
112
+ case IsosurfaceMesh.RENDER_DITHER:
113
+ material = new DitherMaterial({
114
+ color: c,
115
+ opacity: parameters.opacity
116
+ });
117
+ break;
118
+ case IsosurfaceMesh.RENDER_PHONG:
119
+ material = new THREE.MeshPhongMaterial({
120
+ transparent: true,
121
+ color: c,
122
+ opacity: parameters.opacity
123
+ });
124
+ break;
125
+ case IsosurfaceMesh.RENDER_WFRAME:
126
+ // Same as PHONG, but with wireframe set on
127
+ material = new THREE.MeshPhongMaterial({
128
+ color: c,
129
+ wireframe: true
130
+ });
131
+ break;
132
+ default:
133
+ throw new Error('Invalid opacityMode argument passed to IsosurfaceMesh');
134
+ }
135
+
136
+ super(geometry, material);
137
+
138
+ this.opacityMode = parameters.opacityMode;
139
+ this.renderOrder = 0.5;
140
+ }
141
+
142
+ get color() {
143
+ return this.material.color;
144
+ }
145
+
146
+ set color(c) {
147
+ c = new THREE.Color(c);
148
+ this.material.color = c;
149
+ }
150
+
151
+ get opacity() {
152
+ return this.material.opacity;
153
+ }
154
+
155
+ set opacity(o) {
156
+ if (this.opacityMode !== IsosurfaceMesh.RENDER_WFRAME) {
157
+ this.material.opacity = o;
158
+ }
159
+ }
160
+
161
+ }
162
+
163
+ addStaticVar(IsosurfaceMesh, 'RENDER_DITHER', 0);
164
+ addStaticVar(IsosurfaceMesh, 'RENDER_PHONG', 1);
165
+ addStaticVar(IsosurfaceMesh, 'RENDER_WFRAME', 2);
166
+
167
+ addStaticVar(IsosurfaceMesh, 'ISO_SURFACE_NETS', 'surfaceNets');
168
+ addStaticVar(IsosurfaceMesh, 'ISO_MARCHING_CUBES', 'marchingCubes');
169
+ addStaticVar(IsosurfaceMesh, 'ISO_MARCHING_TETRAHEDRA', 'marchingTetrahedra');
170
+
171
+ export { IsosurfaceMesh };
@@ -0,0 +1,100 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * @fileoverview Basic primitives for geometric shapes
5
+ * @module
6
+ */
7
+
8
+ import _ from 'lodash';
9
+ import * as THREE from 'three';
10
+ import {
11
+ AtomImage
12
+ } from '../model.js';
13
+
14
+ class LineMesh extends THREE.Line {
15
+
16
+ /**
17
+ * Create a line to draw on the model
18
+ *
19
+ * @param {Array | AtomImage} p1 Line starting point
20
+ * @param {Array | AtomImage} p2 Line end point
21
+ * @param {Object} parameters Options:
22
+ * - color
23
+ * - dashed
24
+ * - linewidth
25
+ *
26
+ */
27
+ constructor(p1, p2, parameters = {}) {
28
+
29
+ var defaults = {
30
+ color: 0xffffff,
31
+ dashed: false,
32
+ linewidth: 1,
33
+ onOverlay: false
34
+ };
35
+
36
+ parameters = _.merge(defaults, parameters);
37
+
38
+ // Interpret the points
39
+ if (p1 instanceof AtomImage) {
40
+ p1 = p1.xyz;
41
+ }
42
+
43
+ if (p2 instanceof AtomImage) {
44
+ p2 = p2.xyz;
45
+ }
46
+
47
+ // We position this in the middle point
48
+ var center = new THREE.Vector3(p1[0]+p2[0], p1[1]+p2[1], p1[2]+p2[2]).divideScalar(2.0);
49
+ var diff = new THREE.Vector3(p1[0]-p2[0], p1[1]-p2[1], p1[2]-p2[2]).divideScalar(2.0);
50
+
51
+ var geo = new THREE.BufferGeometry().setFromPoints([diff, diff.clone().negate()]);
52
+
53
+ var mat;
54
+
55
+ if (parameters.dashed) {
56
+ mat = new THREE.LineDashedMaterial({
57
+ color: new THREE.Color(parameters.color),
58
+ linewidth: parameters.linewidth,
59
+ dashSize: 0.15,
60
+ gapSize: 0.05,
61
+ depthTest: !parameters.onOverlay
62
+ });
63
+ } else {
64
+ mat = new THREE.LineBasicMaterial({
65
+ color: new THREE.Color(parameters.color),
66
+ linewidth: parameters.linewidth,
67
+ depthTest: !parameters.onOverlay
68
+ });
69
+ }
70
+
71
+ super(geo, mat);
72
+ this.position.copy(center);
73
+
74
+ if (parameters.onOverlay)
75
+ this.renderOrder = 9999;
76
+ this.computeLineDistances();
77
+
78
+ }
79
+
80
+ get color() {
81
+ return this.material.color.getHex();
82
+ }
83
+
84
+ set color(c) {
85
+ this.material.color = new THREE.Color(c);
86
+ }
87
+
88
+ get linewidth() {
89
+ return this.material.linewidth;
90
+ }
91
+
92
+ set linewidth(lw) {
93
+ this.material.linewidth = lw;
94
+ }
95
+
96
+ }
97
+
98
+ export {
99
+ LineMesh
100
+ }
@@ -0,0 +1,172 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * @fileoverview Primitive classes for Sprites and Billboards of various types
5
+ */
6
+
7
+ import _ from 'lodash';
8
+ import * as THREE from 'three';
9
+ import {
10
+ RubikMedium
11
+ } from '../assets/fonts/index.js';
12
+
13
+
14
+ class ImageSprite extends THREE.Sprite {
15
+ /**
16
+ * Create an ImageSprite object
17
+ *
18
+ * @param {Array} position Position of the sprite
19
+ * @param {Object} parameters Options:
20
+ * position
21
+ * size
22
+ * color
23
+ *
24
+ */
25
+ constructor(map, parameters = {}) {
26
+
27
+ var defaults = {
28
+ position: [0, 0, 0],
29
+ size: 1.0,
30
+ color: 0xffffff,
31
+ onOverlay: false
32
+ };
33
+
34
+ parameters = _.merge(defaults, parameters);
35
+
36
+ var p = parameters.position;
37
+
38
+ if (p instanceof Array) {
39
+ p = new THREE.Vector3(p[0], p[1], p[2]);
40
+ }
41
+
42
+
43
+ if (!(map instanceof THREE.Texture)) {
44
+ map = new THREE.TextureLoader().load(map);
45
+ }
46
+ var smat = new THREE.SpriteMaterial({
47
+ map: map,
48
+ color: parameters.color,
49
+ depthTest: !parameters.onOverlay
50
+ });
51
+
52
+ super(smat);
53
+
54
+ this.position.copy(p);
55
+ var s = parameters.size;
56
+ this.scale.copy(new THREE.Vector3(s, s, s));
57
+ this.renderOrder = 2;
58
+
59
+ if (parameters.onOverlay)
60
+ this.renderOrder = 9999;
61
+ }
62
+
63
+ get size() {
64
+ return this.scale.x;
65
+ }
66
+
67
+ set size(s) {
68
+ this.scale.set(s, s, s);
69
+ }
70
+
71
+ get color() {
72
+ return this.material.color.getHex();
73
+ }
74
+
75
+ set color(c) {
76
+ this.material.color = new THREE.Color(c);
77
+ this.needsUpdate = true;
78
+ }
79
+ }
80
+
81
+ class TextSprite extends THREE.Mesh {
82
+
83
+
84
+ constructor(text, parameters = {}) {
85
+
86
+ var defaults = {
87
+ position: [0, 0, 0],
88
+ font: RubikMedium,
89
+ color: 0xffffff,
90
+ opacity: 1.0,
91
+ height: 1.0,
92
+ faceCamera: false,
93
+ fixScale: false,
94
+ shift: [0,0,0],
95
+ geometryOptions: {},
96
+ onOverlay: false
97
+ };
98
+
99
+ parameters = _.merge(defaults, parameters);
100
+
101
+ var p = parameters.position;
102
+
103
+ if (p instanceof Array) {
104
+ p = new THREE.Vector3(p[0], p[1], p[2]);
105
+ }
106
+
107
+ // Create the geometry
108
+ var geo = parameters.font.getTextGeometry(text);
109
+
110
+ // Calculate the scale
111
+ var targScale = parameters.height / geo.layout.height;
112
+
113
+ // Create the material
114
+ var mat = parameters.font.getTextMaterial(parameters.color,
115
+ parameters.opacity,
116
+ parameters.faceCamera,
117
+ parameters.fixScale,
118
+ targScale,
119
+ parameters.shift,
120
+ !parameters.onOverlay);
121
+
122
+ super(geo, mat);
123
+ if (!parameters.faceCamera)
124
+ this.scale.set(targScale,-targScale,targScale);
125
+ this.position.copy(p);
126
+
127
+ if (parameters.onOverlay)
128
+ this.renderOrder = 9999;
129
+ }
130
+
131
+ get color() {
132
+ return this.material.uniforms.color.value.getHex();
133
+ }
134
+
135
+ set color(c) {
136
+ this.material.uniforms.color.value = new THREE.Color(c);
137
+ this.material.uniformsNeedUpdate = true;
138
+ }
139
+
140
+ get opacity() {
141
+ return this.material.uniforms.opacity.value;
142
+ }
143
+
144
+ set opacity(o) {
145
+ this.material.uniforms.opacity.value = o;
146
+ this.material.uniformsNeedUpdate = true;
147
+ }
148
+
149
+ get targScale() {
150
+ return this.material.uniforms.targScale.value;
151
+ }
152
+
153
+ set targScale(s) {
154
+ this.material.uniforms.opacity.value = s;
155
+ this.material.uniformsNeedUpdate = true;
156
+ }
157
+
158
+ get shift() {
159
+ return this.material.uniforms.shift.value.toArray();
160
+ }
161
+
162
+ set shift(s) {
163
+ this.material.uniforms.shift.value = s;
164
+ this.material.uniformsNeedUpdate = true;
165
+ }
166
+
167
+ }
168
+
169
+ export {
170
+ ImageSprite,
171
+ TextSprite
172
+ }