在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.

unity下贴图混合(Texture Blending)的更多相关文章

  1. Unity shader error: “Too many texture interpolators would be used for ForwardBase pass”

    Unity shader error: "Too many texture interpolators would be used for ForwardBase pass" 解决 ...

  2. unity 读取灰度图生成按高程分层设色地形模型

    准备灰度图 1.高程按比例对应hue色相(hsv)生成mesh效果 o.color = float4(hsv2rgb(float3(v.vertex.y/100.0, 0.5, 0.75)), 1.0 ...

  3. unity读取灰度图生成等值线图

    准备灰度图 grayTest.png,放置于Assets下StreamingAssets文件夹中.   在场景中添加RawImage用于显示最后的等值线图.   生成等值线的过程,使用Marching ...

  4. unity读取灰度图生成三维地形mesh

    准备灰度图 IGray.png及草地贴图 IGrass.jpg ,放入Assets下StreamingAssets文件夹中.     创建空材质,用作参数传入脚本.   脚本如下,挂载并传入材质球即可 ...

  5. 【VR视频播放】解决Unity模型贴图反转的问题

    使用UV贴图网模型上贴的时候, 会出现图片反过来的情况. 根本原因是因为, 一般系统的屏幕坐标系(例如Android)是左上角为原点(0,0), 但是Unity的贴图是以左下角为原点(0,0) 方法有 ...

  6. Ubuntu16.04下Neo4j图数据库官网安装部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 说在前面的话  首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu LTS \n \l r ...

  7. Ubuntu14.04下Neo4j图数据库官网安装部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 说在前面的话  首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu 14.04.4 LTS ...

  8. Unity下的开发框架--适应web和微端游戏异步资源请求的框架

    一.   内容简介: 1.   框架对Web与微端游戏特性的支持: Web和微端游戏最重要的特性是,资源是持续从服务器上即时下载下来的.而保证体验流畅的关键就是保证资源下载分散到持续的体验过程中,并保 ...

  9. Unity下XLua方案的各值类型GC优化深度剖析

    转自:http://gad.qq.com/article/detail/25645 前言 Unity下的C#GC Alloc(下面简称gc)是个大问题,而嵌入一个动态类型的Lua后,它们之间的交互很容 ...

随机推荐

  1. 关于4A系统(我对4A系统的维护的理解)

    4A系统 4A系统是统一安全管理平台解决方案,指认证Authentication.账号Account.授权Authorization.审计Audit,中文名称为统一安全管理平台解决方案.即将身份认证. ...

  2. metasploit与Cobaltstrike互相派生shell

    msf 派生 shell 给 Cobalt strike(前提有一个meterpreter) msf exploit(handler) > use exploit/windows/local/p ...

  3. 图解TCP/IP

    序言 ----

  4. design language

    design language https://en.wikipedia.org/wiki/Design_language 设计语言(设计词汇)是一种超架构的方案和风格, 它用于指导产品组件或者架构配 ...

  5. (Python3) 求中位数 代码

    def zhongweishu(a): new=sorted(a) if len(a)%2==0: s=(new[int(len(a)/2-1)]+new[int(len(a)/2)])/2 else ...

  6. CORS跨域 Ajax headers 问题

    今天我们遇到了一个CORS跨域的问题Ajax如下 var url = "http://localhost:11980/api/Demo/GetString"; //api地址 $. ...

  7. python3 练手实例6 做一个简单日历

    import calendar year = int(input('请输入要查询的年份:')) month = int (input('请输入要查询的月数:')) print (calendar.mo ...

  8. Livereload or meta

    静态页面布局的过程中,如果可以一边写一边看见结果,那肯定是很方便的,在最开始使用的DW中实现了这一目标,但并不是浏览器环境下.之后使用gulp中的livereload后配合chrome插件livere ...

  9. 论文翻译——Lattice indexing for spoken term detection

    第II节简要介绍与本文有关的先前工作第III节介绍文中使用的定义以及术语 第IV节介绍如何从原始ASR lattices中生成倒排索引结构 第V节详细介绍了ASR结构以及实验使用的数据 第VI节提供了 ...

  10. will not be managed by Spring/ [managed: 15; max: 15]

    检查事务配置 检查 mybatis 配置文件扫描路径是否正确 <!-- 自动扫描 --> <context:component-scan base-package="com ...