Mirror.js 6.3 KB

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