Unity中,利用shader将NGUI图集大小缩小四倍

2024-06-13 15:08

本文主要是介绍Unity中,利用shader将NGUI图集大小缩小四倍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


如何将ARGB图像(带alpha)分成两张RGB图像(不带alpha,第二张为灰度图),我将导出的alpha图的TextureFormat .RGB24改为 TextureFormat .Alpha8,一个通道的Alpha8竟然比3个通道的RGB大,变大两倍。

using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEditor;class TextureSplit : EditorWindow
{[ MenuItem ("Assets/Texture Slpit" )]static void DoSplit(){Object [] SelectedTexs = Selection.GetFiltered( typeof (Texture2D ), SelectionMode.Assets);foreach (Texture2D Tex in SelectedTexs){string TexPath = AssetDatabase .GetAssetPath(Tex);TextureImporter TexImporter = AssetImporter .GetAtPath(TexPath) as TextureImporter ;TexImporter.isReadable = true ;AssetDatabase .ImportAsset(TexPath);Texture2D ColorTex = new Texture2D(Tex.width, Tex.height, TextureFormat .RGB24, false );ColorTex.SetPixels(Tex.GetPixels());string FilePath = AssetDatabase .GetAssetPath(Tex);int Pos = FilePath.LastIndexOf( '.');FilePath = FilePath.Substring( 0 , Pos);FilePath = FilePath + "_color.png" ;byte [] bytes = ColorTex.EncodeToPNG();System.IO. File .WriteAllBytes(FilePath, bytes);{AssetDatabase .ImportAsset(FilePath);TextureImporter TargetTexImporter = AssetImporter .GetAtPath(FilePath) as TextureImporter ;TargetTexImporter.textureType = TexImporter.textureType;TargetTexImporter.filterMode = TexImporter.filterMode;TargetTexImporter.maxTextureSize = TexImporter.maxTextureSize;TargetTexImporter.textureFormat = TextureImporterFormat .AutomaticCompressed;AssetDatabase .ImportAsset(FilePath);}CLogger .Log("Save " + FilePath + " Succeed" );Texture2D ALphaTex = new Texture2D(Tex.width, Tex.height, TextureFormat .RGB24, false );Color [] Pixels=Tex.GetPixels();for (int i = 0; i < Pixels.Length;i++ ){Pixels[i].r = Pixels[i].a;Pixels[i].g = Pixels[i].a;Pixels[i].b = Pixels[i].a;}ALphaTex.SetPixels(Pixels);FilePath = AssetDatabase .GetAssetPath(Tex);Pos = FilePath.LastIndexOf( '.' );FilePath = FilePath.Substring( 0 , Pos);FilePath = FilePath + "_alpha.png" ;bytes = ALphaTex.EncodeToPNG();System.IO. File .WriteAllBytes(FilePath, bytes);{AssetDatabase .ImportAsset(FilePath);TextureImporter TargetTexImporter = AssetImporter .GetAtPath(FilePath) as TextureImporter ;TargetTexImporter.textureType = TexImporter.textureType;TargetTexImporter.filterMode = TexImporter.filterMode;TargetTexImporter.maxTextureSize = TexImporter.maxTextureSize;TargetTexImporter.textureFormat = TextureImporterFormat .AutomaticCompressed;AssetDatabase .ImportAsset(FilePath);}CLogger .Log("Save " + FilePath + " Succeed" );}}
}



</pre><div></div><div><hr /></div><div></div><div><span style="color:rgb(255,0,0)"><span style="font-size:24px">Shader代码,如何将两张RGB图像合并成</span></span><span style="color:rgb(255,0,0)"><span style="font-size:24px">ARGB图像(带alpha)</span></span></div><div><span style="color:rgb(255,0,0)"><span style="font-size:24px"></span></span><pre name="code" class="plain">Shader "Unlit/Transparent Colored SplitTex"
{Properties{_MainTex ("Base (RGB)", 2D) = "black" {}_AlphaTex("Alpha (A)", 2D) = "black" {}}SubShader{LOD 100Tags{"Queue" = "Transparent""IgnoreProjector" = "True""RenderType" = "Transparent"}Cull OffLighting OffZWrite OffFog { Mode Off }Offset -1, -1Blend SrcAlpha OneMinusSrcAlphaPass{CGPROGRAM#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"struct appdata_t{float4 vertex : POSITION;float2 texcoord : TEXCOORD0;fixed4 color : COLOR;};struct v2f{float4 vertex : SV_POSITION;half2 texcoord : TEXCOORD0;fixed4 color : COLOR;fixed gray : TEXCOORD1;};sampler2D _MainTex;sampler2D _AlphaTex;float4 _MainTex_ST;v2f vert (appdata_t v){v2f o;o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);o.texcoord = v.texcoord;o.color = v.color;o.gray = dot(v.color, fixed4(1,1,1,0));return o;}fixed4 frag (v2f i) : COLOR{fixed4 col,alpha;col = tex2D(_MainTex, i.texcoord);alpha = tex2D(_AlphaTex, i.texcoord);col.a = alpha.r;if(i.gray == 0){float grey = dot(col.rgb, float3(0.299, 0.587, 0.114));col.rgb = float3(grey, grey, grey);}else{col = col * i.color;}                   return col;                   }ENDCG}}SubShader{LOD 100Tags{"Queue" = "Transparent""IgnoreProjector" = "True""RenderType" = "Transparent"}Pass{Cull OffLighting OffZWrite OffFog { Mode Off }Offset -1, -1ColorMask RGBAlphaTest Greater .01Blend SrcAlpha OneMinusSrcAlphaColorMaterial AmbientAndDiffuseSetTexture [_MainTex]{Combine Texture * Primary}}}
}






这篇关于Unity中,利用shader将NGUI图集大小缩小四倍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1057646

相关文章

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

SQL Server 查询数据库及数据文件大小的方法

《SQLServer查询数据库及数据文件大小的方法》文章介绍了查询数据库大小的SQL方法及存储过程实现,涵盖当前数据库、所有数据库的总大小及文件明细,本文结合实例代码给大家介绍的非常详细,感兴趣的... 目录1. 直接使用SQL1.1 查询当前数据库大小1.2 查询所有数据库的大小1.3 查询每个数据库的详

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点

Pyserial设置缓冲区大小失败的问题解决

《Pyserial设置缓冲区大小失败的问题解决》本文主要介绍了Pyserial设置缓冲区大小失败的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录问题描述原因分析解决方案问题描述使用set_buffer_size()设置缓冲区大小后,buf

详解C++中类的大小决定因数

《详解C++中类的大小决定因数》类的大小受多个因素影响,主要包括成员变量、对齐方式、继承关系、虚函数表等,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 非静态数据成员示例:2. 数据对齐(Padding)示例:3. 虚函数(vtable 指针)示例:4. 继承普通继承虚继承5.

MySQL8.0设置redo缓存大小的实现

《MySQL8.0设置redo缓存大小的实现》本文主要在MySQL8.0.30及之后版本中使用innodb_redo_log_capacity参数在线更改redo缓存文件大小,下面就来介绍一下,具有一... mysql 8.0.30及之后版本可以使用innodb_redo_log_capacity参数来更改

java获取图片的大小、宽度、高度方式

《java获取图片的大小、宽度、高度方式》文章介绍了如何将File对象转换为MultipartFile对象的过程,并分享了个人经验,希望能为读者提供参考... 目China编程录Java获取图片的大小、宽度、高度File对象(该对象里面是图片)MultipartFile对象(该对象里面是图片)总结java获取图片

C#中图片如何自适应pictureBox大小

《C#中图片如何自适应pictureBox大小》文章描述了如何在C#中实现图片自适应pictureBox大小,并展示修改前后的效果,修改步骤包括两步,作者分享了个人经验,希望对大家有所帮助... 目录C#图片自适应pictureBox大小编程修改步骤总结C#图片自适应pictureBox大小上图中“z轴

深入理解C++ 空类大小

《深入理解C++空类大小》本文主要介绍了C++空类大小,规定空类大小为1字节,主要是为了保证对象的唯一性和可区分性,满足数组元素地址连续的要求,下面就来了解一下... 目录1. 保证对象的唯一性和可区分性2. 满足数组元素地址连续的要求3. 与C++的对象模型和内存管理机制相适配查看类对象内存在C++中,规

PDFQFZ高效定制:印章位置、大小随心所欲

前言 在科技编织的快节奏时代,我们不仅追求速度,更追求质量,让每一分努力都转化为生活的甜蜜果实——正是在这样的背景下,一款名为PDFQFZ-PDF的实用软件应运而生,它以其独特的功能和高效的处理能力,在PDF文档处理领域脱颖而出。 它的开发,源自于对现代办公效率提升的迫切需求。在数字化办公日益普及的今天,PDF作为一种跨平台、不易被篡改的文档格式,被广泛应用于合同签署、报告提交、证书打印等各个