-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fluid.js
137 lines (95 loc) · 2.83 KB
/
Fluid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var Shader = require( 'gl-shader' );
var FBO = require( 'gl-fbo' );
var drawTriangle = require( 'a-big-triangle' );
var glslify = require( 'glslify' );
var defaultOpts = {
width: 256,
height: 256,
mode: '2d',
wrap: true
};
var Fluid = function( gl, opts ){
var w = opts.width || defaultOpts.width;
var h = opts.height || defaultOpts.height;
var mode = opts.mode || defaultOpts.mode;
var wrap = opts.wrap || defaultOpts.wrap;
this.gl = gl;
this._current = 0;
this._mode = mode;
this._texel = [ 1 / w, 1 / h ];
this._shape = [ w, h ];
this.states = [
FBO( gl, [ w, h ] ),
FBO( gl, [ w, h ] )
];
this.states[0].wrap = gl.MIRRORED_REPEAT;
this.states[1].wrap = gl.MIRRORED_REPEAT;
var vs = glslify( './glsl/fluid.vert' );
var fs;
if( mode === '1d' ){
fs = glslify( './glsl/fluid1d.frag' );
}else
if( mode === '2d' ){
fs = glslify( './glsl/fluid2d.frag' );
}
this._shader = Shader( gl, vs, fs );
};
var create = function( gl, opts ){
return new Fluid( gl, opts );
};
create.Fluid = Fluid;
module.exports = create;
Fluid.prototype = {
update: function(){
var gl = this.gl;
var prev = this.states[ this._current ];
var next = this.states[ this._current ^= 1 ];
this._shader.bind();
this._shader.uniforms.applyDrop = false;
this._shader.uniforms.previous = prev.color[0].bind(0);
this._shader.uniforms.texel = this._texel;
next.bind();
drawTriangle( gl );
gl.bindFramebuffer( gl.FRAMEBUFFER, null );
},
droplet: function( x, y, scale, strength, texture ){
var gl = this.gl;
if( this._mode === '1d' ){
// fake for now.
//x = 1;
//y = 10;
console.log( x, y );
}
var prev = this.states[ this._current ];
var next = this.states[ this._current ^= 1 ];
if( typeof scale === 'number' ){
scale = [ scale, scale ];
}
var shape = this._shape;//.slice(0,this._shape.length);
//shape[1] = 256 * 1.01;
//shape[0] = 256 * 0.99; // this gives some nice effects
//shape[1] = 1;
var scaleX = texture.shape[0] * scale[0];
var scaleY = texture.shape[1] * scale[1];
this._shader.bind();
this._shader.uniforms.previous = prev.color[0].bind(0);
this._shader.uniforms.applyDrop = true;
this._shader.uniforms.droplet = texture.bind(1);
this._shader.uniforms.drop_position = [ x, shape[1] - y ];
this._shader.uniforms.drop_scale = [ scaleX, scaleY ];
this._shader.uniforms.drop_strength = strength;
if( this._mode === '1d' ){
this._shader.uniforms.min = ( shape[1] - y ) - 1;
this._shader.uniforms.max = ( shape[1] - y );
}
next.bind();
//gl.viewport( 0, y-1, shape[0], shape[1] );
//gl.viewport( 0,this._shape[y]/2, this._shape[0],this._shape[1] );
//gl.viewport( -212,10, 0, 512 - 256 );
drawTriangle(gl);
gl.bindFramebuffer( gl.FRAMEBUFFER, null );
},
texture: function(){
return this.states[ this._current ].color[0];
}
};