我正在处理与我的学习有关的项目。我想制作具有特定纹理的3d盒子,到目前为止我创建了一个具有不同纹理的盒子。我还想通过使用数组在网格中显示它们,因此代码在下面,无法弄清楚如何将其转换为数组
PS。作为图像我只是简单地创建了6个不同的颜色框来检查它是如何工作的。
Cube myCube;
int x;
void setup(){
size(800,800,P3D);
myCube = new Cube();
}
void draw(){
myCube.display();
}
class Cube{
PImage tex, tex1, tex2, tex3, tex4, tex5;
void display(){
background(0);
noStroke();
translate(100+x,100+x, 0);
rotateY(map(mouseX, 0, width, 0, PI));
rotateX(map(mouseY, 0, height, 0, PI));
scale(50);
tex = loadImage("image0.jpg");
tex1 = loadImage("image1.jpg");
tex2 = loadImage("image2.jpg");
tex3 = loadImage("image3.jpg");
tex4 = loadImage("image4.jpg");
tex5 = loadImage("image5.jpg");
textureMode(NORMAL);
beginShape(QUADS);
texture(tex);
// +Z "front" face
vertex(-1, -1, 1, 0, 0);
vertex( 1, -1, 1, 1, 0);
vertex( 1, 1, 1, 1, 1);
vertex(-1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
// -Z "back" face
texture(tex1);
vertex( 1, -1, -1, 0, 0);
vertex(-1, -1, -1, 1, 0);
vertex(-1, 1, -1, 1, 1);
vertex( 1, 1, -1, 0, 1);
endShape();
beginShape(QUADS);
// +Y "bottom" face
texture(tex2);
vertex(-1, 1, 1, 0, 0);
vertex( 1, 1, 1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
beginShape(QUADS);
// -Y "top" face
texture(tex3);
vertex(-1, -1, -1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, -1, 1, 1, 1);
vertex(-1, -1, 1, 0, 1);
endShape();
beginShape(QUADS);
// +X "right" face
texture(tex4);
vertex( 1, -1, 1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex( 1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
// -X "left" face
texture(tex5);
vertex(-1, -1, -1, 0, 0);
vertex(-1, -1, 1, 1, 0);
vertex(-1, 1, 1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
}
}
以下是凯文帮助后的代码。
ArrayList<Cube> myCubes = new ArrayList<Cube>();
void setup(){
size(800,800,P3D);
frameRate(60);
for(int i = 0; i < 10; i++){
myCubes.add(new Cube());
}
}
void draw(){
for(Cube myCube : myCubes){
myCube.display();
}
}
class Cube{
PImage tex, tex1, tex2, tex3, tex4, tex5;
float x;
float y;
float scale;
public Cube(){
this.x = random(width);
this.y = random(height);
this. scale = random(10, 50);
}
void display(){
background(0);
noStroke();
pushMatrix();
background(0);
noStroke();
translate(x,y, 0);
rotateY(map(mouseX, 0, width, 0, PI));
rotateX(map(mouseY, 0, height, 0, PI));
scale(scale);
tex = loadImage("image0.jpg");
tex1 = loadImage("image1.jpg");
tex2 = loadImage("image2.jpg");
tex3 = loadImage("image3.jpg");
tex4 = loadImage("image4.jpg");
tex5 = loadImage("image5.jpg");
textureMode(NORMAL);
beginShape(QUADS);
texture(tex);
// +Z "front" face
vertex(-1, -1, 1, 0, 0);
vertex( 1, -1, 1, 1, 0);
vertex( 1, 1, 1, 1, 1);
vertex(-1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
// -Z "back" face
texture(tex1);
vertex( 1, -1, -1, 0, 0);
vertex(-1, -1, -1, 1, 0);
vertex(-1, 1, -1, 1, 1);
vertex( 1, 1, -1, 0, 1);
endShape();
beginShape(QUADS);
// +Y "bottom" face
texture(tex2);
vertex(-1, 1, 1, 0, 0);
vertex( 1, 1, 1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
beginShape(QUADS);
// -Y "top" face
texture(tex3);
vertex(-1, -1, -1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, -1, 1, 1, 1);
vertex(-1, -1, 1, 0, 1);
endShape();
beginShape(QUADS);
// +X "right" face
texture(tex4);
vertex( 1, -1, 1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex( 1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
// -X "left" face
texture(tex5);
vertex(-1, -1, -1, 0, 0);
vertex(-1, -1, 1, 1, 0);
vertex(-1, 1, 1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
popMatrix();
}
}
答案 0 :(得分:1)
首先,您必须这样做,以便Cube
课程的每个实例都显示在不同的位置,否则您将无法判断您是否有多个Cubes
。像这样:
class Cube{
PImage tex, tex1, tex2, tex3, tex4, tex5;
float x;
float y;
float scale;
public Cube(){
this.x = random(width);
this.y = random(height);
this. scale = random(10, 50);
}
void display(){
pushMatrix();
noStroke();
translate(x,y, 0);
rotateY(map(mouseX, 0, width, 0, PI));
rotateX(map(mouseY, 0, height, 0, PI));
scale(scale);
//rest of your code
popMatrix();
注意我已添加对pushMatrix()
和popMatrix()
的调用,因此您的翻译和轮播不会叠加。
然后您可以轻松使用数组而不是单个实例:
Cube[] myCubes = new Cube[10];
void setup(){
size(800,800,P3D);
for(int i = 0; i < myCubes.length; i++){
myCubes[i] = new Cube();
}
}
void draw(){
backgrond(0);
for(Cube myCube : myCubes){
myCube.display();
}
}
您还可以使用ArrayList
:
ArrayList<Cube> myCubes = new ArrayList<Cube>();
void setup(){
size(800,800,P3D);
for(int i = 0; i < 10; i++){
myCubes.add(new Cube());
}
}
void draw(){
for(Cube myCube : myCubes){
myCube.display();
}
}
修改:更新代码后,您只能看到一个Cube
,因为您正在重新绘制每个Cube
的背景,因此您最终会在之前绘制Cubes
。将其移至draw()
函数的开头。此外,每次绘制Cube
时都会加载纹理文件,这会导致您的速度变慢。将其移至Cube
构造函数。把它们放在一起:
ArrayList<Cube> myCubes = new ArrayList<Cube>();
void setup() {
size(800, 800, P3D);
frameRate(60);
for (int i = 0; i < 10; i++) {
myCubes.add(new Cube());
}
}
void draw() {
background(0);
for (Cube myCube : myCubes) {
myCube.display();
}
}
class Cube {
PImage tex, tex1, tex2, tex3, tex4, tex5;
float x;
float y;
float scale;
public Cube() {
this.x = random(width);
this.y = random(height);
this. scale = random(10, 50);
tex = loadImage("image0.jpg");
tex1 = loadImage("image1.jpg");
tex2 = loadImage("image2.jpg");
tex3 = loadImage("image3.jpg");
tex4 = loadImage("image4.jpg");
tex5 = loadImage("image5.jpg");
}
void display() {
noStroke();
pushMatrix();
noStroke();
translate(x, y, 0);
rotateY(map(mouseX, 0, width, 0, PI));
rotateX(map(mouseY, 0, height, 0, PI));
scale(scale);
textureMode(NORMAL);
beginShape(QUADS);
texture(tex);
// +Z "front" face
vertex(-1, -1, 1, 0, 0);
vertex( 1, -1, 1, 1, 0);
vertex( 1, 1, 1, 1, 1);
vertex(-1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
// -Z "back" face
texture(tex1);
vertex( 1, -1, -1, 0, 0);
vertex(-1, -1, -1, 1, 0);
vertex(-1, 1, -1, 1, 1);
vertex( 1, 1, -1, 0, 1);
endShape();
beginShape(QUADS);
// +Y "bottom" face
texture(tex2);
vertex(-1, 1, 1, 0, 0);
vertex( 1, 1, 1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
beginShape(QUADS);
// -Y "top" face
texture(tex3);
vertex(-1, -1, -1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, -1, 1, 1, 1);
vertex(-1, -1, 1, 0, 1);
endShape();
beginShape(QUADS);
// +X "right" face
texture(tex4);
vertex( 1, -1, 1, 0, 0);
vertex( 1, -1, -1, 1, 0);
vertex( 1, 1, -1, 1, 1);
vertex( 1, 1, 1, 0, 1);
endShape();
beginShape(QUADS);
// -X "left" face
texture(tex5);
vertex(-1, -1, -1, 0, 0);
vertex(-1, -1, 1, 1, 0);
vertex(-1, 1, 1, 1, 1);
vertex(-1, 1, -1, 0, 1);
endShape();
popMatrix();
}
}