fixed physics prototype functions & added game functions

beta.r3js.org
polygonboutique 2016-10-04 10:05:18 +02:00
parent b9895f5444
commit 104f7d66bd
1 changed files with 191 additions and 12 deletions

View File

@ -1152,25 +1152,28 @@ GameLib.D3.Physics.Engine.TYPE_GOBLIN = 0x3;
GameLib.D3.Physics.World = function(
id,
name,
engine,
gravity,
rigidBodies
engineType,
gravity
) {
this.id = id;
this.name = name;
this.engine = engine;
this.engineType = engineType;
if (typeof gravity == 'undefined'){
gravity = 9.8;
if (typeof gravity == 'undefined') {
gravity = new THREE.Vector3(0, -9.81, 0);
}
this.gravity = gravity;
if (typeof rigidBodies == 'undefined'){
rigidBodies = [];
this.worldObject = null;
if(this.engineType === GameLib.D3.Physics.Engine.TYPE_CANNON) {
this.worldObject = new CANNON.World();
this.worldObject.gravity.set(this.gravity.x, this.gravity.y, this.gravity.z);
this.worldObject.broadphase = new CANNON.NaiveBroadphase();
this.worldObject.solver.iterations = 10;
}
this.rigidBodies = rigidBodies;
};
/**
@ -2644,7 +2647,31 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
// Physics
// ---------------
GameLib.D3.Physics.World.GetIndexedVertices = function(
GameLib.D3.Physics.World.prototype.AddRigidBody = function(
body
) {
if(this.engineType == GameLib.D3.Physics.Engine.TYPE_CANNON) {
this.worldObject.addBody(body);
}
};
GameLib.D3.Physics.World.prototype.AddVehicle = function(
vehicle
) {
if(this.engineType == GameLib.D3.Physics.Engine.TYPE_CANNON) {
vehicle.addToWorld(this.worldObject);
}
};
GameLib.D3.Physics.World.prototype.Step = function(
timeStep
) {
if(this.engineType == GameLib.D3.Physics.Engine.TYPE_CANNON) {
this.worldObject.step(timeStep);
}
};
GameLib.D3.Physics.World.prototype.GetIndexedVertices = function(
triangleMeshShape
) {
@ -2662,7 +2689,7 @@ GameLib.D3.Physics.World.GetIndexedVertices = function(
};
GameLib.D3.Physics.World.GenerateWireframeViewMesh = function(
GameLib.D3.Physics.World.prototype.GenerateWireframeViewMesh = function(
triangleMeshShape,
normalLength,
scale,
@ -2721,7 +2748,7 @@ GameLib.D3.Physics.World.GenerateWireframeViewMesh = function(
return wireframeTHREEMesh;
};
GameLib.D3.Physics.World.GenerateTriangleCollisionMesh = function(
GameLib.D3.Physics.World.prototype.GenerateTriangleCollisionMesh = function(
threeMesh,
mass, // default = 0
friction, // default = 10
@ -2795,6 +2822,158 @@ GameLib.D3.Physics.World.GenerateTriangleCollisionMesh = function(
}
};
// -----------
// SkyBox
// -----------
GameLib.D3.SkyBox = function (
) {
this.id = null;
this.texturesFolder = null;
};
GameLib.D3.SkyBox.prototype.Load = function (
texturesFolder
) {
this.texturesFolder = texturesFolder;
this.textures = [];
this.materials = [];
this.mesh = {};
this.scene = new THREE.Scene();
this.textureCube = null;
var textureLoader = new THREE.TextureLoader();
// this textures are used to display the skybox
this.textures.push(textureLoader.load(this.texturesFolder + "px.png"));
this.textures.push(textureLoader.load(this.texturesFolder + "nx.png"));
this.textures.push(textureLoader.load(this.texturesFolder + "py.png"));
this.textures.push(textureLoader.load(this.texturesFolder + "ny.png"));
this.textures.push(textureLoader.load(this.texturesFolder + "pz.png"));
this.textures.push(textureLoader.load(this.texturesFolder + "nz.png"));
// assign textures to each cube face
for (var i = 0; i < 6; i ++) {
this.materials.push(new THREE.MeshBasicMaterial({ map: this.textures[i] }));
}
// create cube geometry
this.mesh = new THREE.Mesh(new THREE.CubeGeometry(1, 1, 1), new THREE.MeshFaceMaterial(this.materials));
this.mesh.applyMatrix(new THREE.Matrix4().makeScale(1, 1, -1));
this.scene.add(this.mesh);
// Load env textureCube
// this is used for reflections on meshes
// mesh.material.envMap = this.textureCube;
this.textureCube = new THREE.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 (
threeRenderer,
threeCamera
) {
var cameraPosition = new THREE.Vector3(threeCamera.position.x, threeCamera.position.y, threeCamera.position.z);
threeCamera.position.set(0, 0, 0);
var gl = threeRenderer.context;
gl.disable(gl.DEPTH_TEST);
threeRenderer.render(this.scene, threeCamera);
gl.enable(gl.DEPTH_TEST);
threeCamera.position.copy(cameraPosition);
};
// ---------
// Game
// ---------
GameLib.D3.Game = function (
) {
this.scenes = {};
this.physicsWorlds = [];
this.sceneToPhysicsWorldsMap = {};
};
GameLib.D3.Game.prototype.AddScene = function(
scene
) {
this.scenes[scene.name] = scene;
};
GameLib.D3.Game.prototype.AddPhysicsWorld = function(
physicsWorld
) {
this.physicsWorlds.push(physicsWorld);
};
GameLib.D3.Game.prototype.LinkPhysicsWorldToScene = function(
physicsWorld,
scene
) {
this.sceneToPhysicsWorldsMap[scene.name] = this.sceneToPhysicsWorldsMap[scene.name] || [];
this.sceneToPhysicsWorldsMap[scene.name].push(physicsWorld);
};
GameLib.D3.Game.prototype.GetPhysicsWorldsForScene = function (
scene
) {
return this.sceneToPhysicsWorldsMap[scene.name];
};
GameLib.D3.Game.prototype.ProcessPhysics = function (
timestep
) {
for(var s in this.sceneToPhysicsWorldsMap) {
var physicsWorldArray = this.sceneToPhysicsWorldsMap[s];
var scene = this.scenes[s];
if(scene && physicsWorldArray) {
for(var i = 0, l = physicsWorldArray.length; i < l; i++) {
var physicsWorld = physicsWorldArray[i];
physicsWorld.Step(timestep);
for(var p in physicsWorld.linkedPairs) {
var pair = physicsWorld.linkedPairs[p];
var mesh = pair.threeMesh;
var body = pair.physicsBody;
if(mesh) {
mesh.position.copy(body.position);
mesh.quaternion.copy(body.quaternion);
}
}
}
}
}
};
GameLib.D3.Game.prototype.LinkPair = function (
threeMesh,
physicsBody,
physicsWorld
) {
physicsWorld.linkedPairs = physicsWorld.linkedPairs || [];
physicsWorld.linkedPairs.push({
threeMesh : threeMesh,
physicsBody : physicsBody
});
};
if (typeof module != 'undefined') {
module.exports = GameLib.D3;
}