简体   繁体   中英

Stretch 2D plane to 3D cube

I'm working on a Java game that has both a 2D game panel and "pseudo"-3D one.

It isn't real 3D as it's merely some 2D planes put in a 3D environment (no custom created models).

I'm using jPCT as render engine and I'm currently looking into getting the walls rendered.

In the 2D view, they look like this:
在此处输入图片说明

In the 3D view, I'm trying to get them to look like this:
在此处输入图片说明

This works by stacking 10 planes on top of each other and that gives the illusion of a 3D wall.

The code to get this is:

    Object3D obj = new Object3D(20);

    for (int y=0; y < 10; y++) {
        float fY = y / -10f;
        obj.addTriangle(new SimpleVector(-1, fY, 1), 0, 0,
                        new SimpleVector(-1, fY, -1), 0, 1,
                        new SimpleVector(1, fY, -1), 1, 1,
                        textureManager.getTextureID(topTexture));
        obj.addTriangle(new SimpleVector(1, fY, -1), 1, 1,
                        new SimpleVector(1, fY, 1), 1, 0,
                        new SimpleVector(-1, fY, 1), 0, 0,
                        textureManager.getTextureID(topTexture));
    }

Problem is that when looking straight at it, you get this effect:
http://i.stack.imgur.com/s8hyX.png

This could be reduced by increasing the amount of planes and putting them closer together, but I'm looking for a more efficient way of getting the same effect.

I was thinking of rendering a cube with the 2D image as top texture and using the last line of pixels as textures for the sides, eg extract a 40x1 image at (0,0) and (0,39) from the base image and stretch these across the sides of the cubes (the original images are 40x40).

This won't work perfectly though because the visible part of these images are smaller than 40x40 (eg the top and bottom 40x9 pixels are transparent for a horizontal wall), so I should do some edge-detection and start cutting there.

Any better suggestions to try to do same?

最简单但可能效果最差的解决方案是针对图像中同时与透明像素和非透明像素相邻的任何像素,为该像素渲染一个高矩形立方体。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM