Skip to content

Feature:: Add FBO support + Grid-Displacement-Mouse effect demo #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7bdf5df
feat: init FBO textures test
Robpayot Feb 22, 2025
fbbf37d
feat: test setup fboData
Robpayot Feb 22, 2025
d250f37
fix: clear bindFramebuffer
Robpayot Feb 22, 2025
60f69f8
feat: refacto
Robpayot Feb 22, 2025
07e892e
feat: update texture incr + bind Main buffer after FBO buffer
Robpayot Feb 22, 2025
cb33e0d
feat: set fboProgram uniforms
Robpayot Feb 23, 2025
22023c3
feat: bind FBO texture
Robpayot Feb 23, 2025
0184768
feat: add mouse mvt, main concept set up
Robpayot Feb 23, 2025
6d5870f
feat: test gl.FLOAT texture
Robpayot Feb 23, 2025
3e140b8
feat: use Float texture
Robpayot Feb 23, 2025
561bd37
refacto: drawFBO
Robpayot Feb 23, 2025
29aca49
feat: test mergeEffect fbo program
Robpayot Feb 23, 2025
13490a7
feat: test v_uv
Robpayot Feb 23, 2025
d0b0d74
feat: test fix
Robpayot Feb 23, 2025
9d172a9
fix: test update containerResolution
Robpayot Feb 23, 2025
b1bf396
feat: no need enableVertex on fbo?
Robpayot Feb 23, 2025
db20ae3
feat: display grey texture
Robpayot Feb 23, 2025
30803c5
refacto
Robpayot Feb 23, 2025
bd88c6f
fix: vao render
Robpayot Feb 28, 2025
b23bd1b
Merge branch 'master' into feat/fbo-grid-effect
Robpayot Feb 28, 2025
73d2197
Update index.html
Robpayot Feb 28, 2025
abd00fd
feat: add final effect
Robpayot Mar 1, 2025
2edb06b
feat: add params and GUI
Robpayot Mar 1, 2025
696e8f6
feat: add gridSize system, (reset instance)
Robpayot Mar 1, 2025
7bdb713
feat: clean up
Robpayot Mar 1, 2025
f707dfc
feat: remove old files
Robpayot Mar 1, 2025
e4ad937
refacto aspectRatio
Robpayot Mar 1, 2025
b824bc3
refacto: uniforms renaming
Robpayot Mar 1, 2025
1c13b37
fix: replace u_flowMap, more generic name
Robpayot Mar 5, 2025
f5af392
fix: rename _createFloatTexture
Robpayot Mar 5, 2025
0b319bb
feat: use same attributes as other default effects
Robpayot Mar 5, 2025
44dad02
fix: movement issue (deltaTime)
Robpayot Mar 5, 2025
fe83978
fix: chrome issue
Robpayot Mar 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ const sectionScripts = {
needButton: true,
});
},

section14() {
startDemo('./grid-mouse-displacement.js', {
code: 'code14',
preview: 'preview',
video: 'videos2',
refresh: 'refresh14',
});
},
};

insertSection('section11');
Expand Down
150 changes: 150 additions & 0 deletions demo/grid-mouse-displacement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { Kampos, fbos, effects } from '../index.js';

const GUI = lil.GUI;

const media1 = document.querySelector('#video3');
const target = document.querySelector('#target');

// create the effects/transitions we need
let gridMouseDisplacement;
let flowmapGrid;
let instance;

const guiObj = {
radius: 130,
gridSize: 1000,
relaxation: 0.93,
resetForce: 0.3,
displacementForce: 0.01,
rgbShift: true,
ratio: 'square', // using String here to simplify choices, but in the end it's a Number used for aspectRatio
};

// make sure videos are loaded and playing
prepareVideos([media1]).then(() => {
generateInstance({ aspectRatio: 1 });
});

function generateInstance({ aspectRatio }) {
if (instance) {
instance.destroy();
}

// create the effects/transitions we need
gridMouseDisplacement = effects.gridMouseDisplacement({ aspectRatio });
flowmapGrid = fbos.flowmapGrid({ aspectRatio });

// init kampos
instance = new Kampos({
target,
effects: [gridMouseDisplacement],
fbo: {
size: Math.ceil(Math.sqrt(guiObj.gridSize)),
effects: [flowmapGrid],
},
});

// set media source
const width = media1.videoWidth;
const height = media1.videoHeight;
instance.setSource({ media: media1, width, height });

// start kampos
resizeHandler(target);
instance.play();
}

let rect;
let movement = 1;
let mousePos = [0, 0];
let deltaMouse = [0, 0];
let mouse = {
x: 0,
y: 0,
};

let lastTime = 0;

// this is invoked once in every animation frame, while there's a mouse move over the canvas
function tick(time) {
const deltaTime = (time - lastTime);
lastTime = time;

movement -= (guiObj.resetForce * 0.01 * deltaTime) / 8;
movement = Math.max(0, movement);
// drawing = false;

if (flowmapGrid) {
flowmapGrid.mouse = mousePos;
flowmapGrid.deltaMouse = deltaMouse;
flowmapGrid.movement = movement;
}

requestAnimationFrame(tick);
}

tick(0);

// handler for detecting mouse move
const moveHandler = (e) => {
const { clientX, clientY } = e;

rect = target.getBoundingClientRect();

mouse.x = (clientX - rect.x) / rect.width;
mouse.y = 1 - clientY / rect.height;

deltaMouse = [(mouse.x - mousePos[0]) * 80, (mouse.y - mousePos[1]) * 80];
mousePos = [mouse.x, mouse.y];

movement = 1;

if (flowmapGrid) {
flowmapGrid.containerResolution = [rect.width, rect.height];
}
};

/*
* register event handlers for interaction
*/
target.addEventListener('mousemove', moveHandler);

const resizeHandler = (target) => {
const rect = target.getBoundingClientRect();
flowmapGrid.containerResolution = [rect.width, rect.height];
gridMouseDisplacement.containerResolution = [rect.width, rect.height];
};

window.addEventListener('resize', resizeHandler.bind(null, target));

const setGUI = () => {
const gui = new GUI();

gui.add(guiObj, 'ratio', ['rectangle', 'square']).onChange((value) => {
const ratioUniform = value === 'square' ? 1 : 16 / 9;
flowmapGrid.aspectRatio = ratioUniform;
gridMouseDisplacement.aspectRatio = ratioUniform;
resizeHandler(target);
});
gui.add(guiObj, 'rgbShift').onChange((value) => {
gridMouseDisplacement.rgbShift = value;
});
gui.add(guiObj, 'radius', 1, 300).onChange((value) => {
flowmapGrid.radius = value;
});
gui.add(guiObj, 'gridSize', 100, 8000).onChange((value) => {
// have to recreate whole instance because fboSize needs to change
const ratioUniform = guiObj.ratio === 'square' ? 1 : 16 / 9;
generateInstance({ aspectRatio: ratioUniform });
resizeHandler(target);
});
gui.add(guiObj, 'displacementForce', 0, 0.1).onChange((value) => {
gridMouseDisplacement.displacementForce = value;
});
gui.add(guiObj, 'resetForce', 0.08, 1);
gui.add(guiObj, 'relaxation', 0.8, 0.99).onChange((value) => {
flowmapGrid.relaxation = value;
});
};

setGUI();
16 changes: 16 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,17 @@
</section>
</template>

<template id="section14">
<section id="grid-mouse-displacement">
<div class="code-container">
<button class="refresh-button" id="refresh14">
Refresh
</button>
<textarea id="code14"></textarea>
</div>
</section>
</template>

<nav>
<ul>
<li>
Expand Down Expand Up @@ -434,6 +445,11 @@
>Shape transition</a
>
</li>
<li>
<a href="#section14" data-section-id="section14"
>Grid mouse displacement</a
>
</li>
</ul>
</nav>
<main>
Expand Down
9 changes: 9 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@
needButton: true,
});
},

section14() {
startDemo('./grid-mouse-displacement.js', {
code: 'code14',
preview: 'preview',
video: 'videos2',
refresh: 'refresh14',
});
},
};

insertSection('section11');
Expand Down
Loading