Mirror.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * @author Slayvin / http://slayvin.net
  3. */
  4. THREE.Mirror = function ( width, height, options ) {
  5. THREE.Mesh.call( this, new THREE.PlaneBufferGeometry( width, height ) );
  6. var scope = this;
  7. scope.name = 'mirror_' + scope.id;
  8. scope.matrixNeedsUpdate = true;
  9. options = options || {};
  10. var textureWidth = options.textureWidth !== undefined ? options.textureWidth : 512;
  11. var textureHeight = options.textureHeight !== undefined ? options.textureHeight : 512;
  12. var clipBias = options.clipBias !== undefined ? options.clipBias : 0.0;
  13. var mirrorColor = options.color !== undefined ? new THREE.Color( options.color ) : new THREE.Color( 0x7F7F7F );
  14. var mirrorPlane = new THREE.Plane();
  15. var normal = new THREE.Vector3();
  16. var mirrorWorldPosition = new THREE.Vector3();
  17. var cameraWorldPosition = new THREE.Vector3();
  18. var rotationMatrix = new THREE.Matrix4();
  19. var lookAtPosition = new THREE.Vector3( 0, 0, - 1 );
  20. var clipPlane = new THREE.Vector4();
  21. var textureMatrix = new THREE.Matrix4();
  22. var mirrorCamera = new THREE.PerspectiveCamera();
  23. mirrorCamera.matrixAutoUpdate = true;
  24. var parameters = {
  25. minFilter: THREE.LinearFilter,
  26. magFilter: THREE.LinearFilter,
  27. format: THREE.RGBFormat,
  28. stencilBuffer: false
  29. };
  30. var renderTarget = new THREE.WebGLRenderTarget( textureWidth, textureHeight, parameters );
  31. if ( ! THREE.Math.isPowerOfTwo( textureWidth ) || ! THREE.Math.isPowerOfTwo( textureHeight ) ) {
  32. renderTarget.texture.generateMipmaps = false;
  33. }
  34. var mirrorShader = {
  35. uniforms: {
  36. mirrorColor: { value: new THREE.Color( 0x7F7F7F ) },
  37. mirrorSampler: { value: null },
  38. textureMatrix: { value: new THREE.Matrix4() }
  39. },
  40. vertexShader: [
  41. 'uniform mat4 textureMatrix;',
  42. 'varying vec4 mirrorCoord;',
  43. 'void main() {',
  44. ' vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
  45. ' vec4 worldPosition = modelMatrix * vec4( position, 1.0 );',
  46. ' mirrorCoord = textureMatrix * worldPosition;',
  47. ' gl_Position = projectionMatrix * mvPosition;',
  48. '}'
  49. ].join( '\n' ),
  50. fragmentShader: [
  51. 'uniform vec3 mirrorColor;',
  52. 'uniform sampler2D mirrorSampler;',
  53. 'varying vec4 mirrorCoord;',
  54. 'float blendOverlay(float base, float blend) {',
  55. ' return( base < 0.5 ? ( 2.0 * base * blend ) : (1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );',
  56. '}',
  57. 'void main() {',
  58. ' vec4 color = texture2DProj(mirrorSampler, mirrorCoord);',
  59. ' color = vec4(blendOverlay(mirrorColor.r, color.r), blendOverlay(mirrorColor.g, color.g), blendOverlay(mirrorColor.b, color.b), 1.0);',
  60. ' gl_FragColor = color;',
  61. '}'
  62. ].join( '\n' )
  63. };
  64. var mirrorUniforms = THREE.UniformsUtils.clone( mirrorShader.uniforms );
  65. var material = new THREE.ShaderMaterial( {
  66. fragmentShader: mirrorShader.fragmentShader,
  67. vertexShader: mirrorShader.vertexShader,
  68. uniforms: mirrorUniforms
  69. } );
  70. material.uniforms.mirrorSampler.value = renderTarget.texture;
  71. material.uniforms.mirrorColor.value = mirrorColor;
  72. material.uniforms.textureMatrix.value = textureMatrix;
  73. scope.material = material;
  74. function updateTextureMatrix( camera ) {
  75. camera.updateMatrixWorld();
  76. mirrorCamera.copy( camera );
  77. mirrorCamera.updateProjectionMatrix();
  78. scope.updateMatrixWorld();
  79. mirrorWorldPosition.setFromMatrixPosition( scope.matrixWorld );
  80. cameraWorldPosition.setFromMatrixPosition( camera.matrixWorld );
  81. rotationMatrix.extractRotation( scope.matrixWorld );
  82. normal.set( 0, 0, 1 );
  83. normal.applyMatrix4( rotationMatrix );
  84. var view = mirrorWorldPosition.clone().sub( cameraWorldPosition );
  85. view.reflect( normal ).negate();
  86. view.add( mirrorWorldPosition );
  87. rotationMatrix.extractRotation( camera.matrixWorld );
  88. lookAtPosition.set( 0, 0, - 1 );
  89. lookAtPosition.applyMatrix4( rotationMatrix );
  90. lookAtPosition.add( cameraWorldPosition );
  91. var target = mirrorWorldPosition.clone().sub( lookAtPosition );
  92. target.reflect( normal ).negate();
  93. target.add( mirrorWorldPosition );
  94. mirrorCamera.position.copy( view );
  95. mirrorCamera.up.set( 0, - 1, 0 );
  96. mirrorCamera.up.applyMatrix4( rotationMatrix );
  97. mirrorCamera.up.reflect( normal ).negate();
  98. mirrorCamera.lookAt( target );
  99. mirrorCamera.updateProjectionMatrix();
  100. mirrorCamera.updateMatrixWorld();
  101. mirrorCamera.matrixWorldInverse.getInverse( mirrorCamera.matrixWorld );
  102. // Update the texture matrix
  103. textureMatrix.set(
  104. 0.5, 0.0, 0.0, 0.5,
  105. 0.0, 0.5, 0.0, 0.5,
  106. 0.0, 0.0, 0.5, 0.5,
  107. 0.0, 0.0, 0.0, 1.0
  108. );
  109. textureMatrix.multiply( mirrorCamera.projectionMatrix );
  110. textureMatrix.multiply( mirrorCamera.matrixWorldInverse );
  111. // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  112. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  113. mirrorPlane.setFromNormalAndCoplanarPoint( normal, mirrorWorldPosition );
  114. mirrorPlane.applyMatrix4( mirrorCamera.matrixWorldInverse );
  115. clipPlane.set( mirrorPlane.normal.x, mirrorPlane.normal.y, mirrorPlane.normal.z, mirrorPlane.constant );
  116. var q = new THREE.Vector4();
  117. var projectionMatrix = mirrorCamera.projectionMatrix;
  118. q.x = ( Math.sign( clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
  119. q.y = ( Math.sign( clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
  120. q.z = - 1.0;
  121. q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
  122. // Calculate the scaled plane vector
  123. var c = new THREE.Vector4();
  124. c = clipPlane.multiplyScalar( 2.0 / clipPlane.dot( q ) );
  125. // Replacing the third row of the projection matrix
  126. projectionMatrix.elements[ 2 ] = c.x;
  127. projectionMatrix.elements[ 6 ] = c.y;
  128. projectionMatrix.elements[ 10 ] = c.z + 1.0 - clipBias;
  129. projectionMatrix.elements[ 14 ] = c.w;
  130. }
  131. scope.onBeforeRender = function ( renderer, scene, camera ) {
  132. updateTextureMatrix( camera );
  133. scope.visible = false;
  134. var currentRenderTarget = renderer.getRenderTarget();
  135. renderer.render( scene, mirrorCamera, renderTarget, true );
  136. renderer.setRenderTarget( currentRenderTarget );
  137. scope.visible = true;
  138. };
  139. };
  140. THREE.Mirror.prototype = Object.create( THREE.Mesh.prototype );