PeppersGhostEffect.js 3.2 KB

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