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