forked from ghemingway/cad.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.js
85 lines (71 loc) · 2.76 KB
/
shell.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* G. Hemingway Copyright @2014
* Shell class handles all of the actual geometry - shared between Shapes
*/
"use strict";
/********************************* Helper Functions ********************************/
define(["THREE"], function(THREE) {
function Shell(id, assembly, parent, size, defaultColor, boundingBox) {
var ret = assembly.makeChild(id, this);
if (!ret) {
this._id = id;
this._assembly = assembly;
this._parent = parent;
this._size = size;
this._color = defaultColor;
this._boundingBox = boundingBox;
if (this._boundingBox.empty()) {
console.log("Found empty bounding box: " + this._id);
}
}
return ret;
}
Shell.prototype.unloadData = function() {
console.log("Shell.unloadData - Not Implemented");
};
Shell.prototype.getID = function() {
return this._id;
};
Shell.prototype.addGeometry = function(position, normals, colors) {
this.dispatchEvent({ type: "shellStartLoad", shell: this });
// Create the geometry to hold the data
this._geometry = new THREE.BufferGeometry();
this._geometry.addAttribute('position', Float32Array, this._size * 3, 3);
this._geometry.addAttribute('normal', Float32Array, this._size * 3, 3);
this._geometry.addAttribute('color', Float32Array, this._size * 3, 3);
// Setup the offsets
var chunkSize = 21845;
var i;
this._geometry.offsets = [];
var offsets = this._size / chunkSize;
for (i = 0; i < offsets; i++) {
var offset = {
start: i * chunkSize * 3,
index: i * chunkSize * 3,
count: Math.min( this._size - ( i * chunkSize ), chunkSize ) * 3
};
this._geometry.offsets.push( offset );
}
// Now load the rest of the data
this._geometry.attributes.position.array = position;
this._geometry.attributes.normal.array = normals;
this._geometry.attributes.color.array = colors;
// Compute bbox
this._geometry.computeBoundingBox();
this._boundingBox = this._geometry.boundingBox.clone();
// All done - signal completion
this._isLoaded = true;
this.dispatchEvent({ type: "shellEndLoad", shell: this });
};
Shell.prototype.getBoundingBox = function() {
return this._boundingBox;
};
Shell.prototype.getSize = function() {
return this._size;
};
Shell.prototype.getGeometry = function() {
return this._geometry;
};
// And Graham said, let shells have events too, for they are good
THREE.EventDispatcher.prototype.apply(Shell.prototype);
return Shell;
});