ReflectorNode.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import TextureNode from '../accessors/TextureNode.js';
  2. import { nodeObject, vec2 } from '../shadernode/ShaderNode.js';
  3. import { NodeUpdateType } from '../core/constants.js';
  4. import { viewportTopLeft } from '../display/ViewportNode.js';
  5. import { Matrix4, Vector2, Vector3, Vector4, Object3D, Plane, RenderTarget, HalfFloatType, LinearMipMapLinearFilter } from 'three';
  6. const _reflectorPlane = new Plane();
  7. const _normal = new Vector3();
  8. const _reflectorWorldPosition = new Vector3();
  9. const _cameraWorldPosition = new Vector3();
  10. const _rotationMatrix = new Matrix4();
  11. const _lookAtPosition = new Vector3( 0, 0, - 1 );
  12. const clipPlane = new Vector4();
  13. const _view = new Vector3();
  14. const _target = new Vector3();
  15. const _q = new Vector4();
  16. const _size = new Vector2();
  17. const _defaultRT = new RenderTarget();
  18. const _defaultUV = vec2( viewportTopLeft.x.oneMinus(), viewportTopLeft.y );
  19. let _inReflector = false;
  20. class ReflectorNode extends TextureNode {
  21. constructor( parameters = {} ) {
  22. super( _defaultRT.texture, _defaultUV );
  23. const {
  24. target = new Object3D(),
  25. resolution = 1,
  26. generateMipmaps = false,
  27. bounces = true
  28. } = parameters;
  29. //
  30. this.target = target;
  31. this.resolution = resolution;
  32. this.generateMipmaps = generateMipmaps;
  33. this.bounces = bounces;
  34. this.updateBeforeType = bounces ? NodeUpdateType.RENDER : NodeUpdateType.FRAME;
  35. this.virtualCameras = new WeakMap();
  36. this.renderTargets = new WeakMap();
  37. }
  38. _updateResolution( renderTarget, renderer ) {
  39. const resolution = this.resolution;
  40. renderer.getDrawingBufferSize( _size );
  41. renderTarget.setSize( Math.round( _size.width * resolution ), Math.round( _size.height * resolution ) );
  42. }
  43. setup( builder ) {
  44. this._updateResolution( _defaultRT, builder.renderer );
  45. return super.setup( builder );
  46. }
  47. getTextureNode() {
  48. return this.textureNode;
  49. }
  50. getVirtualCamera( camera ) {
  51. let virtualCamera = this.virtualCameras.get( camera );
  52. if ( virtualCamera === undefined ) {
  53. virtualCamera = camera.clone();
  54. this.virtualCameras.set( camera, virtualCamera );
  55. }
  56. return virtualCamera;
  57. }
  58. getRenderTarget( camera ) {
  59. let renderTarget = this.renderTargets.get( camera );
  60. if ( renderTarget === undefined ) {
  61. renderTarget = new RenderTarget( 0, 0, { type: HalfFloatType } );
  62. if ( this.generateMipmaps === true ) {
  63. renderTarget.texture.minFilter = LinearMipMapLinearFilter;
  64. renderTarget.texture.generateMipmaps = true;
  65. }
  66. this.renderTargets.set( camera, renderTarget );
  67. }
  68. return renderTarget;
  69. }
  70. updateBefore( frame ) {
  71. if ( this.bounces === false && _inReflector ) return false;
  72. _inReflector = true;
  73. const { scene, camera, renderer, material } = frame;
  74. const { target } = this;
  75. const virtualCamera = this.getVirtualCamera( camera );
  76. const renderTarget = this.getRenderTarget( virtualCamera );
  77. renderer.getDrawingBufferSize( _size );
  78. this._updateResolution( renderTarget, renderer );
  79. //
  80. _reflectorWorldPosition.setFromMatrixPosition( target.matrixWorld );
  81. _cameraWorldPosition.setFromMatrixPosition( camera.matrixWorld );
  82. _rotationMatrix.extractRotation( target.matrixWorld );
  83. _normal.set( 0, 0, 1 );
  84. _normal.applyMatrix4( _rotationMatrix );
  85. _view.subVectors( _reflectorWorldPosition, _cameraWorldPosition );
  86. // Avoid rendering when reflector is facing away
  87. if ( _view.dot( _normal ) > 0 ) return;
  88. _view.reflect( _normal ).negate();
  89. _view.add( _reflectorWorldPosition );
  90. _rotationMatrix.extractRotation( camera.matrixWorld );
  91. _lookAtPosition.set( 0, 0, - 1 );
  92. _lookAtPosition.applyMatrix4( _rotationMatrix );
  93. _lookAtPosition.add( _cameraWorldPosition );
  94. _target.subVectors( _reflectorWorldPosition, _lookAtPosition );
  95. _target.reflect( _normal ).negate();
  96. _target.add( _reflectorWorldPosition );
  97. //
  98. virtualCamera.coordinateSystem = camera.coordinateSystem;
  99. virtualCamera.position.copy( _view );
  100. virtualCamera.up.set( 0, 1, 0 );
  101. virtualCamera.up.applyMatrix4( _rotationMatrix );
  102. virtualCamera.up.reflect( _normal );
  103. virtualCamera.lookAt( _target );
  104. virtualCamera.near = camera.near;
  105. virtualCamera.far = camera.far;
  106. virtualCamera.updateMatrixWorld();
  107. virtualCamera.projectionMatrix.copy( camera.projectionMatrix );
  108. // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  109. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  110. _reflectorPlane.setFromNormalAndCoplanarPoint( _normal, _reflectorWorldPosition );
  111. _reflectorPlane.applyMatrix4( virtualCamera.matrixWorldInverse );
  112. clipPlane.set( _reflectorPlane.normal.x, _reflectorPlane.normal.y, _reflectorPlane.normal.z, _reflectorPlane.constant );
  113. const projectionMatrix = virtualCamera.projectionMatrix;
  114. _q.x = ( Math.sign( clipPlane.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
  115. _q.y = ( Math.sign( clipPlane.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
  116. _q.z = - 1.0;
  117. _q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
  118. // Calculate the scaled plane vector
  119. clipPlane.multiplyScalar( 1.0 / clipPlane.dot( _q ) );
  120. const clipBias = 0;
  121. // Replacing the third row of the projection matrix
  122. projectionMatrix.elements[ 2 ] = clipPlane.x;
  123. projectionMatrix.elements[ 6 ] = clipPlane.y;
  124. projectionMatrix.elements[ 10 ] = clipPlane.z - clipBias;
  125. projectionMatrix.elements[ 14 ] = clipPlane.w;
  126. //
  127. this.value = renderTarget.texture;
  128. material.visible = false;
  129. const currentRenderTarget = renderer.getRenderTarget();
  130. renderer.setRenderTarget( renderTarget );
  131. renderer.render( scene, virtualCamera );
  132. renderer.setRenderTarget( currentRenderTarget );
  133. material.visible = true;
  134. _inReflector = false;
  135. }
  136. }
  137. export const reflector = ( parameters ) => nodeObject( new ReflectorNode( parameters ) );
  138. export default ReflectorNode;