日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity 代码集锦之图片处理

發(fā)布時間:2025/3/16 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity 代码集锦之图片处理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、將Texture2D保存為jpg

void TestSaveImageToJPG(Texture2D buffer){File.WriteAllBytes("F:/output.jpg", buffer.EncodeToJPG());}

?

2、將圖片的原始數(shù)據(jù)保存至txt

void TestSaveImageToTXT(Texture2D buffer){byte[] rawData= buffer.GetRawTextureData();//byte[] bt = ChangeRGB(rawData).GetRawTextureData();//轉(zhuǎn)換RGB//char[] ch = Encoding.ASCII.GetChars(bt);//byte[]轉(zhuǎn)為char[]StreamWriter sw = new StreamWriter(@"F:\\output.txt"); //保存到指定路徑for (int i = 0; i < rawData.Length; i++){sw.Write(rawData[i]);sw.Write(' ');}sw.Flush();sw.Close();}

 

?

3、轉(zhuǎn)換圖片的RGB

Texture2D ChangeRGB(Texture2D source){Texture2D result = new Texture2D(source.width, source.height, TextureFormat.RGB24, false);for (int i = 0; i < result.height; ++i){for (int j = 0; j < result.width; ++j){Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);float temp = newColor.r;newColor.r = newColor.b;newColor.b = temp;result.SetPixel(j, i, newColor);}}result.Apply();return result;}

  

 

對于大多數(shù)紋理,更快的是使用GetPixels32,它返回低精度顏色數(shù)據(jù),而無需進(jìn)行昂貴的整數(shù)到浮點轉(zhuǎn)換。其中Color數(shù)組是Texture2D從左到右,從下到上的像素

Texture2D ChangeRGB(Texture2D source){Texture2D result = new Texture2D(source.width, source.height, TextureFormat.RGB24, false);for (int i = 0; i < result.height; ++i){for (int j = 0; j < result.width; ++j){result.SetPixel(j, result.height - i, source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height));}}result.Apply();return result;}

  

下面代碼和上面代碼功能是一樣的,都是將圖片上下并鏡像翻轉(zhuǎn),但是速度卻快了近一倍

Texture2D ChangeRGB(Texture2D source){Texture2D result = new Texture2D(source.width, source.height, TextureFormat.RGB24, false);Color32[] sourceColor = source.GetPixels32();Color32[] newColor = new Color32[sourceColor.Length];for (int i = 0; i < result.height; ++i){for (int j = 0; j < result.width; ++j){newColor[(result.width * (result.height - i - 1)) + j] = sourceColor[(result.width * i) + j];}}result.SetPixels32(newColor);result.Apply();return result;}

  

Texture2D ChangeRGB(Texture2D source){Texture2D result = new Texture2D(source.width, source.height, TextureFormat.RGB24, false);Color32[] sourceColor = source.GetPixels32();Color32[] newColor = new Color32[sourceColor.Length];int currentR = 0;//當(dāng)前的行int currentC = 0;//當(dāng)前的列for (int i = 0; i < sourceColor.Length; i++){if (i % result.width == 0){currentR = i / result.width;currentC = 0;}else{currentC++;}newColor[(result.width * (result.height - currentR - 1)) + currentC] = sourceColor[(result.width * currentR) + currentC];}result.SetPixels32(newColor);result.Apply();return result;}

  

附:

//順時針旋轉(zhuǎn)90度Texture2D ChangeRGB(Texture2D source){Texture2D result = new Texture2D( source.height, source.width, TextureFormat.RGB24, false);Color32[] sourceColor = source.GetPixels32();Color32[] newColor = new Color32[sourceColor.Length];for (int i = 0; i < source.height; ++i){for (int j = 0; j < source.width; ++j){newColor[(source.height * (source.width - 1-j)) + i] = sourceColor[(source.width * i) + j];}}result.SetPixels32(newColor);result.Apply();return result;}

  

//逆時針旋轉(zhuǎn)90度Texture2D ChangeRGB(Texture2D source){Texture2D result = new Texture2D( source.height, source.width, TextureFormat.RGB24, false);Color32[] sourceColor = source.GetPixels32();Color32[] newColor = new Color32[sourceColor.Length];for (int i = 0; i < source.height; ++i){for (int j = 0; j < source.width; ++j){newColor[(source.height * j) + (source.height-1-i)] = sourceColor[(source.width * i) + j];}}result.SetPixels32(newColor);result.Apply();return result;}

  

4、將Texture轉(zhuǎn)為Texture2D,參數(shù)可傳入Texture也可傳入WebCamTexture類型的參數(shù),和上述的“1”配合使用,可將攝像頭的數(shù)據(jù)保存為圖片

Texture2D TextureToTexture2D(Texture texture){Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);RenderTexture currentRT = RenderTexture.active;RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);Graphics.Blit(texture, renderTexture);RenderTexture.active = renderTexture;texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);texture2D.Apply();RenderTexture.active = currentRT;RenderTexture.ReleaseTemporary(renderTexture);return texture2D;}

  

?5、屏幕截圖

Texture2D frameBuffer;Rect camRect;frameBuffer = new Texture2D(width, height, TextureFormat.RGB24, false, false);camRect = new Rect(0, 0, width, height);frameBuffer.ReadPixels(camRect, 0, 0);frameBuffer.Apply();

  

  

轉(zhuǎn)載于:https://www.cnblogs.com/Jason-c/p/10812805.html

總結(jié)

以上是生活随笔為你收集整理的Unity 代码集锦之图片处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 五月激情开心网 | 亚洲资源网 | 国产中文字幕在线视频 | 肥老熟妇伦子伦456视频 | www久久久 | 男女做网站 | 国产又大又粗又硬 | 欧美影院在线观看 | 精品少妇人妻AV无码专区在线 | 欧美精品手机在线 | 一区二区三区免费毛片 | 亚洲www啪成人一区二区麻豆 | 国产中文字幕在线免费观看 | 爱情岛论坛亚洲品质自拍视频 | 午夜在线网站 | 国产夫妻自拍小视频 | 福利网站在线观看 | 9cao| 可乐操亚洲 | 午夜小视频在线 | 97久久人国产精品婷婷 | 国产成人无码网站 | 色欲av永久无码精品无码蜜桃 | 免费看污视频的网站 | 奶罩不戴乳罩邻居hd播放 | 精品免费观看 | 九九热在线视频 | 久久亚洲影院 | 天堂一区在线观看 | 久久国产中文字幕 | 九九热在线视频播放 | 性高潮在线观看 | 超碰在线一区 | 亚洲大乳 | 国产av一区二区三区精品 | 精品69| 日韩激情一区二区 | 国产小视频在线播放 | 三级在线观看 | 免费网站91 | 91午夜免费视频 | eeuss鲁丝片一区二区三区 | 中文字幕亚洲乱码 | 美女超碰 | asian日本肉体pics | 韩国电影一区 | 国产91绿帽单男绿奴 | 国产精品爽爽久久 | 亚洲精品综合在线 | 精品一区二区三区在线免费观看 | 天天爽天天爽夜夜爽毛片 | 福利av在线 | 国产精品99精品无码视 | 极品超粉嫩尤物69xx | 夜夜av| 国产精品一区二区三区在线免费观看 | 国偷自产av一区二区三区麻豆 | 日韩一区二区三区在线视频 | 精品一区二区三区在线播放 | 欧洲视频在线观看 | 国产午夜电影 | 一个人看的www日本高清视频 | 色欲久久久天天天精品综合网 | 91精品国产色综合久久不卡98口 | 欧美日本黄色 | 欧美性爱精品一区 | 亚洲一区视频在线播放 | sao浪受的饥渴日常 91免费入口 | 91pao | 91精品免费视频 | 九色porny视频 | 亚洲欧美日韩精品久久亚洲区 | 丝袜美腿av | 最新毛片网 | 美乳人妻一区二区三区 | 91影院在线观看 | 欧美肥老妇 | 日本成人免费在线 | 日韩欧美一级 | 97夜夜| 日韩天天操 | 欧美激情网站 | 欧美色xxxxx 日本精品一区二区三区四区的功能 | 国产成人精品片 | 日本少妇一区 | 国产剧情在线观看 | 美女扒开腿让人桶爽 | 国产一级特黄a高潮片 | 亚洲国产aⅴ成人精品无吗 日韩乱论 | 国产精品视频一区二区三区 | mm1313亚洲国产精品美女 | 一级黄色免费 | 久久亚洲精精品中文字幕早川悠里 | 国产一区二区三区精品视频 | 国产剧情在线一区 | 亚洲AV无码成人国产精品色 | 亚洲精品 日韩无码 | 中文人妻一区二区三区 | 中文字幕亚洲成人 |