ShaderPass.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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: 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, delta, 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.render( this.scene, this.camera );
  34. } else {
  35. renderer.render( this.scene, this.camera, writeBuffer, this.clear );
  36. }
  37. }
  38. } );