MSAAShader.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @author bhouston / http://clara.io/
  3. *
  4. * Multi-Sample Anti-aliasing shader - for blending together sample buffers
  5. */
  6. THREE.MSAA4Shader = {
  7. shaderID: "msaa",
  8. uniforms: {
  9. "tBackground": { type: "t", value: null },
  10. "tSample0": { type: "t", value: null },
  11. "tSample1": { type: "t", value: null },
  12. //"tSample2": { type: "t", value: null },
  13. //"tSample3": { type: "t", value: null },
  14. "nSamples": { type: "i", value: 4 },
  15. "scale": { type: "f", value: 1.0 }
  16. },
  17. vertexShader: [
  18. "varying vec2 vUv;",
  19. "void main() {",
  20. "vUv = uv;",
  21. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  22. "}"
  23. ].join("\n"),
  24. fragmentShader: [
  25. "varying vec2 vUv;",
  26. "uniform sampler2D tBackground;",
  27. "uniform sampler2D tSample0;",
  28. "uniform sampler2D tSample1;",
  29. "uniform int nSamples;",
  30. "uniform float scale;",
  31. "vec4 composite( vec4 foreground, vec4 background ) {",
  32. "return vec4( mix( background.rgb * background.a, foreground.rgb, foreground.a ), background.a * ( 1.0 - foreground.a ) + foreground.a );",
  33. "}",
  34. "void main() {",
  35. "vec4 background = texture2D( tBackground, vUv );",
  36. "vec4 foreground = composite( texture2D( tSample0, vUv ), background );",
  37. "if( scale < 0.9 ) {",
  38. "foreground += composite( texture2D( tSample1, vUv ), background );",
  39. "}",
  40. "gl_FragColor = vec4( foreground.rgb * scale, foreground.a * scale );",
  41. "}"
  42. ].join("\n")
  43. };