Skip to content

Instantly share code, notes, and snippets.

@lazd
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save lazd/65043426c8b01b2bdd4c to your computer and use it in GitHub Desktop.

Select an option

Save lazd/65043426c8b01b2bdd4c to your computer and use it in GitHub Desktop.
var boxMaterial = new THREE.LineBasicMaterial({
color: new THREE.Color('red')
});
// Create the lines for the "2D box"
var boxGeometry = new THREE.Geometry();
boxGeometry.vertices.push(new THREE.Vector3(-1, 0, 0));
boxGeometry.vertices.push(new THREE.Vector3( 0, 1, 0));
boxGeometry.vertices.push(new THREE.Vector3( 1, 0, 0));
boxGeometry.vertices.push(new THREE.Vector3( 0, -1, 0));
boxGeometry.vertices.push(new THREE.Vector3(-1, 0, 0));
var box = new THREE.Line(boxGeometry, boxMaterial);
box.position.z = -100;
camera.add(box);
// Position the box over an arbitrary point elsewhere in space
var ratio = window.innerWidth/window.innerHeight;
var verticalFov = this.game.camera.fov;
var horizontalFov = Math.toDegrees(2 * Math.atan(Math.tan(Math.toRadians(verticalFov)/2) * (ratio)));
// Project to NDC
var pos = targetMesh.position.project(this.game.camera);
// Sorta project back?
box.position.x = pos.x * ratio * verticalFov;
box.position.y = pos.y * 1/ratio * horizontalFov;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment