TrueSync: TSTerrainCollider.cs Source File

TrueSync

TSTerrainCollider.cs
1 using UnityEngine;
2 using TrueSync.Physics3D;
3 
4 namespace TrueSync {
5 
9  [AddComponentMenu("TrueSync/Physics/TerrainCollider", 0)]
10  [RequireComponent(typeof(Terrain))]
11  public class TSTerrainCollider : TSCollider {
12 
16  public int Resolution {
17  get {
18  var terrain = GetComponent<Terrain>();
19  var data = terrain.terrainData;
20  int resolusion = data.heightmapResolution;
21  return resolusion;
22  }
23  }
24 
28  public Vector3 Size {
29  get {
30  var terrain = GetComponent<Terrain>();
31  var data = terrain.terrainData;
32  return data.size;
33  }
34  }
35 
39  public override Shape CreateShape() {
40  var terrain = GetComponent<Terrain>();
41  var data = terrain.terrainData;
42  int resolusion = data.heightmapResolution;
43  var heightsFloat = data.GetHeights(0, 0, resolusion, resolusion);
44  FP[,] heights = new FP[heightsFloat.GetLength(0), heightsFloat.GetLength(1)];
45 
46  for (int indexI = 0; indexI < heightsFloat.GetLength(0); indexI++) {
47  for (int indexJ = 0; indexJ < heightsFloat.GetLength(1); indexJ++) {
48  heights[indexI, indexJ] = heightsFloat[indexI, indexJ];
49  }
50  }
51 
52  FP verticalScale = data.size.y;
53  for (int x = 0; x < resolusion; x++) {
54  for (int z = 0; z < resolusion; z++)
55  heights[x, z] *= verticalScale;
56  }
57  for (int x = 0; x < resolusion - 1; x++) {
58  for (int z = x; z < resolusion; z++) {
59  FP h1 = heights[x, z];
60  FP h2 = heights[z, x];
61  heights[x, z] = h2;
62  heights[z, x] = h1;
63  }
64  }
65 
66  var result = new TerrainShape(heights, data.size.x / (resolusion - 1), data.size.z / (resolusion - 1));
67  return result;
68  }
69 
70  protected override Vector3 GetGizmosSize() {
71  return Vector3.one;
72  }
73 
74  protected override void DrawGizmos() {
75  }
76 
77  }
78 
79 }
Shape Shape
Shape used by a collider.
Definition: TSCollider.cs:20
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:13
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.
Generated by   doxygen 1.8.11