-
Notifications
You must be signed in to change notification settings - Fork 7
/
HighbrowLayerVisualization.js
342 lines (296 loc) · 10.8 KB
/
HighbrowLayerVisualization.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
var THREE = require('three');
var OBJLoader = require('three-obj-loader');
var ColladaLoader = require('three-collada-loader');
function addGuides(scene) {
// Add guide lines for axes
var material = new THREE.LineBasicMaterial({
color: "blue"
});
var geometry = new THREE.Geometry();
geometry.vertices.push(
new THREE.Vector3( 0, 0, 0 ),
new THREE.Vector3( 10000, 0, 0 )
);
var xline = new THREE.Line( geometry, material );
material = new THREE.LineBasicMaterial({
color: "red"
});
geometry = new THREE.Geometry();
geometry.vertices.push(
new THREE.Vector3( 0, 0, 0 ),
new THREE.Vector3( 0, 10000, 0 )
);
var yline = new THREE.Line( geometry, material );
material = new THREE.LineBasicMaterial({
color: "green"
});
geometry = new THREE.Geometry();
geometry.vertices.push(
new THREE.Vector3( 0, 0, 0 ),
new THREE.Vector3( 0, 0, 10000 )
);
var zline = new THREE.Line( geometry, material );
scene.add( xline );
scene.add( yline );
scene.add( zline );
}
/**
* experiment
*/
function HighbrowLayerVisualization(highbrowLayer, opts) {
if (!opts) opts = {};
this.layer = highbrowLayer;
this.meshCells = [];
this.opts = opts;
this.spacing = opts.spacing;
this.width = undefined;
this.height = undefined;
this.$container = undefined;
this.camera = undefined;
this.controls = undefined;
this.light = undefined;
this.scene = undefined;
this.renderer = undefined;
this.loader = new ColladaLoader();
this.projector = new THREE.Projector();
this.targets = [];
this.cubeSize = opts.cubeSize || 100;
this.clock = new THREE.Clock();
this.loader.options.centerGeometry = true;
this.geometry = new THREE.BoxGeometry(
this.cubeSize, this.cubeSize, this.cubeSize
);
// Use a default spacing.
if (! this.spacing) {
this.spacing = {
x: 1.4, y: 1.4, z: 1.4
};
}
this._setupContainer(opts.elementId);
this._setupCamera();
this._setupScene();
this._setupControls();
this.offset = opts.offset || {};
if (this.offset.x == undefined) this.offset.x = 0;
if (this.offset.y == undefined) this.offset.y = 0;
if (this.offset.z == undefined) this.offset.z = 0;
}
HighbrowLayerVisualization.prototype._setupContainer = function(elementId) {
if (elementId) {
this.$container = $('#' + elementId);
this.width = this.$container.innerWidth();
this.height = this.$container.innerHeight();
} else {
this.$container = $('body');
this.width = window.innerWidth;
this.height = window.innerHeight;
}
};
HighbrowLayerVisualization.prototype._setupCamera = function() {
// Set up camera position.
this.camera = new THREE.PerspectiveCamera(
25, this.width / this.height, 50, 1e7
);
};
HighbrowLayerVisualization.prototype._setupControls = function() {
var controls = this.controls = new THREE.FlyControls(
this.camera, this.renderer.domElement
);
controls.movementSpeed = 1000;
controls.rollSpeed = Math.PI / 24;
controls.autoForward = false;
controls.dragToLook = true;
};
HighbrowLayerVisualization.prototype._setupScene = function() {
var scene;
var renderer;
this.scene = new THREE.Scene();
scene = this.scene;
this.light = new THREE.PointLight(0xFFFFFF);
scene.add(this.light);
renderer = this.renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0xf0f0f0);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(this.width, this.height);
renderer.sortObjects = false;
this.$container.append(renderer.domElement);
};
HighbrowLayerVisualization.prototype._getCellValue = function(index) {
let neuronState = this.layer.getNeuronByIndex(index).getState()
let out = { state: neuronState }
if (neuronState == "inactive") {
out.color = new THREE.Color('#FFFEEE')
} else {
out.color = new THREE.Color('orange')
}
return out;
};
HighbrowLayerVisualization.prototype._getCellOrigin = function(index) {
return this.layer.getNeuronByIndex(index).getOrigin()
};
/**
* Creates all the geometries within the grid. These are only created once and
* updated as cells change over time, so this function should only be called
* one time for each grid of cells created in the scene.
*/
HighbrowLayerVisualization.prototype._createMeshCells =
function(grid, origin, type) {
var scene = this.scene;
// meshCells is a 1-d array indexed by global cell order.
var meshCells = [];
var spacing = this.spacing;
var cube, textTexture, material, cellValue, cellColor;
var cellOrigin;
var textTextures = this.textTextures = []
for (var index = 0; index < this.layer.getNeurons().length; index++) {
cellValue = this._getCellValue(index);
if (cellValue) {
cellOrigin = this._getCellOrigin(index);
cellColor = cellValue.color;
if (cellColor == undefined) {
cellColor = cellValue.state.color;
}
textTexture = new THREEx.DynamicTexture(
64, 64
);
textTexture.context.font = "18px Verdana";
// So we can update the text on each cell.
textTextures.push(textTexture)
material = new THREE.MeshPhongMaterial({
color: cellColor,
transparent: true,
opacity: 1.0,
map: textTexture.texture
});
material.alphaTest = 0.15;
cube = new THREE.Mesh(this.geometry, material);
// Wireframe.
var geo = new THREE.EdgesGeometry( cube.geometry );
var mat = new THREE.LineBasicMaterial(
{ color: 0x333, linewidth: 1 }
);
var wireframe = new THREE.LineSegments( geo, mat );
cube.add( wireframe );
cube.position.x = origin.x + (this.cubeSize * spacing.x)
* cellOrigin.x;
cube.position.y = origin.y + (this.cubeSize * spacing.y)
* cellOrigin.y;
cube.position.z = origin.z + (this.cubeSize * spacing.z)
* cellOrigin.z;
// Allow subclasses to mutate each cube.
if (typeof(this._mutateCube) == 'function') {
this._mutateCube(cube, cellValue, cx, cy, cz)
}
cube.updateMatrix();
cube.matrixAutoUpdate = false;
grid.add(cube);
meshCells.push(cube);
// Keep track of cubes in the grid so they can be clickable.
this.targets.push(cube);
}
}
scene.add(grid);
addGuides(scene);
return meshCells;
};
/*
* Updates the mesh cell colors based on the cells, which might have changed.
* This function should only be called when the cells change.
*/
HighbrowLayerVisualization.prototype._applyMeshCells =
function(meshCells, origin) {
var cube, cellValue, cellOrigin;
var spacing = this.spacing;
var textTexture, displayText, cellPosition;
for (var index = 0; index < this.layer.getNeurons().length; index++) {
cube = meshCells[index];
cellValue = this._getCellValue(index);
cellOrigin = this._getCellOrigin(index);
if (cellValue) {
cube.material.color = new THREE.Color(cellValue.color);
cube.position.x = origin.x + (this.cubeSize * spacing.x)
* cellOrigin.x;
cube.position.y = origin.y + (this.cubeSize * spacing.y)
* cellOrigin.y;
cube.position.z = origin.z + (this.cubeSize * spacing.z)
* cellOrigin.z;
// This will display positional information on the cell texture for
// debugging purposes.
cellPosition = this.layer.getNeuronByIndex(index).getPosition()
textTexture = this.textTextures[index]
textTexture.clear('white')
textTexture.drawText(index, undefined, 30, 'black')
textTexture.drawText(
cellPosition.x + ", " + cellPosition.y + ", " + cellPosition.z,
undefined,
50,
'black'
)
textTexture.texture.needsUpdate = true
// Allow subclasses to mutate each cube.
if (typeof(this._mutateCube) == 'function') {
this._mutateCube(
cube, cellValue, cellOrigin.x, cellOrigin.y, cellOrigin.z
)
}
cube.updateMatrix();
}
}
};
HighbrowLayerVisualization.prototype.getOffsetCenterPosition =
function(cubeSize, spacing, offset) {
var dims = this.layer.getDimensions()
return {
x: (offset.x * cubeSize * spacing.x)
+ (dims.x * cubeSize * spacing.x) / 2,
y: (offset.y * cubeSize * spacing.y)
+ (dims.y * cubeSize * spacing.y) / 2,
z: (offset.z * cubeSize * spacing.z)
};
};
HighbrowLayerVisualization.prototype.getTargets = function() {
return this.targets;
};
HighbrowLayerVisualization.prototype.render = function(opts) {
if (!opts) opts = {};
var me = this;
var renderer = this.renderer;
var scene = this.scene;
var controls = this.controls;
var camera = this.camera;
var light = this.light;
var w = this.width;
var h = this.height;
var grid = new THREE.Group();
// var position = this.position = this.getOffsetCenterPosition(
// this.cubeSize, this.spacing, this.offset
// );
var position = this.position = {x: 0, y: 0, z: 0}
this.meshCells = this._createMeshCells(grid, position);
window.addEventListener('resize', function() {
w = me.width = me.$container.innerWidth();
h = me.height = me.$container.innerHeight();
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
innerRender();
}, false );
this.$container.append(renderer.domElement);
function animate() {
requestAnimationFrame(animate);
innerRender();
}
function innerRender() {
var delta = me.clock.getDelta();
me.controls.update( delta );
light.position.x = camera.position.x;
light.position.y = camera.position.y;
light.position.z = camera.position.z;
renderer.render(scene, camera);
}
animate();
};
HighbrowLayerVisualization.prototype.redraw = function() {
this._applyMeshCells(this.meshCells, this.position);
};
module.exports = HighbrowLayerVisualization;