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

3 lines
77 KiB
JavaScript
Raw Normal View History

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.moveSpe
},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&
},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.ve