physics worlds - hello

beta.r3js.org
Theunis J. Botha 2016-10-11 19:57:12 +02:00
parent 91094883f0
commit e8d6b56f42
1 changed files with 205 additions and 69 deletions

View File

@ -23,14 +23,17 @@ GameLib.D3 = function(
Q,
THREE,
apiUrl,
editorUrl
editorUrl,
CANNON
){
this.config = config;
this.Q = Q;
this.THREE = THREE;
this.CANNON = CANNON;
this.textureLoader = new this.THREE.TextureLoader();
this.apiUrl = apiUrl || this.config.api16.url;
this.editorUrl = editorUrl || this.config.editor.url;
this.path = null;
};
if (typeof require == 'undefined' &&
@ -78,7 +81,6 @@ GameLib.D3.Color = Maths3D.Color;
*/
GameLib.D3.Texture = function(
id,
path,
name,
image,
wrapS,
@ -100,7 +102,6 @@ GameLib.D3.Texture = function(
encoding
) {
this.id = id;
this.path = path;
this.name = name;
this.image = image;
@ -416,7 +417,6 @@ GameLib.D3.Light = function(
*/
GameLib.D3.Material = function(
id,
path,
name,
materialType,
opacity,
@ -475,7 +475,6 @@ GameLib.D3.Material = function(
envMapIntensity
) {
this.id = id;
this.path = path;
this.name = name;
if (typeof materialType == 'undefined') {
materialType = GameLib.D3.Material.TYPE_MESH_STANDARD;
@ -1138,7 +1137,9 @@ GameLib.D3.Physics = function(
engineType,
CANNON,
Ammo,
Goblin
Goblin,
worlds,
customWorlds
) {
this.id = id;
@ -1196,6 +1197,15 @@ GameLib.D3.Physics.Solver = function(
tolerance
) {
this.id = id;
if (typeof name == 'undefined') {
if (solverType == GameLib.D3.Physics.SPLIT_SOLVER) {
name = 'split solver';
} else if (solverType == GameLib.D3.Physics.GS_SOLVER) {
name = 'gs solver';
} else {
name = 'unknown solver';
}
}
this.name = name;
this.solverType = solverType;
this.iterations = iterations;
@ -1215,7 +1225,17 @@ GameLib.D3.Physics.Broadphase = function(
broadphaseType
) {
this.id = id;
if (typeof name == 'undefined') {
name = 'broadphase-' + broadphaseType;
}
this.name = name;
if (typeof broadphaseType == 'undefined') {
console.warn('undefined broadphase type');
throw new Error('undefined broadphase type');
}
this.broadphaseType = broadphaseType;
};
@ -1243,10 +1263,6 @@ GameLib.D3.Physics.World = function(
this.name = name;
this.physics = physics;
//this.worldType = GameLib.D3.Physics.World.TYPE_CANNON_WORLD;
if (typeof gravity == 'undefined') {
gravity = new GameLib.D3.Vector3(0, -9.81, 0);
}
@ -1275,8 +1291,16 @@ GameLib.D3.Physics.World = function(
}
this.rigidBodies = rigidBodies;
physics.worlds.push(this);
physics.customWorlds.push(this.getCustomWorld(this));
/**
* We only set the physics property if we pass it in the constructor,
* because we don't always want the physics object (ex. when we store this world to the API - we also don't then
* want to store the custom worlds - we want to generate them after loading from API)
*/
if (physics) {
this.physics = physics;
this.physics.worlds.push(this);
this.physics.customWorlds.push(this.getCustomWorld(this));
}
};
//GameLib.D3.Physics.World.TYPE_CANNON_WORLD = 0x1;
@ -1483,8 +1507,6 @@ GameLib.D3.Vertex = function(
* @param id
* @param textureLink
* @param filename
* @param uploadPath
* @param apiPath
* @param size
* @param contentType
* @constructor
@ -1493,8 +1515,6 @@ GameLib.D3.Image = function(
id,
textureLink,
filename,
uploadPath,
apiPath,
size,
contentType
) {
@ -1504,16 +1524,6 @@ GameLib.D3.Image = function(
this.textureLink = textureLink;
if (typeof uploadPath == 'undefined') {
uploadPath = null;
}
this.uploadPath = uploadPath;
if (typeof apiPath == 'undefined') {
apiPath = null;
}
this.apiPath = apiPath;
if (typeof size == 'undefined') {
size = 0;
}
@ -1641,30 +1651,6 @@ GameLib.D3.TriangleFace.prototype.clone = function(){
);
};
/**
* Associates bones with their child bones, based on parent bone references
*/
GameLib.D3.prototype.createChildBoneIds = function() {
for (var bi = 0; bi < this.bones.length; bi++) {
var childBoneIds = [];
for (var sbi = 0; sbi < this.bones.length; sbi++) {
if (this.bones[sbi] == this.bones[bi]) {
continue;
}
if (this.bones[sbi].parentBoneId !== null &&
this.bones[sbi].parentBoneId == this.bones[bi].boneId)
{
childBoneIds.push(this.bones[sbi].boneId);
}
}
this.bones[bi].childBoneIds = childBoneIds;
}
};
/**
* This is a work-around function to fix polys which don't triangulate because
* they could lie on Z-plane (XZ or YZ)) - we translate the poly to the origin, systematically rotate the poly around
@ -1673,7 +1659,7 @@ GameLib.D3.prototype.createChildBoneIds = function() {
* @param grain is the amount to systematically rotate the poly by - a finer grain means a more accurate maximum XY
* @return []
*/
GameLib.D3.prototype.fixPolyZPlane = function(verticesFlat, grain) {
GameLib.D3.fixPolyZPlane = function(verticesFlat, grain) {
if ((verticesFlat.length % 3) != 0 && !(verticesFlat.length > 9)) {
console.log("The vertices are not in the right length : " + verticesFlat.length);
@ -1734,7 +1720,7 @@ GameLib.D3.prototype.fixPolyZPlane = function(verticesFlat, grain) {
* @param orientationEdge GameLib.D3.Vector2
* @returns {Array}
*/
GameLib.D3.prototype.fixWindingOrder = function(faces, orientationEdge) {
GameLib.D3.fixWindingOrder = function(faces, orientationEdge) {
/**
* Checks if a TriangleFace belonging to a TriangleEdge has already been processed
@ -2019,18 +2005,11 @@ GameLib.D3.prototype.loadMap = function(gameLibTexture, threeMaterial, threeMate
var imagePath = null;
if (gameLibTexture && gameLibTexture.image && gameLibTexture.image.apiPath && gameLibTexture.image.filename) {
/**
* Load the image from API here if apiPath is defined
*/
imagePath = this.apiUrl + gameLibTexture.image.uploadPath + '/' + gameLibTexture.image.filename;
}
if (gameLibTexture && gameLibTexture.image && gameLibTexture.image.uploadPath && gameLibTexture.image.filename) {
if (gameLibTexture && gameLibTexture.image && gameLibTexture.image.filename) {
/**
* Else, load from upload source
*/
imagePath = this.editorUrl + gameLibTexture.image.uploadPath + '/' + gameLibTexture.image.filename;
imagePath = this.editorUrl + '/uploads' + this.path + '/' + gameLibTexture.image.filename;
}
if (imagePath) {
@ -2476,6 +2455,7 @@ GameLib.D3.prototype.createThreeMaterial = function(blenderMaterial) {
* @param onLoaded callback
*/
GameLib.D3.prototype.loadSceneFromApi = function(scene, onLoaded) {
/**
* First check if this is a client or server side request
*/
@ -2487,28 +2467,182 @@ GameLib.D3.prototype.loadSceneFromApi = function(scene, onLoaded) {
var xhr = new XMLHttpRequest();
xhr.open(
'GET',
this.apiUrl + '/scene/' + scene.name
this.apiUrl + '/scene/load' + scene.path + '/' + scene.name
);
xhr.onreadystatechange = function(xhr, gameLibD3) {
return function() {
if (xhr.readyState == 4) {
var response = JSON.parse(xhr.responseText);
if (!response.scene || response.scene.length == 0) {
return onLoaded(null, null, new Error('Could not load scene'));
}
var scene = response.scene[0];
var physics3ds = [];
if (scene.physics && scene.physics.length > 0) {
for (var p = 0; p < scene.physics.length; p++) {
var physics = scene.physics[p];
var physics3d = new GameLib.D3.Physics(
physics.id,
physics.name,
physics.engineType,
gameLibD3.CANNON,
null,
null
);
var worlds3d = [];
for (var w = 0; w < physics.worlds.length; w++) {
var world = physics.worlds[w];
var broadphase = world.broadphase;
var broadphase3d = new GameLib.D3.Physics.Broadphase(
broadphase.id,
broadphase.name,
broadphase.broadphaseType
);
var solver = world.solver;
var solver3d = new GameLib.D3.Physics.Solver(
solver.id,
solver.name,
solver.solverType,
solver.iterations,
solver.tolerance
);
var bodies = world.rigidBodies;
var bodies3d = [];
for (var b = 0; b < bodies.length; b++) {
var body = bodies[b];
//TODO: add all body properties here
var body3d = new GameLib.D3.Physics.RigidBody(
body.id,
body.name
);
bodies3d.push(body3d);
}
var world3d = new GameLib.D3.Physics.World(
null,
world.name,
physics3d,
new GameLib.D3.Vector3(
world.gravity.x,
world.gravity.y,
world.gravity.z
),
broadphase3d,
solver3d,
bodies3d
);
worlds3d.push(world3d);
}
physics3ds.push(physics3d);
}
}
var lights3d = [];
for (var l = 0; l < scene.lights.length; l++) {
var light = scene.lights[l];
var light3d = new GameLib.D3.Light(
light.id,
light.lightType,
light.name,
new GameLib.D3.Color(
light.color.r,
light.color.g,
light.color.b,
light.color.a
),
light.intensity,
new GameLib.D3.Vector3(
light.position.x,
light.position.y,
light.position.z
),
new GameLib.D3.Vector3(
light.targetPosition.x,
light.targetPosition.y,
light.targetPosition.z
),
new GameLib.D3.Vector4(
light.quaternion.x,
light.quaternion.y,
light.quaternion.z,
light.quaternion.w
),
new GameLib.D3.Vector3(
light.rotation.x,
light.rotation.y,
light.rotation.z
),
new GameLib.D3.Vector3(
light.scale.x,
light.scale.y,
light.scale.z
),
light.distance,
light.decay,
light.power,
light.angle,
light.penumbra
);
lights3d.push(light3d);
};
var scene3d = new GameLib.D3.Scene(
scene._id || scene.id,
scene.path,
scene.name,
scene.meshes,
scene.quaternion,
scene.position,
scene.rotation,
scene.scale,
new GameLib.D3.Vector4(
scene.quaternion.x,
scene.quaternion.y,
scene.quaternion.z,
scene.quaternion.w
),
new GameLib.D3.Vector3(
scene.position.x,
scene.position.y,
scene.position.z
),
new GameLib.D3.Vector3(
scene.rotation.x,
scene.rotation.y,
scene.rotation.z
),
new GameLib.D3.Vector3(
scene.scale.x,
scene.scale.y,
scene.scale.z
),
scene.parentSceneId,
scene.lights,
scene.physics
lights3d,
physics3ds
);
gameLibD3.loadScene(scene3d, onLoaded, false);
@ -2529,6 +2663,8 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
console.log("loading scene " + gameLibScene.name);
this.path = gameLibScene.path;
var meshQ = [];
for (var m = 0; m < gameLibScene.meshes.length; m++) {
@ -3369,5 +3505,5 @@ GameLib.D3.Game.prototype.LinkPair = function (
};
if (typeof module != 'undefined') {
module.exports = GameLib.D3;
module.exports = GameLib;
}