在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. String Byte 互转

    string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转成string: ...

  3. vuex概念详解

    阅读vuex官网以后用自己的话概括起来就是:vuex是vue配套的公共数据管理工具,它可以把一些共享的数据,保存到vuex中,方便整个程序中的任何组件直接获取或修改我们的公共数据. vuex是为了保存 ...

  4. [经验交流] 影响 kubernetes 稳定性的因素

    使用k8s已有近一年的时间,版本从1.2到1.5.1.6.1.7,期间出现并解决了不少问题,下面是我总结的影响k8s集群稳定性的因素: 1. 安装环境 *kubelet版本最好与kube-apiser ...

  5. tp5.0 queue 队列操作

    检查是否安装redis(没有请自行百度安装): phpinfo: 配置thinkphp-queue,没有请执行 composer require topthink/think-queue 加入: 创建 ...

  6. 2018-2019-2 网络对抗技术 20165325 Exp2 后门原理与实践

    2018-2019-2 网络对抗技术 20165325 Exp2 后门原理与实践 实验内容(概要): (1)使用netcat获取主机Shell,cron启动 首先两个电脑(虚拟机)都得有netcat, ...

  7. Django-CSRF,AJAX,FORM

    内容总览1.CSRF相关1>CSRF源码分析2>ajax的实现(ajax的实例(异步计算,参数测试,上传))3>ajax通过csrf的校验 2.FORM组件1>基本使用2> ...

  8. day13 Python数据基本类型

    算数运算 / x除以y // 取整除 %返回除法的余数 !=   不等于 <> 不等于 赋值运算 c+= a等价于c=c+a c-= a等价于c=c-a 逻辑运算 and or not 基 ...

  9. sed 删除文本

    sed删除文本命令可以将指定行或指定行范围进行删除,sed编辑命令的删除文本符号为 d,删除文本的格式为. [ sed]$ cat input [ sed]$ sed '/8/d' input 删除最 ...

  10. .Net 入门资料推荐 (编辑中)

    1.首先推荐 网易云课堂上的一个付费课程:常老师带你学ASP.NET MVC ,价格199元 2. 一个.net的框架,ABP,中文介绍如下 http://www.cnblogs.com/farb/p ...