2016-04-15 42 views
0

WebCamTextureで撮影した写真から、プレーヤーの顔を取得しようとしていて、3Dモデルのテクスチャに適用しようとしていますプレイヤーの顔がモデルの1つを置き換えるようにします。私は写真を持っていて、テクスチャを混在させる準備ができていますが、2回のサイクルでgetPixelsを使用してさまざまな方法で試しましたが、何も編集する必要のない領域に乱れた正方形しか表示されません(右エリア)。下の画像は、私が必要なものを示していますUnity 5テクスチャからGetPixelsを取得し、別のテクスチャ上にSetPixelsを設定すると、これは混乱を招く

The Result

はEDIT:

//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を使用して、2つのテクスチャをレンダリングテクスチャにレンダリングすることができます。これらのテクスチャをヘッドテクスチャでレンダリングし、次に撮影した写真のカメラフィードをテクスチャのクォートオンタップにレンダリングします。テクスチャをレンダリングし、renderTextureTarget.EncodeToPNG()を使ってPNG形式でエンコードしてから、ディスクに保存して後で3dモデルにロードしてください。

関連する問題