ShaderPass.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.ShaderPass = function ( shader, textureID ) {
  5. THREE.Pass.call( this );
  6. this.textureID = ( textureID !== undefined ) ? textureID : "tDiffuse";
  7. if ( shader instanceof THREE.ShaderMaterial ) {
  8. this.uniforms = shader.uniforms;
  9. this.material = shader;
  10. } else if ( shader ) {
  11. this.uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  12. this.material = new THREE.ShaderMaterial( {
  13. defines: Object.assign( {}, shader.defines ),
  14. uniforms: this.uniforms,
  15. vertexShader: shader.vertexShader,
  16. fragmentShader: shader.fragmentShader
  17. } );
  18. }
  19. this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  20. this.scene = new THREE.Scene();
  21. this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
  22. this.quad.frustumCulled = false; // Avoid getting clipped
  23. this.scene.add( this.quad );
  24. };
  25. THREE.ShaderPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  26. constructor: THREE.ShaderPass,
  27. render: function ( renderer, writeBuffer, readBuffer, deltaTime, maskActive ) {
  28. if ( this.uniforms[ this.textureID ] ) {
  29. this.uniforms[ this.textureID ].value = readBuffer.texture;
  30. }
  31. this.quad.material = this.material;
  32. if ( this.renderToScreen ) {
  33. renderer.setRenderTarget( null );
  34. renderer.render( this.scene, this.camera );
  35. } else {
  36. renderer.setRenderTarget( writeBuffer );
  37. // TODO: Avoid using autoClear properties, see https://github.com/mrdoob/three.js/pull/15571#issuecomment-465669600
  38. if ( this.clear ) renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil );
  39. renderer.render( this.scene, this.camera );
  40. }
  41. }
  42. } );