TrueSync
TSTerrainCollider.cs
1 using UnityEngine;
2 
3 namespace TrueSync {
4 
8  [AddComponentMenu("TrueSync/Physics/TerrainCollider", 0)]
9  [RequireComponent(typeof(Terrain))]
10  public class TSTerrainCollider : TSCollider {
11 
15  public int Resolution {
16  get {
17  var terrain = GetComponent<Terrain>();
18  var data = terrain.terrainData;
19  int resolusion = data.heightmapResolution;
20  return resolusion;
21  }
22  }
23 
27  public Vector3 Size {
28  get {
29  var terrain = GetComponent<Terrain>();
30  var data = terrain.terrainData;
31  return data.size;
32  }
33  }
34 
38  public override Shape CreateShape() {
39  var terrain = GetComponent<Terrain>();
40  var data = terrain.terrainData;
41  int resolusion = data.heightmapResolution;
42  var heightsFloat = data.GetHeights(0, 0, resolusion, resolusion);
43  FP[,] heights = new FP[heightsFloat.GetLength(0), heightsFloat.GetLength(1)];
44 
45  for (int indexI = 0; indexI < heightsFloat.GetLength(0); indexI++) {
46  for (int indexJ = 0; indexJ < heightsFloat.GetLength(1); indexJ++) {
47  heights[indexI, indexJ] = heightsFloat[indexI, indexJ];
48  }
49  }
50 
51  FP verticalScale = data.size.y;
52  for (int x = 0; x < resolusion; x++) {
53  for (int z = 0; z < resolusion; z++)
54  heights[x, z] *= verticalScale;
55  }
56  for (int x = 0; x < resolusion - 1; x++) {
57  for (int z = x; z < resolusion; z++) {
58  FP h1 = heights[x, z];
59  FP h2 = heights[z, x];
60  heights[x, z] = h2;
61  heights[z, x] = h1;
62  }
63  }
64 
65  var result = new TerrainShape(heights, data.size.x / (resolusion - 1), data.size.z / (resolusion - 1));
66  return result;
67  }
68 
69  protected override Vector3 GetGizmosSize() {
70  return Vector3.one;
71  }
72 
73  protected override void DrawGizmos() {
74  }
75 
76  }
77 
78 }
Shape Shape
Shape used by a collider.
Definition: TSCollider.cs:19
Collider with a terrain shape.
override void DrawGizmos()
Draws the specific gizmos of concrete collider (for example "Gizmos.DrawWireCube" for a TSBoxCollider...
Abstract collider for 3D shapes.
Definition: TSCollider.cs:12
override Shape CreateShape()
Creates a terrain shape based on a Terrain component attached.
override Vector3 GetGizmosSize()
Returns the gizmos size.
Vector3 Size
Terrain&#39;s size.
int Resolution
Terrain&#39;s resolution.