UNPKG

awv3

Version:
443 lines (386 loc) 21.9 kB
'use strict'; var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray'); var _slicedToArray3 = _interopRequireDefault(_slicedToArray2); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _getIterator2 = require('babel-runtime/core-js/get-iterator'); var _getIterator3 = _interopRequireDefault(_getIterator2); var _helpers = require('./helpers'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } self.addEventListener('message', function (message) { message = message.data; if (message.command === 'Blob' && (message.type === 'Binary' || message.type === 'Base64')) { delete message.type; (0, _helpers.unpack)(message); if (Array.isArray(message.data)) message.data = message.data[0]; } var _message = message, data = _message.data, task = _message.task, options = _message.options; var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = (0, _getIterator3.default)(data.containers), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var container = _step.value; var id = container.id, owner = container.owner, properties = container.properties, _container$meshes = container.meshes, meshes = _container$meshes === undefined ? [] : _container$meshes, _container$lines = container.lines, lines = _container$lines === undefined ? [] : _container$lines, _container$arcs = container.arcs, arcs = _container$arcs === undefined ? [] : _container$arcs, _container$edges = container.edges, edges = _container$edges === undefined ? [] : _container$edges; var material = properties.material; var transfer = [], group = void 0, points = void 0, x = void 0, y = void 0, z = void 0, color = void 0, opacity = void 0; if (lines.length > 0 || arcs.length > 0 || edges.length > 0) { var i, il; (function () { var line = void 0, count = 0, segments = 64; var result = container.line = { groups: [] }; var mergedLines = lines.reduce(function (prev, next) { return prev + next.points.length; }, 0); var mergedEdges = edges.reduce(function (prev, next) { return prev + (next.points.length / 3 - 1) * 6; }, 0); var mergedArcs = arcs.reduce(function (prev, next) { return prev + segments * 6; }, 0); result.vertices = new Float32Array(mergedLines + mergedEdges + mergedArcs); transfer.push(result.vertices.buffer); result.distances = new Float32Array((mergedLines + mergedEdges + mergedArcs) / 3); transfer.push(result.distances.buffer); var firstVec = void 0, lastVec = void 0; mergedLines = 0; for (var index = 0, length = lines.length; index < length; index++) { line = lines[index]; points = line.points; if (options.materials.multi) { line.properties = (0, _extends3.default)({ material: {} }, line.properties); color = line.properties.material.color || material.color || [0, 0, 0]; color = [color[0] / 255, color[1] / 255, color[2] / 255]; opacity = line.properties.material.opacity ? line.properties.material.opacity : material.opacity; opacity = typeof opacity !== 'undefined' ? opacity : 0; group = { start: mergedLines, count: points.length, index: result.groups.length, meta: (0, _extends3.default)({ id: line.id, container: id, owner: owner, layer: line.properties.layer || properties.layer }, line.meta, { type: 'line', start: [points[0], points[1], points[2]], end: [points[points.length - 3], points[points.length - 2], points[points.length - 1]], min: [+Infinity, +Infinity, +Infinity], max: [-Infinity, -Infinity, -Infinity], material: { color: color, opacity: opacity } }) }; result.groups.push(group); } for (var _i = 0, l = points.length / 6; _i < l; _i++) { mergedLines += 6; firstVec = [points[_i * 6 + 0], points[_i * 6 + 1], points[_i * 6 + 2]]; result.vertices[count++] = firstVec[0]; result.vertices[count++] = firstVec[1]; result.vertices[count++] = firstVec[2]; options.materials.multi && (0, _helpers.expand)(group.meta.min, group.meta.max, firstVec); lastVec = [points[_i * 6 + 3], points[_i * 6 + 4], points[_i * 6 + 5]]; result.vertices[count++] = lastVec[0]; result.vertices[count++] = lastVec[1]; result.vertices[count++] = lastVec[2]; options.materials.multi && (0, _helpers.expand)(group.meta.min, group.meta.max, lastVec); } } for (var _index = 0, _length = edges.length; _index < _length; _index++) { line = edges[_index]; points = line.points; if (options.materials.multi) { line.properties = (0, _extends3.default)({ material: {} }, line.properties); color = line.properties.material.color || material.color || [0, 0, 0]; color = [color[0] / 255, color[1] / 255, color[2] / 255]; opacity = line.properties.material.opacity ? line.properties.material.opacity : material.opacity; opacity = typeof opacity !== 'undefined' ? opacity : 0; group = { start: mergedLines, count: (points.length / 3 - 1) * 6, index: result.groups.length, meta: (0, _extends3.default)({ id: line.id, container: id, owner: owner, layer: line.properties.layer || properties.layer }, line.meta, { type: 'line', start: [points[0], points[1], points[2]], end: [points[points.length - 3], points[points.length - 2], points[points.length - 1]], min: [+Infinity, +Infinity, +Infinity], max: [-Infinity, -Infinity, -Infinity], material: { color: color, opacity: opacity } }) }; result.groups.push(group); } // Edges are line-strips, a list of points consisting of 3 floats each, [x, y, z] // In order to convert them to regular lines each endpoint will act as the next start-point for (var _i2 = 1, _l = points.length / 3; _i2 < _l; _i2++) { mergedLines += 6; firstVec = [points[_i2 * 3 - 3], points[_i2 * 3 - 2], points[_i2 * 3 - 1]]; result.vertices[count++] = firstVec[0]; result.vertices[count++] = firstVec[1]; result.vertices[count++] = firstVec[2]; options.materials.multi && (0, _helpers.expand)(group.meta.min, group.meta.max, firstVec); lastVec = [points[_i2 * 3 + 0], points[_i2 * 3 + 1], points[_i2 * 3 + 2]]; result.vertices[count++] = lastVec[0]; result.vertices[count++] = lastVec[1]; result.vertices[count++] = lastVec[2]; options.materials.multi && (0, _helpers.expand)(group.meta.min, group.meta.max, lastVec); } } var radius = void 0, thetaStart = void 0, thetaLength = void 0, center = void 0, zAxis = void 0, xAxis = void 0, yAxis = void 0, scalar = void 0, invScalar = void 0, segment = void 0, start = void 0; for (var _index2 = 0, _length2 = arcs.length; _index2 < _length2; _index2++) { line = arcs[_index2]; radius = line.radius; thetaStart = 0; thetaLength = Math.abs(Math.abs(line.angle) - Math.PI) < 0.000001 ? Math.PI * 2 : line.angle; center = line.center; zAxis = line.zAxis; xAxis = line.xAxis; yAxis = line.zAxis; var _yAxis = yAxis; var _yAxis2 = (0, _slicedToArray3.default)(_yAxis, 3); x = _yAxis2[0]; y = _yAxis2[1]; z = _yAxis2[2]; yAxis[0] = y * xAxis[2] - z * xAxis[1]; yAxis[1] = z * xAxis[0] - x * xAxis[2]; yAxis[2] = x * xAxis[1] - y * xAxis[0]; scalar = Math.sqrt(yAxis[0] * yAxis[0] + yAxis[1] * yAxis[1] + yAxis[2] * yAxis[2]); if (scalar !== 0) { invScalar = 1 / scalar; yAxis[0] *= invScalar; yAxis[1] *= invScalar; yAxis[2] *= invScalar; } else { yAxis[0] = yAxis[1] = yAxis[2] = 0; } var matrix = [xAxis[0], xAxis[1], xAxis[2], 0, yAxis[0], yAxis[1], yAxis[2], 0, zAxis[0], zAxis[1], zAxis[2], 0, center[0], center[1], center[2], 1]; segment = thetaStart / segments * thetaLength; lastVec = [radius * Math.cos(segment), radius * Math.sin(segment), 0]; (0, _helpers.applyMatrix)(lastVec, matrix); if (options.materials.multi) { line.properties = (0, _extends3.default)({ material: {} }, line.properties); color = line.properties.material.color || material.color || [0, 0, 0]; color = [color[0] / 255, color[1] / 255, color[2] / 255]; opacity = line.properties.material.opacity ? line.properties.material.opacity : material.opacity; opacity = typeof opacity !== 'undefined' ? opacity : 0; group = { start: mergedLines, count: segments * 6, index: result.groups.length, meta: (0, _extends3.default)({}, line.meta, { id: line.id, container: id, owner: owner, layer: line.properties.layer || properties.layer, type: 'arc', circle: line.isCircle, angle: line.angle, start: lastVec, radius: radius, thetaStart: thetaStart, thetaLength: thetaLength, center: center, zAxis: zAxis, xAxis: xAxis, yAxis: yAxis, min: [+Infinity, +Infinity, +Infinity], max: [-Infinity, -Infinity, -Infinity], material: { color: color, opacity: opacity } }) }; result.groups.push(group); (0, _helpers.expand)(group.meta.min, group.meta.max, lastVec); } for (var _i3 = 1; _i3 <= segments; _i3++) { mergedLines += 6; firstVec = [lastVec[0], lastVec[1], lastVec[2]]; result.vertices[count++] = firstVec[0]; result.vertices[count++] = firstVec[1]; result.vertices[count++] = firstVec[2]; options.materials.multi && (0, _helpers.expand)(group.meta.min, group.meta.max, firstVec); segment = thetaStart + _i3 / segments * thetaLength; lastVec = [radius * Math.cos(segment), radius * Math.sin(segment), 0]; (0, _helpers.applyMatrix)(lastVec, matrix); result.vertices[count++] = lastVec[0]; result.vertices[count++] = lastVec[1]; result.vertices[count++] = lastVec[2]; options.materials.multi && (0, _helpers.expand)(group.meta.min, group.meta.max, lastVec); } } var d = 0, c = 0; var vertices = result.vertices; for (i = 0, il = vertices.length / 3; i < il; i++) { if (i > 0) { d += (0, _helpers.distance)([result.vertices[i * 3 + 0], result.vertices[i * 3 + 1], result.vertices[i * 3 + 2]], [result.vertices[(i - 1) * 3 + 0], result.vertices[(i - 1) * 3 + 1], result.vertices[(i - 1) * 3 + 2]]); } result.distances[c++] = d; } delete container.lines; })(); } if (meshes.length > 0) { var mesh = void 0, mergedVertices = 0, mergedIndices = 0, mergedUvs = 0, mergedNormals = 0; var result = container.mesh = { groups: [] }; for (var index = 0, l = meshes.length; index < l; index++) { mesh = meshes[index]; mergedVertices += mesh.vertices.length; mergedIndices += mesh.indices.length; if (!!mesh.uvs) mergedUvs += mesh.uvs.length; if (!!mesh.normals) mergedNormals += mesh.normals.length; } // Merge meshes that aren't individual if (mergedVertices > 0) { result.vertices = new Float32Array(mergedVertices); transfer.push(result.vertices.buffer); result.indices = new Uint32Array(mergedIndices); transfer.push(result.indices.buffer); if (mergedUvs > 0) { result.uvs = new Float32Array(mergedUvs); transfer.push(result.uvs.buffer); } if (mergedNormals > 0) { result.normals = new Float32Array(mergedNormals); transfer.push(result.normals.buffer); } mergedVertices = 0; mergedIndices = 0; mergedUvs = 0; mergedNormals = 0; var offset = 0, indexCount = 0; for (var _i4 = 0; _i4 < meshes.length; _i4++) { mesh = meshes[_i4]; if (options.materials.multi) { mesh.properties = (0, _extends3.default)({ material: {}, surface: {} }, mesh.properties); color = mesh.properties.material.color || material.color || [0, 0, 0]; color = [color[0] / 255, color[1] / 255, color[2] / 255]; opacity = mesh.properties.material.opacity ? mesh.properties.material.opacity : material.opacity; opacity = typeof opacity !== 'undefined' ? opacity : 0; group = { start: indexCount, count: mesh.indices.length, index: result.groups.length, meta: (0, _extends3.default)({}, mesh.meta, { id: mesh.id, container: id, owner: owner, layer: mesh.properties.layer || properties.layer, type: mesh.properties.surface.type }, mesh.properties.surface, { min: [+Infinity, +Infinity, +Infinity], max: [-Infinity, -Infinity, -Infinity], material: { color: color, opacity: opacity } }) }; result.groups.push(group); indexCount += mesh.indices.length; } for (var vindex = 0, vlen = mesh.vertices.length / 3; vindex < vlen; vindex++) { x = mesh.vertices[vindex * 3 + 0]; y = mesh.vertices[vindex * 3 + 1]; z = mesh.vertices[vindex * 3 + 2]; options.materials.multi && (0, _helpers.expand)(group.meta.min, group.meta.max, [x, y, z]); result.vertices[mergedVertices++] = x; result.vertices[mergedVertices++] = y; result.vertices[mergedVertices++] = z; } for (var findex = 0, flen = mesh.indices.length; findex < flen; findex++) { result.indices[mergedIndices++] = mesh.indices[findex] + offset; } if (!!mesh.uvs) { for (var uvindex = 0, uvlen = mesh.uvs.length; uvindex < uvlen; uvindex++) { result.uvs[mergedUvs++] = mesh.uvs[uvindex]; } } if (!!mesh.normals) { for (var nindex = 0, nlen = mesh.normals.length; nindex < nlen; nindex++) { result.normals[mergedNormals++] = mesh.normals[nindex]; } } offset += mesh.vertices.length / 3; } } delete container.meshes; } self.postMessage({ action: 'Result', result: container, task: task }, transfer); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } self.postMessage({ action: 'Finished', task: task }); }, false);