Hey, I’m doing an interactive project and I need it to be as fast as possible without delay or lags.
As part of the project, I have a video (let’s say from the webcam) on part of the canvas, and on top of the video I draw some shapes (more than one) and their textures are also the video from the camera.
I know that the texture()
function has to run in WEBGL mode, so I thought about two ways to implement that:
I can make my whole canvas WEBGL, and the second option is to make the canvas regular P2D and then make a p5.Renderer object
using createGraphics(WEBGL)
and draw the video on it, and in the end, paste the object on the canvas. here are the two ways in code:
First option:
let vid;
function setup() {
createCanvas(800, 800, WEBGL);
vid = createCapture(VIDEO);
vid.hide();
}
function draw() {
background(100);
push();
translate(-width / 2, -height / 2);
image(vid, 200, 200, 400, 400);
pop();
texture(vid);
textureMode(NORMAL);
noStroke();
beginShape();
vertex(0, 0, 0, 0);
vertex(100, -100, 1, 0);
vertex(100, 200, 1, 1);
vertex(0, 100, 0, 1);
endShape();
beginShape();
vertex(-200, -200, 0, 0);
vertex(-100, -200, 1, 0);
vertex(-100, -100, 1, 1);
vertex(-200, -100, 0, 1);
endShape();
}
Second option:
let vid;
function setup() {
createCanvas(800, 800);
pg = createGraphics(800, 800, WEBGL);
vid = createCapture(VIDEO);
vid.hide();
}
function draw() {
background(100);
pg.push();
pg.translate(-width / 2, -height / 2);
pg.image(vid, 0, 0, width, height);
pg.pop();
pg.texture(vid);
pg.textureMode(NORMAL);
pg.noStroke();
pg.beginShape();
pg.vertex(0, 0, 0, 0);
pg.vertex(2 * 100, 2 * -100, 1, 0);
pg.vertex(2 * 100, 2 * 200, 1, 1);
pg.vertex(0, 2 * 100, 0, 1);
pg.endShape();
pg.beginShape();
pg.vertex(2 * -200, 2 * -200, 0, 0);
pg.vertex(2 * -100, 2 * -200, 1, 0);
pg.vertex(2 * -100, 2 * -100, 1, 1);
pg.vertex(2 * -200, 2 * -100, 0, 1);
pg.endShape();
image(pg, 200, 200, 400, 400);
}
My question is which one is more optimal for running time and performance (I assume that there is a difference between WEBGL and P2D)?
Here is an example of the two options in the P5 editor if that’s help:
thanks