我用的是 processing.core.PApplet
库的一个简单的Java项目。 我在 setting
函数,我试图在 draw
函数,但奇怪的是纹理没有出现?
这是我用来加载它们的代码。
public void init() throws FileNotFoundException { // this get executed in the 'setting' function of my sketch
Registry.register(getImage("void"), "void");
}
public processing.core.PImage getImage(String name) throws FileNotFoundException {
String path = "src\\main\\resources\\blocks\\textures\\" + name + ".png";
File file = new File(path);
if(file.exists()) {
Logger.info("Texture " + file.getAbsolutePath() + " found.", "TextureMapping");
return sketch.loadImage(path);
} else {
throw new FileNotFoundException("File " + file.getAbsolutePath() + " not found." );
}
}
还有我用来绘制其中一个纹理的代码。
// I create and draw a new cube in the 'draw' function of my sketch
// But it appears without any texture
public Cube(processing.core.PApplet sketch, BlockPos pos, @NotNull Block block) {
this.sketch = sketch;
this.block = block;
position = pos;
texture = Registry.getTextures().get("minecraft:void");
texture.loadPixels();
}
public void draw() {
sketch.pushMatrix();
sketch.translate(position.getX(), position.getY(), position.getZ());
sketch.box(10);
sketch.texture(texture); // Doin' nothing
sketch.popMatrix();
}
文件也在那里,我的 Logger
说它们被找到了,我没有得到任何错误,但纹理却具有PImage的所有属性?
第二件奇怪的事:在 draw
方法,我在 draw
函数。
sketch.image(Registry.getTextures().get("minecraft:void"), 10, 10);
And there, the image loads perfectly ? ?
是的,我正在做一个Minecraft克隆。
我发现
的 texture()
只有当运行在 preDraw()
和 postDraw()
职能,但 box()
函数有这些步骤,所以它不能工作,你必须使用Vertex创建一个立方体。 Processing为我们提供了一个例子,让它 那里 !
我为了定制这个例子而做的是一个叫做 Box
在创建顶点的类中,也可以设置大小,就这样。
public class Box {
private final PApplet sketch;
private final int scale;
public Box(PApplet sketch, int scale) {
this.sketch = sketch;
this.scale = scale;
}
public void generateVertex(PImage texture) {
sketch.scale(scale);
sketch.beginShape(sketch.QUADS);
sketch.texture(texture);
// +Z "front" face
sketch.vertex(-1, -1, 1, 0, 0);
sketch.vertex(1, -1, 1, 1, 0);
sketch.vertex(1, 1, 1, 1, 1);
sketch.vertex(-1, 1, 1, 0, 1);
// -Z "back" face
sketch.vertex(1, -1, -1, 0, 0);
sketch.vertex(-1, -1, -1, 1, 0);
sketch.vertex(-1, 1, -1, 1, 1);
sketch.vertex(1, 1, -1, 0, 1);
// +Y "bottom" face
sketch.vertex(-1, 1, 1, 0, 0);
sketch.vertex(1, 1, 1, 1, 0);
sketch.vertex(1, 1, -1, 1, 1);
sketch.vertex(-1, 1, -1, 0, 1);
// -Y "top" face
sketch.vertex(-1, -1, -1, 0, 0);
sketch.vertex(1, -1, -1, 1, 0);
sketch.vertex(1, -1, 1, 1, 1);
sketch.vertex(-1, -1, 1, 0, 1);
// +X "right" face
sketch.vertex(1, -1, 1, 0, 0);
sketch.vertex(1, -1, -1, 1, 0);
sketch.vertex(1, 1, -1, 1, 1);
sketch.vertex(1, 1, 1, 0, 1);
// -X "left" face
sketch.vertex(-1, -1, -1, 0, 0);
sketch.vertex(-1, -1, 1, 1, 0);
sketch.vertex(-1, 1, 1, 1, 1);
sketch.vertex(-1, 1, -1, 0, 1);
sketch.endShape();
}
public int getScale() {
return scale;
}
}
这完美地解决了我的问题,现在我有了一个带有纹理的立方体!