2
0

CameraHelper.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. * - shows frustum, line of sight and up of the camera
  5. * - suitable for fast updates
  6. * - based on frustum visualization in lightgl.js shadowmap example
  7. * http://evanw.github.com/lightgl.js/tests/shadowmap.html
  8. */
  9. THREE.CameraHelper = function ( camera ) {
  10. var geometry = new THREE.Geometry();
  11. var material = new THREE.LineBasicMaterial( { color: 0xffffff, vertexColors: THREE.FaceColors } );
  12. var pointMap = {};
  13. // colors
  14. var hexFrustum = 0xffaa00;
  15. var hexCone = 0xff0000;
  16. var hexUp = 0x00aaff;
  17. var hexTarget = 0xffffff;
  18. var hexCross = 0x333333;
  19. // near
  20. addLine( "n1", "n2", hexFrustum );
  21. addLine( "n2", "n4", hexFrustum );
  22. addLine( "n4", "n3", hexFrustum );
  23. addLine( "n3", "n1", hexFrustum );
  24. // far
  25. addLine( "f1", "f2", hexFrustum );
  26. addLine( "f2", "f4", hexFrustum );
  27. addLine( "f4", "f3", hexFrustum );
  28. addLine( "f3", "f1", hexFrustum );
  29. // sides
  30. addLine( "n1", "f1", hexFrustum );
  31. addLine( "n2", "f2", hexFrustum );
  32. addLine( "n3", "f3", hexFrustum );
  33. addLine( "n4", "f4", hexFrustum );
  34. // cone
  35. addLine( "p", "n1", hexCone );
  36. addLine( "p", "n2", hexCone );
  37. addLine( "p", "n3", hexCone );
  38. addLine( "p", "n4", hexCone );
  39. // up
  40. addLine( "u1", "u2", hexUp );
  41. addLine( "u2", "u3", hexUp );
  42. addLine( "u3", "u1", hexUp );
  43. // target
  44. addLine( "c", "t", hexTarget );
  45. addLine( "p", "c", hexCross );
  46. // cross
  47. addLine( "cn1", "cn2", hexCross );
  48. addLine( "cn3", "cn4", hexCross );
  49. addLine( "cf1", "cf2", hexCross );
  50. addLine( "cf3", "cf4", hexCross );
  51. function addLine( a, b, hex ) {
  52. addPoint( a, hex );
  53. addPoint( b, hex );
  54. }
  55. function addPoint( id, hex ) {
  56. geometry.vertices.push( new THREE.Vector3() );
  57. geometry.colors.push( new THREE.Color( hex ) );
  58. if ( pointMap[ id ] === undefined ) {
  59. pointMap[ id ] = [];
  60. }
  61. pointMap[ id ].push( geometry.vertices.length - 1 );
  62. }
  63. THREE.Line.call( this, geometry, material, THREE.LinePieces );
  64. this.camera = camera;
  65. this.matrixWorld = camera.matrixWorld;
  66. this.matrixAutoUpdate = false;
  67. this.pointMap = pointMap;
  68. this.update();
  69. };
  70. THREE.CameraHelper.prototype = Object.create( THREE.Line.prototype );
  71. THREE.CameraHelper.prototype.update = function () {
  72. var vector = new THREE.Vector3();
  73. var camera = new THREE.Camera();
  74. var projector = new THREE.Projector();
  75. return function () {
  76. var scope = this;
  77. var w = 1, h = 1;
  78. // we need just camera projection matrix
  79. // world matrix must be identity
  80. camera.projectionMatrix.copy( this.camera.projectionMatrix );
  81. // center / target
  82. setPoint( "c", 0, 0, - 1 );
  83. setPoint( "t", 0, 0, 1 );
  84. // near
  85. setPoint( "n1", - w, - h, - 1 );
  86. setPoint( "n2", w, - h, - 1 );
  87. setPoint( "n3", - w, h, - 1 );
  88. setPoint( "n4", w, h, - 1 );
  89. // far
  90. setPoint( "f1", - w, - h, 1 );
  91. setPoint( "f2", w, - h, 1 );
  92. setPoint( "f3", - w, h, 1 );
  93. setPoint( "f4", w, h, 1 );
  94. // up
  95. setPoint( "u1", w * 0.7, h * 1.1, - 1 );
  96. setPoint( "u2", - w * 0.7, h * 1.1, - 1 );
  97. setPoint( "u3", 0, h * 2, - 1 );
  98. // cross
  99. setPoint( "cf1", - w, 0, 1 );
  100. setPoint( "cf2", w, 0, 1 );
  101. setPoint( "cf3", 0, - h, 1 );
  102. setPoint( "cf4", 0, h, 1 );
  103. setPoint( "cn1", - w, 0, - 1 );
  104. setPoint( "cn2", w, 0, - 1 );
  105. setPoint( "cn3", 0, - h, - 1 );
  106. setPoint( "cn4", 0, h, - 1 );
  107. function setPoint( point, x, y, z ) {
  108. vector.set( x, y, z );
  109. projector.unprojectVector( vector, camera );
  110. var points = scope.pointMap[ point ];
  111. if ( points !== undefined ) {
  112. for ( var i = 0, il = points.length; i < il; i ++ ) {
  113. scope.geometry.vertices[ points[ i ] ].copy( vector );
  114. }
  115. }
  116. }
  117. this.geometry.verticesNeedUpdate = true;
  118. };
  119. }();