PeppersGhostEffect.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {
  2. PerspectiveCamera,
  3. Quaternion,
  4. Vector3
  5. } from '../../../build/three.module.js';
  6. /**
  7. * peppers ghost effect based on http://www.instructables.com/id/Reflective-Prism/?ALLSTEPS
  8. */
  9. var PeppersGhostEffect = function ( renderer ) {
  10. var scope = this;
  11. scope.cameraDistance = 15;
  12. scope.reflectFromAbove = false;
  13. // Internals
  14. var _halfWidth, _width, _height;
  15. var _cameraF = new PerspectiveCamera(); //front
  16. var _cameraB = new PerspectiveCamera(); //back
  17. var _cameraL = new PerspectiveCamera(); //left
  18. var _cameraR = new PerspectiveCamera(); //right
  19. var _position = new Vector3();
  20. var _quaternion = new Quaternion();
  21. var _scale = new Vector3();
  22. // Initialization
  23. renderer.autoClear = false;
  24. this.setSize = function ( width, height ) {
  25. _halfWidth = width / 2;
  26. if ( width < height ) {
  27. _width = width / 3;
  28. _height = width / 3;
  29. } else {
  30. _width = height / 3;
  31. _height = height / 3;
  32. }
  33. renderer.setSize( width, height );
  34. };
  35. this.render = function ( scene, camera ) {
  36. scene.updateMatrixWorld();
  37. if ( camera.parent === null ) camera.updateMatrixWorld();
  38. camera.matrixWorld.decompose( _position, _quaternion, _scale );
  39. // front
  40. _cameraF.position.copy( _position );
  41. _cameraF.quaternion.copy( _quaternion );
  42. _cameraF.translateZ( scope.cameraDistance );
  43. _cameraF.lookAt( scene.position );
  44. // back
  45. _cameraB.position.copy( _position );
  46. _cameraB.quaternion.copy( _quaternion );
  47. _cameraB.translateZ( - ( scope.cameraDistance ) );
  48. _cameraB.lookAt( scene.position );
  49. _cameraB.rotation.z += 180 * ( Math.PI / 180 );
  50. // left
  51. _cameraL.position.copy( _position );
  52. _cameraL.quaternion.copy( _quaternion );
  53. _cameraL.translateX( - ( scope.cameraDistance ) );
  54. _cameraL.lookAt( scene.position );
  55. _cameraL.rotation.x += 90 * ( Math.PI / 180 );
  56. // right
  57. _cameraR.position.copy( _position );
  58. _cameraR.quaternion.copy( _quaternion );
  59. _cameraR.translateX( scope.cameraDistance );
  60. _cameraR.lookAt( scene.position );
  61. _cameraR.rotation.x += 90 * ( Math.PI / 180 );
  62. renderer.clear();
  63. renderer.setScissorTest( true );
  64. renderer.setScissor( _halfWidth - ( _width / 2 ), ( _height * 2 ), _width, _height );
  65. renderer.setViewport( _halfWidth - ( _width / 2 ), ( _height * 2 ), _width, _height );
  66. if ( scope.reflectFromAbove ) {
  67. renderer.render( scene, _cameraB );
  68. } else {
  69. renderer.render( scene, _cameraF );
  70. }
  71. renderer.setScissor( _halfWidth - ( _width / 2 ), 0, _width, _height );
  72. renderer.setViewport( _halfWidth - ( _width / 2 ), 0, _width, _height );
  73. if ( scope.reflectFromAbove ) {
  74. renderer.render( scene, _cameraF );
  75. } else {
  76. renderer.render( scene, _cameraB );
  77. }
  78. renderer.setScissor( _halfWidth - ( _width / 2 ) - _width, _height, _width, _height );
  79. renderer.setViewport( _halfWidth - ( _width / 2 ) - _width, _height, _width, _height );
  80. if ( scope.reflectFromAbove ) {
  81. renderer.render( scene, _cameraR );
  82. } else {
  83. renderer.render( scene, _cameraL );
  84. }
  85. renderer.setScissor( _halfWidth + ( _width / 2 ), _height, _width, _height );
  86. renderer.setViewport( _halfWidth + ( _width / 2 ), _height, _width, _height );
  87. if ( scope.reflectFromAbove ) {
  88. renderer.render( scene, _cameraL );
  89. } else {
  90. renderer.render( scene, _cameraR );
  91. }
  92. renderer.setScissorTest( false );
  93. };
  94. };
  95. export { PeppersGhostEffect };