SSRrPass.js 16 KB

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