-
Notifications
You must be signed in to change notification settings - Fork 0
/
p5Fbo.js
177 lines (142 loc) · 5.04 KB
/
p5Fbo.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
class p5Fbo {
constructor({
renderer,
width,
height,
interpolationMode = LINEAR,
wrapMode = CLAMP,
floatTexture = false
} = {}) {
this.width = width;
this.height = height;
this.gl = renderer.GL;
const gl = this.gl;
this.renderer = renderer;
// Create and bind texture
let im = new p5.Image(this.width, this.height);
this.texture = new p5.Texture(this.renderer, im, { dataType: floatTexture ? gl.FLOAT : gl.UNSIGNED_BYTE });
this.texture.setInterpolation(interpolationMode, interpolationMode);
this.texture.setWrapMode(wrapMode);
this.originalProjectionMatrix = this.renderer.uPMatrix.copy();
this.originalModelViewMatrix = this.renderer.uMVMatrix.copy();
// define size and format of level 0
const level = 0;
// Create and bind the framebuffer
this.frameBuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer);
// attach the texture as the first color attachment
const attachmentPoint = gl.COLOR_ATTACHMENT0;
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
attachmentPoint,
gl.TEXTURE_2D,
this.texture.glTex,
level
);
// create a depth renderbuffer
const depthBuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
// make a depth buffer and the same size as the targetTexture
gl.renderbufferStorage(
gl.RENDERBUFFER,
gl.DEPTH_COMPONENT16,
this.width,
this.height
);
gl.framebufferRenderbuffer(
gl.FRAMEBUFFER,
gl.DEPTH_ATTACHMENT,
gl.RENDERBUFFER,
depthBuffer
);
// Bind back to null
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
this.defaultCamera = this.renderer._curCamera;
// console.log(this.renderer.pop);
// this.fboCamera = createCamera()
// this.fboCamera.perspective(this.defaultCamera.defaultCameraFOV, this.width / this.height, 0.1, 500);
// console.log(this.defaultCamera);
// setCamera(this.defaultCamera);
}
// Call this function whenever you want to start rendering into your fbo
begin() {
const gl = this.gl;
// This is necessary to prevent p5 from using the wrong shader
this.renderer._tex = null;
// render to our targetTexture by binding the framebuffer
gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer);
// render cube with our 3x2 texture
gl.bindTexture(gl.TEXTURE_2D, this.texture.glTex);
// Tell WebGL how to convert from clip space to pixels
gl.viewport(0, 0, this.width, this.height);
this.renderer._pInst.push();
// set projection matrix to size of fbo texture
this.computeCameraSettings();
}
// Updates camera to the correct aspect and size
computeCameraSettings() {
this.renderer._curCamera.defaultCameraFOV = 60 / 180 * Math.PI;
this.renderer._curCamera.defaultAspectRatio = this.width / this.height;
this.renderer._curCamera.defaultEyeX = 0;
this.renderer._curCamera.defaultEyeY = 0;
this.renderer._curCamera.defaultEyeZ = this.height / 2.0 / Math.tan(this.renderer._curCamera.defaultCameraFOV / 2.0);
this.renderer._curCamera.defaultCenterX = 0;
this.renderer._curCamera.defaultCenterY = 0;
this.renderer._curCamera.defaultCenterZ = 0;
this.renderer._curCamera.defaultCameraNear = this.renderer._curCamera.defaultEyeZ * 0.1;
this.renderer._curCamera.defaultCameraFar = this.renderer._curCamera.defaultEyeZ * 10;
this.cameraFOV = this.renderer._curCamera.defaultCameraFOV;
this.aspectRatio = this.renderer._curCamera.defaultAspectRatio;
this.eyeX = this.renderer._curCamera.defaultEyeX;
this.eyeY = this.renderer._curCamera.defaultEyeY;
this.eyeZ = this.renderer._curCamera.defaultEyeZ;
this.centerX = this.renderer._curCamera.defaultCenterX;
this.centerY = this.renderer._curCamera.defaultCenterY;
this.centerZ = this.renderer._curCamera.defaultCenterZ;
this.upX = 0;
this.upY = 1;
this.upZ = 0;
this.cameraNear = this.renderer._curCamera.defaultCameraNear;
this.cameraFar = this.renderer._curCamera.defaultCameraFar;
this.renderer._curCamera.perspective();
this.renderer._curCamera.camera();
this.cameraType = 'default';
}
// Call end once you've done all your render. Super important to do this!
end() {
const gl = this.gl;
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
resetShader();
// Restore original projection matrix
this.renderer._curCamera._computeCameraDefaultSettings();
this.renderer._curCamera._setDefaultCamera();
this.renderer._pInst.pop();
}
getTexture() {
return this.texture;
}
// Copies this framebuffer to another
copyTo(dst) {
const gl = this.gl;
gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer);
gl.bindTexture(gl.TEXTURE_2D, dst.texture.glTex);
gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, dst.width, dst.height, 0);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
// Draw at a given x, y, width, and height.
// You can call without any parameters to draw the fbo at full screen size
draw(x, y, w, h) {
x = x || 0;
y = y || 0;
w = w || width;
h = h || height;
push();
translate(x, y);
scale(1, -1);
texture(this.texture);
plane(w, h);
pop();
}
}