CameraHelper class
@author alteredq / http://alteredqualia.com/
- shows frustum, line of sight and up of the camera
- suitable for fast updates
-
based on frustum visualization in lightgl.js shadowmap example http://evanw.github.com/lightgl.js/tests/shadowmap.html
class CameraHelper extends Line {
static Projector __projector = new Projector();
static Vector3 __v = new Vector3.zero();
static Camera __c = new Camera(0.0 ,0.0);
Camera camera;
Matrix4 matrixWorld;
bool matrixAutoUpdate;
Map pointMap;
CameraHelper( this.camera ) :
matrixAutoUpdate = false,
pointMap = {},
super(
new Geometry(),
new LineBasicMaterial( color: 0xffffff, vertexColors: FaceColors ),
LinePieces) {
matrixWorld = camera.matrixWorld;
// colors
var hexFrustum = 0xffaa00;
var hexCone = 0xff0000;
var hexUp = 0x00aaff;
var hexTarget = 0xffffff;
var hexCross = 0x333333;
// near
addLine( "n1", "n2", hexFrustum );
addLine( "n2", "n4", hexFrustum );
addLine( "n4", "n3", hexFrustum );
addLine( "n3", "n1", hexFrustum );
// far
addLine( "f1", "f2", hexFrustum );
addLine( "f2", "f4", hexFrustum );
addLine( "f4", "f3", hexFrustum );
addLine( "f3", "f1", hexFrustum );
// sides
addLine( "n1", "f1", hexFrustum );
addLine( "n2", "f2", hexFrustum );
addLine( "n3", "f3", hexFrustum );
addLine( "n4", "f4", hexFrustum );
// cone
addLine( "p", "n1", hexCone );
addLine( "p", "n2", hexCone );
addLine( "p", "n3", hexCone );
addLine( "p", "n4", hexCone );
// up
addLine( "u1", "u2", hexUp );
addLine( "u2", "u3", hexUp );
addLine( "u3", "u1", hexUp );
// target
addLine( "c", "t", hexTarget );
addLine( "p", "c", hexCross );
// cross
addLine( "cn1", "cn2", hexCross );
addLine( "cn3", "cn4", hexCross );
addLine( "cf1", "cf2", hexCross );
addLine( "cf3", "cf4", hexCross );
update();
}
addLine( a, b, hex ) {
addPoint( a, hex );
addPoint( b, hex );
}
addPoint( id, hex ) {
geometry.vertices.add( new Vector3.zero() );
geometry.colors.add( new Color( hex ) );
if ( !pointMap.containsKey( id )) {
pointMap[ id ] = [];
}
pointMap[ id ].add( geometry.vertices.length - 1 );
}
setPoint( String point, double x, double y, double z ) {
__v.setValues( x, y, z );
__projector.unprojectVector( __v, __c );
var points = pointMap[ point ];
if ( points != null ) {
var il = points.length;
for ( var i = 0; i < il; i ++ ) {
geometry.vertices[ points[ i ] ].setFrom( __v );
}
}
}
update() {
var w = 1.0, h = 1.0;
// we need just camera projection matrix
// world matrix must be identity
__c.projectionMatrix.setFrom( camera.projectionMatrix );
// center / target
setPoint( "c", 0.0, 0.0, -1.0 );
setPoint( "t", 0.0, 0.0, 1.0 );
// near
setPoint( "n1", -w, -h, -1.0 );
setPoint( "n2", w, -h, -1.0 );
setPoint( "n3", -w, h, -1.0 );
setPoint( "n4", w, h, -1.0 );
// far
setPoint( "f1", -w, -h, 1.0 );
setPoint( "f2", w, -h, 1.0 );
setPoint( "f3", -w, h, 1.0 );
setPoint( "f4", w, h, 1.0 );
// up
setPoint( "u1", w * 0.7, h * 1.1, -1.0 );
setPoint( "u2", -w * 0.7, h * 1.1, -1.0 );
setPoint( "u3", 0.0, h * 2.0, -1.0 );
// cross
setPoint( "cf1", -w, 0.0, 1.0 );
setPoint( "cf2", w, 0.0, 1.0 );
setPoint( "cf3", 0.0, -h, 1.0 );
setPoint( "cf4", 0.0, h, 1.0 );
setPoint( "cn1", -w, 0.0, -1.0 );
setPoint( "cn2", w, 0.0, -1.0 );
setPoint( "cn3", 0.0, -h, -1.0 );
setPoint( "cn4", 0.0, h, -1.0 );
geometry["verticesNeedUpdate"] = true;
}
}
Extends
Object3D > Line > CameraHelper
Constructors
new CameraHelper(Camera camera) #
Creates a new Object instance.
Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.
docs inherited from Object
CameraHelper( this.camera ) :
matrixAutoUpdate = false,
pointMap = {},
super(
new Geometry(),
new LineBasicMaterial( color: 0xffffff, vertexColors: FaceColors ),
LinePieces) {
matrixWorld = camera.matrixWorld;
// colors
var hexFrustum = 0xffaa00;
var hexCone = 0xff0000;
var hexUp = 0x00aaff;
var hexTarget = 0xffffff;
var hexCross = 0x333333;
// near
addLine( "n1", "n2", hexFrustum );
addLine( "n2", "n4", hexFrustum );
addLine( "n4", "n3", hexFrustum );
addLine( "n3", "n1", hexFrustum );
// far
addLine( "f1", "f2", hexFrustum );
addLine( "f2", "f4", hexFrustum );
addLine( "f4", "f3", hexFrustum );
addLine( "f3", "f1", hexFrustum );
// sides
addLine( "n1", "f1", hexFrustum );
addLine( "n2", "f2", hexFrustum );
addLine( "n3", "f3", hexFrustum );
addLine( "n4", "f4", hexFrustum );
// cone
addLine( "p", "n1", hexCone );
addLine( "p", "n2", hexCone );
addLine( "p", "n3", hexCone );
addLine( "p", "n4", hexCone );
// up
addLine( "u1", "u2", hexUp );
addLine( "u2", "u3", hexUp );
addLine( "u3", "u1", hexUp );
// target
addLine( "c", "t", hexTarget );
addLine( "p", "c", hexCross );
// cross
addLine( "cn1", "cn2", hexCross );
addLine( "cn3", "cn4", hexCross );
addLine( "cf1", "cf2", hexCross );
addLine( "cf3", "cf4", hexCross );
update();
}
Properties
bool frustumCulled #
inherited from Object3D
bool visible = false, castShadow = false, receiveShadow = false, frustumCulled = false
bool isDynamic #
inherited from Object3D
bool get isDynamic => _dynamic;
set isDynamic(bool flag) => _dynamic = flag;
Matrix4 matrixRotationWorld #
inherited from Object3D
Matrix4 matrix, matrixWorld, matrixRotationWorld
bool matrixWorldNeedsUpdate #
inherited from Object3D
bool matrixAutoUpdate = false, matrixWorldNeedsUpdate = false
bool receiveShadow #
inherited from Object3D
bool visible = false, castShadow = false, receiveShadow = false
Operators
Methods
void add(Object3D object) #
inherited from Object3D
void add( Object3D object ) {
if ( object == this ) {
print( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
return;
}
if ( object.parent != null ) {
object.parent.remove( object );
}
object.parent = this;
children.add( object );
// add to scene
Object3D scene = this;
while ( scene.parent != null ) {
scene = scene.parent;
}
if ( scene is Scene ) {
scene.addObject( object );
}
}
dynamic addLine(a, b, hex) #
addLine( a, b, hex ) {
addPoint( a, hex );
addPoint( b, hex );
}
dynamic addPoint(id, hex) #
addPoint( id, hex ) {
geometry.vertices.add( new Vector3.zero() );
geometry.colors.add( new Color( hex ) );
if ( !pointMap.containsKey( id )) {
pointMap[ id ] = [];
}
pointMap[ id ].add( geometry.vertices.length - 1 );
}
void applyMatrix(Matrix4 matrix) #
inherited from Object3D
void applyMatrix ( Matrix4 matrix ) {
this.matrix = matrix * this.matrix;
this.scale = getScaleFromMatrix( this.matrix );
Matrix4 mat = extractRotation(new Matrix4.identity(), this.matrix );
this.rotation = calcEulerFromRotationMatrix( mat, this.eulerOrder );
this.position = this.matrix.getTranslation();
}
Object3D getChildByName(String name, bool doRecurse) #
inherited from Object3D
Object3D getChildByName( String name, bool doRecurse ) {
int c;
int cl = children.length;
Object3D child, recurseResult;
children.forEach((child){
if ( child.name == name ) {
return child;
}
if ( doRecurse ) {
recurseResult = child.getChildByName( name, doRecurse );
if ( recurseResult != null ) {
return recurseResult;
}
}
});
return null;
}
dynamic localToWorld(Vector3 vector) #
inherited from Object3D
localToWorld(Vector3 vector) => vector.applyProjection(matrixWorld);
void lookAt(Vector3 vector) #
inherited from Object3D
void lookAt( Vector3 vector ) {
// TODO: Add hierarchy support.
makeLookAt( matrix, vector, position, up );
if ( rotationAutoUpdate ) {
if(useQuaternion)
quaternion.setFromRotationMatrix(matrix);
else
rotation = calcEulerFromRotationMatrix( matrix, eulerOrder );
}
}
void remove(Object3D object) #
inherited from Object3D
void remove( Object3D object ) {
int index = children.indexOf( object );
if ( index != - 1 ){
object.parent = null;
children.removeAt(index);
// remove from scene
Object3D scene = this;
while ( scene.parent != null ) {
scene = scene.parent;
}
if (scene is Scene ) {
scene.removeObject( object );
}
}
}
dynamic setPoint(String point, double x, double y, double z) #
setPoint( String point, double x, double y, double z ) {
__v.setValues( x, y, z );
__projector.unprojectVector( __v, __c );
var points = pointMap[ point ];
if ( points != null ) {
var il = points.length;
for ( var i = 0; i < il; i ++ ) {
geometry.vertices[ points[ i ] ].setFrom( __v );
}
}
}
void translate(num distance, Vector3 axis) #
inherited from Object3D
void translate( num distance, Vector3 axis ) {
matrix.rotate3( axis );
axis.normalize();
position.add( axis.scale( distance ) );
}
void translateX(num distance) #
inherited from Object3D
void translateX( num distance ) => translate( distance, _vector.setValues( 1.0, 0.0, 0.0 ) );
void translateY(num distance) #
inherited from Object3D
void translateY( num distance ) => translate( distance, _vector.setValues( 0.0, 1.0, 0.0 ) );
void translateZ(num distance) #
inherited from Object3D
void translateZ( num distance ) => translate( distance, _vector.setValues( 0.0, 0.0, 1.0 ) );
dynamic update() #
update() {
var w = 1.0, h = 1.0;
// we need just camera projection matrix
// world matrix must be identity
__c.projectionMatrix.setFrom( camera.projectionMatrix );
// center / target
setPoint( "c", 0.0, 0.0, -1.0 );
setPoint( "t", 0.0, 0.0, 1.0 );
// near
setPoint( "n1", -w, -h, -1.0 );
setPoint( "n2", w, -h, -1.0 );
setPoint( "n3", -w, h, -1.0 );
setPoint( "n4", w, h, -1.0 );
// far
setPoint( "f1", -w, -h, 1.0 );
setPoint( "f2", w, -h, 1.0 );
setPoint( "f3", -w, h, 1.0 );
setPoint( "f4", w, h, 1.0 );
// up
setPoint( "u1", w * 0.7, h * 1.1, -1.0 );
setPoint( "u2", -w * 0.7, h * 1.1, -1.0 );
setPoint( "u3", 0.0, h * 2.0, -1.0 );
// cross
setPoint( "cf1", -w, 0.0, 1.0 );
setPoint( "cf2", w, 0.0, 1.0 );
setPoint( "cf3", 0.0, -h, 1.0 );
setPoint( "cf4", 0.0, h, 1.0 );
setPoint( "cn1", -w, 0.0, -1.0 );
setPoint( "cn2", w, 0.0, -1.0 );
setPoint( "cn3", 0.0, -h, -1.0 );
setPoint( "cn4", 0.0, h, -1.0 );
geometry["verticesNeedUpdate"] = true;
}
void updateMatrix() #
inherited from Object3D
void updateMatrix() {
if ( useQuaternion ) {
setRotationFromQuaternion( matrix, quaternion );
} else {
setRotationFromEuler( matrix, rotation, eulerOrder );
}
matrix.setTranslation( position );
if ( scale.x != 1.0 || scale.y != 1.0 || scale.z != 1.0 ) {
matrix.scale( scale );
boundRadiusScale = Math.max( scale.x, Math.max( scale.y, scale.z ) );
}
matrixWorldNeedsUpdate = true;
}
void updateMatrixWorld({bool force: false}) #
inherited from Object3D
void updateMatrixWorld( {bool force: false} ) {
if (matrixAutoUpdate) updateMatrix();
// update matrixWorld
if ( matrixWorldNeedsUpdate || force ) {
if ( parent != null ) {
matrixWorld = parent.matrixWorld * matrix;
} else {
matrixWorld = matrix.clone();
}
matrixWorldNeedsUpdate = false;
force = true;
}
// update children
children.forEach((c) => c.updateMatrixWorld( force: force ) );
}