diff --git a/game-lib-controls.js b/game-lib-controls.js index 2245a7a..2580743 100644 --- a/game-lib-controls.js +++ b/game-lib-controls.js @@ -5,7 +5,7 @@ Controls.FlyControls = function( THREE, canvas ) { - this.flySpeed = 25; + this.flySpeed = 100; this.canvas = canvas; @@ -22,7 +22,21 @@ Controls.FlyControls = function( this.moveUp = 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.element = document.body; @@ -31,43 +45,42 @@ Controls.FlyControls = function( 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) { - if (this.havePointerLock) { - if (event.button == 0) { - this.canRotate = true; - this.element.requestPointerLock(); - } else if(event.button == 2) { - this.canRotate = false; - document.exitPointerLock(); - } - } +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) { + + // 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) { + + // 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); } @@ -164,11 +177,11 @@ Controls.FlyControls.prototype.onKeyDown = function ( event ) { this.moveRight = true; break; - case 32: // space + case 104: // keypad up arrow this.moveUp = true; break; - case 16: + case 98: // keypad down arrow this.moveDown = true; break; } @@ -197,11 +210,11 @@ Controls.FlyControls.prototype.onKeyUp = function ( event ) { this.moveRight = false; break; - case 32: // space + case 104: // keypad up arrow this.moveUp = false; break; - case 16: + case 98: // keypad down arrow this.moveDown = false; break; } diff --git a/game-lib.js b/game-lib.js index 3bac60a..7e8f6c9 100644 --- a/game-lib.js +++ b/game-lib.js @@ -2475,7 +2475,7 @@ GameLib.D3.prototype.createThreeMaterial = function(blenderMaterial) { * @param sceneName * @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 */ @@ -2487,14 +2487,31 @@ GameLib.D3.prototype.loadSceneFromApi = function(sceneName, onLoaded) { var xhr = new XMLHttpRequest(); xhr.open( 'GET', - this.apiUrl + '/scene/' + sceneName + this.apiUrl + '/scene/' + scene.name ); xhr.onreadystatechange = function(xhr, gameLibD3) { return function() { if (xhr.readyState == 4) { 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); @@ -2662,7 +2679,7 @@ GameLib.D3.prototype.loadScene = function(gameLibScene, onLoaded, computeNormals function(gl3d, mesh, geometry) { 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 @@ -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]); + } + + 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); }); };