PeppersGhostEffect.js 3.3 KB

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