CameraHelper.js 4.1 KB

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