RenderPixelatedPass.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import {
  2. WebGLRenderTarget,
  3. MeshNormalMaterial,
  4. ShaderMaterial,
  5. Vector2,
  6. Vector4,
  7. DepthTexture,
  8. NearestFilter
  9. } from 'three';
  10. import { Pass, FullScreenQuad } from './Pass.js';
  11. class RenderPixelatedPass extends Pass {
  12. constructor( pixelSize, scene, camera, options = {} ) {
  13. super();
  14. this.pixelSize = pixelSize;
  15. this.resolution = new Vector2();
  16. this.renderResolution = new Vector2();
  17. this.pixelatedMaterial = this.createPixelatedMaterial();
  18. this.normalMaterial = new MeshNormalMaterial();
  19. this.fsQuad = new FullScreenQuad( this.pixelatedMaterial );
  20. this.scene = scene;
  21. this.camera = camera;
  22. this.normalEdgeStrength = options.normalEdgeStrength || 0.3;
  23. this.depthEdgeStrength = options.depthEdgeStrength || 0.4;
  24. this.beautyRenderTarget = new WebGLRenderTarget();
  25. this.beautyRenderTarget.texture.minFilter = NearestFilter;
  26. this.beautyRenderTarget.texture.magFilter = NearestFilter;
  27. this.beautyRenderTarget.depthTexture = new DepthTexture();
  28. this.normalRenderTarget = new WebGLRenderTarget();
  29. this.normalRenderTarget.texture.minFilter = NearestFilter;
  30. this.normalRenderTarget.texture.magFilter = NearestFilter;
  31. }
  32. dispose() {
  33. this.beautyRenderTarget.dispose();
  34. this.normalRenderTarget.dispose();
  35. this.pixelatedMaterial.dispose();
  36. this.normalMaterial.dispose();
  37. this.fsQuad.dispose();
  38. }
  39. setSize( width, height ) {
  40. this.resolution.set( width, height );
  41. this.renderResolution.set( ( width / this.pixelSize ) | 0, ( height / this.pixelSize ) | 0 );
  42. const { x, y } = this.renderResolution;
  43. this.beautyRenderTarget.setSize( x, y );
  44. this.normalRenderTarget.setSize( x, y );
  45. this.fsQuad.material.uniforms.resolution.value.set( x, y, 1 / x, 1 / y );
  46. }
  47. setPixelSize( pixelSize ) {
  48. this.pixelSize = pixelSize;
  49. this.setSize( this.resolution.x, this.resolution.y );
  50. }
  51. render( renderer, writeBuffer ) {
  52. const uniforms = this.fsQuad.material.uniforms;
  53. uniforms.normalEdgeStrength.value = this.normalEdgeStrength;
  54. uniforms.depthEdgeStrength.value = this.depthEdgeStrength;
  55. renderer.setRenderTarget( this.beautyRenderTarget );
  56. renderer.render( this.scene, this.camera );
  57. const overrideMaterial_old = this.scene.overrideMaterial;
  58. renderer.setRenderTarget( this.normalRenderTarget );
  59. this.scene.overrideMaterial = this.normalMaterial;
  60. renderer.render( this.scene, this.camera );
  61. this.scene.overrideMaterial = overrideMaterial_old;
  62. uniforms.tDiffuse.value = this.beautyRenderTarget.texture;
  63. uniforms.tDepth.value = this.beautyRenderTarget.depthTexture;
  64. uniforms.tNormal.value = this.normalRenderTarget.texture;
  65. if ( this.renderToScreen ) {
  66. renderer.setRenderTarget( null );
  67. } else {
  68. renderer.setRenderTarget( writeBuffer );
  69. if ( this.clear ) renderer.clear();
  70. }
  71. this.fsQuad.render( renderer );
  72. }
  73. createPixelatedMaterial() {
  74. return new ShaderMaterial( {
  75. uniforms: {
  76. tDiffuse: { value: null },
  77. tDepth: { value: null },
  78. tNormal: { value: null },
  79. resolution: {
  80. value: new Vector4(
  81. this.renderResolution.x,
  82. this.renderResolution.y,
  83. 1 / this.renderResolution.x,
  84. 1 / this.renderResolution.y,
  85. )
  86. },
  87. normalEdgeStrength: { value: 0 },
  88. depthEdgeStrength: { value: 0 }
  89. },
  90. vertexShader:
  91. `
  92. varying vec2 vUv;
  93. void main() {
  94. vUv = uv;
  95. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  96. }
  97. `,
  98. fragmentShader:
  99. `
  100. uniform sampler2D tDiffuse;
  101. uniform sampler2D tDepth;
  102. uniform sampler2D tNormal;
  103. uniform vec4 resolution;
  104. uniform float normalEdgeStrength;
  105. uniform float depthEdgeStrength;
  106. varying vec2 vUv;
  107. float getDepth(int x, int y) {
  108. return texture2D( tDepth, vUv + vec2(x, y) * resolution.zw ).r;
  109. }
  110. vec3 getNormal(int x, int y) {
  111. return texture2D( tNormal, vUv + vec2(x, y) * resolution.zw ).rgb * 2.0 - 1.0;
  112. }
  113. float depthEdgeIndicator(float depth, vec3 normal) {
  114. float diff = 0.0;
  115. diff += clamp(getDepth(1, 0) - depth, 0.0, 1.0);
  116. diff += clamp(getDepth(-1, 0) - depth, 0.0, 1.0);
  117. diff += clamp(getDepth(0, 1) - depth, 0.0, 1.0);
  118. diff += clamp(getDepth(0, -1) - depth, 0.0, 1.0);
  119. return floor(smoothstep(0.01, 0.02, diff) * 2.) / 2.;
  120. }
  121. float neighborNormalEdgeIndicator(int x, int y, float depth, vec3 normal) {
  122. float depthDiff = getDepth(x, y) - depth;
  123. vec3 neighborNormal = getNormal(x, y);
  124. // Edge pixels should yield to faces who's normals are closer to the bias normal.
  125. vec3 normalEdgeBias = vec3(1., 1., 1.); // This should probably be a parameter.
  126. float normalDiff = dot(normal - neighborNormal, normalEdgeBias);
  127. float normalIndicator = clamp(smoothstep(-.01, .01, normalDiff), 0.0, 1.0);
  128. // Only the shallower pixel should detect the normal edge.
  129. float depthIndicator = clamp(sign(depthDiff * .25 + .0025), 0.0, 1.0);
  130. return (1.0 - dot(normal, neighborNormal)) * depthIndicator * normalIndicator;
  131. }
  132. float normalEdgeIndicator(float depth, vec3 normal) {
  133. float indicator = 0.0;
  134. indicator += neighborNormalEdgeIndicator(0, -1, depth, normal);
  135. indicator += neighborNormalEdgeIndicator(0, 1, depth, normal);
  136. indicator += neighborNormalEdgeIndicator(-1, 0, depth, normal);
  137. indicator += neighborNormalEdgeIndicator(1, 0, depth, normal);
  138. return step(0.1, indicator);
  139. }
  140. void main() {
  141. vec4 texel = texture2D( tDiffuse, vUv );
  142. float depth = 0.0;
  143. vec3 normal = vec3(0.0);
  144. if (depthEdgeStrength > 0.0 || normalEdgeStrength > 0.0) {
  145. depth = getDepth(0, 0);
  146. normal = getNormal(0, 0);
  147. }
  148. float dei = 0.0;
  149. if (depthEdgeStrength > 0.0)
  150. dei = depthEdgeIndicator(depth, normal);
  151. float nei = 0.0;
  152. if (normalEdgeStrength > 0.0)
  153. nei = normalEdgeIndicator(depth, normal);
  154. float Strength = dei > 0.0 ? (1.0 - depthEdgeStrength * dei) : (1.0 + normalEdgeStrength * nei);
  155. gl_FragColor = texel * Strength;
  156. }
  157. `
  158. } );
  159. }
  160. }
  161. export { RenderPixelatedPass };