(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "../libs/OimoPhysics/index.js"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("../libs/OimoPhysics/index.js")); } else { var mod = { exports: {} }; factory(mod.exports, global.index); global.OimoPhysics = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, OIMO) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.OimoPhysics = OimoPhysics; OIMO = _interopRequireWildcard(OIMO); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function OimoPhysics() { return _OimoPhysics.apply(this, arguments); } function _OimoPhysics() { _OimoPhysics = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { var frameRate, world, getShape, meshes, meshMap, addMesh, handleMesh, handleInstancedMesh, setMeshPosition, lastTime, step; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: step = function _step() { var time = performance.now(); if (lastTime > 0) { // console.time( 'world.step' ); world.step(1 / frameRate); // console.timeEnd( 'world.step' ); } lastTime = time; // for (var i = 0, l = meshes.length; i < l; i++) { var mesh = meshes[i]; if (mesh.isInstancedMesh) { var array = mesh.instanceMatrix.array; var bodies = meshMap.get(mesh); for (var j = 0; j < bodies.length; j++) { var body = bodies[j]; compose(body.getPosition(), body.getOrientation(), array, j * 16); } mesh.instanceMatrix.needsUpdate = true; } else if (mesh.isMesh) { var _body2 = meshMap.get(mesh); mesh.position.copy(_body2.getPosition()); mesh.quaternion.copy(_body2.getOrientation()); } } }; setMeshPosition = function _setMeshPosition(mesh, position) { var index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; if (mesh.isInstancedMesh) { var bodies = meshMap.get(mesh); var body = bodies[index]; body.setPosition(new OIMO.Vec3(position.x, position.y, position.z)); } else if (mesh.isMesh) { var _body = meshMap.get(mesh); _body.setPosition(new OIMO.Vec3(position.x, position.y, position.z)); } }; handleInstancedMesh = function _handleInstancedMesh(mesh, mass, shape) { var array = mesh.instanceMatrix.array; var bodies = []; for (var i = 0; i < mesh.count; i++) { var index = i * 16; var shapeConfig = new OIMO.ShapeConfig(); shapeConfig.geometry = shape; var bodyConfig = new OIMO.RigidBodyConfig(); bodyConfig.type = mass === 0 ? OIMO.RigidBodyType.STATIC : OIMO.RigidBodyType.DYNAMIC; bodyConfig.position = new OIMO.Vec3(array[index + 12], array[index + 13], array[index + 14]); var body = new OIMO.RigidBody(bodyConfig); body.addShape(new OIMO.Shape(shapeConfig)); world.addRigidBody(body); bodies.push(body); } if (mass > 0) { meshes.push(mesh); meshMap.set(mesh, bodies); } }; handleMesh = function _handleMesh(mesh, mass, shape) { var shapeConfig = new OIMO.ShapeConfig(); shapeConfig.geometry = shape; var bodyConfig = new OIMO.RigidBodyConfig(); bodyConfig.type = mass === 0 ? OIMO.RigidBodyType.STATIC : OIMO.RigidBodyType.DYNAMIC; bodyConfig.position = new OIMO.Vec3(mesh.position.x, mesh.position.y, mesh.position.z); var body = new OIMO.RigidBody(bodyConfig); body.addShape(new OIMO.Shape(shapeConfig)); world.addRigidBody(body); if (mass > 0) { meshes.push(mesh); meshMap.set(mesh, body); } }; addMesh = function _addMesh(mesh) { var mass = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var shape = getShape(mesh.geometry); if (shape !== null) { if (mesh.isInstancedMesh) { handleInstancedMesh(mesh, mass, shape); } else if (mesh.isMesh) { handleMesh(mesh, mass, shape); } } }; getShape = function _getShape(geometry) { var parameters = geometry.parameters; // TODO change type to is* if (geometry.type === 'BoxGeometry') { var sx = parameters.width !== undefined ? parameters.width / 2 : 0.5; var sy = parameters.height !== undefined ? parameters.height / 2 : 0.5; var sz = parameters.depth !== undefined ? parameters.depth / 2 : 0.5; return new OIMO.OBoxGeometry(new OIMO.Vec3(sx, sy, sz)); } else if (geometry.type === 'SphereGeometry' || geometry.type === 'IcosahedronGeometry') { var radius = parameters.radius !== undefined ? parameters.radius : 1; return new OIMO.OSphereGeometry(radius); } return null; }; frameRate = 60; world = new OIMO.World(2, new OIMO.Vec3(0, -9.8, 0)); // meshes = []; meshMap = new WeakMap(); // lastTime = 0; // animate setInterval(step, 1000 / frameRate); return _context.abrupt("return", { addMesh: addMesh, setMeshPosition: setMeshPosition // addCompoundMesh }); case 13: case "end": return _context.stop(); } } }, _callee); })); return _OimoPhysics.apply(this, arguments); } function compose(position, quaternion, array, index) { var x = quaternion.x, y = quaternion.y, z = quaternion.z, w = quaternion.w; var x2 = x + x, y2 = y + y, z2 = z + z; var xx = x * x2, xy = x * y2, xz = x * z2; var yy = y * y2, yz = y * z2, zz = z * z2; var wx = w * x2, wy = w * y2, wz = w * z2; array[index + 0] = 1 - (yy + zz); array[index + 1] = xy + wz; array[index + 2] = xz - wy; array[index + 3] = 0; array[index + 4] = xy - wz; array[index + 5] = 1 - (xx + zz); array[index + 6] = yz + wx; array[index + 7] = 0; array[index + 8] = xz + wy; array[index + 9] = yz - wx; array[index + 10] = 1 - (xx + yy); array[index + 11] = 0; array[index + 12] = position.x; array[index + 13] = position.y; array[index + 14] = position.z; array[index + 15] = 1; } });