ShaderPass.js 1.3 KB
Newer Older
A
alteredq 已提交
1 2 3 4
/**
 * @author alteredq / http://alteredqualia.com/
 */

S
SUNAG 已提交
5
THREE.ShaderPass = function( shader, textureID ) {
A
alteredq 已提交
6

7 8
	THREE.Pass.call( this );

9
	this.textureID = ( textureID !== undefined ) ? textureID : "tDiffuse";
A
alteredq 已提交
10

S
SUNAG 已提交
11
	if ( shader instanceof THREE.ShaderMaterial ) {
A
alteredq 已提交
12

S
SUNAG 已提交
13
		this.uniforms = shader.uniforms;
A
alteredq 已提交
14

S
SUNAG 已提交
15
		this.material = shader;
A
alteredq 已提交
16

S
SUNAG 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	}
	else if ( shader ) {

		this.uniforms = THREE.UniformsUtils.clone( shader.uniforms );

		this.material = new THREE.ShaderMaterial( {

			defines: shader.defines || {},
			uniforms: this.uniforms,
			vertexShader: shader.vertexShader,
			fragmentShader: shader.fragmentShader

		} );

	}
A
alteredq 已提交
32

G
gero3 已提交
33
	this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
S
SUNAG 已提交
34
	this.scene = new THREE.Scene();
35

M
Mr.doob 已提交
36
	this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
37 38
	this.scene.add( this.quad );

A
alteredq 已提交
39 40
};

41 42
THREE.ShaderPass.prototype = Object.create( THREE.Pass.prototype );

43
Object.assign( THREE.ShaderPass.prototype, {
44 45

	render: function( renderer, writeBuffer, readBuffer, delta, maskActive ) {
A
alteredq 已提交
46

47 48
		if ( this.uniforms[ this.textureID ] ) {

49
			this.uniforms[ this.textureID ].value = readBuffer.texture;
50 51

		}
A
alteredq 已提交
52

53
		this.quad.material = this.material;
A
alteredq 已提交
54 55 56

		if ( this.renderToScreen ) {

57
			renderer.render( this.scene, this.camera );
A
alteredq 已提交
58 59 60

		} else {

61
			renderer.render( this.scene, this.camera, writeBuffer, this.clear );
A
alteredq 已提交
62 63 64 65 66

		}

	}

67
} );