创建3D立方体帆布 [英] Create 3D cube with Canvas
问题描述
我有一个2D位图,我想转换成3D立方体(例如像我的世界:)
我设法绕使用相机在三维空间中的图像,但我不明白如何控制它或如何创建一个立方体出来,任何人有想法?请注意我只允许使用帆布和OpenGL的无
编辑:
这是接近我得到:
这code:
矩阵mMatrix = canvas.getMatrix();
canvas.save();
摄像头摄像头=新相机();
camera.save();
camera.rotateY(30);
camera.getMatrix(mMatrix);
。mMatrix preTranslate(-30,0);
mMatrix.postTranslate(30,0);
canvas.concat(mMatrix);
canvas.drawBitmap(B,150,150,空);
canvas.drawBitmap(B,180,180,空);
camera.restore();
canvas.restore();
canvas.save();
好了,因为没有帮我,我找了做它和一个可行的解决办法的方式思想的另一种方式,它的效率不高,这可能会降低程序下来,而这样做,所以在使用它之前三思而后行。
我做它的方式是这样的:
首先,我创建了一个油漆立方体(可以是任何3D形状)
然后,我切立方体两侧,分别保存它们。
加载所有那些剩下的图像之后是code:
公共位图CreateACube(位图B2D){
位图的结果= Bitmap.createBitmap(B2D);
立方体纹理绘画对他们的/ *装载面* /
位图上方;
位图左;
位图前;
TOP = BitmapFactory.de codeResource(getResources(),R.drawable.top);
顶= CubeCreator(顶部,B2D,顶);
左= BitmapFactory.de codeResource(getResources(),R.drawable.left);
左= CubeCreator(左,B2D,左);
前面= BitmapFactory.de codeResource(getResources(),R.drawable.front);
前面= CubeCreator(前,B2D,前);
位图合并;
合并=覆盖(上左); //所有立方体双方连接在一起成一个位图
合并=叠加(合并前);
结果= Bitmap.createScaledBitmap(合并,merge.getWidth()* 2,merge.getHeight()* 2,假); //缩放的结果,您可以删除,如果你不想。
返回结果;
}
私人位图CubeCreator(位图srcBmp,位图B2D,一个String){
/ *获得立方体侧的位图,所述纹理位图,和侧名的字符串* /
INT宽度= srcBmp.getWidth();
INT高度= srcBmp.getHeight();
INT宽度2 = b2D.getWidth();
INT身高2 = b2D.getHeight();
INT rows1 = 0;
INT rows2 = 0;
位图dstBitmap = Bitmap.createBitmap(宽度,高度,Config.ARGB_8888);
/ *运行在立方体侧的每个像素* /
对于(INT行= 0;&行LT;高度;排++){
rows1 = ++ rows1%身高2; //对纹理的每个像素运行,CH375复位,如果达到了最后一个像素
对于(INT COL = 0;&山坳LT;宽度;山坳++){
rows2 = ++ rows2%宽2;
INT像素= srcBmp.getPixel(COL,行);
INT阿尔法= Color.alpha(像素);
INT dstColor = b2D.getPixel(rows2,rows1);
开关(S){//你可以添加更多的双方,我用3
案前线:{
浮动[] = HSV新的浮动[3];
INT颜色= dstColor;
Color.colorToHSV(颜色,HSV);
HSV [2] * = 0.8f; //给亮度某些双方让它看起来更加的3D
dstColor = Color.HSVToColor(HSV);
打破;
}
案左:{
浮动[] = HSV新的浮动[3];
INT颜色= dstColor;
Color.colorToHSV(颜色,HSV);
HSV [2] * = 0.44f;
dstColor = Color.HSVToColor(HSV);
打破;
}
案顶:{
浮动[] = HSV新的浮动[3];
INT颜色= dstColor;
Color.colorToHSV(颜色,HSV);
HSV [2] * = 1.2F;
dstColor = Color.HSVToColor(HSV);
打破;
} }
INT pixel2 = srcBmp.getPixel(COL,行);
如果(pixel2!= Color.TRANSPARENT)//检查是否侧的当前像素不是透明
dstBitmap.setPixel(COL,行,dstColor);
其他{
dstBitmap.setPixel(COL,行,pixel2);
}
}
} 返回dstBitmap;
}
公共静态位图覆盖(位图BMP1,位图BMP2){
/ *两个位图连接在一起* /
位图bmOverlay = Bitmap.createBitmap(bmp1.getWidth(),bmp1.getHeight(),bmp1.getConfig());
帆布帆布=新的Canvas(bmOverlay);
canvas.drawBitmap(BMP1,新的Matrix(),NULL);
canvas.drawBitmap(BMP2,0,0,NULL);
返回bmOverlay;
}
的code摘要:我在立方体面(正面,左侧,顶部)的每个像素运行,而这样做我也是在纹理位图的每个像素运行,与纹理的像素上色侧像素。这之后,我所有的颜色方面连接在一起成一个位图和归还。
结果:
I have a 2d bitmap that I would like to convert into a 3d cube(example like in minecraft: )
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.
Edit: this is as close as I got:
using this code:
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:
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.
Result:
这篇关于创建3D立方体帆布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!