Unity地形割草砍树

前段时间最近懒癌发作,业余时间什么都不相干,最近开始整理之前的工作

有需求是砍树,要求可以针对单棵树,暂无存档需求,后来又追加了割草

由于之前做过运行时的地形编辑器,所以对地形还算熟悉

割草

  1. 草的数据存在DetailLayer中,根据Layer逐像素单独存储密度,以图片的形式存储每个Layer对应一种草
  2. 准备好替换用的草,构建映射表,草分两次砍光,第一次砍半截
  3. 输入位置和半径,逐Layer获取区块数据并缓存,清空当前区块密度
  4. Layer将映射表的草密度替换成缓存的值

用到的方法:

1
2
data.GetDetailLayer(xBase, yBase, width, height, layer);
data.SetDetailLayer(xBase, yBase, newLayer, newMap);

砍树

跟割草不同的是,树所有的实例都存在一个List里,所以只要搞定位置判断就可以,难点反而是是碰撞检测

需要注意的有两点

  1. 必须刷新区块的高度数据才能刷新地形碰撞器 ~~
  2. 不管预制是什么Layer地形检测时返回的全是地形的Layer

5.6 及后续版本,无法通过刷新高度数据更新碰撞器,需要重新激活碰撞器来刷新碰撞器

核心代码:

1
2
3
List<TreeInstance> treeList = new List<TreeInstance>(terrain.terrainData.treeInstances);
treeList.RemoveAt(index);
terrain.terrainData.treeInstances = treeList.ToArray();

实现代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using MM = Wonderm.MessageSystem.MessageManager;

namespace TerrainNs
{
[RequireComponent(typeof(Terrain))]
public class TerrainChopCtr : MonoBehaviour
{
[SerializeField]
private bool backupTerrainData = true;

[Header("Tree")]
[SerializeField]
private GameObject fallingTreePrefab;

[SerializeField, Range(0, 50)]
private int treeHealth = 1;

[SerializeField, Range(1, 20)]
private int treeRadius = 5;

[Header("Grass")]
[SerializeField]
//[Label("替换的后缀")]
private string grassExtra;

[SerializeField, Range(1, 20)]
private float grassHeightThreshold = 2;

private Terrain terrain;
private TerrainData data;
private TreeInstance[] treeInstances;
private int index = -1;
private Vector3 pos;
private float rot;
private GameObject treeObj;
private int treeId;
private Dictionary<int, int> treeHealthDic;
private Dictionary<int, int> layerDateDic;

private void Start()
{
terrain = GetComponent<Terrain>();
if (backupTerrainData)
{
var collider = GetComponent<TerrainCollider>();
terrain.terrainData = Instantiate<TerrainData>(terrain.terrainData);
collider.terrainData = terrain.terrainData;
}
data = terrain.terrainData;
Init();

//MM.Instance.Subscribe(MMConst.TerrainTreeChop, TreeChop);
//MM.Instance.Subscribe(MMConst.TerrainGrassChop, GrassChop);
}

private void Init()
{
if (treeHealthDic == null)
{
treeHealthDic = new Dictionary<int, int>();
}
if (layerDateDic == null)
{
layerDateDic = new Dictionary<int, int>();
}
layerDateDic.Clear();
for (int layer = 0; layer < data.detailPrototypes.Length; layer++)
{
int index = GetReplaceLayer(layer);
if (index != -1)
{
layerDateDic.Add(layer, index);
}
}
}

private void OnDestroy()
{

//MM.Instance.Unsubscribe(MMConst.TerrainTreeChop, TreeChop);
//MM.Instance.Unsubscribe(MMConst.TerrainGrassChop, GrassChop);
}

#if UNITY_EDITOR
public enum ChopType
{
None,
Tree,
Detail,
}
[Header("Test")]
[SerializeField]
private ChopType chopType = ChopType.None;
[SerializeField]
private LayerMask mask;
[SerializeField, Range(0, 128)]
private float grassRadius;
private RaycastHit hit;
private void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, mask.value))
{
Debug.DrawLine(ray.origin, hit.point, Color.blue);
if (Input.GetMouseButtonDown(0))
{
if (chopType == ChopType.Tree)
{
TreeChop(hit.point);
}
else if (chopType == ChopType.Detail)
{
Mowing(hit.point, (int)grassRadius);
}
}
}
}
#endif

#region Tree

public void TreeChop(object[] obj)
{
TreeChop((Vector3)obj[0]);
}

public void TreeChop(Vector3 point)
{
Profiler.BeginSample("TreeChop-----SetHeights");
if (HasTree(point, terrain.terrainData))
{
if (treeHealthDic.ContainsKey(treeId))
{
treeHealthDic[treeId]--;
}
else
{
treeHealthDic.Add(treeId, treeHealth);
}
if (treeHealthDic[treeId] > 0)
{
return;
}

#region Create Tree

var parent = new GameObject("FallDownTree");
parent.transform.position = pos;
parent.transform.rotation = Quaternion.Euler(0, Mathf.Rad2Deg * rot, 0);
parent.transform.localScale = treeObj.transform.localScale;
//var tw = Pathea.GameUtils.AddChild(parent, fallingTreePrefab);
//Pathea.GameUtils.AddChild(tw, treeObj);
//Pathea.Times.MonoTimer.Instance.RunTask(() => { Destroy(parent); }, 3);
List<TreeInstance> treeList = new List<TreeInstance>(terrain.terrainData.treeInstances);
treeList.RemoveAt(index);
terrain.terrainData.treeInstances = treeList.ToArray();

#endregion Create Tree

#region Flush Terrain

Vector3 offset = point - terrain.transform.position;
int xBase = Mathf.Clamp((int)offset.x, 0, terrain.terrainData.heightmapWidth);
int yBase = Mathf.Clamp((int)offset.z, 0, terrain.terrainData.heightmapWidth);
int w = Mathf.Clamp(treeRadius, 0, terrain.terrainData.heightmapWidth - xBase);
int h = Mathf.Clamp(treeRadius, 0, terrain.terrainData.heightmapHeight - yBase);
float[,] heights = terrain.terrainData.GetHeights(xBase, yBase, w, h);
terrain.terrainData.SetHeights(xBase, yBase, heights);

#endregion Flush Terrain
}
}

private bool HasTree(Vector3 point, TerrainData data)
{
Profiler.BeginSample("TreeChop-----HasTree");
index = -1;
treeInstances = data.treeInstances;
float minDis = 100;
for (int i = 0; i < treeInstances.Length; i++)
{
var tree = treeInstances[i];
var prefab = data.treePrototypes[tree.prototypeIndex];
var sps = prefab.prefab.GetComponentsInChildren<SphereCollider>();
var treePos = Vector3.Scale(tree.position, data.size) + terrain.GetPosition();
foreach (var sp in sps)
{
var dis = Vector3.Distance(treePos + sp.transform.localPosition + sp.center, point);
if (dis <= sp.radius && dis < minDis)
{
minDis = dis;
index = i;
pos = treePos;
rot = tree.rotation;
treeObj = prefab.prefab;
treeId = tree.position.GetHashCode();
Debug.Log(treeObj.name + "dis:" + dis + "\trot" + rot);
}
}
var ccs = prefab.prefab.GetComponentsInChildren<CapsuleCollider>();
foreach (var cc in ccs)
{
var from = treePos + cc.transform.localPosition + cc.center + cc.transform.up * (cc.height - cc.radius) / 2;
var to = treePos + cc.transform.localPosition + cc.center - cc.transform.up * (cc.height - cc.radius) / 2;
var dis = DisPoint2Line(point, from, to);
if (dis <= cc.radius && dis < minDis)
{
minDis = dis;
index = i;
pos = treePos;
rot = tree.rotation;
treeObj = prefab.prefab;
treeId = tree.position.GetHashCode();
Debug.Log(treeObj.name + "\tdis:" + dis + "\trot" + rot);
}
}
}
Profiler.EndSample();
return index != -1;
}

#endregion Tree

#region Detail

public void GrassChop(object[] obj)
{
Mowing((Vector3)obj[0], (int)obj[1]);
}

public void Mowing(Vector3 point, int radius)
{
var h = terrain.SampleHeight(point);
//Debug.Log("terrainH:\t" + h + "\nterrainY:\t" + terrain.transform.position.y + "\nPointY:\t" + point.y);
if (Mathf.Abs(h + transform.position.y - point.y) > grassHeightThreshold)
{
return;
}
int mapSize = terrain.terrainData.detailResolution;
if (terrain.terrainData.size.x != terrain.terrainData.size.z)
{
Debug.LogError("X and Y Size of terrain have to be the same ");
return;
}
float PrPxSize = mapSize / terrain.terrainData.size.x;
Vector3 texturePoint3D = point - terrain.transform.position;
texturePoint3D = texturePoint3D * PrPxSize;
Vector2 xz = new Vector2(texturePoint3D.x, texturePoint3D.z);
Mowing(1, xz, radius);
Mowing(2, xz, radius);
}

private void Mowing(int order, Vector2 xz, int radius)
{
int xBase = Mathf.Clamp((int)xz.x - radius, 0, data.detailWidth);
int yBase = Mathf.Clamp((int)xz.y - radius, 0, data.detailHeight);
int width = Mathf.Clamp(2 * radius, 0, data.detailWidth - xBase);
int height = Mathf.Clamp(2 * radius, 0, data.detailHeight - yBase);
for (int layer = 0; layer < data.detailPrototypes.Length; layer++)
{
if (data.detailPrototypes[layer].prototypeTexture == null)
{
continue;
}
bool has = data.detailPrototypes[layer].prototypeTexture.name.Contains(grassExtra);
if (order == 1 && !has)
{
continue;
}
if (order == 2 && has)
{
continue;
}
int newLayer = -1;
if (layerDateDic.ContainsKey(layer))
{
newLayer = layerDateDic[layer];
}
var map = data.GetDetailLayer(xBase, yBase, width, height, layer);
int[,] newMap = null;
if (newLayer != -1)
{
newMap = data.GetDetailLayer(xBase, yBase, width, height, newLayer);
}
Vector2 p = Vector2.zero;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
p.x = x + xBase;
p.y = y + yBase;
int cache = map[y, x];
if (cache == 0)
{
continue;
}
if (Vector2.Distance(xz, p) < radius)
{
map[y, x] = 0;
}
if (newLayer != -1)
{
newMap[y, x] = cache;
}
}
}
data.SetDetailLayer(xBase, yBase, layer, map);
if (newLayer != -1)
data.SetDetailLayer(xBase, yBase, newLayer, newMap);
}
}

private int GetReplaceLayer(int layer)
{
if (data.detailPrototypes[layer].prototypeTexture == null)
{
return -1;
}
string layerName = data.detailPrototypes[layer].prototypeTexture.name + grassExtra;
for (int i = 0; i < data.detailPrototypes.Length; i++)
{
if (data.detailPrototypes[i].prototypeTexture == null)
{
continue;
}
if (data.detailPrototypes[i].prototypeTexture.name == layerName)
{
return i;
}
}
return -1;
}

#endregion Detail

public static float DisPoint2Line(Vector3 point, Vector3 start, Vector3 end)
{
Vector3 vec1 = point - start;
Vector3 vec2 = end - start;
Vector3 vecProj = Vector3.Project(vec1, vec2);
float dis = Mathf.Sqrt(Mathf.Pow(Vector3.Magnitude(vec1), 2) - Mathf.Pow(Vector3.Magnitude(vecProj), 2));
return dis;
}
}
}