准备灰度图  

1、高程按比例对应hue色相(hsv)生成mesh效果
o.color = float4(hsv2rgb(float3(v.vertex.y/100.0, 0.5, 0.75)), 1.0);

unity shader

  1. Shader "Unlit/vertexColor 1"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. }
  7. SubShader
  8. {
  9. Tags { "RenderType"="Opaque" }
  10. LOD 100
  11.  
  12. Pass
  13. {
  14. CGPROGRAM
  15. #pragma vertex vert
  16. #pragma fragment frag
  17. // make fog work
  18. #pragma multi_compile_fog
  19. #include "UnityCG.cginc"
  20.  
  21. struct appdata
  22. {
  23. float4 vertex : POSITION;
  24. float2 uv : TEXCOORD0;
  25. };
  26.  
  27. struct v2f
  28. {
  29. float2 uv : TEXCOORD0;
  30. UNITY_FOG_COORDS(1)
  31. float4 vertex : SV_POSITION;
  32. float4 color:COLOR;
  33. };
  34.  
  35. sampler2D _MainTex;
  36. float4 _MainTex_ST;
  37. float3 hsv2rgb(float3 c)
  38. {
  39. float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
  40. float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
  41. return c.z * lerp(K.xxx, saturate(p - K.xxx), c.y);
  42. }
  43.  
  44.  
  45. v2f vert (appdata v)
  46. {
  47. v2f o;
  48. o.vertex = UnityObjectToClipPos(v.vertex);
  49. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  50. o.color = float4(hsv2rgb(float3(v.vertex.y/100.0, 0.5, 0.75)), 1.0);
  51. UNITY_TRANSFER_FOG(o,o.vertex);
  52. return o;
  53. }
  54. fixed4 frag (v2f i) : SV_Target
  55. {
  56. // sample the texture
  57. //fixed4 col = tex2D(_MainTex, i.uv);
  58. fixed4 col = i.color;
  59. // apply fog
  60. UNITY_APPLY_FOG(i.fogCoord, col);
  61. return col;
  62. }
  63. ENDCG
  64. }
  65. }
  66. }

  

2、高程按比例对应色带生成mesh效果

准备色带图

unity shder

  1. Shader "Unlit/colorRamp"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. _ColorRamp("Color Ramp", 2D) = "white" {}
  7. }
  8. SubShader
  9. {
  10. Tags { "RenderType"="Opaque" }
  11. LOD 100
  12.  
  13. Pass
  14. {
  15. CGPROGRAM
  16. #pragma vertex vert
  17. #pragma fragment frag
  18. // make fog work
  19. #pragma multi_compile_fog
  20. #include "UnityCG.cginc"
  21.  
  22. struct appdata
  23. {
  24. float4 vertex : POSITION;
  25. float2 uv : TEXCOORD0;
  26. };
  27.  
  28. struct v2f
  29. {
  30. float2 uv : TEXCOORD0;
  31. UNITY_FOG_COORDS(1)
  32. float4 vertex : SV_POSITION;
  33. float2 colorUV : TEXCOORD1;
  34. };
  35.  
  36. sampler2D _MainTex;
  37. sampler2D _ColorRamp;
  38. float4 _MainTex_ST;
  39. v2f vert (appdata v)
  40. {
  41. v2f o;
  42. o.vertex = UnityObjectToClipPos(v.vertex);
  43. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  44. UNITY_TRANSFER_FOG(o,o.vertex);
  45. o.colorUV = float2(v.vertex.y / 100.0,0);
  46. return o;
  47. }
  48. fixed4 frag (v2f i) : SV_Target
  49. {
  50. fixed4 col = tex2D(_ColorRamp,i.colorUV);
  51. // apply fog
  52. UNITY_APPLY_FOG(i.fogCoord, col);
  53. return col;
  54. }
  55. ENDCG
  56. }
  57. }
  58. }

  

mesh创建脚本

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class meshCreate2 : MonoBehaviour {
  6. private Texture textureGray;//灰度图
  7. private Texture textureGrass;//草地贴图
  8. private int tGrayWidth = 0, tGrayHeight = 0;//灰度图的宽和高
  9. private bool bCreate = false;//是否完成创建
  10. private List<GameObject> meshList;//mesh集合
  11. private Texture2D texture2dGray;
  12. public float zScale = 100;//高度参数
  13.  
  14. [Tooltip("传入mesh使用的材质")]
  15. public Material meshMaterial;
  16.  
  17. void Start()
  18. {
  19. StartCoroutine(loadImage("IGray.png", (t) => textureGray = t));
  20. StartCoroutine(loadImage("IGrass.jpg", (t) => textureGrass = t));
  21. meshList = new List<GameObject>();
  22. }
  23.  
  24. void Update()
  25. {
  26. if (textureGray != null && textureGrass != null)
  27. {
  28. if (bCreate == false)
  29. {
  30. tGrayWidth = textureGray.width;
  31. tGrayHeight = textureGray.height;
  32. meshMaterial.mainTexture = textureGrass;//设置材质贴图
  33. //mesh顶点数目最大65000,则取mes为250*250=62500
  34. int xNum = 1 + tGrayWidth / 250;//x方向mesh个数
  35. int zNum = 1 + tGrayHeight / 250; //z方向mesh个数
  36. texture2dGray = (Texture2D)textureGray;
  37. //根据灰度图创建mesh
  38. for (int i = 0; i < xNum; i++)
  39. {
  40. for (int j = 0; j < zNum; j++)
  41. {
  42. if (i < xNum - 1 && j < zNum - 1)
  43. {
  44. meshList.Add(
  45. createMesh("meshX" + i.ToString() + "Z" + j.ToString(), 251, 251,
  46. i * new Vector3(2500, 0, 0) + j * new Vector3(0, 0,2500),
  47. (i + 1) * new Vector3(2500, 0, 0) + (j + 1) * new Vector3(0, 0,2500) + new Vector3(10, 0,10),
  48. i * new Vector2(250, 0) + j * new Vector2(0, 250),
  49. (i + 1) * new Vector2(250, 0) + (j + 1) * new Vector2(0, 250) + new Vector2(1, 1)));
  50. }
  51. else if (i == xNum - 1 && j < zNum - 1)
  52. {
  53. meshList.Add(createMesh("meshX" + i.ToString() + "Z" + j.ToString(), tGrayWidth % 250, 251,
  54. i * new Vector3(2500, 0, 0) + j * new Vector3(0, 0,2500),
  55. i * new Vector3(2500, 0, 0) + new Vector3(10 * (tGrayWidth % 250), 0,10) + (j + 1) * new Vector3(0, 0,2500),
  56. i * new Vector2(250, 0) + j * new Vector2(0, 250),
  57. i * new Vector2(250, 0) + new Vector2(tGrayWidth % 250, 1) + (j + 1) * new Vector2(0, 250)));
  58. }
  59. else if (i < xNum - 1 && j == zNum - 1)
  60. {
  61. meshList.Add(createMesh("meshX" + i.ToString() + "Z" + j.ToString(), 251, tGrayHeight % 250,
  62. i * new Vector3(2500, 0, 0) + j * new Vector3(0, 0,2500),
  63. (i + 1) * new Vector3(2500, 0, 0) + j * new Vector3(0, 0,2500) + new Vector3(10, 0, 10 * (tGrayHeight % 250)),
  64. i * new Vector2(250, 0) + j * new Vector2(0, 250),
  65. (i + 1) * new Vector2(250, 0) + j * new Vector2(0, 150) + new Vector2(1, tGrayHeight % 250)));
  66. }
  67. else if (i == xNum - 1 && j == zNum - 1)
  68. {
  69. meshList.Add(createMesh("meshX" + i.ToString() + "Z" + j.ToString(), tGrayWidth % 250, tGrayHeight % 250,
  70. i * new Vector3(2500, 0, 0) + j * new Vector3(0, 0, 2500),
  71. i * new Vector3(2500, 0, 0) + j * new Vector3(0, 0, 2500) + new Vector3(10 * (tGrayWidth % 250), 0, 10 * (tGrayHeight % 250)),
  72. i * new Vector2(250, 0) + j * new Vector2(0, 250),
  73. i * new Vector2(250, 0) + j * new Vector2(0, 250) + new Vector2(tGrayWidth % 250, tGrayHeight % 250)));
  74. }
  75. }
  76. }
  77. bCreate = true;
  78. }
  79. }
  80. }
  81.  
  82. //加载图片
  83. IEnumerator loadImage(string imagePath, System.Action<Texture> action)
  84. {
  85. WWW www = new WWW("file://" + Application.streamingAssetsPath + "/" + imagePath);
  86. yield return www;
  87. if (www.error == null)
  88. {
  89. action(www.texture);
  90. }
  91. }
  92.  
  93.  
  94. /// <summary>
  95. ///创建mesh
  96. /// </summary>
  97. /// <param name="meshName">mesh名称</param>
  98. /// <param name="row">行数</param>
  99. /// <param name="col">列数</param>
  100. /// <param name="minPoint">最小点位置</param>
  101. /// <param name="maxPoint">最大点位置</param>
  102. /// <param name="minImgPosition">最小点灰度图位置</param>
  103. /// <param name="maxImgPosition">最大点灰度图位置</param>
  104. /// <returns></returns>
  105. ///
  106.  
  107.  
  108. private GameObject createMesh(string meshName, int row, int col, Vector3 minPoint, Vector3 maxPoint, Vector2 minImgPosition, Vector2 maxImgPosition)
  109. {
  110. GameObject meshObject = new GameObject(meshName);
  111.  
  112. int verticeNum = row * col;
  113. Vector3[] vertices = new Vector3[verticeNum];//顶点数组大小
  114. int[] triangles = new int[verticeNum * 3 * 2];//三角集合数组,保存顶点索引
  115. // Vector3[] normals = new Vector3[verticeNum];//顶点法线数组大小
  116. Vector2[] uvs = new Vector2[verticeNum];
  117. float rowF = (float)row;
  118. float colF = (float)col;
  119. Vector3 xStep = new Vector3((maxPoint.x - minPoint.x) / rowF, 0, 0);
  120. Vector3 zSetp = new Vector3(0, 0, (maxPoint.z - minPoint.z) / colF);
  121. int k = 0;
  122.  
  123. for (int i = 0; i < row; i++)
  124. {
  125. for (int j = 0; j < col; j++)
  126. {
  127. float tempZ = texture2dGray.GetPixel((int)minImgPosition.x + i, (int)minImgPosition.y + j).grayscale;
  128. vertices[i + j * row] = minPoint + xStep * i + zSetp * j + new Vector3(0, tempZ * zScale,0);
  129.  
  130. uvs[i + j * row] = new Vector2((float)i / rowF, (float)j / colF);
  131.  
  132. if (j < col - 1 && i < row - 1)
  133. {
  134. triangles[k++] = j * row + i;
  135. triangles[k++] = j * row + i + row;
  136. triangles[k++] = j * row + i + 1;
  137.  
  138. triangles[k++] = j * row + i + row;
  139. triangles[k++] = j * row + i + row + 1;
  140. triangles[k++] = j * row + i + 1;
  141. }
  142. }
  143. }
  144. Mesh mesh = new Mesh();
  145. mesh.vertices = vertices;
  146. mesh.triangles = triangles;
  147. // mesh.normals = normals;
  148. mesh.uv = uvs;
  149. mesh.RecalculateBounds();
  150. mesh.RecalculateNormals();
  151. meshObject.AddComponent<MeshFilter>();
  152. meshObject.AddComponent<MeshRenderer>();
  153. meshObject.GetComponent<MeshFilter>().mesh = mesh;
  154. meshObject.GetComponent<MeshRenderer>().material = meshMaterial;
  155.  
  156. return meshObject;
  157. }
  158. }

  相机漫游控制脚本

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MyCameraControl : MonoBehaviour
  6. {
  7. public Camera mainCam;
  8. //旋转变量;
  9. private float m_deltX = 0f;
  10. private float m_deltY = 0f;
  11. //缩放变量;
  12. private float m_distance = 10f;
  13. private float m_mSpeed = 5f;
  14. //移动变量;
  15. private Vector3 m_mouseMovePos = Vector3.zero;
  16. //平移速度
  17. float Speed = 200f;
  18.  
  19. void Start()
  20. {
  21. // transform.localPosition = new Vector3(0, m_distance, 0);
  22. }
  23.  
  24. void Update()
  25. {
  26.  
  27. if (Input.GetKey(KeyCode.W))
  28. {
  29. transform.Translate(Vector3.forward * Time.deltaTime * Speed);
  30. }
  31. if (Input.GetKey(KeyCode.A))
  32. {
  33. transform.Translate(Vector3.left * Time.deltaTime * Speed);
  34. }
  35. if (Input.GetKey(KeyCode.S))
  36. {
  37. transform.Translate(Vector3.forward * Time.deltaTime * -Speed);
  38. }
  39. if (Input.GetKey(KeyCode.D))
  40. {
  41. transform.Translate(Vector3.left * Time.deltaTime * -Speed);
  42. }
  43. //鼠标左键控制旋转
  44. if (Input.GetMouseButton(0))
  45. {
  46. m_deltX += Input.GetAxis("Mouse X") * m_mSpeed;
  47. m_deltY -= Input.GetAxis("Mouse Y") * m_mSpeed;
  48. m_deltX = ClampAngle(m_deltX, -360, 360);//旋转幅度 左右
  49. m_deltY = ClampAngle(m_deltY, -70, 70);//旋转幅度 上下
  50. transform.rotation = Quaternion.Euler(m_deltY, m_deltX, 0);
  51. }
  52. //鼠标滑轮缩放
  53. if (Input.GetAxis("Mouse ScrollWheel") != 0)
  54. {
  55. //自由缩放方式;
  56. m_distance = Input.GetAxis("Mouse ScrollWheel") * 10f;
  57. transform.localPosition = transform.position + transform.forward * m_distance;
  58. }
  59.  
  60. //相机位置跳到点击处;
  61. if (Input.GetMouseButtonDown(1)) //0-左键 1-右键 2-滑轮
  62. {
  63. Ray ray = mainCam.ScreenPointToRay(Input.mousePosition);//从摄像机发出到点击坐标的射线
  64. RaycastHit hitInfo;
  65. if (Physics.Raycast(ray, out hitInfo))
  66. {
  67. m_mouseMovePos = hitInfo.point;
  68. transform.localPosition = m_mouseMovePos;
  69. }
  70. }
  71.  
  72. }
  73.  
  74. float ClampAngle(float angle, float minAngle, float maxAgnle)
  75. {
  76. if (angle <= -360)
  77. angle += 360;
  78. if (angle >= 360)
  79. angle -= 360;
  80.  
  81. return Mathf.Clamp(angle, minAngle, maxAgnle);
  82. }
  83.  
  84.  
  85. }

 

本文链接

https://www.cnblogs.com/gucheng/p/10945429.html

 

版权声明:本文为gucheng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/gucheng/p/10945429.html