GlitchPass.js 3.1 KB
Newer Older
1
/**
2
 * @author alteredq / http://alteredqualia.com/
3 4 5 6
 */

THREE.GlitchPass = function ( dt_size ) {

7 8
	THREE.Pass.call( this );

9
	if ( THREE.DigitalGlitch === undefined ) console.error( "THREE.GlitchPass relies on THREE.DigitalGlitch" );
10

11 12 13
	var shader = THREE.DigitalGlitch;
	this.uniforms = THREE.UniformsUtils.clone( shader.uniforms );

G
gero3 已提交
14
	if ( dt_size == undefined ) dt_size = 64;
15 16


G
gero3 已提交
17
	this.uniforms[ "tDisp" ].value = this.generateHeightmap( dt_size );
18

19

G
gero3 已提交
20
	this.material = new THREE.ShaderMaterial( {
21 22 23
		uniforms: this.uniforms,
		vertexShader: shader.vertexShader,
		fragmentShader: shader.fragmentShader
G
gero3 已提交
24
	} );
25

G
gero3 已提交
26
	this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
27 28
	this.scene  = new THREE.Scene();

M
Mr.doob 已提交
29
	this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
30
	this.scene.add( this.quad );
31

G
gero3 已提交
32 33
	this.goWild = false;
	this.curF = 0;
34
	this.generateTrigger();
35

36 37
};

38 39
THREE.GlitchPass.prototype = Object.create( THREE.Pass.prototype );

40 41
THREE.GlitchPass.prototype = {

42 43 44
	constructor: THREE.GlitchPass,

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

46
		this.uniforms[ "tDiffuse" ].value = readBuffer;
G
gero3 已提交
47 48
		this.uniforms[ 'seed' ].value = Math.random();//default seeding
		this.uniforms[ 'byp' ].value = 0;
49

50
		if ( this.curF % this.randX == 0 || this.goWild == true ) {
G
gero3 已提交
51

G
gero3 已提交
52
			this.uniforms[ 'amount' ].value = Math.random() / 30;
G
gero3 已提交
53 54 55 56 57
			this.uniforms[ 'angle' ].value = THREE.Math.randFloat( - Math.PI, Math.PI );
			this.uniforms[ 'seed_x' ].value = THREE.Math.randFloat( - 1, 1 );
			this.uniforms[ 'seed_y' ].value = THREE.Math.randFloat( - 1, 1 );
			this.uniforms[ 'distortion_x' ].value = THREE.Math.randFloat( 0, 1 );
			this.uniforms[ 'distortion_y' ].value = THREE.Math.randFloat( 0, 1 );
G
gero3 已提交
58
			this.curF = 0;
59
			this.generateTrigger();
G
gero3 已提交
60

G
gero3 已提交
61
		} else if ( this.curF % this.randX < this.randX / 5 ) {
G
gero3 已提交
62

G
gero3 已提交
63
			this.uniforms[ 'amount' ].value = Math.random() / 90;
G
gero3 已提交
64 65 66 67 68 69
			this.uniforms[ 'angle' ].value = THREE.Math.randFloat( - Math.PI, Math.PI );
			this.uniforms[ 'distortion_x' ].value = THREE.Math.randFloat( 0, 1 );
			this.uniforms[ 'distortion_y' ].value = THREE.Math.randFloat( 0, 1 );
			this.uniforms[ 'seed_x' ].value = THREE.Math.randFloat( - 0.3, 0.3 );
			this.uniforms[ 'seed_y' ].value = THREE.Math.randFloat( - 0.3, 0.3 );

G
gero3 已提交
70
		} else if ( this.goWild == false ) {
G
gero3 已提交
71

G
gero3 已提交
72
			this.uniforms[ 'byp' ].value = 1;
G
gero3 已提交
73

74
		}
75

G
gero3 已提交
76
		this.curF ++;
77
		this.quad.material = this.material;
78

79
		if ( this.renderToScreen ) {
G
gero3 已提交
80

81
			renderer.render( this.scene, this.camera );
G
gero3 已提交
82

G
gero3 已提交
83
		} else {
G
gero3 已提交
84

85
			renderer.render( this.scene, this.camera, writeBuffer, this.clear );
G
gero3 已提交
86

87
		}
G
gero3 已提交
88

89
	},
90

91
	generateTrigger: function() {
G
gero3 已提交
92 93 94

		this.randX = THREE.Math.randInt( 120, 240 );

95
	},
96

97
	generateHeightmap: function( dt_size ) {
G
gero3 已提交
98

G
gero3 已提交
99 100
		var data_arr = new Float32Array( dt_size * dt_size * 3 );
		var length = dt_size * dt_size;
101

102
		for ( var i = 0; i < length; i ++ ) {
G
gero3 已提交
103 104

			var val = THREE.Math.randFloat( 0, 1 );
G
gero3 已提交
105 106 107
			data_arr[ i * 3 + 0 ] = val;
			data_arr[ i * 3 + 1 ] = val;
			data_arr[ i * 3 + 2 ] = val;
G
gero3 已提交
108

109
		}
110

111 112 113
		var texture = new THREE.DataTexture( data_arr, dt_size, dt_size, THREE.RGBFormat, THREE.FloatType );
		texture.needsUpdate = true;
		return texture;
G
gero3 已提交
114

115
	}
116

G
gero3 已提交
117
};