From d15740ec3a159398fbd962df653f9580a082c3b7 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Fri, 17 Apr 2015 10:14:48 +0200 Subject: [PATCH] Added arrows instead of cameras --- js/FixedCamera.js | 55 ++++++++++++++++++++++++++++++++++++-------- js/PointerCamera.js | 11 +++++---- prototype/js/main.js | 29 +++++++++++++++++++---- 3 files changed, 78 insertions(+), 17 deletions(-) diff --git a/js/FixedCamera.js b/js/FixedCamera.js index c90da3e..01d7261 100644 --- a/js/FixedCamera.js +++ b/js/FixedCamera.js @@ -30,22 +30,22 @@ var FixedCamera = function(arg1, arg2, arg3, arg4, position, target) { var geometry = new THREE.Geometry(); - var position = this.position.clone(); + this.center = this.position.clone(); var left = Tools.cross(direction, this.up); var other = Tools.cross(direction, left); - position.sub(direction); + this.center.sub(direction); left.normalize(); other.normalize(); left = Tools.mul(left, 0.2); other = Tools.mul(other, 0.2); - geometry.vertices.push(Tools.sum( Tools.sum( position, left), other), - Tools.diff(Tools.sum( position, other), left), - Tools.diff(Tools.diff(position, left), other), - Tools.sum( Tools.diff(position, other), left), - Tools.sum(position, direction) + 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) ); geometry.faces.push(new THREE.Face3(0,1,2), // new THREE.Face3(0,2,1), @@ -97,14 +97,20 @@ var FixedCamera = function(arg1, arg2, arg3, arg4, position, target) { }); this.mesh = new THREE.Mesh(geometry, material); + this.arrow = new THREE.Line(new THREE.Geometry(), new THREE.LineBasicMaterial({color: 0xff0000}), THREE.LinePieces); + this.arrow.material.linewidth=10; + this.arrow.geometry.vertices.push(new THREE.Vector3()); + this.arrow.geometry.vertices.push(new THREE.Vector3()); + this.arrow.geometry.vertices[0] = new THREE.Vector3();// mainCamera.position.clone(); + this.arrow.geometry.vertices[1] = this.position.clone(); } FixedCamera.prototype = Object.create(THREE.PerspectiveCamera.prototype); FixedCamera.prototype.constructor = FixedCamera; // Update function -FixedCamera.prototype.update = function(position) { +FixedCamera.prototype.update = function(mainCamera) { // Compute distance between center of camera and position - dist = Tools.norm2(Tools.diff(position, this.position)); + dist = Tools.norm2(Tools.diff(mainCamera.position, this.center)); var low_bound = 1; var high_bound = 5; @@ -123,8 +129,38 @@ FixedCamera.prototype.update = function(position) { // Update opacity this.mesh.material.transparent = new_value < 0.9; this.border.material.transparent = new_value < 0.9; + this.arrow.material.transparent = new_value < 0.9; this.mesh.material.opacity = new_value; this.border.material.opacity = new_value; + this.arrow.material.opacity = new_value; + + this.regenerateArrow(mainCamera); +} + +FixedCamera.prototype.regenerateArrow = function(mainCamera) { + var vertices = new Array(); + var t = [0,1]; + var f = [mainCamera.position.clone(), this.position.clone()]; + var fp = [Tools.diff(mainCamera.target, mainCamera.position), Tools.diff(this.target, this.position)]; + var hermite = new Hermite.Polynom(t,f,fp); + + vertices.push(hermite.eval(0.5)); + for (var i = 0.5; i <= 1.001; i += 0.05) { + var point = hermite.eval(i); + vertices.push(point, point); + } + + this.arrow.geometry.vertices = vertices; + + // 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; + } // Look function @@ -136,6 +172,7 @@ FixedCamera.prototype.addToScene = function(scene) { scene.add(this); scene.add(this.mesh); scene.add(this.border); + scene.add(this.arrow); } FixedCamera.prototype.traverse = function(callback) { diff --git a/js/PointerCamera.js b/js/PointerCamera.js index e70f307..471d65f 100644 --- a/js/PointerCamera.js +++ b/js/PointerCamera.js @@ -142,10 +142,12 @@ PointerCamera.prototype.update = function() { left.normalize(); left.multiplyScalar(400.0 * delta); - if (this.moveForward) this.position.add(Tools.mul(forward, this.speed)); - if (this.moveBackward) this.position.sub(Tools.mul(forward, this.speed)); - if (this.moveLeft) this.position.add(Tools.mul(left, this.speed)); - if (this.moveRight) this.position.sub(Tools.mul(left, this.speed)); + var speed = this.speed; + if (this.boost) speed *= 10; + if (this.moveForward) this.position.add(Tools.mul(forward, speed)); + if (this.moveBackward) this.position.sub(Tools.mul(forward, speed)); + if (this.moveLeft) this.position.add(Tools.mul(left, speed)); + if (this.moveRight) this.position.sub(Tools.mul(left, speed)); this.target = this.position.clone(); this.target.add(forward); @@ -181,6 +183,7 @@ PointerCamera.prototype.onKeyEvent = function(event, toSet) { case 37: case 81: this.moveLeft = toSet; break; // left / q case 40: case 83: this.moveBackward = toSet; break; // down / s case 39: case 68: this.moveRight = toSet; break; // right / d + case 32: this.boost = toSet; break; // Qwerty keyboards // case 38: case 87: this.moveForward = toSet; break; // up / w diff --git a/prototype/js/main.js b/prototype/js/main.js index 467a3a1..1f94824 100644 --- a/prototype/js/main.js +++ b/prototype/js/main.js @@ -40,12 +40,12 @@ function init() { container.appendChild(renderer.domElement); // init light - var directional_light = new THREE.DirectionalLight(0xaaaaaa); + var directional_light = new THREE.DirectionalLight(0x777777); directional_light.position.set(1, 0.5, 1).normalize(); directional_light.castShadow = true; scene.add(directional_light); - var ambient_light = new THREE.AmbientLight(0x666666); + var ambient_light = new THREE.AmbientLight(0x777777); scene.add(ambient_light); // on initialise la camera que l’on place ensuite sur la scène @@ -70,7 +70,7 @@ function init() { // THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() ); var loader = new THREE.OBJMTLLoader(); - loader.load( '/data/castle/princess peaches castle (outside).obj', + loader.load( '/data/castle/princess peaches castle (outside).obj', '/data/castle/princess peaches castle (outside).mtl', function ( object ) { object.up = new THREE.Vector3(0,0,1); @@ -114,6 +114,27 @@ function init() { }); }, onProgress, onError ); + // loader.load( '/data/bobomb/bobomb battlefeild.obj', + // '/data/bobomb/bobomb battlefeild.mtl', + // function ( object ) { + // // object.position.z -= 10.9; + // // object.position.y += 0.555; + // // object.position.x += 3.23; + + // var theta = 0.27; + // object.rotation.y = Math.PI - theta; + + // object.up = new THREE.Vector3(0,0,1); + // scene.add(object); + // object.traverse(function (object) { + // if (object instanceof THREE.Mesh) { + // object.material.side = THREE.DoubleSide; + // console.log(object.geometry.vertices.length); + // object.geometry.mergeVertices(); + // object.geometry.computeVertexNormals(); + // } + // }); + // }, onProgress, onError ); createCamera( new THREE.Vector3(-3.349895207953063, 5.148106346852601, 0.3365943929701533), @@ -179,7 +200,7 @@ function animate() { requestAnimationFrame(animate); stats.begin(); - cameras.update(cameras.mainCamera().position); + cameras.update(cameras.mainCamera()); cameras.look(); renderer.render(scene, cameras.mainCamera());