java - 使用 Canvas 创建 3D 立方体

标签 java android canvas bitmap

我有一个 2D 位图,我想将其转换为 3D 立方体(例如《我的世界》中的示例: enter image description here )

我设法使用“相机”在 3D 空间中旋转图像,但我不明白如何控制它或如何用它创建一个立方体,有人有想法吗?请注意,我只能使用 Canvas ,不能使用 OpenGL。

编辑: 这是我得到的最接近的结果: enter image description here

使用此代码:

        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();

最佳答案

好吧,因为没有人帮助我,所以我寻找另一种方法来做到这一点,并想到了一种可行的解决方法,它效率不高,而且可能会减慢程序的运行速度,所以在使用它之前请三思。

我的做法是这样的:

首先,我用油漆创建了一个立方体(可以是任何 3D 形状) enter image description here 然后,我切掉立方体的边并将它们分开保存。 enter image description here

加载这些图像后,剩下的就是代码:

    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;
}

代码摘要:我在立方体侧面(前部、左侧、顶部)的每个像素上运行,同时在纹理位图中的每个像素上运行,并对侧面进行着色像素与纹理的像素。之后,我将所有彩色面连接在一起形成一个位图并返回它们。

结果: enter image description here

关于java - 使用 Canvas 创建 3D 立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28050435/

相关文章:

java - 读取 APK 中的文件

java - 在并行流中获取安全上下文时主体为空

java - android studio mergedebugresources java.lang.ArrayIndexOutOfBoundsException(无错误信息)

android - 约束布局 0dp 无法正确呈现

image - 有什么办法可以拯救脏 Canvas ?

java - Spring Boot Web 应用程序中的嵌入式 Tomcat 不以 module-info 启动

java - CDI 是否为 RequestScoped 重用代理?

java - 使用 SharedPreferences 的 ClassCastException

wpf - 如何删除 WPF Canvas 子项附加的顶部/底部/左/右属性?

javascript - 如何检索渲染的 Canvas 元素的坐标 (x,y,w,h)?