physar
Version:
Physar enables Cannon.js physics in your Spark AR projects.
1 lines • 8.68 kB
JavaScript
!function(t){var r={};function e(o){if(r[o])return r[o].exports;var i=r[o]={i:o,l:!1,exports:{}};return t[o].call(i.exports,i,i.exports,e),i.l=!0,i.exports}e.m=t,e.c=r,e.d=function(t,r,o){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:o})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(e.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var i in t)e.d(o,i,function(r){return t[r]}.bind(null,i));return o},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=4)}([function(t,r){t.exports=require("Time")},function(t,r){t.exports=require("cannon")},function(t,r){t.exports=require("Diagnostics")},function(t,r){t.exports=require("Scene")},function(t,r,e){"use strict";e.r(r);var o={PADCHAR:"=",ALPHA:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",getbyte64:function(t,r){var e=o.ALPHA.indexOf(t.charAt(r));if(-1===e)throw o.makeDOMException();return e},decode:function(t){t=""+t;var r,e,i,s=o.getbyte64,n=t.length;if(0===n)return t;if(n%4!=0)throw Error();r=0,t.charAt(n-1)===o.PADCHAR&&(r=1,t.charAt(n-2)===o.PADCHAR&&(r=2),n-=4);var a=[];for(e=0;e<n;e+=4)i=s(t,e)<<18|s(t,e+1)<<12|s(t,e+2)<<6|s(t,e+3),a.push(String.fromCharCode(i>>16,i>>8&255,255&i));switch(r){case 1:i=s(t,e)<<18|s(t,e+1)<<12|s(t,e+2)<<6,a.push(String.fromCharCode(i>>16,i>>8&255));break;case 2:i=s(t,e)<<18|s(t,e+1)<<12,a.push(String.fromCharCode(i>>16))}return a.join("")},getbyte:function(t,r){var e=t.charCodeAt(r);if(e>255)throw o.makeDOMException();return e},encode:function(t){if(1!==arguments.length)throw new SyntaxError("Not enough arguments");var r,e,i=o.PADCHAR,s=o.ALPHA,n=o.getbyte,a=[],c=(t=""+t).length-t.length%3;if(0===t.length)return t;for(r=0;r<c;r+=3)e=n(t,r)<<16|n(t,r+1)<<8|n(t,r+2),a.push(s.charAt(e>>18)),a.push(s.charAt(e>>12&63)),a.push(s.charAt(e>>6&63)),a.push(s.charAt(63&e));switch(t.length-c){case 1:e=n(t,r)<<16,a.push(s.charAt(e>>18)+s.charAt(e>>12&63)+i+i);break;case 2:e=n(t,r)<<16|n(t,r+1)<<8,a.push(s.charAt(e>>18)+s.charAt(e>>12&63)+s.charAt(e>>6&63)+i)}return a.join("")}},i=o;var s=e(3),n=e(0),a=s.root.find("SphereObject"),c=s.root.find("Cube01"),d=s.root.find("plane0"),h=new class{constructor(t){this.Diagnostics=void 0,this.Time=void 0,this.currentInterval=void 0,this.lastInterval=void 0,this.lastTime=void 0,this.fixedTimeStep=1/60,this.maxSubSteps=3,this.timeInterval=5,this.C=void 0,this.worldObjects=[],this.worldConstraints=[],this.defaults={body:{mass:0,radius:1,transform:{rotation:{x:0,y:0,z:0,w:.5},position:{x:0,y:0,z:0},scale:{x:1,y:1,z:1}}},sync:{properties:["position","rotation"],axes:["x","y","z"],rotation:{enabled:!0,allAxis:!0,x:!0,y:!0,z:!0},position:{enabled:!0,allAxis:!0,x:!0,y:!0,z:!0}}},this.initRequiredSparkModules(),this.createCWorld(t)}initRequiredSparkModules(){this.Time=e(0),this.C=e(1),this.Diagnostics=e(2)}createCWorld(t){this.world=new this.C.World,this.world.broadphase=new this.C.NaiveBroadphase,this.world.gravity.set(t.x,t.y,t.z)}beginSync(){this.initialWorldState=this.worldObjects,this.currentInterval=this.Time.setInterval(t=>{if(void 0!==this.lastTime){let r=(t-this.lastTime)/1e3;this.world.step(this.fixedTimeStep,r,this.maxSubSteps),this.objectSync()}this.lastTime=t},this.timeInterval)}objectSync(){this.worldObjects.forEach(t=>{if(t.spark){const r=t.sync,e=r.properties,o=r.axes,i={};t.body.quaternion.toEuler(i),e.forEach(r=>{t.sync[r].enabled&&o.forEach(e=>{if(t.sync[r].allAxis||t.sync[r][e]){const o=`rotation${e.toUpperCase()}`;"position"==r&&(t.spark.transform[e]=t.body[r][e]),"rotation"==r&&(t.spark.transform[o]=i[e])}})})}})}stopSync(){this.currentInterval&&(this.Time.clearInterval(this.currentInterval),this.lastInterval=currentInterval,this.currentInterval=void 0),this.resetWorldState()}getDefaults(t){return t?this.defaults[t]:this.defaults}fillWithDefaults(t){const r=this.getDefaults();return Object.keys(r).forEach(e=>{t.hasOwnProperty(e)||(t[e]=r[e]),Object.keys(r[e]).forEach(o=>{Array.isArray(t[e][o])?r[e][o].forEach(i=>{t[e][o].indexOf(i)<0&&t[e][o].push(r[e][o][i])}):(t[e].hasOwnProperty(o)||(t[e][o]=r[e][o]),"object"!=typeof r[e][o]||Array.isArray(r[e][o])||Object.keys(r[e][o]).forEach(i=>{t[e][o].hasOwnProperty(i)||(t[e][o][i]=r[e][o][i])}))})}),t}resetWorldState(){this.initialWorldState&&this.worldObjects.forEach((t,r)=>{const e=this.initialWorldState[r],o=e.body,i=t.body;if(i.velocity.setZero(),i.initVelocity.setZero(),i.angularVelocity.setZero(),i.initAngularVelocity.setZero(),i.force.setZero(),i.torque.setZero(),i.quaternion=o.quaternion,i.position=o.position,t.spark){const r=t.sync,o=r.properties,i=r.axes,s={};t.body.quaternion.toEuler(s),o.forEach(r=>{t.sync[r].enabled&&i.forEach(o=>{if(t.sync[r].allAxis||t.sync[r][o]){const i=`rotation${o.toUpperCase()}`;"position"==r&&(t.spark.transform[o]=e.spark.transform[o]),"rotation"==r&&(t.spark.transform[i]=e.spark.transform[i])}})})}})}createObject(t,r,e){const{sync:o,body:i}=this.fillWithDefaults(e),{mass:s,radius:n,transform:a}=i,c=a.rotation,d=a.scale,h=a.position,l=new this.C.Vec3(h.x,h.y,h.z),u=new this.C.Quaternion(c.x,c.y,c.z,c.w),f=t=>{switch(t){case"box":return new this.C.Box(new this.C.Vec3(d.x,d.y,d.z));case"sphere":return new this.C.Sphere(n);case"ground":return new this.C.Plane}},y=t=>{const e={mass:s,shape:t,position:l,quaternion:u,angularDamping:.8};return"sphere"==r&&(e.radius=n),"ground"==r&&(e.mass=0),e},p=(t=>{const r=new this.C.Body(y(f(t)));if("ground"==t){const t=-Math.PI/2,e=new this.C.Vec3(1,0,0);r.quaternion.setFromAxisAngle(e,t)}return r})(r);return this.addObjectToPhysicsWorld(p,t,o,"ground"==r)}find(t,r){if("object"==t)for(var e=0;e<this.worldObjects.length;e++)if(this.worldObjects[e].id==r)return this.worldObjects[e];if("constraint"==t)for(e=0;e<this.worldConstraints.length;e++)if(this.worldConstraints[e].id==r)return this.worldConstraints[e]}createConstraint(t,r){if(!r.bodyA||!r.bodyB)return;this.log(r);let{bodyA:e,bodyB:o}=r;const i=r;Object.keys(i).forEach((t,r)=>{const{x:e,y:o,z:s}=i[t];i[t]=this.getVec3(i[t])});let{pivotA:s,pivotB:n,axisA:a,axisB:c}=i;const d=this.find("object",e),h=this.find("object",o);if(!d||!h)return void this.log("Bodies where not found.");let l;switch(t){case"point":l=new this.C.PointToPointConstraint(d.body,s,h.body,n);break;case"hinge":l=new this.C.HingeConstraint(d.body,h.body,i);break;case"conetwist":l=new this.C.ConeTwistConstraint(d.body,h.body,i);break;case"lock":l=new this.C.LockConstraint(d.body,h.body,{});break;default:return}return this.addConstraintToWorld(l,d.id,h.id)}addConstraintToWorld(t,r,e){if(!r||!e)return;this.world.addConstraint(t),this.log("Added constraint to world.");const o=i.encode(`${r}.${e}`),s={id:o,constraint:t,isActive:!0};return this.worldConstraints.push(s),this.log(`Constraint ${o} added to world.`),o}removeConstraint(t,r=!1){const e=this.find("constraint",t);if(e&&this.world.removeConstraint(e.constraint),r){const t=this.worldConstraints.indexOf(e);this.worldConstraints.splice(t,1)}}disableConstraint(t){const r=this.find("constraint",t);if(r){const t=this.worldConstraints.indexOf(r);this.worldConstraints[t].isActive=!1,this.worldConstraints[t].constraint.disable()}}enableConstraint(t){const r=this.find("constraint",t);r.constraint;if(r&&0==r.isActive){const t=this.worldConstraints.indexOf(r);this.worldConstraints[t].isActive=!0,this.worldConstraints[t].enable()}}log(t){this.Diagnostics.log(t)}addObjectToPhysicsWorld(t,r,e,o){this.world.addBody(t);const i={...this.getDefaults("sync"),...e},s=((t,r)=>{for(r=t="";t++<36;r+=51*t&52?(15^t?8^Math.random()*(20^t?16:4):4).toString(16):"-");return r})(),n={id:s,body:t,spark:r,sync:i,isGround:o};return this.worldObjects.push(n),s}createCMaterial(t){let r=void 0;return null==t&&(r=new this.C.Material),r}getVec3(t){const{x:r,y:e,z:o}=t;return new this.C.Vec3(r,e,o)}start(){this.beginSync()}}({x:0,y:-9.82,z:0}),l={body:{mass:1,radius:.01,transform:{position:{x:0,y:2,z:0}}}};h.createObject(d,"ground",{body:{mass:0,transform:{position:{x:0,y:0,z:0}}}});var u={bodyA:h.createObject(a,"sphere",l),bodyB:h.createObject(c,"box",{body:{mass:5,transform:{scale:{x:.01,y:.01,z:.01},position:{x:0,y:10,z:0},rotation:{x:.2,y:.8,z:.3,w:0}}}}),pivotA:l.body.transform.position},f=h.createConstraint("lock",u);n.setTimeout((function(){h.disableConstraint(f)}),5e3),h.start()}]);