-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
268 lines (222 loc) · 9.6 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
var THREE = require("three"),
TrackballControls = require("./TrackballControls");
/********************************************************************
* Convenience function to generate colors from scalar array and LUT
*******************************************************************/
function updateFieldColorBuffer(lut, fieldData, buf) {
//var buf = new Float32Array(fieldData.length * 3);
for (var i = 0; i < fieldData.length; ++i) {
var idx = i * 3,
color = lut.getColor(fieldData[i]);
buf[idx] = color[0];
buf[idx + 1] = color[1];
buf[idx + 2] = color[2];
}
return buf;
}
/********************************************************************
* Convenience function to generate the correct number of empties
*******************************************************************/
function updateGreyColorBuffer(buf) {
for (var i = 0; i < buf.length; ++i) {
buf[i] = 0.5;
}
return buf;
}
export default class GeometryBuilder {
constructor(lutMgr, geometryDataModel, pipelineModel, queryDataModel) {
this.meshMap = {};
this.firstSceneLoad = true;
this.lookupTableManager = lutMgr;
this.geometryDataModel = geometryDataModel;
this.pipelineModel = pipelineModel;
this.queryDataModel = queryDataModel;
this.layerMap = this.queryDataModel.originalData.Geometry.layer_map,
this.fieldMap = this.queryDataModel.originalData.CompositePipeline.fields;
this.maxSize = queryDataModel.originalData.Geometry.object_size;
// Handle pipeline color change
const updatePipeline = (pipelineQuery, envelope) => {
var size = pipelineQuery.length;
for(var i = 0; i < size; i += 2) {
var objectName = this.layerMap[pipelineQuery[i]],
fieldName = this.fieldMap[pipelineQuery[i+1]];
// if (fieldName !== '_') {
if (fieldName) {
this.geometryDataModel.colorGeometryBy(objectName, fieldName);
this.updateObjectVisibility(objectName, true);
} else {
this.updateObjectVisibility(objectName, false);
}
}
this.queryDataModel.fetchData();
};
this.pipelineModel.onChange(updatePipeline);
// Handle data fetching
this.queryDataModel.onDataChange( (data, envelope) => {
if(data.scene) {
this.geometryDataModel.loadScene(data.scene.data);
if(this.firstSceneLoad) {
this.firstSceneLoad = false;
updatePipeline(this.pipelineModel.getPipelineQuery());
}
}
});
// Handle LookupTable change
this.lookupTableManager.addFields(this.queryDataModel.originalData.Geometry.ranges,
this.queryDataModel.originalData.LookupTables);
this.lookupTableManager.onChange( (data, envelope) => {
this.updateColoring(data.change, data.lut);
});
// Scene management
this.camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
this.camera.position.z = 50;
this.scene = new THREE.Scene();
this.scene.add( this.camera );
this.dirLight = new THREE.DirectionalLight( 0xffffff );
this.dirLight.position.set( 200, 200, 1000 ).normalize();
// this.dirLight2 = new THREE.DirectionalLight( 0xffffff );
// this.dirLight2.position.set( 200, -200, 1000).normalize();
this.camera.add( this.dirLight );
this.camera.add( this.dirLight.target );
// this.camera.add( this.dirLight2 );
// this.camera.add( this.dirLight2.target );
this.geometryBuilderSubscription = this.geometryDataModel.onGeometryReady( (data, envelope) => {
this.updateGeometry(data);
});
}
destroy() {
// Remove listener
if(this.geometryBuilderSubscription) {
this.geometryBuilderSubscription.unsubscribe();
this.geometryBuilderSubscription = null;
}
}
configureRenderer(canvas) {
this.renderer = new THREE.WebGLRenderer({ canvas });
this.renderer.setSize( window.innerWidth, window.innerHeight );
this.controls = new TrackballControls(this.camera, canvas);
this.controls.rotateSpeed = 5.0;
this.controls.zoomSpeed = 20;
this.controls.panSpeed = 2;
this.controls.noZoom = false;
this.controls.noPan = false;
this.controls.staticMoving = true;
this.controls.dynamicDampingFactor = 0.3;
// Create a render() method that can be called from anywhere
this.render = () => {
requestAnimationFrame( this.render );
this.controls.update();
this.renderer.render(this.scene, this.camera);
};
this.queryDataModel.fetchData();
}
updateColoring(whatChanged, lookupTable) {
for (var name in this.meshMap) {
var renderInfo = this.meshMap[name];
if (renderInfo.colorArrayName === lookupTable.name) {
var colors = updateFieldColorBuffer(lookupTable, renderInfo.fieldData, renderInfo.colorBuffer);
renderInfo.mesh.geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
}
}
}
updateGeometry(geo){
if (!this.meshMap.hasOwnProperty(geo.name)) {
// Create new Geometry
var pSize = this.maxSize[geo.name].points,
iSize = this.maxSize[geo.name].index,
geom = new THREE.BufferGeometry(),
material = new THREE.MeshPhongMaterial({
color: 0xdddddd,
specular: 0x444444,
shininess: 10,
side: THREE.DoubleSide,
vertexColors: THREE.VertexColors,
shading: THREE.FlatShading
}),
colorBuf = new Float32Array(pSize * 3);
// Add object to the scene
var sceneObject = new THREE.Mesh( geom, material );
this.scene.add( sceneObject );
// Register geometry
this.meshMap[geo.name] = {
'mesh': sceneObject,
'material': material,
'colorBuffer': colorBuf
};
// Allocate max size object
let pArray = new Float32Array(pSize * 3);
let iArray = new Uint32Array(iSize);
for(let i = 0; i < pSize; i++) {
pArray[i] = Math.random();
}
for(let i = 0; i < iSize; i++) {
iArray[i] = i % pSize;
}
geom.addAttribute( 'position', new THREE.BufferAttribute( pArray, 3 ) );
geom.setIndex( new THREE.BufferAttribute( iArray, 1 ) );
geom.addAttribute( 'color', new THREE.BufferAttribute( colorBuf, 3 ) );
geom.computeFaceNormals();
//geom.computeVertexNormals();
this.renderer.render(this.scene, this.camera);
} else {
var renderInfo = this.meshMap[geo.name],
geometry = renderInfo.mesh.geometry,
colors = renderInfo.colorBuffer;
if (geometry.vertices && geo.points.length !== geometry.vertices.length) {
console.log("******** We may have a problem here, new point count = " + geo.points.length + ", old point count = " + geometry.vertices.length);
// FIXME: Allocate new color buffer here
}
// geometry.setIndex( new THREE.BufferAttribute( geo.index, 1 ) );
var index = geometry.getIndex(),
pos = geometry.getAttribute ( 'position' ),
color = geometry.getAttribute ( 'color' );
if(index){
index.array = geo.index;
index.needsUpdate = true;
}
if(pos){
pos.array = geo.points;
pos.needsUpdate = true;
}
geometry.computeFaceNormals();
//geometry.computeVertexNormals();
// geometry.normalizeNormals();
if (geo.hasOwnProperty('field')) {
renderInfo.colorArrayName = geo.fieldName;
renderInfo.fieldData = geo.field;
colors = updateFieldColorBuffer(this.lookupTableManager.getLookupTable(geo.fieldName), geo.field, colors);
} else {
renderInfo.colorArrayName = null;
renderInfo.fieldData = null;
colors = updateGreyColorBuffer(colors);
}
if(color) {
color.array = colors;
color.needsUpdate = true;
}
geometry.computeBoundingBox();
}
}
updateObjectVisibility(name, visibility) {
if(this.meshMap[name]){
this.meshMap[name].mesh.visible = visibility;
}
}
resetCamera() {
// Get bounds
var bbox = new THREE.Box3();
for(var meshName in this.meshMap) {
var mesh = this.meshMap[meshName].mesh;
mesh.geometry.computeBoundingBox();
bbox.expandByPoint(mesh.geometry.boundingBox.min).expandByPoint(mesh.geometry.boundingBox.max);
}
let {center, radius} = bbox.getBoundingSphere();
this.controls.resetCamera(center, radius);
}
updateSize(width, height) {
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
this.renderer.setSize( width, height );
this.controls.handleResize();
}
}