接着上次的二维面和三维面功能,在借助了高亮插件的点缀之后,能够达到一个让人满意的三维线效果。
最近研究了一下对Unity自身的地形做高度控制的一些东西,有一点小小的成果,现在就分享给大家。
其实在很久以前我就研究过在Unity里如何对模型进行顶点控制的一些东西,并能够实现点筛选和挖坑。只是很大程度上挖坑的精确度取决于模型网格的疏密程度。如果网格顶点过于密集的话,就会影响运算的效率。关于挖模型的东西以后有机会再整理一下发上来吧。
先上图:
1.点选多边形区域,用的是上次的那个三维线,加上了高亮插件的效果。还不错吧。
2.点击执行塌陷,多边形区域内的地形向下塌陷。
先说一下我大体上的思路吧:
1.在画多边形区域的时候,把所有顶点(Vector3)都记录在一个list里。
2.执行塌陷,遍历地形中所有的像素点判断是否在多边形区域内,如果在区域内,变化顶点高度,如果不在区域内,不予处理。
3.在这里我的筛选用了一个点是否在多边形内的算法。就是从这个点向多边形发射线,如果交点的个数是基数,则表示这个点在多边形内部,如果交点的个数是偶数,则表示该点在多边形外部。这个算法对于凸多边形和凹多边形同样成立。
4.在做高度控制的时候,还需要对地形的API有所了解。
5.对了,里边还有一个根据高度数据生成地貌的东东,大致的内容就是读取TXT,然后根据TXT里的高度信息,给地形赋值。
大致的核心内容就这些吧,至于一些具体的处理细节,看代码吧。
Digging脚本:
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Digging : MonoBehaviour
- {
- public DrawMeshLine meshline;
- Terrain ter;
- void Start ()
- {
- ter = GetComponent<Terrain>();
- }
- void Update ()
- {
- }
- void OnGUI ()
- {
- if(GUILayout.Button("塌陷"))
- {
- TerDigging(meshline.LinePointContainer,ter);
- }
- }
- //地形塌陷
- void TerDigging (List<Vector3> Points,Terrain ter)
- {
- float[,] tempHeight = new float[1,1];
- float[,,] tempAlpha = new float[1,1,1];
- tempAlpha[0,0,0] = 1.0f;
- tempHeight[0,0] = 0.0f;
- for(int i = 0 ; i < ter.terrainData.heightmapWidth ; i ++)
- {
- for(int j = 0 ; j < ter.terrainData.heightmapHeight ; j ++)
- {
- float terHeight = ter.terrainData.GetHeight(i,j);
- Vector3 terPointPos = new Vector3(i,terHeight,j);
- if(isPointInPolygon(terPointPos,Points))
- {
- ter.terrainData.SetHeights(i,j,tempHeight);
- ter.terrainData.SetAlphamaps(i,j,tempAlpha);
- }
- }
- }
- }
- //判断点是否在一个多边形内
- bool isPointInPolygon (Vector3 Point,List<Vector3> Points)
- {
- int j=0, cnt = 0;
- for (int i = 0; i < Points.Count; i++)
- {
- j = (i == Points.Count - 1) ? 0 : j + 1;
- if ((Points[i].z!=Points[j].z)&&(((Point.z >= Points[i].z) && (Point.z < Points[j].z)) || ((Point.z >= Points[j].z) && (Point.z < Points[i].z))) && (Point.x < (Points[j].x - Points[i].x) * (Point.z - Points[i].z) / (Points[j].z - Points[i].z) + Points[i].x)) cnt++;
- }
- return (cnt%2>0)?true:false;
- }
- }
高度控制代码里的Alphamap是为了以后改进用的,在当前并没有用到。
TerrainCreate脚本:
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- public class TerrainCreate : MonoBehaviour
- {
- Terrain ter;
- int mapWidth;
- int mapHeight;
- StreamReader sr;
- void Start ()
- {
- //获取当前地形实例
- ter = GetComponent<Terrain>();
- //获取地形的宽和高的分辨率
- mapWidth = ter.terrainData.heightmapWidth;
- mapHeight = ter.terrainData.heightmapHeight;
- print(mapWidth + " " + mapHeight);
- //地形高度二维数组
- float[,] heights = new float[mapWidth,mapHeight];
- List<float> tempHeight = new List<float>();
- //读取TXT坐标点
- print(Application.dataPath);
- sr = File.OpenText(Application.dataPath + @"\dx.asc");
- //TODO...
- int ncols = int.Parse(sr.ReadLine().Replace("ncols","").Trim());
- int nrows = int.Parse(sr.ReadLine().Replace("nrows","").Trim());
- // print(ncols + " " + nrows);
- for(int i = 0 ; i < 4 ; i ++)
- {
- sr.ReadLine();
- //TODO...
- }
- //遍历地形上的每个像素点
- for(int i = 0 ; i < nrows ; i ++)
- {
- string lineInfo = sr.ReadLine();
- string[] lineString = lineInfo.Split(new char[]{' '});
- for(int j = 0 ; j < ncols ; j ++)
- {
- if(lineString[j].Trim() != "")
- {
- float temp = 0.0f;
- float.TryParse(lineString[j],out temp);
- if(temp == -9999)temp = 200.0f;
- heights[i,j] = temp;
- tempHeight.Add(temp);
- }
- }
- }
- tempHeight.Sort();
- for(int ii = 0 ; ii < nrows ; ii ++)
- {
- for(int jj = 0 ; jj < ncols ; jj ++)
- {
- heights[ii,jj] = heights[ii,jj] / NormalHeight(tempHeight[tempHeight.Count - 1]);
- }
- }
- //地形高度赋值
- ter.terrainData.SetHeights(0,0,heights);
- }
- //给高度一个标准的数量级,避免因为高度过大超出地形的高度限制
- int NormalHeight (float temp)
- {
- int num = 0;
- while(temp > 1)
- {
- num ++;
- temp = temp / 10;
- }
- return (int)Mathf.Pow(10,num);
- }
- }
当然,现在做的这个只是个雏形,东西太简陋,算法还没有经过优化,只是能够勉强实现这个功能,但是在效率和实用性方面还达不到要求。
代码写的也是漏洞百出,不过我相信,只要思路是对的,稍加改进,一定是个很好的功能。