CameraHelper.js 4.6 KB

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