Merge branch 'feature-triMesh' into develop
commit
74d04acc02
402
game-lib.js
402
game-lib.js
|
@ -1163,61 +1163,69 @@ 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;
|
||||
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.broadphase = new CANNON.SAPBroadphase();
|
||||
this.worldObject.solver.iterations = 10;
|
||||
}
|
||||
this.rigidBodies = rigidBodies;
|
||||
};
|
||||
|
||||
/**
|
||||
* Physics Rigid Body Superset
|
||||
* @param id
|
||||
* @param name
|
||||
* @param position
|
||||
* @param rotation
|
||||
* @param scale
|
||||
* @param shapes GameLib.D3.Physics.Shape[]
|
||||
* @constructor
|
||||
*/
|
||||
GameLib.D3.Physics.RigidBody = function(
|
||||
id,
|
||||
name,
|
||||
position,
|
||||
rotation,
|
||||
scale,
|
||||
shapes
|
||||
name
|
||||
) {
|
||||
this.position = new this.Vector3(0,0,0);
|
||||
this.bodyObject = null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Physics Rigid Body Vehicle Superset
|
||||
* @constructor
|
||||
*/
|
||||
GameLib.D3.Physics.RigidBody.Vehicle = function() {
|
||||
|
||||
GameLib.D3.Physics.Vehicle = function(
|
||||
) {
|
||||
this.vehicleObject = null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Physics Shape Superset
|
||||
* @constructor
|
||||
*/
|
||||
GameLib.D3.Physics.Shape = function() {};
|
||||
GameLib.D3.Physics.Shape = function(
|
||||
shapeObject, // Physics engine specific
|
||||
shapeType
|
||||
) {
|
||||
this.shapeObject = shapeObject;
|
||||
this.shapeType = shapeType;
|
||||
this.scale = new GameLib.D3.Vector3(1, 1, 1);
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.Shape.TYPE_SPHERE = 1;
|
||||
GameLib.D3.Physics.Shape.TYPE_BOX = 2;
|
||||
GameLib.D3.Physics.Shape.TYPE_TRIMESH = 3;
|
||||
|
||||
/**
|
||||
* Physics Convex Hull Shape Superset
|
||||
|
@ -2657,7 +2665,199 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
|
|||
// Physics
|
||||
// ---------------
|
||||
|
||||
GameLib.D3.Physics.World.GetIndexedVertices = function(
|
||||
GameLib.D3.Physics.Shape.prototype.Update = function(
|
||||
physicsWorld
|
||||
) {
|
||||
if(physicsWorld.engineType === GameLib.D3.Physics.Engine.TYPE_CANNON) {
|
||||
if(this.shapeType === GameLib.D3.Physics.Shape.TYPE_TRIMESH) {
|
||||
this.shapeObject.setScale(new CANNON.Vec3(this.scale.x, this.scale.y, this.scale.z));
|
||||
this.shapeObject.scale.x = this.scale.x;
|
||||
this.shapeObject.scale.y = this.scale.y;
|
||||
this.shapeObject.scale.z = this.scale.z;
|
||||
this.shapeObject.updateAABB();
|
||||
this.shapeObject.updateNormals();
|
||||
this.shapeObject.updateEdges();
|
||||
this.shapeObject.updateBoundingSphereRadius();
|
||||
this.shapeObject.updateTree();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.World.prototype.AddShape = function(
|
||||
shape, // d3.physics.shape
|
||||
rigidBody
|
||||
) {
|
||||
shape.shape = shape;
|
||||
|
||||
if(this.engineType === GameLib.D3.Physics.Engine.TYPE_CANNON) {
|
||||
rigidBody.bodyObject.addShape(shape.shapeObject);
|
||||
}
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.World.prototype.CreateRigidVehicle = function(
|
||||
chassisBody // Physics.RigidBody
|
||||
) {
|
||||
var rigidVehicle = new GameLib.D3.Physics.Vehicle();
|
||||
|
||||
if(this.engineType == GameLib.D3.Physics.Engine.TYPE_CANNON) {
|
||||
var vehicle = new CANNON.RigidVehicle({
|
||||
chassisBody: chassisBody.bodyObject
|
||||
});
|
||||
rigidVehicle.vehicleObject = vehicle;
|
||||
return rigidVehicle;
|
||||
}
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.World.prototype.AddWheel = function(
|
||||
vehicle,
|
||||
rigidBody,
|
||||
position,
|
||||
axis,
|
||||
direction
|
||||
) {
|
||||
if(this.engineType == GameLib.D3.Physics.Engine.TYPE_CANNON) {
|
||||
vehicle.vehicleObject.addWheel({
|
||||
body: rigidBody.bodyObject,
|
||||
position: new CANNON.Vec3(position.x, position.y, position.z),
|
||||
axis: new CANNON.Vec3(axis.x, axis.y, axis.z),
|
||||
direction: new CANNON.Vec3(direction.x, direction.y, direction.z)
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.Vehicle.prototype.GetWheelInfo = function(
|
||||
|
||||
) {
|
||||
return this.vehicleObject.wheelBodies;
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.World.prototype.CreateRigidBody = function(
|
||||
mass,
|
||||
friction,
|
||||
position,
|
||||
quaternion,
|
||||
velocity,
|
||||
angularVelocity,
|
||||
linearDamping,
|
||||
angularDamping,
|
||||
allowSleep,
|
||||
sleepSpeedLimit,
|
||||
sleepTimeLimit,
|
||||
collisionFilterGroup,
|
||||
collisionFilterMask,
|
||||
fixedRotation,
|
||||
shape
|
||||
) {
|
||||
|
||||
var rigidBody = new GameLib.D3.Physics.RigidBody(0, "null");
|
||||
|
||||
position = position || new GameLib.D3.Vector3();
|
||||
velocity = velocity || new GameLib.D3.Vector3();
|
||||
angularVelocity = angularVelocity || new GameLib.D3.Vector3();
|
||||
quaternion = quaternion || new GameLib.D3.Vector4(0, 0, 0, 1);
|
||||
mass = typeof mass == "undefined" ? 0 : mass;
|
||||
friction = typeof friction == "undefined" ? 5 : friction;
|
||||
linearDamping = typeof linearDamping == "undefined" ? 0.01 : linearDamping;
|
||||
angularDamping = typeof angularDamping == "undefined" ? 0.01 : angularDamping;
|
||||
allowSleep = typeof allowSleep == "undefined" ? true : allowSleep;
|
||||
sleepSpeedLimit = typeof sleepSpeedLimit == "undefined" ? 0.1 : sleepSpeedLimit;
|
||||
sleepTimeLimit = typeof sleepTimeLimit == "undefined" ? 1.0 : sleepTimeLimit;
|
||||
collisionFilterGroup = typeof collisionFilterGroup == "undefined" ? 1 : collisionFilterGroup;
|
||||
collisionFilterMask = typeof collisionFilterMask == "undefined" ? 1 : collisionFilterMask;
|
||||
fixedRotation = typeof fixedRotation == "undefined" ? false : fixedRotation;
|
||||
shape = typeof shape == "undefined" ? null : shape;
|
||||
|
||||
|
||||
// Create the bodyObject
|
||||
if(this.engineType == GameLib.D3.Physics.Engine.TYPE_CANNON) {
|
||||
rigidBody.bodyObject = new CANNON.Body(
|
||||
{
|
||||
mass: mass,
|
||||
friction: friction,
|
||||
position: new CANNON.Vec3(position.x, position.y, position.z),
|
||||
velocity: new CANNON.Vec3(velocity.x, velocity.y, velocity.z),
|
||||
quaternion: new CANNON.Quaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w),
|
||||
angularVelocity: new CANNON.Vec3(angularVelocity.x, angularVelocity.y, angularVelocity.z),
|
||||
linearDamping: linearDamping,
|
||||
angularDamping: angularDamping,
|
||||
allowSleep: allowSleep,
|
||||
sleepSpeedLimit: sleepSpeedLimit,
|
||||
sleepTimeLimit: sleepTimeLimit,
|
||||
collisionFilterGroup: collisionFilterGroup,
|
||||
collisionFilterMask: collisionFilterMask,
|
||||
fixedRotation: fixedRotation,
|
||||
shape: shape
|
||||
}
|
||||
);
|
||||
|
||||
return rigidBody;
|
||||
}
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.World.prototype.CreateTriMeshShape = function(
|
||||
vertices, // flat list of floats
|
||||
indices // float list of floats
|
||||
) {
|
||||
if(this.engineType == GameLib.D3.Physics.Engine.TYPE_CANNON) {
|
||||
return new GameLib.D3.Physics.Shape(new CANNON.Trimesh(vertices, indices), GameLib.D3.Physics.Shape.TYPE_TRIMESH);
|
||||
}
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.World.prototype.CreateSphereShape = function (
|
||||
radius
|
||||
) {
|
||||
if(this.engineType == GameLib.D3.Physics.Engine.TYPE_CANNON) {
|
||||
return new GameLib.D3.Physics.Shape(new CANNON.Sphere(radius), GameLib.D3.Physics.Shape.TYPE_SPHERE);
|
||||
}
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.World.prototype.CreateBoxShape = function(
|
||||
halfExtensions // vec3
|
||||
) {
|
||||
if(this.engineType == GameLib.D3.Physics.Engine.TYPE_CANNON) {
|
||||
return new GameLib.D3.Physics.Shape(new CANNON.Box(new CANNON.Vec3(halfExtensions.x, halfExtensions.y, halfExtensions.z)), GameLib.D3.Physics.Shape.TYPE_BOX);
|
||||
}
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.World.prototype.AddRigidBody = function(
|
||||
rigidBody // Physics.RigidBody
|
||||
) {
|
||||
if(this.engineType === GameLib.D3.Physics.Engine.TYPE_CANNON) {
|
||||
this.worldObject.addBody(rigidBody.bodyObject);
|
||||
}
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.World.prototype.AddVehicle = function(
|
||||
vehicle // note: this is the cannon vehicle...
|
||||
) {
|
||||
if(this.engineType == GameLib.D3.Physics.Engine.TYPE_CANNON) {
|
||||
vehicle.vehicleObject.addToWorld(this.worldObject);
|
||||
}
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.World.prototype.Step = function(
|
||||
timeStep
|
||||
) {
|
||||
if(this.engineType == GameLib.D3.Physics.Engine.TYPE_CANNON) {
|
||||
|
||||
var now = performance.now() / 1000;
|
||||
|
||||
if(!this.lastCallTime){
|
||||
// last call time not saved, cant guess elapsed time. Take a simple step.
|
||||
this.worldObject.step(timeStep);
|
||||
this.lastCallTime = now;
|
||||
return;
|
||||
}
|
||||
|
||||
var timeSinceLastCall = now - this.lastCallTime;
|
||||
|
||||
this.worldObject.step(timeStep, timeSinceLastCall);
|
||||
|
||||
this.lastCallTime = now;
|
||||
}
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.World.prototype.GetIndexedVertices = function(
|
||||
triangleMeshShape
|
||||
) {
|
||||
|
||||
|
@ -2675,7 +2875,7 @@ GameLib.D3.Physics.World.GetIndexedVertices = function(
|
|||
|
||||
};
|
||||
|
||||
GameLib.D3.Physics.World.GenerateWireframeViewMesh = function(
|
||||
GameLib.D3.Physics.World.prototype.GenerateWireframeViewMesh = function(
|
||||
triangleMeshShape,
|
||||
normalLength,
|
||||
scale,
|
||||
|
@ -2734,7 +2934,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
|
||||
|
@ -2808,6 +3008,160 @@ 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) {
|
||||
if(physicsWorld.engineType === GameLib.D3.Physics.Engine.TYPE_CANNON) {
|
||||
mesh.position.copy(body.bodyObject.position);
|
||||
mesh.quaternion.copy(body.bodyObject.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;
|
||||
}
|
Loading…
Reference in New Issue