better loading and displaying of scenes, start work with physics data
parent
be9718aaee
commit
91094883f0
|
@ -5,7 +5,7 @@ Controls.FlyControls = function(
|
||||||
THREE,
|
THREE,
|
||||||
canvas
|
canvas
|
||||||
) {
|
) {
|
||||||
this.flySpeed = 25;
|
this.flySpeed = 100;
|
||||||
|
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
|
|
||||||
|
@ -22,7 +22,21 @@ Controls.FlyControls = function(
|
||||||
this.moveUp = false;
|
this.moveUp = false;
|
||||||
this.moveDown = false;
|
this.moveDown = false;
|
||||||
|
|
||||||
// Lock cursor
|
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.havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
|
||||||
this.element = document.body;
|
this.element = document.body;
|
||||||
|
|
||||||
|
@ -31,43 +45,42 @@ Controls.FlyControls = function(
|
||||||
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
|
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.mouseUpCallback = this.onMouseUp.bind(this);
|
|
||||||
this.mouseDownCallback = this.onMouseDown.bind(this);
|
|
||||||
this.mouseMoveCallback = this.onMouseMove.bind(this);
|
|
||||||
this.keyDownCallback = this.onKeyDown.bind(this);
|
|
||||||
this.keyUpCallback = this.onKeyUp.bind(this);
|
|
||||||
//this.mouseClickCallback = this.onMouseClick.bind(this);
|
|
||||||
|
|
||||||
this.camera = camera;
|
|
||||||
|
|
||||||
// Add listeners
|
|
||||||
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);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Controls.FlyControls.prototype.onMouseClick = function(event) {
|
Controls.FlyControls.prototype.onMouseWheel = function(event) {
|
||||||
if (this.havePointerLock) {
|
this.moveForward = true;
|
||||||
if (event.button == 0) {
|
this.applyTranslation(event.wheelDelta * 0.001);
|
||||||
this.canRotate = true;
|
event.preventDefault();
|
||||||
this.element.requestPointerLock();
|
this.moveForward = false;
|
||||||
} else if(event.button == 2) {
|
|
||||||
this.canRotate = false;
|
|
||||||
document.exitPointerLock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Controls.FlyControls.prototype.onMouseDown = function(event) {
|
Controls.FlyControls.prototype.onMouseDown = function(event) {
|
||||||
if (event.button == 0) {
|
|
||||||
|
// 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.canRotate = true;
|
||||||
this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false);
|
this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Controls.FlyControls.prototype.onMouseUp = function(event) {
|
Controls.FlyControls.prototype.onMouseUp = function(event) {
|
||||||
if (event.button == 0) {
|
|
||||||
|
// 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.canRotate = false;
|
||||||
this.canvas.removeEventListener('mousemove', this.mouseMoveCallback);
|
this.canvas.removeEventListener('mousemove', this.mouseMoveCallback);
|
||||||
}
|
}
|
||||||
|
@ -164,11 +177,11 @@ Controls.FlyControls.prototype.onKeyDown = function ( event ) {
|
||||||
this.moveRight = true;
|
this.moveRight = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 32: // space
|
case 104: // keypad up arrow
|
||||||
this.moveUp = true;
|
this.moveUp = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 16:
|
case 98: // keypad down arrow
|
||||||
this.moveDown = true;
|
this.moveDown = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -197,11 +210,11 @@ Controls.FlyControls.prototype.onKeyUp = function ( event ) {
|
||||||
this.moveRight = false;
|
this.moveRight = false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 32: // space
|
case 104: // keypad up arrow
|
||||||
this.moveUp = false;
|
this.moveUp = false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 16:
|
case 98: // keypad down arrow
|
||||||
this.moveDown = false;
|
this.moveDown = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
65
game-lib.js
65
game-lib.js
|
@ -2475,7 +2475,7 @@ 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
|
||||||
*/
|
*/
|
||||||
|
@ -2487,14 +2487,31 @@ GameLib.D3.prototype.loadSceneFromApi = function(sceneName, onLoaded) {
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.open(
|
xhr.open(
|
||||||
'GET',
|
'GET',
|
||||||
this.apiUrl + '/scene/' + sceneName
|
this.apiUrl + '/scene/' + 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);
|
|
||||||
|
var scene = response.scene[0];
|
||||||
|
|
||||||
|
var scene3d = new GameLib.D3.Scene(
|
||||||
|
scene._id || scene.id,
|
||||||
|
scene.path,
|
||||||
|
scene.name,
|
||||||
|
scene.meshes,
|
||||||
|
scene.quaternion,
|
||||||
|
scene.position,
|
||||||
|
scene.rotation,
|
||||||
|
scene.scale,
|
||||||
|
scene.parentSceneId,
|
||||||
|
scene.lights,
|
||||||
|
scene.physics
|
||||||
|
);
|
||||||
|
|
||||||
|
gameLibD3.loadScene(scene3d, onLoaded, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(xhr, this);
|
}(xhr, this);
|
||||||
|
@ -2662,7 +2679,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
|
||||||
|
@ -2753,9 +2770,45 @@ 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]);
|
||||||
}
|
}
|
||||||
}).catch(function(error){
|
|
||||||
|
for (var l = 0; l < threeLights.length; l++) {
|
||||||
|
threeScene.add(threeLights[l]);
|
||||||
|
}
|
||||||
|
|
||||||
|
onLoaded(
|
||||||
|
gameLibScene,
|
||||||
|
{
|
||||||
|
scene: threeScene,
|
||||||
|
lights: threeLights,
|
||||||
|
meshes: threeMeshes
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}.bind(this)).catch(function(error){
|
||||||
console.log(error);
|
console.log(error);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue