2015-04-27 31 views
0

我想在統一爲android創建一個角色定製。現在情景是,我有一個服裝模型,它是一個紋理2D,也是用戶可以在這個服裝模型上應用的一系列圖案和顏色。現在,當用戶將圖案應用到連衣裙上時,我需要將連衣裙改變爲以該圖案顯示。對於顏色,我可以將rgb值更改爲所需的顏色值。但對於這種圖案,我需要遍歷連衣裙的每個像素,並將相應顏色的圖案應用於連衣裙的像素。我通過以下代碼實現了這一點。在團結中將紋理圖案填充到另一個紋理上

IEnumerator Print() { 
    Texture2D tex = DressTexture; 
    Color32[] DressColor = tex.GetPixels32(); 
    Color32[] PatternColor = PatternTexture.GetPixels32(); 
    int j = 0; 
    Texture2D NewDressPattern = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false); 
    for(int i = 0; i < DressColor.Length; i++) { 
     if(DressColor[i].a != 0) { 
      DressColor[i] = PatternColor[j]; 
      j++; 
      if(j == PatternColor.Length - 1) { 
       j = 0; 
      } 
     } 
     else { 
      j = 0; 
     } 
     yield return null; 
    } 
    NewDressPattern.SetPixels32(DressColor); 
    NewDressPattern.Apply(); 
    Debug.Log("texture created"); 
    Sprite spr = Sprite.Create(NewDressPattern, new Rect(0, 0, tex.width, tex.height), Vector2.zero); 
    Debug.Log("sprite created"); 
    sprite.sprite = spr; 
} 

現在問題是這個操作太慢而無法完成。任何更好的方法來達到此目的的建議將會非常有用。此外,我不知道很多着色器。

回答

0

紋理操作總是需要一些耗時的處理。您可以採取一些解決方法來儘量減少對用戶的影響。就像做多線程或非阻塞計算一樣(我認爲這就是你現在正在做的)。但他們只會將問題最小化,不會解決問題。

您沒有提及2D環境中使用的紋理作爲精靈還是用於3D模型。

2D遊戲:

我會做的是我會用一個單獨的精靈爲每個對象。然後我會重疊精靈,結果就像你上面要做的那樣。

對於3D模型:

你可以使用一些簡單的着色器重疊的多個紋理。只是谷歌它,你會有很多的例子。像這些簡單着色器不需要火箭科學知識:)

+0

感謝:例如,下面的代碼使用內置的「漫詳細信息」着色器的Texture2D對象,並把結果渲染紋理結合起來。遊戲使用精靈。爲每個模式創建單獨的精靈是不可能的,因爲這會增加構建大小。如果我有10件禮服和5種圖案,我將不得不製作50個我認爲不太好的精靈。 –

+0

然後最好使用着色器來做。 @gamedevelopmentgerm的回答是我認爲的一個好開始。 –

0

此外,渲染到紋理技術可用於預渲染多個紋理傳遞。對於@Can Baycay答案

public RenderTexture PreRenderCustomDressTexture(int customTextureWidth, int customTextureHeight, Texture2D pattern1, Texture2D pattern2) 
{ 
    RenderTexture customDressTexture = new RenderTexture(customTextureWidth, customTextureHeight, 32, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default); 

    Material blitMaterial = new Material(Shader.Find("Diffuse Detail")); 
    blitMaterial.SetColor("_Color", Color.white); 
    blitMaterial.SetTexture("_MainTex", pattern1); 
    blitMaterial.SetTexture("_Detail", pattern2); 
    Graphics.Blit(pattern1, customDressTexture, blitMaterial); 

    return customDressTexture; 
} 
相關問題