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

3 lines
66 KiB
JavaScript
Raw Normal View History

2016-10-28 15:30:15 +02: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,i){this.boneIndex=e,this.weight=i},GameLib.D3.Bone=function(e,i,t,n,s,o,a,r,h,c){this.id=e,this.name=t,this.boneId=i,"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,i,t,n,s){this.id=e,"undefined"==typeof i&&(i="broadphase-"+t),this.name=i,"undefined"==typeof t&&(t=GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE),this.broadphaseType=t,"undefined"==typeof n&&(n=null),this.engine=n,this.instance=null,s&&this.createInstance()},GameLib.D3.Broadphase.prototype.createInstance=function(){if(!(this.engine instanceof GameLib.D3.Engine))throw console.warn("No Engine"),new Error("No Engine");this.engine.isNotCannonThrow();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.SAPBroardphase}return this.instance=e,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,i,t,n){this.r=e,this.g=i,this.b=t,this.a=n},GameLib.D3.Engine=function(e,i){this.engineType=e,this.instance=i},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,i){this.meshId=e,"undefined"==typeof i&&(i=[]),this.componentIds=i},GameLib.D3.FlyControls=function(e,i,t){this.flySpeed=100,this.canvas=t,this.THREE=i,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.webkitReque
useCustomSlidingRotationalSpeed:this.useCustomSlidingRotationalSpeed,customSlidingRotationalSpeed:this.customSlidingRotationalSpeed})},GameLib.D3.RigidBodyVehicle=function(e,i,t){this.componentId=GameLib.D3.Tools.RandomId(),this.engine=e,this.engine.isNotCannonThrow(),this.chassisBody=i,"undefined"==typeof t&&(t=[]),this.wheels=t,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,i,t,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 i?0:i,this.friction="undefined"==typeof t?5:t,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,i,t){i||(i=new GameLib.D3.Vector3(0,0,0)),t||(t=new GameLib.D3.Vector4(0,0,0,1)),this.instance.addShape(e.instance,new this.engine.instance.Vec3(i.x,i.y,i.z),new this.engine.instance.Quaternion(t.x,t.y,t.z,t.w))},GameLib.D3.RigidWheel=function(e,i,t,n){this.componentId=GameLib.D3.Tools.RandomId(),this.body=e,this.position=i,this.axis=t,this.direction=n},GameLib.D3.Scene=function(e,i,t,n,s,o,a,r,h,c,l,p){this.id=e,this.sceneId=GameLib.D3.Tools.RandomId(),this.path=i,this.name=t,""==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,i,t,n,s,o){if("undefined"==typeof XMLHttpRequest)return console.warn("implement server side loading from API here"),i(null,new Error("not implemented"));var a=new XMLHttpRequest;a.open("GET",o+"/scene/load"+e.path+"/"+e.name),a.onreadystatecha
2016-10-28 15:31:45 +02:00
d[l]=v,v++),"undefined"==typeof d[p]&&(m.push(t[p].x,t[p].y,t[p].z),d[p]=v,v++),u.push(d[c],d[l],d[p]),f++}var L=GameLib.D3.Shape(this.engine,GameLib.D3.Shape.SHAPE_TYPE_TRIMESH,i.scale,m,u),b=GameLib.D3.RigidBody(this.engine,0,12);b.addShape(L),this.addRigidBody(b);for(var g in i.children)this.generateTriangleMeshShape(e,i.children[g]);return b},GameLib.D3.World.prototype.fixupTriangleMeshShape=function(e,i,t,n,s,o,a){this.engine.isNotCannonThrow(),o.isNotThreeThrow(),null!=t&&"undefined"!=typeof t&&0!=t||(t=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 i){var y=i[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*t,T.y-w.y*t,T.z-w.z*t);if(r.raycastClosest(T,E,{},h),a){var x=new o.instance.Geometry,P=new o.instance.Mesh(x,new o.instance.MeshBasicMaterial({color:16711680,wireframe:!0,opacity:1})),_=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),t/2,h.hasHit?new o.instance.Color(0,1,0):new o.instance.Color(1,0,0));P.add(_),l.add(P),c.push(P)}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 N=0;N<v.length;N++)if(N!=S){var R=v[N];if(!R.marked&&I.center.distanceTo(R.center)<=n){var V=M[S].indices.length;M[S].indices.push(V,V+1,V+2),M[S].vertices.push(R.vertices[0].x,R.vertices[0].y,R.vertices[0].z),M[S].vertices.push(R.vertices[1].x,R.vertices[1].y,R.vertices[1].z),M[S].vertices.push(R.vertices[2].x,R.vertices[2].y,R.vertices[2].z),R.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);