Feature:: Add FBO support + Grid-Displacement-Mouse effect demo#74
Feature:: Add FBO support + Grid-Displacement-Mouse effect demo#74Robpayot wants to merge 33 commits into
Conversation
| * @return {{gl: WebGLRenderingContext, data: kamposSceneData, [dimensions]: {width: number, height: number}}} | ||
| */ | ||
| export function init({ gl, plane, effects, dimensions, noSource }) { | ||
| export function init({ gl, plane, effects, dimensions, noSource, fbo }) { |
There was a problem hiding this comment.
Interesting, my initial thought was to allow a programs property to allow providing multiple programs instead of effects. Do you this that would make sense? Or just be an overkill?
There was a problem hiding this comment.
I see what you mean but the fbo programs needs to be rendered in a specific order to draw / clear framebuffers and textures, and then "classic" effects can be drawn. So I think if we put all the logics in the same effets array, it might be difficult/overkilled to sorts them or do the checks on these rather than a separated object right?
There was a problem hiding this comment.
Not same effects array. I meant you could specify something like programs: { effects, plane }[]. But I can do this generalization later.
|
|
||
| if (vao) { | ||
| extensions.vao.bindVertexArrayOES(vao); | ||
| if (fboData) _enableVertexAttributes(gl, attributes); |
There was a problem hiding this comment.
If we're using VAO here why do we need to reenable attributes here?
There was a problem hiding this comment.
For some reason I need to enable the positions here otherwise the program is invisible (only with FBO program rendered before). It might be because after clearing the framebuffer it needs to enable the other program's attributes?
But let me know if you find a solution to make it work only using the vao.
There was a problem hiding this comment.
ok, cool, don't get stuck on it, I'll try investigating later
| // bind fbo texture | ||
| gl.activeTexture(startTex); | ||
| gl.bindTexture(gl.TEXTURE_2D, fboData.oldInfo.tex); | ||
| gl.uniform1i(gl.getUniformLocation(program, 'u_flowMap'), 0); |
There was a problem hiding this comment.
This is very specific to the FlowMap effect. Either we use some generic uniform name here, or we extract this logic to be some how connected to the effect using the FBO?
I think I rather have the former
There was a problem hiding this comment.
Yes, like I said here, I think for now the FBO is moslty meant to be for flowMaps. BUT I agree to make it more generic. I replaced it by u_FBOMap
Notes: For now this should be only used for flowmap I think, other FBO like GPGPU effects can be more complex and needs more textures: Example here (tutorial) where it's using a velocityMap to move particles
|
|
||
| // // Set uniforms | ||
| _setUniforms(gl, uniforms); | ||
| gl.uniform1i(gl.getUniformLocation(program, 'u_flowMap'), 0); |
| return TEXTURE_WRAP[key] || TEXTURE_WRAP['stretch']; | ||
| } | ||
|
|
||
| function createFloatTexture(gl, data, width, height) { |
There was a problem hiding this comment.
Not API, internal:
| function createFloatTexture(gl, data, width, height) { | |
| function _createFloatTexture(gl, data, width, height) { |
| gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); | ||
| gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
There was a problem hiding this comment.
Did you use NEAREST because of the need to make the hard cut for the grid effect specifically?
Perhaps this should (at least later if not now) should be configurable?
There was a problem hiding this comment.
Yes, it was for that. I think like we discussed on Google Chat, in the future, it would be nice to make the FBO texture configurable in parameters like (textureType = FLOAT, textureCut = 'LINEAR' or 'NEAREST') ...
| gl.bindBuffer(gl.ARRAY_BUFFER, buffer); | ||
| gl.bufferData( | ||
| gl.ARRAY_BUFFER, | ||
| new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), |
There was a problem hiding this comment.
I guess this is the coords for clip space? Could we use same ones used for the main program? I mean not the buffer/array itself, but perhaps the same config?
There was a problem hiding this comment.
Updated so now it's similar to the default attributes like for other effects
FBOs
To have a FBO effect you need at least one program running in parallel of the "Kampos effect". I preferred to manage the FBO data in a separate object called
fbo.Example when creating a Kampos effect that needs a FBO map:
Your fbo effect can be different, In the example I made one for a flowMap grid effect in
src/fbo/flowmap-grid.js. But we could add another one for a more simple flowmap effect insrc/fbo/flowmap.jsand do something like this exampleNotes: For now this should be only used for flowmap I think, other FBO like GPGPU effects can be more complex and needs more textures: Example here (tutorial) where it's using a velocityMap to move particles
Modifcation in core.js
If using a FBO when creating a Kampos() instance. the core.js js will create a FBO program
_initFBOProgram(gl, plane, fbo)in a object that gather all important information:fboData.Then if
fboDataexists, the draw function will calldrawFBOwhere all the FBO drawing logic exists. Notice that they are other thing to take care of iffboDataexists like binding textures that I couldn't put in drawFBO for order reason.Notes: For the grid effect I had to use a
gl.FLOATtype texture, so I created a function for that. But I think in the future we might want a default Texture in a FBO effect. In that case we could add a parameters saytextureTypeand use default or float texture based on that.Demo
The demo is using
grid-mouse-displacement.jseffect with different parameters. Some of them likeaspectRationeeds to be updated both in the effect and the fboEffect programs.grid-effect.mp4