UNPKG

@awayjs/renderer

Version:
708 lines (707 loc) 28.6 kB
import { Vector3D, Box, Sphere } from '@awayjs/core'; import { GeneratorUtils, MeshView } from './GeneratorUtils'; import { HitTestCache } from './HitTestCache'; var TriangleElementsUtils = /** @class */ (function () { function TriangleElementsUtils() { } //TODO - generate this dyanamically based on num tris TriangleElementsUtils.hitTest = function (x, y, _z, box, triangleElements, count, offset) { if (offset === void 0) { offset = 0; } var positionAttributes = triangleElements.positions; var curveAttributes = triangleElements.getCustomAtributes('curves'); var posStride = positionAttributes.stride; var curveStride = curveAttributes ? curveAttributes.stride : null; var positions = positionAttributes.get(count, offset); var curves = curveAttributes ? curveAttributes.get(count, offset) : null; var indices; var len; if (triangleElements.indices) { indices = triangleElements.indices.get(count, offset); positions = positionAttributes.get(positionAttributes.count); curves = curveAttributes ? curveAttributes.get(curveAttributes.count) : null; len = count * triangleElements.indices.dimensions; } else { positions = positionAttributes.get(count, offset); curves = curveAttributes ? curveAttributes.get(count, offset) : null; len = count; } var id0; var id1; var id2; var ax; var ay; var bx; var by; var cx; var cy; var hitTestCache = triangleElements.hitTestCache[offset] || (triangleElements.hitTestCache[offset] = new HitTestCache()); var index = hitTestCache.lastCollisionIndex; if (index != -1 && index < len) { precheck: { if (indices) { id0 = indices[index + 2]; id1 = indices[index + 1]; id2 = indices[index]; } else { id0 = index + 2; id1 = index + 1; id2 = index; } ax = positions[id0 * posStride]; ay = positions[id0 * posStride + 1]; bx = positions[id1 * posStride]; by = positions[id1 * posStride + 1]; cx = positions[id2 * posStride]; cy = positions[id2 * posStride + 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 dot = dx * nx + dy * ny; if (dot > 0) break precheck; dx = bx - x; dy = by - y; nx = cy - by; ny = -(cx - bx); dot = dx * nx + dy * ny; if (dot > 0) break precheck; dx = cx - x; dy = cy - y; nx = ay - cy; ny = -(ax - cx); dot = dx * nx + dy * ny; if (dot > 0) break precheck; if (curves) { //check if not solid if (curves[id0 * curveStride + 2] != -128) { var v0x = bx - ax; var v0y = by - ay; var v1x = cx - ax; var v1y = cy - ay; var v2x = x - ax; var v2y = y - ay; var den = v0x * v1y - v1x * v0y; var v = (v2x * v1y - v1x * v2y) / den; var w = (v0x * v2y - v2x * v0y) / den; //var u:number = 1 - v - w; //commented out as inlined away //here be dragons var uu = 0.5 * v + w; var vv = w; var d = uu * uu - vv; var az = curves[id0 * curveStride]; if (d > 0 && az == -128) { break precheck; } else if (d < 0 && az == 127) { 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 + 2]; id1 = indices[k + 1]; id2 = indices[k]; } else { id0 = k + 2; id1 = k + 1; id2 = k; } ax = positions[id0 * posStride]; ay = positions[id0 * posStride + 1]; bx = positions[id1 * posStride]; by = positions[id1 * posStride + 1]; cx = positions[id2 * posStride]; cy = positions[id2 * posStride + 1]; //subtractions to push into positive space var min_index_x = Math.floor((Math.min(ax, bx, cx) - minx) * conversionX); var min_index_y = Math.floor((Math.min(ay, by, cy) - miny) * conversionY); var max_index_x = Math.floor((Math.max(ax, bx, cx) - minx) * conversionX); var max_index_y = Math.floor((Math.max(ay, by, cy) - 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) { id2 = indices[k]; } else { id2 = k; } if (id2 == index) continue; if (indices) { id0 = indices[k + 2]; id1 = indices[k + 1]; } else { id0 = k + 2; id1 = k + 1; } ax = positions[id0 * posStride]; ay = positions[id0 * posStride + 1]; bx = positions[id1 * posStride]; by = positions[id1 * posStride + 1]; cx = positions[id2 * posStride]; cy = positions[id2 * posStride + 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 dot = dx * nx + dy * ny; if (dot > 0) continue; dx = bx - x; dy = by - y; nx = cy - by; ny = -(cx - bx); dot = dx * nx + dy * ny; if (dot > 0) continue; dx = cx - x; dy = cy - y; nx = ay - cy; ny = -(ax - cx); dot = dx * nx + dy * ny; if (dot > 0) continue; if (curves) { //check if not solid if (curves[id0 * curveStride + 2] != -128) { var v0x = bx - ax; var v0y = by - ay; var v1x = cx - ax; var v1y = cy - ay; var v2x = x - ax; var v2y = y - ay; var den = v0x * v1y - v1x * v0y; var v = (v2x * v1y - v1x * v2y) / den; var w = (v0x * v2y - v2x * v0y) / den; //var u:number = 1 - v - w; //commented out as inlined away //here be dragons var uu = 0.5 * v + w; var vv = w; var d = uu * uu - vv; var az = curves[id0 * curveStride]; if (d > 0 && az == -128) { continue; } else if (d < 0 && az == 127) { continue; } } } hitTestCache.lastCollisionIndex = k; return true; } hitTestCache.lastCollisionIndex = -1; return false; } //brute force for (var k = 0; k < len; k += 3) { if (indices) { id2 = indices[k]; } else { id2 = k; } if (id2 == index) continue; if (indices) { id0 = indices[k + 2]; id1 = indices[k + 1]; } else { id0 = k + 2; id1 = k + 1; } ax = positions[id0 * posStride]; ay = positions[id0 * posStride + 1]; bx = positions[id1 * posStride]; by = positions[id1 * posStride + 1]; cx = positions[id2 * posStride]; cy = positions[id2 * posStride + 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 dot = dx * nx + dy * ny; if (dot > 0) continue; dx = bx - x; dy = by - y; nx = cy - by; ny = -(cx - bx); dot = dx * nx + dy * ny; if (dot > 0) continue; dx = cx - x; dy = cy - y; nx = ay - cy; ny = -(ax - cx); dot = dx * nx + dy * ny; if (dot > 0) continue; if (curves) { //check if not solid if (curves[id0 * curveStride + 2] != -128) { var v0x = bx - ax; var v0y = by - ay; var v1x = cx - ax; var v1y = cy - ay; var v2x = x - ax; var v2y = y - ay; var den = v0x * v1y - v1x * v0y; var v = (v2x * v1y - v1x * v2y) / den; var w = (v0x * v2y - v2x * v0y) / den; //var u:number = 1 - v - w; //commented out as inlined away //here be dragons var uu = 0.5 * v + w; var vv = w; var d = uu * uu - vv; var az = curves[id0 * curveStride]; if (d > 0 && az == -128) { continue; } else if (d < 0 && az == 127) { continue; } } } hitTestCache.lastCollisionIndex = k; return true; } hitTestCache.lastCollisionIndex = -1; return false; }; TriangleElementsUtils.getBoxBounds = function (positionAttributes, indexAttributes, matrix3D, 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 i = 0; var index; var pos1, pos2, pos3, rawData; if (matrix3D) rawData = matrix3D._rawData; if (target == null) { target = cache || new Box(); index = indices ? indices[i] * posStride : i * posStride; if (matrix3D) { if (posDim == 3) { 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 == 3 ? positions[index + 2] : 0; } maxX = minX = pos1; maxY = minY = pos2; maxZ = minZ = posDim == 3 ? pos3 : 0; i++; } else { maxX = (minX = target.x) + target.width; maxY = (minY = target.y) + target.height; maxZ = (minZ = target.z) + target.depth; } for (; i < len; i++) { index = indices ? indices[i] * posStride : i * posStride; if (matrix3D) { if (posDim == 3) { 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 == 3 ? positions[index + 2] : 0; } if (pos1 < minX) minX = pos1; else if (pos1 > maxX) maxX = pos1; if (pos2 < minY) minY = pos2; else if (pos2 > maxY) maxY = pos2; if (posDim == 3) { if (pos3 < minZ) minZ = pos3; else if (pos3 > maxZ) maxZ = pos3; } } target.width = maxX - (target.x = minX); target.height = maxY - (target.y = minY); target.depth = maxZ - (target.z = minZ); return target; }; TriangleElementsUtils.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 == 3 ? 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; }; TriangleElementsUtils.prepareScale9 = function (elem, bounds, grid, copy, emitUV, uvMatrix) { if (elem._numElements !== 0) { throw 'Indices not support yet'; } var target = copy ? elem.clone() : elem; var shapeBounds = TriangleElementsUtils.getBoxBounds(elem.positions, elem.indices, null, null, null, elem._numElements | elem._numVertices, 0); var sliceX = [ -Infinity, grid.x, grid.right, Infinity ]; var sliceY = [ -Infinity, grid.y, grid.bottom, Infinity ]; 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; } } var indices = Array.from({ length: 9 }, function (_) { return 0; }); target.scale9Indices = indices; target.scale9Grid = grid; target.originalScale9Bounds = bounds; if (emitUV) { if (target.positions.dimensions !== 2) { throw 'Emit UV support only 2D position buffer'; } var uv = target.positions.get(target._numVertices, 0).slice(0); var raw = uvMatrix.rawData; for (var i = 0; i < uv.length; i += 2) { var x = uv[i + 0]; var y = uv[i + 1]; uv[i + 0] = x * raw[0] + y * raw[2] + raw[4]; uv[i + 1] = x * raw[1] + y * raw[3] + raw[5]; } target.setUVs(uv); target.invalidate(); } // shape already in valid region // not require run slicer for this case if (chunkX.from === chunkX.to && chunkY.from === chunkY.to) { target.scale9Indices[chunkY.from * 3 + chunkX.from] = target._numVertices; return target; } // run splitter var attrs = target.uvs ? [target.positions, target.uvs] : [target.positions]; var mesh = MeshView.fromAttributes(attrs, target._numVertices, 3); var vector = new Vector3D(0, 0); vector.setTo(1, 0, 0, 0); // we slice only over 2 offsets for (var i = chunkX.from; i < chunkX.to; i++) { //mesh = GeneratorUtils.SliceHodgman(mesh, vector, sliceX[i]); mesh = GeneratorUtils.SliceAllNaive(mesh, vector, sliceX[i + 1]); } vector.setTo(0, 1, 0, 0); // we slice only over 2 offsets for (var i = chunkY.from; i < chunkY.to; i++) { // generate errors // mesh = GeneratorUtils.SliceHodgman(mesh, vector, sliceY[i]); // Naive is more stable, but unoptimal mesh = GeneratorUtils.SliceAllNaive(mesh, vector, sliceY[i + 1]); } mesh.normalise(); // ordering - determine chunk id for (var _i = 0, _a = mesh.poly; _i < _a.length; _i++) { var p = _a[_i]; for (var i = 0; i < 9; i++) { // over middle point // because all polygons should be inside rect, all their points should too, // we can use any points to chek chunk // but use middle point to redice slicing errors // BUT this is not garanteed stable results var data = p.middle.getData(0); var px = data[0]; var py = data[1]; if ((px >= sliceX[i % 3]) && (px < sliceX[i % 3 + 1]) && (py >= sliceY[i / 3 | 0]) && (py < sliceY[(i / 3 | 0) + 1])) { // slicer can not generate user data p.userData = i; } } } mesh.poly.sort(function (a, b) { return a.userData - b.userData; }); for (var i = 0; i < mesh.poly.length; i++) { var index = mesh.poly[i].userData; indices[index] = i * 3 + 3; } // pos var pos = mesh.toFloatArray(0); target.setPositions(pos); if (target.uvs) { var uvs = mesh.toFloatArray(1); target.setUVs(uvs); } //throw '[TriangleElementUtils] Not implemented'; target.invalidate(); return target; }; TriangleElementsUtils.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 newElem; var positions; if (copy) { // there are not garaties that a buffer is 2 and not has stride // and a element may has UV // should working newElem = elem.clone(); newElem.scale9Grid = elem.scale9Grid; newElem.initialScale9Positions = elem.initialScale9Positions; newElem.scale9Indices = elem.scale9Indices; positions = newElem.positions.get(newElem._numVertices); } else { 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; if (init || !elem.initialScale9Positions) { initPos = []; // we store only XY, but buffer can be XYZ initPos.length = elem._numVertices * 2; var vindex_1 = 0; var len = elem.positions.length; for (var i = 0; i < len; i += stride) { initPos[vindex_1 + 0] = positions[attrOffset + i + 0]; initPos[vindex_1 + 1] = positions[attrOffset + i + 1]; vindex_1 += 2; } elem.initialScale9Positions = initPos; } else { 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]) { var vx = initPos[vindex * 2 + 0] - originalRect.x; var vy = initPos[vindex * 2 + 1] - originalRect.y; vx = offsetx + vx * scalex; vy = offsety + vy * scaley; vx /= scaleX; vy /= scaleY; positions[attrindex + 0] = vx + originalRect.x; positions[attrindex + 1] = vy + originalRect.y; // we should include a stride, because buffer maybe be contecated // or XYZ instead of XY attrindex += stride; vindex++; } } //console.log("positions",positions); if (copy) { newElem.positions.invalidate(); newElem.invalidate(); return newElem; } elem.positions.invalidate(); elem.invalidate(); return elem; }; return TriangleElementsUtils; }()); export { TriangleElementsUtils };