PGraphics 相机定位只发生在下一帧
PGraphics camera positioning only happens in the next frame
From this Github issue:
计算机规格:Mac OS Sierra 10.12.3,Processing 3.2.3
在 Processing PGraphics 相机中使用动态值时,这些值只会应用于 下一 帧。我无法将当前帧保存到一个文件中,这个偏移量不是问题。这是预期的行为吗?
考虑以下代码:
- 它将显示一个旋转立方体、一个红色旋转正方形和当前帧数。
- 有一个
x_up
全局变量控制相机中的该值(默认 0.0)。
- 如果
frameCount % 90 == 0
:
- 更改
x_up
(从 0.0 到 1.0)。
- 将填充更改为透明蓝色。
- 保存一个文件
"output/#####_" + x_up + "_.png"
(例如:00090_1.0_.png
)
- 如果
frameCount % 90 == 1
:
- 以相同的约定保存另一个文件,无填充,无
x_up
更改(例如:00091_0.0_.png
)
PGraphics pg;
PMatrix mat_scene;
float x_up;
void setup() {
size(600, 600, P3D);
pg = createGraphics(width, height, P3D);
mat_scene = getMatrix();
}
void draw() {
pg.beginDraw();
pg.hint(DISABLE_DEPTH_TEST);
pg.background(200);
pg.noFill();
// change stuff if frame % 90
if (frameCount % 90 == 0) {
x_up = 1.0;
pg.fill(0, 0, 255, 10);
} else {
x_up = 0.0;
}
// the red rect
pg.pushMatrix();
pg.setMatrix(mat_scene);
pg.stroke(255, 0, 0);
pg.rectMode(CENTER);
pg.translate(width * .5, height * .5, -600);
pg.rotateZ(radians(float(frameCount)));
pg.rect(0, 0, 600, 600);
pg.popMatrix();
// the cube
pg.pushMatrix();
pg.stroke(128);
pg.translate(10, 100, -200);
pg.rotateZ(radians(float(frameCount)));
pg.box(300);
pg.popMatrix();
// the camera
pg.beginCamera();
pg.camera(width, height, -height, 0, 0, 0, x_up, 0.0, 1.0);
pg.endCamera();
// the frame counter
pg.pushMatrix();
pg.fill(255);
pg.setMatrix(mat_scene);
pg.textSize(20);
pg.text(frameCount, 20, 30);
pg.popMatrix();
pg.endDraw();
image(pg, 0, 0);
if (frameCount > 10 && frameCount % 90 == 0) {
saveFrame("output/#####_" + x_up + "_.png");
}
if (frameCount > 10 && frameCount % 90 == 1) {
saveFrame("output/#####_" + x_up + "_.png");
}
}
您可以看到每 90 帧出现一次“光点”。如果您查看输出文件夹,您会在第 90 帧中看到类似这样的内容:
第 91 帧中的类似内容:
请注意,您可以看出它是只有相机,因为两个属性(蓝色和相机x_up)在第 90 帧中都发生了变化,但只有第 91 帧显示了变化在相机中。第 90 帧正确显示了两个框中的蓝色填充。即使我将帧速率设置为 1,也会发生这种情况。如果我使用 pg.save
而不是 saveFrame
,也会发生这种情况。
这是一个错误吗?我可能遗漏了一些明显的东西,但我不是 3D 转换或相机方面的专家。
您在完成所有绘图后调用camera()
函数。所以每一帧,你这样做:
- 移动场景中的对象并拍照。
- 现在移动相机。
所以在第 90 帧上,您绘制场景,然后移动相机。所以在第 91 帧,相机使用的是最后一帧的位置。
要解决此问题,只需将对 camera()
的调用移动到 之前 绘制所有内容(但在设置 x_up
变量之后。
From this Github issue:
计算机规格:Mac OS Sierra 10.12.3,Processing 3.2.3
在 Processing PGraphics 相机中使用动态值时,这些值只会应用于 下一 帧。我无法将当前帧保存到一个文件中,这个偏移量不是问题。这是预期的行为吗?
考虑以下代码:
- 它将显示一个旋转立方体、一个红色旋转正方形和当前帧数。
- 有一个
x_up
全局变量控制相机中的该值(默认 0.0)。 - 如果
frameCount % 90 == 0
:- 更改
x_up
(从 0.0 到 1.0)。 - 将填充更改为透明蓝色。
- 保存一个文件
"output/#####_" + x_up + "_.png"
(例如:00090_1.0_.png
)
- 更改
- 如果
frameCount % 90 == 1
:- 以相同的约定保存另一个文件,无填充,无
x_up
更改(例如:00091_0.0_.png
)
- 以相同的约定保存另一个文件,无填充,无
PGraphics pg;
PMatrix mat_scene;
float x_up;
void setup() {
size(600, 600, P3D);
pg = createGraphics(width, height, P3D);
mat_scene = getMatrix();
}
void draw() {
pg.beginDraw();
pg.hint(DISABLE_DEPTH_TEST);
pg.background(200);
pg.noFill();
// change stuff if frame % 90
if (frameCount % 90 == 0) {
x_up = 1.0;
pg.fill(0, 0, 255, 10);
} else {
x_up = 0.0;
}
// the red rect
pg.pushMatrix();
pg.setMatrix(mat_scene);
pg.stroke(255, 0, 0);
pg.rectMode(CENTER);
pg.translate(width * .5, height * .5, -600);
pg.rotateZ(radians(float(frameCount)));
pg.rect(0, 0, 600, 600);
pg.popMatrix();
// the cube
pg.pushMatrix();
pg.stroke(128);
pg.translate(10, 100, -200);
pg.rotateZ(radians(float(frameCount)));
pg.box(300);
pg.popMatrix();
// the camera
pg.beginCamera();
pg.camera(width, height, -height, 0, 0, 0, x_up, 0.0, 1.0);
pg.endCamera();
// the frame counter
pg.pushMatrix();
pg.fill(255);
pg.setMatrix(mat_scene);
pg.textSize(20);
pg.text(frameCount, 20, 30);
pg.popMatrix();
pg.endDraw();
image(pg, 0, 0);
if (frameCount > 10 && frameCount % 90 == 0) {
saveFrame("output/#####_" + x_up + "_.png");
}
if (frameCount > 10 && frameCount % 90 == 1) {
saveFrame("output/#####_" + x_up + "_.png");
}
}
您可以看到每 90 帧出现一次“光点”。如果您查看输出文件夹,您会在第 90 帧中看到类似这样的内容:
第 91 帧中的类似内容:
请注意,您可以看出它是只有相机,因为两个属性(蓝色和相机x_up)在第 90 帧中都发生了变化,但只有第 91 帧显示了变化在相机中。第 90 帧正确显示了两个框中的蓝色填充。即使我将帧速率设置为 1,也会发生这种情况。如果我使用 pg.save
而不是 saveFrame
,也会发生这种情况。
这是一个错误吗?我可能遗漏了一些明显的东西,但我不是 3D 转换或相机方面的专家。
您在完成所有绘图后调用camera()
函数。所以每一帧,你这样做:
- 移动场景中的对象并拍照。
- 现在移动相机。
所以在第 90 帧上,您绘制场景,然后移动相机。所以在第 91 帧,相机使用的是最后一帧的位置。
要解决此问题,只需将对 camera()
的调用移动到 之前 绘制所有内容(但在设置 x_up
变量之后。