网站首页 > 博客文章 正文
当使用unity开发过程中,需要用到图片压缩的时候,往往会不知所措,下面为压缩图片的算法,仅供参考:
public enum ImageFilterMode : int
{
Nearest = 0,
Biliner = 1,
Average = 2//最优
}
//压缩图片
public Texture2D ResizeTexture(Texture2D pSource, ImageFilterMode pFilterMode, Vector2 newSize)
{
//*** Variables
int i;
//*** Get All the source pixels
Color[] aSourceColor = pSource.GetPixels(0);
Vector2 vSourceSize = new Vector2(pSource.width, pSource.height);
//*** Calculate New Size
float xWidth = Mathf.RoundToInt(newSize.x);
float xHeight = Mathf.RoundToInt(newSize.y);
//*** Make New
Texture2D oNewTex = new Texture2D((int)xWidth, (int)xHeight, TextureFormat.RGBA32, false);
//*** Make destination array
int xLength = (int)xWidth * (int)xHeight;
Color[] aColor = new Color[xLength];
Vector2 vPixelSize = new Vector2(vSourceSize.x / xWidth, vSourceSize.y / xHeight);
//*** Loop through destination pixels and process
Vector2 vCenter = new Vector2();
for (i = 0; i < xLength; i++)
{
//*** Figure out x&y
float xX = (float)i % xWidth;
float xY = Mathf.Floor((float)i / xWidth);
//*** Calculate Center
vCenter.x = (xX / xWidth) * vSourceSize.x;
vCenter.y = (xY / xHeight) * vSourceSize.y;
//*** Do Based on mode
//*** Nearest neighbour (testing)
if (pFilterMode == ImageFilterMode.Nearest)
{
//*** Nearest neighbour (testing)
vCenter.x = Mathf.Round(vCenter.x);
vCenter.y = Mathf.Round(vCenter.y);
//*** Calculate source index
int xSourceIndex = (int)((vCenter.y * vSourceSize.x) + vCenter.x);
//*** Copy Pixel
aColor[i] = aSourceColor[xSourceIndex];
}
//*** Bilinear
else if (pFilterMode == ImageFilterMode.Biliner)
{
//*** Get Ratios
float xRatioX = vCenter.x - Mathf.Floor(vCenter.x);
float xRatioY = vCenter.y - Mathf.Floor(vCenter.y);
//*** Get Pixel index's
int xIndexTL = (int)((Mathf.Floor(vCenter.y) * vSourceSize.x) + Mathf.Floor(vCenter.x));
int xIndexTR = (int)((Mathf.Floor(vCenter.y) * vSourceSize.x) + Mathf.Ceil(vCenter.x));
int xIndexBL = (int)((Mathf.Ceil(vCenter.y) * vSourceSize.x) + Mathf.Floor(vCenter.x));
int xIndexBR = (int)((Mathf.Ceil(vCenter.y) * vSourceSize.x) + Mathf.Ceil(vCenter.x));
//*** Calculate Color
aColor[i] = Color.Lerp(
Color.Lerp(aSourceColor[xIndexTL], aSourceColor[xIndexTR], xRatioX),
Color.Lerp(aSourceColor[xIndexBL], aSourceColor[xIndexBR], xRatioX),
xRatioY
);
}
//*** Average
else if (pFilterMode == ImageFilterMode.Average)
{
//*** Calculate grid around point
int xXFrom = (int)Mathf.Max(Mathf.Floor(vCenter.x - (vPixelSize.x * 0.5f)), 0);
int xXTo = (int)Mathf.Min(Mathf.Ceil(vCenter.x + (vPixelSize.x * 0.5f)), vSourceSize.x);
int xYFrom = (int)Mathf.Max(Mathf.Floor(vCenter.y - (vPixelSize.y * 0.5f)), 0);
int xYTo = (int)Mathf.Min(Mathf.Ceil(vCenter.y + (vPixelSize.y * 0.5f)), vSourceSize.y);
//*** Loop and accumulate
Vector4 oColorTotal = new Vector4();
Color oColorTemp = new Color();
float xGridCount = 0;
for (int iy = xYFrom; iy < xYTo; iy++)
{
for (int ix = xXFrom; ix < xXTo; ix++)
{
//*** Get Color
oColorTemp += aSourceColor[(int)(((float)iy * vSourceSize.x) + ix)];
//*** Sum
xGridCount++;
}
}
//*** Average Color
aColor[i] = oColorTemp / (float)xGridCount;
}
}
//*** Set Pixels
oNewTex.SetPixels(aColor);
oNewTex.Apply();
//*** Return
return oNewTex;
}
猜你喜欢
- 2024-09-09 大厂面试难,进不去?你要找的Unity的答案都在这儿了
- 2024-09-09 灵活运用拍摄技巧,免于来自女朋友的皮肉之苦(Unity-摄像机)
- 2024-09-09 Unity2019基础教程:Unity怎样添加角色?怎样使镜头跟随角色?
- 2024-09-09 【免费教程】解锁创意图片新玩法:黏土与瓷娃娃风格滤镜
- 2024-09-09 Unity自学笔记(1)2d场景的创建及人物移动
- 2024-09-09 Unity引擎2018版发布 渲染品质可媲美电影级别
- 2024-09-09 Unity功能知识点以及功能介绍(unity 介绍)
- 2024-09-09 耐克 Pegasus Turbo Next Nature "Together"官方图片
- 2024-09-09 人工智能写的unity照片滚动播放(unity图片动画)
- 2024-09-09 UNITY这些纹理动画,你值得拥有!(unity2019地形纹理)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)