本文主要是介绍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图集大小缩小四倍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!