CameraHelper.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { Camera } from '../cameras/Camera.js';
  2. import { Vector3 } from '../math/Vector3.js';
  3. import { LineSegments } from '../objects/LineSegments.js';
  4. import { Color } from '../math/Color.js';
  5. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  6. import { BufferGeometry } from '../core/BufferGeometry.js';
  7. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  8. const _vector = new Vector3();
  9. const _camera = new Camera();
  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. class CameraHelper extends LineSegments {
  17. constructor( camera ) {
  18. const geometry = new BufferGeometry();
  19. const material = new LineBasicMaterial( { color: 0xffffff, vertexColors: true, toneMapped: false } );
  20. const vertices = [];
  21. const colors = [];
  22. const pointMap = {};
  23. // colors
  24. const colorFrustum = new Color( 0xffaa00 );
  25. const colorCone = new Color( 0xff0000 );
  26. const colorUp = new Color( 0x00aaff );
  27. const colorTarget = new Color( 0xffffff );
  28. const colorCross = new Color( 0x333333 );
  29. // near
  30. addLine( 'n1', 'n2', colorFrustum );
  31. addLine( 'n2', 'n4', colorFrustum );
  32. addLine( 'n4', 'n3', colorFrustum );
  33. addLine( 'n3', 'n1', colorFrustum );
  34. // far
  35. addLine( 'f1', 'f2', colorFrustum );
  36. addLine( 'f2', 'f4', colorFrustum );
  37. addLine( 'f4', 'f3', colorFrustum );
  38. addLine( 'f3', 'f1', colorFrustum );
  39. // sides
  40. addLine( 'n1', 'f1', colorFrustum );
  41. addLine( 'n2', 'f2', colorFrustum );
  42. addLine( 'n3', 'f3', colorFrustum );
  43. addLine( 'n4', 'f4', colorFrustum );
  44. // cone
  45. addLine( 'p', 'n1', colorCone );
  46. addLine( 'p', 'n2', colorCone );
  47. addLine( 'p', 'n3', colorCone );
  48. addLine( 'p', 'n4', colorCone );
  49. // up
  50. addLine( 'u1', 'u2', colorUp );
  51. addLine( 'u2', 'u3', colorUp );
  52. addLine( 'u3', 'u1', colorUp );
  53. // target
  54. addLine( 'c', 't', colorTarget );
  55. addLine( 'p', 'c', colorCross );
  56. // cross
  57. addLine( 'cn1', 'cn2', colorCross );
  58. addLine( 'cn3', 'cn4', colorCross );
  59. addLine( 'cf1', 'cf2', colorCross );
  60. addLine( 'cf3', 'cf4', colorCross );
  61. function addLine( a, b, color ) {
  62. addPoint( a, color );
  63. addPoint( b, color );
  64. }
  65. function addPoint( id, color ) {
  66. vertices.push( 0, 0, 0 );
  67. colors.push( color.r, color.g, color.b );
  68. if ( pointMap[ id ] === undefined ) {
  69. pointMap[ id ] = [];
  70. }
  71. pointMap[ id ].push( ( vertices.length / 3 ) - 1 );
  72. }
  73. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  74. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  75. super( geometry, material );
  76. this.type = 'CameraHelper';
  77. this.camera = camera;
  78. if ( this.camera.updateProjectionMatrix ) this.camera.updateProjectionMatrix();
  79. this.matrix = camera.matrixWorld;
  80. this.matrixAutoUpdate = false;
  81. this.pointMap = pointMap;
  82. this.update();
  83. }
  84. update() {
  85. const geometry = this.geometry;
  86. const pointMap = this.pointMap;
  87. const w = 1, h = 1;
  88. // we need just camera projection matrix inverse
  89. // world matrix must be identity
  90. _camera.projectionMatrixInverse.copy( this.camera.projectionMatrixInverse );
  91. // center / target
  92. setPoint( 'c', pointMap, geometry, _camera, 0, 0, - 1 );
  93. setPoint( 't', pointMap, geometry, _camera, 0, 0, 1 );
  94. // near
  95. setPoint( 'n1', pointMap, geometry, _camera, - w, - h, - 1 );
  96. setPoint( 'n2', pointMap, geometry, _camera, w, - h, - 1 );
  97. setPoint( 'n3', pointMap, geometry, _camera, - w, h, - 1 );
  98. setPoint( 'n4', pointMap, geometry, _camera, w, h, - 1 );
  99. // far
  100. setPoint( 'f1', pointMap, geometry, _camera, - w, - h, 1 );
  101. setPoint( 'f2', pointMap, geometry, _camera, w, - h, 1 );
  102. setPoint( 'f3', pointMap, geometry, _camera, - w, h, 1 );
  103. setPoint( 'f4', pointMap, geometry, _camera, w, h, 1 );
  104. // up
  105. setPoint( 'u1', pointMap, geometry, _camera, w * 0.7, h * 1.1, - 1 );
  106. setPoint( 'u2', pointMap, geometry, _camera, - w * 0.7, h * 1.1, - 1 );
  107. setPoint( 'u3', pointMap, geometry, _camera, 0, h * 2, - 1 );
  108. // cross
  109. setPoint( 'cf1', pointMap, geometry, _camera, - w, 0, 1 );
  110. setPoint( 'cf2', pointMap, geometry, _camera, w, 0, 1 );
  111. setPoint( 'cf3', pointMap, geometry, _camera, 0, - h, 1 );
  112. setPoint( 'cf4', pointMap, geometry, _camera, 0, h, 1 );
  113. setPoint( 'cn1', pointMap, geometry, _camera, - w, 0, - 1 );
  114. setPoint( 'cn2', pointMap, geometry, _camera, w, 0, - 1 );
  115. setPoint( 'cn3', pointMap, geometry, _camera, 0, - h, - 1 );
  116. setPoint( 'cn4', pointMap, geometry, _camera, 0, h, - 1 );
  117. geometry.getAttribute( 'position' ).needsUpdate = true;
  118. }
  119. }
  120. function setPoint( point, pointMap, geometry, camera, x, y, z ) {
  121. _vector.set( x, y, z ).unproject( camera );
  122. const points = pointMap[ point ];
  123. if ( points !== undefined ) {
  124. const position = geometry.getAttribute( 'position' );
  125. for ( let i = 0, l = points.length; i < l; i ++ ) {
  126. position.setXYZ( points[ i ], _vector.x, _vector.y, _vector.z );
  127. }
  128. }
  129. }
  130. export { CameraHelper };