当前位置: 首页 > news >正文

unity下贴图混合(Texture Blending)

在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 = 0; x < tt2.width; x++)
            {
                for (int y = 0; 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内置函数改为块操作,经过测试,能大概少一半的耗时

 1 public void MergeTexture(Texture2D tt1, Texture2D tt2, int offsetX, int offsetY)
 2         {
 3             
 4             Texture2D newTex= new Texture2D(tt1.width, tt1.height, TextureFormat.ARGB32, false);
 5             
 6             newTex.SetPixels(tt1.GetPixels());
 7             Color32[] colors = tt2.GetPixels32();
 8             // tt1.
 9             newTex.SetPixels32(offsetX, tt1.height - offsetY - tt2.height,tt2.width,tt2.height, colors,0);
10             newTex.Apply();
11             GameObject.Find("obj001").GetComponent<Renderer>().material.mainTexture = newTex;
12         }

上述方案基本上是在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(1024, 1024, 32);           //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(0, 1024, 1024, 0);            //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(0, 0, rt.width, rt.height), 0, 0);
        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.   

转载于:https://www.cnblogs.com/chenc-c/p/10538907.html

相关文章:

  • elasticsearch中ik词库配置远程热加载
  • OL4加载geowebcache 部署的离线切片
  • 在Net MVC中应用JsTree
  • nginx代理tcp协议连接mysql
  • markdown操作手册
  • [转载]URI 源码分析
  • HTML之常用标签及属性
  • jmeter 常见问题汇总
  • SPOJ COT3.Combat on a tree(博弈论 Trie合并)
  • HDU 2883 kebab
  • C++学习笔记30,指针的引用(2)
  • fatal error C1010: 在查找预编译头时遇到意外的文件结尾
  • c# Winform dev控件之ChartControl
  • Spring框架学习07——基于传统代理类的AOP实现
  • html迪士尼网页实现代码
  • css系列之关于字体的事
  • ES6之路之模块详解
  • ESLint简单操作
  • javascript数组去重/查找/插入/删除
  • Java的Interrupt与线程中断
  • Laravel Mix运行时关于es2015报错解决方案
  • Less 日常用法
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • OpenStack安装流程(juno版)- 添加网络服务(neutron)- controller节点
  • python学习笔记 - ThreadLocal
  • Python学习笔记 字符串拼接
  • Python学习之路13-记分
  • QQ浏览器x5内核的兼容性问题
  • Redis的resp协议
  • Redux系列x:源码分析
  • spring cloud gateway 源码解析(4)跨域问题处理
  • Spring Security中异常上抛机制及对于转型处理的一些感悟
  • uva 10370 Above Average
  • Wamp集成环境 添加PHP的新版本
  • 编写符合Python风格的对象
  • 免费小说阅读小程序
  • 强力优化Rancher k8s中国区的使用体验
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • 最近的计划
  • 湖北分布式智能数据采集方法有哪些?
  • #Lua:Lua调用C++生成的DLL库
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • (Arcgis)Python编程批量将HDF5文件转换为TIFF格式并应用地理转换和投影信息
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (JS基础)String 类型
  • (LeetCode) T14. Longest Common Prefix
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (附源码)计算机毕业设计SSM疫情下的学生出入管理系统
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (离散数学)逻辑连接词
  • (力扣)循环队列的实现与详解(C语言)
  • (轉貼) 寄發紅帖基本原則(教育部禮儀司頒布) (雜項)