Skip to content

Commit 8f136a4

Browse files
committed
feat: add function to get data based on world rect
1 parent 318de83 commit 8f136a4

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

Runtime/TerrainInfluenceMap.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,34 @@ public Color[] GetDatas()
170170

171171
Rect RemapTerrainRectToLocalRect(in Rect worldRect)
172172
{
173-
Rect locRect = new Rect();
174-
locRect.position = worldRect.position - Position;
175-
locRect.size = worldRect.size / Resolution;
176-
return locRect;
173+
int resolution = Resolution;
174+
Vector3 terrainPos = m_terrain.GetPosition();
175+
Vector2 terrain2DPos = new Vector2(terrainPos.x, terrainPos.z);
176+
177+
TerrainData terrainData = m_terrain.terrainData;
178+
Vector2 localMin2D = (worldRect.min - terrain2DPos) / terrainData.size.x * resolution;
179+
Vector2 localMax2D = (worldRect.max - terrain2DPos) / terrainData.size.x * resolution;
180+
181+
float xMin = Mathf.Ceil(Mathf.Clamp(localMin2D.x, 0f, resolution));
182+
float yMin = Mathf.Ceil(Mathf.Clamp(localMin2D.y, 0f, resolution));
183+
float xMax = Mathf.Ceil(Mathf.Clamp(localMax2D.x, 0f, resolution));
184+
float yMax = Mathf.Ceil(Mathf.Clamp(localMax2D.y, 0f, resolution));
185+
186+
Rect localRect = new Rect(xMin, yMin, xMax - xMin, yMax - yMin);
187+
return localRect;
177188
}
178189

190+
/// <summary>
191+
/// Get pixel based on world rect in x, z coord
192+
/// </summary>
193+
/// <param name="worldRect"></param>
194+
/// <returns>2D Rect based on x, z coord of the world world</returns>
179195
public Color[] GetDatasInWorld(in Rect worldRect)
180196
{
181-
return GetDatas(RemapTerrainRectToLocalRect(worldRect));
197+
Rect localRect = RemapTerrainRectToLocalRect(worldRect);
198+
if ((int)localRect.height * (int)localRect.width == 0)
199+
return null;
200+
return GetDatas(localRect);
182201
}
183202

184203
private void GenerateRenderTexture()

Samples~/Demo/Scripts/GameManager.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -235,33 +235,18 @@ Vector2 GetSelectionStat(Vector2 pos1, Vector2 pos2)
235235
Vector2 rst = Vector2.zero;
236236
float pixelCount = 0;
237237

238+
// transform mouse screen space to world space
238239
Vector3 mousePos1 = m_camera.ScreenToWorldPoint(pos1);
239240
Vector3 mousePos2 = m_camera.ScreenToWorldPoint(pos2);
240241

241242
Vector2 minMousePos = new Vector2(Math.Min(mousePos1.x, mousePos2.x), Math.Min(mousePos1.z, mousePos2.z));
242243
Vector2 maxMousePos = new Vector2(Math.Max(mousePos1.x, mousePos2.x), Math.Max(mousePos1.z, mousePos2.z));
244+
Rect worldRect = Rect.MinMaxRect(minMousePos.x, minMousePos.y, maxMousePos.x, maxMousePos.y);
243245

244246
for (var index = 0; index < terrains.Length; index++)
245247
{
246-
int resolution = InfluenceMapTeam1[index].Resolution;
247-
Vector3 terrainPos = terrains[index].GetPosition();
248-
Vector2 terrain2DPos = new Vector2(terrainPos.x, terrainPos.z);
249-
250-
Vector2 localMin2D = (minMousePos - terrain2DPos) / terrains[index].terrainData.size.x * resolution;
251-
Vector2 localMax2D = (maxMousePos - terrain2DPos) / terrains[index].terrainData.size.x * resolution;
252-
253-
float xMin = Mathf.Ceil(Mathf.Clamp(localMin2D.x, 0f, resolution));
254-
float yMin = Mathf.Ceil(Mathf.Clamp(localMin2D.y, 0f, resolution));
255-
float xMax = Mathf.Ceil(Mathf.Clamp(localMax2D.x, 0f, resolution));
256-
float yMax = Mathf.Ceil(Mathf.Clamp(localMax2D.y, 0f, resolution));
257-
258-
Rect localRect = new Rect(xMin, yMin, xMax - xMin, yMax - yMin);
259-
260-
if ((int)localRect.width * (int)localRect.height == 0)
261-
continue;
262-
263-
Color[] colors1 = InfluenceMapTeam1[index].GetDatas(localRect);
264-
Color[] colors2 = InfluenceMapTeam2[index].GetDatas(localRect);
248+
Color[] colors1 = InfluenceMapTeam1[index].GetDatasInWorld(worldRect);
249+
Color[] colors2 = InfluenceMapTeam2[index].GetDatasInWorld(worldRect);
265250

266251
if (colors1 == null || colors2 == null || colors1.Length != colors2.Length)
267252
continue;

0 commit comments

Comments
 (0)