在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. 树莓派的系统安装,并且利用网线直连 Mac 进行配置

    最近单位给了我一个新的树莓派3B+让我自己玩.下面是我记录的我如何安装 Raspbian Stretch Lite 系统,然后如何成功不用独立显示屏而利用 MacBook 对其进行配置. 安装 Ras ...

  2. vs 开发 win32 程序,调出控制台窗口,方便调试

    设置方法 项目 -> 属性 -> 生成事件 ->后期生成事件 -> 命令行 中添加 editbin /SUBSYSTEM:CONSOLE $(OutDir)\$(Project ...

  3. [转载]如何在ubuntu上使用github

    来源:https://blog.csdn.net/tina_ttl/article/details/51326684 https://blog.csdn.net/u013551462/article/ ...

  4. Linux 定时运行设置

    脚本设置位置: /etc/cron.d SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # 每个小时的01分钟执行这个脚本 ...

  5. react native 子组件向父组件传值

    父组件:   引入子组件:import CheckBox from  '../checkbox';   父子之间交互通信,接受子组件的值 fn(val){this.setState({roleType ...

  6. 【bzoj 3495】PA2010 Riddle

    Description 有n个城镇被分成了k个郡,有m条连接城镇的无向边.要求给每个郡选择一个城镇作为首都,满足每条边至少有一个端点是首都. Input 第一行有三个整数,城镇数n(1<=n&l ...

  7. 小程序 <web-view></web-view> 中使用 form 表单提交

    在最近的小程序项目中,使用到了 <web-view></web-view> 内嵌 H5 页面,在 H5 中需要使用 form 表单提交数据. H5 使用的技术框架是 vue+v ...

  8. 背景上实现阴影——linear-gradient

    /*从元素顶部有条阴影,两种方式,第二种更好,能控制阴影的宽度*/background-image: linear-gradient(0deg, rgba(226, 226, 226, 0) 97%, ...

  9. jquery实现点击页面空白处,弹框消失

    要求:点击1,弹框2显示,点击空白处,弹框2消失 $("#AddDevices"):按钮1 $(".addDeviceBox")弹框2 //点击添加设备弹框 $ ...

  10. FT View SE联合Studio 5000仿真

    ​前言:一个实际的自动化项目,都是综合性的,不仅需要PLC进行逻辑.顺序.运动等控制,还需要在上位机进行监视和操作.当没有物理PLC时,上位机软件就无法连接到实际的变量数据,开发出来的界面和功能无法验 ...