加工成型

Making a shape in processing

我正在制作有关处理的动画。然后,我对代码有疑问。通常,我的代码比较长。但是,我制作了一个简单的代码,对初学者也很有用。我的示例代码:

 void setup() {
  size(250, 250);
}

void draw() {
  background(102);
  translate(100, 100);
  beginShape();
  vertex(0,0);
  vertex(-50, 50);
  vertex(0, 100);
  endShape();
}

我必须使用三个顶点,并且我想像 texture() 中那样对形状进行纹理处理。怎么做到的?

为了将图像作为纹理放在形状上:

  1. 使用 P2D 或 P3D 渲染,而不是默认渲染。
  2. 加载要为您的形状添加纹理的图像。
  3. 开始设置形状时调用texture()函数。
  4. 为纹理图像建立 mode (IMAGE) 和 wrap (CLAMP)。
  5. 选择图像的三个点作为三角形的顶点。例如 (0, 0)(0, img.height)(img.width, img.height).
PImage img;

void setup() {
  size(250, 250, P2D);
  img = loadImage("rectangular_image.jpg");
  textureMode(IMAGE);
  textureWrap(CLAMP);
}

void draw() {
  background(102);
  translate(100, 100);
  noStroke();
  beginShape();
  texture(img);
  vertex(0, 0, 0, 0);
  vertex(-50, 50, 0, img.height);
  vertex(0, 100, img.width, img.height);
  endShape();
}