-
Notifications
You must be signed in to change notification settings - Fork 94
/
output.js
executable file
·124 lines (102 loc) · 2.58 KB
/
output.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
//const transforms = require('./glsl-transforms.js')
var Output = function ({ regl, precision, label = "", width, height}) {
this.regl = regl
this.precision = precision
this.label = label
this.positionBuffer = this.regl.buffer([
[-2, 0],
[0, -2],
[2, 2]
])
this.draw = () => {}
this.init()
this.pingPongIndex = 0
// for each output, create two fbos for pingponging
this.fbos = (Array(2)).fill().map(() => this.regl.framebuffer({
color: this.regl.texture({
mag: 'nearest',
width: width,
height: height,
format: 'rgba'
}),
depthStencil: false
}))
// array containing render passes
// this.passes = []
}
Output.prototype.resize = function(width, height) {
this.fbos.forEach((fbo) => {
fbo.resize(width, height)
})
// console.log(this)
}
Output.prototype.getCurrent = function () {
return this.fbos[this.pingPongIndex]
}
Output.prototype.getTexture = function () {
var index = this.pingPongIndex ? 0 : 1
return this.fbos[index]
}
Output.prototype.init = function () {
// console.log('clearing')
this.transformIndex = 0
this.fragHeader = `
precision ${this.precision} float;
uniform float time;
varying vec2 uv;
`
this.fragBody = ``
this.vert = `
precision ${this.precision} float;
attribute vec2 position;
varying vec2 uv;
void main () {
uv = position;
gl_Position = vec4(2.0 * position - 1.0, 0, 1);
}`
this.attributes = {
position: this.positionBuffer
}
this.uniforms = {
time: this.regl.prop('time'),
resolution: this.regl.prop('resolution')
}
this.frag = `
${this.fragHeader}
void main () {
vec4 c = vec4(0, 0, 0, 0);
vec2 st = uv;
${this.fragBody}
gl_FragColor = c;
}
`
return this
}
Output.prototype.render = function (passes) {
let pass = passes[0]
//console.log('pass', pass, this.pingPongIndex)
var self = this
var uniforms = Object.assign(pass.uniforms, { prevBuffer: () => {
//var index = this.pingPongIndex ? 0 : 1
// var index = self.pingPong[(passIndex+1)%2]
// console.log('ping pong', self.pingPongIndex)
return self.fbos[self.pingPongIndex]
}
})
self.draw = self.regl({
frag: pass.frag,
vert: self.vert,
attributes: self.attributes,
uniforms: uniforms,
count: 3,
framebuffer: () => {
self.pingPongIndex = self.pingPongIndex ? 0 : 1
return self.fbos[self.pingPongIndex]
}
})
}
Output.prototype.tick = function (props) {
// console.log(props)
this.draw(props)
}
export default Output