用画布创建3D立方体 [英] Create 3D cube with Canvas

查看:285
本文介绍了用画布创建3D立方体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2d位图,我想转换成一个3d立方体(例如像在minecraft:)

I have a 2d bitmap that I would like to convert into a 3d cube(example like in minecraft: )

我设法使用相机在3D空间中旋转图像,但我不能理解如何控制它或如何创建一个立方体出来了,有谁有想法?请注意,我只允许使用画布和没有OpenGL。

I managed to rotate around images in 3d space using the "Camera" but I can't understand how to control it or how to create a cube out of it, anyone has ideas? Please notice i'm allowed only to use canvas and no OpenGL.

编辑:
这是接近:

this is as close as I got:

使用此代码:

        Matrix mMatrix = canvas.getMatrix();
        canvas.save();
        Camera camera=new Camera();
        camera.save();
        camera.rotateY(30);
        camera.getMatrix(mMatrix);
        mMatrix.preTranslate(-30, 0);
        mMatrix.postTranslate(30, 0);
        canvas.concat(mMatrix);
        canvas.drawBitmap(b, 150, 150, null);
        canvas.drawBitmap(b, 180, 180, null);
        camera.restore();
        canvas.restore();
        canvas.save();


推荐答案

好吧,因为没有帮助我寻找另一个做它的方式和想法的工作,它的工作的解决方法,它的效率不高,可能会减慢程序,同时做它,所以三思而后行使用它。

Okay, so because none helped me I looked for another way of doing it and thought of a workaround way that works, its not efficient and it might slow the program down while doing it, so think twice before using it.

我这样做的方式是这样的:

The way I did it is like that:

首先,我创建了一个多维数据集可以是任何3D形状)

然后,我剪了立方体并单独保存它们。

First of all, I created a cube in paint (could be any 3D shape) Then, I cut the cube sides and saved them separately.

加载这些图像全部是代码:

After loading those images all is left is the code:

    public Bitmap CreateACube(Bitmap b2D){
    Bitmap result=Bitmap.createBitmap(b2D);
    /*loading sides of cube and painting texture on them*/
    Bitmap top;
    Bitmap left;
    Bitmap front;
    top=BitmapFactory.decodeResource(getResources(), R.drawable.top);
    top=CubeCreator(top, b2D,"top");
    left=BitmapFactory.decodeResource(getResources(), R.drawable.left);
    left=CubeCreator(left, b2D,"left");
    front=BitmapFactory.decodeResource(getResources(), R.drawable.front);
    front=CubeCreator(front, b2D,"front");
    Bitmap merge;
    merge=overlay(top, left);//connecting all cube sides together into one bitmap
    merge=overlay(merge, front);
    result=Bitmap.createScaledBitmap(merge, merge.getWidth()*2, merge.getHeight()*2, false); //Scaling the result, you can remove if you don't want to.
    return result;
}
private Bitmap CubeCreator(Bitmap srcBmp,Bitmap b2D,String s){
/*gets cube side bitmap, the texture bitmap, and a string of the side name*/
      int width = srcBmp.getWidth();
        int height = srcBmp.getHeight();
        int width2=b2D.getWidth();
        int height2=b2D.getHeight();
        int rows1=0;
        int rows2=0;
        Bitmap dstBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        /*Running on every pixel in the cube side*/
        for (int row = 0; row < height; row++) {
            rows1=++rows1%height2; //running on every pixel on the texture and reseting it if reached the last pixel
            for (int col = 0; col < width; col++) {
                 rows2=++rows2%width2;
                int pixel = srcBmp.getPixel(col, row);
                int alpha = Color.alpha(pixel);
                int dstColor=b2D.getPixel(rows2, rows1);
                switch(s){//you can add more sides, I used 3
                case "front":{
                    float[] hsv = new float[3];
                    int color = dstColor;
                    Color.colorToHSV(color, hsv);
                    hsv[2] *= 0.8f; // giving brightness to certain sides to make it look more 3D
                    dstColor = Color.HSVToColor(hsv);
                break;
                }
                case "left":{
                    float[] hsv = new float[3];
                    int color = dstColor;
                    Color.colorToHSV(color, hsv);
                    hsv[2] *= 0.44f; 
                    dstColor = Color.HSVToColor(hsv);
                    break;
                }
                case "top":{
                    float[] hsv = new float[3];
                    int color = dstColor;
                    Color.colorToHSV(color, hsv);
                    hsv[2] *= 1.2f; 
                    dstColor = Color.HSVToColor(hsv);
                    break;
                }

                }
                int pixel2=srcBmp.getPixel(col, row);
                if(pixel2!= Color.TRANSPARENT)//checking if the current pixel of the side is not transparent
                dstBitmap.setPixel(col, row, dstColor);
                else{
                    dstBitmap.setPixel(col, row, pixel2);
                }
            }
        }

        return dstBitmap;
}
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
    /*connects two bitmaps together*/
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, 0, 0, null);
    return bmOverlay;
}

代码摘要 (前,左,上)的像素,同时我也运行纹理位图中的每个像素,并用纹理的像素为侧面像素着色。之后,我将所有彩色面连接成一个位图并返回。

Summary of the code: I run on every pixel of the cube side (front, left, top) and while doing so I also run on every pixel in the texture bitmap, and color the side pixels with the pixels of the texture. After it I connect all colored sides together into one bitmap and return them.

结果:

这篇关于用画布创建3D立方体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆