简体   繁体   English

使用triangle_strip模式处理形状的纹理

[英]processing texture for shape with triangle_strip mode

I want to construct TRIANGLE_STRIP shape with different texture in each sector. 我想在每个扇区中构造具有不同纹理的TRIANGLE_STRIP形状。 Is it possible to use TRIANGLE_STRIP shape in this case? 在这种情况下可以使用TRIANGLE_STRIP形状吗? I can't understand how to set horizontal and vertical coordinate for the texture mapping in this shape mode because each triangle shares vertex points with each other and because of that I can set horizontal and vertical coordinate for the texture mapping only for one image. 我无法理解如何在此形状模式下为纹理映射设置水平和垂直坐标,因为每个三角形彼此共享顶点,因此我只能为一个图像设置纹理映射的水平和垂直坐标。

PImage img1, img2, img3, img4, img5;
void setup() {
  size(800, 300, P3D);
  img1 = loadImage("img1.png");
  img2 = loadImage("img2.png");
  ....
  textureMode(NORMAL);
 // textureWrap(REPEAT);
}
void draw(){
  background(0);
  stroke(255);
   beginShape(TRIANGLE_STRIP);
   texture(img1);
    vertex(30, 286,0,1);
    vertex(158, 30, 0.5, 0);
    vertex(286, 286,1,1);
    texture(img2);
    vertex(414, 30, ?, ?);
    texture(img3);
    vertex(542, 286, ?, ?);
    texture(img4);
    vertex(670, 30,?,?);
    texture(img4);
    vertex(798, 286,?,?);
    endShape();
}

UPD UPD

I wand to acheive the result similar to that animation: https://mir-s3-cdn-cf.behance.net/project_modules/disp/7d7bf511219015.560f42336f0bd.gif 我想获得类似于该动画的结果: https : //mir-s3-cdn-cf.behance.net/project_modules/disp/7d7bf511219015.560f42336f0bd.gif

First of all I want to construct complicated object based on TRIANGLE_STRIP or QUAD_STRIP shape mode and after that just change z coordinate of vertexes. 首先,我想基于TRIANGLE_STRIP或QUAD_STRIP形状模式构造复杂的对象,然后仅更改顶点的z坐标。

So I just took image with such inscription and cut it on different images for each sector of shape. 因此,我只是用这样的铭文拍摄了图像,并针对每个形状的扇形将其切成不同的图像。

If someone knows how to make it in more easy way I will be very thankful for help. 如果有人知道如何以更简单的方式做到这一点,我将非常感谢您的帮助。

全图

扇形

Step 1: Create a small sketch that simply displays a triangle strip (without any texturing) over the entire space you want to take up. 第1步:创建一个小的草图,该草图仅在要占用的整个空间上显示一个三角形带(没有任何纹理)。 Here's an example that fills the whole screen: 这是一个显示整个屏幕的示例:

void setup() {
  size(300, 200);
}
void draw() {
  background(0);
  stroke(0);
  beginShape(TRIANGLE_STRIP);

  vertex(0, 200);
  vertex(0, 0);
  vertex(50, 200);
  vertex(100, 0);
  vertex(150, 200);
  vertex(200, 0);
  vertex(250, 200);
  vertex(300, 0);
  vertex(300, 200);

  endShape();
}

The goal is to make sure your vertexes cover the area you want your image to cover. 目的是确保您的顶点覆盖您要覆盖图像的区域。 You want something that looks like this: 您想要看起来像这样的东西:

This will also make it easier to map the vertex coordinates to image texture coordinates. 这也将使将顶点坐标映射到图像纹理坐标更加容易。

Step 2: Create an image that you want to use as a texture. 第2步:创建要用作纹理的图像。 I'll use this one: 我将使用这个:

Step 3: For each vertex you're drawing on the screen, figure out where in the image that point is. 步骤3:对于要在屏幕上绘制的每个顶点,请找出该点在图像中的位置。 If a point is in the middle of the screen, then you need to figure out the position of the middle of the image. 如果一个点位于屏幕中间,则需要找出图像中间的位置。 That's your values for u and v . 这就是您对uv值。

Alternatively, you can use textureMode(NORMAL) so you can specify u and v as normalized values between 0 and 1 . 另外,您可以使用textureMode(NORMAL)以便可以将uv指定为01之间的归一化值。 The middle of the image becomes point (.5, .5) . 图像的中间变为点(.5, .5)

Which approach you take is up to you, but in either case you have to map the screen vertex positions to the image u, v positions. 哪种方法取决于您,但是无论哪种情况,您都必须将屏幕顶点位置映射到图像的u, v位置。 I'll use the normalized values here: 我将在此处使用归一化的值:

PImage img;

void setup() {
  size(300, 200, P3D);
  img = loadImage("test.png");
  textureMode(NORMAL);
}
void draw() {
  background(0);
  stroke(0);
  beginShape(TRIANGLE_STRIP);

  texture(img);
  vertex(0, 200, 0, 1);
  vertex(0, 0, 0, 0);
  vertex(50, 200, .16, 1);
  vertex(100, 0, .33, 0);
  vertex(150, 200, .5, 1);
  vertex(200, 0, .66, 0);
  vertex(250, 200, .83, 1);
  vertex(300, 0, 1, 0);
  vertex(300, 200, 1, 1);

  endShape();
}

Step 4: Now you can modify the position of one of the vertexes, and you'll morph the image drawn on screen: 步骤4:现在,您可以修改其中一个顶点的位置,并且可以变形在屏幕上绘制的图像:

PImage img;

void setup() {
  size(300, 200, P3D);
  img = loadImage("test.png");
  textureMode(NORMAL);
}
void draw() {
  background(0);
  stroke(0);
  beginShape(TRIANGLE_STRIP);

  texture(img);
  vertex(0, 200, 0, 1);
  vertex(0, 0, 0, 0);
  vertex(50, 200, .16, 1);
  vertex(100, 0, .33, 0);
  vertex(mouseX, mouseY, .5, 1);
  vertex(200, 0, .66, 0);
  vertex(250, 200, .83, 1);
  vertex(300, 0, 1, 0);
  vertex(300, 200, 1, 1);

  endShape();
}

You can play around with it to get the exact effect you're looking for, but following these steps should be your general approach. 您可以尝试使用它以获得所需的确切效果,但是遵循这些步骤应该是您的一般方法。 Note that you only need to use a single image, and you need to figure out the u and v values for every vertex you draw on screen. 请注意,您只需要使用一个图像,并且需要计算屏幕上绘制的每个顶点的uv值。 Start with a triangle mesh that displays the image normally, and then modify that to morph your image. 从可正常显示图像的三角形网格开始,然后对其进行修改以使图像变形。

Also note that I could have done a lot of this calculation programatically. 另请注意,我可以通过编程方式完成很多此类计算。 For example, instead of hard-coding the value 150 , I could have used width/2.0 . 例如,我可以使用width/2.0而不是对值150进行硬编码。 But first you need to understand the relationship between the x,y on screen and the u,v in the texture. 但是首先,您需要了解屏幕上的x,y和纹理中的u,v之间的关系。 Once you understand that relationship, you can calculate them programatically if you want. 一旦了解了这种关系,就可以根据需要以编程方式计算它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM