Unity3D日常開發(fā)】(十三)Unity3D中實現(xiàn)熱力圖、風(fēng)向圖、溫度圖效果

      網(wǎng)友投稿 1555 2022-05-30

      推薦閱讀

      CSDN主頁

      GitHub開源地址

      Unity3D插件分享

      簡書地址

      我的個人博客

      QQ群:1040082875

      大家好,我是佛系工程師

      ☆恬靜的小魔龍☆

      ,不定時更新Unity開發(fā)技巧,覺得有用記得一鍵三連哦。

      一、前言

      因一個任務(wù)要完成如何在Unity上面實現(xiàn)熱力圖的效果,所以百度了很久,發(fā)現(xiàn)資料很少,現(xiàn)在就把我總結(jié)的如何在Unity上面基于Canvas實現(xiàn)熱力圖效果的實現(xiàn)過程分享出來, 此前轉(zhuǎn)載了一篇主要講的是如何根據(jù)數(shù)據(jù)值,在Canvas上重新繪制RGBA的值,完成熱力圖的繪制,不過用的是H5寫的,我修改了一下,用C#重寫的

      效果圖:

      項目源文件已經(jīng)上傳CSDN:

      https://download.csdn.net/download/q764424567/13109963

      二、參考資料

      1.基于Canvas的熱力圖繪制方法【http://www.blueidea.com/tech/web/2010/7933.asp 】

      2.Unity(OpenGL)實現(xiàn)“陰陽師畫符”、劃線功能【https://blog.csdn.net/yuanhandsome/article/details/78366250】

      三、正文

      實現(xiàn)過程:

      1.首先從文檔中解析數(shù)據(jù),獲取需要的數(shù)據(jù)(當(dāng)然,這一步也可以在網(wǎng)上獲取數(shù)據(jù),然后再解析)

      2.將獲取的數(shù)據(jù)進行分析

      3.保存貼圖到Texture,將保存的貼圖賦值給Image的Sprite(保存的貼圖也可以賦值給物理對象的貼圖)

      4.清除數(shù)據(jù)

      第一步、解析數(shù)據(jù)

      就以下面這一組數(shù)據(jù)為例

      第一行數(shù)據(jù),代表是的模擬網(wǎng)格 160160

      第二行數(shù)據(jù),模擬半徑 -80km至80km范圍;東西方向(x軸)區(qū)間范圍,西→東

      第三行數(shù)據(jù),劑量值(z軸),即160160網(wǎng)格范圍內(nèi)的最小值和最大值

      第6-164,表示各個網(wǎng)格點的濃度值

      就是說有160*160個數(shù)據(jù),可以用二維數(shù)組去接收數(shù)據(jù)

      然后用一個List數(shù)組去接收解析完的數(shù)據(jù)

      using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.UI; using UnityEngine.EventSystems; public class Draw : MonoBehaviour { //文件路徑名 private string fileName; //讀取數(shù)據(jù) private string str; //保存數(shù)據(jù) private List numberList = new List(); string tempData_Space; string[] tempData_Array; int[] tempData_IntArray; //保存解析完的數(shù)據(jù) private int[,] m_Sum = new int[159, 159]; void Start() { ReadFile(); } //讀指定目錄下的文件 public void ReadFile() { //指定目錄下的文件 fileName = "X:/2015_m01_d01_0000(utc+0800)_l00_csi_1hr_conc.grd"; StreamReader sr = File.OpenText(fileName); while (true) { //按行讀取,str保存的是一行的數(shù)據(jù) str = sr.ReadLine(); int lineLength = 0; if (str != null) { //得到獲取到的這行數(shù)據(jù)的長度 lineLength = str.Length; //過濾掉前幾行和后幾行的數(shù)據(jù) if (lineLength >= 1000) { //SPlit函數(shù)只能分割單個空格,所以兩個空格替換成一個空格 tempData_Space = str.Replace(" ", " "); //分割字符串 tempData_Array = tempData_Space.Split(' '); //保存獲取到的數(shù)據(jù) tempData_IntArray = new int[tempData_Array.Length]; //取后兩位數(shù)據(jù) for (int i = 0; i < tempData_Array.Length; i++) { tempData_IntArray[i] = int.Parse(tempData_Array[i].Substring(8, 2)); } //添加到List數(shù)組中 for (int i = 0; i < tempData_IntArray.Length; i++) { numberList.Add(tempData_IntArray[i]); } } } if (str == null) { break; } } //將List數(shù)組中保存的數(shù)據(jù)賦值給二維數(shù)組 for (int x = 0; x < m_Width; x++) { for (int y = 0; y < m_Height; y++) { int i = y + x * m_Width; m_Sum[x, y] = numberList[i]; } } //關(guān)閉數(shù)據(jù)流 sr.Close(); }

      第二步、分析數(shù)據(jù)

      數(shù)據(jù)保存到m_Sum二維數(shù)組中,剩下的就是從二維數(shù)組中讀取數(shù)據(jù),然后進行分析

      【Unity3d日常開發(fā)】(十三)Unity3D中實現(xiàn)熱力圖、風(fēng)向圖、溫度圖效果

      讀取到的數(shù)據(jù)是最后兩位的數(shù)據(jù),然后將數(shù)據(jù)轉(zhuǎn)成int類型進行對比

      實現(xiàn)代碼

      //對比數(shù)據(jù) public void DataCompare() { //因為二維數(shù)組的數(shù)量是159*159所以就在這個范圍內(nèi)讀取 for (int i = 0; i < 159; i++) { for (int j = 0; j < 159; j++) { //數(shù)據(jù)對比 int temp = m_Sum[i, j]; if (temp <= 6) { Debug.Log("透明"); } else if (temp > 6 && temp <= 7) { Debug.Log("紅色"); } else if (temp > 7 && temp <= 8) { Debug.Log("橘色"); } else if (temp > 8 && temp <= 9) { Debug.Log("黃色"); } else if (temp > 9 && temp <= 10) { Debug.Log("綠色"); } else if (temp > 10 && temp <= 11) { Debug.Log("藍色"); } else if (temp > 11 && temp <= 12) { Debug.Log("靛藍色"); } else if (temp > 12) { Debug.Log("紫色"); } } } }

      第三步、保存貼圖

      最重要的一步,如何將分析過的數(shù)據(jù)生成貼圖,并且賦值給Image呢

      主要就是這兩行的代碼

      //保存貼圖變量 private Texture2D texture; private Image targetMaterial; //創(chuàng)建設(shè)置貼圖文件 void CreatTexture() { texture = new Texture2D(m_Width, m_Height); int targetWidth = m_Width; int targetHeight = m_Height; for (int i = 0; i < targetWidth; i++) { for (int j = 0; j < targetHeight; j++) { int temp = m_Sum[i, j]; if (temp <= 6) { texture.SetPixel(i, j, m_ColorTransparency); } else if (temp > 6 && temp <= 7) { texture.SetPixel(i, j, m_ColorRed); } else if (temp > 7 && temp <= 8) { texture.SetPixel(i, j, m_ColorOrange); } else if (temp > 8 && temp <= 9) { texture.SetPixel(i, j, m_ColorYellow); } else if (temp > 9 && temp <= 10) { texture.SetPixel(i, j, m_ColorGreen); } else if (temp > 10 && temp <= 11) { texture.SetPixel(i, j, m_ColorBlue); } else if (temp > 11 && temp <= 12) { texture.SetPixel(i, j, m_ColorIndigo); } else if (temp > 12) { texture.SetPixel(i, j, m_ColorPurple); } } } //應(yīng)用貼圖 texture.Apply(); //將貼圖數(shù)據(jù)賦值給Image的sprite targetMaterial.sprite = Sprite.Create(texture, new Rect(0, 0, m_Width, m_Height), Vector2.zero); ; }

      第四步、清除數(shù)據(jù)

      清除數(shù)組中的數(shù)據(jù),供下次讀取數(shù)據(jù)

      //清理數(shù)組中的數(shù)據(jù) public void ClearList() { numberList.Clear(); }

      整體代碼

      using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.UI; using UnityEngine.EventSystems; public class Draw : MonoBehaviour { //顏色設(shè)置 public Color m_ColorTransparency; public Color m_ColorRed; public Color m_ColorOrange; public Color m_ColorYellow; public Color m_ColorGreen; public Color m_ColorBlue; public Color m_ColorIndigo; public Color m_ColorPurple; //保存貼圖 private Texture2D texture; private Image targetMaterial; //文件路徑名 private string fileName; //讀取數(shù)據(jù) private string str; //保存數(shù)據(jù) private List numberList = new List(); string tempData_Space; string[] tempData_Array; int[] tempData_IntArray; //保存解析完的數(shù)據(jù) private int[,] m_Sum = new int[159, 159]; //長寬設(shè)置 private int m_Width = 159; private int m_Height = 159; void Start() { targetMaterial = gameObject.GetComponent(); targetMaterial.color = Color.white; ReadFile(); CreatTexture(); ClearList(); } //清理數(shù)組中的數(shù)據(jù) public void ClearList() { numberList.Clear(); } //讀指定目錄下的文件 public void ReadFile() { //指定目錄下的文件 fileName = "X:/2015_m01_d01_0000(utc+0800)_l00_csi_1hr_conc.grd"; StreamReader sr = File.OpenText(fileName); while (true) { //按行讀取,str保存的是一行的數(shù)據(jù) str = sr.ReadLine(); int lineLength = 0; if (str != null) { //得到獲取到的這行數(shù)據(jù)的長度 lineLength = str.Length; //過濾掉前幾行和后幾行的數(shù)據(jù) if (lineLength >= 1000) { //SPlit函數(shù)只能分割單個空格,所以兩個空格替換成一個空格 tempData_Space = str.Replace(" ", " "); //分割字符串 tempData_Array = tempData_Space.Split(' '); //保存獲取到的數(shù)據(jù) tempData_IntArray = new int[tempData_Array.Length]; //取后兩位數(shù)據(jù) for (int i = 0; i < tempData_Array.Length; i++) { tempData_IntArray[i] = int.Parse(tempData_Array[i].Substring(8, 2)); } //添加到List數(shù)組中 for (int i = 0; i < tempData_IntArray.Length; i++) { numberList.Add(tempData_IntArray[i]); } } } if (str == null) { break; } } //將List數(shù)組中保存的數(shù)據(jù)賦值給二維數(shù)組 for (int x = 0; x < m_Width; x++) { for (int y = 0; y < m_Height; y++) { int i = y + x * m_Width; m_Sum[x, y] = numberList[i]; } } //關(guān)閉數(shù)據(jù)流 sr.Close(); } //創(chuàng)建設(shè)置貼圖文件 void CreatTexture() { texture = new Texture2D(m_Width, m_Height); int targetWidth = m_Width; int targetHeight = m_Height; for (int i = 0; i < targetWidth; i++) { for (int j = 0; j < targetHeight; j++) { int temp = m_Sum[i, j]; if (temp <= 6) { texture.SetPixel(i, j, m_ColorTransparency); } else if (temp > 6 && temp <= 7) { texture.SetPixel(i, j, m_ColorRed); } else if (temp > 7 && temp <= 8) { texture.SetPixel(i, j, m_ColorOrange); } else if (temp > 8 && temp <= 9) { texture.SetPixel(i, j, m_ColorYellow); } else if (temp > 9 && temp <= 10) { texture.SetPixel(i, j, m_ColorGreen); } else if (temp > 10 && temp <= 11) { texture.SetPixel(i, j, m_ColorBlue); } else if (temp > 11 && temp <= 12) { texture.SetPixel(i, j, m_ColorIndigo); } else if (temp > 12) { texture.SetPixel(i, j, m_ColorPurple); } } } //應(yīng)用貼圖 texture.Apply(); //將貼圖數(shù)據(jù)賦值給Image的sprite targetMaterial.sprite = Sprite.Create(texture, new Rect(0, 0, m_Width, m_Height), Vector2.zero); ; } }

      四、工程文件

      鏈接:https://pan.baidu.com/s/1akdyDVrwHjQJm9RvD4Rh8w

      提取碼:y4be

      Github地址:

      https://github.com/764424567/Demo_DrawImg

      Gitee地址(嫌棄Github地址下載太慢可以用碼云):

      https://gitee.com/itMonon/Demo_DrawImg

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:Verilog設(shè)計實例(1)線性反饋移位寄存器(LFSR)
      下一篇:20個 CSS 快速提升技巧
      相關(guān)文章
      亚洲日韩乱码久久久久久| 亚洲AV无码AV日韩AV网站| 久久精品夜色噜噜亚洲A∨| 亚洲av无码有乱码在线观看| avtt天堂网手机版亚洲| 亚洲精品综合久久中文字幕| 无码专区—VA亚洲V天堂| 亚洲成AV人片在线观看| 国产亚洲成av片在线观看| 国产亚洲一区二区手机在线观看| 2022中文字字幕久亚洲| 亚洲国产综合无码一区二区二三区 | 亚洲欧洲另类春色校园网站| 亚洲国产精品综合福利专区| 精品亚洲A∨无码一区二区三区| 久久亚洲国产成人精品性色 | 亚洲国产成人久久综合| 亚洲国产精品无码中文lv| 亚洲高清国产拍精品熟女| 亚洲国产成人久久精品大牛影视| 亚洲AV无码一区二区三区久久精品| 一本色道久久综合亚洲精品蜜桃冫| 日韩亚洲国产高清免费视频| 国产AV无码专区亚洲AV毛网站| 国产亚洲情侣一区二区无码AV| 亚洲无码黄色网址| 亚洲无码黄色网址| 永久亚洲成a人片777777| 亚洲码国产精品高潮在线| 国产A在亚洲线播放| 亚洲一区二区电影| 亚洲乱码一二三四区麻豆| 97久久国产亚洲精品超碰热| 亚洲色欲啪啪久久WWW综合网| 亚洲精品无码aⅴ中文字幕蜜桃| 久久亚洲精品成人无码| 亚洲精品乱码久久久久久不卡| 中文字幕亚洲综合久久菠萝蜜| 国产日韩亚洲大尺度高清| 无码乱人伦一区二区亚洲一| 亚洲福利电影一区二区?|