r3-legacy/build/game-lib-min.js

3 lines
66 KiB
JavaScript
Raw Normal View History

2016-11-01 14:34:17 +01:00
function GameLib(){}if("undefined"==typeof GameLib.D3&&(GameLib.D3=function(){}),"undefined"==typeof Q){if("undefined"==typeof require)throw console.warn("You need the Q promise library for the GameLib.D3"),new Error("You need the Q promise library for the GameLib.D3");var Q=require("q")}GameLib.D3.BoneWeight=function(e,t){this.boneIndex=e,this.weight=t},GameLib.D3.Bone=function(e,t,i,n,s,o,a,r,h,c){this.id=e,this.name=i,this.boneId=t,"undefined"==typeof n&&(n=[]),this.childBoneIds=n,"undefined"==typeof s&&(s=null),this.parentBoneId=s,"undefined"==typeof o&&(o=new GameLib.D3.Vector4),this.quaternion=o,"undefined"==typeof a&&(a=new GameLib.D3.Vector3(0,0,0)),this.position=a,"undefined"==typeof r&&(r=new GameLib.D3.Vector3(0,0,0)),this.rotation=r,"undefined"==typeof h&&(h=new GameLib.D3.Vector3(1,1,1)),this.scale=h,"undefined"==typeof c&&(c=new GameLib.D3.Vector3(0,1,0)),this.up=c},GameLib.D3.Broadphase=function(e,t,i,n){this.id=e,"undefined"==typeof t&&(t="broadphase-"+i),this.name=t,"undefined"==typeof i&&(i=GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE),this.broadphaseType=i,this.engine=n,this.engine.isNotCannonThrow(),this.instance=this.createInstance()},GameLib.D3.Broadphase.prototype.createInstance=function(){var e=null;if(this.broadphaseType==GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE)e=new this.engine.instance.NaiveBroadphase;else if(this.broadphaseType==GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID)e=new this.engine.instance.GridBroadphase;else{if(this.broadphaseType!=GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP)throw console.warn("Unsupported broadphase type: "+this.broadphaseType),new Error("Unsupported broadphase type: "+this.broadphaseType);e=new this.engine.instance.SAPBroadphase}return e},GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE=1,GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID=2,GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP=3,GameLib.D3.Color=function(e,t,i,n){this.r=e,this.g=t,this.b=i,this.a=n},GameLib.D3.Engine=function(e,t){this.engineType=e,this.instance=t},GameLib.D3.Engine.prototype.isCannon=function(){return this.engineType==GameLib.D3.Engine.ENGINE_TYPE_CANNON},GameLib.D3.Engine.prototype.isNotCannonThrow=function(){if(this.engineType!=GameLib.D3.Engine.ENGINE_TYPE_CANNON)throw console.warn("Only CANNON supported for this function"),new Error("Only CANNON supported for this function")},GameLib.D3.Engine.prototype.isAmmo=function(){return this.engineType==GameLib.D3.Engine.ENGINE_TYPE_AMMO},GameLib.D3.Engine.prototype.isGoblin=function(){return this.engineType==GameLib.D3.Engine.ENGINE_TYPE_GOBLIN},GameLib.D3.Engine.ENGINE_TYPE_CANNON=1,GameLib.D3.Engine.ENGINE_TYPE_AMMO=2,GameLib.D3.Engine.ENGINE_TYPE_GOBLIN=3,GameLib.D3.Entity=function(e,t){this.meshId=e,"undefined"==typeof t&&(t=[]),this.componentIds=t},GameLib.D3.FlyControls=function(e,t,i){this.flySpeed=100,this.canvas=i,this.THREE=t,this.yaw=0,this.pitch=0,this.canRotate=!1,this.moveForward=!1,this.moveBackward=!1,this.moveLeft=!1,this.moveRight=!1,this.moveUp=!1,this.moveDown=!1,this.mouseUpCallback=this.onMouseUp.bind(this),this.mouseDownCallback=this.onMouseDown.bind(this),this.mouseMoveCallback=this.onMouseMove.bind(this),this.mouseWheelCallback=this.onMouseWheel.bind(this),this.keyDownCallback=this.onKeyDown.bind(this),this.keyUpCallback=this.onKeyUp.bind(this),this.camera=e,this.canvas.addEventListener("keydown",this.keyDownCallback,!1),this.canvas.addEventListener("keyup",this.keyUpCallback,!1),this.canvas.addEventListener("mousedown",this.mouseDownCallback,!1),this.canvas.addEventListener("mouseup",this.mouseUpCallback,!1),this.canvas.addEventListener("mousewheel",this.mouseWheelCallback,!1),this.havePointerLock="pointerLockElement"in document||"mozPointerLockElement"in document||"webkitPointerLockElement"in document,this.element=document.body,this.havePointerLock&&(this.element.requestPointerLock=this.element.requestPointerLock||this.element.mozRequestPointerLock||this.element.webkitRequestPointerLock,document.exitPointerLock=document.exitPointerLock||document.mozExitPointerLock||document.webkitExitPointerLock)},GameLib.D3.FlyControls.prototype.o
this.componentId=GameLib.D3.Tools.RandomId(),this.engine=e,this.engine.isNotCannonThrow(),this.chassisBody=t,"undefined"==typeof i&&(i=[]),this.wheels=i,this.instance=this.createInstance()},GameLib.D3.RigidBodyVehicle.prototype.getWheelInfo=function(){return this.instance.wheelBodies},GameLib.D3.RigidBodyVehicle.prototype.createInstance=function(){return new this.engine.instance.RigidVehicle({chassisBody:this.chassisBody.instance})},GameLib.D3.RigidBodyVehicle.prototype.addWheel=function(e){this.wheels.push(e),this.instance.addWheel({body:e.body.instance,position:new this.engine.instance.Vec3(e.position.x,e.position.y,e.position.z),axis:new this.engine.instance.Vec3(e.axis.x,e.axis.y,e.axis.z),direction:new this.engine.instance.Vec3(e.direction.x,e.direction.y,e.direction.z)})},GameLib.D3.RigidBody=function(e,t,i,n,s,o,a,r,h,c,l,p,d,m,u,f){this.componentId=GameLib.D3.Tools.RandomId(),this.position=n||new GameLib.D3.Vector3,this.velocity=o||new GameLib.D3.Vector3,this.angularVelocity=a||new GameLib.D3.Vector3,this.quaternion=s||new GameLib.D3.Vector4(0,0,0,1),this.mass="undefined"==typeof t?0:t,this.friction="undefined"==typeof i?5:i,this.linearDamping="undefined"==typeof r?.01:r,this.angularDamping="undefined"==typeof h?.01:h,this.allowSleep="undefined"==typeof c||c,this.sleepSpeedLimit="undefined"==typeof l?.1:l,this.sleepTimeLimit="undefined"==typeof p?1:p,this.collisionFilterGroup="undefined"==typeof d?1:d,this.collisionFilterMask="undefined"==typeof m?1:m,this.fixedRotation="undefined"!=typeof u&&u,this.shape="undefined"==typeof f?null:f,this.engine=e,this.engine.isNotCannonThrow(),this.instance=this.createInstance()},GameLib.D3.RigidBody.prototype.createInstance=function(){var e=new this.engine.instance.Body({mass:mass,friction:this.friction,position:new this.engine.instance.Vec3(this.position.x,this.position.y,this.position.z),velocity:new this.engine.instance.Vec3(this.velocity.x,this.velocity.y,this.velocity.z),quaternion:new this.engine.instance.Quaternion(this.quaternion.x,this.quaternion.y,this.quaternion.z,this.quaternion.w),angularVelocity:new this.engine.instance.Vec3(this.angularVelocity.x,this.angularVelocity.y,this.angularVelocity.z),linearDamping:this.linearDamping,angularDamping:this.angularDamping,allowSleep:this.allowSleep,sleepSpeedLimit:this.sleepSpeedLimit,sleepTimeLimit:this.sleepTimeLimit,collisionFilterGroup:this.collisionFilterGroup,collisionFilterMask:this.collisionFilterMask,fixedRotation:this.fixedRotation,shape:this.shape.instance});return this.instance=e,e},GameLib.D3.RigidBody.prototype.addShape=function(e,t,i){t||(t=new GameLib.D3.Vector3(0,0,0)),i||(i=new GameLib.D3.Vector4(0,0,0,1)),this.instance.addShape(e.instance,new this.engine.instance.Vec3(t.x,t.y,t.z),new this.engine.instance.Quaternion(i.x,i.y,i.z,i.w))},GameLib.D3.RigidWheel=function(e,t,i,n){this.componentId=GameLib.D3.Tools.RandomId(),this.body=e,this.position=t,this.axis=i,this.direction=n},GameLib.D3.Scene=function(e,t,i,n,s,o,a,r,h,c,l,p){this.id=e,this.sceneId=GameLib.D3.Tools.RandomId(),this.path=t,this.name=i,""==this.name.trim()&&(this.name="unnamed"),"undefined"==typeof n&&(n=[]),this.meshes=n,"undefined"==typeof s&&(s=new GameLib.D3.Vector4),this.quaternion=s,"undefined"==typeof o&&(o=new GameLib.D3.Vector3(0,0,0)),this.position=o,"undefined"==typeof a&&(a=new GameLib.D3.Vector3(0,0,0)),this.rotation=a,"undefined"==typeof r&&(r=new GameLib.D3.Vector3(1,1,1)),this.scale=r,"undefined"==typeof h&&(h=null),this.parentSceneId=h,"undefined"==typeof c&&(c=[]),this.lights=c,"undefined"==typeof l&&(l=[]),this.worlds=l,"undefined"==typeof p&&(p=[]),this.entities=p},GameLib.D3.Scene.LoadSceneFromApi=function(e,t,i,n,s,o){if("undefined"==typeof XMLHttpRequest)return console.warn("implement server side loading from API here"),t(null,new Error("not implemented"));var a=new XMLHttpRequest;a.open("GET",o+"/scene/load"+e.path+"/"+e.name),a.onreadystatechange=function(e){return function(){if(4==e.readyState){var o=JSON.parse(e.responseText);if(!o.scene||0==o.scene.length)return t(null,null,new Error("Could not load scene"));var a=o
b.addShape(L),this.addRigidBody(b);for(var g in t.children)this.generateTriangleMeshShape(e,t.children[g]);return b},GameLib.D3.World.prototype.fixupTriangleMeshShape=function(e,t,i,n,s,o,a){this.engine.isNotCannonThrow(),o.isNotThreeThrow(),null!=i&&"undefined"!=typeof i&&0!=i||(i=10),null!=n&&"undefined"!=typeof n||(n=13);var r=this.instance,h=new this.engine.instance.RaycastResult,c=[],l=[],p=0,d=0,m=0,u=[];for(var f in t){var y=t[f],v=[];p+=y.indices.length/3;for(var L=0;L<y.indices.length/3;L++){var b=y.indices[3*L],g=y.indices[3*L+1],D=y.indices[3*L+2],G=(new o.instance.Vector3).add(new o.instance.Vector3(y.vertices[3*b],y.vertices[3*b+1],y.vertices[3*b+2])).add(new o.instance.Vector3(y.vertices[3*g],y.vertices[3*g+1],y.vertices[3*g+2])).add(new o.instance.Vector3(y.vertices[3*D],y.vertices[3*D+1],y.vertices[3*D+2])).divideScalar(3),w=new this.engine.instance.Vec3;y.getNormal(L,w);var T=new this.engine.instance.Vec3(G.x+w.x,G.y+w.y,G.z+w.z),E=new this.engine.instance.Vec3(T.x-w.x*i,T.y-w.y*i,T.z-w.z*i);if(r.raycastClosest(T,E,{},h),a){var x=new o.instance.Geometry,_=new o.instance.Mesh(x,new o.instance.MeshBasicMaterial({color:16711680,wireframe:!0,opacity:1})),P=new o.instance.ArrowHelper(new o.instance.Vector3(w.x,w.y,w.z).normalize(),new o.instance.Vector3(T.x,T.y,T.z),i/2,h.hasHit?new o.instance.Color(0,1,0):new o.instance.Color(1,0,0));_.add(P),l.add(_),c.push(_)}h.hasHit||v.push({faceIndex:L,vertices:[new this.engine.instance.Vec3(y.vertices[3*b],y.vertices[3*b+1],y.vertices[3*b+2]),new this.engine.instance.Vec3(y.vertices[3*g],y.vertices[3*g+1],y.vertices[3*g+2]),new this.engine.instance.Vec3(y.vertices[3*D],y.vertices[3*D+1],y.vertices[3*D+2])],center:G,parent:y})}for(var M={},S=0;S<v.length;S++){var I=v[S];if(!I.marked){M[S]={indices:[],vertices:[]};var V=M[S].indices.length;M[S].indices.push(V,V+1,V+2),M[S].vertices.push(I.vertices[0].x,I.vertices[0].y,I.vertices[0].z),M[S].vertices.push(I.vertices[1].x,I.vertices[1].y,I.vertices[1].z),M[S].vertices.push(I.vertices[2].x,I.vertices[2].y,I.vertices[2].z);for(var R=0;R<v.length;R++)if(R!=S){var N=v[R];if(!N.marked&&I.center.distanceTo(N.center)<=n){var V=M[S].indices.length;M[S].indices.push(V,V+1,V+2),M[S].vertices.push(N.vertices[0].x,N.vertices[0].y,N.vertices[0].z),M[S].vertices.push(N.vertices[1].x,N.vertices[1].y,N.vertices[1].z),M[S].vertices.push(N.vertices[2].x,N.vertices[2].y,N.vertices[2].z),N.marked=!0}}}}for(var A in M){var z=M[A],C=new GameLib.D3.Shape(this.engine,GameLib.D3.Shape.SHAPE_TYPE_TRIMESH,null,z.vertices,z.indices);if(s)e.addShape(C);else{var Y=new GameLib.D3.RigidBody(this.engine,0,12);Y.addShape(C),this.instance.addRigidBody(Y)}u.push(C),m+=z.indices.length/3,console.log("created mesh shape",z.indices.length/3)}console.log("i = "+f,v),d+=v.length}return console.log("total faces",p),console.log("total broken faces",d),console.log("broken faces in percent",d/p*100),console.log("total fixed faces",m),console.log("fixed triangle mesh shapes",u.length),{brokenFaceIndicators:c,fixedTriangleMeshShapes:u,wireframeMeshes:l}},"undefined"!=typeof module&&(module.exports=GameLib);