2016-04-15 147 views
0

我想从一张照片,与WebCamTexture,玩家的脸,然后将其应用到一个3D模型的纹理这样玩家的脸就会取代模型的一个。我有这张照片,并且我已经准备好了纹理,但是我尝试了不同的方式,有两个周期和一个getPixels区域,但没有任何区别,只是在我需要编辑的区域出现了一个乱七八糟的正方形(右侧区)。下面的图片展示了我所需要的:Unity 5 GetPixels从一个纹理,然后SetPixels在另一个纹理,这使混乱

The Result

编辑: 这是我的尝试:

//path of the photo taken 
_SavePath = Application.dataPath+"/Snap"; 
//The texture of the photo taken from WebCamTexture (my webcam output is 800x600) 
Texture2D snap = new Texture2D(wct.width, wct.height); 
//The texture of the 3D model (800x800 px) that I need to edit to apply a section of the photo taken (snap texture) 
Texture2D texFace = new Texture2D(OrigText.width, OrigText.height); 
//set pixels of the photo 
snap.SetPixels(wct.GetPixels()); 
snap.Apply(); 
//This divides the image into logic squares so find the points from where to start the loop to get the section of the photo (based on the webcam output resolution) 
int sc_ux = snap.width/8; 
int sc_uy = snap.height/6; 
//same here for the final 3D Model texture 
int te_ux = texFace.width/6; 
int te_uy = texFace.height/6; 
//Getting the points 
int x1 = sc_ux * 3; 
int y1 = sc_uy * 2; 
int x2 = sc_ux * 5; 
int y2 = sc_uy * 5; 
int x3 = te_ux * 2; 
int y3 = te_uy * 1; 
int x4 = te_ux * 4; 
int y4 = te_uy * 4; 

int xx = x1; 
int yy = y1; 
//same of SetPixels function, I've tried with a loop too. Here I create a copy of the original texture (OrigText) to texFace. 
for (int y = 0; y < OrigText.height; y++) { 
    for (int x = 0; x < OrigText.width; x++) { 
     texFace.SetPixel (x, y, OrigText.GetPixel (x, y)); 
    } 
} 
texFace.Apply(); 
//Then I loop starti from the area that I need to change with the pixels of the area I need from snap texture (photo taken from webcam) 
for (int y = y3; y <= y4; y++) { 
    for (int x = x3; x <= x4; x++) { 
     Color getcolor = snap.GetPixel(xx, yy); 
     texFace.SetPixel (x, y, getcolor); 
     xx += 1; 
    } 
    yy += 1; 
} 
//apply 
texFace.Apply(); 

而这就是我从获得:

Not what I want

有人可以帮我吗?

+0

不应该在某个时候重置'xx':'yy + = 1; xx = x1;' – TaW

+0

你说得对,我会试试看。 – Khelthos

+0

作为一个方面说明,你应该尝试使用Set/GetPixels32,这些内存更友好。 – Everts

回答

0

我建议你看看使用RenderTexture而不是从数组中复制像素,这样你就可以更好地控制你正在做的事情。您可以使用Unity UI,并将两个纹理渲染到具有头部纹理的渲染纹理目标,然后将拍摄的照片的相机馈送投射到纹理的四边形顶部,当渲染这些东西时,我将获取渲染的像素纹理并使用renderTextureTarget.EncodeToPNG()将其作为PNG进行编码,然后保存到磁盘并稍后加载到3d模型中:-)