SSRrPass.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 } 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. var SSRrPass = function ( { renderer, scene, camera, width, height, selects, encoding, morphTargets = false } ) {
  25. Pass.call( this );
  26. this.width = ( width !== undefined ) ? width : 512;
  27. this.height = ( height !== undefined ) ? height : 512;
  28. this.clear = true;
  29. this.renderer = renderer;
  30. this.scene = scene;
  31. this.camera = camera;
  32. this.output = 0;
  33. // this.output = 1;
  34. this.ior = SSRrShader.uniforms.ior.value;
  35. this.maxDistance = SSRrShader.uniforms.maxDistance.value;
  36. this.surfDist = SSRrShader.uniforms.surfDist.value;
  37. this.encoding = encoding;
  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. var 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. var 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( { morphTargets } );
  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 Pass.FullScreenQuad( null );
  188. this.originalClearColor = new Color();
  189. };
  190. SSRrPass.prototype = Object.assign( Object.create( Pass.prototype ), {
  191. constructor: SSRrPass,
  192. dispose: function () {
  193. // dispose render targets
  194. this.beautyRenderTarget.dispose();
  195. this.specularRenderTarget.dispose();
  196. this.normalSelectsRenderTarget.dispose();
  197. this.refractiveRenderTarget.dispose();
  198. this.ssrrRenderTarget.dispose();
  199. // dispose materials
  200. this.normalMaterial.dispose();
  201. this.refractiveOnMaterial.dispose();
  202. this.refractiveOffMaterial.dispose();
  203. this.copyMaterial.dispose();
  204. this.depthRenderMaterial.dispose();
  205. // dipsose full screen quad
  206. this.fsQuad.dispose();
  207. },
  208. render: function ( renderer, writeBuffer /*, readBuffer, deltaTime, maskActive */ ) {
  209. // render beauty and depth
  210. if ( this.encoding ) this.beautyRenderTarget.texture.encoding = this.encoding;
  211. renderer.setRenderTarget( this.beautyRenderTarget );
  212. renderer.clear();
  213. this.scene.children.forEach( child => {
  214. if ( this.selects.includes( child ) ) {
  215. child.visible = false;
  216. } else {
  217. child.visible = true;
  218. }
  219. } );
  220. renderer.render( this.scene, this.camera );
  221. renderer.setRenderTarget( this.specularRenderTarget );
  222. renderer.clear();
  223. this.scene.children.forEach( child => {
  224. if ( this.selects.includes( child ) ) {
  225. child.visible = true;
  226. child._SSRrPassBackupMaterial = child.material;
  227. child.material = this.specularMaterial;
  228. } else if ( ! child.isLight ) {
  229. child.visible = false;
  230. }
  231. } );
  232. renderer.render( this.scene, this.camera );
  233. this.scene.children.forEach( child => {
  234. if ( this.selects.includes( child ) ) {
  235. child.material = child._SSRrPassBackupMaterial;
  236. }
  237. } );
  238. // render normalSelectss
  239. this.scene.children.forEach( child => {
  240. if ( this.selects.includes( child ) ) {
  241. child.visible = true;
  242. } else {
  243. child.visible = false;
  244. }
  245. } );
  246. this.renderOverride( renderer, this.normalMaterial, this.normalSelectsRenderTarget, 0, 0 );
  247. this.renderRefractive( renderer, this.refractiveOnMaterial, this.refractiveRenderTarget, 0, 0 );
  248. // render SSRr
  249. this.ssrrMaterial.uniforms[ 'ior' ].value = this.ior;
  250. this.ssrrMaterial.uniforms[ 'maxDistance' ].value = this.maxDistance;
  251. this.ssrrMaterial.uniforms[ 'surfDist' ].value = this.surfDist;
  252. this.ssrrMaterial.uniforms[ 'tSpecular' ].value = this.specularRenderTarget.texture;
  253. this.renderPass( renderer, this.ssrrMaterial, this.ssrrRenderTarget );
  254. // output result to screen
  255. switch ( this.output ) {
  256. case SSRrPass.OUTPUT.Default:
  257. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
  258. this.copyMaterial.blending = NoBlending;
  259. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  260. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.ssrrRenderTarget.texture;
  261. this.copyMaterial.blending = NormalBlending;
  262. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  263. break;
  264. case SSRrPass.OUTPUT.SSRr:
  265. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.ssrrRenderTarget.texture;
  266. this.copyMaterial.blending = NoBlending;
  267. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  268. break;
  269. case SSRrPass.OUTPUT.Beauty:
  270. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
  271. this.copyMaterial.blending = NoBlending;
  272. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  273. break;
  274. case SSRrPass.OUTPUT.Depth:
  275. this.depthRenderMaterial.uniforms[ 'tDepth' ].value = this.beautyRenderTarget.depthTexture;
  276. this.renderPass( renderer, this.depthRenderMaterial, this.renderToScreen ? null : writeBuffer );
  277. break;
  278. case SSRrPass.OUTPUT.DepthSelects:
  279. this.depthRenderMaterial.uniforms[ 'tDepth' ].value = this.normalSelectsRenderTarget.depthTexture;
  280. this.renderPass( renderer, this.depthRenderMaterial, this.renderToScreen ? null : writeBuffer );
  281. break;
  282. case SSRrPass.OUTPUT.NormalSelects:
  283. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.normalSelectsRenderTarget.texture;
  284. this.copyMaterial.blending = NoBlending;
  285. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  286. break;
  287. case SSRrPass.OUTPUT.Refractive:
  288. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.refractiveRenderTarget.texture;
  289. this.copyMaterial.blending = NoBlending;
  290. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  291. break;
  292. case SSRrPass.OUTPUT.Specular:
  293. this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.specularRenderTarget.texture;
  294. this.copyMaterial.blending = NoBlending;
  295. this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
  296. break;
  297. default:
  298. console.warn( 'THREE.SSRrPass: Unknown output type.' );
  299. }
  300. },
  301. renderPass: function ( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
  302. // save original state
  303. this.originalClearColor.copy( renderer.getClearColor( this.tempColor ) );
  304. var originalClearAlpha = renderer.getClearAlpha( this.tempColor );
  305. var originalAutoClear = renderer.autoClear;
  306. renderer.setRenderTarget( renderTarget );
  307. // setup pass state
  308. renderer.autoClear = false;
  309. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  310. renderer.setClearColor( clearColor );
  311. renderer.setClearAlpha( clearAlpha || 0.0 );
  312. renderer.clear();
  313. }
  314. this.fsQuad.material = passMaterial;
  315. this.fsQuad.render( renderer );
  316. // restore original state
  317. renderer.autoClear = originalAutoClear;
  318. renderer.setClearColor( this.originalClearColor );
  319. renderer.setClearAlpha( originalClearAlpha );
  320. },
  321. renderOverride: function ( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
  322. this.originalClearColor.copy( renderer.getClearColor( this.tempColor ) );
  323. var originalClearAlpha = renderer.getClearAlpha( this.tempColor );
  324. var originalAutoClear = renderer.autoClear;
  325. renderer.setRenderTarget( renderTarget );
  326. renderer.autoClear = false;
  327. clearColor = overrideMaterial.clearColor || clearColor;
  328. clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
  329. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  330. renderer.setClearColor( clearColor );
  331. renderer.setClearAlpha( clearAlpha || 0.0 );
  332. renderer.clear();
  333. }
  334. this.scene.overrideMaterial = overrideMaterial;
  335. renderer.render( this.scene, this.camera );
  336. this.scene.overrideMaterial = null;
  337. // restore original state
  338. renderer.autoClear = originalAutoClear;
  339. renderer.setClearColor( this.originalClearColor );
  340. renderer.setClearAlpha( originalClearAlpha );
  341. },
  342. renderRefractive: function ( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {
  343. this.originalClearColor.copy( renderer.getClearColor( this.tempColor ) );
  344. var originalClearAlpha = renderer.getClearAlpha( this.tempColor );
  345. var originalAutoClear = renderer.autoClear;
  346. renderer.setRenderTarget( renderTarget );
  347. renderer.autoClear = false;
  348. clearColor = overrideMaterial.clearColor || clearColor;
  349. clearAlpha = overrideMaterial.clearAlpha || clearAlpha;
  350. if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
  351. renderer.setClearColor( clearColor );
  352. renderer.setClearAlpha( clearAlpha || 0.0 );
  353. renderer.clear();
  354. }
  355. this.scene.children.forEach( child => {
  356. child.visible = true;
  357. } );
  358. this.scene.traverse( child => {
  359. child._SSRrPassBackupMaterial = child.material;
  360. if ( this.selects.includes( child ) ) {
  361. child.material = this.refractiveOnMaterial;
  362. } else {
  363. child.material = this.refractiveOffMaterial;
  364. }
  365. } );
  366. this.scene._SSRrPassBackupBackground = this.scene.background;
  367. this.scene.background = null;
  368. this.scene._SSRrPassBackupFog = this.scene.fog;
  369. this.scene.fog = null;
  370. renderer.render( this.scene, this.camera );
  371. this.scene.fog = this.scene._SSRrPassBackupFog;
  372. this.scene.background = this.scene._SSRrPassBackupBackground;
  373. this.scene.traverse( child => {
  374. child.material = child._SSRrPassBackupMaterial;
  375. } );
  376. // restore original state
  377. renderer.autoClear = originalAutoClear;
  378. renderer.setClearColor( this.originalClearColor );
  379. renderer.setClearAlpha( originalClearAlpha );
  380. },
  381. setSize: function ( width, height ) {
  382. this.width = width;
  383. this.height = height;
  384. this.ssrrMaterial.defines.MAX_STEP = Math.sqrt( width * width + height * height );
  385. this.ssrrMaterial.needsUpdate = true;
  386. this.beautyRenderTarget.setSize( width, height );
  387. this.specularRenderTarget.setSize( width, height );
  388. this.ssrrRenderTarget.setSize( width, height );
  389. this.normalSelectsRenderTarget.setSize( width, height );
  390. this.refractiveRenderTarget.setSize( width, height );
  391. this.ssrrMaterial.uniforms[ 'resolution' ].value.set( width, height );
  392. this.ssrrMaterial.uniforms[ 'cameraProjectionMatrix' ].value.copy( this.camera.projectionMatrix );
  393. this.ssrrMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.copy( this.camera.projectionMatrixInverse );
  394. },
  395. } );
  396. SSRrPass.OUTPUT = {
  397. 'Default': 0,
  398. 'SSRr': 1,
  399. 'Beauty': 3,
  400. 'Depth': 4,
  401. 'DepthSelects': 9,
  402. 'NormalSelects': 5,
  403. 'Refractive': 7,
  404. 'Specular': 8,
  405. };
  406. export { SSRrPass };