SSRrPass.js 16 KB

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