2015-04-02 12:38:06 +02:00
|
|
|
// Initialization
|
|
|
|
|
|
|
|
// class camera extends THREE.PerspectiveCamera
|
2015-05-11 09:31:10 +02:00
|
|
|
var ArrowCamera = function(arg1, arg2, arg3, arg4, position, target) {
|
2015-04-02 12:38:06 +02:00
|
|
|
THREE.PerspectiveCamera.apply(this, arguments);
|
|
|
|
|
|
|
|
// Set Position
|
2015-04-07 12:04:59 +02:00
|
|
|
if (position === undefined) {
|
2015-04-02 12:38:06 +02:00
|
|
|
this.position = new THREE.Vector3(0,0,5);
|
2015-04-07 11:33:33 +02:00
|
|
|
} else {
|
2015-04-02 12:38:06 +02:00
|
|
|
this.position.x = position.x;
|
|
|
|
this.position.y = position.y;
|
|
|
|
this.position.z = position.z;
|
|
|
|
}
|
|
|
|
|
2015-04-07 12:04:59 +02:00
|
|
|
if (target === undefined)
|
2015-04-07 11:33:33 +02:00
|
|
|
target = new THREE.Vector3(0,0,0);
|
2015-04-02 12:38:06 +02:00
|
|
|
|
|
|
|
var direction = target.clone();
|
|
|
|
direction.sub(this.position);
|
|
|
|
direction.normalize();
|
|
|
|
|
2015-05-11 09:31:10 +02:00
|
|
|
this.center = this.position.clone();
|
|
|
|
this.center.sub(direction);
|
|
|
|
|
2015-04-02 12:38:06 +02:00
|
|
|
this.target = this.position.clone();
|
2015-04-16 12:23:47 +02:00
|
|
|
this.target.add(Tools.mul(direction,20));
|
2015-04-02 12:38:06 +02:00
|
|
|
|
2015-05-11 09:31:10 +02:00
|
|
|
|
2015-05-11 14:37:41 +02:00
|
|
|
this.arrow = new THREE.Mesh(new THREE.Geometry(), new THREE.MeshLambertMaterial({color: 0x0000ff, side:THREE.BackSide}));
|
2015-05-11 09:31:10 +02:00
|
|
|
|
|
|
|
this.object3D = new THREE.Object3D();
|
|
|
|
this.object3D.add(this.initExtremity());
|
|
|
|
this.object3D.add(this.arrow);
|
|
|
|
|
|
|
|
this.fullArrow = false;
|
|
|
|
}
|
|
|
|
ArrowCamera.prototype = Object.create(THREE.PerspectiveCamera.prototype);
|
|
|
|
ArrowCamera.prototype.constructor = ArrowCamera;
|
|
|
|
|
|
|
|
ArrowCamera.prototype.initExtremity = function() {
|
2015-04-02 12:38:06 +02:00
|
|
|
var geometry = new THREE.Geometry();
|
|
|
|
|
2015-05-11 09:31:10 +02:00
|
|
|
var direction = this.target.clone();
|
|
|
|
direction.sub(this.position);
|
|
|
|
direction.normalize();
|
|
|
|
|
2015-04-02 12:38:06 +02:00
|
|
|
var left = Tools.cross(direction, this.up);
|
|
|
|
var other = Tools.cross(direction, left);
|
2015-04-16 12:23:47 +02:00
|
|
|
|
|
|
|
|
2015-04-02 12:38:06 +02:00
|
|
|
left.normalize();
|
|
|
|
other.normalize();
|
2015-04-16 12:23:47 +02:00
|
|
|
left = Tools.mul(left, 0.2);
|
|
|
|
other = Tools.mul(other, 0.2);
|
|
|
|
|
2015-04-17 10:14:48 +02:00
|
|
|
geometry.vertices.push(Tools.sum( Tools.sum( this.position, left), other),
|
|
|
|
Tools.diff(Tools.sum( this.position, other), left),
|
|
|
|
Tools.diff(Tools.diff(this.position, left), other),
|
|
|
|
Tools.sum( Tools.diff(this.position, other), left),
|
|
|
|
Tools.sum(this.position, direction)
|
2015-04-02 12:38:06 +02:00
|
|
|
);
|
|
|
|
|
2015-04-29 16:45:11 +02:00
|
|
|
geometry.faces.push(new THREE.Face3(0,2,1), // new THREE.Face3(0,2,1),
|
|
|
|
new THREE.Face3(0,3,2), // new THREE.Face3(0,3,2)
|
2015-04-16 12:23:47 +02:00
|
|
|
new THREE.Face3(4,1,2),
|
|
|
|
new THREE.Face3(4,0,1),
|
|
|
|
new THREE.Face3(4,3,0),
|
|
|
|
new THREE.Face3(4,2,3)
|
2015-04-02 12:38:06 +02:00
|
|
|
);
|
|
|
|
|
2015-04-16 12:23:47 +02:00
|
|
|
geometry.computeFaceNormals();
|
|
|
|
|
|
|
|
var material = new THREE.MeshLambertMaterial({
|
2015-05-11 14:37:41 +02:00
|
|
|
color : 0x0000ff,
|
2015-04-02 12:38:06 +02:00
|
|
|
transparent : true,
|
|
|
|
opacity : 0.5,
|
2015-04-29 16:45:11 +02:00
|
|
|
side: THREE.FrontSide
|
2015-04-02 12:38:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.mesh = new THREE.Mesh(geometry, material);
|
2015-05-11 09:31:10 +02:00
|
|
|
return this.mesh;
|
2015-04-02 12:38:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update function
|
2015-05-11 09:31:10 +02:00
|
|
|
ArrowCamera.prototype.update = function(mainCamera) {
|
2015-04-13 10:11:42 +02:00
|
|
|
// Compute distance between center of camera and position
|
2015-04-17 10:14:48 +02:00
|
|
|
dist = Tools.norm2(Tools.diff(mainCamera.position, this.center));
|
2015-04-13 10:11:42 +02:00
|
|
|
|
2015-04-16 12:23:47 +02:00
|
|
|
var low_bound = 1;
|
|
|
|
var high_bound = 5;
|
|
|
|
var new_value;
|
|
|
|
|
|
|
|
if (dist < low_bound) {
|
|
|
|
new_value = 0;
|
|
|
|
}
|
|
|
|
else if (dist > high_bound) {
|
|
|
|
new_value = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
new_value = (dist - low_bound)/(high_bound - low_bound);
|
|
|
|
}
|
2015-04-13 10:11:42 +02:00
|
|
|
|
2015-04-16 12:23:47 +02:00
|
|
|
// Update opacity
|
2015-04-20 16:34:41 +02:00
|
|
|
this.object3D.traverse(function(elt) {
|
|
|
|
if (elt instanceof THREE.Mesh) {
|
|
|
|
elt.material.transparent = new_value < 0.9;
|
|
|
|
elt.material.opacity = new_value;
|
2015-04-23 10:43:58 +02:00
|
|
|
|
2015-04-23 10:50:18 +02:00
|
|
|
if (new_value < 0.1)
|
2015-04-23 10:48:40 +02:00
|
|
|
elt.material.transparent = elt.visible = false;
|
2015-04-20 16:34:41 +02:00
|
|
|
}
|
|
|
|
});
|
2015-04-17 10:14:48 +02:00
|
|
|
|
|
|
|
this.regenerateArrow(mainCamera);
|
|
|
|
}
|
|
|
|
|
2015-05-11 09:31:10 +02:00
|
|
|
ArrowCamera.prototype.regenerateArrow = function(mainCamera) {
|
2015-04-17 10:14:48 +02:00
|
|
|
var vertices = new Array();
|
2015-05-12 10:03:45 +02:00
|
|
|
|
|
|
|
// First point of curve
|
2015-04-22 14:33:40 +02:00
|
|
|
var f0 = mainCamera.position.clone();
|
|
|
|
f0.add(Tools.sum(Tools.mul(this.up,-1), Tools.diff(this.target, this.position).normalize()));
|
2015-04-17 11:22:11 +02:00
|
|
|
|
2015-05-12 10:03:45 +02:00
|
|
|
// Last point of curve
|
|
|
|
var f1 = this.position.clone();
|
|
|
|
|
|
|
|
// Last derivative of curve
|
|
|
|
var fp1 = Tools.diff(this.target, this.position);
|
|
|
|
fp1.normalize();
|
|
|
|
|
|
|
|
// Camera direction
|
|
|
|
var dir = Tools.diff(this.position, mainCamera.position);
|
|
|
|
dir.normalize();
|
|
|
|
|
|
|
|
if (fp1.dot(dir) < -0.5) {
|
|
|
|
// Regen polynom with better stuff
|
|
|
|
var new_dir = Tools.cross(Tools.diff(this.position, mainCamera.position).normalize(), mainCamera.up);
|
|
|
|
new_dir.multiplyScalar(new_dir.dot(fp1) < 0 ? 1 : -1);
|
|
|
|
new_dir.add(dir);
|
|
|
|
new_dir.add(dir);
|
|
|
|
new_dir.multiplyScalar(2);
|
|
|
|
f0.add(new_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
fp1.multiplyScalar(4);
|
2015-04-17 11:22:11 +02:00
|
|
|
|
2015-05-12 10:03:45 +02:00
|
|
|
var hermite = new Hermite.special.Polynom(f0, f1, fp1);
|
2015-04-17 10:14:48 +02:00
|
|
|
|
2015-04-17 16:42:30 +02:00
|
|
|
var up = this.up.clone();
|
2015-04-20 16:34:41 +02:00
|
|
|
var point;
|
|
|
|
var deriv;
|
2015-04-23 17:49:59 +02:00
|
|
|
var limit = this.fullArrow ? 0.1 : 0.3;
|
2015-04-21 09:37:32 +02:00
|
|
|
|
2015-04-20 16:34:41 +02:00
|
|
|
// for (var i = this.fullArrow ? 0 : 0.5; i <= 1.001; i += 0.05) {
|
2015-05-11 16:47:45 +02:00
|
|
|
for (var i = 1; i > limit; i -= 0.05) {
|
2015-04-20 16:34:41 +02:00
|
|
|
point = hermite.eval(i);
|
|
|
|
deriv = hermite.prime(i);
|
|
|
|
up.cross(deriv);
|
|
|
|
up.cross(deriv);
|
|
|
|
up.multiplyScalar(-1);
|
|
|
|
up.normalize();
|
|
|
|
|
2015-04-22 11:13:59 +02:00
|
|
|
var coeff = 0.1;
|
2015-04-22 10:17:54 +02:00
|
|
|
var left = Tools.cross(up, deriv); left.normalize(); left.multiplyScalar(coeff);
|
|
|
|
var other = Tools.cross(deriv, left); other.normalize(); other.multiplyScalar(coeff);
|
2015-04-17 16:42:30 +02:00
|
|
|
|
|
|
|
vertices.push(
|
|
|
|
Tools.sum(Tools.sum(point, left), other),
|
|
|
|
Tools.sum(Tools.diff(point, left), other),
|
|
|
|
Tools.diff(point, Tools.sum(other,left)),
|
|
|
|
Tools.sum(Tools.diff(point, other), left)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
var faces = new Array();
|
|
|
|
|
|
|
|
for (var i = 0; i < vertices.length - 4; i+= 4) {
|
|
|
|
faces.push(new THREE.Face3(i,i+1,i+5),new THREE.Face3(i,i+5,i+4),
|
|
|
|
new THREE.Face3(i+1,i+2,i+6),new THREE.Face3(i+1,i+6,i+5),
|
|
|
|
new THREE.Face3(i+2,i+3,i+7),new THREE.Face3(i+2,i+7,i+6),
|
|
|
|
new THREE.Face3(i,i+7,i+3), new THREE.Face3(i,i+4,i+7));
|
2015-04-17 10:14:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 16:34:41 +02:00
|
|
|
var len = vertices.length;
|
|
|
|
faces.push(new THREE.Face3(len-4,len-3,len-2), new THREE.Face3(len-4,len-2,len-1));
|
|
|
|
|
2015-04-17 16:42:30 +02:00
|
|
|
|
2015-04-17 10:14:48 +02:00
|
|
|
this.arrow.geometry.vertices = vertices;
|
2015-04-17 16:42:30 +02:00
|
|
|
this.arrow.geometry.faces = faces;
|
|
|
|
|
2015-04-21 09:37:32 +02:00
|
|
|
// this.arrow.geometry.mergeVertices();
|
2015-04-17 16:42:30 +02:00
|
|
|
this.arrow.geometry.computeFaceNormals();
|
2015-04-20 10:05:54 +02:00
|
|
|
// this.arrow.geometry.computeVertexNormals();
|
|
|
|
this.arrow.geometry.computeBoundingSphere();
|
2015-04-17 10:14:48 +02:00
|
|
|
|
|
|
|
// this.arrow.geometry.vertices[0] = new THREE.Vector3(); // mainCamera.position.clone();
|
|
|
|
// this.arrow.geometry.vertices[1] = this.position.clone();
|
|
|
|
|
|
|
|
this.arrow.geometry.dynamic = true;
|
|
|
|
this.arrow.geometry.verticesNeedUpdate = true;
|
|
|
|
this.arrow.geometry.elementsNeedUpdate = true;
|
|
|
|
this.arrow.geometry.groupsNeedUpdate = true;
|
|
|
|
this.arrow.geometry.normalsNeedUpdate = true;
|
2015-04-24 17:00:37 +02:00
|
|
|
// this.arrow.geometry.facesNeedUpdate = true;
|
2015-04-17 10:14:48 +02:00
|
|
|
|
2015-04-02 12:38:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look function
|
2015-05-11 09:31:10 +02:00
|
|
|
ArrowCamera.prototype.look = function() {
|
2015-04-02 12:38:06 +02:00
|
|
|
this.lookAt(this.target);
|
|
|
|
}
|
|
|
|
|
2015-05-11 09:31:10 +02:00
|
|
|
ArrowCamera.prototype.addToScene = function(scene) {
|
2015-04-02 12:38:06 +02:00
|
|
|
scene.add(this);
|
2015-04-20 10:05:54 +02:00
|
|
|
scene.add(this.object3D);
|
2015-04-02 12:38:06 +02:00
|
|
|
}
|
2015-04-13 09:48:50 +02:00
|
|
|
|
2015-05-11 09:31:10 +02:00
|
|
|
ArrowCamera.prototype.traverse = function(callback) {
|
2015-04-20 10:05:54 +02:00
|
|
|
this.object3D.traverse(callback);
|
2015-04-13 09:48:50 +02:00
|
|
|
}
|
2015-05-11 09:31:10 +02:00
|
|
|
|
|
|
|
ArrowCamera.prototype.containsObject = function(object) {
|
|
|
|
return object.parent === this.object3D;
|
|
|
|
}
|