首页 > 其他分享 >Unity之贴图混合

Unity之贴图混合

时间:2023-11-16 17:47:23浏览次数:34  
标签:贴图 RenderTexture tt2 tt1 混合 Unity Texture2D height newTex

有如下几种方式:

1.CPU端逐像素根据alpha通道进行叠加

 1         public void MergeTexture(Texture2D tt1, Texture2D tt2, int offsetX, int offsetY)
 2         {
 3             Texture2D newTex = new Texture2D(tt1.width, tt1.height, TextureFormat.ARGB32, false);
 4 
 5             newTex.SetPixels(tt1.GetPixels());
 6 
 7             for (int x = 0; x < tt2.width; x++)
 8             {
 9                 for (int y = 0; y < tt2.height; y++)
10                 {
11                     var PixelColorFore = tt2.GetPixel(x, y) * tt2.GetPixel(x, y).a;
12                     var newY = tt1.height - offsetY - tt2.height + y;
13                     var PixelColorBack = tt1.GetPixel(x + offsetX, newY) * tt1.GetPixel(x + offsetX, newY).a;
14                     newTex.SetPixel(x + offsetX, newY, PixelColorFore+ PixelColorBack);
15                 }
16             }
17             newTex.Apply();
18             System.IO.File.WriteAllBytes(Application.dataPath + "/" + tt1.name + ".png", newTex.EncodeToPNG());
19             GameObject.Find("obj001").GetComponent<Renderer>().material.mainTexture = newTex;
20         }

2.逐像素操作改为块操作

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

3.调用Unity的底层绘制接口在GPU上进行操作

 1         public Texture2D texture;        //Starting image.
 2         public Texture2D stampTexture;   //Texture to Graphics.Drawtexture on my RenderTexture.
 3         public float posX = 256f;        //Position the DrawTexture command while testing.
 4         public float posY = 256f;        //Position the DrawTexture command while testing.
 5         RenderTexture rt;                //RenderTexture to use as buffer.
 6 
 7         public void MergeTexture()
 8         {
 9             rt = new RenderTexture(1024, 1024, 32);           //Create RenderTexture 1024x1024 pixels in size.
10             GameObject.Find("obj001").GetComponent<Renderer>().material.mainTexture = rt;   //Assign my RenderTexure to be the main texture of my object.
11             RenderTexture.active = rt;
12             Graphics.Blit(texture, rt);          //Blit my starting texture to my RenderTexture.
13             RenderTexture.active = rt;                      //Set my RenderTexture active so DrawTexture will draw to it.
14             GL.PushMatrix();                                //Saves both projection and modelview matrices to the matrix stack.
15             GL.LoadPixelMatrix(0, 1024, 1024, 0);            //Setup a matrix for pixel-correct rendering.
16                                                              //Draw my stampTexture on my RenderTexture positioned by posX and posY.
17             Graphics.DrawTexture(new Rect(posX, posY, stampTexture.width, stampTexture.height), stampTexture);
18             Texture2D png = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false);
19             png.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
20             System.IO.File.WriteAllBytes(Application.dataPath + "/" + "nihao.png", png.EncodeToPNG());
21             GL.PopMatrix();
22             //Restores both projection and modelview matrices off the top of the matrix stack.
23             RenderTexture.active = null;                    //De-activate my RenderTexture.
24         }

 

转载请注明出处:https://www.cnblogs.com/jietian331/p/17836843.html

 

标签:贴图,RenderTexture,tt2,tt1,混合,Unity,Texture2D,height,newTex
From: https://www.cnblogs.com/jietian331/p/17836843.html

相关文章

  • 最高加速9倍!字节跳动开源8比特混合精度Transformer引擎
    前言 近年来,Transformer已经成为了NLP和CV等领域的主流模型,但庞大的模型参数限制了它的高效训练和推理。于是字节跳动在2019年12月和2021年6月分别推出了高效推理和训练引擎LightSeq,大大加速了Transformer系列模型的训练和推理,也打通了Transformer从训练到推理......
  • [Unity3D]检测鼠标点击角色移动
    学习工具Unity3D学习内容如何检测鼠标点击移动角色自己的理解原理:通过检测鼠标的光线投射(Raycast)是否与地面碰撞(RaycastHit),再检测鼠标左键(Input.GetMouseButton(0))是否点击,如果两个都符合则执行移动方法源代码如下:privateboolInteractWithMovement(){Raycas......
  • 【Unity UGUI】UGUI适配
    1/*=================================================2*FileName:SafeAreaPanel.cs3*Author:None4*UnityVersion:2021.3.20f15*Date:2023-07-2016:116*Description:UI适配组件7*History:8*---------......
  • unity 打包问题记录
     问题1: ErrorbuildingPlayerbecausescriptshadcompilererrors问题描述:在打包准备真机测试时发现了这个问题,到处查找解决办法后发现了问题,以下一些解决办法提供了思路。解决思路:(1)、关于AB打包出现的错误:ErrorbuildingPlayerbecausescriptshadcompilererrors的解......
  • Unity-观察者模式(observer)
    Unity-观察者模式(observer)引言​ 了解完委托(delegate)与事件(event)之后,我们来讨论一个问题。​ 假设我们有一个热水器,我们给他同上电,当水温超过95度的时候:1、扬声器会开始发出语音,告诉你温度;2、液晶屏也会改变水温的显示,来提示水已经烧开了​ 现在我们需要写一个程序来模拟这个......
  • Unity-背包系统
    Unity-背包系统简介​ 背包是每个成功游戏中不可缺少的,玩家获取的装备与道具将会放入背包,需要时再拿出来使用。如果没有背包来储存玩家在游戏中获得的武器和道具,或许游戏将会变得十分单一枯燥,出招方式一成不变。​ 有了背包系统,玩家才可以使用不同的武器,搭配不同的道具,使出不同......
  • Unity-FSM有限状态机
    Unity-FSM有限状态机什么是有限状态机?​ 在编写一些需要判断多个条件的程序时,我们常常会用到if-else语句,这样能够很好的帮我们解决多数问题。但在游戏开发过程中,一个角色的行为不是一成不变的,需要实时的进行修改,此时如果我们使用的是if-else来判断角色所处状态,就需要修改整......
  • Unity-场景的异步加载
    Unity-场景的异步加载为什么需要异步加载​ 在诸多大型游戏里,场景渲染精度都是动态的,随着场景与角色距离的增加,渲染精度也在递减,这样极大的减少了硬件性能的消耗。​ 但如果角色使用了某些传送技能,将自己传送到为渲染的地点,游戏可能就会因为需要瞬间渲染大量的场景而卡顿。此时......
  • Unity-Menu&场景切换
    Unity-Menu&场景切换开始界面1.要创建开始界面,首先要新建一个场景,用于添加游戏开始界面的内容2.新建按钮步骤:UI>画板>Button(按钮)>根据需要设置按钮3.给按钮添加代码,使得按下按钮就可以进入下一关/退出游戏(1)代码内容需要用到usingUnityEngine.SceneManagement的头文件......
  • Unity-敌人(Enemy)
    Unity-敌人(Enemy)引言​ 敌人是每个游戏中不可缺少的部分,设计得好的敌人可以给游戏增添很多乐趣,设计得差的则会非常影响我们的游戏体验。​ 经过这段时间的学习,我们已经接触了非常多的敌人代码的写法,但是就是没有系统的归类,导致每次写敌人,都要从头开始。现在是时候将他们进行一......