From e46e708029b0d5aecf2403b78ed1fc463955f5ee Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 12 Apr 2024 21:38:45 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20MRjs=20-=20Auto=20Generated=20Di?= =?UTF-8?q?st=20=F0=9F=91=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes at 374564ff0d2b2217fc5efa6b8ec4479739a69ac6 --- dist/mr.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/mr.js b/dist/mr.js index c3648caa..fef9f2f3 100644 --- a/dist/mr.js +++ b/dist/mr.js @@ -619,7 +619,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpac /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { "use strict"; -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ PhysicsSystem: () => (/* binding */ PhysicsSystem)\n/* harmony export */ });\n/* harmony import */ var three__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! three */ \"./node_modules/three/build/three.module.js\");\n/* harmony import */ var mrjs_core_MRSystem__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! mrjs/core/MRSystem */ \"./src/core/MRSystem.js\");\n/* harmony import */ var mrjs_core_MREntity__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! mrjs/core/MREntity */ \"./src/core/MREntity.js\");\n/* harmony import */ var mrjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! mrjs */ \"./src/index.js\");\n/* harmony import */ var mrjs_core_entities_MRDivEntity__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! mrjs/core/entities/MRDivEntity */ \"./src/core/entities/MRDivEntity.js\");\n/* harmony import */ var mrjs_core_entities_MRPanelEntity__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! mrjs/core/entities/MRPanelEntity */ \"./src/core/entities/MRPanelEntity.js\");\n/* harmony import */ var mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! mrjs/core/entities/MRModelEntity */ \"./src/core/entities/MRModelEntity.js\");\n\n\n\n\n\n\n\n\n\n\n/**\n * @class PhysicsSystem\n * @classdesc The physics system functions differently from other systems,\n * Rather than attaching components, physical properties such as\n * shape, body, mass, etc are definied as attributes.\n * if shape and body are not defined, they default to the geometry\n * of the entity, if there is no geometry, there is no physics defined\n * on the entity.\n *\n * Alternatively, you can also expressly attach a comp-physics\n * attribute for more detailed control.\n * @augments MRSystem\n */\nclass PhysicsSystem extends mrjs_core_MRSystem__WEBPACK_IMPORTED_MODULE_0__.MRSystem {\n /**\n * @class\n * @description PhysicsSystem's default constructor - sets up useful world and debug information alongside an initial `Rapier` event queue.\n */\n constructor() {\n super(false);\n this.debug = this.app.debug;\n\n // Temp objects to not have to create and destroy the system\n // items in init and update functions constantly.\n this.tempWorldPosition = new three__WEBPACK_IMPORTED_MODULE_6__.Vector3();\n this.tempWorldQuaternion = new three__WEBPACK_IMPORTED_MODULE_6__.Quaternion();\n this.tempWorldScale = new three__WEBPACK_IMPORTED_MODULE_6__.Vector3();\n this.tempBBox = new three__WEBPACK_IMPORTED_MODULE_6__.Box3();\n this.tempSize = new three__WEBPACK_IMPORTED_MODULE_6__.Vector3();\n this.tempCenter = new three__WEBPACK_IMPORTED_MODULE_6__.Vector3();\n\n //for oriented BBox\n this.savedQuaternion = new three__WEBPACK_IMPORTED_MODULE_6__.Quaternion();\n\n if (this.debug && this.debug == 'true') {\n const material = new three__WEBPACK_IMPORTED_MODULE_6__.LineBasicMaterial({\n color: 0xffffff,\n vertexColors: true,\n });\n const geometry = new three__WEBPACK_IMPORTED_MODULE_6__.BufferGeometry();\n this.lines = new three__WEBPACK_IMPORTED_MODULE_6__.LineSegments(geometry, material);\n this.app.scene.add(this.lines);\n }\n }\n\n /**\n * @function\n * @description The per global scene event update call. Based on the captured physics events for the frame, handles all items appropriately.\n */\n eventUpdate = () => {\n for (const entity of this.registry) {\n if (entity.physics?.body == null) {\n continue;\n }\n if (entity instanceof mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__.MRModelEntity) {\n this.updateSimpleBody(entity);\n } else if (entity instanceof mrjs_core_entities_MRDivEntity__WEBPACK_IMPORTED_MODULE_3__.MRDivEntity) {\n this.updateUIBody(entity);\n }\n }\n };\n\n // TODO: polish and move this into MRSystem\n /**\n * @function\n * @description a function called when a specific entity has an event update\n * @param {Event} e - the event generated by the entity\n */\n entityEventUpdate = (e) => {\n if (!e.target.physics.body) {\n return;\n }\n if (e.target instanceof mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__.MRModelEntity) {\n this.updateSimpleBody(e.target);\n } else if (e.target instanceof mrjs_core_entities_MRDivEntity__WEBPACK_IMPORTED_MODULE_3__.MRDivEntity) {\n this.updateUIBody(e.target);\n }\n };\n\n /**\n * @function\n * @description The per-frame system update call. Based on the captured physics events for the frame, handles all items appropriately.\n * @param {number} deltaTime - given timestep to be used for any feature changes\n * @param {object} frame - given frame information to be used for any feature changes\n */\n update(deltaTime, frame) {\n // per-frame time step\n mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.step(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.eventQueue);\n\n for (const entity of this.registry) {\n if (entity.physics?.body == null) {\n continue;\n }\n this.updateBody(entity);\n }\n\n this.updateDebugRenderer();\n }\n\n /**\n * @function\n * @description When a new entity is created, adds it to the physics registry and initializes the physics aspects of the entity.\n * @param {MREntity} entity - the entity being set up\n */\n onNewEntity(entity) {\n if (entity.physics.type == 'none') {\n return;\n }\n\n if (entity instanceof mrjs_core_entities_MRDivEntity__WEBPACK_IMPORTED_MODULE_3__.MRDivEntity) {\n this.initPhysicsBody(entity);\n this.registry.add(entity);\n entity.addEventListener('modelchange', (e) => {\n this.entityEventUpdate(e);\n });\n\n entity.addEventListener('entityupdated', (e) => {\n this.entityEventUpdate(e);\n });\n }\n }\n\n /**\n * @function\n * @description when an entity is removed, remove and destroy it's physics body\n * @param {MREntity} entity - the removed entity\n */\n entityRemoved(entity) {\n mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.removeRigidBody(entity.physics.body);\n entity.physics.body = null;\n }\n\n /**\n * @function\n * @description Initializes the rigid body used by the physics part of the entity\n * @param {MREntity} entity - the entity being updated\n */\n initPhysicsBody(entity) {\n const rigidBodyDesc = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.RigidBodyDesc.fixed();\n entity.physics.body = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.createRigidBody(rigidBodyDesc);\n entity.object3D.getWorldPosition(this.tempWorldPosition);\n entity.object3D.getWorldQuaternion(this.tempWorldQuaternion);\n entity.physics.body.setTranslation(...this.tempWorldPosition, true);\n entity.physics.body.setRotation(this.tempWorldQuaternion, true);\n // TODO: we should find a way to consolidate these 2, UI and Model are created in slightly different ways\n // and model will get more complex as we add convexMesh support\n if (entity instanceof mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__.MRModelEntity) {\n this.initSimpleBody(entity);\n } else if (entity instanceof mrjs_core_entities_MRDivEntity__WEBPACK_IMPORTED_MODULE_3__.MRDivEntity) {\n this.initUIEntityBody(entity);\n }\n }\n\n /**\n * @function\n * @description Initializes the rigid body used by the physics for non-nr-model div entities\n * @param {MREntity} entity - the entity being updated\n */\n initUIEntityBody(entity) {\n entity.physics.halfExtents = new three__WEBPACK_IMPORTED_MODULE_6__.Vector3();\n\n this.tempBBox.setFromCenterAndSize(entity.object3D.position, new three__WEBPACK_IMPORTED_MODULE_6__.Vector3(entity.width, entity.height, 0.002));\n this.tempWorldScale.setFromMatrixScale(entity.object3D.matrixWorld);\n this.tempBBox.getSize(this.tempSize);\n this.tempSize.multiply(this.tempWorldScale);\n\n entity.physics.halfExtents.copy(this.tempSize);\n entity.physics.halfExtents.divideScalar(2);\n\n const rigidBodyDesc = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.RigidBodyDesc.fixed();\n entity.physics.body = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.createRigidBody(rigidBodyDesc);\n\n let colliderDesc = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ColliderDesc.cuboid(...entity.physics.halfExtents);\n colliderDesc.setCollisionGroups(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.CollisionGroups.UI);\n entity.physics.collider = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.createCollider(colliderDesc, entity.physics.body);\n mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.COLLIDER_ENTITY_MAP[entity.physics.collider.handle] = entity;\n entity.physics.collider.setActiveCollisionTypes(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveCollisionTypes.DEFAULT | mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveCollisionTypes.KINEMATIC_FIXED);\n entity.physics.collider.setActiveEvents(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveEvents.COLLISION_EVENTS);\n }\n\n /**\n * @function\n * @description Initializes a simple bounding box collider based on the visual bounds of the entity\n * @param {MREntity} entity - the entity being updated\n */\n initSimpleBody(entity) {\n entity.physics.halfExtents = new three__WEBPACK_IMPORTED_MODULE_6__.Vector3();\n\n if (entity instanceof mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__.MRModelEntity) {\n this.savedQuaternion.copy(entity.object3D.quaternion);\n entity.object3D.quaternion.set(0, 0, 0, 1);\n entity.object3D.updateMatrixWorld(true);\n\n entity.object3D.remove(entity.background);\n\n this.tempBBox.setFromObject(entity.object3D, true);\n\n entity.object3D.add(entity.background);\n entity.object3D.quaternion.copy(this.savedQuaternion);\n } else {\n this.tempBBox.setFromObject(entity.object3D, true);\n }\n\n this.tempBBox.getSize(this.tempSize);\n\n entity.physics.halfExtents.copy(this.tempSize);\n entity.physics.halfExtents.divideScalar(2);\n\n let colliderDesc = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ColliderDesc.cuboid(...entity.physics.halfExtents);\n colliderDesc.setCollisionGroups(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.CollisionGroups.UI);\n\n this.tempBBox.getCenter(this.tempCenter);\n entity.object3D.getWorldPosition(this.tempWorldPosition);\n this.tempCenter.subVectors(this.tempCenter, this.tempWorldPosition);\n\n colliderDesc.setTranslation(...this.tempCenter);\n\n entity.physics.collider = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.createCollider(colliderDesc, entity.physics.body);\n\n entity.physics.collider.setActiveCollisionTypes(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveCollisionTypes.DEFAULT | mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveCollisionTypes.KINEMATIC_FIXED);\n entity.physics.collider.setActiveEvents(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveEvents.COLLISION_EVENTS);\n\n mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.COLLIDER_ENTITY_MAP[entity.physics.collider.handle] = entity;\n }\n\n /**\n * @function\n * @description Initializes a Rigid Body detailed convexMesh collider for the entity\n * NOTE: not currently in use until we can sync it with animations\n * @param {MREntity} entity - the entity being updated\n */\n initDetailedBody(entity) {\n const rigidBodyDesc = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.RigidBodyDesc.fixed();\n entity.physics.body = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.createRigidBody(rigidBodyDesc);\n\n entity.physics.colliders = [];\n\n entity.object3D.traverse((child) => {\n if (child.isMesh) {\n let collider = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.createCollider(this.initConvexMeshCollider(child, entity.compStyle.scale), entity.physics.body);\n collider.setCollisionGroups(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.CollisionGroups.UI);\n entity.physics.colliders.push(collider);\n mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.COLLIDER_ENTITY_MAP[collider.handle] = entity;\n collider.setActiveCollisionTypes(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveCollisionTypes.DEFAULT | mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveCollisionTypes.KINEMATIC_FIXED);\n collider.setActiveEvents(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveEvents.COLLISION_EVENTS);\n }\n });\n }\n\n // /**\n // * @function\n // * @param object3D\n // * @param scale\n // * @description Initializes a convexMesh collider from a THREE.js geometry\n // * NOTE: not currently in use until we can sync it with animations\n // * NOTE: commenting for now until we make sure this works like the other init functions.\n // * @param {object} entity - the entity being updated\n // */\n // initConvexMeshCollider(object3D, scale) {\n // // const positionAttribute = object3D.geometry.getAttribute('position');\n // // const vertices = [];\n // // for (let i = 0; i < positionAttribute.count; i++) {\n // // const vertex = new THREE.Vector3().fromBufferAttribute(positionAttribute, i).multiplyScalar(scale);\n // // vertices.push(vertex.toArray());\n // // }\n\n // // // Convert vertices to a flat Float32Array as required by RAPIER.ConvexHull\n // // const verticesFlat = new Float32Array(vertices.flat());\n\n // // return mrjsUtils.physics.RAPIER.ColliderDesc.convexMesh(verticesFlat);\n // }\n\n /**\n * @function\n * @description Updates the rigid body used by the physics part of the entity\n * @param {MREntity} entity - the entity being updated\n */\n updateBody(entity) {\n if (!entity.physics.body) {\n return;\n }\n\n if (entity.compStyle.visibility == 'hidden' && entity.physics.body.isEnabled()) {\n entity.physics.body.setEnabled(false);\n } else if (!entity.physics.body.isEnabled() && entity.compStyle.visibility == 'visible') {\n entity.physics.body.setEnabled(true);\n // TODO: we should find a way to consolidate these 2, UI and Model are created in slightly different ways\n // and model will get more complex as we add convexMesh support\n if (entity instanceof mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__.MRModelEntity) {\n this.updateSimpleBody(entity);\n } else if (entity instanceof mrjs_core_entities_MRDivEntity__WEBPACK_IMPORTED_MODULE_3__.MRDivEntity) {\n this.updateUIBody(entity);\n }\n }\n\n if (entity instanceof mrjs_core_entities_MRPanelEntity__WEBPACK_IMPORTED_MODULE_4__.MRPanelEntity) {\n entity.panel.getWorldPosition(this.tempWorldPosition);\n } else {\n entity.object3D.getWorldPosition(this.tempWorldPosition);\n }\n entity.physics.body.setTranslation({ ...this.tempWorldPosition }, true);\n\n entity.object3D.getWorldQuaternion(this.tempWorldQuaternion);\n entity.physics.body.setRotation(this.tempWorldQuaternion, true);\n }\n\n /**\n * @function\n * @description Updates the rigid body used by the physics part of the div entity\n * @param {MREntity} entity - the entity being updated\n */\n updateUIBody(entity) {\n this.tempBBox.setFromCenterAndSize(entity.object3D.position, new three__WEBPACK_IMPORTED_MODULE_6__.Vector3(entity.width, entity.height, 0.002));\n\n this.tempWorldScale.setFromMatrixScale(entity instanceof mrjs_core_entities_MRPanelEntity__WEBPACK_IMPORTED_MODULE_4__.MRPanelEntity ? entity.panel.matrixWorld : entity.object3D.matrixWorld);\n this.tempBBox.getSize(this.tempSize);\n this.tempSize.multiply(this.tempWorldScale);\n\n entity.physics.halfExtents.copy(this.tempSize);\n entity.physics.halfExtents.divideScalar(2);\n\n entity.physics.collider.setHalfExtents(entity.physics.halfExtents);\n }\n\n /**\n * @function\n * @description Updates the rigid body used by the physics part of the model entity\n * @param {MREntity} entity - the entity being updated\n */\n updateSimpleBody(entity) {\n if (entity instanceof mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__.MRModelEntity) {\n this.savedQuaternion.copy(entity.object3D.quaternion);\n entity.object3D.quaternion.set(0, 0, 0, 1);\n entity.object3D.updateMatrixWorld(true);\n\n entity.object3D.remove(entity.background);\n\n this.tempBBox.setFromObject(entity.object3D, true);\n\n entity.object3D.add(entity.background);\n entity.object3D.quaternion.copy(this.savedQuaternion);\n } else {\n this.tempBBox.setFromObject(entity.object3D, true);\n }\n\n this.tempBBox.getSize(this.tempSize);\n\n entity.physics.halfExtents.copy(this.tempSize);\n entity.physics.halfExtents.divideScalar(2);\n entity.physics.collider.setHalfExtents(entity.physics.halfExtents);\n\n this.tempBBox.getCenter(this.tempCenter);\n entity.object3D.getWorldPosition(this.tempWorldPosition);\n this.tempCenter.subVectors(this.tempCenter, this.tempWorldPosition);\n entity.physics.collider.setTranslationWrtParent({ ...this.tempCenter });\n }\n\n /**\n * @function\n * @description Updates the debug renderer to either be on or off based on the 'this.debug' variable. Handles the drawing of the visual lines.\n */\n updateDebugRenderer() {\n if (!this.debug || this.debug == 'false') {\n return;\n }\n const buffers = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.debugRender();\n this.lines.geometry.setAttribute('position', new three__WEBPACK_IMPORTED_MODULE_6__.BufferAttribute(buffers.vertices, 3));\n this.lines.geometry.setAttribute('color', new three__WEBPACK_IMPORTED_MODULE_6__.BufferAttribute(buffers.colors, 4));\n }\n}\n\n\n//# sourceURL=webpack://mrjs/./src/core/componentSystems/PhysicsSystem.js?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ PhysicsSystem: () => (/* binding */ PhysicsSystem)\n/* harmony export */ });\n/* harmony import */ var three__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! three */ \"./node_modules/three/build/three.module.js\");\n/* harmony import */ var mrjs_core_MRSystem__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! mrjs/core/MRSystem */ \"./src/core/MRSystem.js\");\n/* harmony import */ var mrjs_core_MREntity__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! mrjs/core/MREntity */ \"./src/core/MREntity.js\");\n/* harmony import */ var mrjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! mrjs */ \"./src/index.js\");\n/* harmony import */ var mrjs_core_entities_MRDivEntity__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! mrjs/core/entities/MRDivEntity */ \"./src/core/entities/MRDivEntity.js\");\n/* harmony import */ var mrjs_core_entities_MRPanelEntity__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! mrjs/core/entities/MRPanelEntity */ \"./src/core/entities/MRPanelEntity.js\");\n/* harmony import */ var mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! mrjs/core/entities/MRModelEntity */ \"./src/core/entities/MRModelEntity.js\");\n\n\n\n\n\n\n\n\n\n\n/**\n * @class PhysicsSystem\n * @classdesc The physics system functions differently from other systems,\n * Rather than attaching components, physical properties such as\n * shape, body, mass, etc are definied as attributes.\n * if shape and body are not defined, they default to the geometry\n * of the entity, if there is no geometry, there is no physics defined\n * on the entity.\n *\n * Alternatively, you can also expressly attach a comp-physics\n * attribute for more detailed control.\n * @augments MRSystem\n */\nclass PhysicsSystem extends mrjs_core_MRSystem__WEBPACK_IMPORTED_MODULE_0__.MRSystem {\n /**\n * @class\n * @description PhysicsSystem's default constructor - sets up useful world and debug information alongside an initial `Rapier` event queue.\n */\n constructor() {\n super(false);\n this.debug = this.app.debug;\n\n // Temp objects to not have to create and destroy the system\n // items in init and update functions constantly.\n this.tempWorldPosition = new three__WEBPACK_IMPORTED_MODULE_6__.Vector3();\n this.tempWorldQuaternion = new three__WEBPACK_IMPORTED_MODULE_6__.Quaternion();\n this.tempWorldScale = new three__WEBPACK_IMPORTED_MODULE_6__.Vector3();\n this.tempBBox = new three__WEBPACK_IMPORTED_MODULE_6__.Box3();\n this.tempSize = new three__WEBPACK_IMPORTED_MODULE_6__.Vector3();\n this.tempCenter = new three__WEBPACK_IMPORTED_MODULE_6__.Vector3();\n\n //for oriented BBox\n this.savedQuaternion = new three__WEBPACK_IMPORTED_MODULE_6__.Quaternion();\n\n if (this.debug && this.debug == 'true') {\n const material = new three__WEBPACK_IMPORTED_MODULE_6__.LineBasicMaterial({\n color: 0xffffff,\n vertexColors: true,\n });\n const geometry = new three__WEBPACK_IMPORTED_MODULE_6__.BufferGeometry();\n this.lines = new three__WEBPACK_IMPORTED_MODULE_6__.LineSegments(geometry, material);\n this.app.scene.add(this.lines);\n }\n }\n\n /**\n * @function\n * @description The per global scene event update call. Based on the captured physics events for the frame, handles all items appropriately.\n */\n eventUpdate = () => {\n for (const entity of this.registry) {\n if (entity.physics?.body == null) {\n continue;\n }\n if (entity instanceof mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__.MRModelEntity) {\n this.updateSimpleBody(entity);\n } else if (entity instanceof mrjs_core_entities_MRDivEntity__WEBPACK_IMPORTED_MODULE_3__.MRDivEntity) {\n this.updateUIBody(entity);\n }\n }\n };\n\n // TODO: polish and move this into MRSystem\n /**\n * @function\n * @description a function called when a specific entity has an event update\n * @param {Event} e - the event generated by the entity\n */\n entityEventUpdate = (e) => {\n if (!e.target.physics.body) {\n return;\n }\n if (e.target instanceof mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__.MRModelEntity) {\n this.updateSimpleBody(e.target);\n } else if (e.target instanceof mrjs_core_entities_MRDivEntity__WEBPACK_IMPORTED_MODULE_3__.MRDivEntity) {\n this.updateUIBody(e.target);\n }\n };\n\n /**\n * @function\n * @description The per-frame system update call. Based on the captured physics events for the frame, handles all items appropriately.\n * @param {number} deltaTime - given timestep to be used for any feature changes\n * @param {object} frame - given frame information to be used for any feature changes\n */\n update(deltaTime, frame) {\n // per-frame time step\n mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.step(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.eventQueue);\n\n for (const entity of this.registry) {\n if (entity.physics?.body == null) {\n continue;\n }\n this.updateBody(entity);\n }\n\n this.updateDebugRenderer();\n }\n\n /**\n * @function\n * @description When a new entity is created, adds it to the physics registry and initializes the physics aspects of the entity.\n * @param {MREntity} entity - the entity being set up\n */\n onNewEntity(entity) {\n if (entity.physics.type == 'none') {\n return;\n }\n\n if (entity instanceof mrjs_core_entities_MRDivEntity__WEBPACK_IMPORTED_MODULE_3__.MRDivEntity) {\n this.initPhysicsBody(entity);\n this.registry.add(entity);\n entity.addEventListener('modelchange', (e) => {\n this.entityEventUpdate(e);\n });\n\n entity.addEventListener('entityupdated', (e) => {\n this.entityEventUpdate(e);\n });\n }\n }\n\n /**\n * @function\n * @description when an entity is removed, remove and destroy it's physics body\n * @param {MREntity} entity - the removed entity\n */\n entityRemoved(entity) {\n mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.removeRigidBody(entity.physics.body);\n entity.physics.body = null;\n }\n\n /**\n * @function\n * @description Initializes the rigid body used by the physics part of the entity\n * @param {MREntity} entity - the entity being updated\n */\n initPhysicsBody(entity) {\n const rigidBodyDesc = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.RigidBodyDesc.fixed();\n entity.physics.body = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.createRigidBody(rigidBodyDesc);\n entity.object3D.getWorldPosition(this.tempWorldPosition);\n entity.object3D.getWorldQuaternion(this.tempWorldQuaternion);\n entity.physics.body.setTranslation(...this.tempWorldPosition, true);\n entity.physics.body.setRotation(this.tempWorldQuaternion, true);\n // TODO: we should find a way to consolidate these 2, UI and Model are created in slightly different ways\n // and model will get more complex as we add convexMesh support\n if (entity instanceof mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__.MRModelEntity) {\n this.initSimpleBody(entity);\n } else if (entity instanceof mrjs_core_entities_MRDivEntity__WEBPACK_IMPORTED_MODULE_3__.MRDivEntity) {\n this.initUIEntityBody(entity);\n }\n }\n\n /**\n * @function\n * @description Initializes the rigid body used by the physics for non-nr-model div entities\n * @param {MREntity} entity - the entity being updated\n */\n initUIEntityBody(entity) {\n entity.physics.halfExtents = new three__WEBPACK_IMPORTED_MODULE_6__.Vector3();\n\n this.tempBBox.setFromCenterAndSize(entity.object3D.position, new three__WEBPACK_IMPORTED_MODULE_6__.Vector3(entity.width, entity.height, 0.002));\n this.tempWorldScale.setFromMatrixScale(entity.object3D.matrixWorld);\n this.tempBBox.getSize(this.tempSize);\n this.tempSize.multiply(this.tempWorldScale);\n\n entity.physics.halfExtents.copy(this.tempSize);\n entity.physics.halfExtents.divideScalar(2);\n\n const rigidBodyDesc = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.RigidBodyDesc.fixed();\n entity.physics.body = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.createRigidBody(rigidBodyDesc);\n\n let colliderDesc = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ColliderDesc.cuboid(...entity.physics.halfExtents);\n colliderDesc.setCollisionGroups(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.CollisionGroups.UI);\n entity.physics.collider = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.createCollider(colliderDesc, entity.physics.body);\n mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.COLLIDER_ENTITY_MAP[entity.physics.collider.handle] = entity;\n entity.physics.collider.setActiveCollisionTypes(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveCollisionTypes.DEFAULT | mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveCollisionTypes.KINEMATIC_FIXED);\n entity.physics.collider.setActiveEvents(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveEvents.COLLISION_EVENTS);\n }\n\n /**\n * @function\n * @description Initializes a simple bounding box collider based on the visual bounds of the entity\n * @param {MREntity} entity - the entity being updated\n */\n initSimpleBody(entity) {\n entity.physics.halfExtents = new three__WEBPACK_IMPORTED_MODULE_6__.Vector3();\n\n if (entity instanceof mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__.MRModelEntity) {\n this.savedQuaternion.copy(entity.object3D.quaternion);\n entity.object3D.quaternion.set(0, 0, 0, 1);\n entity.object3D.updateMatrixWorld(true);\n\n entity.object3D.remove(entity.background);\n\n this.tempBBox.setFromObject(entity.object3D, true);\n\n entity.object3D.add(entity.background);\n entity.object3D.quaternion.copy(this.savedQuaternion);\n } else {\n this.tempBBox.setFromObject(entity.object3D, true);\n }\n\n this.tempBBox.getSize(this.tempSize);\n\n entity.physics.halfExtents.copy(this.tempSize);\n entity.physics.halfExtents.divideScalar(2);\n\n let colliderDesc = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ColliderDesc.cuboid(...entity.physics.halfExtents);\n colliderDesc.setCollisionGroups(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.CollisionGroups.UI);\n\n this.tempBBox.getCenter(this.tempCenter);\n entity.object3D.getWorldPosition(this.tempWorldPosition);\n this.tempCenter.subVectors(this.tempCenter, this.tempWorldPosition);\n\n colliderDesc.setTranslation(...this.tempCenter);\n\n entity.physics.collider = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.createCollider(colliderDesc, entity.physics.body);\n\n entity.physics.collider.setActiveCollisionTypes(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveCollisionTypes.DEFAULT | mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveCollisionTypes.KINEMATIC_FIXED);\n entity.physics.collider.setActiveEvents(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveEvents.COLLISION_EVENTS);\n\n mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.COLLIDER_ENTITY_MAP[entity.physics.collider.handle] = entity;\n }\n\n /**\n * @function\n * @description Initializes a Rigid Body detailed convexMesh collider for the entity\n * NOTE: not currently in use until we can sync it with animations\n * @param {MREntity} entity - the entity being updated\n */\n initDetailedBody(entity) {\n const rigidBodyDesc = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.RigidBodyDesc.fixed();\n entity.physics.body = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.createRigidBody(rigidBodyDesc);\n\n entity.physics.colliders = [];\n\n entity.object3D.traverse((child) => {\n if (child.isMesh) {\n let collider = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.createCollider(this.initConvexMeshCollider(child, entity.compStyle.scale), entity.physics.body);\n collider.setCollisionGroups(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.CollisionGroups.UI);\n entity.physics.colliders.push(collider);\n mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.COLLIDER_ENTITY_MAP[collider.handle] = entity;\n collider.setActiveCollisionTypes(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveCollisionTypes.DEFAULT | mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveCollisionTypes.KINEMATIC_FIXED);\n collider.setActiveEvents(mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.RAPIER.ActiveEvents.COLLISION_EVENTS);\n }\n });\n }\n\n // /**\n // * @function\n // * @param object3D\n // * @param scale\n // * @description Initializes a convexMesh collider from a THREE.js geometry\n // * NOTE: not currently in use until we can sync it with animations\n // * NOTE: commenting for now until we make sure this works like the other init functions.\n // * @param {object} entity - the entity being updated\n // */\n // initConvexMeshCollider(object3D, scale) {\n // // const positionAttribute = object3D.geometry.getAttribute('position');\n // // const vertices = [];\n // // for (let i = 0; i < positionAttribute.count; i++) {\n // // const vertex = new THREE.Vector3().fromBufferAttribute(positionAttribute, i).multiplyScalar(scale);\n // // vertices.push(vertex.toArray());\n // // }\n\n // // // Convert vertices to a flat Float32Array as required by RAPIER.ConvexHull\n // // const verticesFlat = new Float32Array(vertices.flat());\n\n // // return mrjsUtils.physics.RAPIER.ColliderDesc.convexMesh(verticesFlat);\n // }\n\n /**\n * @function\n * @description Updates the rigid body used by the physics part of the entity\n * @param {MREntity} entity - the entity being updated\n */\n updateBody(entity) {\n if (!entity.physics.body) {\n return;\n }\n\n if ((entity.compStyle.visibility == 'hidden' || entity.compStyle.pointerEvents == 'none') && entity.physics.body.isEnabled()) {\n entity.physics.body.setEnabled(false);\n } else if (!entity.physics.body.isEnabled() && entity.compStyle.visibility == 'visible' && entity.compStyle.pointerEvents != 'none') {\n entity.physics.body.setEnabled(true);\n // TODO: we should find a way to consolidate these 2, UI and Model are created in slightly different ways\n // and model will get more complex as we add convexMesh support\n if (entity instanceof mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__.MRModelEntity) {\n this.updateSimpleBody(entity);\n } else if (entity instanceof mrjs_core_entities_MRDivEntity__WEBPACK_IMPORTED_MODULE_3__.MRDivEntity) {\n this.updateUIBody(entity);\n }\n }\n\n if (entity instanceof mrjs_core_entities_MRPanelEntity__WEBPACK_IMPORTED_MODULE_4__.MRPanelEntity) {\n entity.panel.getWorldPosition(this.tempWorldPosition);\n } else {\n entity.object3D.getWorldPosition(this.tempWorldPosition);\n }\n entity.physics.body.setTranslation({ ...this.tempWorldPosition }, true);\n\n entity.object3D.getWorldQuaternion(this.tempWorldQuaternion);\n entity.physics.body.setRotation(this.tempWorldQuaternion, true);\n }\n\n /**\n * @function\n * @description Updates the rigid body used by the physics part of the div entity\n * @param {MREntity} entity - the entity being updated\n */\n updateUIBody(entity) {\n this.tempBBox.setFromCenterAndSize(entity.object3D.position, new three__WEBPACK_IMPORTED_MODULE_6__.Vector3(entity.width, entity.height, 0.002));\n\n this.tempWorldScale.setFromMatrixScale(entity instanceof mrjs_core_entities_MRPanelEntity__WEBPACK_IMPORTED_MODULE_4__.MRPanelEntity ? entity.panel.matrixWorld : entity.object3D.matrixWorld);\n this.tempBBox.getSize(this.tempSize);\n this.tempSize.multiply(this.tempWorldScale);\n\n entity.physics.halfExtents.copy(this.tempSize);\n entity.physics.halfExtents.divideScalar(2);\n\n entity.physics.collider.setHalfExtents(entity.physics.halfExtents);\n }\n\n /**\n * @function\n * @description Updates the rigid body used by the physics part of the model entity\n * @param {MREntity} entity - the entity being updated\n */\n updateSimpleBody(entity) {\n if (entity instanceof mrjs_core_entities_MRModelEntity__WEBPACK_IMPORTED_MODULE_5__.MRModelEntity) {\n this.savedQuaternion.copy(entity.object3D.quaternion);\n entity.object3D.quaternion.set(0, 0, 0, 1);\n entity.object3D.updateMatrixWorld(true);\n\n entity.object3D.remove(entity.background);\n\n this.tempBBox.setFromObject(entity.object3D, true);\n\n entity.object3D.add(entity.background);\n entity.object3D.quaternion.copy(this.savedQuaternion);\n } else {\n this.tempBBox.setFromObject(entity.object3D, true);\n }\n\n this.tempBBox.getSize(this.tempSize);\n\n entity.physics.halfExtents.copy(this.tempSize);\n entity.physics.halfExtents.divideScalar(2);\n entity.physics.collider.setHalfExtents(entity.physics.halfExtents);\n\n this.tempBBox.getCenter(this.tempCenter);\n entity.object3D.getWorldPosition(this.tempWorldPosition);\n this.tempCenter.subVectors(this.tempCenter, this.tempWorldPosition);\n entity.physics.collider.setTranslationWrtParent({ ...this.tempCenter });\n }\n\n /**\n * @function\n * @description Updates the debug renderer to either be on or off based on the 'this.debug' variable. Handles the drawing of the visual lines.\n */\n updateDebugRenderer() {\n if (!this.debug || this.debug == 'false') {\n return;\n }\n const buffers = mrjs__WEBPACK_IMPORTED_MODULE_2__.mrjsUtils.physics.world.debugRender();\n this.lines.geometry.setAttribute('position', new three__WEBPACK_IMPORTED_MODULE_6__.BufferAttribute(buffers.vertices, 3));\n this.lines.geometry.setAttribute('color', new three__WEBPACK_IMPORTED_MODULE_6__.BufferAttribute(buffers.colors, 4));\n }\n}\n\n\n//# sourceURL=webpack://mrjs/./src/core/componentSystems/PhysicsSystem.js?"); /***/ }),