merge conflicts

beta.r3js.org
Theunis J. Botha 2016-10-13 13:11:47 +02:00
commit ad112e9841
2 changed files with 675 additions and 92 deletions

225
game-lib-controls.js Normal file
View File

@ -0,0 +1,225 @@
function Controls() {}
Controls.FlyControls = function(
camera,
THREE,
canvas
) {
this.flySpeed = 100;
this.canvas = canvas;
this.THREE = THREE;
this.yaw = 0;
this.pitch = 0;
this.canRotate = false;
this.moveForward = false;
this.moveBackward = false;
this.moveLeft = false;
this.moveRight = false;
this.moveUp = false;
this.moveDown = false;
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 = camera;
this.canvas.addEventListener('keydown', this.keyDownCallback, false);
this.canvas.addEventListener('keyup', this.keyUpCallback, false);
this.canvas.addEventListener('mousedown', this.mouseDownCallback, false);
this.canvas.addEventListener('mouseup', this.mouseUpCallback, false);
this.canvas.addEventListener('mousewheel', this.mouseWheelCallback, false);
this.havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
this.element = document.body;
if (this.havePointerLock) {
this.element.requestPointerLock = this.element.requestPointerLock || this.element.mozRequestPointerLock || this.element.webkitRequestPointerLock;
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
}
};
Controls.FlyControls.prototype.onMouseWheel = function(event) {
this.moveForward = true;
this.applyTranslation(event.wheelDelta * 0.001);
event.preventDefault();
this.moveForward = false;
};
Controls.FlyControls.prototype.onMouseDown = function(event) {
// if (event.button == 0) {
// this.canRotate = true;
// this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false);
// if (this.havePointerLock) {
// this.element.requestPointerLock();
// }
// }
if (event.button == 1) {
this.canRotate = true;
this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false);
}
};
Controls.FlyControls.prototype.onMouseUp = function(event) {
// if (event.button == 0) {
// this.canRotate = false;
// this.canvas.removeEventListener('mousemove', this.mouseMoveCallback);
// if (this.havePointerLock) {
// document.exitPointerLock();
// }
// }
if (event.button == 1) {
this.canRotate = false;
this.canvas.removeEventListener('mousemove', this.mouseMoveCallback);
}
};
Controls.FlyControls.prototype.applyRotation = function() {
this.camera.rotation.set(this.pitch, this.yaw, 0, "YXZ");
};
Controls.FlyControls.prototype.applyTranslation = function(deltaTime) {
var direction = new this.THREE.Vector3(0, 0, -1);
var rotation = new this.THREE.Euler(0, 0, 0, "YXZ");
rotation.set(this.pitch, this.yaw, 0, "YXZ");
direction = direction.applyEuler(rotation);
if(this.moveForward) {
var newPos = direction.normalize();
this.camera.position.x += newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y += newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z += newPos.z * (deltaTime * this.flySpeed);
} else if(this.moveBackward) {
var newPos = direction.normalize();
this.camera.position.x -= newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y -= newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z -= newPos.z * (deltaTime * this.flySpeed);
}
if(this.moveLeft) {
var forward = direction.normalize();
var right = forward.cross(new this.THREE.Vector3(0, 1, 0));
var newPos = right;
this.camera.position.x -= newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y -= newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z -= newPos.z * (deltaTime * this.flySpeed);
} else if(this.moveRight) {
var forward = direction.normalize();
var right = forward.cross(new this.THREE.Vector3(0, 1, 0));
var newPos = right;
this.camera.position.x += newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y += newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z += newPos.z * (deltaTime * this.flySpeed);
}
// Absolute Y-Axis
if(this.moveUp) {
this.camera.position.y += (deltaTime * this.flySpeed);
} else if(this.moveDown) {
this.camera.position.y -= (deltaTime * this.flySpeed);
}
};
Controls.FlyControls.prototype.update = function(deltaTime) {
this.applyRotation();
this.applyTranslation(deltaTime);
};
Controls.FlyControls.prototype.onMouseMove = function ( event ) {
if (this.canRotate) {
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
this.yaw -= movementX * 0.002;
this.pitch -= movementY * 0.002;
}
};
Controls.FlyControls.prototype.onKeyDown = function ( event ) {
switch ( event.keyCode ) {
case 87: // w
this.moveForward = true;
break;
case 65: // a
this.moveLeft = true;
break;
case 83: // s
this.moveBackward = true;
break;
case 68: // d
this.moveRight = true;
break;
case 104: // keypad up arrow
this.moveUp = true;
break;
case 98: // keypad down arrow
this.moveDown = true;
break;
}
};
Controls.FlyControls.prototype.onKeyUp = function ( event ) {
switch ( event.keyCode ) {
case 38: // up
case 87: // w
this.moveForward = false;
break;
case 37: // left
case 65: // a
this.moveLeft = false;
break;
case 40: // down
case 83: // s
this.moveBackward = false;
break;
case 39: // right
case 68: // d
this.moveRight = false;
break;
case 104: // keypad up arrow
this.moveUp = false;
break;
case 98: // keypad down arrow
this.moveDown = false;
break;
}
};
if (typeof module !== 'undefined') {
module.exports = Controls;
}

View File

@ -2,6 +2,10 @@ if (typeof require != 'undefined') {
var Maths3D = require('./game-lib-maths.js'); var Maths3D = require('./game-lib-maths.js');
} }
if (typeof require != 'undefined') {
var Controls = require('./game-lib-controls.js');
}
function GameLib() {} function GameLib() {}
/** /**
@ -19,14 +23,18 @@ GameLib.D3 = function(
Q, Q,
THREE, THREE,
apiUrl, apiUrl,
editorUrl editorUrl,
CANNON,
path
){ ){
this.config = config; this.config = config;
this.Q = Q; this.Q = Q;
this.THREE = THREE; this.THREE = THREE;
this.CANNON = CANNON;
this.textureLoader = new this.THREE.TextureLoader(); this.textureLoader = new this.THREE.TextureLoader();
this.apiUrl = apiUrl || this.config.api16.url; this.apiUrl = apiUrl || this.config.api16.url;
this.editorUrl = editorUrl || this.config.editor.url; this.editorUrl = editorUrl || this.config.editor.url;
this.path = path;
}; };
if (typeof require == 'undefined' && if (typeof require == 'undefined' &&
@ -34,6 +42,13 @@ if (typeof require == 'undefined' &&
console.warn("You need a proper Maths3D library in order to use this library"); console.warn("You need a proper Maths3D library in order to use this library");
} }
if (typeof require == 'undefined' &&
typeof Controls == 'undefined') {
console.warn("You need a proper Control library in order to use this library");
}
GameLib.D3.Controls = Controls;
GameLib.D3.Vector2 = Maths3D.Vector2; GameLib.D3.Vector2 = Maths3D.Vector2;
GameLib.D3.Vector3 = Maths3D.Vector3; GameLib.D3.Vector3 = Maths3D.Vector3;
GameLib.D3.Vector4 = Maths3D.Vector4; GameLib.D3.Vector4 = Maths3D.Vector4;
@ -67,7 +82,6 @@ GameLib.D3.Color = Maths3D.Color;
*/ */
GameLib.D3.Texture = function( GameLib.D3.Texture = function(
id, id,
path,
name, name,
image, image,
wrapS, wrapS,
@ -89,7 +103,6 @@ GameLib.D3.Texture = function(
encoding encoding
) { ) {
this.id = id; this.id = id;
this.path = path;
this.name = name; this.name = name;
this.image = image; this.image = image;
@ -405,7 +418,6 @@ GameLib.D3.Light = function(
*/ */
GameLib.D3.Material = function( GameLib.D3.Material = function(
id, id,
path,
name, name,
materialType, materialType,
opacity, opacity,
@ -464,7 +476,6 @@ GameLib.D3.Material = function(
envMapIntensity envMapIntensity
) { ) {
this.id = id; this.id = id;
this.path = path;
this.name = name; this.name = name;
if (typeof materialType == 'undefined') { if (typeof materialType == 'undefined') {
materialType = GameLib.D3.Material.TYPE_MESH_STANDARD; materialType = GameLib.D3.Material.TYPE_MESH_STANDARD;
@ -685,7 +696,7 @@ GameLib.D3.Material = function(
this.alphaTest = alphaTest; this.alphaTest = alphaTest;
if (typeof clippingPlanes == 'undefined') { if (typeof clippingPlanes == 'undefined') {
clippingPlanes = null; clippingPlanes = [];
} }
this.clippingPlanes = clippingPlanes; this.clippingPlanes = clippingPlanes;
@ -1110,70 +1121,186 @@ GameLib.D3.Bone = function(
this.rawData = null;//rawData; this.rawData = null;//rawData;
}; };
GameLib.D3.Physics = function() {};
/** /**
* Physics Engine Superset * Physics Superset
* @param id * @param id
* @param name * @param name
* @param engineType * @param engineType
* @param engine * @param CANNON
* @param Ammo
* @param Goblin
* @param worlds GameLib.D3.Physics.World[]
* @constructor * @constructor
*/ */
GameLib.D3.Physics.Engine = function( GameLib.D3.Physics = function(
id, id,
name, name,
engineType, engineType,
engine CANNON,
Ammo,
Goblin,
worlds,
customWorlds
) { ) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.engineType = engineType; this.engineType = engineType;
this.engine = engine;
this.CANNON = CANNON;
this.Ammo = Ammo;
this.Goblin = Goblin;
this.worlds = [];
this.customWorlds = [];
}; };
/** /**
* Physics Engine Types * Physics Engine Types
* @type {number} * @type {number}
*/ */
GameLib.D3.Physics.Engine.TYPE_CANNON = 0x1; GameLib.D3.Physics.TYPE_CANNON = 0x1;
GameLib.D3.Physics.Engine.TYPE_AMMO = 0x2; GameLib.D3.Physics.TYPE_AMMO = 0x2;
GameLib.D3.Physics.Engine.TYPE_GOBLIN = 0x3; GameLib.D3.Physics.TYPE_GOBLIN = 0x3;
/**
* Broadphase Types
* @type {number}
*/
GameLib.D3.Physics.BROADPHASE_TYPE_NAIVE = 0x1;
GameLib.D3.Physics.BROADPHASE_TYPE_GRID = 0x2;
GameLib.D3.Physics.BROADPHASE_TYPE_SAP = 0x3;
/**
* Solver Types
* @type {number}
*/
GameLib.D3.Physics.SPLIT_SOLVER = 0x1;
GameLib.D3.Physics.GS_SOLVER = 0x2;
/**
* Physics Solver Superset
* @param id
* @param name
* @param solverType
* @param iterations
* @param tolerance
* @constructor
*/
GameLib.D3.Physics.Solver = function(
id,
name,
solverType,
iterations,
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;
this.tolerance = tolerance;
};
/**
* Physics Broadphase Superset
* @param id
* @param name
* @param broadphaseType
* @constructor
*/
GameLib.D3.Physics.Broadphase = function(
id,
name,
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;
};
/** /**
* Physics World Superset * Physics World Superset
* @param id * @param id
* @param name * @param name
* @param physics
* @param gravity * @param gravity
* @param rigidBodies GameLib.D3.Physics.RigidBody[] * @param broadphase
* @param engine GameLib.D3.Physics.Engine * @param solver
* @param rigidBodies
* @constructor * @constructor
*/ */
GameLib.D3.Physics.World = function( GameLib.D3.Physics.World = function(
id, id,
name, name,
engineType, physics,
gravity gravity,
broadphase,
solver,
rigidBodies
) { ) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.engineType = engineType;
if (typeof gravity == 'undefined') { if (typeof gravity == 'undefined') {
gravity = new THREE.Vector3(0, -9.81, 0); gravity = new GameLib.D3.Vector3(0, -9.81, 0);
} }
this.gravity = gravity; this.gravity = gravity;
this.worldObject = null; if (typeof broadphase == 'undefined') {
broadphase = new GameLib.D3.Physics.Broadphase(
null,
'broadPhaseNaive',
GameLib.D3.Physics.BROADPHASE_TYPE_NAIVE
);
}
this.broadphase = broadphase;
if(this.engineType === GameLib.D3.Physics.Engine.TYPE_CANNON) { if (typeof solver == 'undefined') {
this.worldObject = new CANNON.World(); solver = new GameLib.D3.Physics.Solver(
this.worldObject.gravity.set(this.gravity.x, this.gravity.y, this.gravity.z); null,
this.worldObject.broadphase = new CANNON.NaiveBroadphase(); 'GSSolver',
//this.worldObject.broadphase = new CANNON.SAPBroadphase(); GameLib.D3.Physics.GS_SOLVER
this.worldObject.solver.iterations = 10; );
}
this.solver = solver;
if (typeof rigidBodies == 'undefined') {
rigidBodies = [];
}
this.rigidBodies = rigidBodies;
/**
* 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));
} }
}; };
@ -1192,6 +1319,7 @@ GameLib.D3.Physics.RigidBody = function(
/** /**
* Physics Rigid Body Vehicle Superset * Physics Rigid Body Vehicle Superset
* TODO: body + wheels[]
* @constructor * @constructor
*/ */
GameLib.D3.Physics.RigidVehicle = function( GameLib.D3.Physics.RigidVehicle = function(
@ -1201,6 +1329,7 @@ GameLib.D3.Physics.RigidVehicle = function(
/** /**
* Physics Raycast Vehicle Superset * Physics Raycast Vehicle Superset
* TODO: body + wheels[]
* @constructor * @constructor
*/ */
GameLib.D3.Physics.RaycastVehicle = function( GameLib.D3.Physics.RaycastVehicle = function(
@ -1389,8 +1518,6 @@ GameLib.D3.Vertex = function(
* @param id * @param id
* @param textureLink * @param textureLink
* @param filename * @param filename
* @param uploadPath
* @param apiPath
* @param size * @param size
* @param contentType * @param contentType
* @constructor * @constructor
@ -1399,8 +1526,6 @@ GameLib.D3.Image = function(
id, id,
textureLink, textureLink,
filename, filename,
uploadPath,
apiPath,
size, size,
contentType contentType
) { ) {
@ -1410,16 +1535,6 @@ GameLib.D3.Image = function(
this.textureLink = textureLink; 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') { if (typeof size == 'undefined') {
size = 0; size = 0;
} }
@ -1470,7 +1585,8 @@ GameLib.D3.Scene = function(
rotation, rotation,
scale, scale,
parentSceneId, parentSceneId,
lights lights,
physics
) { ) {
this.id = id; this.id = id;
this.path = path; this.path = path;
@ -1509,6 +1625,11 @@ GameLib.D3.Scene = function(
lights = []; lights = [];
} }
this.lights = lights; this.lights = lights;
if (typeof physics == 'undefined') {
physics = [];
}
this.physics = physics;
}; };
/** /**
@ -1541,30 +1662,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 * 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 * they could lie on Z-plane (XZ or YZ)) - we translate the poly to the origin, systematically rotate the poly around
@ -1573,7 +1670,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 * @param grain is the amount to systematically rotate the poly by - a finer grain means a more accurate maximum XY
* @return [] * @return []
*/ */
GameLib.D3.prototype.fixPolyZPlane = function(verticesFlat, grain) { GameLib.D3.fixPolyZPlane = function(verticesFlat, grain) {
if ((verticesFlat.length % 3) != 0 && !(verticesFlat.length > 9)) { if ((verticesFlat.length % 3) != 0 && !(verticesFlat.length > 9)) {
console.log("The vertices are not in the right length : " + verticesFlat.length); console.log("The vertices are not in the right length : " + verticesFlat.length);
@ -1634,7 +1731,7 @@ GameLib.D3.prototype.fixPolyZPlane = function(verticesFlat, grain) {
* @param orientationEdge GameLib.D3.Vector2 * @param orientationEdge GameLib.D3.Vector2
* @returns {Array} * @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 * Checks if a TriangleFace belonging to a TriangleEdge has already been processed
@ -1919,22 +2016,17 @@ GameLib.D3.prototype.loadMap = function(gameLibTexture, threeMaterial, threeMate
var imagePath = null; var imagePath = null;
if (gameLibTexture && gameLibTexture.image && gameLibTexture.image.apiPath && gameLibTexture.image.filename) { if (gameLibTexture && gameLibTexture.image && 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) {
/** /**
* Else, load from upload source * 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) { if (imagePath) {
this.textureLoader.crossOrigin = '';
this.textureLoader.load( this.textureLoader.load(
imagePath, imagePath,
function(texture) { function(texture) {
@ -2134,8 +2226,7 @@ GameLib.D3.prototype.loadMaps = function(blenderMaterial, blenderMaps, threeMate
if ( if (
blenderTexture && blenderTexture &&
blenderTexture.image && blenderTexture.image &&
blenderTexture.image.filename && blenderTexture.image.filename
blenderTexture.image.uploadPath
) { ) {
var threeMap = null; var threeMap = null;
@ -2373,7 +2464,8 @@ GameLib.D3.prototype.createThreeMaterial = function(blenderMaterial) {
* @param sceneName * @param sceneName
* @param onLoaded callback * @param onLoaded callback
*/ */
GameLib.D3.prototype.loadSceneFromApi = function(sceneName, onLoaded) { GameLib.D3.prototype.loadSceneFromApi = function(scene, onLoaded) {
/** /**
* First check if this is a client or server side request * First check if this is a client or server side request
*/ */
@ -2385,14 +2477,185 @@ GameLib.D3.prototype.loadSceneFromApi = function(sceneName, onLoaded) {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open( xhr.open(
'GET', 'GET',
this.apiUrl + '/scene/pong%20latest' this.apiUrl + '/scene/load' + scene.path + '/' + scene.name
); );
xhr.onreadystatechange = function(xhr, gameLibD3) { xhr.onreadystatechange = function(xhr, gameLibD3) {
return function() { return function() {
if (xhr.readyState == 4) { if (xhr.readyState == 4) {
var response = JSON.parse(xhr.responseText); var response = JSON.parse(xhr.responseText);
gameLibD3.loadScene(response.scene[0], onLoaded, false);
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,
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,
lights3d,
physics3ds
);
gameLibD3.loadScene(scene3d, onLoaded, false);
} }
} }
}(xhr, this); }(xhr, this);
@ -2410,6 +2673,8 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
console.log("loading scene " + gameLibScene.name); console.log("loading scene " + gameLibScene.name);
this.path = gameLibScene.path;
var meshQ = []; var meshQ = [];
for (var m = 0; m < gameLibScene.meshes.length; m++) { for (var m = 0; m < gameLibScene.meshes.length; m++) {
@ -2560,7 +2825,7 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
function(gl3d, mesh, geometry) { function(gl3d, mesh, geometry) {
return function(materials) { return function(materials) {
console.log("loaded all materials maps (" + materials.length + ")"); console.log("loaded material : " + materials[0].name);
/** /**
* We don't support MultiMaterial atm - it doesn't work with raycasting * We don't support MultiMaterial atm - it doesn't work with raycasting
@ -2651,16 +2916,108 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals
} }
} }
onLoaded(threeMeshes, threeLights); var threeScene = new this.THREE.Scene();
threeScene.name = gameLibScene.name;
threeScene.position.x = gameLibScene.position.x;
threeScene.position.y = gameLibScene.position.y;
threeScene.position.z = gameLibScene.position.z;
threeScene.rotation.x = gameLibScene.rotation.x;
threeScene.rotation.y = gameLibScene.rotation.y;
threeScene.rotation.z = gameLibScene.rotation.z;
threeScene.scale.x = gameLibScene.scale.x;
threeScene.scale.y = gameLibScene.scale.y;
threeScene.scale.z = gameLibScene.scale.z;
threeScene.quaternion.x = gameLibScene.quaternion.x;
threeScene.quaternion.y = gameLibScene.quaternion.y;
threeScene.quaternion.z = gameLibScene.quaternion.z;
threeScene.quaternion.w = gameLibScene.quaternion.w;
for (var m = 0; m < threeMeshes.length; m++) {
threeScene.add(threeMeshes[m]);
}
for (var l = 0; l < threeLights.length; l++) {
threeScene.add(threeLights[l]);
}
onLoaded(
gameLibScene,
{
scene: threeScene,
lights: threeLights,
meshes: threeMeshes
}
);
} }
}).catch(function(error){ }.bind(this)).catch(function(error){
console.log(error); console.log(error);
}); });
}; };
// --------------- /** ---------------
// Physics * Physics
// --------------- * --------------- */
/**
* Creates a Custom World object from a GameLib.D3.Physics.World
*/
GameLib.D3.Physics.World.prototype.getCustomWorld = function() {
var engineType = this.physics.engineType;
if (engineType != GameLib.D3.Physics.TYPE_CANNON) {
console.warn('Unsupported engine type: ' + engineType);
throw new Error('Unsupported engine type: ' + engineType);
}
var customWorld = new this.physics.CANNON.World();
var cannonBroadphase = null;
if (this.broadphase.broadphaseType == GameLib.D3.Physics.BROADPHASE_TYPE_NAIVE) {
cannonBroadphase = new this.physics.CANNON.NaiveBroadphase();
} else if (this.broadphase.broadphaseType == GameLib.D3.Physics.BROADPHASE_TYPE_GRID) {
cannonBroadphase = new this.physics.CANNON.GridBroadphase();
} else if (this.broadphase.broadphaseType == GameLib.D3.Physics.BROADPHASE_TYPE_SAP) {
cannonBroadphase = new this.physics.CANNON.SAPBroardphase();
} else {
console.warn('Unsupported broadphase type: ' + this.broadphase.broadphaseType);
throw new Error('Unsupported broadphase type: ' + this.broadphase.broadphaseType);
}
customWorld.broadphase = cannonBroadphase;
var cannonSolver = null;
if (this.solver.solverType == GameLib.D3.Physics.SPLIT_SOLVER) {
cannonSolver = new this.physics.CANNON.SplitSolver();
} else if (this.solver.solverType == GameLib.D3.Physics.GS_SOLVER) {
cannonSolver = new this.physics.CANNON.GSSolver();
cannonSolver.iterations = this.solver.iterations;
}
customWorld.solver = cannonSolver;
customWorld.gravity.x = this.gravity.x;
customWorld.gravity.y = this.gravity.y;
customWorld.gravity.z = this.gravity.z;
for (var b = 0; b < this.rigidBodies.length; b++) {
var customBody = this.createCustomBody(this.rigidBodies[b]);
//customWorld.AddRigidBody();
}
customWorld.name = this.name;
return customWorld;
};
GameLib.D3.Physics.Shape.prototype.Update = function( GameLib.D3.Physics.Shape.prototype.Update = function(
physicsWorld physicsWorld
@ -2900,6 +3257,7 @@ GameLib.D3.Physics.World.prototype.Step = function(
//return; //return;
var now = performance.now() / 1000; var now = performance.now() / 1000;
if(!this.lastCallTime){ if(!this.lastCallTime){
// last call time not saved, cant guess elapsed time. Take a simple step. // last call time not saved, cant guess elapsed time. Take a simple step.
this.worldObject.step(timeStep); this.worldObject.step(timeStep);
@ -3350,5 +3708,5 @@ GameLib.D3.GenerateHeightmapDataFromImage = function (
}; };
if (typeof module != 'undefined') { if (typeof module != 'undefined') {
module.exports = GameLib.D3; module.exports = GameLib;
} }