File size: 9,472 Bytes
41791c1 | 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 | using System;
using UnityEngine;
using UnityEngine.UI;
public class MapMeshProcessor : MonoBehaviour
{
#region Properties
public float[] DiscretizedMapBuffer
{
get { return m_discretizedMapBuffer; }
}
public float[] DiscretizedMapFineBuffer
{
get { return m_discretizedMapFineBuffer; }
}
public int GridSize
{
get { return m_gridSize; }
set { m_gridSize = value; }
}
public Vector3 MaxHeightLocation
{
get { return new Vector3(m_maxHeightMeshLocation[0] * m_scale.x, m_maxHeight * m_scale.z, -m_maxHeightMeshLocation[1] * m_scale.y); }
}
#endregion
#region Messages
void Start()
{
m_scale = transform.localScale;
m_discretizedMapBuffer = new float[m_gridSize * m_gridSize];
UpdateMesh();
UpdateMapGrid();
}
#endregion
#region Methods
public void UpdateMesh()
{
m_currentMesh = GetComponent<MeshFilter>().mesh;
}
public void UpdateMesh(Mesh mesh)
{
m_currentMesh = mesh;
}
/// <summary>
/// Update the discretized map buffers
/// </summary>
public void UpdateMapGrid()
{
m_maxHeight = -10;
int[] triangles = m_currentMesh.triangles;
int pixelSize = m_fineGridSize + 1;
if (m_discretizedMapFineBuffer is null)
{
m_discretizedMapFineBuffer = new float[pixelSize * pixelSize];
}
// Loop over the triangles
for (int t = 0; t < triangles.Length / 3; t++)
{
Vector3 a = m_currentMesh.vertices[m_currentMesh.triangles[t * 3]];
Vector3 b = m_currentMesh.vertices[m_currentMesh.triangles[t * 3 + 1]];
Vector3 c = m_currentMesh.vertices[m_currentMesh.triangles[t * 3 + 2]];
// assume that X-Y plane is the map region and Z is the height
float[] p1 = MapMeshPointToGrid(a, m_fineGridSize);
float[] p2 = MapMeshPointToGrid(b, m_fineGridSize);
float[] p3 = MapMeshPointToGrid(c, m_fineGridSize);
// no negative values. int is same as floor
int x_min = (int)Mathf.Min(p1[0], p2[0], p3[0]);
int x_max = (int)Mathf.Ceil(Mathf.Max(p1[0], p2[0], p3[0]));
int y_min = (int)Mathf.Min(p1[1], p2[1], p3[1]);
int y_max = (int)Mathf.Ceil(Mathf.Max(p1[1], p2[1], p3[1]));
if (x_max > m_fineGridSize) x_max = m_fineGridSize;
if (y_max > m_fineGridSize) y_max = m_fineGridSize;
for (int i = x_min; i <= x_max; i++)
{
for (int j = y_min; j <= y_max; j++)
{
float[] gridPoint = new float[2] { i, j };
if (WithinTriangle(gridPoint, p1, p2, p3))
{
// Unmap grid point (int) to the original mesh coordinate
float[] meshPoint;
meshPoint = UnmapGridLocationToMeshXY(gridPoint);
float height = PlaneInterpolation(a, b, c, meshPoint[0], meshPoint[1]);
m_discretizedMapFineBuffer[i + j * pixelSize] = height;
if (height > m_maxHeight)
{
m_maxHeight = height;
m_maxHeightMeshLocation = meshPoint;
}
}
}
}
}
UpdateDownsampledMapBuffer();
}
/// <summary>
/// Get the height of the map at (x, z) in the current Map GameObject coordinate.
/// Assume that the mesh is -90 rotated around X axis, and the mesh origin coincides with the origin of GameObject.transform
/// </summary>
/// <param name="x">X position in the coordinate of the Map</param>
/// <param name="z">Z position in the coordinate of the Map</param>
/// <returns></returns>
public float GetMapHeight(float x, float z)
{
float[] gridLocation = MapMeshPointToGrid(x / m_scale.x, -z / m_scale.y, m_fineGridSize);
// Naive flooring
int u = (int)gridLocation[0];
int v = (int)gridLocation[1];
// Boundary check
if (u < 0 || v < 0 || u > m_fineGridSize || v > m_fineGridSize)
{
return float.NaN;
}
return m_discretizedMapFineBuffer[u + v * (m_fineGridSize + 1)] * m_scale.y;
}
public void DrawHeightMapInRawImage(bool useFineBuffer = false)
{
float[] buffer;
int pixelSize;
if (useFineBuffer)
{
buffer = m_discretizedMapFineBuffer;
pixelSize = m_fineGridSize + 1;
}
else
{
buffer = m_discretizedMapBuffer;
pixelSize = m_gridSize;
}
float maxHeight = Mathf.Max(buffer);
float minHeight = Mathf.Min(buffer);
Texture2D texture = new Texture2D(pixelSize, pixelSize);
for (int i = 0; i < pixelSize; i++)
{
for (int j = 0; j < pixelSize; j++)
{
float gray = (-minHeight + buffer[i + j * pixelSize]) / (maxHeight - minHeight);
texture.SetPixel(pixelSize - j - 1, pixelSize - i - 1, new Color(gray, gray, gray));
}
}
texture.Apply();
GameObject gameObjectRawImage = GameObject.Find("RawImage");
RawImage rawImage = gameObjectRawImage.GetComponent<RawImage>();
rawImage.texture = texture;
}
#endregion
#region Private
private void UpdateDownsampledMapBuffer()
{
if (m_discretizedMapBuffer is null)
{
m_discretizedMapBuffer = new float[m_gridSize * m_gridSize];
}
int factor = m_fineGridSize / m_gridSize;
for (int x = 0; x < m_gridSize; x++)
{
for (int y = 0; y < m_gridSize; y++)
{
float sum = 0f;
// Calculate the average value for this patch in the original image
for (int i = 0; i < factor; i++)
{
for (int j = 0; j < factor; j++)
{
int originalX = x * factor + i;
int originalY = y * factor + j;
sum += m_discretizedMapFineBuffer[originalX + originalY * (m_fineGridSize + 1)];
}
}
m_discretizedMapBuffer[x + m_gridSize * y] = sum / (factor * factor);
}
}
}
private float[] MapMeshPointToGrid(Vector3 p, int gridSize)
{
float[] gridLocation = new float[2];
// Assume the origin is at the center of the mesh
gridLocation[0] = (p.x / m_meshSize + 0.5f) * gridSize;
gridLocation[1] = (p.y / m_meshSize + 0.5f) * gridSize;
return gridLocation;
}
private float[] MapMeshPointToGrid(float x, float y, int gridSize)
{
float[] gridLocation = new float[2];
// Assume the origin is at the center of the mesh
gridLocation[0] = (x / m_meshSize + 0.5f) * gridSize;
gridLocation[1] = (y / m_meshSize + 0.5f) * gridSize;
return gridLocation;
}
private float[] UnmapGridLocationToMeshXY(float[] gridPoint)
{
float[] meshLocation = new float[2];
meshLocation[0] = (gridPoint[0] / m_fineGridSize - 0.5f) * m_meshSize;
meshLocation[1] = (gridPoint[1] / m_fineGridSize - 0.5f) * m_meshSize;
return meshLocation;
}
private bool WithinTriangle(float[] p, float[] p1, float[] p2, float[] p3)
{
// Calculate area of the main triangle
float mainTriangleArea = TriangleArea(p1, p2, p3);
// Calculate areas of sub-triangles
float triangle1Area = TriangleArea(p, p1, p2);
float triangle2Area = TriangleArea(p, p2, p3);
float triangle3Area = TriangleArea(p, p3, p1);
// Tolerance
float tolerance = 0.0001f;
// Check if sum of sub-triangles' areas is approximately equal to main triangle's area
return Math.Abs(mainTriangleArea - (triangle1Area + triangle2Area + triangle3Area)) <= tolerance;
}
private float TriangleArea(float[] a, float[] b, float[] c)
{
return Mathf.Abs(a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1])) / 2.0f;
}
public static float PlaneInterpolation(Vector3 a, Vector3 b, Vector3 c, float x, float y)
{
Plane plane = new Plane(a, b, c);
// Calculate the vectors relative to point a
Vector3 ab = b - a;
Vector3 ac = c - a;
// Calculate the normal of the plane
Vector3 normal = plane.normal;
float A = normal.x;
float B = normal.y;
float C = normal.z;
// Calculate the constant D
float D = -Vector3.Dot(normal, a);
// Substitute x and y into the plane equation to solve for z
float z = (-D - A * x - B * y) / C;
return z;
}
[NonSerialized]
float[] m_discretizedMapBuffer;
[NonSerialized]
float[] m_discretizedMapFineBuffer;
[NonSerialized]
Mesh m_currentMesh;
[NonSerialized]
Vector3 m_scale;
[NonSerialized]
float m_meshSize = 4.0f;
[NonSerialized]
int m_fineGridSize = 100;
[NonSerialized]
float m_maxHeight;
[NonSerialized]
float[] m_maxHeightMeshLocation;
[SerializeField]
int m_gridSize;
#endregion
}
|