UNPKG

@smoud/tiny

Version:

Fast and tiny JavaScript library for HTML5 game and playable ads creation.

1 lines 15.3 kB
!function(){"use strict";for(var t=[],i=0;i<256;i++)t[i]=(i<16?'0':'')+i.toString(16);var s={DEG2RAD:Math.PI/180,RAD2DEG:180/Math.PI,generateUUID:function(){var i=4294967295*Math.random()|0,s=4294967295*Math.random()|0,h=4294967295*Math.random()|0,n=4294967295*Math.random()|0;return(t[255&i]+t[i>>8&255]+t[i>>16&255]+t[i>>24&255]+'-'+t[255&s]+t[s>>8&255]+'-'+t[s>>16&15|64]+t[s>>24&255]+'-'+t[63&h|128]+t[h>>8&255]+'-'+t[h>>16&255]+t[h>>24&255]+t[255&n]+t[n>>8&255]+t[n>>16&255]+t[n>>24&255]).toUpperCase()},clamp:function(t,i,s){return Math.max(i,Math.min(s,t))},euclideanModulo:function(t,i){return(t%i+i)%i},mapLinear:function(t,i,s,h,n){return h+(t-i)*(n-h)/(s-i)},lerp:function(t,i,s){return(1-s)*t+s*i},smoothstep:function(t,i,s){return t<=i?0:t>=s?1:(t=(t-i)/(s-i))*t*(3-2*t)},smootherstep:function(t,i,s){return t<=i?0:t>=s?1:(t=(t-i)/(s-i))*t*t*(t*(6*t-15)+10)},randInt:function(t,i){return t+Math.floor(Math.random()*(i-t+1))},randFloat:function(t,i){return t+Math.random()*(i-t)},randFloatSpread:function(t){return t*(.5-Math.random())},degToRad:function(t){return t*s.DEG2RAD},radToDeg:function(t){return t*s.RAD2DEG},isPowerOfTwo:function(t){return 0==(t&t-1)&&0!==t},ceilPowerOfTwo:function(t){return Math.pow(2,Math.ceil(Math.log(t)/Math.LN2))},floorPowerOfTwo:function(t){return Math.pow(2,Math.floor(Math.log(t)/Math.LN2))}};function h(t,i,s,h){this._x=t||0,this._y=i||0,this._z=s||0,this._w=void 0!==h?h:1}Object.assign(h,{slerp:function(t,i,s,h){return s.copy(t).slerp(i,h)},slerpFlat:function(t,i,s,h,n,r,e){var a=s[h+0],o=s[h+1],u=s[h+2],c=s[h+3],y=n[r+0],l=n[r+1],x=n[r+2],_=n[r+3];if(c!==_||a!==y||o!==l||u!==x){var f=1-e,z=a*y+o*l+u*x+c*_,m=z>=0?1:-1,M=1-z*z;if(M>Number.EPSILON){var p=Math.sqrt(M),d=Math.atan2(p,z*m);f=Math.sin(f*d)/p,e=Math.sin(e*d)/p}var w=e*m;if(a=a*f+y*w,o=o*f+l*w,u=u*f+x*w,c=c*f+_*w,f===1-e){var g=1/Math.sqrt(a*a+o*o+u*u+c*c);a*=g,o*=g,u*=g,c*=g}}t[i]=a,t[i+1]=o,t[i+2]=u,t[i+3]=c}}),Object.defineProperties(h.prototype,{x:{get:function(){return this._x},set:function(t){this._x=t,this._onChangeCallback()}},y:{get:function(){return this._y},set:function(t){this._y=t,this._onChangeCallback()}},z:{get:function(){return this._z},set:function(t){this._z=t,this._onChangeCallback()}},w:{get:function(){return this._w},set:function(t){this._w=t,this._onChangeCallback()}}}),Object.assign(h.prototype,{isQuaternion:!0,set:function(t,i,s,h){return this._x=t,this._y=i,this._z=s,this._w=h,this._onChangeCallback(),this},clone:function(){return new this.constructor(this._x,this._y,this._z,this._w)},copy:function(t){return this._x=t.x,this._y=t.y,this._z=t.z,this._w=t.w,this._onChangeCallback(),this},setFromEuler:function(t,i){if(!t||!t.isEuler)throw new Error('Tiny.Quaternion: .setFromEuler() now expects an Euler rotation rather than a Vector3 and order.');var s=t._x,h=t._y,n=t._z,r=t.order,e=Math.cos,a=Math.sin,o=e(s/2),u=e(h/2),c=e(n/2),y=a(s/2),l=a(h/2),x=a(n/2);return'XYZ'===r?(this._x=y*u*c+o*l*x,this._y=o*l*c-y*u*x,this._z=o*u*x+y*l*c,this._w=o*u*c-y*l*x):'YXZ'===r?(this._x=y*u*c+o*l*x,this._y=o*l*c-y*u*x,this._z=o*u*x-y*l*c,this._w=o*u*c+y*l*x):'ZXY'===r?(this._x=y*u*c-o*l*x,this._y=o*l*c+y*u*x,this._z=o*u*x+y*l*c,this._w=o*u*c-y*l*x):'ZYX'===r?(this._x=y*u*c-o*l*x,this._y=o*l*c+y*u*x,this._z=o*u*x-y*l*c,this._w=o*u*c+y*l*x):'YZX'===r?(this._x=y*u*c+o*l*x,this._y=o*l*c+y*u*x,this._z=o*u*x-y*l*c,this._w=o*u*c-y*l*x):'XZY'===r&&(this._x=y*u*c-o*l*x,this._y=o*l*c-y*u*x,this._z=o*u*x+y*l*c,this._w=o*u*c+y*l*x),!1!==i&&this._onChangeCallback(),this},setFromAxisAngle:function(t,i){var s=i/2,h=Math.sin(s);return this._x=t.x*h,this._y=t.y*h,this._z=t.z*h,this._w=Math.cos(s),this._onChangeCallback(),this},setFromRotationMatrix:function(t){var i,s=t.elements,h=s[0],n=s[4],r=s[8],e=s[1],a=s[5],o=s[9],u=s[2],c=s[6],y=s[10],l=h+a+y;return l>0?(i=.5/Math.sqrt(l+1),this._w=.25/i,this._x=(c-o)*i,this._y=(r-u)*i,this._z=(e-n)*i):h>a&&h>y?(i=2*Math.sqrt(1+h-a-y),this._w=(c-o)/i,this._x=.25*i,this._y=(n+e)/i,this._z=(r+u)/i):a>y?(i=2*Math.sqrt(1+a-h-y),this._w=(r-u)/i,this._x=(n+e)/i,this._y=.25*i,this._z=(o+c)/i):(i=2*Math.sqrt(1+y-h-a),this._w=(e-n)/i,this._x=(r+u)/i,this._y=(o+c)/i,this._z=.25*i),this._onChangeCallback(),this},setFromUnitVectors:function(t,i){var s=t.dot(i)+1;return s<1e-6?(s=0,Math.abs(t.x)>Math.abs(t.z)?(this._x=-t.y,this._y=t.x,this._z=0,this._w=s):(this._x=0,this._y=-t.z,this._z=t.y,this._w=s)):(this._x=t.y*i.z-t.z*i.y,this._y=t.z*i.x-t.x*i.z,this._z=t.x*i.y-t.y*i.x,this._w=s),this.normalize()},angleTo:function(t){return 2*Math.acos(Math.abs(s.clamp(this.dot(t),-1,1)))},rotateTowards:function(t,i){var s=this.angleTo(t);if(0===s)return this;var h=Math.min(1,i/s);return this.slerp(t,h),this},inverse:function(){return this.conjugate()},conjugate:function(){return this._x*=-1,this._y*=-1,this._z*=-1,this._onChangeCallback(),this},dot:function(t){return this._x*t._x+this._y*t._y+this._z*t._z+this._w*t._w},lengthSq:function(){return this._x*this._x+this._y*this._y+this._z*this._z+this._w*this._w},length:function(){return Math.sqrt(this._x*this._x+this._y*this._y+this._z*this._z+this._w*this._w)},normalize:function(){var t=this.length();return 0===t?(this._x=0,this._y=0,this._z=0,this._w=1):(t=1/t,this._x=this._x*t,this._y=this._y*t,this._z=this._z*t,this._w=this._w*t),this._onChangeCallback(),this},multiply:function(t,i){return void 0!==i?this.multiplyQuaternions(t,i):this.multiplyQuaternions(this,t)},premultiply:function(t){return this.multiplyQuaternions(t,this)},multiplyQuaternions:function(t,i){var s=t._x,h=t._y,n=t._z,r=t._w,e=i._x,a=i._y,o=i._z,u=i._w;return this._x=s*u+r*e+h*o-n*a,this._y=h*u+r*a+n*e-s*o,this._z=n*u+r*o+s*a-h*e,this._w=r*u-s*e-h*a-n*o,this._onChangeCallback(),this},slerp:function(t,i){if(0===i)return this;if(1===i)return this.copy(t);var s=this._x,h=this._y,n=this._z,r=this._w,e=r*t._w+s*t._x+h*t._y+n*t._z;if(e<0?(this._w=-t._w,this._x=-t._x,this._y=-t._y,this._z=-t._z,e=-e):this.copy(t),e>=1)return this._w=r,this._x=s,this._y=h,this._z=n,this;var a=1-e*e;if(a<=Number.EPSILON){var o=1-i;return this._w=o*r+i*this._w,this._x=o*s+i*this._x,this._y=o*h+i*this._y,this._z=o*n+i*this._z,this.normalize(),this._onChangeCallback(),this}var u=Math.sqrt(a),c=Math.atan2(u,e),y=Math.sin((1-i)*c)/u,l=Math.sin(i*c)/u;return this._w=r*y+this._w*l,this._x=s*y+this._x*l,this._y=h*y+this._y*l,this._z=n*y+this._z*l,this._onChangeCallback(),this},equals:function(t){return t._x===this._x&&t._y===this._y&&t._z===this._z&&t._w===this._w},fromArray:function(t,i){return void 0===i&&(i=0),this._x=t[i],this._y=t[i+1],this._z=t[i+2],this._w=t[i+3],this._onChangeCallback(),this},toArray:function(t,i){return void 0===t&&(t=[]),void 0===i&&(i=0),t[i]=this._x,t[i+1]=this._y,t[i+2]=this._z,t[i+3]=this._w,t},_onChange:function(t){return this._onChangeCallback=t,this},_onChangeCallback:function(){}});var n=new e,r=new h;function e(t,i,s){this.x=t||0,this.y=i||0,this.z=s||0}function a(){this.origin=new Tiny.Vec3,this.direction=new Tiny.Vec3}Object.assign(e.prototype,{isVector3:!0,set:function(t,i,s){return this.x=t,this.y=i,this.z=s,this},setScalar:function(t){return this.x=t,this.y=t,this.z=t,this},setX:function(t){return this.x=t,this},setY:function(t){return this.y=t,this},setZ:function(t){return this.z=t,this},setComponent:function(t,i){switch(t){case 0:this.x=i;break;case 1:this.y=i;break;case 2:this.z=i;break;default:throw new Error('index is out of range: '+t)}return this},getComponent:function(t){switch(t){case 0:return this.x;case 1:return this.y;case 2:return this.z;default:throw new Error('index is out of range: '+t)}},clone:function(){return new this.constructor(this.x,this.y,this.z)},copy:function(t){return this.x=t.x,this.y=t.y,this.z=t.z,this},add:function(t,i){return void 0!==i?this.addVectors(t,i):(this.x+=t.x,this.y+=t.y,this.z+=t.z,this)},addScalar:function(t){return this.x+=t,this.y+=t,this.z+=t,this},addVectors:function(t,i){return this.x=t.x+i.x,this.y=t.y+i.y,this.z=t.z+i.z,this},addScaledVector:function(t,i){return this.x+=t.x*i,this.y+=t.y*i,this.z+=t.z*i,this},sub:function(t,i){return void 0!==i?this.subVectors(t,i):(this.x-=t.x,this.y-=t.y,this.z-=t.z,this)},subScalar:function(t){return this.x-=t,this.y-=t,this.z-=t,this},subVectors:function(t,i){return this.x=t.x-i.x,this.y=t.y-i.y,this.z=t.z-i.z,this},multiply:function(t,i){return void 0!==i?this.multiplyVectors(t,i):(this.x*=t.x,this.y*=t.y,this.z*=t.z,this)},multiplyScalar:function(t){return this.x*=t,this.y*=t,this.z*=t,this},multiplyVectors:function(t,i){return this.x=t.x*i.x,this.y=t.y*i.y,this.z=t.z*i.z,this},applyEuler:function(t){return!t||t.isEuler,this.applyQuaternion(r.setFromEuler(t))},applyAxisAngle:function(t,i){return this.applyQuaternion(r.setFromAxisAngle(t,i))},applyMatrix3:function(t){var i=this.x,s=this.y,h=this.z,n=t.elements;return this.x=n[0]*i+n[3]*s+n[6]*h,this.y=n[1]*i+n[4]*s+n[7]*h,this.z=n[2]*i+n[5]*s+n[8]*h,this},applyNormalMatrix:function(t){return this.applyMatrix3(t).normalize()},applyMatrix4:function(t){var i=this.x,s=this.y,h=this.z,n=t.elements,r=1/(n[3]*i+n[7]*s+n[11]*h+n[15]);return this.x=(n[0]*i+n[4]*s+n[8]*h+n[12])*r,this.y=(n[1]*i+n[5]*s+n[9]*h+n[13])*r,this.z=(n[2]*i+n[6]*s+n[10]*h+n[14])*r,this},applyQuaternion:function(t){var i=this.x,s=this.y,h=this.z,n=t.x,r=t.y,e=t.z,a=t.w,o=a*i+r*h-e*s,u=a*s+e*i-n*h,c=a*h+n*s-r*i,y=-n*i-r*s-e*h;return this.x=o*a+y*-n+u*-e-c*-r,this.y=u*a+y*-r+c*-n-o*-e,this.z=c*a+y*-e+o*-r-u*-n,this},project:function(t){return this.applyMatrix4(t.matrixWorldInverse).applyMatrix4(t.projectionMatrix)},unproject:function(t){return this.applyMatrix4(t.projectionMatrixInverse).applyMatrix4(t.matrixWorld)},transformDirection:function(t){var i=this.x,s=this.y,h=this.z,n=t.elements;return this.x=n[0]*i+n[4]*s+n[8]*h,this.y=n[1]*i+n[5]*s+n[9]*h,this.z=n[2]*i+n[6]*s+n[10]*h,this.normalize()},divide:function(t){return this.x/=t.x,this.y/=t.y,this.z/=t.z,this},divideScalar:function(t){return this.multiplyScalar(1/t)},min:function(t){return this.x=Math.min(this.x,t.x),this.y=Math.min(this.y,t.y),this.z=Math.min(this.z,t.z),this},max:function(t){return this.x=Math.max(this.x,t.x),this.y=Math.max(this.y,t.y),this.z=Math.max(this.z,t.z),this},clamp:function(t,i){return this.x=Math.max(t.x,Math.min(i.x,this.x)),this.y=Math.max(t.y,Math.min(i.y,this.y)),this.z=Math.max(t.z,Math.min(i.z,this.z)),this},clampScalar:function(t,i){return this.x=Math.max(t,Math.min(i,this.x)),this.y=Math.max(t,Math.min(i,this.y)),this.z=Math.max(t,Math.min(i,this.z)),this},clampLength:function(t,i){var s=this.length();return this.divideScalar(s||1).multiplyScalar(Math.max(t,Math.min(i,s)))},floor:function(){return this.x=Math.floor(this.x),this.y=Math.floor(this.y),this.z=Math.floor(this.z),this},ceil:function(){return this.x=Math.ceil(this.x),this.y=Math.ceil(this.y),this.z=Math.ceil(this.z),this},round:function(){return this.x=Math.round(this.x),this.y=Math.round(this.y),this.z=Math.round(this.z),this},roundToZero:function(){return this.x=this.x<0?Math.ceil(this.x):Math.floor(this.x),this.y=this.y<0?Math.ceil(this.y):Math.floor(this.y),this.z=this.z<0?Math.ceil(this.z):Math.floor(this.z),this},negate:function(){return this.x=-this.x,this.y=-this.y,this.z=-this.z,this},dot:function(t){return this.x*t.x+this.y*t.y+this.z*t.z},lengthSq:function(){return this.x*this.x+this.y*this.y+this.z*this.z},length:function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)},manhattanLength:function(){return Math.abs(this.x)+Math.abs(this.y)+Math.abs(this.z)},normalize:function(){return this.divideScalar(this.length()||1)},setLength:function(t){return this.normalize().multiplyScalar(t)},lerp:function(t,i){return this.x+=(t.x-this.x)*i,this.y+=(t.y-this.y)*i,this.z+=(t.z-this.z)*i,this},lerpVectors:function(t,i,s){return this.subVectors(i,t).multiplyScalar(s).add(t)},cross:function(t,i){return void 0!==i?this.crossVectors(t,i):this.crossVectors(this,t)},crossVectors:function(t,i){var s=t.x,h=t.y,n=t.z,r=i.x,e=i.y,a=i.z;return this.x=h*a-n*e,this.y=n*r-s*a,this.z=s*e-h*r,this},projectOnVector:function(t){var i=t.dot(this)/t.lengthSq();return this.copy(t).multiplyScalar(i)},projectOnPlane:function(t){return n.copy(this).projectOnVector(t),this.sub(n)},reflect:function(t){return this.sub(n.copy(t).multiplyScalar(2*this.dot(t)))},angleTo:function(t){var i=Math.sqrt(this.lengthSq()*t.lengthSq()),h=this.dot(t)/i;return Math.acos(s.clamp(h,-1,1))},distanceTo:function(t){return Math.sqrt(this.distanceToSquared(t))},distanceToSquared:function(t){var i=this.x-t.x,s=this.y-t.y,h=this.z-t.z;return i*i+s*s+h*h},manhattanDistanceTo:function(t){return Math.abs(this.x-t.x)+Math.abs(this.y-t.y)+Math.abs(this.z-t.z)},setFromSpherical:function(t){return this.setFromSphericalCoords(t.radius,t.phi,t.theta)},setFromSphericalCoords:function(t,i,s){var h=Math.sin(i)*t;return this.x=h*Math.sin(s),this.y=Math.cos(i)*t,this.z=h*Math.cos(s),this},setFromCylindrical:function(t){return this.setFromCylindricalCoords(t.radius,t.theta,t.y)},setFromCylindricalCoords:function(t,i,s){return this.x=t*Math.sin(i),this.y=s,this.z=t*Math.cos(i),this},setFromMatrixPosition:function(t){var i=t.elements;return this.x=i[12],this.y=i[13],this.z=i[14],this},setFromMatrixScale:function(t){var i=this.setFromMatrixColumn(t,0).length(),s=this.setFromMatrixColumn(t,1).length(),h=this.setFromMatrixColumn(t,2).length();return this.x=i,this.y=s,this.z=h,this},setFromMatrixColumn:function(t,i){return this.fromArray(t.elements,4*i)},equals:function(t){return t.x===this.x&&t.y===this.y&&t.z===this.z},fromArray:function(t,i){return void 0===i&&(i=0),this.x=t[i],this.y=t[i+1],this.z=t[i+2],this},toArray:function(t,i){return void 0===t&&(t=[]),void 0===i&&(i=0),t[i]=this.x,t[i+1]=this.y,t[i+2]=this.z,t},fromAttribute:function(t,i){return this.x=t.getX(i),this.y=t.getY(i),this.z=t.getZ(i),this}}),a.prototype={castMouse:function(t,i){if(t.isOrthographicCamera){var{left:s,right:h,bottom:n,top:r,zoom:e}=t,a=s/e+(h-s)/e*(.5*i.x+.5),o=n/e+(r-n)/e*(.5*i.y+.5);this.origin.set(a,o,0),this.origin.applyMatrix4(t.worldMatrix),this.direction.x=-t.worldMatrix.elements[8],this.direction.y=-t.worldMatrix.elements[9],this.direction.z=-t.worldMatrix.elements[10]}else t.worldMatrix.getTranslation(this.origin),this.direction.set(i.x,i.y,.5),t.unproject(this.direction),this.direction.sub(this.origin).normalize()},intersectBox:function(t,i,s){var h,n,r,e,a,o;i=i||this.origin;var u=1/(s=s||this.direction).x,c=1/s.y,y=1/s.z,l=t.min,x=t.max;return h=((u>=0?l.x:x.x)-i.x)*u,n=((u>=0?x.x:l.x)-i.x)*u,r=((c>=0?l.y:x.y)-i.y)*c,h>(e=((c>=0?x.y:l.y)-i.y)*c)||r>n?0:(r>h&&(h=r),e<n&&(n=e),a=((y>=0?l.z:x.z)-i.z)*y,h>(o=((y>=0?x.z:l.z)-i.z)*y)||a>n?0:(a>h&&(h=a),o<n&&(n=o),n<0?0:h>=0?h:n))}};var o={min:new e,max:new e},u=new Tiny.Vec3(1,1,1),c=new Tiny.Vec3,y={init:function(){this.raycaster=new a},preHandle:function(t,i){this.game.camera&&(c.set(t/this.game.width*2-1,-i/this.game.height*2+1,0),this.raycaster.castMouse(this.game.camera,c))}};Tiny.Input.systems.push(y),Tiny.Input.checkBounds3D=function(t){t.input.hitBox?u.copy(t.input.hitBox):t.getWorldScale(u);var i=c.copy(u).multiplyScalar(.5);if(o.min.copy(t.position).sub(i),o.max.copy(t.position).add(i),this.raycaster.intersectBox(o))return!0},Tiny.Input.prototype.add3d=function(t,i){(i=i||{}).checkBounds=Tiny.Input.checkBounds3D,this.add(t,i)}}();