我在一个简单的Java中使用processing.core.PApplet库。
我在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);在那里,图像加载得很完美?
是的,我在做一个“我的克隆人”
发布于 2020-04-22 13:33:42
我找到了!
texture()只有在preDraw()函数和postDraw()函数之间运行时才能工作,但是box()函数有这些步骤,所以它不能工作,您必须使用顶点创建一个多维数据集。
处理为我们提供了一个使其成为那里的例子!
我为自定义这个示例所做的是一个创建顶点的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;
}
}这完全解决了我的问题,现在我有一个立方体的纹理!
https://stackoverflow.com/questions/61349128
复制相似问题