CameraHelper.js 5.0 KB

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