Skip to content

Commit 52d8b07

Browse files
committed
feat: add selection of influence
1 parent 7f5f414 commit 52d8b07

File tree

8 files changed

+348
-38
lines changed

8 files changed

+348
-38
lines changed

Runtime/TerrainInfluenceMap.cs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using Unity.Profiling;
44
using UnityEditor;
5+
using UnityEditor.VersionControl;
56
using UnityEngine;
67
using UnityEngine.Assertions;
78
using UnityEngine.Rendering;
@@ -29,6 +30,14 @@ public int Resolution
2930
GenerateRenderTexture();
3031
}
3132
}
33+
34+
public Vector2 Position
35+
{
36+
get
37+
{
38+
return new Vector2(m_terrain.GetPosition().x, m_terrain.GetPosition().z);
39+
}
40+
}
3241
public bool lowPrescision = true;
3342

3443
private List<IInfluencer> m_influencers = new List<IInfluencer>();
@@ -133,26 +142,46 @@ void Update()
133142
}
134143
#endregion
135144

136-
public Color[] GetDatas()
145+
public Color[] GetDatas(Rect localRect)
137146
{
138-
return GetDatas(0, 0, m_renderTexture.width, m_renderTexture.height);
139-
}
147+
Assert.IsFalse(localRect.xMin < 0 || localRect.yMin < 0 || localRect.xMin > m_renderTexture.width ||
148+
localRect.yMin > m_renderTexture.height ||
149+
localRect.width < 0 || localRect.height < 0 || localRect.width > m_renderTexture.width ||
150+
localRect.height > m_renderTexture.height ||
151+
(int) localRect.height * (int) localRect.width == 0);
152+
153+
Texture2D texture = new Texture2D((int)localRect.width, (int)localRect.height, lowPrescision ? TextureFormat.R8 : TextureFormat.R16, false);
140154

141-
public Color[] GetDatas(int x, int y, int width, int height)
142-
{
143-
Texture2D texture = new Texture2D(width, height, lowPrescision ? TextureFormat.R8 : TextureFormat.R16, false);
144-
Rect rectReadPicture = new Rect(0, 0, width, height);
145-
146155
RenderTexture.active = m_renderTexture;
147-
156+
148157
// Read pixels
149-
texture.ReadPixels(rectReadPicture, 0, 0);
158+
localRect.y = m_renderTexture.height - localRect.y - localRect.height; // renderTexture is vertical flipped
159+
texture.ReadPixels(localRect, 0, 0);
150160
texture.Apply();
151-
161+
152162
RenderTexture.active = null; // added to avoid errors
153163
return texture.GetPixels();
154164
}
155165

166+
public Color[] GetDatas()
167+
{
168+
return GetDatas(new Rect(0, 0, m_renderTexture.width, m_renderTexture.height));
169+
}
170+
171+
Rect RemapTerrainRectToLocalRect(in Rect worldRect)
172+
{
173+
Rect locRect = new Rect();
174+
locRect.position = worldRect.position - Position;
175+
locRect.size = worldRect.size / Resolution;
176+
return locRect;
177+
}
178+
179+
public Color[] GetDatasInWorld(in Rect worldRect)
180+
{
181+
182+
return GetDatas(RemapTerrainRectToLocalRect(worldRect));
183+
}
184+
156185
private void GenerateRenderTexture()
157186
{
158187
#if UNITY_EDITOR
@@ -167,8 +196,8 @@ private void GenerateRenderTexture()
167196
m_renderTexture.Create();
168197

169198
#if UNITY_EDITOR
170-
if (m_drawDebug)
171-
m_terrain.materialTemplate.SetTexture(s_shaderPropertyTexture, m_renderTexture);
199+
if (m_drawDebug)
200+
m_terrain.materialTemplate.SetTexture(s_shaderPropertyTexture, m_renderTexture);
172201
#endif
173202
}
174203

@@ -183,7 +212,7 @@ public void UpdateBuffer()
183212

184213
TerrainData terrainData = m_terrain.terrainData;
185214
Vector2 terrainSize = new Vector2(terrainData.size.x, terrainData.size.z);
186-
Vector2 terrainMinPos = new Vector2(m_terrain.GetPosition().x, m_terrain.GetPosition().z);
215+
Vector2 terrainMinPos = Position;
187216
Vector2 terrainMaxPos = terrainMinPos + terrainSize;
188217

189218
foreach (IInfluencer influencer in m_influencers)

Samples~/Demo/Demo.unity

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,12 @@ MonoBehaviour:
262262
m_Script: {fileID: 11500000, guid: 95defaa93e48bc748b130a6887251d30, type: 3}
263263
m_Name:
264264
m_EditorClassIdentifier:
265-
camera: {fileID: 817550761}
265+
m_camera: {fileID: 817550761}
266266
terrains: []
267267
InfluenceMapTeam1: []
268268
InfluenceMapTeam2: []
269269
m_drawDebug: 0
270+
renderTexture: []
270271
--- !u!4 &377053535
271272
Transform:
272273
m_ObjectHideFlags: 0
@@ -293,7 +294,7 @@ GameObject:
293294
m_Layer: 0
294295
m_Name: Team2Start
295296
m_TagString: Untagged
296-
m_Icon: {fileID: 0}
297+
m_Icon: {fileID: 419385456094870383, guid: 0000000000000000d000000000000000, type: 0}
297298
m_NavMeshLayer: 0
298299
m_StaticEditorFlags: 0
299300
m_IsActive: 1
@@ -305,7 +306,7 @@ Transform:
305306
m_PrefabAsset: {fileID: 0}
306307
m_GameObject: {fileID: 475135626}
307308
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
308-
m_LocalPosition: {x: 20, y: 0, z: 0}
309+
m_LocalPosition: {x: 25, y: 0, z: 0}
309310
m_LocalScale: {x: 1, y: 1, z: 1}
310311
m_Children: []
311312
m_Father: {fileID: 0}
@@ -438,15 +439,15 @@ PrefabInstance:
438439
objectReference: {fileID: 0}
439440
- target: {fileID: 490976, guid: 65f2022d55af99148b54465b7b1af222, type: 3}
440441
propertyPath: m_LocalPosition.x
441-
value: 0
442+
value: -50
442443
objectReference: {fileID: 0}
443444
- target: {fileID: 490976, guid: 65f2022d55af99148b54465b7b1af222, type: 3}
444445
propertyPath: m_LocalPosition.y
445446
value: 0
446447
objectReference: {fileID: 0}
447448
- target: {fileID: 490976, guid: 65f2022d55af99148b54465b7b1af222, type: 3}
448449
propertyPath: m_LocalPosition.z
449-
value: -37.36
450+
value: -50
450451
objectReference: {fileID: 0}
451452
- target: {fileID: 490976, guid: 65f2022d55af99148b54465b7b1af222, type: 3}
452453
propertyPath: m_LocalRotation.w
@@ -540,8 +541,8 @@ Camera:
540541
near clip plane: 0.3
541542
far clip plane: 1000
542543
field of view: 60
543-
orthographic: 0
544-
orthographic size: 25
544+
orthographic: 1
545+
orthographic size: 75
545546
m_Depth: -1
546547
m_CullingMask:
547548
serializedVersion: 2
@@ -565,7 +566,7 @@ Transform:
565566
m_PrefabAsset: {fileID: 0}
566567
m_GameObject: {fileID: 817550757}
567568
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071067}
568-
m_LocalPosition: {x: 0, y: 50, z: 0}
569+
m_LocalPosition: {x: 51.52, y: 50, z: 53.59}
569570
m_LocalScale: {x: 1, y: 1, z: 1}
570571
m_Children: []
571572
m_Father: {fileID: 0}
@@ -798,7 +799,7 @@ Transform:
798799
m_PrefabAsset: {fileID: 0}
799800
m_GameObject: {fileID: 2303954083559105647}
800801
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
801-
m_LocalPosition: {x: -20, y: 0, z: 0}
802+
m_LocalPosition: {x: -25, y: 0, z: 0}
802803
m_LocalScale: {x: 1, y: 1, z: 1}
803804
m_Children: []
804805
m_Father: {fileID: 0}

Samples~/Demo/Scripts/Controls/TopCamera.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#endif
77

8+
[RequireComponent(typeof(Camera))]
89
public class TopCamera : MonoBehaviour
910
{
1011
[SerializeField] private int MoveSpeed = 20;
@@ -16,19 +17,29 @@ public class TopCamera : MonoBehaviour
1617
Action<float> OnMoveHorizontal;
1718
Action<float> OnMoveVertical;
1819

20+
private Camera m_camera;
21+
1922
Vector3 Move = Vector3.zero;
2023

2124
private void Start()
2225
{
26+
m_camera = GetComponent<Camera>();
27+
2328
OnMouseScroll += (float value) =>
2429
{
2530
if (value < 0f && transform.position.y < MaxHeight)
2631
{
27-
Move.y += ZoomSpeed * Time.deltaTime;
32+
if (m_camera.orthographic)
33+
m_camera.orthographicSize += ZoomSpeed * Time.deltaTime;
34+
else
35+
Move.y += ZoomSpeed * Time.deltaTime;
2836
}
2937
else if (value > 0f && transform.position.y > MinHeight)
3038
{
31-
Move.y -= ZoomSpeed * Time.deltaTime;
39+
if (m_camera.orthographic)
40+
m_camera.orthographicSize -= ZoomSpeed * Time.deltaTime;
41+
else
42+
Move.y -= ZoomSpeed * Time.deltaTime;
3243
}
3344
};
3445

0 commit comments

Comments
 (0)