Обработка PApplet, загрузка изображения не работает - PullRequest
1 голос
/ 21 апреля 2020

Я использую библиотеку 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);

И вот, изображение отлично загружается ???

да, я делаю клон Minecraft

1 Ответ

0 голосов
/ 22 апреля 2020

Я нашел!

texture() работает только при запуске между функциями preDraw() и postDraw(), но функция box() включает в себя эти шаги, поэтому она не может работать, вы должны создать куб, используя Vertex.
Обработка предлагает нам пример, чтобы сделать его там !

То, что я сделал для настройки этого примера, - это класс 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;
    }
}

И это отлично решает мою проблему, теперь у меня есть куб с текстурами!

...