有如下几种方式:
1.CPU端逐像素根据alpha通道进行叠加
1 public void MergeTexture(Texture2D tt1, Texture2D tt2, int offsetX, int offsetY) 2 { 3 Texture2D newTex = new Texture2D(tt1.width, tt1.height, TextureFormat.ARGB32, false); 4 5 newTex.SetPixels(tt1.GetPixels()); 6 7 for (int x = 0; x < tt2.width; x++) 8 { 9 for (int y = 0; y < tt2.height; y++) 10 { 11 var PixelColorFore = tt2.GetPixel(x, y) * tt2.GetPixel(x, y).a; 12 var newY = tt1.height - offsetY - tt2.height + y; 13 var PixelColorBack = tt1.GetPixel(x + offsetX, newY) * tt1.GetPixel(x + offsetX, newY).a; 14 newTex.SetPixel(x + offsetX, newY, PixelColorFore+ PixelColorBack); 15 } 16 } 17 newTex.Apply(); 18 System.IO.File.WriteAllBytes(Application.dataPath + "/" + tt1.name + ".png", newTex.EncodeToPNG()); 19 GameObject.Find("obj001").GetComponent<Renderer>().material.mainTexture = newTex; 20 }
2.逐像素操作改为块操作
1 public void MergeTexture(Texture2D tt1, Texture2D tt2, int offsetX, int offsetY) 2 { 3 Texture2D newTex= new Texture2D(tt1.width, tt1.height, TextureFormat.ARGB32, false); 4 5 newTex.SetPixels(tt1.GetPixels()); 6 Color32[] colors = tt2.GetPixels32(); 7 // tt1. 8 newTex.SetPixels32(offsetX, tt1.height - offsetY - tt2.height,tt2.width,tt2.height, colors,0); 9 newTex.Apply(); 10 GameObject.Find("obj001").GetComponent<Renderer>().material.mainTexture = newTex; 11 }
3.调用Unity的底层绘制接口在GPU上进行操作
1 public Texture2D texture; //Starting image. 2 public Texture2D stampTexture; //Texture to Graphics.Drawtexture on my RenderTexture. 3 public float posX = 256f; //Position the DrawTexture command while testing. 4 public float posY = 256f; //Position the DrawTexture command while testing. 5 RenderTexture rt; //RenderTexture to use as buffer. 6 7 public void MergeTexture() 8 { 9 rt = new RenderTexture(1024, 1024, 32); //Create RenderTexture 1024x1024 pixels in size. 10 GameObject.Find("obj001").GetComponent<Renderer>().material.mainTexture = rt; //Assign my RenderTexure to be the main texture of my object. 11 RenderTexture.active = rt; 12 Graphics.Blit(texture, rt); //Blit my starting texture to my RenderTexture. 13 RenderTexture.active = rt; //Set my RenderTexture active so DrawTexture will draw to it. 14 GL.PushMatrix(); //Saves both projection and modelview matrices to the matrix stack. 15 GL.LoadPixelMatrix(0, 1024, 1024, 0); //Setup a matrix for pixel-correct rendering. 16 //Draw my stampTexture on my RenderTexture positioned by posX and posY. 17 Graphics.DrawTexture(new Rect(posX, posY, stampTexture.width, stampTexture.height), stampTexture); 18 Texture2D png = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false); 19 png.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); 20 System.IO.File.WriteAllBytes(Application.dataPath + "/" + "nihao.png", png.EncodeToPNG()); 21 GL.PopMatrix(); 22 //Restores both projection and modelview matrices off the top of the matrix stack. 23 RenderTexture.active = null; //De-activate my RenderTexture. 24 }
转载请注明出处:https://www.cnblogs.com/jietian331/p/17836843.html
标签:贴图,RenderTexture,tt2,tt1,混合,Unity,Texture2D,height,newTex From: https://www.cnblogs.com/jietian331/p/17836843.html