Reflector.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * @author Slayvin / http://slayvin.net
  3. */
  4. THREE.Reflector = function ( geometry, options ) {
  5. THREE.Mesh.call( this, geometry );
  6. this.type = 'Reflector';
  7. var scope = this;
  8. options = options || {};
  9. var color = ( options.color !== undefined ) ? new THREE.Color( options.color ) : new THREE.Color( 0x7F7F7F );
  10. var textureWidth = options.textureWidth || 512;
  11. var textureHeight = options.textureHeight || 512;
  12. var clipBias = options.clipBias || 0;
  13. var shader = options.shader || THREE.Reflector.ReflectorShader;
  14. var recursion = options.recursion !== undefined ? options.recursion : 0;
  15. //
  16. var reflectorPlane = new THREE.Plane();
  17. var normal = new THREE.Vector3();
  18. var reflectorWorldPosition = new THREE.Vector3();
  19. var cameraWorldPosition = new THREE.Vector3();
  20. var rotationMatrix = new THREE.Matrix4();
  21. var lookAtPosition = new THREE.Vector3( 0, 0, - 1 );
  22. var clipPlane = new THREE.Vector4();
  23. var viewport = new THREE.Vector4();
  24. var view = new THREE.Vector3();
  25. var target = new THREE.Vector3();
  26. var q = new THREE.Vector4();
  27. var textureMatrix = new THREE.Matrix4();
  28. var virtualCamera = new THREE.PerspectiveCamera();
  29. var parameters = {
  30. minFilter: THREE.LinearFilter,
  31. magFilter: THREE.LinearFilter,
  32. format: THREE.RGBFormat,
  33. stencilBuffer: false
  34. };
  35. var renderTarget = new THREE.WebGLRenderTarget( textureWidth, textureHeight, parameters );
  36. if ( ! THREE.Math.isPowerOfTwo( textureWidth ) || ! THREE.Math.isPowerOfTwo( textureHeight ) ) {
  37. renderTarget.texture.generateMipmaps = false;
  38. }
  39. var material = new THREE.ShaderMaterial( {
  40. uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
  41. fragmentShader: shader.fragmentShader,
  42. vertexShader: shader.vertexShader
  43. } );
  44. material.uniforms.tDiffuse.value = renderTarget.texture;
  45. material.uniforms.color.value = color;
  46. material.uniforms.textureMatrix.value = textureMatrix;
  47. this.material = material;
  48. this.renderOrder = - Infinity; // render first
  49. this.onBeforeRender = function ( renderer, scene, camera ) {
  50. if ( 'recursion' in camera.userData ) {
  51. if ( camera.userData.recursion === recursion ) return;
  52. camera.userData.recursion ++;
  53. }
  54. reflectorWorldPosition.setFromMatrixPosition( scope.matrixWorld );
  55. cameraWorldPosition.setFromMatrixPosition( camera.matrixWorld );
  56. rotationMatrix.extractRotation( scope.matrixWorld );
  57. normal.set( 0, 0, 1 );
  58. normal.applyMatrix4( rotationMatrix );
  59. view.subVectors( reflectorWorldPosition, cameraWorldPosition );
  60. // Avoid rendering when reflector is facing away
  61. if ( view.dot( normal ) > 0 ) return;
  62. view.reflect( normal ).negate();
  63. view.add( reflectorWorldPosition );
  64. rotationMatrix.extractRotation( camera.matrixWorld );
  65. lookAtPosition.set( 0, 0, - 1 );
  66. lookAtPosition.applyMatrix4( rotationMatrix );
  67. lookAtPosition.add( cameraWorldPosition );
  68. target.subVectors( reflectorWorldPosition, lookAtPosition );
  69. target.reflect( normal ).negate();
  70. target.add( reflectorWorldPosition );
  71. virtualCamera.position.copy( view );
  72. virtualCamera.up.set( 0, 1, 0 );
  73. virtualCamera.up.applyMatrix4( rotationMatrix );
  74. virtualCamera.up.reflect( normal );
  75. virtualCamera.lookAt( target );
  76. virtualCamera.far = camera.far; // Used in WebGLBackground
  77. virtualCamera.updateMatrixWorld();
  78. virtualCamera.projectionMatrix.copy( camera.projectionMatrix );
  79. virtualCamera.userData.recursion = 0;
  80. // Update the texture matrix
  81. textureMatrix.set(
  82. 0.5, 0.0, 0.0, 0.5,
  83. 0.0, 0.5, 0.0, 0.5,
  84. 0.0, 0.0, 0.5, 0.5,
  85. 0.0, 0.0, 0.0, 1.0
  86. );
  87. textureMatrix.multiply( virtualCamera.projectionMatrix );
  88. textureMatrix.multiply( virtualCamera.matrixWorldInverse );
  89. textureMatrix.multiply( scope.matrixWorld );
  90. // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  91. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  92. reflectorPlane.setFromNormalAndCoplanarPoint( normal, reflectorWorldPosition );
  93. reflectorPlane.applyMatrix4( virtualCamera.matrixWorldInverse );
  94. clipPlane.set( reflectorPlane.normal.x, reflectorPlane.normal.y, reflectorPlane.normal.z, reflectorPlane.constant );
  95. var projectionMatrix = virtualCamera.projectionMatrix;
  96. q.x = ( Math.sign( clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
  97. q.y = ( Math.sign( clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
  98. q.z = - 1.0;
  99. q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
  100. // Calculate the scaled plane vector
  101. clipPlane.multiplyScalar( 2.0 / clipPlane.dot( q ) );
  102. // Replacing the third row of the projection matrix
  103. projectionMatrix.elements[ 2 ] = clipPlane.x;
  104. projectionMatrix.elements[ 6 ] = clipPlane.y;
  105. projectionMatrix.elements[ 10 ] = clipPlane.z + 1.0 - clipBias;
  106. projectionMatrix.elements[ 14 ] = clipPlane.w;
  107. // Render
  108. scope.visible = false;
  109. var currentRenderTarget = renderer.getRenderTarget();
  110. var currentVrEnabled = renderer.vr.enabled;
  111. var currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
  112. renderer.vr.enabled = false; // Avoid camera modification and recursion
  113. renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
  114. renderer.render( scene, virtualCamera, renderTarget, true );
  115. renderer.vr.enabled = currentVrEnabled;
  116. renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
  117. renderer.setRenderTarget( currentRenderTarget );
  118. // Restore viewport
  119. var bounds = camera.bounds;
  120. if ( bounds !== undefined ) {
  121. var size = renderer.getSize();
  122. var pixelRatio = renderer.getPixelRatio();
  123. viewport.x = bounds.x * size.width * pixelRatio;
  124. viewport.y = bounds.y * size.height * pixelRatio;
  125. viewport.z = bounds.z * size.width * pixelRatio;
  126. viewport.w = bounds.w * size.height * pixelRatio;
  127. renderer.state.viewport( viewport );
  128. }
  129. scope.visible = true;
  130. };
  131. this.getRenderTarget = function () {
  132. return renderTarget;
  133. };
  134. };
  135. THREE.Reflector.prototype = Object.create( THREE.Mesh.prototype );
  136. THREE.Reflector.prototype.constructor = THREE.Reflector;
  137. THREE.Reflector.ReflectorShader = {
  138. uniforms: {
  139. 'color': {
  140. type: 'c',
  141. value: null
  142. },
  143. 'tDiffuse': {
  144. type: 't',
  145. value: null
  146. },
  147. 'textureMatrix': {
  148. type: 'm4',
  149. value: null
  150. }
  151. },
  152. vertexShader: [
  153. 'uniform mat4 textureMatrix;',
  154. 'varying vec4 vUv;',
  155. 'void main() {',
  156. ' vUv = textureMatrix * vec4( position, 1.0 );',
  157. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  158. '}'
  159. ].join( '\n' ),
  160. fragmentShader: [
  161. 'uniform vec3 color;',
  162. 'uniform sampler2D tDiffuse;',
  163. 'varying vec4 vUv;',
  164. 'float blendOverlay( float base, float blend ) {',
  165. ' return( base < 0.5 ? ( 2.0 * base * blend ) : ( 1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );',
  166. '}',
  167. 'vec3 blendOverlay( vec3 base, vec3 blend ) {',
  168. ' return vec3( blendOverlay( base.r, blend.r ), blendOverlay( base.g, blend.g ), blendOverlay( base.b, blend.b ) );',
  169. '}',
  170. 'void main() {',
  171. ' vec4 base = texture2DProj( tDiffuse, vUv );',
  172. ' gl_FragColor = vec4( blendOverlay( base.rgb, color ), 1.0 );',
  173. '}'
  174. ].join( '\n' )
  175. };