SSRrPass.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. import {
  2. AddEquation,
  3. Color,
  4. NormalBlending,
  5. DepthTexture,
  6. SrcAlphaFactor,
  7. OneMinusSrcAlphaFactor,
  8. MeshNormalMaterial,
  9. MeshBasicMaterial,
  10. NearestFilter,
  11. NoBlending,
  12. RGBAFormat,
  13. ShaderMaterial,
  14. UniformsUtils,
  15. UnsignedShortType,
  16. WebGLRenderTarget,
  17. HalfFloatType,
  18. MeshStandardMaterial
  19. } from 'three';
  20. import { Pass, FullScreenQuad } from '../postprocessing/Pass.js';
  21. import { SSRrShader } from '../shaders/SSRrShader.js';
  22. import { SSRrDepthShader } from '../shaders/SSRrShader.js';
  23. import { CopyShader } from '../shaders/CopyShader.js';
  24. class SSRrPass extends Pass {
  25. constructor( { renderer, scene, camera, width, height, selects, encoding } ) {
  26. super();
  27. this.width = ( width !== undefined ) ? width : 512;
  28. this.height = ( height !== undefined ) ? height : 512;
  29. this.clear = true;
  30. this.renderer = renderer;
  31. this.scene = scene;
  32. this.camera = camera;
  33. this.output = 0;
  34. // this.output = 1;
  35. this.ior = SSRrShader.uniforms.ior.value;
  36. this.maxDistance = SSRrShader.uniforms.maxDistance.value;
  37. this.surfDist = SSRrShader.uniforms.surfDist.value;
  38. this.encoding = encoding;
  39. this.tempColor = new Color();
  40. this.selects = selects;
  41. this._specular = SSRrShader.defines.SPECULAR;
  42. Object.defineProperty( this, 'specular', {
  43. get() {
  44. return this._specular;
  45. },
  46. set( val ) {
  47. if ( this._specular === val ) return;
  48. this._specular = val;
  49. this.ssrrMaterial.defines.SPECULAR = val;
  50. this.ssrrMaterial.needsUpdate = true;
  51. }
  52. } );
  53. this._fillHole = SSRrShader.defines.FILL_HOLE;
  54. Object.defineProperty( this, 'fillHole', {
  55. get() {
  56. return this._fillHole;
  57. },
  58. set( val ) {
  59. if ( this._fillHole === val ) return;
  60. this._fillHole = val;
  61. this.ssrrMaterial.defines.FILL_HOLE = val;
  62. this.ssrrMaterial.needsUpdate = true;
  63. }
  64. } );
  65. this._infiniteThick = SSRrShader.defines.INFINITE_THICK;
  66. Object.defineProperty( this, 'infiniteThick', {
  67. get() {
  68. return this._infiniteThick;
  69. },
  70. set( val ) {
  71. if ( this._infiniteThick === val ) return;
  72. this._infiniteThick = val;
  73. this.ssrrMaterial.defines.INFINITE_THICK = val;
  74. this.ssrrMaterial.needsUpdate = true;
  75. }
  76. } );
  77. // beauty render target with depth buffer
  78. const depthTexture = new DepthTexture();
  79. depthTexture.type = UnsignedShortType;
  80. depthTexture.minFilter = NearestFilter;
  81. depthTexture.magFilter = NearestFilter;
  82. this.beautyRenderTarget = new WebGLRenderTarget( this.width, this.height, {
  83. minFilter: NearestFilter,
  84. magFilter: NearestFilter,
  85. format: RGBAFormat,
  86. depthTexture: depthTexture,
  87. depthBuffer: true
  88. } );
  89. this.specularRenderTarget = new WebGLRenderTarget( this.width, this.height, { // TODO: Can merge with refractiveRenderTarget?
  90. minFilter: NearestFilter,
  91. magFilter: NearestFilter,
  92. format: RGBAFormat,
  93. } );
  94. // normalSelects render target
  95. const depthTextureSelects = new DepthTexture();
  96. depthTextureSelects.type = UnsignedShortType;
  97. depthTextureSelects.minFilter = NearestFilter;
  98. depthTextureSelects.magFilter = NearestFilter;
  99. this.normalSelectsRenderTarget = new WebGLRenderTarget( this.width, this.height, {
  100. minFilter: NearestFilter,
  101. magFilter: NearestFilter,
  102. format: RGBAFormat,
  103. type: HalfFloatType,
  104. depthTexture: depthTextureSelects,
  105. depthBuffer: true
  106. } );
  107. // refractive render target
  108. this.refractiveRenderTarget = new WebGLRenderTarget( this.width, this.height, {
  109. minFilter: NearestFilter,
  110. magFilter: NearestFilter,
  111. format: RGBAFormat
  112. } );
  113. // ssrr render target
  114. this.ssrrRenderTarget = new WebGLRenderTarget( this.width, this.height, {
  115. minFilter: NearestFilter,
  116. magFilter: NearestFilter,
  117. format: RGBAFormat
  118. } );
  119. // ssrr material
  120. if ( SSRrShader === undefined ) {
  121. console.error( 'THREE.SSRrPass: The pass relies on SSRrShader.' );
  122. }
  123. this.ssrrMaterial = new ShaderMaterial( {
  124. defines: Object.assign( {}, SSRrShader.defines, {
  125. MAX_STEP: Math.sqrt( this.width * this.width + this.height * this.height )
  126. } ),
  127. uniforms: UniformsUtils.clone( SSRrShader.uniforms ),
  128. vertexShader: SSRrShader.vertexShader,
  129. fragmentShader: SSRrShader.fragmentShader,
  130. blending: NoBlending
  131. } );
  132. this.ssrrMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
  133. this.ssrrMaterial.uniforms[ 'tSpecular' ].value = this.specularRenderTarget.texture;
  134. this.ssrrMaterial.uniforms[ 'tNormalSelects' ].value = this.normalSelectsRenderTarget.texture;
  135. this.ssrrMaterial.needsUpdate = true;
  136. this.ssrrMaterial.uniforms[ 'tRefractive' ].value = this.refractiveRenderTarget.texture;
  137. this.ssrrMaterial.uniforms[ 'tDepth' ].value = this.beautyRenderTarget.depthTexture;
  138. this.ssrrMaterial.uniforms[ 'tDepthSelects' ].value = this.normalSelectsRenderTarget.depthTexture;
  139. this.ssrrMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  140. this.ssrrMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  141. this.ssrrMaterial.uniforms[ 'resolution' ].value.set( this.width, this.height );
  142. this.ssrrMaterial.uniforms[ 'cameraProjectionMatrix' ].value.copy( this.camera.projectionMatrix );
  143. this.ssrrMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  144. // normal material
  145. this.normalMaterial = new MeshNormalMaterial();
  146. this.normalMaterial.blending = NoBlending;
  147. // refractiveOn material
  148. this.refractiveOnMaterial = new MeshBasicMaterial( {
  149. color: 'white'
  150. } );
  151. // refractiveOff material
  152. this.refractiveOffMaterial = new MeshBasicMaterial( {
  153. color: 'black'
  154. } );
  155. // specular material
  156. this.specularMaterial = new MeshStandardMaterial( {
  157. color: 'black',
  158. metalness: 0,
  159. roughness: .2,
  160. } );
  161. // material for rendering the depth
  162. this.depthRenderMaterial = new ShaderMaterial( {
  163. defines: Object.assign( {}, SSRrDepthShader.defines ),
  164. uniforms: UniformsUtils.clone( SSRrDepthShader.uniforms ),
  165. vertexShader: SSRrDepthShader.vertexShader,
  166. fragmentShader: SSRrDepthShader.fragmentShader,
  167. blending: NoBlending
  168. } );
  169. this.depthRenderMaterial.uniforms[ 'tDepth' ].value = this.beautyRenderTarget.depthTexture;
  170. this.depthRenderMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
  171. this.depthRenderMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
  172. // material for rendering the content of a render target
  173. this.copyMaterial = new ShaderMaterial( {
  174. uniforms: UniformsUtils.clone( CopyShader.uniforms ),
  175. vertexShader: CopyShader.vertexShader,
  176. fragmentShader: CopyShader.fragmentShader,
  177. transparent: true,
  178. depthTest: false,
  179. depthWrite: false,
  180. blendSrc: SrcAlphaFactor,
  181. blendDst: OneMinusSrcAlphaFactor,
  182. blendEquation: AddEquation,
  183. blendSrcAlpha: SrcAlphaFactor,
  184. blendDstAlpha: OneMinusSrcAlphaFactor,
  185. blendEquationAlpha: AddEquation,
  186. // premultipliedAlpha:true,
  187. } );
  188. this.fsQuad = new FullScreenQuad( null );
  189. this.originalClearColor = new Color();
  190. }
  191. dispose() {
  192. // dispose render targets
  193. this.beautyRenderTarget.dispose();
  194. this.specularRenderTarget.dispose();
  195. this.normalSelectsRenderTarget.dispose();
  196. this.refractiveRenderTarget.dispose();
  197. this.ssrrRenderTarget.dispose();
  198. // dispose materials
  199. this.normalMaterial.dispose();
  200. this.refractiveOnMaterial.dispose();
  201. this.refractiveOffMaterial.dispose();
  202. this.copyMaterial.dispose();
  203. this.depthRenderMaterial.dispose();
  204. // dipsose full screen quad
  205. this.fsQuad.dispose();
  206. }
  207. render( renderer, writeBuffer /*, readBuffer, deltaTime, maskActive */ ) {
  208. // render beauty and depth
  209. if ( this.encoding ) this.beautyRenderTarget.texture.encoding = this.encoding;
  210. renderer.setRenderTarget( this.beautyRenderTarget );
  211. renderer.clear();
  212. this.scene.children.forEach( child => {
  213. if ( this.selects.includes( child ) ) {
  214. child.visible = false;
  215. } else {
  216. child.visible = true;
  217. }
  218. } );
  219. renderer.render( this.scene, this.camera );
  220. renderer.setRenderTarget( this.specularRenderTarget );
  221. renderer.clear();
  222. this.scene.children.forEach( child => {
  223. if ( this.selects.includes( child ) ) {
  224. child.visible = true;
  225. child._SSRrPassBackupMaterial = child.material;
  226. child.material = this.specularMaterial;
  227. } else if ( ! child.isLight ) {
  228. child.visible = false;
  229. }
  230. } );
  231. renderer.render( this.scene, this.camera );
  232. this.scene.children.forEach( child => {
  233. if ( this.selects.includes( child ) ) {
  234. child.material = child._SSRrPassBackupMaterial;
  235. }
  236. } );
  237. // render normalSelectss
  238. this.scene.children.forEach( child => {
  239. if ( this.selects.includes( child ) ) {
  240. child.visible = true;
  241. } else {
  242. child.visible = false;
  243. }
  244. } );
  245. this.renderOverride( renderer, this.normalMaterial, this.normalSelectsRenderTarget, 0, 0 );
  246. this.renderRefractive( renderer, this.refractiveOnMaterial, this.refractiveRenderTarget, 0, 0 );
  247. // render SSRr
  248. this.ssrrMaterial.uniforms[ 'ior' ].value = this.ior;
  249. this.ssrrMaterial.uniforms[ 'maxDistance' ].value = this.maxDistance;
  250. this.ssrrMaterial.uniforms[ 'surfDist' ].value = this.surfDist;
  251. this.ssrrMaterial.uniforms[ 'tSpecular' ].value = this.specularRenderTarget.texture;
  252. this.renderPass( renderer, this.ssrrMaterial, this.ssrrRenderTarget );
  253. // output result to screen
  254. switch ( this.output ) {
  255. case SSRrPass.OUTPUT.Default:
  256. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
  257. this.copyMaterial.blending = NoBlending;
  258. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  259. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.ssrrRenderTarget.texture;
  260. this.copyMaterial.blending = NormalBlending;
  261. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  262. break;
  263. case SSRrPass.OUTPUT.SSRr:
  264. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.ssrrRenderTarget.texture;
  265. this.copyMaterial.blending = NoBlending;
  266. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  267. break;
  268. case SSRrPass.OUTPUT.Beauty:
  269. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
  270. this.copyMaterial.blending = NoBlending;
  271. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  272. break;
  273. case SSRrPass.OUTPUT.Depth:
  274. this.depthRenderMaterial.uniforms[ 'tDepth' ].value = this.beautyRenderTarget.depthTexture;
  275. this.renderPass( renderer, this.depthRenderMaterial, this.renderToScreen ? null : writeBuffer );
  276. break;
  277. case SSRrPass.OUTPUT.DepthSelects:
  278. this.depthRenderMaterial.uniforms[ 'tDepth' ].value = this.normalSelectsRenderTarget.depthTexture;
  279. this.renderPass( renderer, this.depthRenderMaterial, this.renderToScreen ? null : writeBuffer );
  280. break;
  281. case SSRrPass.OUTPUT.NormalSelects:
  282. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.normalSelectsRenderTarget.texture;
  283. this.copyMaterial.blending = NoBlending;
  284. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  285. break;
  286. case SSRrPass.OUTPUT.Refractive:
  287. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.refractiveRenderTarget.texture;
  288. this.copyMaterial.blending = NoBlending;
  289. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  290. break;
  291. case SSRrPass.OUTPUT.Specular:
  292. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.specularRenderTarget.texture;
  293. this.copyMaterial.blending = NoBlending;
  294. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  295. break;
  296. default:
  297. console.warn( 'THREE.SSRrPass: Unknown output type.' );
  298. }
  299. }
  300. renderPass( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
  301. // save original state
  302. this.originalClearColor.copy( renderer.getClearColor( this.tempColor ) );
  303. const originalClearAlpha = renderer.getClearAlpha( this.tempColor );
  304. const originalAutoClear = renderer.autoClear;
  305. renderer.setRenderTarget( renderTarget );
  306. // setup pass state
  307. renderer.autoClear = false;
  308. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  309. renderer.setClearColor( clearColor );
  310. renderer.setClearAlpha( clearAlpha || 0.0 );
  311. renderer.clear();
  312. }
  313. this.fsQuad.material = passMaterial;
  314. this.fsQuad.render( renderer );
  315. // restore original state
  316. renderer.autoClear = originalAutoClear;
  317. renderer.setClearColor( this.originalClearColor );
  318. renderer.setClearAlpha( originalClearAlpha );
  319. }
  320. renderOverride( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
  321. this.originalClearColor.copy( renderer.getClearColor( this.tempColor ) );
  322. const originalClearAlpha = renderer.getClearAlpha( this.tempColor );
  323. const originalAutoClear = renderer.autoClear;
  324. renderer.setRenderTarget( renderTarget );
  325. renderer.autoClear = false;
  326. clearColor = overrideMaterial.clearColor || clearColor;
  327. clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
  328. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  329. renderer.setClearColor( clearColor );
  330. renderer.setClearAlpha( clearAlpha || 0.0 );
  331. renderer.clear();
  332. }
  333. this.scene.overrideMaterial = overrideMaterial;
  334. renderer.render( this.scene, this.camera );
  335. this.scene.overrideMaterial = null;
  336. // restore original state
  337. renderer.autoClear = originalAutoClear;
  338. renderer.setClearColor( this.originalClearColor );
  339. renderer.setClearAlpha( originalClearAlpha );
  340. }
  341. renderRefractive( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
  342. this.originalClearColor.copy( renderer.getClearColor( this.tempColor ) );
  343. const originalClearAlpha = renderer.getClearAlpha( this.tempColor );
  344. const originalAutoClear = renderer.autoClear;
  345. renderer.setRenderTarget( renderTarget );
  346. renderer.autoClear = false;
  347. clearColor = overrideMaterial.clearColor || clearColor;
  348. clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
  349. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  350. renderer.setClearColor( clearColor );
  351. renderer.setClearAlpha( clearAlpha || 0.0 );
  352. renderer.clear();
  353. }
  354. this.scene.children.forEach( child => {
  355. child.visible = true;
  356. } );
  357. this.scene.traverse( child => {
  358. child._SSRrPassBackupMaterial = child.material;
  359. if ( this.selects.includes( child ) ) {
  360. child.material = this.refractiveOnMaterial;
  361. } else {
  362. child.material = this.refractiveOffMaterial;
  363. }
  364. } );
  365. this.scene._SSRrPassBackupBackground = this.scene.background;
  366. this.scene.background = null;
  367. this.scene._SSRrPassBackupFog = this.scene.fog;
  368. this.scene.fog = null;
  369. renderer.render( this.scene, this.camera );
  370. this.scene.fog = this.scene._SSRrPassBackupFog;
  371. this.scene.background = this.scene._SSRrPassBackupBackground;
  372. this.scene.traverse( child => {
  373. child.material = child._SSRrPassBackupMaterial;
  374. } );
  375. // restore original state
  376. renderer.autoClear = originalAutoClear;
  377. renderer.setClearColor( this.originalClearColor );
  378. renderer.setClearAlpha( originalClearAlpha );
  379. }
  380. setSize( width, height ) {
  381. this.width = width;
  382. this.height = height;
  383. this.ssrrMaterial.defines.MAX_STEP = Math.sqrt( width * width + height * height );
  384. this.ssrrMaterial.needsUpdate = true;
  385. this.beautyRenderTarget.setSize( width, height );
  386. this.specularRenderTarget.setSize( width, height );
  387. this.ssrrRenderTarget.setSize( width, height );
  388. this.normalSelectsRenderTarget.setSize( width, height );
  389. this.refractiveRenderTarget.setSize( width, height );
  390. this.ssrrMaterial.uniforms[ 'resolution' ].value.set( width, height );
  391. this.ssrrMaterial.uniforms[ 'cameraProjectionMatrix' ].value.copy( this.camera.projectionMatrix );
  392. this.ssrrMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  393. }
  394. }
  395. SSRrPass.OUTPUT = {
  396. 'Default': 0,
  397. 'SSRr': 1,
  398. 'Beauty': 3,
  399. 'Depth': 4,
  400. 'DepthSelects': 9,
  401. 'NormalSelects': 5,
  402. 'Refractive': 7,
  403. 'Specular': 8,
  404. };
  405. export { SSRrPass };