PeppersGhostEffect.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. console.warn( "THREE.PeppersGhostEffect: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * Created by tpowellmeto on 29/10/2015.
  4. *
  5. * peppers ghost effect based on http://www.instructables.com/id/Reflective-Prism/?ALLSTEPS
  6. */
  7. THREE.PeppersGhostEffect = function ( renderer ) {
  8. var scope = this;
  9. scope.cameraDistance = 15;
  10. scope.reflectFromAbove = false;
  11. // Internals
  12. var _halfWidth, _width, _height;
  13. var _cameraF = new THREE.PerspectiveCamera(); //front
  14. var _cameraB = new THREE.PerspectiveCamera(); //back
  15. var _cameraL = new THREE.PerspectiveCamera(); //left
  16. var _cameraR = new THREE.PerspectiveCamera(); //right
  17. var _position = new THREE.Vector3();
  18. var _quaternion = new THREE.Quaternion();
  19. var _scale = new THREE.Vector3();
  20. // Initialization
  21. renderer.autoClear = false;
  22. this.setSize = function ( width, height ) {
  23. _halfWidth = width / 2;
  24. if ( width < height ) {
  25. _width = width / 3;
  26. _height = width / 3;
  27. } else {
  28. _width = height / 3;
  29. _height = height / 3;
  30. }
  31. renderer.setSize( width, height );
  32. };
  33. this.render = function ( scene, camera ) {
  34. scene.updateMatrixWorld();
  35. if ( camera.parent === null ) camera.updateMatrixWorld();
  36. camera.matrixWorld.decompose( _position, _quaternion, _scale );
  37. // front
  38. _cameraF.position.copy( _position );
  39. _cameraF.quaternion.copy( _quaternion );
  40. _cameraF.translateZ( scope.cameraDistance );
  41. _cameraF.lookAt( scene.position );
  42. // back
  43. _cameraB.position.copy( _position );
  44. _cameraB.quaternion.copy( _quaternion );
  45. _cameraB.translateZ( - ( scope.cameraDistance ) );
  46. _cameraB.lookAt( scene.position );
  47. _cameraB.rotation.z += 180 * ( Math.PI / 180 );
  48. // left
  49. _cameraL.position.copy( _position );
  50. _cameraL.quaternion.copy( _quaternion );
  51. _cameraL.translateX( - ( scope.cameraDistance ) );
  52. _cameraL.lookAt( scene.position );
  53. _cameraL.rotation.x += 90 * ( Math.PI / 180 );
  54. // right
  55. _cameraR.position.copy( _position );
  56. _cameraR.quaternion.copy( _quaternion );
  57. _cameraR.translateX( scope.cameraDistance );
  58. _cameraR.lookAt( scene.position );
  59. _cameraR.rotation.x += 90 * ( Math.PI / 180 );
  60. renderer.clear();
  61. renderer.setScissorTest( true );
  62. renderer.setScissor( _halfWidth - ( _width / 2 ), ( _height * 2 ), _width, _height );
  63. renderer.setViewport( _halfWidth - ( _width / 2 ), ( _height * 2 ), _width, _height );
  64. if ( scope.reflectFromAbove ) {
  65. renderer.render( scene, _cameraB );
  66. } else {
  67. renderer.render( scene, _cameraF );
  68. }
  69. renderer.setScissor( _halfWidth - ( _width / 2 ), 0, _width, _height );
  70. renderer.setViewport( _halfWidth - ( _width / 2 ), 0, _width, _height );
  71. if ( scope.reflectFromAbove ) {
  72. renderer.render( scene, _cameraF );
  73. } else {
  74. renderer.render( scene, _cameraB );
  75. }
  76. renderer.setScissor( _halfWidth - ( _width / 2 ) - _width, _height, _width, _height );
  77. renderer.setViewport( _halfWidth - ( _width / 2 ) - _width, _height, _width, _height );
  78. if ( scope.reflectFromAbove ) {
  79. renderer.render( scene, _cameraR );
  80. } else {
  81. renderer.render( scene, _cameraL );
  82. }
  83. renderer.setScissor( _halfWidth + ( _width / 2 ), _height, _width, _height );
  84. renderer.setViewport( _halfWidth + ( _width / 2 ), _height, _width, _height );
  85. if ( scope.reflectFromAbove ) {
  86. renderer.render( scene, _cameraL );
  87. } else {
  88. renderer.render( scene, _cameraR );
  89. }
  90. renderer.setScissorTest( false );
  91. };
  92. };