2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Represents a camera that can be used easily
|
|
|
|
* @constructor
|
|
|
|
* @augments THREE.PerspectiveCamera
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
var PointerCamera = function() {
|
|
|
|
THREE.PerspectiveCamera.apply(this, arguments);
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* A reference to the renderer
|
|
|
|
* @type {THREE.Renderer}
|
|
|
|
*/
|
2015-06-02 11:01:28 +02:00
|
|
|
this.renderer = arguments[4];
|
|
|
|
|
|
|
|
if (arguments[5] === undefined)
|
2015-04-11 17:18:07 +02:00
|
|
|
listenerTarget = document;
|
|
|
|
else
|
2015-06-02 11:01:28 +02:00
|
|
|
listenerTarget = arguments[5];
|
2015-04-11 17:18:07 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Theta angle of the camera
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
this.theta = Math.PI;
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Phi angle of the camera
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
this.phi = Math.PI;
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Indicates if the camera is following a linear motion
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2015-04-08 11:36:15 +02:00
|
|
|
this.moving = false;
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Indicates if the user is dragging the camera
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2015-04-07 11:13:19 +02:00
|
|
|
this.dragging = false;
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current position of the cursor
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
2015-04-07 11:13:19 +02:00
|
|
|
this.mouse = {x: 0, y: 0};
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Current movement of the cursor
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
this.mouseMove = {x: 0, y: 0};
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Current position of the camera (optical center)
|
|
|
|
* @type {THREE.Vector}
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
this.position = new THREE.Vector3();
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current direction of the camera
|
|
|
|
* @type {THREE.Vector}
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
this.forward = new THREE.Vector3();
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Vector pointing to the left of the camera
|
|
|
|
* @type {THREE.Vector}
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
this.left = new THREE.Vector3();
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Point that the camera is targeting
|
|
|
|
* @type {THREE.Vector}
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
this.target = new THREE.Vector3(0,1,0);
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Indicates the different motions that the camera should have according to the keyboard events
|
|
|
|
* @type {Object}
|
|
|
|
* @description Contains the following booleans
|
|
|
|
* <ul>
|
|
|
|
* <li>increasePhi</li>
|
|
|
|
* <li>decreasePhi</li>
|
|
|
|
* <li>increaseTheta</li>
|
|
|
|
* <li>decreaseTheta</li>
|
|
|
|
* <li>boost</li>
|
|
|
|
* <li>moveForward</li>
|
|
|
|
* <li>moveBackward</li>
|
|
|
|
* <li>moveLeft</li>
|
|
|
|
* <li>moveRight</li>
|
|
|
|
* </ul>
|
|
|
|
*/
|
2015-05-19 14:25:35 +02:00
|
|
|
this.motion = {};
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Sentitivity of the mouse
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
this.sensitivity = 0.05;
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Speed of the camera
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
this.speed = 1;
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Raycaster used to compute collisions
|
|
|
|
* @type {THREE.Raycaster}
|
|
|
|
*/
|
2015-04-22 10:17:54 +02:00
|
|
|
this.raycaster = new THREE.Raycaster();
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* History of the moves of the camera
|
|
|
|
* @type {History}
|
|
|
|
*/
|
2015-04-30 14:53:55 +02:00
|
|
|
this.history = new History();
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Option to enable or disable the pointer lock
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2015-06-19 15:54:09 +02:00
|
|
|
this.shouldLock = true;
|
2015-06-23 15:31:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current state of the pointer (locked or not)
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2015-06-19 15:54:09 +02:00
|
|
|
this.pointerLocked = false;
|
|
|
|
|
2015-06-25 10:56:20 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
this.listenerTarget = listenerTarget;
|
|
|
|
|
2015-04-02 12:38:06 +02:00
|
|
|
// Set events from the document
|
|
|
|
var self = this;
|
|
|
|
var onKeyDown = function(event) {self.onKeyDown(event);};
|
|
|
|
var onKeyUp = function(event) {self.onKeyUp(event);};
|
2015-06-19 15:54:09 +02:00
|
|
|
var onMouseDown = function(event) {if (event.which === 1) self.onMouseDown(event); };
|
|
|
|
var onMouseUp = function(event) {if (event.which === 1) self.onMouseUp(event); };
|
2015-04-07 11:13:19 +02:00
|
|
|
var onMouseMove = function(event) {self.onMouseMove(event); };
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-04-28 09:43:24 +02:00
|
|
|
document.addEventListener('keydown', onKeyDown, false);
|
|
|
|
document.addEventListener('keyup', onKeyUp, false);
|
2015-06-19 15:54:09 +02:00
|
|
|
|
|
|
|
document.addEventListener('pointerlockchange', function(event) { self.onPointerLockChange(event); }, false);
|
|
|
|
document.addEventListener('mozpointerlockchange', function(event) { self.onPointerLockChange(event); }, false);
|
|
|
|
document.addEventListener('webkitpointerlockchange', function(event) { self.onPointerLockChange(event); }, false);
|
|
|
|
|
2015-06-25 10:56:20 +02:00
|
|
|
document.addEventListener('mousemove', function(event) {self.onMouseMovePointer(event);}, false);
|
|
|
|
|
2015-06-19 15:54:09 +02:00
|
|
|
listenerTarget.addEventListener('mousedown', function() {self.lockPointer();}, false);
|
|
|
|
listenerTarget.addEventListener('mousedown', onMouseDown, false);
|
|
|
|
listenerTarget.addEventListener('mousemove', onMouseMove, false);
|
2015-04-11 17:18:07 +02:00
|
|
|
listenerTarget.addEventListener('mouseup', onMouseUp, false);
|
2015-06-03 15:45:40 +02:00
|
|
|
listenerTarget.addEventListener('mouseout', onMouseUp, false);
|
2015-04-22 11:02:54 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Option to enable or disable the collisions
|
|
|
|
* @type {Boolean}
|
|
|
|
*/
|
2015-04-22 11:02:54 +02:00
|
|
|
this.collisions = true;
|
2015-05-26 11:49:24 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Is true when we should log the camera angles. It will be set to false
|
|
|
|
* once is done, and reset to true after a certain period of time
|
|
|
|
* @param {Boolean}
|
|
|
|
*/
|
2015-06-22 10:09:52 +02:00
|
|
|
this.shouldLogCameraAngles = true;
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* The camera we will move to when we'll reset the camera
|
|
|
|
* @param {Object}
|
|
|
|
*/
|
2015-05-26 11:49:24 +02:00
|
|
|
this.resetElements = resetBobombElements();
|
2015-04-02 12:38:06 +02:00
|
|
|
}
|
|
|
|
PointerCamera.prototype = Object.create(THREE.PerspectiveCamera.prototype);
|
|
|
|
PointerCamera.prototype.constructor = PointerCamera;
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Locks the pointer inside the canvas, and displays a gun sight at the middle of the renderer
|
|
|
|
* This method works only if the browser supports requestPointerLock
|
|
|
|
*/
|
2015-06-19 15:54:09 +02:00
|
|
|
PointerCamera.prototype.lockPointer = function() {
|
|
|
|
|
|
|
|
if (this.shouldLock) {
|
2015-06-25 10:56:20 +02:00
|
|
|
this.renderer.domElement.requestPointerLock =
|
|
|
|
this.renderer.domElement.requestPointerLock ||
|
|
|
|
this.renderer.domElement.mozRequestPointerLock ||
|
|
|
|
this.renderer.domElement.webkitRequestPointerLock;
|
2015-06-19 15:54:09 +02:00
|
|
|
|
2015-06-25 10:56:20 +02:00
|
|
|
if (this.renderer.domElement.requestPointerLock) {
|
2015-06-19 15:54:09 +02:00
|
|
|
|
2015-06-25 10:56:20 +02:00
|
|
|
this.renderer.domElement.requestPointerLock();
|
2015-06-19 15:54:09 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-25 10:56:20 +02:00
|
|
|
/**
|
|
|
|
* Check that the pointer is locked or not, and updated locked attribute
|
|
|
|
* @returns true if the pointer is locked, false otherwise
|
|
|
|
*/
|
|
|
|
PointerCamera.prototype.isLocked = function() {
|
|
|
|
var toto =
|
|
|
|
document.pointerLockElement === this.renderer.domElement ||
|
|
|
|
document.mozPointerLockElement === this.renderer.domElement ||
|
|
|
|
document.webkitPointerLockElement === this.renderer.domElement;
|
|
|
|
|
|
|
|
return toto;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Update the camera when the pointer lock changes state
|
|
|
|
*/
|
2015-06-19 15:54:09 +02:00
|
|
|
PointerCamera.prototype.onPointerLockChange = function() {
|
|
|
|
|
2015-06-25 10:56:20 +02:00
|
|
|
if (this.isLocked()) {
|
|
|
|
|
|
|
|
// The pointer is locked : adapt the state of the camera
|
|
|
|
this.pointerLocked = true;
|
|
|
|
this.mousePointer.render();
|
2015-06-19 15:54:09 +02:00
|
|
|
|
2015-06-25 10:56:20 +02:00
|
|
|
this.mouse.x = this.renderer.domElement.width/2;
|
|
|
|
this.mouse.y = this.renderer.domElement.height/2;
|
|
|
|
|
|
|
|
// Remove start canvas
|
|
|
|
this.startCanvas.clear();
|
|
|
|
|
|
|
|
} else {
|
2015-06-19 15:54:09 +02:00
|
|
|
|
|
|
|
this.pointerLocked = false;
|
2015-06-19 17:45:52 +02:00
|
|
|
this.mousePointer.clear();
|
2015-06-19 15:54:09 +02:00
|
|
|
|
2015-06-25 10:56:20 +02:00
|
|
|
this.theta = this.previousTheta;
|
|
|
|
this.phi = this.previousPhi;
|
|
|
|
|
2015-06-19 15:54:09 +02:00
|
|
|
this.mouseMove.x = 0;
|
|
|
|
this.mouseMove.y = 0;
|
|
|
|
|
2015-06-25 10:56:20 +02:00
|
|
|
// Draw start canvas only if should lock
|
|
|
|
if (this.shouldLock)
|
|
|
|
this.startCanvas.render();
|
|
|
|
else
|
|
|
|
this.startCanvas.clear();
|
|
|
|
|
2015-06-19 15:54:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Update the position of the camera
|
|
|
|
* @param {Number} time number of milliseconds between the previous and the next frame
|
|
|
|
*/
|
2015-05-13 09:11:54 +02:00
|
|
|
PointerCamera.prototype.update = function(time) {
|
2015-04-08 11:36:15 +02:00
|
|
|
if (this.moving) {
|
2015-05-13 09:29:37 +02:00
|
|
|
this.linearMotion(time);
|
2015-05-11 10:20:19 +02:00
|
|
|
} else if (this.movingHermite) {
|
2015-05-13 09:29:37 +02:00
|
|
|
this.hermiteMotion(time);
|
2015-05-11 10:20:19 +02:00
|
|
|
} else {
|
2015-05-13 09:11:54 +02:00
|
|
|
this.normalMotion(time);
|
2015-05-11 10:20:19 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-08 11:36:15 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Update the camera according to its linear motion
|
|
|
|
* @param {Number} time number of milliseconds between the previous and the next frame
|
|
|
|
*/
|
2015-05-13 09:29:37 +02:00
|
|
|
PointerCamera.prototype.linearMotion = function(time) {
|
2015-05-11 10:20:19 +02:00
|
|
|
var position_direction = Tools.diff(this.new_position, this.position);
|
|
|
|
var target_direction = Tools.diff(this.new_target, this.target);
|
2015-04-08 11:36:15 +02:00
|
|
|
|
2015-05-13 09:29:37 +02:00
|
|
|
this.position.add(Tools.mul(position_direction, 0.05 * time / 20));
|
|
|
|
this.target.add(Tools.mul(target_direction, 0.05 * time / 20));
|
2015-04-08 11:36:15 +02:00
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
if (Tools.norm2(Tools.diff(this.position, this.new_position)) < 0.01 &&
|
|
|
|
Tools.norm2(Tools.diff(this.target, this.new_target)) < 0.01) {
|
|
|
|
this.moving = false;
|
2015-06-19 17:45:52 +02:00
|
|
|
this.anglesFromVectors();
|
2015-05-11 10:20:19 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-05 16:52:39 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Update the camera according to its hermite motion
|
|
|
|
* @param {Number} time number of milliseconds between the previous and the next frame
|
|
|
|
*/
|
2015-05-13 09:29:37 +02:00
|
|
|
PointerCamera.prototype.hermiteMotion = function(time) {
|
2015-05-11 10:20:19 +02:00
|
|
|
var eval = this.hermitePosition.eval(this.t);
|
|
|
|
this.position.x = eval.x;
|
|
|
|
this.position.y = eval.y;
|
|
|
|
this.position.z = eval.z;
|
2015-05-05 16:52:39 +02:00
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
this.target = Tools.sum(this.position, this.hermiteAngles.eval(this.t));
|
2015-05-05 16:52:39 +02:00
|
|
|
|
2015-05-13 09:29:37 +02:00
|
|
|
this.t += 0.01 * time / 20;
|
2015-05-22 10:03:34 +02:00
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
if (this.t > 1) {
|
|
|
|
this.movingHermite = false;
|
|
|
|
this.anglesFromVectors();
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 11:36:15 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Update the camera according to the user's input
|
|
|
|
* @param {Number} time number of milliseconds between the previous and the next frame
|
|
|
|
*/
|
2015-05-13 09:11:54 +02:00
|
|
|
PointerCamera.prototype.normalMotion = function(time) {
|
2015-06-25 10:56:20 +02:00
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
// Update angles
|
2015-06-11 11:49:42 +02:00
|
|
|
if (this.motion.increasePhi) {this.phi += this.sensitivity * time / 20; this.changed = true; }
|
|
|
|
if (this.motion.decreasePhi) {this.phi -= this.sensitivity * time / 20; this.changed = true; }
|
|
|
|
if (this.motion.increaseTheta) {this.theta += this.sensitivity * time / 20; this.changed = true; }
|
|
|
|
if (this.motion.decreaseTheta) {this.theta -= this.sensitivity * time / 20; this.changed = true; }
|
2015-04-30 14:53:55 +02:00
|
|
|
|
2015-06-25 10:56:20 +02:00
|
|
|
if ( this.isLocked() || this.dragging) {
|
|
|
|
|
|
|
|
this.theta += (this.mouseMove.x * 20 / time);
|
|
|
|
this.phi -= (this.mouseMove.y * 20 / time);
|
2015-04-08 11:36:15 +02:00
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
this.mouseMove.x = 0;
|
|
|
|
this.mouseMove.y = 0;
|
|
|
|
|
2015-06-25 10:56:20 +02:00
|
|
|
this.vectorsFromAngles();
|
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
this.changed = true;
|
2015-06-22 09:41:59 +02:00
|
|
|
|
|
|
|
if (this.shouldLogCameraAngles) {
|
|
|
|
|
|
|
|
this.shouldLogCameraAngles = false;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
setTimeout(function() {
|
|
|
|
self.shouldLogCameraAngles = true;
|
2015-06-22 10:09:52 +02:00
|
|
|
}, 100);
|
2015-06-22 09:41:59 +02:00
|
|
|
|
2015-06-22 10:09:52 +02:00
|
|
|
var event = new BD.Event.KeyboardEvent();
|
2015-06-22 09:41:59 +02:00
|
|
|
event.camera = this;
|
|
|
|
|
|
|
|
}
|
2015-05-11 10:20:19 +02:00
|
|
|
}
|
2015-04-08 11:36:15 +02:00
|
|
|
|
2015-05-11 10:20:19 +02:00
|
|
|
// Clamp phi and theta
|
|
|
|
this.phi = Math.min(Math.max(-(Math.PI/2-0.1),this.phi), Math.PI/2-0.1);
|
|
|
|
this.theta = ((this.theta - Math.PI) % (2*Math.PI)) + Math.PI;
|
|
|
|
|
|
|
|
// Compute vectors (position and target)
|
|
|
|
this.vectorsFromAngles();
|
|
|
|
|
|
|
|
// Update with events
|
|
|
|
var delta = 0.1;
|
|
|
|
var forward = this.forward.clone();
|
|
|
|
forward.multiplyScalar(400.0 * delta);
|
|
|
|
var left = this.up.clone();
|
|
|
|
left.cross(forward);
|
|
|
|
left.normalize();
|
|
|
|
left.multiplyScalar(400.0 * delta);
|
|
|
|
|
|
|
|
// Move only if no collisions
|
2015-05-13 09:29:37 +02:00
|
|
|
var speed = this.speed * time / 20;
|
2015-05-11 10:20:19 +02:00
|
|
|
var direction = new THREE.Vector3();
|
|
|
|
|
2015-05-19 14:25:35 +02:00
|
|
|
if (this.motion.boost) speed *= 10;
|
|
|
|
if (this.motion.moveForward) {direction.add(Tools.mul(forward, speed)); this.changed = true;}
|
|
|
|
if (this.motion.moveBackward) {direction.sub(Tools.mul(forward, speed)); this.changed = true;}
|
|
|
|
if (this.motion.moveLeft) {direction.add(Tools.mul(left, speed)); this.changed = true;}
|
|
|
|
if (this.motion.moveRight) {direction.sub(Tools.mul(left, speed)); this.changed = true;}
|
2015-05-11 10:20:19 +02:00
|
|
|
|
|
|
|
if (!this.collisions || !this.isColliding(direction)) {
|
|
|
|
this.position.add(direction);
|
2015-04-07 11:13:19 +02:00
|
|
|
}
|
2015-05-11 10:20:19 +02:00
|
|
|
|
|
|
|
// Update angle
|
|
|
|
this.target = this.position.clone();
|
|
|
|
this.target.add(forward);
|
2015-04-17 10:33:41 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Reset the camera to its resetElements, and finishes any motion
|
|
|
|
*/
|
2015-04-17 10:33:41 +02:00
|
|
|
PointerCamera.prototype.reset = function() {
|
2015-05-26 11:49:24 +02:00
|
|
|
this.resetPosition();
|
2015-05-12 16:02:42 +02:00
|
|
|
this.moving = false;
|
|
|
|
this.movingHermite = false;
|
2015-05-20 15:20:59 +02:00
|
|
|
(new BD.Event.ResetClicked()).send();
|
2015-05-11 12:04:37 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Reset the position of th camera
|
|
|
|
*/
|
2015-05-26 11:49:24 +02:00
|
|
|
PointerCamera.prototype.resetPosition = function() {
|
|
|
|
this.position.copy(this.resetElements.position);
|
|
|
|
this.target.copy(this.resetElements.target);
|
2015-04-17 10:33:41 +02:00
|
|
|
this.anglesFromVectors();
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Computes the vectors (forward, left, ...) according to theta and phi
|
|
|
|
*/
|
2015-04-17 10:33:41 +02:00
|
|
|
PointerCamera.prototype.vectorsFromAngles = function() {
|
|
|
|
// Update direction
|
|
|
|
this.forward.y = Math.sin(this.phi);
|
|
|
|
|
|
|
|
var cos = Math.cos(this.phi);
|
|
|
|
this.forward.z = cos * Math.cos(this.theta);
|
|
|
|
this.forward.x = cos * Math.sin(this.theta);
|
|
|
|
this.forward.normalize();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Computes theta and phi according to the vectors (forward, left, ...)
|
|
|
|
*/
|
2015-04-17 10:33:41 +02:00
|
|
|
PointerCamera.prototype.anglesFromVectors = function() {
|
|
|
|
var forward = Tools.diff(this.target, this.position);
|
|
|
|
forward.normalize();
|
2015-04-07 11:13:19 +02:00
|
|
|
|
2015-04-17 10:33:41 +02:00
|
|
|
this.phi = Math.asin(forward.y);
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-04-17 10:33:41 +02:00
|
|
|
// Don't know why this line works... But thanks Thierry-san and
|
|
|
|
// Bastien because it seems to work...
|
|
|
|
this.theta = Math.atan2(forward.x, forward.z);
|
2015-04-08 11:36:15 +02:00
|
|
|
}
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Creates a linear motion to another camera
|
|
|
|
* @param {Camera} camera Camera to move to
|
|
|
|
* @param {Boolean} [toSave=true] true if you want to save the current state of the camera
|
|
|
|
*/
|
2015-04-30 14:53:55 +02:00
|
|
|
PointerCamera.prototype.move = function(otherCamera, toSave) {
|
|
|
|
if (toSave === undefined)
|
|
|
|
toSave = true;
|
|
|
|
|
2015-04-08 11:36:15 +02:00
|
|
|
this.moving = true;
|
|
|
|
this.new_target = otherCamera.target.clone();
|
|
|
|
this.new_position = otherCamera.position.clone();
|
2015-04-16 12:23:47 +02:00
|
|
|
var t = [0,1];
|
|
|
|
var f = [this.position.clone(), this.new_position];
|
|
|
|
var fp = [Tools.diff(this.target, this.position), Tools.diff(this.new_target, this.new_position)];
|
|
|
|
this.hermite = new Hermite.Polynom(t,f,fp);
|
|
|
|
this.t = 0;
|
2015-04-30 14:53:55 +02:00
|
|
|
|
|
|
|
if (toSave) {
|
|
|
|
if (this.changed) {
|
|
|
|
this.save();
|
|
|
|
this.changed = false;
|
|
|
|
}
|
|
|
|
this.history.addState({position: otherCamera.position.clone(), target: otherCamera.target.clone()});
|
|
|
|
}
|
2015-04-02 12:38:06 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Creates a hermite motion to another camera
|
|
|
|
* @param {Camera} camera Camera to move to
|
|
|
|
* @param {Boolean} [toSave=true] true if you want to save the current state of the camera
|
|
|
|
*/
|
2015-05-05 16:52:39 +02:00
|
|
|
PointerCamera.prototype.moveHermite = function(otherCamera, toSave) {
|
|
|
|
if (toSave === undefined)
|
|
|
|
toSave = true;
|
|
|
|
|
|
|
|
this.movingHermite = true;
|
|
|
|
this.t = 0;
|
|
|
|
|
|
|
|
this.hermitePosition = new Hermite.special.Polynom(
|
|
|
|
this.position.clone(),
|
|
|
|
otherCamera.position.clone(),
|
2015-05-11 10:20:19 +02:00
|
|
|
Tools.mul(Tools.diff(otherCamera.target, otherCamera.position).normalize(),4)
|
2015-05-05 16:52:39 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
this.hermiteAngles = new Hermite.special.Polynom(
|
|
|
|
Tools.diff(this.target, this.position),
|
|
|
|
Tools.diff(otherCamera.target, otherCamera.position),
|
|
|
|
new THREE.Vector3()
|
|
|
|
);
|
|
|
|
|
|
|
|
if (toSave) {
|
|
|
|
if (this.changed) {
|
|
|
|
this.save();
|
|
|
|
this.changed = false;
|
|
|
|
}
|
|
|
|
this.history.addState({position: otherCamera.position.clone(), target: otherCamera.target.clone()});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Checks the collisions between the collidables objects and the camera
|
|
|
|
* @param {THREE.Vector3} direction the direction of the camera
|
|
|
|
* @returns {Boolean} true if there is a collision, false otherwise
|
|
|
|
*/
|
2015-04-22 10:17:54 +02:00
|
|
|
PointerCamera.prototype.isColliding = function(direction) {
|
2015-06-16 15:14:43 +02:00
|
|
|
this.raycaster.set(this.position, direction.clone().normalize());
|
|
|
|
var intersects = this.raycaster.intersectObjects(this.collidableObjects, true);
|
|
|
|
|
|
|
|
for (var i in intersects) {
|
|
|
|
if (intersects[i].distance < Tools.norm(direction) + this.speed * 300 &&
|
|
|
|
intersects[i].object.raycastable) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2015-04-22 10:17:54 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Look method. Equivalent to gluLookAt for the current camera
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
PointerCamera.prototype.look = function() {
|
|
|
|
this.lookAt(this.target);
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Adds the camera to the scene
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
PointerCamera.prototype.addToScene = function(scene) {
|
|
|
|
scene.add(this);
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Manages keyboard events
|
|
|
|
* @param {event} event the event that happened
|
|
|
|
* @param {Booelean} toSet true if the key was pressed, false if released
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
PointerCamera.prototype.onKeyEvent = function(event, toSet) {
|
2015-05-19 14:25:35 +02:00
|
|
|
// Create copy of state
|
|
|
|
var motionJsonCopy = JSON.stringify(this.motion);
|
|
|
|
|
2015-04-02 12:38:06 +02:00
|
|
|
switch ( event.keyCode ) {
|
|
|
|
// Azerty keyboards
|
2015-05-19 14:25:35 +02:00
|
|
|
case 38: case 90: this.motion.moveForward = toSet; break; // up / z
|
|
|
|
case 37: case 81: this.motion.moveLeft = toSet; break; // left / q
|
|
|
|
case 40: case 83: this.motion.moveBackward = toSet; break; // down / s
|
|
|
|
case 39: case 68: this.motion.moveRight = toSet; break; // right / d
|
|
|
|
case 32: this.motion.boost = toSet; break;
|
2015-04-02 12:38:06 +02:00
|
|
|
|
|
|
|
// Qwerty keyboards
|
2015-05-19 14:25:35 +02:00
|
|
|
case 38: case 87: this.motion.moveForward = toSet; break; // up / w
|
|
|
|
case 37: case 65: this.motion.moveLeft = toSet; break; // left / a
|
|
|
|
case 40: case 83: this.motion.moveBackward = toSet; break; // down / s
|
|
|
|
case 39: case 68: this.motion.moveRight = toSet; break; // right / d
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-05-19 14:25:35 +02:00
|
|
|
case 73: case 104: this.motion.increasePhi = toSet; break; // 8 Up for angle
|
|
|
|
case 75: case 98: this.motion.decreasePhi = toSet; break; // 2 Down for angle
|
|
|
|
case 74: case 100: this.motion.increaseTheta = toSet; break; // 4 Left for angle
|
|
|
|
case 76: case 102: this.motion.decreaseTheta = toSet; break; // 6 Right for angle
|
2015-04-16 12:23:47 +02:00
|
|
|
|
|
|
|
case 13: if (toSet) this.log(); break;
|
2015-04-02 12:38:06 +02:00
|
|
|
}
|
2015-05-19 14:25:35 +02:00
|
|
|
if (motionJsonCopy != JSON.stringify(this.motion)) {
|
|
|
|
// Log any change
|
|
|
|
var event = new BD.Event.KeyboardEvent();
|
|
|
|
event.camera = this;
|
|
|
|
event.send();
|
|
|
|
}
|
2015-04-02 12:38:06 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Manages the key pressed events
|
|
|
|
* @param {event} event the event to manage
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
PointerCamera.prototype.onKeyDown = function(event) {
|
|
|
|
this.onKeyEvent(event, true);
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Manages the key released events
|
|
|
|
* @param {event} event the event to manage
|
|
|
|
*/
|
2015-04-02 12:38:06 +02:00
|
|
|
PointerCamera.prototype.onKeyUp = function(event) {
|
|
|
|
this.onKeyEvent(event, false);
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Manages the mouse down events. Start drag'n'dropping if the options are set to drag'n'drop
|
|
|
|
* @param {event} event the event to manage
|
|
|
|
*/
|
2015-04-07 11:13:19 +02:00
|
|
|
PointerCamera.prototype.onMouseDown = function(event) {
|
2015-06-25 10:56:20 +02:00
|
|
|
|
2015-06-19 17:45:52 +02:00
|
|
|
if (!this.shouldLock) {
|
2015-06-25 10:56:20 +02:00
|
|
|
|
2015-06-19 17:45:52 +02:00
|
|
|
this.mouse.x = ( ( event.clientX - this.renderer.domElement.offsetLeft ) / this.renderer.domElement.width ) * 2 - 1;
|
|
|
|
this.mouse.y = - ( ( event.clientY - this.renderer.domElement.offsetTop ) / this.renderer.domElement.height ) * 2 + 1;
|
2015-04-07 11:13:19 +02:00
|
|
|
|
2015-06-19 17:45:52 +02:00
|
|
|
this.dragging = true;
|
|
|
|
this.mouseMoved = false;
|
|
|
|
}
|
2015-04-07 11:13:19 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Manages the mouse move events. Modifies the target of the camera according to the drag'n'drop motion
|
|
|
|
* @param {event} event the event to manage
|
|
|
|
*/
|
2015-04-07 11:13:19 +02:00
|
|
|
PointerCamera.prototype.onMouseMove = function(event) {
|
2015-06-25 10:56:20 +02:00
|
|
|
|
2015-06-19 17:45:52 +02:00
|
|
|
if (!this.shouldLock && this.dragging) {
|
2015-04-07 11:13:19 +02:00
|
|
|
var mouse = {x: this.mouse.x, y: this.mouse.y};
|
2015-06-02 11:01:28 +02:00
|
|
|
this.mouse.x = ( ( event.clientX - this.renderer.domElement.offsetLeft ) / this.renderer.domElement.width ) * 2 - 1;
|
|
|
|
this.mouse.y = - ( ( event.clientY - this.renderer.domElement.offsetTop ) / this.renderer.domElement.height ) * 2 + 1;
|
2015-04-07 11:13:19 +02:00
|
|
|
|
2015-04-08 11:36:15 +02:00
|
|
|
this.mouseMove.x = this.mouse.x - mouse.x;
|
|
|
|
this.mouseMove.y = this.mouse.y - mouse.y;
|
2015-06-25 14:14:50 +02:00
|
|
|
|
2015-05-20 15:20:59 +02:00
|
|
|
this.mouseMoved = true;
|
2015-04-07 11:13:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Manages the mouse move envent in case of pointer lock
|
|
|
|
* @param {event} event the event to manage
|
|
|
|
*/
|
2015-06-19 15:54:09 +02:00
|
|
|
PointerCamera.prototype.onMouseMovePointer = function(e) {
|
|
|
|
|
2015-06-25 14:14:50 +02:00
|
|
|
if (this.isLocked()) {
|
2015-06-25 10:56:20 +02:00
|
|
|
|
|
|
|
// Backup theta and phi
|
|
|
|
this.previousTheta = this.theta;
|
|
|
|
this.previousPhi = this.phi;
|
2015-06-19 15:54:09 +02:00
|
|
|
|
|
|
|
this.mouseMove.x = e.movementX || e.mozMovementX || e.webkitMovementX || 0;
|
|
|
|
this.mouseMove.y = e.movementY || e.mozMovementY || e.webkitMovementY || 0;
|
|
|
|
|
2015-06-22 09:41:59 +02:00
|
|
|
this.mouseMove.x *= -(this.sensitivity/5);
|
|
|
|
this.mouseMove.y *= (this.sensitivity/5);
|
2015-06-25 14:14:50 +02:00
|
|
|
|
2015-06-19 15:54:09 +02:00
|
|
|
this.mouseMoved = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Manages the mouse up event. Stops the dragging
|
|
|
|
* @param {event} event the event to manage
|
|
|
|
*/
|
2015-04-07 11:13:19 +02:00
|
|
|
PointerCamera.prototype.onMouseUp = function(event) {
|
|
|
|
this.onMouseMove(event);
|
2015-05-20 15:20:59 +02:00
|
|
|
|
|
|
|
// Send log to DB
|
|
|
|
if (this.dragging && this.mouseMoved && !this.moving && !this.movingHermite) {
|
|
|
|
var event = new BD.Event.KeyboardEvent();
|
|
|
|
event.camera = this;
|
|
|
|
event.send();
|
|
|
|
}
|
|
|
|
|
2015-04-07 11:13:19 +02:00
|
|
|
this.dragging = false;
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Logs the camera to the terminal (pratical to create recommended views)
|
|
|
|
*/
|
2015-04-16 12:23:47 +02:00
|
|
|
PointerCamera.prototype.log = function() {
|
2015-05-11 16:47:45 +02:00
|
|
|
console.log("createCamera(\nnew THREE.Vector3(" + this.position.x + "," + this.position.y + ',' + this.position.z + '),\n'
|
|
|
|
+ "new THREE.Vector3(" + this.target.x + "," + this.target.y + ',' + this.target.z + ')\n)');
|
2015-04-16 12:23:47 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Save the current state of the camera in the history
|
|
|
|
*/
|
2015-04-29 15:51:25 +02:00
|
|
|
PointerCamera.prototype.save = function() {
|
2015-04-30 14:53:55 +02:00
|
|
|
var backup = {};
|
|
|
|
backup.position = this.position.clone();
|
|
|
|
backup.target = this.target.clone();
|
|
|
|
this.history.addState(backup);
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Undo last motion according to the history
|
|
|
|
*/
|
2015-04-30 14:53:55 +02:00
|
|
|
PointerCamera.prototype.undo = function() {
|
|
|
|
var move = this.history.undo();
|
2015-05-20 16:28:53 +02:00
|
|
|
if (move !== undefined) {
|
|
|
|
var event = new BD.Event.PreviousNextClicked();
|
|
|
|
event.previous = true;
|
|
|
|
event.camera = move;
|
|
|
|
event.send();
|
|
|
|
|
2015-04-30 14:53:55 +02:00
|
|
|
this.move(move, false);
|
2015-05-20 16:28:53 +02:00
|
|
|
}
|
2015-04-30 14:53:55 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Redo last motion according to the history
|
|
|
|
*/
|
2015-04-30 14:53:55 +02:00
|
|
|
PointerCamera.prototype.redo = function() {
|
|
|
|
var move = this.history.redo();
|
2015-05-20 16:28:53 +02:00
|
|
|
if (move !== undefined) {
|
|
|
|
var event = new BD.Event.PreviousNextClicked();
|
|
|
|
event.previous = false;
|
|
|
|
event.camera = move;
|
|
|
|
event.send();
|
|
|
|
|
2015-04-30 14:53:55 +02:00
|
|
|
this.move(move, false);
|
2015-05-20 16:28:53 +02:00
|
|
|
}
|
2015-04-29 15:51:25 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Checks if there is a undo possibility in the history
|
|
|
|
* @returns {Boolean} true if undo is possible, false otherwise
|
|
|
|
*/
|
2015-04-30 14:53:55 +02:00
|
|
|
PointerCamera.prototype.undoable = function() {
|
|
|
|
return this.history.undoable();
|
|
|
|
}
|
|
|
|
|
2015-06-23 15:31:23 +02:00
|
|
|
/**
|
|
|
|
* Checks if there is a redo possibility in the history
|
|
|
|
* @returns {Boolean} true if redo is possible, false otherwise
|
|
|
|
*/
|
2015-04-30 14:53:55 +02:00
|
|
|
PointerCamera.prototype.redoable = function() {
|
|
|
|
return this.history.redoable();
|
2015-04-29 15:51:25 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 09:22:32 +02:00
|
|
|
PointerCamera.prototype.toList = function() {
|
|
|
|
this.updateMatrix();
|
|
|
|
this.updateMatrixWorld();
|
|
|
|
|
|
|
|
var frustum = new THREE.Frustum();
|
|
|
|
var projScreenMatrix = new THREE.Matrix4();
|
|
|
|
projScreenMatrix.multiplyMatrices(this.projectionMatrix, this.matrixWorldInverse);
|
|
|
|
|
|
|
|
frustum.setFromMatrix(new THREE.Matrix4().multiplyMatrices(this.projectionMatrix, this.matrixWorldInverse));
|
|
|
|
|
|
|
|
var ret =
|
|
|
|
[[this.position.x, this.position.y, this.position.z],
|
|
|
|
[this.target.x, this.target.y, this.target.z]];
|
|
|
|
|
|
|
|
for (var i = 0; i < frustum.planes.length; i++) {
|
|
|
|
|
|
|
|
var p = frustum.planes[i];
|
|
|
|
|
|
|
|
ret.push([
|
|
|
|
p.normal.x, p.normal.y, p.normal.z, p.constant
|
|
|
|
]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|