【问题标题】:Processing PApplet loading image not working处理 PApplet 加载图像不起作用
【发布时间】:2020-08-04 12:42:41
【问题描述】:

我在一个简单的 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);

然后,图像加载完美???

是的,我正在做一个 Minecraft 克隆

【问题讨论】:

    标签: java processing textures loadimage


    【解决方案1】:

    我找到了!

    texture() 仅在preDraw()postDraw() 函数之间运行时有效,但box() 函数中有这些步骤,所以它不能工作,你必须使用顶点创建一个立方体。
    Processing 为我们提供了一个例子,使它成为 there

    我为这个例子做的自定义是一个创建顶点的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;
        }
    }
    

    这完美地解决了我的问题,现在我有了一个带有纹理的立方体!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多