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

3 lines
77 KiB
JavaScript

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,o,s,a,r,h,c){this.id=e,this.name=i,this.boneId=t,"undefined"==typeof n&&(n=[]),this.childBoneIds=n,"undefined"==typeof o&&(o=null),this.parentBoneId=o,"undefined"==typeof s&&(s=new GameLib.D3.Vector4),this.quaternion=s,"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.ComponentCamera=function(e,t){this.componentId=e||GameLib.D3.Tools.RandomId(),this.parentEntity=null,GameLib.D3.Utils.Extend(GameLib.D3.ComponentCamera,GameLib.D3.ComponentInterface),this.threeCamera=t},GameLib.D3.ComponentCamera.prototype.onLateUpdate=function(e,t){this.threeCamera.quaternion.copy(t.quaternion),this.threeCamera.position.copy(t.position)},GameLib.D3.ComponentColorFlash=function(e){this.componentId=e||GameLib.D3.Tools.RandomId(),this.parentEntity=null,GameLib.D3.Utils.Extend(GameLib.D3.ComponentColorFlash,GameLib.D3.ComponentInterface)},GameLib.D3.ComponentColorFlash.prototype.onUpdate=function(e,t){this.parentEntity.mesh.material.color=new THREE.Color(Math.random(),Math.random(),Math.random())},GameLib.D3.ComponentColorFlash.prototype.onSetParentEntity=function(e,t){t.mesh.material=new THREE.MeshBasicMaterial},GameLib.D3.ComponentFlyControls=function(e){this.componentId=e||GameLib.D3.Tools.RandomId(),this.parentEntity=null,GameLib.D3.Utils.Extend(GameLib.D3.ComponentFlyControls,GameLib.D3.ComponentInterface),this.pitch=0,this.yaw=0,this.canRotate=!1,this.moveSpeed=22.2,this.moveForward=!1,this.moveBackward=!1,this.moveLeft=!1,this.moveRight=!1,this.moveUp=!1,this.moveDown=!1},GameLib.D3.ComponentFlyControls.prototype.onUpdate=function(e,t){var i=new THREE.Euler(this.pitch,this.yaw,0,"YXZ"),n=(new THREE.Quaternion).setFromEuler(i);t.quaternion.x=n.x,t.quaternion.y=n.y,t.quaternion.z=n.z,t.quaternion.w=n.w;var o=new THREE.Vector3(0,0,-1);if(o=o.applyEuler(i).normalize(),this.moveForward?(t.position.x+=o.x*(e*this.moveSpeed),t.position.y+=o.y*(e*this.moveSpeed),t.position.z+=o.z*(e*this.moveSpeed)):this.moveBackward&&(t.position.x-=o.x*(e*this.moveSpeed),t.position.y-=o.y*(e*this.moveSpeed),t.position.z-=o.z*(e*this.moveSpeed)),this.moveLeft){var s=o.cross(new THREE.Vector3(0,1,0));t.position.x-=s.x*(e*this.moveSpeed),t.position.y-=s.y*(e*this.moveSpeed),t.position.z-=s.z*(e*this.moveSpeed)}else if(this.moveRight){var s=o.cross(new THREE.Vector3(0,1,0));t.position.x+=s.x*(e*this.moveSpeed),t.position.y+=s.y*(e*this.moveSpeed),t.position.z+=s.z*(e*this.moveSpeed)}this.moveUp?t.position.y+=e*this.moveSpeed:this.moveDown&&(t.position.y-=e*this.moveSpeed)},GameLib.D3.ComponentFlyControls.prototype.onSetParentEntity=function(e,t){var i=this,n="pointerLockElement"in document||"mozPointerLockElement"in document||"webkitPointerLockElement"in document,o=document.body;document.addEventListener("click",function(e){n&&(0==e.button?(i.canRotate=!0,o.requestPointerLock()):2==e.button&&(i.canRotate=!1,document.exitPointerLock()))}),n&&(o.requestPointerLock=o.requestPointerLock||o.mozRequestPointerLock||o.webkitRequestPointerLock,document.exitPointerLock=document.exitPointerLock||document.mozExitPointerLock||document.webkitExitPointerLock),document.addEventListener("mousemove",function(e){if(i.canRotate){var t=e.movementX||e.mozMovementX||e.webkitMovementX||0,n=e.movementY||e.mozMovementY||e.webkitMovementY||0;i.yaw-=.002*t,i.pitch-=.002*n}sys.mouseCoords.set(e.clientX/window.innerWidth*2-1,2*-(e.clientY/window.innerHeight)+1)},!1),document.addEventListener("keydown",function(e){switch(e.keyCode){case 87:i.moveForward=!0;break;case 65:i.moveLeft=!0;break;case 83:i.moveBackward=!0;break;case 68:i.moveRight=!0;break;case 32:i.moveUp=!0;break;case 16:i.moveDown=!0}},!1),document.addEventListener("keyup",function(e){switch(e.keyCode){case 38:case 87:i.moveForward=!1;break;case 37:case 65:i.moveLeft=!1;break;case 40:case 83:i.moveBackward=!1;break;case 39:case 68:i.moveRight=!1;break;case 32:i.moveUp=!1;break;case 16:i.moveDown=!1}},!1)},GameLib.D3.ComponentFollow=function(e,t,i,n){this.componentId=e||GameLib.D3.Tools.RandomId(),this.parentEntity=null,GameLib.D3.Utils.Extend(GameLib.D3.ComponentFollow,GameLib.D3.ComponentInterface),this.targetEntity=t,this.moveSpeed=2.5,GameLib.D3.Utils.UndefinedOrNull(i)&&(i=new GameLib.D3.Vector3(0,0,0)),this.targetOffset=i,this.minDistance=n||0},GameLib.D3.ComponentFollow.prototype.onUpdate=function(e,t){if(this.targetEntity){var i=(new THREE.Vector3).copy(this.targetEntity.position);i.x+=this.targetOffset.x,i.y+=this.targetOffset.y,i.z+=this.targetOffset.z,i.distanceTo(t.position)>this.minDistance&&(t.position=t.position.lerp(i,this.moveSpeed*e))}},GameLib.D3.ComponentInterface=function(e){this.componentId=e||GameLib.D3.Tools.RandomId(),this.parentEntity=null},GameLib.D3.ComponentInterface.prototype.setParentEntity=function(e,t){this.parentEntity=t,this.onSetParentEntity(e,t)},GameLib.D3.ComponentInterface.prototype.update=function(e,t){this.onUpdate(e,t)},GameLib.D3.ComponentInterface.prototype.lateUpdate=function(e,t){this.onLateUpdate(e,t)},GameLib.D3.ComponentInterface.prototype.register=function(e){this.onRegistered(e)},GameLib.D3.ComponentInterface.prototype.onUpdate=function(e,t){},GameLib.D3.ComponentInterface.prototype.onLateUpdate=function(e,t){},GameLib.D3.ComponentInterface.prototype.onRegistered=function(e){},GameLib.D3.ComponentInterface.prototype.onSetParentEntity=function(e,t){},GameLib.D3.ComponentLookAt=function(e,t,i){this.componentId=e||GameLib.D3.Tools.RandomId(),this.parentEntity=null,GameLib.D3.Utils.Extend(GameLib.D3.ComponentLookAt,GameLib.D3.ComponentInterface),this.targetEntity=t,this.targetOffset=i||new GameLib.D3.Vector3(0,0,0)},GameLib.D3.ComponentLookAt.prototype.onUpdate=function(e,t){if(this.targetEntity){var i=this.targetEntity.position,n=(new THREE.Matrix4).lookAt(new THREE.Vector3(t.position.x,t.position.y,t.position.z),new THREE.Vector3(i.x,i.y,i.z),new THREE.Vector3(0,1,0)),o=(new THREE.Quaternion).setFromRotationMatrix(n);this.parentEntity.quaternion.x=o.x,this.parentEntity.quaternion.y=o.y,this.parentEntity.quaternion.z=o.z,this.parentEntity.quaternion.w=o.w}},GameLib.D3.ComponentMeshPermutation=function(e,t,i,n){this.componentId=e||GameLib.D3.Tools.RandomId(),this.parentEntity=null,this.positionOffset=t||new GameLib.D3.Vector3(0,0,0),this.quaternionOffset=i||new GameLib.D3.Quaternion(0,0,0,1),this.scaleOffset=n||new GameLib.D3.Vector3(0,0,0),GameLib.D3.Utils.Extend(GameLib.D3.ComponentMeshPermutation,GameLib.D3.ComponentInterface)},GameLib.D3.ComponentMeshPermutation.prototype.onLateUpdate=function(e,t){if(t&&t.mesh){var i=new THREE.Quaternion;i.copy(t.mesh.quaternion);var n=i.clone(),o=new THREE.Vector3;o.copy(t.mesh.position);var s=new THREE.Quaternion;s.copy(this.quaternionOffset),i=i.multiply(s).normalize();var a=new THREE.Vector3;a.copy(this.positionOffset),o=o.add(a.applyQuaternion(n));var r=new THREE.Vector3;r.copy(t.mesh.scale);var h=new THREE.Vector3;h.copy(this.scaleOffset),r=r.add(h),t.mesh.position.copy(o),t.mesh.quaternion.copy(i),t.mesh.scale.copy(r)}},GameLib.D3.ComponentRaycastVehicleControls=function(e,t,i,n,o,s,a){this.componentId=e||GameLib.D3.Tools.RandomId(),this.parentEntity=null,this.raycastVehicleComponent=null,this.frontLWheelIndex=t||0,this.frontRWheelIndex=i||1,this.backLWheelIndex=n||2,this.backRWheelIndex=o||3,this.maxForce=s||400,this.steering=a||.5,GameLib.D3.Utils.Extend(GameLib.D3.ComponentRaycastVehicleControls,GameLib.D3.ComponentInterface)},GameLib.D3.ComponentRaycastVehicleControls.prototype.onSetParentEntity=function(e,t){if(console.log("Set parent!"),this.raycastVehicleComponent=t.getComponent(GameLib.D3.RaycastVehicle),this.raycastVehicleComponent){var i=this;document.addEventListener("keydown",function(e){73==e.keyCode?(i.raycastVehicleComponent.instance.applyEngineForce(-i.maxForce,i.backLWheelIndex),i.raycastVehicleComponent.instance.applyEngineForce(-i.maxForce,i.backRWheelIndex)):74==e.keyCode?(i.raycastVehicleComponent.instance.setSteeringValue(i.steering,i.frontLWheelIndex),i.raycastVehicleComponent.instance.setSteeringValue(i.steering,i.frontRWheelIndex)):75==e.keyCode?(i.raycastVehicleComponent.instance.applyEngineForce(i.maxForce,i.backLWheelIndex),i.raycastVehicleComponent.instance.applyEngineForce(i.maxForce,i.backRWheelIndex)):76==e.keyCode&&(i.raycastVehicleComponent.instance.setSteeringValue(-i.steering,i.frontLWheelIndex),i.raycastVehicleComponent.instance.setSteeringValue(-i.steering,i.frontRWheelIndex))},!1),document.addEventListener("keyup",function(e){73==e.keyCode?(i.raycastVehicleComponent.instance.applyEngineForce(0,i.backLWheelIndex),i.raycastVehicleComponent.instance.applyEngineForce(0,i.backRWheelIndex)):74==e.keyCode?(i.raycastVehicleComponent.instance.setSteeringValue(0,i.frontLWheelIndex),i.raycastVehicleComponent.instance.setSteeringValue(0,i.frontRWheelIndex)):75==e.keyCode?(i.raycastVehicleComponent.instance.applyEngineForce(0,i.backLWheelIndex),i.raycastVehicleComponent.instance.applyEngineForce(0,i.backRWheelIndex)):76==e.keyCode&&(i.raycastVehicleComponent.instance.setSteeringValue(0,i.frontLWheelIndex),i.raycastVehicleComponent.instance.setSteeringValue(0,i.frontRWheelIndex))},!1)}else console.warn("NO RAYCAST VEHICLE FOUND!")},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,i,n,o){this.meshId=e,"undefined"==typeof t&&(t=[]),this.componentIds=t,this.parentScene=null,this.mesh=null,GameLib.D3.Utils.UndefinedOrNull(i)&&(i=new GameLib.D3.Vector3(0,0,0)),this.position=i,GameLib.D3.Utils.UndefinedOrNull(n)&&(n=new GameLib.D3.Vector4(0,0,0,1)),this.quaternion=n,GameLib.D3.Utils.UndefinedOrNull(o)&&(o=new GameLib.D3.Vector3(1,1,1)),this.scale=o},GameLib.D3.Entity.prototype.update=function(e){for(var t in this.componentIds){var i=this.componentIds[t],n=this.parentScene.componentIdToComponent[i];n&&n.onUpdate&&n.onUpdate(e,this)}this.mesh&&(this.mesh.position.set(this.position.x,this.position.y,this.position.z),this.mesh.quaternion.set(this.quaternion.x,this.quaternion.y,this.quaternion.z,this.quaternion.w),this.mesh.scale.set(this.scale.x,this.scale.y,this.scale.z)),this.onUpdate(e)},GameLib.D3.Entity.prototype.lateUpdate=function(e){for(var t in this.componentIds){var i=this.componentIds[t],n=this.parentScene.componentIdToComponent[i];n&&n.onLateUpdate&&n.onLateUpdate(e,this)}this.onLateUpdate(e)},GameLib.D3.Entity.prototype.register=function(e){this.parentScene=e,null!=this.meshId&&e.meshIdToMesh[this.meshId]&&(e.instance.add(e.meshIdToMesh[this.meshId]),this.mesh=e.meshIdToMesh[this.meshId]),this.onRegistered(e)},GameLib.D3.Entity.prototype.addComponentId=function(e){this.componentIds.push(e)},GameLib.D3.Entity.prototype.addComponent=function(e){this.parentScene.registerComponent(e),this.componentIds.push(e.componentId),e.setParentEntity&&"function"==typeof e.setParentEntity&&e.setParentEntity(this.parentScene,this)},GameLib.D3.Entity.prototype.getComponent=function(e){for(var t in this.componentIds){var i=this.componentIds[t],n=this.parentScene.componentIdToComponent[i];if(n instanceof e)return n}return null},GameLib.D3.Entity.prototype.onUpdate=function(e){},GameLib.D3.Entity.prototype.onLateUpdate=function(e){},GameLib.D3.Entity.prototype.onRegistered=function(e){},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.onMouseWheel=function(e){this.moveForward=!0,this.applyTranslation(.001*e.wheelDelta),e.preventDefault(),this.moveForward=!1},GameLib.D3.FlyControls.prototype.onMouseDown=function(e){1==e.button&&(this.canRotate=!0,this.canvas.addEventListener("mousemove",this.mouseMoveCallback,!1))},GameLib.D3.FlyControls.prototype.onMouseUp=function(e){1==e.button&&(this.canRotate=!1,this.canvas.removeEventListener("mousemove",this.mouseMoveCallback))},GameLib.D3.FlyControls.prototype.applyRotation=function(){this.camera.rotation.set(this.pitch,this.yaw,0,"YXZ")},GameLib.D3.FlyControls.prototype.applyTranslation=function(e){var t=new this.THREE.Vector3(0,0,-1),i=new this.THREE.Euler(0,0,0,"YXZ");i.set(this.pitch,this.yaw,0,"YXZ"),t=t.applyEuler(i);var n=t.normalize(),o=n.cross(new this.THREE.Vector3(0,1,0));this.moveForward?(this.camera.position.x+=n.x*(e*this.flySpeed),this.camera.position.y+=n.y*(e*this.flySpeed),this.camera.position.z+=n.z*(e*this.flySpeed)):this.moveBackward&&(this.camera.position.x-=n.x*(e*this.flySpeed),this.camera.position.y-=n.y*(e*this.flySpeed),this.camera.position.z-=n.z*(e*this.flySpeed)),this.moveLeft?(this.camera.position.x-=o.x*(e*this.flySpeed),this.camera.position.y-=o.y*(e*this.flySpeed),this.camera.position.z-=o.z*(e*this.flySpeed)):this.moveRight&&(this.camera.position.x+=o.x*(e*this.flySpeed),this.camera.position.y+=o.y*(e*this.flySpeed),this.camera.position.z+=o.z*(e*this.flySpeed)),this.moveUp?this.camera.position.y+=e*this.flySpeed:this.moveDown&&(this.camera.position.y-=e*this.flySpeed)},GameLib.D3.FlyControls.prototype.update=function(e){this.applyRotation(),this.applyTranslation(e)},GameLib.D3.FlyControls.prototype.onMouseMove=function(e){if(this.canRotate){var t=e.movementX||e.mozMovementX||e.webkitMovementX||0,i=e.movementY||e.mozMovementY||e.webkitMovementY||0;this.yaw-=.002*t,this.pitch-=.002*i}},GameLib.D3.FlyControls.prototype.onKeyDown=function(e){switch(e.keyCode){case 87:this.moveForward=!0;break;case 65:this.moveLeft=!0;break;case 83:this.moveBackward=!0;break;case 68:this.moveRight=!0;break;case 104:this.moveUp=!0;break;case 98:this.moveDown=!0}},GameLib.D3.FlyControls.prototype.onKeyUp=function(e){switch(e.keyCode){case 38:case 87:this.moveForward=!1;break;case 37:case 65:this.moveLeft=!1;break;case 40:case 83:this.moveBackward=!1;break;case 39:case 68:this.moveRight=!1;break;case 104:this.moveUp=!1;break;case 98:this.moveDown=!1}},GameLib.D3.Game=function(){this.scenes={}},GameLib.D3.Game.prototype.addScene=function(e,t){this.scenes[t]=e},GameLib.D3.Game.prototype.processPhysics=function(e){for(var t in this.scenes){var i=this.scenes[t];for(var n in i.worlds){var o=i.worlds[n];o.step(e)}}},GameLib.D3.Game.prototype.render=function(e,t,i){for(var n in this.scenes){var o=this.scenes[n];o.render(e,t,i)}},GameLib.D3.Game.prototype.update=function(e,t){for(var i in this.scenes){var n=this.scenes[i];for(var o in n.worlds){var s=n.worlds[o];s.step(t)}n.update(e),n.lateUpdate(e)}},GameLib.D3.Graphics=function(e,t){this.graphicsType=e,this.instance=t},GameLib.D3.Graphics.prototype.isThree=function(){return this.graphicsType==GameLib.D3.Graphics.GRAPHICS_TYPE_THREE},GameLib.D3.Graphics.prototype.isNotThreeThrow=function(){if(this.graphicsType!=GameLib.D3.Graphics.GRAPHICS_TYPE_THREE)throw console.warn("Only THREE supported for this function"),new Error("Only THREE supported for this function")},GameLib.D3.Graphics.GRAPHICS_TYPE_THREE=1,GameLib.D3.Heightmap=function(e,t,i,n,o){"undefined"==typeof e&&(e=0),this.sizeX=e,"undefined"==typeof t&&(t=0),this.sizeY=t,"undefined"==typeof i&&(i=[]),this.matrix=i,"undefined"==typeof n&&(n=10),this.elementSize=n,"undefined"==typeof o&&(o=15),this.elementSize=o},GameLib.D3.Heightmap.GenerateInstanceMesh=function(e,t,i){e.isNotThreeThrow(),i.isNotCannonThrow();for(var n=new e.instance.Geometry,o=new i.instance.Vec3,s=new i.instance.Vec3,a=new i.instance.Vec3,r=0;r<t.instance.data.length-1;r++)for(var h=0;h<t.instance.data[r].length-1;h++)for(var c=0;c<2;c++){t.instance.getConvexTrianglePillar(r,h,0===c),o.copy(t.instance.pillarConvex.vertices[0]),s.copy(t.instance.pillarConvex.vertices[1]),a.copy(t.instance.pillarConvex.vertices[2]),o.vadd(t.instance.pillarOffset,o),s.vadd(t.instance.pillarOffset,s),a.vadd(t.instance.pillarOffset,a),n.vertices.push(new e.instance.Vector3(o.x,o.y,o.z),new e.instance.Vector3(s.x,s.y,s.z),new e.instance.Vector3(a.x,a.y,a.z));var p=n.vertices.length-3;n.faces.push(new e.instance.Face3(p,p+1,p+2))}return n.computeBoundingSphere(),n.computeFaceNormals(),new e.instance.Mesh(n,new e.instance.MeshNormalMaterial({wireframe:!1,shading:e.instance.SmoothShading}))},GameLib.D3.Heightmap.GenerateHeightmapDataFromImage=function(e,t,i){var n=new Image;n.onload=function(){var e=document.createElement("canvas");e.width=n.width,e.height=n.height;var o=e.getContext("2d");o.drawImage(n,0,0);for(var s=o.getImageData(0,0,n.width,n.height),a=s.data,r=[],h=0,c=a.length;h<c;h+=4)r.push(a[h]);for(var p=[],l=n.width,d=n.height,m=0;m<l;m++){p.push([]);for(var u=0;u<d;u++){var f=r[l-m+u*d]/255*t;p[m].push(f)}}i(new GameLib.D3.Heightmap(l,d,p,10,t))},n.src=e},GameLib.D3.Image=function(e,t,i,n,o){this.id=e,this.filename=i,this.textureLink=t,"undefined"==typeof n&&(n=0),this.size=n,"undefined"==typeof o&&(o="application/octet-stream",this.filename.match(/(png)$/i)&&(o="image/png"),this.filename.match(/(jpg|jpeg)$/i)&&(o="image/jpeg"),this.filename.match(/(gif)$/i)&&(o="image/gif")),this.contentType=o},GameLib.D3.Light=function(e,t,i,n,o,s,a,r,h,c,p,l,d,m,u){this.id=e,this.lightType=t,this.name=i,this.color=n,this.intensity=o,"undefined"==typeof s&&(s=new GameLib.D3.Vector3(0,0,0)),this.position=s,"undefined"==typeof a&&(a=new GameLib.D3.Vector3(0,0,0)),this.targetPosition=a,"undefined"==typeof r&&(r=new GameLib.D3.Vector4),this.quaternion=r,"undefined"==typeof h&&(h=new GameLib.D3.Vector3(0,0,0)),this.rotation=h,"undefined"==typeof c&&(c=new GameLib.D3.Vector3(1,1,1)),this.scale=c,"undefined"==typeof p&&(p=0),this.distance=p,"undefined"==typeof l&&(l=1),this.decay=l,"undefined"==typeof d&&(d=4*Math.PI),this.power=d,"undefined"==typeof m&&(m=Math.PI/3),this.angle=m,"undefined"==typeof u&&(u=0),this.penumbra=u},GameLib.D3.Material=function(e,t,i,n,o,s,a,r,h,c,p,l,d,m,u,f,y,L,v,b,D,G,g,w,E,T,x,S,_,P,I,M,R,V,C,z,N,A,O,Y,k,F,H,U,B,q,W,Q,X,J,Z,j,K,$,ee,te,ie){this.id=e,this.name=t,"undefined"==typeof i&&(i=GameLib.D3.Material.TYPE_MESH_STANDARD),this.materialType=i,"undefined"==typeof n&&(n=1),this.opacity=n,"undefined"==typeof o&&(o=GameLib.D3.Material.TYPE_FRONT_SIDE),this.side=o,"undefined"==typeof s&&(s=!1),this.transparent=s,"undefined"==typeof a&&(a={alpha:null,ao:null,bump:null,diffuse:null,displacement:null,emissive:null,environment:null,light:null,metalness:null,normal:null,roughness:null,specular:null}),this.maps=a,"undefined"==typeof r&&(r=new GameLib.D3.Color(.06,.06,.06,.06)),this.specular=r,"undefined"==typeof h&&(h=1),this.lightMapIntensity=h,"undefined"==typeof c&&(c=1),this.aoMapIntensity=c,"undefined"==typeof p&&(p=new GameLib.D3.Color(1,1,1,1)),this.color=p,"undefined"==typeof l&&(l=new GameLib.D3.Color(0,0,0,0)),this.emissive=l,"undefined"==typeof d&&(d=1),this.emissiveIntensity=d,"undefined"==typeof m&&(m=GameLib.D3.Material.TYPE_MULTIPLY_OPERATION),this.combine=m,"undefined"==typeof u&&(u=30),this.shininess=u,"undefined"==typeof f&&(f=1),this.reflectivity=f,"undefined"==typeof y&&(y=.98),this.refractionRatio=y,"undefined"==typeof L&&(L=!0),this.fog=L,"undefined"==typeof v&&(v=!1),this.wireframe=v,"undefined"==typeof b&&(b=1),this.wireframeLineWidth=b,"undefined"==typeof D&&(D="round"),this.wireframeLineCap=D,"undefined"==typeof G&&(G="round"),this.wireframeLineJoin=G,"undefined"==typeof g&&(g=GameLib.D3.Material.TYPE_NO_COLORS),this.vertexColors=g,"undefined"==typeof w&&(w=!1),this.skinning=w,"undefined"==typeof E&&(E=!1),this.morphTargets=E,"undefined"==typeof T&&(T=!1),this.morphNormals=T,"undefined"==typeof q&&(q=0),this.overdraw=q,"undefined"==typeof x&&(x=1),this.lineWidth=x,"undefined"==typeof S&&(S="round"),this.lineCap=S,"undefined"==typeof _&&(_="round"),this.lineJoin=_,"undefined"==typeof P&&(P=3),this.dashSize=P,"undefined"==typeof I&&(I=1),this.gapWidth=I,"undefined"==typeof M&&(M=GameLib.D3.Material.TYPE_NORMAL_BLENDING),this.blending=M,"undefined"==typeof R&&(R=GameLib.D3.Material.TYPE_SRC_ALPHA_FACTOR),this.blendSrc=R,"undefined"==typeof V&&(V=GameLib.D3.Material.TYPE_ONE_MINUS_SRC_ALPHA_FACTOR),this.blendDst=V,"undefined"==typeof C&&(C=GameLib.D3.Material.TYPE_ADD_EQUATION),this.blendEquation=C,"undefined"==typeof z&&(z=!0),this.depthTest=z,"undefined"==typeof N&&(N=GameLib.D3.Material.TYPE_LESS_EQUAL_DEPTH),this.depthFunc=N,"undefined"==typeof A&&(A=!0),this.depthWrite=A,"undefined"==typeof O&&(O=!1),this.polygonOffset=O,"undefined"==typeof Y&&(Y=1),this.polygonOffsetFactor=Y,"undefined"==typeof k&&(k=1),this.polygonOffsetUnits=k,"undefined"==typeof F&&(F=0),this.alphaTest=F,"undefined"==typeof H&&(H=[]),this.clippingPlanes=H,"undefined"==typeof U&&(U=!1),this.clipShadows=U,"undefined"==typeof B&&(B=!0),this.visible=B,"undefined"==typeof W&&(W=GameLib.D3.Material.TYPE_FLAT_SHADING),this.shading=W,"undefined"==typeof Q&&(Q=1),this.bumpScale=Q,"undefined"==typeof X&&(X=1),this.normalScale=X,"undefined"==typeof J&&(J=1),this.displacementScale=J,"undefined"==typeof Z&&(Z=0),this.displacementBias=Z,"undefined"==typeof j&&(j=.5),this.roughness=j,"undefined"==typeof K&&(K=.5),this.metalness=K,"undefined"==typeof $&&($=1),this.pointSize=$,"undefined"==typeof ee&&(ee=!0),this.pointSizeAttenuation=ee,"undefined"==typeof te&&(te=0),this.spriteRotation=te,"undefined"==typeof ie&&(ie=1),this.envMapIntensity=ie},GameLib.D3.Material.TYPE_MULTIPLY_OPERATION=0,GameLib.D3.Material.TYPE_MIX_OPERATION=1,GameLib.D3.Material.TYPE_ADD_OPERATION=2,GameLib.D3.Material.TYPE_NO_COLORS=0,GameLib.D3.Material.TYPE_FACE_COLORS=1,GameLib.D3.Material.TYPE_VERTEX_COLORS=2,GameLib.D3.Material.TYPE_NORMAL_BLENDING=1,GameLib.D3.Material.TYPE_ADDITIVE_BLENDING=2,GameLib.D3.Material.TYPE_SUBTRACTIVE_BLENDING=3,GameLib.D3.Material.TYPE_MULTIPLY_BLENDING=4,GameLib.D3.Material.TYPE_CUSTOM_BLENDING=5,GameLib.D3.Material.TYPE_ZERO_FACTOR=200,GameLib.D3.Material.TYPE_ONE_FACTOR=201,GameLib.D3.Material.TYPE_SRC_COLOR_FACTOR=202,GameLib.D3.Material.TYPE_ONE_MINUS_SRC_COLOR_FACTOR=203,GameLib.D3.Material.TYPE_SRC_ALPHA_FACTOR=204,GameLib.D3.Material.TYPE_ONE_MINUS_SRC_ALPHA_FACTOR=205,GameLib.D3.Material.TYPE_DST_ALPHA_FACTOR=206,GameLib.D3.Material.TYPE_ONE_MINUS_DST_ALPHA_FACTOR=207,GameLib.D3.Material.TYPE_DST_COLOR_FACTOR=208,GameLib.D3.Material.TYPE_ONE_MINUS_DST_COLOR_FACTOR=209,GameLib.D3.Material.TYPE_SRC_ALPHA_SATURATE_FACTOR=210,GameLib.D3.Material.TYPE_ADD_EQUATION=100,GameLib.D3.Material.TYPE_SUBTRACT_EQUATION=101,GameLib.D3.Material.TYPE_REVERSE_SUBTRACT_EQUATION=102,GameLib.D3.Material.TYPE_MIN_EQUATION=103,GameLib.D3.Material.TYPE_MAX_EQUATION=104,GameLib.D3.Material.TYPE_NEVER_DEPTH=0,GameLib.D3.Material.TYPE_ALWAYS_DEPTH=1,GameLib.D3.Material.TYPE_LESS_DEPTH=2,GameLib.D3.Material.TYPE_LESS_EQUAL_DEPTH=3,GameLib.D3.Material.TYPE_EQUAL_DEPTH=4,GameLib.D3.Material.TYPE_GREATER_EQUAL_DEPTH=5,GameLib.D3.Material.TYPE_GREATER_DEPTH=6,GameLib.D3.Material.TYPE_NOT_EQUAL_DEPTH=7,GameLib.D3.Material.TYPE_FRONT_SIDE=0,GameLib.D3.Material.TYPE_BACK_SIDE=1,GameLib.D3.Material.TYPE_DOUBLE_SIDE=2,GameLib.D3.Material.TYPE_FLAT_SHADING=1,GameLib.D3.Material.TYPE_SMOOTH_SHADING=2,GameLib.D3.Material.TYPE_LINE_BASIC="LineBasicMaterial",GameLib.D3.Material.TYPE_LINE_DASHED="LineDashedMaterial",GameLib.D3.Material.TYPE_MESH_BASIC="MeshBasicMaterial",GameLib.D3.Material.TYPE_MESH_DEPTH="MeshDepthMaterial",GameLib.D3.Material.TYPE_MESH_LAMBERT="MeshLambertMaterial",GameLib.D3.Material.TYPE_MESH_NORMAL="MeshNormalMaterial",GameLib.D3.Material.TYPE_MESH_PHONG="MeshPhongMaterial",GameLib.D3.Material.TYPE_MESH_STANDARD="MeshStandardMaterial",GameLib.D3.Material.TYPE_POINTS="PointsMaterial",GameLib.D3.Material.TYPE_SPRITE="SpriteMaterial",GameLib.D3.Material.TYPE_MULTI_MATERIAL="MultiMaterial",GameLib.D3.Material.createInstanceMaterial=function(e,t,i,n){var o=Q.defer(),s=null,a=[];if(e.materialType==GameLib.D3.Material.TYPE_MESH_STANDARD?(s=new t.instance.MeshStandardMaterial({name:e.name,opacity:e.opacity,transparent:e.transparent,blending:e.blending,blendSrc:e.blendSrc,blendDst:e.blendDst,blendEquation:e.blendEquation,depthTest:e.depthTest,depthFunc:e.depthFunc,depthWrite:e.depthWrite,polygonOffset:e.polygonOffset,polygonOffsetFactor:e.polygonOffsetFactor,polygonOffsetUnits:e.polygonOffsetUnits,alphaTest:e.alphaTest,clippingPlanes:e.clippingPlanes,clipShadows:e.clipShadows,overdraw:e.overdraw,visible:e.visible,side:e.side,color:new t.instance.Color(e.color.r,e.color.g,e.color.b),roughness:e.roughness,metalness:e.metalness,lightMapIntensity:e.lightMapIntensity,aoMapIntensity:e.aoMapIntensity,emissive:new t.instance.Color(e.emissive.r,e.emissive.g,e.emissive.b),emissiveIntensity:e.emissiveIntensity,bumpScale:e.bumpScale,normalScale:e.normalScale,displacementScale:e.displacementScale,refractionRatio:e.refractionRatio,fog:e.fog,shading:e.shading,wireframe:e.wireframe,wireframeLinewidth:e.wireframeLineWidth,wireframeLinecap:e.wireframeLineCap,wireframeLinejoin:e.wireframeLineJoin,vertexColors:e.vertexColors,skinning:e.skinning,morphTargets:e.morphTargets,morphNormals:e.morphNormals}),a.push("diffuse","light","ao","emissive","bump","normal","displacement","roughness","metalness","alpha","environment")):e.materialType==GameLib.D3.Material.TYPE_MESH_PHONG?(s=new t.instance.MeshPhongMaterial({name:e.name,opacity:e.opacity,transparent:e.transparent,blending:e.blending,blendSrc:e.blendSrc,blendDst:e.blendDst,blendEquation:e.blendEquation,depthTest:e.depthTest,depthFunc:e.depthFunc,depthWrite:e.depthWrite,polygonOffset:e.polygonOffset,polygonOffsetFactor:e.polygonOffsetFactor,polygonOffsetUnits:e.polygonOffsetUnits,alphaTest:e.alphaTest,clippingPlanes:e.clippingPlanes,clipShadows:e.clipShadows,overdraw:e.overdraw,visible:e.visible,side:e.side,color:new t.instance.Color(e.color.r,e.color.g,e.color.b),specular:new t.instance.Color(e.specular.r,e.specular.g,e.specular.b),shininess:e.shininess,lightMapIntensity:e.lightMapIntensity,aoMapIntensity:e.aoMapIntensity,emissive:new t.instance.Color(e.emissive.r,e.emissive.g,e.emissive.b),emissiveIntensity:e.emissiveIntensity,bumpScale:e.bumpScale,normalScale:e.normalScale,displacementScale:e.displacementScale,combine:e.combine,refractionRatio:e.refractionRatio,fog:e.fog,shading:e.shading,wireframe:e.wireframe,wireframeLinewidth:e.wireframeLineWidth,wireframeLinecap:e.wireframeLineCap,wireframeLinejoin:e.wireframeLineJoin,vertexColors:e.vertexColors,skinning:e.skinning,morphTargets:e.morphTargets,morphNormals:e.morphNormals}),a.push("diffuse","light","ao","emissive","bump","normal","displacement","specular","alpha","environment")):console.log("material type is not implemented yet: "+e.materialType+" - material indexes could be screwed up"),a.length>0){var r=GameLib.D3.Texture.loadMaps(e,a,s,t,i,n);Q.all(r).then(function(){o.resolve(s)}).catch(function(e){console.log(e),o.reject(e)})}else o.resolve(s);return o.promise},GameLib.D3.Matrix3=function(e,t,i){this.identity(),e&&(this.rows[0]=e),t&&(this.rows[1]=t),i&&(this.rows[2]=i)},GameLib.D3.Matrix3.prototype.identity=function(){this.rows=[new GameLib.D3.Vector4(1,0,0),new GameLib.D3.Vector4(0,1,0),new GameLib.D3.Vector4(0,0,1)]},GameLib.D3.Matrix4=function(e,t,i,n){this.identity(),e&&(this.rows[0]=e),t&&(this.rows[1]=t),i&&(this.rows[2]=i),n&&(this.rows[3]=n)},GameLib.D3.Matrix4.prototype.rotationMatrixX=function(e){return this.identity(),this.rows[1]=new GameLib.D3.Vector4(0,Math.cos(e),-1*Math.sin(e),0),this.rows[2]=new GameLib.D3.Vector4(0,Math.sin(e),Math.cos(e),0),this},GameLib.D3.Matrix4.prototype.rotationMatrixY=function(e){return this.identity(),this.rows[0]=new GameLib.D3.Vector4(Math.cos(e),0,Math.sin(e),0),this.rows[2]=new GameLib.D3.Vector4(-1*Math.sin(e),0,Math.cos(e),0),this},GameLib.D3.Matrix4.prototype.rotationMatrixZ=function(e){return this.identity(),this.rows[0]=new GameLib.D3.Vector4(Math.cos(e),-1*Math.sin(e),0,0),this.rows[1]=new GameLib.D3.Vector4(Math.sin(e),Math.cos(e),0,0),this},GameLib.D3.Matrix4.prototype.rotateX=function(e,t){return this.identity(),this.rotationMatrixX(e),this.multiply(t)},GameLib.D3.Matrix4.prototype.rotateY=function(e,t){return this.identity(),this.rotationMatrixY(e),this.multiply(t)},GameLib.D3.Matrix4.prototype.rotateZ=function(e,t){return this.identity(),this.rotationMatrixZ(e),this.multiply(t)},GameLib.D3.Matrix4.prototype.multiply=function(e){return e instanceof GameLib.D3.Vector4?new GameLib.D3.Vector4(this.rows[0].x*e.x+this.rows[0].y*e.y+this.rows[0].z*e.z+this.rows[0].w*e.w,this.rows[1].x*e.x+this.rows[1].y*e.y+this.rows[1].z*e.z+this.rows[1].w*e.w,this.rows[2].x*e.x+this.rows[2].y*e.y+this.rows[2].z*e.z+this.rows[2].w*e.w,this.rows[3].x*e.x+this.rows[3].y*e.y+this.rows[3].z*e.z+this.rows[3].w*e.w):e instanceof GameLib.D3.Vector3?new GameLib.D3.Vector3(this.rows[0].x*e.x+this.rows[0].y*e.y+this.rows[0].z*e.z,this.rows[1].x*e.x+this.rows[1].y*e.y+this.rows[1].z*e.z,this.rows[2].x*e.x+this.rows[2].y*e.y+this.rows[2].z*e.z):void 0;
},GameLib.D3.Matrix4.prototype.identity=function(){this.rows=[new GameLib.D3.Vector4(1,0,0,0),new GameLib.D3.Vector4(0,1,0,0),new GameLib.D3.Vector4(0,0,1,0),new GameLib.D3.Vector4(0,0,0,1)]},GameLib.D3.Matrix4.prototype.lookAt=function(e,t,i){var n=new GameLib.D3.Vector3(e.x,e.y,e.z),o=n.subtract(t).normalize();0===o.squared()&&(o.z=1);var s=i.cross(o).normalize();0===s.squared()&&(o.x+=1e-4,s=i.cross(o).normalize());var a=o.cross(s);return this.rows[0].x=s.x,this.rows[0].y=s.y,this.rows[0].z=s.z,this.rows[1].x=a.x,this.rows[1].y=a.y,this.rows[1].z=a.z,this.rows[2].x=o.x,this.rows[2].y=o.y,this.rows[2].z=o.z,this},GameLib.D3.Mesh=function(e,t,i,n,o,s,a,r,h,c,p,l,d,m,u,f,y,L,v){this.id=e,this.meshId=GameLib.D3.Tools.RandomId(),this.path=t,this.name=i,this.meshType=n,this.vertices=o,this.faces=s,"undefined"==typeof a&&(a=null),this.skeleton=a,"undefined"==typeof r&&(r=[]),this.faceVertexUvs=r,"undefined"==typeof h&&(h=[]),this.skinIndices=h,"undefined"==typeof c&&(c=[]),this.skinWeights=c,"undefined"==typeof p&&(p=[]),this.materials=p,"undefined"==typeof l&&(l=new GameLib.D3.Vector3(0,0,0)),this.position=l,"undefined"==typeof d&&new GameLib.D3.Vector4,this.quaternion=d,"undefined"==typeof m&&(m=new GameLib.D3.Vector3(0,0,0)),this.rotation=m,"undefined"==typeof u&&(u=new GameLib.D3.Vector3(1,1,1)),this.scale=u,"undefined"==typeof f&&(f=new GameLib.D3.Vector3(0,1,0)),this.up=f,this.physics=y,this.parentMeshId=L,this.parentSceneId=v},GameLib.D3.Mesh.TYPE_NORMAL=0,GameLib.D3.Mesh.TYPE_SKINNED=1,GameLib.D3.Mesh.createInstanceMesh=function(e,t,i,n){var o=null;if(e.meshType==GameLib.D3.Mesh.TYPE_NORMAL&&(o=new n.instance.Mesh(t,i)),e.meshType==GameLib.D3.Mesh.TYPE_SKINNED){for(var s=e.skeleton.bones,a=e.skinIndices,r=e.skinWeights,h=[],c=0;c<s.length;c++){var p=new n.instance.Bone;p.name=s[c].name,p.position.x=s[c].position.x,p.position.y=s[c].position.y,p.position.z=s[c].position.z,p.rotation.x=s[c].rotation.x,p.rotation.y=s[c].rotation.y,p.rotation.z=s[c].rotation.z,p.quaternion.x=s[c].quaternion.x,p.quaternion.y=s[c].quaternion.y,p.quaternion.z=s[c].quaternion.z,p.quaternion.w=s[c].quaternion.w,p.scale.x=s[c].scale.x,p.scale.y=s[c].scale.y,p.scale.z=s[c].scale.z,p.up.x=s[c].up.x,p.up.y=s[c].up.y,p.up.z=s[c].up.z,h.push(p)}for(var l=0;l<s.length;l++)for(var d=0;d<s[l].childBoneIds.length;d++)h[l].add(h[s[l].childBoneIds[d]]);for(var m=0;m<a.length;m++)t.skinIndices.push(new n.instance.Vector4(a[m].x,a[m].y,a[m].z,a[m].w));for(var u=0;u<r.length;u++)t.skinWeights.push(new n.instance.Vector4(r[u].x,r[u].y,r[u].z,r[u].w));o=new n.instance.SkinnedMesh(t,i);var f=new n.instance.Skeleton(h);f.useVertexTexture=e.skeleton.useVertexTexture;for(var y=0;y<s.length;y++)if(null===s[y].parentBoneId){o.add(h[y]);break}o.bind(f),o.pose(),o.skeleton.skeletonHelper=new n.instance.SkeletonHelper(o),o.skeleton.skeletonHelper.material.linewidth=5}return null==o&&console.log("cannot handle meshes of type "+e.meshType+" yet."),e.threeMeshId=o.id,o},GameLib.D3.prototype.invertWindingOrder=function(e){for(var t=0;t<e.length;t++){var i=e[t].v1;e[t].v1=e[t].v2,e[t].v2=i;var n=e[t].triangle.v1uv;e[t].triangle.v1uv=e[t].triangle.v2uv,e[t].triangle.v2uv=n}return e},GameLib.D3.prototype.resetWindingOrder=function(e,t){for(var i=new GameLib.D3.Vector3.Points,n=0;n<t.length;n++)i.add(new GameLib.D3.Vector3(t[n].position.x,t[n].position.y,t[n].position.z));for(var o=i.average(),s=[],a=0;a<e.length;a+=3){var r=e[a],h=e[a+1],c=e[a+2];s.push({v0:r,v1:h,v2:c,edges:[{v0:r,v1:h},{v0:h,v1:c},{v0:c,v1:r}],winding:0,edgeIndex:-1,processed:!1})}for(var p=0;p<s.length;p++)if(GameLib.D3.Vector3.clockwise(t[s[p].v0].position,t[s[p].v1].position,t[s[p].v2].position,o)){console.log("clockwise");var l=s[p].v1;s[p].v1=s[p].v2,s[p].v2=l}else console.log("not clockwise");return s},GameLib.D3.fixWindingOrder=function(e,t){function i(e,t){for(var i=0;i<e.length;i++)if(e[i].triangle.equals(t))return!0;return!1}function n(e,t,i){for(var n=0;n<t.length;n++)if(t[n].v0==e.x&&t[n].v1==e.y||t[n].v1==e.x&&t[n].v2==e.y||t[n].v2==e.x&&t[n].v0==e.y||t[n].v0==e.y&&t[n].v1==e.x||t[n].v1==e.y&&t[n].v2==e.x||t[n].v2==e.y&&t[n].v0==e.x){var o=new GameLib.D3.TriangleFace(t[n].v0,t[n].v1,t[n].v2,t[n].materialIndex,t[n].v0uv,t[n].v1uv,t[n].v2uv);if(o.equals(i))continue;return new GameLib.D3.TriangleEdge(o,e)}return null}for(var o=[new GameLib.D3.TriangleEdge(new GameLib.D3.TriangleFace(e[0].v0,e[0].v1,e[0].v2,e[0].materialIndex,e[0].v0uv,e[0].v1uv,e[0].v2uv),t)],s=[];o.length>0;){var a=o.pop();if(a.triangle.v0==a.edge.x&&a.triangle.v1==a.edge.y||a.triangle.v1==a.edge.x&&a.triangle.v2==a.edge.y||a.triangle.v2==a.edge.x&&a.triangle.v0==a.edge.y){var r=a.triangle.v1;a.triangle.v1=a.triangle.v2,a.triangle.v2=r;var h=a.triangle.v1uv;a.triangle.v1uv=a.triangle.v2uv,a.triangle.v2uv=h}s.push(a);for(var c=[new GameLib.D3.Vector2(a.triangle.v0,a.triangle.v1),new GameLib.D3.Vector2(a.triangle.v1,a.triangle.v2),new GameLib.D3.Vector2(a.triangle.v2,a.triangle.v0)],p=0;p<c.length;p++){var l=n(c[p],e,a.triangle);l&&!i(s,l.triangle)&&o.push(l)}}for(var d=[],m=0;m<s.length;m++){for(var u=!1,f=0;f<d.length;f++)if(d[f].equals(s[m].triangle)){u=!0;break}u||d.push(s[m].triangle)}return d},GameLib.D3.fixPolyZPlane=function(e,t){e.length%3==0||e.length>9||console.log("The vertices are not in the right length : "+e.length);for(var i=[],n=new GameLib.D3.Vector4.Points,o=0;o<e.length;o+=3)n.add(new GameLib.D3.Vector3(e[o],e[o+1],e[o+2]));for(n.toOrigin(),n.maximizeXDistance(t),n.maximizeYDistance(t),o=0;o<n.vectors.length;o++)i.push([n.vectors[o].x,n.vectors[o].y]);return i},GameLib.D3.Physics=function(e,t,i,n){this.id=e,this.name=t,this.engine=i,"undefined"==typeof n&&(n=[]),this.worlds=n},GameLib.D3.Physics.SPLIT_SOLVER=1,GameLib.D3.Physics.GS_SOLVER=2,GameLib.D3.PolyVertex=function(e,t,i,n,o){this.localIndex=e,this.mvertIndex=t,this.uv=i,this.materialIndex=n,this.edgeIndex=o},GameLib.D3.PolyVertex.prototype.clone=function(){return new GameLib.D3.PolyVertex(this.localIndex,this.mvertIndex,this.uv.copy(),this.materialIndex,this.edgeIndex)},GameLib.D3.RaycastVehicle=function(e,t,i,n){this.engine=e,this.engine.isNotCannonThrow(),this.componentId=GameLib.D3.Tools.RandomId(),this.chassisBody=t,"undefined"==typeof i&&(i=[]),this.wheels=i,GameLib.D3.Utils.UndefinedOrNull(n)&&(n=[]),this.wheelBodies=n,this.instance=this.createInstance(),GameLib.D3.Utils.Extend(GameLib.D3.RaycastVehicle,GameLib.D3.ComponentInterface)},GameLib.D3.RaycastVehicle.prototype.createInstance=function(){return new this.engine.instance.RaycastVehicle({chassisBody:this.chassisBody.instance})},GameLib.D3.RaycastVehicle.prototype.addWheel=function(e,t){this.wheels.push(e),this.wheelBodies.push(t),e.wheelIndex=this.instance.addWheel(e.instance)},GameLib.D3.RaycastVehicle.prototype.getWheelInfo=function(){return this.instance.wheelInfos},GameLib.D3.RaycastVehicle.prototype.onUpdate=function(e,t){for(var i=0;i<this.getWheelInfo().length;i++){this.instance.updateWheelTransform(i);var n=this.getWheelInfo()[i].worldTransform,o=this.wheelBodies[i].instance;o.position.copy(n.position),o.quaternion.copy(n.quaternion)}},GameLib.D3.RaycastVehicle.prototype.onRegistered=function(e){},GameLib.D3.RaycastWheel=function(e,t,i,n,o,s,a,r,h,c,p,l,d,m,u,f,y,L,v,b,D,G,g,w,E,T,x){this.engine=e,this.engine.isNotCannonThrow(),this.componentId=GameLib.D3.Tools.RandomId(),"undefined"!=typeof t&&null!=t||(t=new this.engine.instance.Vec3),this.chassisConnectionPointLocal=t,"undefined"!=typeof i&&null!=i||(i=new this.engine.instance.Vec3),this.chassisConnectionPointWorld=i,"undefined"!=typeof n&&null!=n||(n=new this.engine.instance.Vec3),this.directionLocal=n,"undefined"!=typeof o&&null!=o||(o=new this.engine.instance.Vec3),this.directionWorld=o,"undefined"!=typeof s&&null!=s||(s=new this.engine.instance.Vec3),this.axleLocal=s,"undefined"!=typeof a&&null!=a||(a=new this.engine.instance.Vec3),this.axleWorld=a,"undefined"!=typeof r&&null!=r||(r=1),this.suspensionRestLength=r,"undefined"!=typeof h&&null!=h||(h=2),this.suspensionMaxLength=h,"undefined"!=typeof c&&null!=c||(c=1),this.radius=c,"undefined"!=typeof p&&null!=p||(p=100),this.suspensionStiffness=p,"undefined"!=typeof l&&null!=l||(l=10),this.dampingCompression=l,"undefined"!=typeof d&&null!=d||(d=10),this.dampingRelaxation=d,"undefined"!=typeof m&&null!=m||(m=1e4),this.frictionSlip=m,"undefined"!=typeof u&&null!=u||(u=0),this.steering=u,"undefined"!=typeof f&&null!=f||(f=0),this.rotation=f,"undefined"!=typeof y&&null!=y||(y=0),this.deltaRotation=y,"undefined"!=typeof L&&null!=L||(L=.01),this.rollInfluence=L,"undefined"!=typeof v&&null!=v||(v=Number.MAX_VALUE),this.maxSuspensionForce=v,"undefined"!=typeof b&&null!=b||(b=1),this.clippedInvContactDotSuspension=b,"undefined"!=typeof D&&null!=D||(D=0),this.suspensionRelativeVelocity=D,"undefined"!=typeof G&&null!=G||(G=0),this.suspensionForce=G,"undefined"!=typeof g&&null!=g||(g=0),this.skidInfo=g,"undefined"!=typeof w&&null!=w||(w=0),this.suspensionLength=w,"undefined"!=typeof E&&null!=E||(E=1),this.maxSuspensionTravel=E,"undefined"!=typeof T&&null!=T||(T=!1),this.useCustomSlidingRotationalSpeed=T,"undefined"!=typeof x&&null!=x||(x=-.1),this.customSlidingRotationalSpeed=x,this.instance=this.createInstance(),this.wheelIndex=-1},GameLib.D3.RaycastWheel.prototype.createInstance=function(){return{chassisConnectionPointLocal:this.chassisConnectionPointLocal,chassisConnectionPointWorld:this.chassisConnectionPointWorld,directionLocal:this.directionLocal,directionWorld:this.directionWorld,axleLocal:this.axleLocal,axleWorld:this.axleWorld,suspensionRestLength:this.suspensionRestLength,suspensionMaxLength:this.suspensionMaxLength,radius:this.radius,suspensionStiffness:this.suspensionStiffness,dampingCompression:this.dampingCompression,dampingRelaxation:this.dampingRelaxation,frictionSlip:this.frictionSlip,steering:this.steering,rotation:this.rotation,deltaRotation:this.deltaRotation,rollInfluence:this.rollInfluence,maxSuspensionForce:this.maxSuspensionForce,clippedInvContactDotSuspension:this.clippedInvContactDotSuspension,suspensionRelativeVelocity:this.suspensionRelativeVelocity,suspensionForce:this.suspensionForce,skidInfo:this.skidInfo,suspensionLength:this.suspensionLength,maxSuspensionTravel:this.maxSuspensionTravel,useCustomSlidingRotationalSpeed:this.useCustomSlidingRotationalSpeed,customSlidingRotationalSpeed:this.customSlidingRotationalSpeed}},GameLib.D3.RigidBodyVehicle=function(e,t,i){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,o,s,a,r,h,c,p,l,d,m,u,f){this.componentId=GameLib.D3.Tools.RandomId(),this.position=n||new GameLib.D3.Vector3,this.velocity=s||new GameLib.D3.Vector3,this.angularVelocity=a||new GameLib.D3.Vector3,this.quaternion=o||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 p?.1:p,this.sleepTimeLimit="undefined"==typeof l?1:l,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.Utils.Extend(GameLib.D3.RigidBody,GameLib.D3.ComponentInterface)},GameLib.D3.RigidBody.prototype.createInstance=function(){var e=new this.engine.instance.Body({mass:this.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&&this.shape.instance?this.shape.instance:null});return this.instance=e,e},GameLib.D3.RigidBody.prototype.addShape=function(e,t,i){t&&"undefined"!=typeof t||(t=new GameLib.D3.Vector3(0,0,0)),i&&"undefined"!=typeof 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.RigidBody.prototype.onUpdate=function(e,t){if(t){var i=new THREE.Quaternion;i.copy(this.instance.quaternion);var n=new THREE.Vector3;n.copy(this.instance.position),t.position.x=n.x,t.position.y=n.y,t.position.z=n.z,t.quaternion.x=i.x,t.quaternion.y=i.y,t.quaternion.z=i.z,t.quaternion.w=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,o,s,a,r,h,c,p,l){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 o&&(o=new GameLib.D3.Vector4),this.quaternion=o,"undefined"==typeof s&&(s=new GameLib.D3.Vector3(0,0,0)),this.position=s,"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 p&&(p=[]),this.worlds=p,"undefined"==typeof l&&(l=[]),this.entities=l,this.instance=this.createInstance(),this.meshIdToMesh={},this.componentIdToComponent={}},GameLib.D3.Scene.prototype.createInstance=function(){var e=new THREE.Scene;return e.render=!0,e},GameLib.D3.Scene.LoadSceneFromApi=function(e,t,i,n,o,s){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",s+"/scene/load"+e.path+"/"+e.name),a.onreadystatechange=function(e){return function(){if(4==e.readyState){var s=JSON.parse(e.responseText);if(!s.scene||0==s.scene.length)return t(null,null,new Error("Could not load scene"));var a=s.scene[0],r=[];a.worlds&&a.worlds.length>0&&console.warn("Implement physics worlds code here");for(var h=[],c=0;c<a.lights.length;c++){var p=a.lights[c],l=new GameLib.D3.Light(p.id,p.lightType,p.name,new GameLib.D3.Color(p.color.r,p.color.g,p.color.b,p.color.a),p.intensity,new GameLib.D3.Vector3(p.position.x,p.position.y,p.position.z),new GameLib.D3.Vector3(p.targetPosition.x,p.targetPosition.y,p.targetPosition.z),new GameLib.D3.Vector4(p.quaternion.x,p.quaternion.y,p.quaternion.z,p.quaternion.w),new GameLib.D3.Vector3(p.rotation.x,p.rotation.y,p.rotation.z),new GameLib.D3.Vector3(p.scale.x,p.scale.y,p.scale.z),p.distance,p.decay,p.power,p.angle,p.penumbra);h.push(l)}var d=new GameLib.D3.Scene(a._id||a.id,a.path,a.name,a.meshes,new GameLib.D3.Vector4(a.quaternion.x,a.quaternion.y,a.quaternion.z,a.quaternion.w),new GameLib.D3.Vector3(a.position.x,a.position.y,a.position.z),new GameLib.D3.Vector3(a.rotation.x,a.rotation.y,a.rotation.z),new GameLib.D3.Vector3(a.scale.x,a.scale.y,a.scale.z),a.parentSceneId,h,r);GameLib.D3.Scene.LoadScene(d,t,!1,i,n,o)}}}(a),a.send()},GameLib.D3.Scene.LoadScene=function(e,t,i,n,o,s){console.log("loading scene "+e.name),n.isNotThreeThrow();for(var a=[],r=0;r<e.meshes.length;r++){var h=e.meshes[r];console.log("loading mesh "+h.name);for(var c=new n.instance.Geometry,p=h.vertices,l=h.faces,d=h.faceVertexUvs,m=h.materials,u=0;u<p.length;u++)c.vertices.push(new n.instance.Vector3(p[u].position.x,p[u].position.y,p[u].position.z));for(var f=0;f<l.length;f++){var y=new n.instance.Face3(l[f].v0,l[f].v1,l[f].v2,new n.instance.Vector3(l[f].normal.x,l[f].normal.y,l[f].normal.z),new n.instance.Color(l[f].color.r,l[f].color.g,l[f].color.b),l[f].materialIndex);y.vertexColors=[new n.instance.Color(l[f].vertexColors[0].r,l[f].vertexColors[0].g,l[f].vertexColors[0].b),new n.instance.Color(l[f].vertexColors[1].r,l[f].vertexColors[1].g,l[f].vertexColors[1].b),new n.instance.Color(l[f].vertexColors[2].r,l[f].vertexColors[2].g,l[f].vertexColors[2].b)],y.normal=new n.instance.Vector3(l[f].normal.x,l[f].normal.y,l[f].normal.z),y.vertexNormals=[new n.instance.Vector3(l[f].vertexNormals[0].x,l[f].vertexNormals[0].y,l[f].vertexNormals[0].z),new n.instance.Vector3(l[f].vertexNormals[1].x,l[f].vertexNormals[1].y,l[f].vertexNormals[1].z),new n.instance.Vector3(l[f].vertexNormals[2].x,l[f].vertexNormals[2].y,l[f].vertexNormals[2].z)],c.faces.push(y)}c.faceVertexUvs=[];for(var L=0;L<d.length;L++){var v=d[L];c.faceVertexUvs[L]=[];for(var b=0;b<v.length;b++)c.faceVertexUvs[L][b]=[],c.faceVertexUvs[L][b].push(new n.instance.Vector2(v[b][0].x,v[b][0].y),new n.instance.Vector2(v[b][1].x,v[b][1].y),new n.instance.Vector2(v[b][2].x,v[b][2].y))}i&&(c.computeFaceNormals(),c.computeVertexNormals());for(var D=[],G=0;G<m.length;G++)D.push(GameLib.D3.Material.createInstanceMaterial(m[G],n,o,s));var g=Q.all(D).then(function(e,t){return function(i){console.log("loaded material : "+i[0].name);var o=i[0],s=GameLib.D3.Mesh.createInstanceMesh(e,t,o,n);return s.name=e.name,s.position.x=e.position.x,s.position.y=e.position.y,s.position.z=e.position.z,s.rotation.x=e.rotation.x,s.rotation.y=e.rotation.y,s.rotation.z=e.rotation.z,s.scale.x=e.scale.x,s.scale.y=e.scale.y,s.scale.z=e.scale.z,s.quaternion.x=e.quaternion.x,s.quaternion.y=e.quaternion.y,s.quaternion.z=e.quaternion.z,s.quaternion.w=e.quaternion.w,s}}(h,c)).catch(function(e){console.log(e)});a.push(g)}Q.all(a).then(function(i){if(console.log("all meshes have loaded"),"undefined"!=typeof t){for(var o=[],s=0;s<e.lights.length;s++){var a=e.lights[s],r=null;"AmbientLight"==a.lightType&&(r=new n.instance.AmbientLight(a.color,a.intensity)),"DirectionalLight"==a.lightType&&(r=new n.instance.DirectionalLight(a.color,a.intensity)),"PointLight"==a.lightType&&(r=new n.instance.PointLight(a.color,a.intensity),r.distance=a.distance,r.decay=a.decay),"SpotLight"==a.lightType&&(r=new n.instance.SpotLight(a.color,a.intensity),r.distance=a.distance,r.angle=a.angle,r.penumbra=a.penumbra,r.decay=a.decay),r.position.x=a.position.x,r.position.y=a.position.y,r.position.z=a.position.z,r.rotation.x=a.rotation.x,r.rotation.y=a.rotation.y,r.rotation.z=a.rotation.z,null==r?console.warn("Does not support lights of type :"+a.lightType+", not imported"):(r.name=a.name,o.push(r))}var h=new n.instance.Scene;h.name=e.name,h.position.x=e.position.x,h.position.y=e.position.y,h.position.z=e.position.z,h.rotation.x=e.rotation.x,h.rotation.y=e.rotation.y,h.rotation.z=e.rotation.z,h.scale.x=e.scale.x,h.scale.y=e.scale.y,h.scale.z=e.scale.z,h.quaternion.x=e.quaternion.x,h.quaternion.y=e.quaternion.y,h.quaternion.z=e.quaternion.z,h.quaternion.w=e.quaternion.w;for(var c=0;c<i.length;c++)h.add(i[c]);for(var p=0;p<o.length;p++)h.add(o[p]);t(e,{scene:h,lights:o,meshes:i})}}).catch(function(e){console.log(e)})},GameLib.D3.Scene.prototype.update=function(e){for(var t in this.entities)this.entities[t].update(e)},GameLib.D3.Scene.prototype.lateUpdate=function(e){for(var t in this.entities)this.entities[t].lateUpdate(e)},GameLib.D3.Scene.prototype.render=function(e,t,i){t.render(this.instance,i)},GameLib.D3.Scene.prototype.registerEntity=function(e){this.entities.push(e),e.register(this);for(var t in e.componentIds){var i=e.componentIds[t],n=this.componentIdToComponent[i];n&&n.setParentEntity(this,e)}},GameLib.D3.Scene.prototype.registerComponent=function(e){this.componentIdToComponent[e.componentId]=e,e.onRegistered&&"function"==typeof e.onRegistered&&e.onRegistered(this)},GameLib.D3.Scene.prototype.registerLight=function(e){this.instance.add(e)},GameLib.D3.Shape=function(e,t,i,n,o,s,a,r,h,c,p,l){this.engine=e,this.engine.isNotCannonThrow(),this.shapeType=t,"undefined"==typeof i&&(i=new GameLib.D3.Vector3(1,1,1)),this.scale=i,"undefined"==typeof n&&(n=[]),this.vertices=n,"undefined"==typeof o&&(o=[]),this.indices=o,"undefined"==typeof s&&(s=1),this.radius=s,"undefined"==typeof a&&(a=new GameLib.D3.Vector3(1,1,1)),this.halfExtensions=a,"undefined"==typeof r&&(r=1),this.radiusTop=r,"undefined"==typeof h&&(h=1),this.radiusBottom=h,"undefined"==typeof c&&(c=1),this.height=c,"undefined"==typeof p&&(p=1),this.numSegments=p,"undefined"==typeof l&&(l=new GameLib.D3.Heightmap),this.heightmap=l,this.instance=this.createInstance()},GameLib.D3.Shape.SHAPE_TYPE_SPHERE=1,GameLib.D3.Shape.SHAPE_TYPE_BOX=2,GameLib.D3.Shape.SHAPE_TYPE_TRIMESH=3,GameLib.D3.Shape.SHAPE_TYPE_CYLINDER=4,GameLib.D3.Shape.SHAPE_TYPE_HEIGHT_MAP=5,GameLib.D3.Shape.SHAPE_TYPE_CONVEX_HULL=6,GameLib.D3.Shape.prototype.createInstance=function(){var e=null;if(this.shapeType==GameLib.D3.Shape.SHAPE_TYPE_TRIMESH)e=new this.engine.instance.Trimesh(this.vertices,this.indices);else if(this.shapeType==GameLib.D3.Shape.SHAPE_TYPE_SPHERE)e=new this.engine.instance.Sphere(this.radius);else if(this.shapeType==GameLib.D3.Shape.SHAPE_TYPE_BOX)e=new this.engine.instance.Box(new this.engine.instance.Vec3(this.halfExtensions.x,this.halfExtensions.y,this.halfExtensions.z));else if(this.shapeType==GameLib.D3.Shape.SHAPE_TYPE_CYLINDER)e=new this.engine.instance.Cylinder(this.radiusTop,this.radiusBottom,this.height,this.numSegments);else{if(this.shapeType!=GameLib.D3.Shape.SHAPE_TYPE_HEIGHT_MAP)throw this.shapeType==GameLib.D3.Shape.SHAPE_TYPE_CONVEX_HULL?(console.warn("Shape type not implemented: "+this.shapeType),new Error("Shape type not implemented: "+this.shapeType)):(console.warn("Shape type not implemented: "+this.shapeType),new Error("Shape type not implemented: "+this.shapeType));e=new this.engine.instance.Heightfield(this.heightmap.matrix,{elementSize:this.heightmap.elementSize})}return this.instance=e,e},GameLib.D3.Shape.prototype.update=function(e){e.isNotCannonThrow(),this.shapeType===GameLib.D3.Shape.SHAPE_TYPE_TRIMESH&&(this.instance.setScale(new e.instance.Vec3(this.scale.x,this.scale.y,this.scale.z)),this.instance.updateAABB(),this.instance.updateNormals(),this.instance.updateEdges(),this.instance.updateBoundingSphereRadius(),this.instance.updateTree())},GameLib.D3.Skeleton=function(e,t,i,n,o,s,a,r){this.id=e,this.bones=t,"undefined"==typeof i&&(i=[]),this.boneInverses=i,"undefined"==typeof n&&(n=!1),this.useVertexTexture=n,1==this.useVertexTexture&&console.warn("support for vertex texture bones is not supported yet - something could break somewhere"),"undefined"==typeof o&&(o=0),this.boneTextureWidth=o,"undefined"==typeof s&&(s=0),this.boneTextureHeight=s,"undefined"==typeof a&&(a=[]),this.boneMatrices=a,"undefined"==typeof r&&(r=[]),this.boneTexture=r},GameLib.D3.SkyBox=function(e){this.id=null,this.graphics=e,this.graphics.isNotThreeThrow(),this.texturesFolder=null},GameLib.D3.SkyBox.prototype.load=function(e){this.texturesFolder=e,this.textures=[],this.materials=[],this.mesh={},this.scene=new this.graphics.instance.Scene,this.textureCube=null;var t=new this.graphics.instance.TextureLoader;this.textures.push(t.load(this.texturesFolder+"px.png")),this.textures.push(t.load(this.texturesFolder+"nx.png")),this.textures.push(t.load(this.texturesFolder+"py.png")),this.textures.push(t.load(this.texturesFolder+"ny.png")),this.textures.push(t.load(this.texturesFolder+"pz.png")),this.textures.push(t.load(this.texturesFolder+"nz.png"));for(var i=0;i<6;i++)this.materials.push(new this.graphics.instance.MeshBasicMaterial({map:this.textures[i]}));this.mesh=new this.graphics.instance.Mesh(new this.graphics.instance.CubeGeometry(1,1,1),new this.graphics.instance.MeshFaceMaterial(this.materials)),this.mesh.applyMatrix((new this.graphics.instance.Matrix4).makeScale(1,1,-1)),this.scene.add(this.mesh),this.textureCube=(new this.graphics.instance.CubeTextureLoader).load([this.texturesFolder+"px.png",this.texturesFolder+"nx.png",this.texturesFolder+"py.png",this.texturesFolder+"ny.png",this.texturesFolder+"pz.png",this.texturesFolder+"nz.png"])},GameLib.D3.SkyBox.prototype.render=function(e,t){var i=new this.graphics.instance.Vector3(t.position.x,t.position.y,t.position.z);t.position.set(0,0,0);var n=e.context;n.disable(n.DEPTH_TEST),e.render(this.scene,t),n.enable(n.DEPTH_TEST),t.position.copy(i)},GameLib.D3.Solver=function(e,t,i,n,o,s){this.id=e,this.engine=t,this.engine.isNotCannonThrow(),"undefined"==typeof i&&(i=GameLib.D3.Solver.GS_SOLVER),this.solverType=i,"undefined"==typeof n&&(n=this.solverType==GameLib.D3.Solver.SPLIT_SOLVER?"split solver":this.solverType==GameLib.D3.Solver.GS_SOLVER?"gs solver":"unknown solver"),this.name=n,"undefined"==typeof o&&(o=10),this.iterations=o,"undefined"==typeof s&&(s=1e-7),this.tolerance=s,this.instance=this.createInstance(),this.instance.tolerance=s,this.instance.iterations=o},GameLib.D3.Solver.prototype.createInstance=function(){var e=null;if(this.solverType==GameLib.D3.Solver.SPLIT_SOLVER)e=new this.engine.instance.SplitSolver;else{if(this.solverType!=GameLib.D3.Solver.GS_SOLVER)throw console.warn("Unsupported solver type: "+this.solverType),new Error("Unsupported solver type: "+this.solverType);e=new this.engine.instance.GSSolver}return e},GameLib.D3.Solver.SPLIT_SOLVER=1,GameLib.D3.Solver.GS_SOLVER=2,GameLib.D3.Texture=function(e,t,i,n,o,s,a,r,h,c,p,l,d,m,u,f,y,L,v,b){this.id=e,this.name=t,this.image=i,"undefined"==typeof n&&(n=GameLib.D3.Texture.TYPE_REPEAT_WRAPPING),this.wrapS=n,"undefined"==typeof o&&(o=GameLib.D3.Texture.TYPE_REPEAT_WRAPPING),this.wrapT=o,"undefined"==typeof s&&(s=new GameLib.D3.Vector2(1,1)),this.repeat=s,"undefined"==typeof a&&(a=null),this.data=a,"undefined"==typeof r&&(r=GameLib.D3.Texture.TYPE_RGBA_FORMAT),this.format=r,"undefined"==typeof h&&(h=GameLib.D3.Texture.TYPE_UV_MAPPING),this.mapping=h,"undefined"==typeof c&&(c=GameLib.D3.Texture.TYPE_LINEAR_FILTER),this.magFilter=c,"undefined"==typeof p&&(p=GameLib.D3.Texture.TYPE_LINEAR_MIPMAP_LINEAR_FILTER),this.minFilter=p,"undefined"==typeof l&&(l=GameLib.D3.Texture.TYPE_UNSIGNED_BYTE),this.textureType=l,"undefined"==typeof d&&(d=1),this.anisotropy=d,"undefined"==typeof m&&(m=new GameLib.D3.Vector2(0,0)),this.offset=m,"undefined"==typeof u&&(u=!0),this.generateMipmaps=u,"undefined"==typeof f&&(f=!0),this.flipY=f,"undefined"==typeof y&&(y=[]),this.mipmaps=y,"undefined"==typeof L&&(L=4),this.unpackAlignment=L,"undefined"==typeof v&&(v=!1),this.premultiplyAlpha=v,"undefined"==typeof b&&(b=GameLib.D3.Texture.TYPE_LINEAR_ENCODING),this.encoding=b},GameLib.D3.Texture.TYPE_ALPHA_FORMAT=1019,GameLib.D3.Texture.TYPE_RGB_FORMAT=1020;GameLib.D3.Texture.TYPE_RGBA_FORMAT=1021;GameLib.D3.Texture.TYPE_LUMINANCE_FORMAT=1022,GameLib.D3.Texture.TYPE_LUMINANCE_ALPHA_FORMAT=1023,GameLib.D3.Texture.TYPE_DEPTH_FORMAT=1026,GameLib.D3.Texture.TYPE_UV_MAPPING=300,GameLib.D3.Texture.TYPE_CUBE_REFLECTION_MAPPING=301,GameLib.D3.Texture.TYPE_CUBE_REFRACTION_MAPPING=302,GameLib.D3.Texture.TYPE_EQUI_RECTANGULAR_REFLECTION_MAPPING=303,GameLib.D3.Texture.TYPE_EQUI_RECTANGULAR_REFRACTION_MAPPING=304,GameLib.D3.Texture.TYPE_SPHERICAL_REFLECTION_MAPPING=305,GameLib.D3.Texture.TYPE_CUBE_UV_REFLECTION_MAPPING=306,GameLib.D3.Texture.TYPE_CUBE_UV_REFRACTION_MAPPING=307,GameLib.D3.Texture.TYPE_REPEAT_WRAPPING=1e3,GameLib.D3.Texture.TYPE_CLAMP_TO_EDGE_WRAPPING=1001,GameLib.D3.Texture.TYPE_MIRRORED_REPEAT_WRAPPING=1002,GameLib.D3.Texture.TYPE_NEAREST_FILTER=1003,GameLib.D3.Texture.TYPE_NEAREST_MIPMAP_NEAREST_FILTER=1004,GameLib.D3.Texture.TYPE_NEAREST_MIPMAP_LINEAR_FILTER=1005,GameLib.D3.Texture.TYPE_LINEAR_FILTER=1006,GameLib.D3.Texture.TYPE_LINEAR_MIPMAP_NEAREST_FILTER=1007,GameLib.D3.Texture.TYPE_LINEAR_MIPMAP_LINEAR_FILTER=1008,GameLib.D3.Texture.TYPE_UNSIGNED_BYTE=1009,GameLib.D3.Texture.TYPE_BYTE=1010,GameLib.D3.Texture.TYPE_SHORT=1011,GameLib.D3.Texture.TYPE_UNSIGNED_SHORT=1012,GameLib.D3.Texture.TYPE_INT=1013,GameLib.D3.Texture.TYPE_UNSIGNED_INT=1014,GameLib.D3.Texture.TYPE_FLOAT=1015,GameLib.D3.Texture.TYPE_HALF_FLOAT=1025,GameLib.D3.Texture.TYPE_LINEAR_ENCODING=3e3,GameLib.D3.Texture.TYPE_SRGB_ENCODING=3001,GameLib.D3.Texture.TYPE_GAMMA_ENCODING=3007,GameLib.D3.Texture.TYPE_RGBE_ENCODING=3002,GameLib.D3.Texture.TYPE_LOG_LUV_ENCODING=3003,GameLib.D3.Texture.TYPE_RGBM7_ENCODING=3004,GameLib.D3.Texture.TYPE_RGBM16_ENCODING=3005,GameLib.D3.Texture.TYPE_RGBD_ENCODING=3006,GameLib.D3.Texture.loadMap=function(e,t,i,n,o,s){var a=Q.defer(),r=null,h=new n.instance.TextureLoader;return e&&e.image&&e.image.filename&&(r=o+"/"+e.image.filename),r?(h.crossOrigin="",h.load(r,function(o){t[i]=o,t[i].name=e.name,t[i].anisotropy=e.anisotropy,t[i].encoding=e.encoding,t[i].flipY=e.flipY,t[i].generateMipmaps=e.generateMipmaps,t[i].magFilter=e.magFilter,t[i].minFilter=e.minFilter,t[i].mapping=e.mapping,t[i].mipmaps=e.mipmaps,t[i].offset=new n.instance.Vector2(e.offset.x,e.offset.y),t[i].premultiplyAlpha=e.premultiplyAlpha,t[i].textureType=e.textureType,t[i].wrapS=e.wrapS,t[i].wrapT=e.wrapT,t[i].unpackAlignment=e.unpackAlignment,t.needsUpdate=!0,a.resolve(!0)},function(e){s&&s(Math.round(e.loaded/e.total*100))},function(e){console.log("an error occurred while trying to load the image : "+r),a.resolve(null)})):a.resolve(null),a.promise},GameLib.D3.Texture.loadMaps=function(e,t,i,n,o,s){for(var a=[],r=0;r<t.length;r++){var h=t[r];if(e.maps.hasOwnProperty(h)){var c=e.maps[h];if(c&&c.image&&c.image.filename){var p=null;"alpha"==h&&(p="alhpaMap"),"ao"==h&&(p="aoMap"),"bump"==h&&(p="bumpMap"),"displacement"==h&&(p="displacementMap"),"emissive"==h&&(p="emissiveMap"),"environment"==h&&(p="envMap"),"light"==h&&(p="lightMap"),"specular"==h&&(p="specularMap"),"diffuse"==h&&(p="map"),"roughness"==h&&(p="roughnessMap"),"metalness"==h&&(p="metalnessMap"),null==p&&console.warn("unsupported map type : "+h),a.push(GameLib.D3.Texture.loadMap(e.maps[h],i,p,n,o,s))}}}return a},GameLib.D3.Tools=function(){},GameLib.D3.Tools.RandomId=function(){return Math.random().toString(36).substr(2,10)},GameLib.D3.TriangleEdge=function(e,t){this.triangle=e,this.edge=t},GameLib.D3.TriangleFace=function(e,t,i,n,o,s,a,r,h,c,p){this.v0=e,this.v1=t,this.v2=i,this.materialIndex=n,this.v0uv=o,this.v1uv=s,this.v2uv=a,r||(r=new GameLib.D3.Color(255,255,255,255)),this.color=r,h||(h=[new GameLib.D3.Color(255,255,255,255),new GameLib.D3.Color(255,255,255,255),new GameLib.D3.Color(255,255,255,255)]),this.vertexColors=h,c||(c=[new GameLib.D3.Vector3,new GameLib.D3.Vector3,new GameLib.D3.Vector3]),this.vertexNormals=c,p||(p=new GameLib.D3.Vector3(0)),this.normal=p},GameLib.D3.TriangleFace.prototype.clone=function(){return new GameLib.D3.TriangleFace(this.v0,this.v1,this.v2,this.materialIndex,this.v0uv.copy(),this.v1uv.copy(),this.v2uv.copy())},GameLib.D3.TriangleFace.prototype.equals=function(e){return!!(this.v0==e.v0&&this.v1==e.v1&&this.v2==e.v2||this.v0==e.v0&&this.v1==e.v2&&this.v2==e.v1||this.v0==e.v1&&this.v1==e.v0&&this.v2==e.v2||this.v0==e.v1&&this.v1==e.v2&&this.v2==e.v0||this.v0==e.v2&&this.v1==e.v0&&this.v2==e.v1||this.v0==e.v2&&this.v1==e.v1&&this.v2==e.v0)},GameLib.D3.Utils=function(){},GameLib.D3.Utils.Extend=function(e,t){for(var i in t.prototype)e.prototype[i]||(e.prototype[i]=t.prototype[i]);
},GameLib.D3.Utils.UndefinedOrNull=function(e){return"undefined"==typeof e||null==e},GameLib.D3.Utils.Raycast=function(e,t,i,n){console.log("not implemented yet")},GameLib.D3.Vector2=function(e,t){this.x=0,this.y=0,e&&(this.x=e),t&&(this.y=t)},GameLib.D3.Vector2.prototype.copy=function(){return new GameLib.D3.Vector2(this.x,this.y)},GameLib.D3.Vector2.prototype.equals=function(e){return!!(this.x==e.x&&this.y==e.y||this.y==e.x&&this.x==e.y)},GameLib.D3.Vector3=function(e,t,i){this.x=0,this.y=0,this.z=0,e&&(this.x=e),t&&(this.y=t),i&&(this.z=i)},GameLib.D3.Vector3.prototype.subtract=function(e){return e instanceof GameLib.D3.Vector3&&(this.x-=e.x,this.y-=e.y,this.z-=e.z),e instanceof GameLib.D3.Vector4&&console.warn("trying to subtract vector of bigger length (4 vs 3))"),this},GameLib.D3.Vector3.prototype.cross=function(e){return new GameLib.D3.Vector3(this.y*e.z-this.z*e.y,this.z*e.x-this.x*e.z,this.x*e.y-this.y*e.x)},GameLib.D3.Vector3.prototype.negative=function(){return this.x*=-1,this.y*=-1,this.z*=-1,this},GameLib.D3.Vector3.clockwise=function(e,t,i,n){var o=GameLib.D3.Vector3.normal(e,t,i),s=e.copy(),a=o.dot(s.subtract(n));return a>0},GameLib.D3.Vector3.normal=function(e,t,i){var n=t.copy(),o=i.copy();return n.subtract(e).cross(o.subtract(e))},GameLib.D3.Vector3.prototype.lookAt=function(e,t){var i=GameLib.D3.Matrix4.lookAt(this,e,t);this.multiply(i)},GameLib.D3.Vector3.prototype.translate=function(e){return this.x+=e.x,this.y+=e.y,this.z+=e.z,this},GameLib.D3.Vector3.prototype.squared=function(){return this.x*this.x+this.y*this.y+this.z*this.z},GameLib.D3.Vector3.prototype.copy=function(){return new GameLib.D3.Vector3(this.x,this.y,this.z)},GameLib.D3.Vector3.prototype.multiply=function(e){if(e instanceof GameLib.D3.Vector3)this.x*=e.x,this.y*=e.y,this.z*=e.z;else{if(!(e instanceof GameLib.D3.Matrix4))throw console.log("functionality not implemented - please do this"),new Error("not implemented");var t=e.rows[0].x*this.x+e.rows[0].y*this.y+e.rows[0].z*this.z,i=e.rows[1].x*this.x+e.rows[1].y*this.y+e.rows[1].z*this.z,n=e.rows[2].x*this.x+e.rows[2].y*this.y+e.rows[2].z*this.z;this.x=t,this.y=i,this.z=n}return this},GameLib.D3.Vector3.prototype.dot=function(e){return this.x*e.x+this.y*e.y+this.z*e.z},GameLib.D3.Vector3.prototype.normalize=function(){var e=1e-6,t=this.squared();if(t<e)return this;var i=1/Math.sqrt(t);return this.x*=i,this.y*=i,this.z*=i,this},GameLib.D3.Vector4=function(e,t,i,n){this.x=0,this.y=0,this.z=0,this.w=0,e&&(this.x=e),t&&(this.y=t),i&&(this.z=i),n&&(this.w=n)},GameLib.D3.Vector4.prototype.translate=function(e){return this.x+=e.x,this.y+=e.y,this.z+=e.z,this},GameLib.D3.Vector4.prototype.copy=function(){return new GameLib.D3.Vector4(this.x,this.y,this.z,this.w)},GameLib.D3.Vector4.prototype.multiply=function(e){if(e instanceof GameLib.D3.Vector3)this.x*=e.x,this.y*=e.y,this.z*=e.z;else{if(!(e instanceof GameLib.D3.Matrix4))throw console.log("functionality not implemented - please do this"),new Error("not implemented");var t=e.rows[0].x*this.x+e.rows[0].y*this.y+e.rows[0].z*this.z+e.rows[0].w*this.w,i=e.rows[1].x*this.x+e.rows[1].y*this.y+e.rows[1].z*this.z+e.rows[1].w*this.w,n=e.rows[2].x*this.x+e.rows[2].y*this.y+e.rows[2].z*this.z+e.rows[2].w*this.w,o=e.rows[3].x*this.x+e.rows[3].y*this.y+e.rows[3].z*this.z+e.rows[3].w*this.w;this.x=t,this.y=i,this.z=n,this.w=o}},GameLib.D3.Vector4.prototype.normalize=function(){var e=1e-6,t=this.x*this.x+this.y*this.y+this.z*this.z;if(t<e)return this;var i=1/Math.sqrt(t);return this.x*=i,this.y*=i,this.z*=i,this},GameLib.D3.Vector4.prototype.subtract=function(e){return e instanceof GameLib.D3.Vector3&&(this.x-=e.x,this.y-=e.y,this.z-=e.z),e instanceof GameLib.D3.Vector4&&(this.x-=e.x,this.y-=e.y,this.z-=e.z,this.w-=e.w),this},GameLib.D3.Vector4.Points=function(){this.vectors=[]},GameLib.D3.Vector4.Points.prototype.add=function(e){if(e instanceof GameLib.D3.Vector3&&(e=new GameLib.D3.Vector4(e.x,e.y,e.z,1)),!e instanceof GameLib.D3.Vector4)throw console.warn("Vector needs to be of type Vector4"),new Error("Vector needs to be of type Vector4");return this.vectors.push(e),this},GameLib.D3.Vector4.Points.prototype.copy=function(){for(var e=[],t=0;t<this.vectors.length;t++)e.push(this.vectors[t].copy());return e},GameLib.D3.Vector4.Points.prototype.maximizeXDistance=function(e){for(var t=0,i=(new GameLib.D3.Matrix4).rotationMatrixY(e),n=0,o=this.copy(),s=0,a=0;a<2*Math.PI;a+=e){t++;for(var r=0;r<this.vectors.length;r++)this.vectors[r]=i.multiply(this.vectors[r]);var h=this.distances();h.x>s&&(s=h.x,n=t*e)}this.vectors=o;for(var c=(new GameLib.D3.Matrix4).rotationMatrixY(n),p=0;p<this.vectors.length;p++)this.vectors[p]=c.multiply(this.vectors[p])},GameLib.D3.Vector4.Points.prototype.maximizeYDistance=function(e){for(var t=0,i=(new GameLib.D3.Matrix4).rotationMatrixX(e),n=0,o=this.copy(),s=0,a=0;a<2*Math.PI;a+=e){t++;for(var r=0;r<this.vectors.length;r++)this.vectors[r]=i.multiply(this.vectors[r]);var h=this.distances();h.y>s&&(s=h.y,n=t*e)}this.vectors=o;for(var c=(new GameLib.D3.Matrix4).rotationMatrixX(n),p=0;p<this.vectors.length;p++)this.vectors[p]=c.multiply(this.vectors[p])},GameLib.D3.Vector4.Points.prototype.lookAt=function(e,t){var i=this.average();console.log("poly center : "+JSON.stringify(i));var n=(new GameLib.D3.Matrix4).lookAt(i,e,t);n.rows[0]=new GameLib.D3.Vector4(1,0,0,0),n.rows[1]=new GameLib.D3.Vector4(0,0,1,0),n.rows[2]=new GameLib.D3.Vector4(0,1,0,0),console.log("look at matrix : "+JSON.stringify(n,null,2));for(var o=0;o<this.vectors.length;o++)console.log("vector "+o+" (before): "+JSON.stringify(this.vectors[o])),this.vectors[o]=n.multiply(this.vectors[o]),console.log("vector "+o+" (after) : "+JSON.stringify(this.vectors[o]))},GameLib.D3.Vector4.Points.prototype.distances=function(){for(var e=this.vectors[0].x,t=this.vectors[0].y,i=this.vectors[0].z,n=this.vectors[0].x,o=this.vectors[0].y,s=this.vectors[0].z,a=0;a<this.vectors.length;a++)this.vectors[a].x<e&&(e=this.vectors[a].x),this.vectors[a].y<t&&(t=this.vectors[a].y),this.vectors[a].z<i&&(i=this.vectors[a].z),this.vectors[a].x>n&&(n=this.vectors[a].x),this.vectors[a].y>o&&(o=this.vectors[a].y),this.vectors[a].z>s&&(s=this.vectors[a].z);return new GameLib.D3.Vector3(Math.abs(n-e),Math.abs(o-t),Math.abs(o-i))},GameLib.D3.Vector4.Points.prototype.average=function(){for(var e=0,t=0,i=0,n=0;n<this.vectors.length;n++)e+=this.vectors[n].x,t+=this.vectors[n].y,i+=this.vectors[n].z;return new GameLib.D3.Vector3(e/this.vectors.length,t/this.vectors.length,i/this.vectors.length)},GameLib.D3.Vector4.Points.prototype.negative=function(){for(var e=0;e<this.vectors.length;e++)this.vectors[e].x*=-1,this.vectors[e].y*=-1,this.vectors[e].z*=-1;return this},GameLib.D3.Vector4.Points.prototype.toOrigin=function(){for(var e=this.average().negative(),t=0;t<this.vectors.length;t++)this.vectors[t].translate(e)},GameLib.D3.Vertex=function(e,t){this.position=e,this.boneWeights=t},GameLib.D3.World=function(e,t,i,n,o,s,a){this.id=e,this.name=t,"undefined"==typeof n&&(n=new GameLib.D3.Vector3(0,-9.81,0)),this.gravity=n,"undefined"==typeof o&&(o=new GameLib.D3.Broadphase(null,"broadPhaseNaive",GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE,i)),this.broadphase=o,"undefined"==typeof s&&(s=new GameLib.D3.Solver(null,i,GameLib.D3.Solver.GS_SOLVER)),this.solver=s,"undefined"==typeof a&&(a=[]),this.rigidBodies=a,this.engine=i,this.engine.isNotCannonThrow(),this.instance=this.createInstance()},GameLib.D3.World.prototype.createInstance=function(){var e=new this.engine.instance.World;return e.broadphase=this.broadphase.instance,e.solver=this.solver.instance,e.gravity.x=this.gravity.x,e.gravity.y=this.gravity.y,e.gravity.z=this.gravity.z,e.name=this.name,e},GameLib.D3.World.prototype.addRigidBody=function(e){this.instance.addBody(e.instance)},GameLib.D3.World.prototype.addVehicle=function(e){e.instance.addToWorld(this.instance)},GameLib.D3.World.prototype.step=function(e){var t=performance.now()/1e3;if(!this.lastCallTime)return this.instance.step(e),void(this.lastCallTime=t);var i=t-this.lastCallTime;this.instance.step(e,i),this.lastCallTime=t},GameLib.D3.World.prototype.GetIndexedVertices=function(e){return this.engine.engineType==GameLib.D3.Physics.TYPE_CANNON?{vertices:e.vertices,indices:e.indices}:null},GameLib.D3.World.prototype.generateWireframeViewTriangleMesh=function(e,t,i,n,o,s){e.isNotThreeThrow(),this.engine.isNotCannonThrow(),"undefined"==typeof i&&(i=10),"undefined"==typeof n&&(n=new e.instance.Vector3(1,1,1)),"undefined"==typeof o&&(o=.5),"undefined"==typeof s&&(s=16711422);for(var a=new e.instance.Geometry,r=new e.instance.Mesh(a,new e.instance.MeshBasicMaterial({color:s,wireframe:!0,opacity:o})),h=0,c=t.instance.vertices.length/3;h<c;++h)a.vertices.push(new e.instance.Vector3(t.instance.vertices[3*h],t.instance.vertices[3*h+1],t.instance.vertices[3*h+2]));for(var p=0,c=t.instance.indices.length/3;p<c;++p){var l=t.instance.indices[3*p],d=t.instance.indices[3*p+1],m=t.instance.indices[3*p+2];a.faces.push(new e.instance.Face3(l,d,m));var u=(new e.instance.Vector3).add(a.vertices[l]).add(a.vertices[d]).add(a.vertices[m]).divideScalar(3),f=new this.engine.instance.Vec3;t.instance.getNormal(p,f);var y=new e.instance.ArrowHelper(new e.instance.Vector3(f.x,f.y,f.z),u,i,new e.instance.Color(f.x,f.y,f.z));r.add(y)}return r.scale.x=n.x,r.scale.y=n.y,r.scale.z=n.z,r},GameLib.D3.World.prototype.generateWireframeViewConvexPolyMesh=function(e,t,i,n,o,s){e.isNotThreeThrow(),this.engine.isNotCannonThrow(),"undefined"==typeof i&&(i=10),"undefined"==typeof n&&(n=new e.instance.Vector3(1,1,1)),"undefined"==typeof o&&(o=.5),"undefined"==typeof s&&(s=16711422);for(var a=new e.instance.Geometry,r=new e.instance.Mesh(a,new e.instance.MeshBasicMaterial({color:s,wireframe:!0,opacity:o})),h=0,c=t.instance.vertices.length;h<c;h++){var p=t.instance.vertices[h];a.vertices.push(new e.instance.Vector3(p.x,p.y,p.z))}for(var h=0,c=t.instance.faces.length;h<c;h++){var l=t.instance.faces[h],d=l[0],m=l[1],u=l[2];a.faces.push(new e.instance.Face3(d,m,u));var f=(new e.instance.Vector3).add(a.vertices[d]).add(a.vertices[m]).add(a.vertices[u]).divideScalar(3),y=t.instance.faceNormals[h],L=new e.instance.Vector3(y.x,y.y,y.z),v=new e.instance.ArrowHelper(L,f,i,new e.instance.Color(L.x,L.y,L.z));r.add(v)}return r.scale.x=n.x,r.scale.y=n.y,r.scale.z=n.z,r},GameLib.D3.World.prototype.generateTriangleMeshShapeDivided=function(e,t,i,n,o,s,a){e.isNotThreeThrow(),this.engine.isNotCannonThrow(),null!=i&&"undefined"!=typeof i||(i=0),null!=n&&"undefined"!=typeof n||(n=10),null!=o&&"undefined"!=typeof o||(o=!1);for(var r=0,h=s||0,c=a||0,p=t.geometry.faces.length,l=o?c*h:p,d=[],m=[],u=[],f=0;f<=p;f++){if(r==l||f==p){var y=null,L=new this.engine.instance.Trimesh(m,u);if(L.setScale(new this.engine.instance.Vec3(t.scale.x,t.scale.y,t.scale.z)),L.updateAABB(),L.updateNormals(),L.updateEdges(),L.updateBoundingSphereRadius(),L.updateTree(),y=new this.engine.instance.Body({mass:i,friction:n}),y.addShape(L),d.push({threeObject:o?null:t,physicsObject:y}),m=[],u=[],r=0,f==p)return d}var v=t.geometry.faces[f];u.push(u.length),u.push(u.length),u.push(u.length);var b=t.geometry.vertices[v.a],D=t.geometry.vertices[v.b],G=t.geometry.vertices[v.c];m.push(b.x,b.y,b.z),m.push(D.x,D.y,D.z),m.push(G.x,G.y,G.z),r++}},GameLib.D3.World.prototype.generateTriangleMeshShape=function(e,t){for(var i=[],n=0,o=t.geometry.vertices.length;n<o;n++){var s=t.geometry.vertices[n];i.push(new this.engine.instance.Vec3(s.x*t.scale.x,s.y*t.scale.y,s.z*t.scale.z))}for(var a=[],r=0,h=t.geometry.faces.length;r<h;r++){var c=t.geometry.faces[r].a,p=t.geometry.faces[r].b,l=t.geometry.faces[r].c;a.push([c,p,l])}for(var d={},m=[],u=[],f=0,y=a.length,L=0,r=0;r<y;r++){var c=a[r][0],p=a[r][1],l=a[r][2];"undefined"==typeof d[c]&&(m.push(i[c].x,i[c].y,i[c].z),d[c]=L,L++),"undefined"==typeof d[p]&&(m.push(i[p].x,i[p].y,i[p].z),d[p]=L,L++),"undefined"==typeof d[l]&&(m.push(i[l].x,i[l].y,i[l].z),d[l]=L,L++),u.push(d[c],d[p],d[l]),f++}return new GameLib.D3.Shape(this.engine,GameLib.D3.Shape.SHAPE_TYPE_TRIMESH,{x:1,y:1,z:1},m,u)},GameLib.D3.World.prototype.fixupTriangleMeshShape=function(e,t,i,n,o,s,a){this.engine.isNotCannonThrow(),s.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=[],p=0,l=0,d=0,m=[];for(var u in t){var f=t[u].instance,y=[];p+=f.indices.length/3;for(var L=0;L<f.indices.length/3;L++){var v=f.indices[3*L],b=f.indices[3*L+1],D=f.indices[3*L+2],G=(new s.instance.Vector3).add(new s.instance.Vector3(f.vertices[3*v],f.vertices[3*v+1],f.vertices[3*v+2])).add(new s.instance.Vector3(f.vertices[3*b],f.vertices[3*b+1],f.vertices[3*b+2])).add(new s.instance.Vector3(f.vertices[3*D],f.vertices[3*D+1],f.vertices[3*D+2])).divideScalar(3),g=new this.engine.instance.Vec3;f.getNormal(L,g);var w=new this.engine.instance.Vec3(G.x+g.x,G.y+g.y,G.z+g.z),E=new this.engine.instance.Vec3(w.x-g.x*i,w.y-g.y*i,w.z-g.z*i);if(r.raycastClosest(w,E,{},h),a){var T=new s.instance.Geometry,x=new s.instance.Mesh(T,new s.instance.MeshBasicMaterial({color:16711680,wireframe:!0,opacity:1})),S=new s.instance.ArrowHelper(new s.instance.Vector3(g.x,g.y,g.z).normalize(),new s.instance.Vector3(w.x,w.y,w.z),i/2,h.hasHit?new s.instance.Color(0,1,0):new s.instance.Color(1,0,0));x.add(S),c.push(x)}h.hasHit||y.push({faceIndex:L,vertices:[new this.engine.instance.Vec3(f.vertices[3*v],f.vertices[3*v+1],f.vertices[3*v+2]),new this.engine.instance.Vec3(f.vertices[3*b],f.vertices[3*b+1],f.vertices[3*b+2]),new this.engine.instance.Vec3(f.vertices[3*D],f.vertices[3*D+1],f.vertices[3*D+2])],center:G,parent:f})}for(var _={},P=0;P<y.length;P++){var I=y[P];if(!I.marked){_[P]={indices:[],vertices:[]};var M=_[P].indices.length;_[P].indices.push(M,M+1,M+2),_[P].vertices.push(I.vertices[0].x,I.vertices[0].y,I.vertices[0].z),_[P].vertices.push(I.vertices[1].x,I.vertices[1].y,I.vertices[1].z),_[P].vertices.push(I.vertices[2].x,I.vertices[2].y,I.vertices[2].z);for(var R=0;R<y.length;R++)if(R!=P){var V=y[R];if(!V.marked&&I.center.distanceTo(V.center)<=n){var M=_[P].indices.length;_[P].indices.push(M,M+1,M+2),_[P].vertices.push(V.vertices[0].x,V.vertices[0].y,V.vertices[0].z),_[P].vertices.push(V.vertices[1].x,V.vertices[1].y,V.vertices[1].z),_[P].vertices.push(V.vertices[2].x,V.vertices[2].y,V.vertices[2].z),V.marked=!0}}}}for(var C in _){var z=_[C],N=new GameLib.D3.Shape(this.engine,GameLib.D3.Shape.SHAPE_TYPE_TRIMESH,{x:1,y:1,z:1},z.vertices,z.indices);if(o)e.addShape(N);else{var A=new GameLib.D3.RigidBody(this.engine,0,12);A.addShape(N),this.addRigidBody(A)}m.push(N),d+=z.indices.length/3}console.log("i = "+u,y),l+=y.length}return console.log("total faces",p),console.log("total broken faces",l),console.log("broken faces in percent",l/p*100),console.log("total fixed faces",d),console.log("fixed triangle mesh shapes",m.length),{brokenFaceIndicators:c,fixedTriangleMeshShapes:m}},"undefined"!=typeof module&&(module.exports=GameLib);