PeppersGhostEffect.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import {
  2. PerspectiveCamera,
  3. Quaternion,
  4. Vector3
  5. } from 'three';
  6. /**
  7. * A class that implements a peppers ghost effect.
  8. *
  9. * Reference: [Reflective Prism]{@link http://www.instructables.com/id/Reflective-Prism/?ALLSTEPS}
  10. */
  11. class PeppersGhostEffect {
  12. /**
  13. * Constructs a new peppers ghost effect.
  14. *
  15. * @param {(WebGPURenderer|WebGLRenderer)} renderer - The renderer.
  16. */
  17. constructor( renderer ) {
  18. const scope = this;
  19. scope.cameraDistance = 15;
  20. scope.reflectFromAbove = false;
  21. // Internals
  22. let _halfWidth, _width, _height;
  23. const _cameraF = new PerspectiveCamera(); //front
  24. const _cameraB = new PerspectiveCamera(); //back
  25. const _cameraL = new PerspectiveCamera(); //left
  26. const _cameraR = new PerspectiveCamera(); //right
  27. const _position = new Vector3();
  28. const _quaternion = new Quaternion();
  29. const _scale = new Vector3();
  30. // Initialization
  31. renderer.autoClear = false;
  32. /**
  33. * Resizes the effect.
  34. *
  35. * @param {number} width - The width of the effect in logical pixels.
  36. * @param {number} height - The height of the effect in logical pixels.
  37. */
  38. this.setSize = function ( width, height ) {
  39. _halfWidth = width / 2;
  40. if ( width < height ) {
  41. _width = width / 3;
  42. _height = width / 3;
  43. } else {
  44. _width = height / 3;
  45. _height = height / 3;
  46. }
  47. renderer.setSize( width, height );
  48. };
  49. /**
  50. * When using this effect, this method should be called instead of the
  51. * default {@link WebGLRenderer#render}.
  52. *
  53. * @param {Object3D} scene - The scene to render.
  54. * @param {Camera} camera - The camera.
  55. */
  56. this.render = function ( scene, camera ) {
  57. if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
  58. if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
  59. camera.matrixWorld.decompose( _position, _quaternion, _scale );
  60. // front
  61. _cameraF.position.copy( _position );
  62. _cameraF.quaternion.copy( _quaternion );
  63. _cameraF.translateZ( scope.cameraDistance );
  64. _cameraF.lookAt( scene.position );
  65. // back
  66. _cameraB.position.copy( _position );
  67. _cameraB.quaternion.copy( _quaternion );
  68. _cameraB.translateZ( - ( scope.cameraDistance ) );
  69. _cameraB.lookAt( scene.position );
  70. _cameraB.rotation.z += 180 * ( Math.PI / 180 );
  71. // left
  72. _cameraL.position.copy( _position );
  73. _cameraL.quaternion.copy( _quaternion );
  74. _cameraL.translateX( - ( scope.cameraDistance ) );
  75. _cameraL.lookAt( scene.position );
  76. _cameraL.rotation.x += 90 * ( Math.PI / 180 );
  77. // right
  78. _cameraR.position.copy( _position );
  79. _cameraR.quaternion.copy( _quaternion );
  80. _cameraR.translateX( scope.cameraDistance );
  81. _cameraR.lookAt( scene.position );
  82. _cameraR.rotation.x += 90 * ( Math.PI / 180 );
  83. renderer.clear();
  84. renderer.setScissorTest( true );
  85. renderer.setScissor( _halfWidth - ( _width / 2 ), ( _height * 2 ), _width, _height );
  86. renderer.setViewport( _halfWidth - ( _width / 2 ), ( _height * 2 ), _width, _height );
  87. if ( scope.reflectFromAbove ) {
  88. renderer.render( scene, _cameraB );
  89. } else {
  90. renderer.render( scene, _cameraF );
  91. }
  92. renderer.setScissor( _halfWidth - ( _width / 2 ), 0, _width, _height );
  93. renderer.setViewport( _halfWidth - ( _width / 2 ), 0, _width, _height );
  94. if ( scope.reflectFromAbove ) {
  95. renderer.render( scene, _cameraF );
  96. } else {
  97. renderer.render( scene, _cameraB );
  98. }
  99. renderer.setScissor( _halfWidth - ( _width / 2 ) - _width, _height, _width, _height );
  100. renderer.setViewport( _halfWidth - ( _width / 2 ) - _width, _height, _width, _height );
  101. if ( scope.reflectFromAbove ) {
  102. renderer.render( scene, _cameraR );
  103. } else {
  104. renderer.render( scene, _cameraL );
  105. }
  106. renderer.setScissor( _halfWidth + ( _width / 2 ), _height, _width, _height );
  107. renderer.setViewport( _halfWidth + ( _width / 2 ), _height, _width, _height );
  108. if ( scope.reflectFromAbove ) {
  109. renderer.render( scene, _cameraL );
  110. } else {
  111. renderer.render( scene, _cameraR );
  112. }
  113. renderer.setScissorTest( false );
  114. };
  115. }
  116. }
  117. export { PeppersGhostEffect };
粤ICP备19079148号