ReflectorForSSRPass.js 10 KB

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