在unity制作自定义时,经常会遇到自定义妆容等问题,美术会提供大量的眉毛/胡子/腮红等贴图,来供用户选择。

美术给出的眉毛的小贴图如下:

在用户选用不同的胡子眉毛,可以将选定的小贴图和皮肤base贴图进行融合,得到完整的Character贴图。

       

Method1:CPU端逐像素根据alpha通道进行叠加。

public void MergeTexture_(Texture2D tt1, Texture2D tt2, int offsetX, int offsetY)
{ Texture2D newTex = new Texture2D(tt1.width, tt1.height, TextureFormat.ARGB32, false); newTex.SetPixels(tt1.GetPixels()); for (int x = ; x < tt2.width; x++)
{
for (int y = ; y < tt2.height; y++)
{
var PixelColorFore = tt2.GetPixel(x, y) * tt2.GetPixel(x, y).a;
var newY = tt1.height - offsetY - tt2.height + y;
var PixelColorBack = tt1.GetPixel(x + offsetX, newY) * tt1.GetPixel(x + offsetX, newY).a;
newTex.SetPixel(x + offsetX, newY, PixelColorFore+ PixelColorBack);
}
}
newTex.Apply();
System.IO.File.WriteAllBytes(Application.dataPath + "/" + tt1.name + ".png", newTex.EncodeToPNG());
GameObject.Find("obj001").GetComponent<Renderer>().material.mainTexture = newTex;
}

这里注意下,由于需要对Texture进行读取像素,因此需要将相应的Texture设置为Read/Write Enabled,否则会报错。

逐像素操作可以用unity内置函数改为块操作,经过测试,能大概少一半的耗时

 public void MergeTexture(Texture2D tt1, Texture2D tt2, int offsetX, int offsetY)
{ Texture2D newTex= new Texture2D(tt1.width, tt1.height, TextureFormat.ARGB32, false); newTex.SetPixels(tt1.GetPixels());
Color32[] colors = tt2.GetPixels32();
// tt1.
newTex.SetPixels32(offsetX, tt1.height - offsetY - tt2.height,tt2.width,tt2.height, colors,);
newTex.Apply();
GameObject.Find("obj001").GetComponent<Renderer>().material.mainTexture = newTex;
}

上述方案基本上是在CPU端进行的,实际上可以调用Unity的底层绘制接口在GPU上进行操作,主要是利用RenderTexture和Graphics.DrawTexture()进行操作

 public Texture2D texture;        //Starting image.
public Texture2D stampTexture; //Texture to Graphics.Drawtexture on my RenderTexture.
public float posX = 256f; //Position the DrawTexture command while testing.
public float posY = 256f; //Position the DrawTexture command while testing.
RenderTexture rt; //RenderTexture to use as buffer. void Start()
{
rt = new RenderTexture(, , ); //Create RenderTexture 512x512 pixels in size.
GameObject.Find("obj001").GetComponent<Renderer>().material.mainTexture = rt; //Assign my RenderTexure to be the main texture of my object.
RenderTexture.active = rt;
Graphics.Blit(texture, rt); //Blit my starting texture to my RenderTexture.
RenderTexture.active = rt; //Set my RenderTexture active so DrawTexture will draw to it.
GL.PushMatrix(); //Saves both projection and modelview matrices to the matrix stack.
GL.LoadPixelMatrix(, , , ); //Setup a matrix for pixel-correct rendering.
//Draw my stampTexture on my RenderTexture positioned by posX and posY.
Graphics.DrawTexture(new Rect(posX, posY, stampTexture.width, stampTexture.height), stampTexture);
Texture2D png = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false);
png.ReadPixels(new Rect(, , rt.width, rt.height), , );
System.IO.File.WriteAllBytes(Application.dataPath + "/" + "nihao.png", png.EncodeToPNG());
GL.PopMatrix();
//Restores both projection and modelview matrices off the top of the matrix stack.
RenderTexture.active = null; //De-activate my RenderTexture.

最新文章

  1. iOSIPV6简单测试环境搭建
  2. SQL Server客户端请求
  3. JavaScript学习01 语言简介、基本使用和变量声明
  4. C++基础笔记(四)C++内存管理
  5. 下载discuz 6 论坛的附件
  6. NEC遥控信号解码(包含完整代码)
  7. WGS84、GCJ-02(火星坐标)、百度坐标,Web墨卡托坐标
  8. php 关了浏览器也可以自动运行脚本
  9. Delphi xe7并行编程快速入门(转)
  10. win10下Anaconda 2 和 3 共存安装,并切换jupyter notebook和Pycharm中的对应版本
  11. Pandas 拼接操作 数据处理
  12. SqlSever大数据分页【转】
  13. drools 的一个小demo
  14. jquery获取table,遍历输出tr中各个td的内容(转载)
  15. iOS five years[转]
  16. 珍藏40个android应用源码分享
  17. Python+Django(Python Web项目初体验)
  18. 服务器的日志一直报Packet for query is too large (7632997 &gt; 4194304). You can change this value on the server by setting the max_allowed_packet&#39; variable.的解决方法
  19. YOURLS&#39; API
  20. 1087 1 10 100 1000(打表 set 数学)

热门文章

  1. tensorflow Pipeline 之TextLineReader 和decode_csv多分割替代方案
  2. Spring Cloud微服务实战:手把手带你整合eureka&amp;zuul&amp;feign&amp;hystrix
  3. docker部署archery
  4. JS 获取本月第一天零点时间戳并转化成yy-mm-dd
  5. MySQL学习13 - 索引
  6. 推送提交(git push)
  7. 浅入深出Vue:环境搭建
  8. Python中区分函数和方法
  9. vue组件通讯方法汇总(在不使用vuex的情况下)
  10. 【项目】Selenium和pymongo复习