RenderPixelatedPass.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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: /* glsl */`
  91. varying vec2 vUv;
  92. void main() {
  93. vUv = uv;
  94. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  95. }
  96. `,
  97. fragmentShader: /* glsl */`
  98. uniform sampler2D tDiffuse;
  99. uniform sampler2D tDepth;
  100. uniform sampler2D tNormal;
  101. uniform vec4 resolution;
  102. uniform float normalEdgeStrength;
  103. uniform float depthEdgeStrength;
  104. varying vec2 vUv;
  105. float getDepth(int x, int y) {
  106. return texture2D( tDepth, vUv + vec2(x, y) * resolution.zw ).r;
  107. }
  108. vec3 getNormal(int x, int y) {
  109. return texture2D( tNormal, vUv + vec2(x, y) * resolution.zw ).rgb * 2.0 - 1.0;
  110. }
  111. float depthEdgeIndicator(float depth, vec3 normal) {
  112. float diff = 0.0;
  113. diff += clamp(getDepth(1, 0) - depth, 0.0, 1.0);
  114. diff += clamp(getDepth(-1, 0) - depth, 0.0, 1.0);
  115. diff += clamp(getDepth(0, 1) - depth, 0.0, 1.0);
  116. diff += clamp(getDepth(0, -1) - depth, 0.0, 1.0);
  117. return floor(smoothstep(0.01, 0.02, diff) * 2.) / 2.;
  118. }
  119. float neighborNormalEdgeIndicator(int x, int y, float depth, vec3 normal) {
  120. float depthDiff = getDepth(x, y) - depth;
  121. vec3 neighborNormal = getNormal(x, y);
  122. // Edge pixels should yield to faces who's normals are closer to the bias normal.
  123. vec3 normalEdgeBias = vec3(1., 1., 1.); // This should probably be a parameter.
  124. float normalDiff = dot(normal - neighborNormal, normalEdgeBias);
  125. float normalIndicator = clamp(smoothstep(-.01, .01, normalDiff), 0.0, 1.0);
  126. // Only the shallower pixel should detect the normal edge.
  127. float depthIndicator = clamp(sign(depthDiff * .25 + .0025), 0.0, 1.0);
  128. return (1.0 - dot(normal, neighborNormal)) * depthIndicator * normalIndicator;
  129. }
  130. float normalEdgeIndicator(float depth, vec3 normal) {
  131. float indicator = 0.0;
  132. indicator += neighborNormalEdgeIndicator(0, -1, depth, normal);
  133. indicator += neighborNormalEdgeIndicator(0, 1, depth, normal);
  134. indicator += neighborNormalEdgeIndicator(-1, 0, depth, normal);
  135. indicator += neighborNormalEdgeIndicator(1, 0, depth, normal);
  136. return step(0.1, indicator);
  137. }
  138. void main() {
  139. vec4 texel = texture2D( tDiffuse, vUv );
  140. float depth = 0.0;
  141. vec3 normal = vec3(0.0);
  142. if (depthEdgeStrength > 0.0 || normalEdgeStrength > 0.0) {
  143. depth = getDepth(0, 0);
  144. normal = getNormal(0, 0);
  145. }
  146. float dei = 0.0;
  147. if (depthEdgeStrength > 0.0)
  148. dei = depthEdgeIndicator(depth, normal);
  149. float nei = 0.0;
  150. if (normalEdgeStrength > 0.0)
  151. nei = normalEdgeIndicator(depth, normal);
  152. float Strength = dei > 0.0 ? (1.0 - depthEdgeStrength * dei) : (1.0 + normalEdgeStrength * nei);
  153. gl_FragColor = texel * Strength;
  154. }
  155. `
  156. } );
  157. }
  158. }
  159. export { RenderPixelatedPass };