2
0

Refractor.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. console.warn( "THREE.Refractor: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. THREE.Refractor = function ( geometry, options ) {
  3. THREE.Mesh.call( this, geometry );
  4. this.type = 'Refractor';
  5. var scope = this;
  6. options = options || {};
  7. var color = ( options.color !== undefined ) ? new THREE.Color( options.color ) : new THREE.Color( 0x7F7F7F );
  8. var textureWidth = options.textureWidth || 512;
  9. var textureHeight = options.textureHeight || 512;
  10. var clipBias = options.clipBias || 0;
  11. var shader = options.shader || THREE.Refractor.RefractorShader;
  12. //
  13. var virtualCamera = new THREE.PerspectiveCamera();
  14. virtualCamera.matrixAutoUpdate = false;
  15. virtualCamera.userData.refractor = true;
  16. //
  17. var refractorPlane = new THREE.Plane();
  18. var textureMatrix = new THREE.Matrix4();
  19. // render target
  20. var parameters = {
  21. minFilter: THREE.LinearFilter,
  22. magFilter: THREE.LinearFilter,
  23. format: THREE.RGBFormat,
  24. stencilBuffer: false
  25. };
  26. var renderTarget = new THREE.WebGLRenderTarget( textureWidth, textureHeight, parameters );
  27. if ( ! THREE.MathUtils.isPowerOfTwo( textureWidth ) || ! THREE.MathUtils.isPowerOfTwo( textureHeight ) ) {
  28. renderTarget.texture.generateMipmaps = false;
  29. }
  30. // material
  31. this.material = new THREE.ShaderMaterial( {
  32. uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
  33. vertexShader: shader.vertexShader,
  34. fragmentShader: shader.fragmentShader,
  35. transparent: true // ensures, refractors are drawn from farthest to closest
  36. } );
  37. this.material.uniforms[ "color" ].value = color;
  38. this.material.uniforms[ "tDiffuse" ].value = renderTarget.texture;
  39. this.material.uniforms[ "textureMatrix" ].value = textureMatrix;
  40. // functions
  41. var visible = ( function () {
  42. var refractorWorldPosition = new THREE.Vector3();
  43. var cameraWorldPosition = new THREE.Vector3();
  44. var rotationMatrix = new THREE.Matrix4();
  45. var view = new THREE.Vector3();
  46. var normal = new THREE.Vector3();
  47. return function visible( camera ) {
  48. refractorWorldPosition.setFromMatrixPosition( scope.matrixWorld );
  49. cameraWorldPosition.setFromMatrixPosition( camera.matrixWorld );
  50. view.subVectors( refractorWorldPosition, cameraWorldPosition );
  51. rotationMatrix.extractRotation( scope.matrixWorld );
  52. normal.set( 0, 0, 1 );
  53. normal.applyMatrix4( rotationMatrix );
  54. return view.dot( normal ) < 0;
  55. };
  56. } )();
  57. var updateRefractorPlane = ( function () {
  58. var normal = new THREE.Vector3();
  59. var position = new THREE.Vector3();
  60. var quaternion = new THREE.Quaternion();
  61. var scale = new THREE.Vector3();
  62. return function updateRefractorPlane() {
  63. scope.matrixWorld.decompose( position, quaternion, scale );
  64. normal.set( 0, 0, 1 ).applyQuaternion( quaternion ).normalize();
  65. // flip the normal because we want to cull everything above the plane
  66. normal.negate();
  67. refractorPlane.setFromNormalAndCoplanarPoint( normal, position );
  68. };
  69. } )();
  70. var updateVirtualCamera = ( function () {
  71. var clipPlane = new THREE.Plane();
  72. var clipVector = new THREE.Vector4();
  73. var q = new THREE.Vector4();
  74. return function updateVirtualCamera( camera ) {
  75. virtualCamera.matrixWorld.copy( camera.matrixWorld );
  76. virtualCamera.matrixWorldInverse.getInverse( virtualCamera.matrixWorld );
  77. virtualCamera.projectionMatrix.copy( camera.projectionMatrix );
  78. virtualCamera.far = camera.far; // used in WebGLBackground
  79. // The following code creates an oblique view frustum for clipping.
  80. // see: Lengyel, Eric. “Oblique View Frustum Depth Projection and Clipping”.
  81. // Journal of Game Development, Vol. 1, No. 2 (2005), Charles River Media, pp. 5–16
  82. clipPlane.copy( refractorPlane );
  83. clipPlane.applyMatrix4( virtualCamera.matrixWorldInverse );
  84. clipVector.set( clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.constant );
  85. // calculate the clip-space corner point opposite the clipping plane and
  86. // transform it into camera space by multiplying it by the inverse of the projection matrix
  87. var projectionMatrix = virtualCamera.projectionMatrix;
  88. q.x = ( Math.sign( clipVector.x ) + projectionMatrix.elements[ 8 ] ) / projectionMatrix.elements[ 0 ];
  89. q.y = ( Math.sign( clipVector.y ) + projectionMatrix.elements[ 9 ] ) / projectionMatrix.elements[ 5 ];
  90. q.z = - 1.0;
  91. q.w = ( 1.0 + projectionMatrix.elements[ 10 ] ) / projectionMatrix.elements[ 14 ];
  92. // calculate the scaled plane vector
  93. clipVector.multiplyScalar( 2.0 / clipVector.dot( q ) );
  94. // replacing the third row of the projection matrix
  95. projectionMatrix.elements[ 2 ] = clipVector.x;
  96. projectionMatrix.elements[ 6 ] = clipVector.y;
  97. projectionMatrix.elements[ 10 ] = clipVector.z + 1.0 - clipBias;
  98. projectionMatrix.elements[ 14 ] = clipVector.w;
  99. };
  100. } )();
  101. // This will update the texture matrix that is used for projective texture mapping in the shader.
  102. // see: http://developer.download.nvidia.com/assets/gamedev/docs/projective_texture_mapping.pdf
  103. function updateTextureMatrix( camera ) {
  104. // this matrix does range mapping to [ 0, 1 ]
  105. textureMatrix.set(
  106. 0.5, 0.0, 0.0, 0.5,
  107. 0.0, 0.5, 0.0, 0.5,
  108. 0.0, 0.0, 0.5, 0.5,
  109. 0.0, 0.0, 0.0, 1.0
  110. );
  111. // we use "Object Linear Texgen", so we need to multiply the texture matrix T
  112. // (matrix above) with the projection and view matrix of the virtual camera
  113. // and the model matrix of the refractor
  114. textureMatrix.multiply( camera.projectionMatrix );
  115. textureMatrix.multiply( camera.matrixWorldInverse );
  116. textureMatrix.multiply( scope.matrixWorld );
  117. }
  118. //
  119. function render( renderer, scene, camera ) {
  120. scope.visible = false;
  121. var currentRenderTarget = renderer.getRenderTarget();
  122. var currentXrEnabled = renderer.xr.enabled;
  123. var currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
  124. renderer.xr.enabled = false; // avoid camera modification
  125. renderer.shadowMap.autoUpdate = false; // avoid re-computing shadows
  126. renderer.setRenderTarget( renderTarget );
  127. if ( renderer.autoClear === false ) renderer.clear();
  128. renderer.render( scene, virtualCamera );
  129. renderer.xr.enabled = currentXrEnabled;
  130. renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
  131. renderer.setRenderTarget( currentRenderTarget );
  132. // restore viewport
  133. var viewport = camera.viewport;
  134. if ( viewport !== undefined ) {
  135. renderer.state.viewport( viewport );
  136. }
  137. scope.visible = true;
  138. }
  139. //
  140. this.onBeforeRender = function ( renderer, scene, camera ) {
  141. // Render
  142. renderTarget.texture.encoding = renderer.outputEncoding;
  143. // ensure refractors are rendered only once per frame
  144. if ( camera.userData.refractor === true ) return;
  145. // avoid rendering when the refractor is viewed from behind
  146. if ( ! visible( camera ) === true ) return;
  147. // update
  148. updateRefractorPlane();
  149. updateTextureMatrix( camera );
  150. updateVirtualCamera( camera );
  151. render( renderer, scene, camera );
  152. };
  153. this.getRenderTarget = function () {
  154. return renderTarget;
  155. };
  156. };
  157. THREE.Refractor.prototype = Object.create( THREE.Mesh.prototype );
  158. THREE.Refractor.prototype.constructor = THREE.Refractor;
  159. THREE.Refractor.RefractorShader = {
  160. uniforms: {
  161. 'color': {
  162. value: null
  163. },
  164. 'tDiffuse': {
  165. value: null
  166. },
  167. 'textureMatrix': {
  168. value: null
  169. }
  170. },
  171. vertexShader: [
  172. 'uniform mat4 textureMatrix;',
  173. 'varying vec4 vUv;',
  174. 'void main() {',
  175. ' vUv = textureMatrix * vec4( position, 1.0 );',
  176. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  177. '}'
  178. ].join( '\n' ),
  179. fragmentShader: [
  180. 'uniform vec3 color;',
  181. 'uniform sampler2D tDiffuse;',
  182. 'varying vec4 vUv;',
  183. 'float blendOverlay( float base, float blend ) {',
  184. ' return( base < 0.5 ? ( 2.0 * base * blend ) : ( 1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );',
  185. '}',
  186. 'vec3 blendOverlay( vec3 base, vec3 blend ) {',
  187. ' return vec3( blendOverlay( base.r, blend.r ), blendOverlay( base.g, blend.g ), blendOverlay( base.b, blend.b ) );',
  188. '}',
  189. 'void main() {',
  190. ' vec4 base = texture2DProj( tDiffuse, vUv );',
  191. ' gl_FragColor = vec4( blendOverlay( base.rgb, color ), 1.0 );',
  192. '}'
  193. ].join( '\n' )
  194. };