所以我想创建一个随机的"多云"处理随时间变化的背景。这是通过随机生成一组像素的亮度来完成的。不幸的是,我只能让它渲染一次然后不再渲染。代码如下
float increment = 0.02;
void setup() {
size(800,200);
noLoop();
}
void draw() {
background(0);
// Optional: adjust noise detail here
// noiseDetail(8,0.65f);
loadPixels();
float xoff = 0.0; // Start xoff at 0
// For every x,y coordinate in a 2D space, calculate a noise value and produce a brightness value
for (int x = 0; x < width; x++) {
xoff += increment; // Increment xoff
float yoff = 0.0; // For every xoff, start yoff at 0
for (int y = 0; y < height; y++) {
yoff += increment; // Increment yoff
// Calculate noise and scale by 255
float bright = noise(xoff,yoff)*255;
// Try using this line instead
//float bright = random(0,255);
// Set each pixel onscreen to a grayscale value
pixels[x+y*width] = color(bright);
}
}
updatePixels();
}
答案 0 :(得分:1)
首先,您正在调用noLoop()
函数,这会阻止Processing每秒调用draw()
函数60次。
但即使你摆脱了这一点,你仍然会在每一帧都看到同样的事情。这是因为您每帧都将完全相同的参数传递到noise()
函数中。
因此,对您的问题的简短回答是,您需要随时更改传递给noise()
函数的参数。你如何做到这一点取决于你想要做什么。您可以将整个事物向某个方向移动,也可以修改内部偏移。你可以做很多不同的事情,但基本的答案是你需要每帧传递不同的值。
展示我所谈论的内容的最快方法是将此行移出draw()
函数并移至草图顶部:
float xoff = 0.0;
现在,将在帧之间保留偏移量,并且您将看到整个事物向上移动。你必须玩它才能获得你想要的确切效果。