Skip to content
Permalink
Browse files

Buffers transfered to web workers.

Fixed bug in indexed wireframing.
Indexed wireframes now use same vertex buffer.
Cleaned up resetView

Closes #239.
Closes #241.
  • Loading branch information...
tsherif committed Dec 18, 2014
1 parent cfe80bd commit a0d65c81fd390e38951b6ace3a7f8623a7e622ae
@@ -30,7 +30,7 @@
// BrainBrowser Surface Viewer.
$(function() {
"use strict";

var THREE = BrainBrowser.SurfaceViewer.THREE;
var atlas_labels = {};

@@ -671,10 +671,12 @@ $(function() {
});
viewer.loadModelFromURL("models/left-color-mesh.obj", {
format: "mniobj",
recenter: true,
cancel: defaultCancelOptions(current_request)
});
viewer.loadModelFromURL("models/right-color-mesh.obj", {
format: "mniobj",
recenter: true,
cancel: defaultCancelOptions(current_request)
});
},
@@ -469,6 +469,7 @@ BrainBrowser.SurfaceViewer.modules.loading = function(viewer) {

parse_worker.addEventListener("message", function(event) {
var model_data = event.data;
var transfer;

if (model_data.error){
error_message = "error parsing model.\n" +
@@ -480,7 +481,7 @@ BrainBrowser.SurfaceViewer.modules.loading = function(viewer) {
throw new Error(error_message);
}

model_data.colors = model_data.colors || [0.7, 0.7, 0.7, 1.0];
model_data.colors = model_data.colors || new Float32Array([0.7, 0.7, 0.7, 1.0]);

if (BrainBrowser.WEBGL_UINT_INDEX_ENABLED) {
loadIndexedModel(model_data, callback);
@@ -491,7 +492,21 @@ BrainBrowser.SurfaceViewer.modules.loading = function(viewer) {
callback(event.data);
});

deindex_worker.postMessage(model_data);
transfer = [model_data.vertices.buffer];

if (model_data.normals) {
transfer.push(model_data.normals.buffer);
}

if (model_data.colors) {
transfer.push(model_data.colors.buffer);
}

model_data.shapes.forEach(function(shape) {
transfer.push(shape.indices.buffer);
});

deindex_worker.postMessage(model_data, transfer);
}

parse_worker.terminate();
@@ -610,22 +625,23 @@ BrainBrowser.SurfaceViewer.modules.loading = function(viewer) {
var is_line = model_data.type === "line";
var render_depth = options.render_depth;
var pick_ignore = options.pick_ignore;
var recenter = options.recenter || model_data.split;
var new_shapes = [];
var shape, shape_data;
var i, count;
var object_description = {is_line: is_line};
var model_position, model_normal, model_color;
var position_buffer, normal_buffer, color_buffer;

if (BrainBrowser.WEBGL_UINT_INDEX_ENABLED) {

model_position = new THREE.BufferAttribute(new Float32Array(model_data.vertices), 3);
position_buffer = new THREE.BufferAttribute(new Float32Array(model_data.vertices), 3);

if (model_data.normals) {
model_normal = new THREE.BufferAttribute(new Float32Array(model_data.normals), 3);
normal_buffer = new THREE.BufferAttribute(new Float32Array(model_data.normals), 3);
}

if (model_data.colors) {
model_color = new THREE.BufferAttribute(new Float32Array(model_data.colors), 4);
color_buffer = new THREE.BufferAttribute(new Float32Array(model_data.colors), 4);
}
}

@@ -637,40 +653,45 @@ BrainBrowser.SurfaceViewer.modules.loading = function(viewer) {
for (i = 0, count = shapes.length; i < count; i++) {
shape_data = model_data.shapes[i];

if (shape_data.indices.length === 0) {
continue;
}

if (BrainBrowser.WEBGL_UINT_INDEX_ENABLED) {
setShapeColors(model_color.array, shape_data.color, shape_data.indices);
setShapeColors(color_buffer.array, shape_data.color, shape_data.indices);

object_description = {
position: model_position,
normal: model_normal,
color: model_color,
position: position_buffer,
normal: normal_buffer,
color: color_buffer,
index: new THREE.BufferAttribute(new Uint32Array(shape_data.indices), 1),
};

} else {

model_position = model_normal = model_color = null;
position_buffer = normal_buffer = color_buffer = null;

model_position = new THREE.BufferAttribute(new Float32Array(shape_data.unindexed.position), 3);
position_buffer = new THREE.BufferAttribute(new Float32Array(shape_data.unindexed.position), 3);

if (shape_data.unindexed.normal) {
model_normal = new THREE.BufferAttribute(new Float32Array(shape_data.unindexed.normal), 3);
normal_buffer = new THREE.BufferAttribute(new Float32Array(shape_data.unindexed.normal), 3);
}

if (shape_data.unindexed.color) {
model_color = new THREE.BufferAttribute(new Float32Array(shape_data.unindexed.color), 4);
color_buffer = new THREE.BufferAttribute(new Float32Array(shape_data.unindexed.color), 4);
}

object_description = {
position: model_position,
normal: model_normal,
color: model_color
position: position_buffer,
normal: normal_buffer,
color: color_buffer
};

}

object_description.is_line = is_line;
object_description.centroid = shape_data.centroid;
object_description.recenter = recenter;

shape = createObject(object_description);
shape.name = shape_data.name || filename + "_" + (i + 1);
@@ -711,6 +732,7 @@ BrainBrowser.SurfaceViewer.modules.loading = function(viewer) {
var index = object_description.index;
var centroid = object_description.centroid;
var is_line = object_description.is_line;
var recenter = object_description.recenter;

var geometry = new THREE.BufferGeometry();
var index_array, tmp_position_array, position_index;
@@ -719,21 +741,23 @@ BrainBrowser.SurfaceViewer.modules.loading = function(viewer) {

geometry.dynamic = true;

if (index) {
index_array = index.array;
// tmp_position_array used because there will be repeats in the index array.
tmp_position_array = new Float32Array(position_array);
for (i = 0, count = index_array.length; i < count; i++) {
position_index = index_array[i] * 3;
position_array[position_index] = tmp_position_array[position_index] - centroid.x;
position_array[position_index + 1] = tmp_position_array[position_index + 1] - centroid.y;
position_array[position_index + 2] = tmp_position_array[position_index + 2] - centroid.z;
}
} else {
for (i = 0, count = position_array.length; i < count; i += 3) {
position_array[i] -= centroid.x;
position_array[i + 1] -= centroid.y;
position_array[i + 2] -= centroid.z;
if (recenter) {
if (index) {
index_array = index.array;
// tmp_position_array used because there will be repeats in the index array.
tmp_position_array = new Float32Array(position_array);
for (i = 0, count = index_array.length; i < count; i++) {
position_index = index_array[i] * 3;
position_array[position_index] = tmp_position_array[position_index] - centroid.x;
position_array[position_index + 1] = tmp_position_array[position_index + 1] - centroid.y;
position_array[position_index + 2] = tmp_position_array[position_index + 2] - centroid.z;
}
} else {
for (i = 0, count = position_array.length; i < count; i += 3) {
position_array[i] -= centroid.x;
position_array[i + 1] -= centroid.y;
position_array[i + 2] -= centroid.z;
}
}
}

@@ -759,11 +783,15 @@ BrainBrowser.SurfaceViewer.modules.loading = function(viewer) {
} else {
material = new THREE.MeshPhongMaterial({color: 0xFFFFFF, ambient: 0xFFFFFF, specular: 0x101010, shininess: 150, vertexColors: THREE.VertexColors});
shape = new THREE.Mesh(geometry, material);
shape.has_wireframe = true;
shape.userData.has_wireframe = true;
}

shape.userData.centroid = centroid;
shape.position.set(centroid.x, centroid.y, centroid.z);

if (recenter) {
shape.userData.recentered = true;
shape.position.set(centroid.x, centroid.y, centroid.z);
}

return shape;
}
@@ -173,7 +173,6 @@ BrainBrowser.SurfaceViewer.modules.rendering = function(viewer) {
*/
viewer.resetView = function() {
var model = viewer.model;
var child, wireframe, centroid;
var i, count;
var inv = new THREE.Matrix4();
inv.getInverse(model.matrix);
@@ -182,19 +181,6 @@ BrainBrowser.SurfaceViewer.modules.rendering = function(viewer) {
camera.position.set(0, 0, default_camera_distance);
light.position.set(0, 0, default_camera_distance);
viewer.zoom = 1;

for (i = 0, count = viewer.model.children.length; i < count; i++) {
child = model.children[i];
centroid = child.userData.centroid;
if (centroid) {
child.position.set(centroid.x, centroid.y, centroid.z);
} else {
child.position.set(0, 0, 0);
}
child.rotation.set(0, 0, 0);

wireframe = child.getObjectByName("__WIREFRAME__");
}

viewer.updated = true;
};
@@ -331,14 +317,19 @@ BrainBrowser.SurfaceViewer.modules.rendering = function(viewer) {
} else {
// We're dealing with an imported model.

// Objects have their origins centered
// to help with transparency, so to check
// check against original vertices we have
// move them back.
centroid = intersect_object.userData.centroid;
cx = centroid.x;
cy = centroid.y;
cz = centroid.z;
if (intersect_object.userData.recentered) {
// Objects have their origins centered
// to help with transparency, so to check
// check against original vertices we have
// move them back.
centroid = intersect_object.userData.centroid;
cx = centroid.x;
cy = centroid.y;
cz = centroid.z;
} else {
cx = cy = cz = 0;
}


inv_matrix.getInverse(intersect_object.matrixWorld);
intersect_point = intersection.point.applyMatrix4(inv_matrix);
@@ -159,7 +159,7 @@ BrainBrowser.SurfaceViewer.modules.views = function(viewer) {
wireframe = shape.getObjectByName("__WIREFRAME__");
if (wireframe) {
toggleWireframe(shape, wireframe, is_wireframe);
} else if (shape.has_wireframe) {
} else if (shape.userData.has_wireframe && !shape.userData.creating_wireframe) {
createWireframe(shape, function(wireframe) {
toggleWireframe(shape, wireframe, is_wireframe);
});
@@ -225,6 +225,7 @@ BrainBrowser.SurfaceViewer.modules.views = function(viewer) {
////////////////////////////////////

function createWireframe(shape, callback) {
shape.userData.creating_wireframe = true;
if (active_wireframe_jobs < MAX_WIREFRAME_WORKERS) {
launchWireframeWorker(shape, callback);
} else {
@@ -240,18 +241,27 @@ BrainBrowser.SurfaceViewer.modules.views = function(viewer) {
var message;

worker.addEventListener("message", function(event) {
var positions = event.data.positions || geometry.position.array;
var colors = event.data.colors || geometry.color.array;
var indices = event.data.indices;

var wire_geometry = new THREE.BufferGeometry();
var material, wireframe;
var position_buffer, color_buffer, index_buffer;

if (event.data.positions) {
position_buffer = new THREE.BufferAttribute(event.data.positions, 3);
} else {
position_buffer = geometry.position;
}

wire_geometry.addAttribute("position", new THREE.BufferAttribute(positions, 3));
wire_geometry.addAttribute("color", new THREE.BufferAttribute(colors, 4));
if (event.data.colors) {
color_buffer = new THREE.BufferAttribute(event.data.colors, 4);
} else {
color_buffer = geometry.color;
}

wire_geometry.addAttribute("position", position_buffer);
wire_geometry.addAttribute("color", color_buffer);

if (indices) {
wire_geometry.addAttribute("index", new THREE.BufferAttribute(indices, 1));
if (event.data.indices) {
wire_geometry.addAttribute("index", new THREE.BufferAttribute(event.data.indices, 1));
}

wire_geometry.attributes.color.needsUpdate = true;
@@ -262,6 +272,7 @@ BrainBrowser.SurfaceViewer.modules.views = function(viewer) {
wireframe.name = "__WIREFRAME__";
wireframe.material.visible = false;
shape.add(wireframe);
shape.creating_wireframe = false;
active_wireframe_jobs--;
callback(wireframe);

@@ -33,19 +33,40 @@
var verts = data.vertices;
var norms = data.normals;
var colors = data.colors;
var transfer = [verts.buffer];
var i, count;
var shape, unindexed;

for (i = 0, count = shapes.length; i < count; i++) {
shape = shapes[i];
unindexed = deindex(shapes[i].indices, verts, norms, shape.color || colors);
unindexed = deindex(shape.indices, verts, norms, shape.color || colors);
shape.centroid = unindexed.centroid;
shape.unindexed = unindexed.unindexed;

transfer.push(shape.indices.buffer);

transfer.push(shape.unindexed.position.buffer);

if (shape.unindexed.normal) {
transfer.push(shape.unindexed.normal.buffer);
}

if (shape.unindexed.color) {
transfer.push(shape.unindexed.color.buffer);
}
}

if (norms) {
transfer.push(norms.buffer);
}

if (colors) {
transfer.push(colors.buffer);
}

data.deindexed = true;

self.postMessage(data);
self.postMessage(data, transfer);
});

function deindex(indices, verts, norms, colors) {
@@ -29,9 +29,8 @@
"use strict";

self.addEventListener("message", function(e) {
var message = e.data;
var data = message.data;
self.postMessage(parse(data));
var result = parse(e.data.data)
self.postMessage(result, [result.values.buffer]);
});

function parse(string) {
@@ -57,7 +56,7 @@
}

return {
values: values,
values: new Float32Array(values),
min: min,
max: max
};

0 comments on commit a0d65c8

Please sign in to comment.
You can’t perform that action at this time.