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开发者_StackOverflow社区 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:
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, e.g. 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 (e.g. 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 simplest but likely least performant solution is for any pixel in your image that is next to both a transparent pixel and a non transparent pixel, render a tall rectangular cube for just that pixel.
精彩评论