ReflectorForSSRPass.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. ( function () {
  2. var ReflectorForSSRPass = function ( geometry, options ) {
  3. THREE.Mesh.call( this, geometry );
  4. this.type = 'ReflectorForSSRPass';
  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 || ReflectorForSSRPass.ReflectorShader;
  12. var useDepthTexture = options.useDepthTexture === true;
  13. var yAxis = new THREE.Vector3( 0, 1, 0 );
  14. var vecTemp0 = new THREE.Vector3();
  15. var vecTemp1 = new THREE.Vector3(); //
  16. scope.needsUpdate = false;
  17. scope.maxDistance = ReflectorForSSRPass.ReflectorShader.uniforms.maxDistance.value;
  18. scope.opacity = ReflectorForSSRPass.ReflectorShader.uniforms.opacity.value;
  19. scope.color = color;
  20. scope.resolution = options.resolution || new THREE.Vector2( window.innerWidth, window.innerHeight );
  21. scope._distanceAttenuation = ReflectorForSSRPass.ReflectorShader.defines.DISTANCE_ATTENUATION;
  22. Object.defineProperty( scope, 'distanceAttenuation', {
  23. get() {
  24. return scope._distanceAttenuation;
  25. },
  26. set( val ) {
  27. if ( scope._distanceAttenuation === val ) return;
  28. scope._distanceAttenuation = val;
  29. scope.material.defines.DISTANCE_ATTENUATION = val;
  30. scope.material.needsUpdate = true;
  31. }
  32. } );
  33. scope._fresnel = ReflectorForSSRPass.ReflectorShader.defines.FRESNEL;
  34. Object.defineProperty( scope, 'fresnel', {
  35. get() {
  36. return scope._fresnel;
  37. },
  38. set( val ) {
  39. if ( scope._fresnel === val ) return;
  40. scope._fresnel = val;
  41. scope.material.defines.FRESNEL = val;
  42. scope.material.needsUpdate = true;
  43. }
  44. } );
  45. var normal = new THREE.Vector3();
  46. var reflectorWorldPosition = new THREE.Vector3();
  47. var cameraWorldPosition = new THREE.Vector3();
  48. var rotationMatrix = new THREE.Matrix4();
  49. var lookAtPosition = new THREE.Vector3( 0, 0, - 1 );
  50. var view = new THREE.Vector3();
  51. var target = new THREE.Vector3();
  52. var textureMatrix = new THREE.Matrix4();
  53. var virtualCamera = new THREE.PerspectiveCamera();
  54. if ( useDepthTexture ) {
  55. var depthTexture = new THREE.DepthTexture();
  56. depthTexture.type = THREE.UnsignedShortType;
  57. depthTexture.minFilter = THREE.NearestFilter;
  58. depthTexture.magFilter = THREE.NearestFilter;
  59. }
  60. var parameters = {
  61. minFilter: THREE.LinearFilter,
  62. magFilter: THREE.LinearFilter,
  63. format: THREE.RGBFormat,
  64. depthTexture: useDepthTexture ? depthTexture : null
  65. };
  66. var renderTarget = new THREE.WebGLRenderTarget( textureWidth, textureHeight, parameters );
  67. if ( ! THREE.MathUtils.isPowerOfTwo( textureWidth ) || ! THREE.MathUtils.isPowerOfTwo( textureHeight ) ) {
  68. renderTarget.texture.generateMipmaps = false;
  69. }
  70. var material = new THREE.ShaderMaterial( {
  71. transparent: useDepthTexture,
  72. defines: Object.assign( {}, ReflectorForSSRPass.ReflectorShader.defines, {
  73. useDepthTexture
  74. } ),
  75. uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
  76. fragmentShader: shader.fragmentShader,
  77. vertexShader: shader.vertexShader
  78. } );
  79. material.uniforms[ 'tDiffuse' ].value = renderTarget.texture;
  80. material.uniforms[ 'color' ].value = scope.color;
  81. material.uniforms[ 'textureMatrix' ].value = textureMatrix;
  82. if ( useDepthTexture ) {
  83. material.uniforms[ 'tDepth' ].value = renderTarget.depthTexture;
  84. }
  85. this.material = material;
  86. const globalPlane = new THREE.Plane( new THREE.Vector3( 0, 1, 0 ), clipBias );
  87. const globalPlanes = [ globalPlane ];
  88. this.doRender = function ( renderer, scene, camera ) {
  89. material.uniforms[ 'maxDistance' ].value = scope.maxDistance;
  90. material.uniforms[ 'color' ].value = scope.color;
  91. material.uniforms[ 'opacity' ].value = scope.opacity;
  92. vecTemp0.copy( camera.position ).normalize();
  93. vecTemp1.copy( vecTemp0 ).reflect( yAxis );
  94. material.uniforms[ 'fresnelCoe' ].value = ( vecTemp0.dot( vecTemp1 ) + 1. ) / 2.; // TODO: Also need to use glsl viewPosition and viewNormal per pixel.
  95. reflectorWorldPosition.setFromMatrixPosition( scope.matrixWorld );
  96. cameraWorldPosition.setFromMatrixPosition( camera.matrixWorld );
  97. rotationMatrix.extractRotation( scope.matrixWorld );
  98. normal.set( 0, 0, 1 );
  99. normal.applyMatrix4( rotationMatrix );
  100. view.subVectors( reflectorWorldPosition, cameraWorldPosition ); // Avoid rendering when reflector is facing away
  101. if ( view.dot( normal ) > 0 ) return;
  102. view.reflect( normal ).negate();
  103. view.add( reflectorWorldPosition );
  104. rotationMatrix.extractRotation( camera.matrixWorld );
  105. lookAtPosition.set( 0, 0, - 1 );
  106. lookAtPosition.applyMatrix4( rotationMatrix );
  107. lookAtPosition.add( cameraWorldPosition );
  108. target.subVectors( reflectorWorldPosition, lookAtPosition );
  109. target.reflect( normal ).negate();
  110. target.add( reflectorWorldPosition );
  111. virtualCamera.position.copy( view );
  112. virtualCamera.up.set( 0, 1, 0 );
  113. virtualCamera.up.applyMatrix4( rotationMatrix );
  114. virtualCamera.up.reflect( normal );
  115. virtualCamera.lookAt( target );
  116. virtualCamera.far = camera.far; // Used in WebGLBackground
  117. virtualCamera.updateMatrixWorld();
  118. virtualCamera.projectionMatrix.copy( camera.projectionMatrix );
  119. material.uniforms[ 'virtualCameraNear' ].value = camera.near;
  120. material.uniforms[ 'virtualCameraFar' ].value = camera.far;
  121. material.uniforms[ 'virtualCameraMatrixWorld' ].value = virtualCamera.matrixWorld;
  122. material.uniforms[ 'virtualCameraProjectionMatrix' ].value = camera.projectionMatrix;
  123. material.uniforms[ 'virtualCameraProjectionMatrixInverse' ].value = camera.projectionMatrixInverse;
  124. material.uniforms[ 'resolution' ].value = scope.resolution; // Update the texture matrix
  125. textureMatrix.set( 0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0 );
  126. textureMatrix.multiply( virtualCamera.projectionMatrix );
  127. textureMatrix.multiply( virtualCamera.matrixWorldInverse );
  128. textureMatrix.multiply( scope.matrixWorld ); // Render
  129. renderTarget.texture.encoding = renderer.outputEncoding; // scope.visible = false;
  130. var currentRenderTarget = renderer.getRenderTarget();
  131. var currentXrEnabled = renderer.xr.enabled;
  132. var currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
  133. var currentClippingPlanes = renderer.clippingPlanes;
  134. renderer.xr.enabled = false; // Avoid camera modification
  135. renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
  136. renderer.clippingPlanes = globalPlanes;
  137. renderer.setRenderTarget( renderTarget );
  138. renderer.state.buffers.depth.setMask( true ); // make sure the depth buffer is writable so it can be properly cleared, see #18897
  139. if ( renderer.autoClear === false ) renderer.clear();
  140. renderer.render( scene, virtualCamera );
  141. renderer.xr.enabled = currentXrEnabled;
  142. renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
  143. renderer.clippingPlanes = currentClippingPlanes;
  144. renderer.setRenderTarget( currentRenderTarget ); // Restore viewport
  145. var viewport = camera.viewport;
  146. if ( viewport !== undefined ) {
  147. renderer.state.viewport( viewport );
  148. } // scope.visible = true;
  149. };
  150. this.getRenderTarget = function () {
  151. return renderTarget;
  152. };
  153. };
  154. ReflectorForSSRPass.prototype = Object.create( THREE.Mesh.prototype );
  155. ReflectorForSSRPass.prototype.constructor = ReflectorForSSRPass;
  156. ReflectorForSSRPass.ReflectorShader = {
  157. defines: {
  158. DISTANCE_ATTENUATION: true,
  159. FRESNEL: true
  160. },
  161. uniforms: {
  162. color: {
  163. value: null
  164. },
  165. tDiffuse: {
  166. value: null
  167. },
  168. tDepth: {
  169. value: null
  170. },
  171. textureMatrix: {
  172. value: new THREE.Matrix4()
  173. },
  174. maxDistance: {
  175. value: 180
  176. },
  177. opacity: {
  178. value: 0.5
  179. },
  180. fresnelCoe: {
  181. value: null
  182. },
  183. virtualCameraNear: {
  184. value: null
  185. },
  186. virtualCameraFar: {
  187. value: null
  188. },
  189. virtualCameraProjectionMatrix: {
  190. value: new THREE.Matrix4()
  191. },
  192. virtualCameraMatrixWorld: {
  193. value: new THREE.Matrix4()
  194. },
  195. virtualCameraProjectionMatrixInverse: {
  196. value: new THREE.Matrix4()
  197. },
  198. resolution: {
  199. value: new THREE.Vector2()
  200. }
  201. },
  202. vertexShader: [ 'uniform mat4 textureMatrix;', 'varying vec4 vUv;', 'void main() {', ' vUv = textureMatrix * vec4( position, 1.0 );', ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );', '}' ].join( '\n' ),
  203. fragmentShader: `
  204. uniform vec3 color;
  205. uniform sampler2D tDiffuse;
  206. uniform sampler2D tDepth;
  207. uniform float maxDistance;
  208. uniform float opacity;
  209. uniform float fresnelCoe;
  210. uniform float virtualCameraNear;
  211. uniform float virtualCameraFar;
  212. uniform mat4 virtualCameraProjectionMatrix;
  213. uniform mat4 virtualCameraProjectionMatrixInverse;
  214. uniform mat4 virtualCameraMatrixWorld;
  215. uniform vec2 resolution;
  216. varying vec4 vUv;
  217. #include <packing>
  218. float blendOverlay( float base, float blend ) {
  219. return( base < 0.5 ? ( 2.0 * base * blend ) : ( 1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );
  220. }
  221. vec3 blendOverlay( vec3 base, vec3 blend ) {
  222. return vec3( blendOverlay( base.r, blend.r ), blendOverlay( base.g, blend.g ), blendOverlay( base.b, blend.b ) );
  223. }
  224. float getDepth( const in vec2 uv ) {
  225. return texture2D( tDepth, uv ).x;
  226. }
  227. float getViewZ( const in float depth ) {
  228. return perspectiveDepthToViewZ( depth, virtualCameraNear, virtualCameraFar );
  229. }
  230. vec3 getViewPosition( const in vec2 uv, const in float depth/*clip space*/, const in float clipW ) {
  231. vec4 clipPosition = vec4( ( vec3( uv, depth ) - 0.5 ) * 2.0, 1.0 );//ndc
  232. clipPosition *= clipW; //clip
  233. return ( virtualCameraProjectionMatrixInverse * clipPosition ).xyz;//view
  234. }
  235. void main() {
  236. vec4 base = texture2DProj( tDiffuse, vUv );
  237. #ifdef useDepthTexture
  238. vec2 uv=(gl_FragCoord.xy-.5)/resolution.xy;
  239. uv.x=1.-uv.x;
  240. float depth = texture2DProj( tDepth, vUv ).r;
  241. float viewZ = getViewZ( depth );
  242. float clipW = virtualCameraProjectionMatrix[2][3] * viewZ+virtualCameraProjectionMatrix[3][3];
  243. vec3 viewPosition=getViewPosition( uv, depth, clipW );
  244. vec3 worldPosition=(virtualCameraMatrixWorld*vec4(viewPosition,1)).xyz;
  245. if(worldPosition.y>maxDistance) discard;
  246. float op=opacity;
  247. #ifdef DISTANCE_ATTENUATION
  248. float ratio=1.-(worldPosition.y/maxDistance);
  249. float attenuation=ratio*ratio;
  250. op=opacity*attenuation;
  251. #endif
  252. #ifdef FRESNEL
  253. op*=fresnelCoe;
  254. #endif
  255. gl_FragColor = vec4( blendOverlay( base.rgb, color ), op );
  256. #else
  257. gl_FragColor = vec4( blendOverlay( base.rgb, color ), 1.0 );
  258. #endif
  259. }
  260. `
  261. };
  262. THREE.ReflectorForSSRPass = ReflectorForSSRPass;
  263. } )();