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

M
Mr.doob 已提交
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

M
Mr.doob 已提交
17
	} else if ( shader ) {
S
SUNAG 已提交
18

A
aardgoose 已提交
19
		this.uniforms = Object.assign( {}, shader.uniforms );
S
SUNAG 已提交
20 21 22 23 24 25 26 27 28 29 30

		this.material = new THREE.ShaderMaterial( {

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

		} );

	}
A
alteredq 已提交
31

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

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

A
alteredq 已提交
38 39
};

M
Mr.doob 已提交
40
THREE.ShaderPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
41

M
Mr.doob 已提交
42
	constructor: THREE.ShaderPass,
43 44

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

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

A
aardgoose 已提交
48
			this.uniforms[ this.textureID ] = new THREE.Uniform( readBuffer.texture );
49 50

		}
A
alteredq 已提交
51

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

		if ( this.renderToScreen ) {

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

		} else {

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

		}

	}

66
} );