UNPKG

@awayjs/renderer

Version:
545 lines (544 loc) 23.2 kB
import { Vector3D, Box, Sphere } from '@awayjs/core'; import { HitTestCache } from './HitTestCache'; var MAX_INT = 268435456 - 1; // 2^28 - 1 var MIN_INT = -268435456; // -2^28 var LineElementsUtils = /** @class */ (function () { function LineElementsUtils() { } //TODO - generate this dyanamically based on num tris LineElementsUtils.hitTest = function (x, y, z, thickness, box, lineElements, count, offset) { if (offset === void 0) { offset = 0; } var positionAttributes = lineElements.positions; var posStride = positionAttributes.stride; var positions = positionAttributes.get(count, offset); var indices; var len; if (lineElements.indices) { indices = lineElements.indices.get(count, offset); positions = positionAttributes.get(positionAttributes.count); len = count * lineElements.indices.dimensions; } else { positions = positionAttributes.get(count, offset); len = count; } var id0; var id1; var ax; var ay; var bx; var by; var hitTestCache = lineElements.hitTestCache[offset] || (lineElements.hitTestCache[offset] = new HitTestCache()); var index = hitTestCache.lastCollisionIndex; if (index != -1 && index < len) { precheck: { if (indices) { id0 = indices[index] * posStride; id1 = indices[index + 1] * posStride; } else { id0 = index * posStride; id1 = (index + 1) * posStride; } ax = positions[id0]; ay = positions[id0 + 1]; bx = positions[id1]; by = positions[id1 + 1]; //from a to p var dx = ax - x; var dy = ay - y; //edge normal (a-b) var nx = by - ay; var ny = -(bx - ax); var D = Math.sqrt(nx * nx + ny * ny); //TODO: should strictly speaking be an elliptical calculation, use circle to approx temp if (Math.abs((dx * nx) + (dy * ny)) > thickness * D) break precheck; //edge vector var dot = (dx * ny) - (dy * nx); if (dot > D * D || dot < 0) break precheck; return true; } } //hard coded min vertex count to bother using a grid for if (len > 150) { var cells = hitTestCache.cells; var divisions = cells.length ? hitTestCache.divisions : (hitTestCache.divisions = Math.min(Math.ceil(Math.sqrt(len)), 32)); var conversionX = divisions / box.width; var conversionY = divisions / box.height; var minx = box.x; var miny = box.y; if (!cells.length) { //build grid //now we have bounds start creating grid cells and filling cells.length = divisions * divisions; for (var k = 0; k < len; k += 3) { if (indices) { id0 = indices[k] * posStride; id1 = indices[k + 1] * posStride; } else { id0 = k * posStride; id1 = (k + 1) * posStride; } ax = positions[id0]; ay = positions[id0 + 1]; bx = positions[id1]; by = positions[id1 + 1]; //subtractions to push into positive space var min_index_x = Math.floor((Math.min(ax, bx) - minx) * conversionX); var min_index_y = Math.floor((Math.min(ay, by) - miny) * conversionY); var max_index_x = Math.floor((Math.max(ax, bx) - minx) * conversionX); var max_index_y = Math.floor((Math.max(ay, by) - miny) * conversionY); for (var i = min_index_x; i <= max_index_x; i++) { for (var j = min_index_y; j <= max_index_y; j++) { var c = i + j * divisions; var nodes_1 = cells[c] || (cells[c] = new Array()); //push in the triangle ids nodes_1.push(k); } } } } var index_x = Math.floor((x - minx) * conversionX); var index_y = Math.floor((y - miny) * conversionY); var nodes = cells[index_x + index_y * divisions]; if (nodes == null) { hitTestCache.lastCollisionIndex = -1; return false; } var nodeCount = nodes.length; for (var n = 0; n < nodeCount; n++) { var k = nodes[n]; if (indices) { id0 = indices[k] * posStride; id1 = indices[k + 1] * posStride; } else { id0 = k * posStride; id1 = (k + 1) * posStride; } ax = positions[id0]; ay = positions[id0 + 1]; bx = positions[id1]; by = positions[id1 + 1]; //from a to p var dx = ax - x; var dy = ay - y; //edge normal (a-b) var nx = by - ay; var ny = -(bx - ax); var D = Math.sqrt(nx * nx + ny * ny); //TODO: should strictly speaking be an elliptical calculation, use circle to approx temp if (Math.abs((dx * nx) + (dy * ny)) > thickness * D) continue; //edge vector var dot = (dx * ny) - (dy * nx); if (dot > D * D || dot < 0) continue; hitTestCache.lastCollisionIndex = k; return true; } hitTestCache.lastCollisionIndex = -1; return false; } //brute force for (var k = 0; k < len; k += 6) { if (indices) { id0 = indices[k] * posStride; id1 = indices[k + 1] * posStride; } else { id0 = k * posStride; id1 = (k + 1) * posStride; } ax = positions[id0]; ay = positions[id0 + 1]; bx = positions[id1]; by = positions[id1 + 1]; //from a to p var dx = ax - x; var dy = ay - y; //edge normal (a-b) var nx = by - ay; var ny = -(bx - ax); var D = Math.sqrt(nx * nx + ny * ny); //TODO: should strictly speaking be an elliptical calculation, use circle to approx temp if (Math.abs((dx * nx) + (dy * ny)) > thickness * D) continue; //edge vector var dot = (dx * ny) - (dy * nx); if (dot > D * D || dot < 0) continue; hitTestCache.lastCollisionIndex = k; return true; } hitTestCache.lastCollisionIndex = -1; return false; }; LineElementsUtils.getBoxBounds = function (positionAttributes, indexAttributes, matrix3D, thicknessScale, cache, target, count, offset) { if (offset === void 0) { offset = 0; } var positions; var posDim = positionAttributes.dimensions; var posStride = positionAttributes.stride; var minX = 0, minY = 0, minZ = 0; var maxX = 0, maxY = 0, maxZ = 0; var indices; var len; if (indexAttributes) { len = count * indexAttributes.dimensions; indices = indexAttributes.get(count, offset); positions = positionAttributes.get(positionAttributes.count); } else { len = count; positions = positionAttributes.get(count, offset); } if (len == 0) return target; var index; var pos1, pos2, pos3, rawData; if (matrix3D) rawData = matrix3D._rawData; for (var i = 0; i < len; i += 3) { index = (indices) ? indices[i] * posStride : i * posStride; if (matrix3D) { if (posDim == 6) { pos1 = positions[index] * rawData[0] + positions[index + 1] * rawData[4] + positions[index + 2] * rawData[8] + rawData[12]; pos2 = positions[index] * rawData[1] + positions[index + 1] * rawData[5] + positions[index + 2] * rawData[9] + rawData[13]; pos3 = positions[index] * rawData[2] + positions[index + 1] * rawData[6] + positions[index + 2] * rawData[10] + rawData[14]; } else { pos1 = positions[index] * rawData[0] + positions[index + 1] * rawData[4] + rawData[12]; pos2 = positions[index] * rawData[1] + positions[index + 1] * rawData[5] + rawData[13]; } } else { pos1 = positions[index]; pos2 = positions[index + 1]; pos3 = (posDim == 6) ? positions[index + 2] : 0; } if (i == 0) { maxX = minX = pos1; maxY = minY = pos2; maxZ = minZ = (posDim == 6) ? pos3 : 0; } else { if (pos1 < minX) minX = pos1; else if (pos1 > maxX) maxX = pos1; if (pos2 < minY) minY = pos2; else if (pos2 > maxY) maxY = pos2; if (posDim == 6) { if (pos3 < minZ) minZ = pos3; else if (pos3 > maxZ) maxZ = pos3; } } } var box = new Box(minX, minY); box.right = maxX; box.bottom = maxY; this.mergeThinkness(box, thicknessScale, matrix3D); return box.union(target, target || cache); }; LineElementsUtils.mergeThinkness = function (target, thicknessScale, matrix3D) { var rawData = matrix3D === null || matrix3D === void 0 ? void 0 : matrix3D._rawData; var thicknessX = matrix3D ? thicknessScale.x * rawData[0] + thicknessScale.y * rawData[4] : thicknessScale.x; var thicknessY = matrix3D ? thicknessScale.x * rawData[1] + thicknessScale.y * rawData[5] : thicknessScale.y; target.x -= thicknessX; target.y -= thicknessY; target.width += thicknessX * 2; target.height += thicknessY * 2; return target; }; LineElementsUtils.getSphereBounds = function (positionAttributes, center, matrix3D, cache, output, count, offset) { if (offset === void 0) { offset = 0; } var positions = positionAttributes.get(count, offset); var posDim = positionAttributes.dimensions; var posStride = positionAttributes.stride; var maxRadiusSquared = 0; var radiusSquared; var len = count * posStride; var distanceX; var distanceY; var distanceZ; for (var i = 0; i < len; i += posStride) { distanceX = positions[i] - center.x; distanceY = positions[i + 1] - center.y; distanceZ = (posDim == 6) ? positions[i + 2] - center.z : -center.z; radiusSquared = distanceX * distanceX + distanceY * distanceY + distanceZ * distanceZ; if (maxRadiusSquared < radiusSquared) maxRadiusSquared = radiusSquared; } if (output == null) output = new Sphere(); output.x = center.x; output.y = center.y; output.z = center.z; output.radius = Math.sqrt(maxRadiusSquared); return output; }; LineElementsUtils.prepareScale9 = function (elem, bounds, grid, clone) { var target = clone ? elem.clone() : elem; var shapeBounds = LineElementsUtils.getBoxBounds(elem.positions, elem.indices, null, new Vector3D(), null, null, elem._numElements || elem._numVertices); var sliceX = [ MIN_INT, grid.x, grid.right, MAX_INT ]; var sliceY = [ MIN_INT, grid.y, grid.bottom, MAX_INT ]; var chunkX = { from: 0, to: 0 }; var chunkY = { from: 0, to: 0 }; for (var i = 1; i < 3; i++) { if (shapeBounds.x > sliceX[i]) { chunkX.from = chunkX.to = i; } if (shapeBounds.y > sliceY[i]) { chunkY.from = chunkY.to = i; } } for (var i = 0; i < 3; i++) { if (shapeBounds.right >= sliceX[i] && i >= chunkX.from) { chunkX.to = i; } if (shapeBounds.bottom >= sliceY[i] && i >= chunkY.from) { chunkY.to = i; } } target.scale9Grid = grid; target.originalScale9Bounds = bounds; var indices = target.scale9Indices = Array.from({ length: 9 }, function (_) { return 0; }); var stack = this.restoreLineSegments(target); // shape already in valid region // not require run slicer for this case if (chunkX.from === chunkX.to && chunkY.from === chunkY.to) { var buff = new Float32Array(stack.length * 3); for (var i = 0; i < stack.length; i++) { buff[i * 3 + 0] = stack[i].x; buff[i * 3 + 1] = stack[i].y; buff[i * 3 + 2] = 0; } target.initialScale9Positions = buff; target.scale9Indices[chunkY.from * 3 + chunkX.from] = target._numElements || target._numVertices; return target; } var posByChunks = []; var count = 0; while (stack.length && count < 10000) { var b = stack.pop(); var a = stack.pop(); var cX = 0; var cY = 0; var breakAll = false; for (var y = chunkY.from; y <= chunkY.to; y++) { var day = ~~((sliceY[y + 1] - a.y) * 10000) / 10000; var dby = ~~((sliceY[y + 1] - b.y) * 10000) / 10000; // slicer is crossed, emit point if (day * dby < 0) { var alpha = day / (day - dby); var c = new Vector3D(a.x + alpha * (b.x - a.x), a.y + alpha * (b.y - a.y), 0, 0); // push new segs; stack.push(c, b, a, c); // drop process breakAll = true; break; } if ((a.y + b.y) * 0.5 > sliceY[y] && (a.y + b.y) * 0.5 <= sliceY[y + 1]) { cY = y; } for (var x = chunkX.from; x <= chunkX.to; x++) { var dax = ~~((sliceX[x + 1] - a.x) * 10000) / 10000; var dbx = ~~((sliceX[x + 1] - b.x) * 10000) / 10000; // slicer is crossed, emit point if (dax * dbx < 0) { var alpha = dax / (dax - dbx); var c = new Vector3D(a.x + alpha * (b.x - a.x), a.y + alpha * (b.y - a.y), 0, 0); // push new segs; stack.push(c, b, a, c); // drop process breakAll = true; break; } if ((a.x + b.x) * 0.5 > sliceX[x] && (a.x + b.x) * 0.5 <= sliceX[x + 1]) { cX = x; } } if (breakAll) break; } if (!breakAll) { if (!posByChunks[cX + cY * 3]) { posByChunks[cX + cY * 3] = []; } posByChunks[cX + cY * 3].push(a.x, a.y, 0, b.x, b.y, 0); count += 2; } } var posBuff = new Float32Array(count * 3); var thinBuff = new Float32Array(count); // fill by same thinkness thinBuff.fill(target.thickness.get(1)[0]); var nextIndices = 0; for (var i = 0; i < 9; i++) { if (!posByChunks[i]) { continue; } posBuff.set(posByChunks[i], nextIndices * 3); nextIndices += posByChunks[i].length / 3; indices[i] = nextIndices; } target.initialScale9Positions = posBuff; target.setPositions(posBuff); target.setThickness(thinBuff); target.invalidate(); return target; }; LineElementsUtils.restoreLineSegments = function (elem) { var positionAttributes = elem.positions; var indexAttributes = elem.indices; var count = elem._numElements || elem._numVertices; var posDim = positionAttributes.dimensions; var posStride = positionAttributes.stride; var indices; var positions; var len; if (indexAttributes) { len = count * indexAttributes.dimensions; indices = indexAttributes.get(count, 0); positions = positionAttributes.get(positionAttributes.count); } else { len = count; positions = positionAttributes.get(count, 0); } var out = []; for (var i = 0; i < len; i += 6) { var index = indices ? indices[i] * posStride : i * posStride; out.push(new Vector3D(positions[index], positions[index + 1], posDim === 6 ? positions[index + 2] : 0, 0)); index += (posDim == 6) ? 3 : 2; out.push(new Vector3D(positions[index], positions[index + 1], posDim === 6 ? positions[index + 2] : 0, 0)); } return out; }; LineElementsUtils.updateScale9 = function (elem, originalRect, scaleX, scaleY, init, copy) { // todo: for now this only works for Float2Attributes. if (init === void 0) { init = false; } if (copy === void 0) { copy = false; } if (elem.scale9Indices.length !== 9) { throw 'ElementUtils: Error - triangleElement does not provide valid slice9Indices!'; } var offsets = elem.scale9Grid; var left = offsets.x - originalRect.x; var right = originalRect.right - offsets.right; var top = offsets.y - originalRect.y; var bottom = originalRect.bottom - offsets.bottom; var s_len = elem.scale9Indices.length; var innerWidth = originalRect.width * scaleX - (left + right); var innerHeight = originalRect.height * scaleY - (top + bottom); var cornerScaleX = 1; var cornerScaleY = 1; // reduce a overflow, when scale to small if (innerWidth < 0) { innerWidth = 0; cornerScaleX = originalRect.width * scaleX / (left + right); } if (innerHeight < 0) { innerHeight = 0; cornerScaleY = originalRect.height * scaleY / (top + bottom); } var innerScaleX = innerWidth / offsets.width; var innerScaleY = innerHeight / offsets.height; var stride = elem.positions.stride; var attrOffset = elem.positions.offset; var dim = elem.positions.dimensions; var positions = elem.positions.get(elem._numVertices); // todo: i had trouble when just cloning the positions // for now i just create the initialSlice9Positions by iterating the positions var initPos = elem.initialScale9Positions; var slice9Indices = elem.scale9Indices; var slice9Offsets_x = [ 0, left * cornerScaleX - left * innerScaleX, innerWidth - offsets.width * cornerScaleX, ]; var slice9Offsets_y = [ 0, top * cornerScaleY - top * innerScaleY, innerHeight - offsets.height * cornerScaleY, ]; // internal buffer iterator var attrindex = attrOffset; var vindex = 0; // iterating over the 9 chunks - keep in mind that we are constructing a 3x3 grid: for (var s = 0; s < s_len; s++) { var row = s / 3 | 0; var col = s % 3; // only need to x-scale if this is the middle column // if the innerWidth<=0 we can skip this complete column var scalex = col === 1 ? innerScaleX : cornerScaleX; // only need to y-scale if this is the middle row // if the innerHeight<=0 we can skip this complete row var scaley = row === 1 ? innerScaleY : cornerScaleY; // offsetx is different for each column var offsetx = slice9Offsets_x[col]; // offsety is different for each row var offsety = slice9Offsets_y[row]; // iterate the verts and apply the translation / scale // slice9Indices is vertices indeces, is not attribute indices while (vindex < slice9Indices[s]) { // start point var vxs = originalRect.x + (offsetx + (initPos[vindex * 3 + 0] - originalRect.x) * scalex) / scaleX; var vys = originalRect.y + (offsety + (initPos[vindex * 3 + 1] - originalRect.y) * scaley) / scaleY; // end point var vxe = originalRect.x + (offsetx + (initPos[vindex * 3 + 3] - originalRect.x) * scalex) / scaleX; var vye = originalRect.y + (offsety + (initPos[vindex * 3 + 4] - originalRect.y) * scaley) / scaleY; for (var i = 0; i < 2; i++) { var index = attrindex; // super complex, line has a lot of doubled vertices positions[index++] = vxs; positions[index++] = vys; dim === 6 && (positions[index++] = 0); positions[index++] = vxe; positions[index++] = vye; dim === 6 && (positions[index++] = 0); attrindex += stride; index = attrindex; positions[index++] = vxe; positions[index++] = vye; dim === 6 && (positions[index++] = 0); positions[index++] = vxs; positions[index++] = vys; dim === 6 && (positions[index++] = 0); attrindex += stride; } // we should include a stride, because buffer maybe be contecated // or XYZ instead of XY vindex += 2; } } elem.positions.invalidate(); elem.invalidate(); return elem; }; return LineElementsUtils; }()); export { LineElementsUtils };