Skip to content

Commit 0323323

Browse files
committed
Added a grid to spatial partition pattern
1 parent 0012553 commit 0323323

File tree

4 files changed

+201
-99
lines changed

4 files changed

+201
-99
lines changed

Assets/Patterns/19. Spatial Partition/Grid/Scripts/GameController.cs

+79-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Collections;
21
using System.Collections.Generic;
32
using UnityEngine;
43

@@ -9,37 +8,35 @@ namespace SpatialPartition.Grid
98
public class GameController : MonoBehaviour
109
{
1110
//Drags
12-
public GameObject battlefieldObj;
13-
1411
public Unit unitPrefab;
1512

1613
public Transform unitParentTrans;
1714

1815
//Private
1916
private Grid grid;
2017

21-
//The number of units we start with
18+
//The number of units moving on the map
2219
private const int NUMBER_OF_UNITS = 100;
2320

2421
//To keep track of all units so we can move them
2522
private HashSet<Unit> allUnits = new HashSet<Unit>();
2623

24+
//Display the grid with lines
25+
26+
//Grid material
27+
private Material gridMaterial;
28+
29+
//Grid mesh
30+
private Mesh gridMesh;
31+
2732

2833

2934
void Start()
3035
{
3136
grid = new Grid();
3237

33-
34-
//Make the battlefield the same size as the grid
3538
float battlefieldWidth = Grid.NUM_CELLS * Grid.CELL_SIZE;
3639

37-
battlefieldObj.transform.localScale = new Vector3(battlefieldWidth, 1f, battlefieldWidth);
38-
39-
//The grid starts at origo, so we need to change position as well
40-
battlefieldObj.transform.position = new Vector3(battlefieldWidth * 0.5f, 0f, battlefieldWidth * 0.5f);
41-
42-
4340
//Add units within the grid at random positions
4441
for (int i = 0; i < NUMBER_OF_UNITS; i++)
4542
{
@@ -70,5 +67,75 @@ void Update()
7067
//Units attack each other
7168
grid.HandleMelee();
7269
}
70+
71+
72+
73+
private void LateUpdate()
74+
{
75+
//Display the grid with lines
76+
if (gridMaterial == null)
77+
{
78+
gridMaterial = new Material(Shader.Find("Unlit/Color"));
79+
80+
gridMaterial.color = Color.black;
81+
}
82+
83+
if (grid == null)
84+
{
85+
return;
86+
}
87+
88+
if (gridMesh == null)
89+
{
90+
gridMesh = InitGridMesh();
91+
}
92+
93+
//Display the mesh
94+
Graphics.DrawMesh(gridMesh, Vector3.zero, Quaternion.identity, gridMaterial, 0, Camera.main, 0);
95+
}
96+
97+
98+
99+
private Mesh InitGridMesh()
100+
{
101+
//Generate the vertices
102+
List<Vector3> lineVertices = new ();
103+
104+
float battlefieldWidth = Grid.NUM_CELLS * Grid.CELL_SIZE;
105+
106+
Vector3 linePosX = Vector3.zero;
107+
Vector3 linePosY = Vector3.zero;
108+
109+
for (int x = 0; x <= Grid.NUM_CELLS; x++)
110+
{
111+
lineVertices.Add(linePosX);
112+
lineVertices.Add(linePosX + Vector3.right * battlefieldWidth);
113+
114+
lineVertices.Add(linePosY);
115+
lineVertices.Add(linePosY + Vector3.forward * battlefieldWidth);
116+
117+
linePosX += Vector3.forward * Grid.CELL_SIZE;
118+
linePosY += Vector3.right * Grid.CELL_SIZE;
119+
}
120+
121+
122+
//Generate the indices
123+
List<int> indices = new ();
124+
125+
for (int i = 0; i < lineVertices.Count; i++)
126+
{
127+
indices.Add(i);
128+
}
129+
130+
131+
//Generate the mesh
132+
Mesh gridMesh = new ();
133+
134+
gridMesh.SetVertices(lineVertices);
135+
gridMesh.SetIndices(indices, MeshTopology.Lines, 0);
136+
137+
138+
return gridMesh;
139+
}
73140
}
74141
}

Assets/Patterns/19. Spatial Partition/Grid/grid spatial partition.unity

+50-87
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ RenderSettings:
4343
--- !u!157 &3
4444
LightmapSettings:
4545
m_ObjectHideFlags: 0
46-
serializedVersion: 11
46+
serializedVersion: 12
4747
m_GIWorkflowMode: 0
4848
m_GISettings:
4949
serializedVersion: 2
@@ -54,14 +54,15 @@ LightmapSettings:
5454
m_EnableBakedLightmaps: 1
5555
m_EnableRealtimeLightmaps: 1
5656
m_LightmapEditorSettings:
57-
serializedVersion: 10
57+
serializedVersion: 12
5858
m_Resolution: 2
5959
m_BakeResolution: 40
6060
m_AtlasSize: 1024
6161
m_AO: 0
6262
m_AOMaxDistance: 1
6363
m_CompAOExponent: 1
6464
m_CompAOExponentDirect: 0
65+
m_ExtractAmbientOcclusion: 0
6566
m_Padding: 2
6667
m_LightmapParameters: {fileID: 0}
6768
m_LightmapsBakeMode: 1
@@ -76,20 +77,29 @@ LightmapSettings:
7677
m_PVRDirectSampleCount: 32
7778
m_PVRSampleCount: 500
7879
m_PVRBounces: 2
80+
m_PVREnvironmentSampleCount: 500
81+
m_PVREnvironmentReferencePointCount: 2048
82+
m_PVRFilteringMode: 2
83+
m_PVRDenoiserTypeDirect: 0
84+
m_PVRDenoiserTypeIndirect: 0
85+
m_PVRDenoiserTypeAO: 0
7986
m_PVRFilterTypeDirect: 0
8087
m_PVRFilterTypeIndirect: 0
8188
m_PVRFilterTypeAO: 0
82-
m_PVRFilteringMode: 1
89+
m_PVREnvironmentMIS: 0
8390
m_PVRCulling: 1
8491
m_PVRFilteringGaussRadiusDirect: 1
8592
m_PVRFilteringGaussRadiusIndirect: 5
8693
m_PVRFilteringGaussRadiusAO: 2
8794
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
8895
m_PVRFilteringAtrousPositionSigmaIndirect: 2
8996
m_PVRFilteringAtrousPositionSigmaAO: 1
90-
m_ShowResolutionOverlay: 1
97+
m_ExportTrainingData: 0
98+
m_TrainingDataDestination: TrainingData
99+
m_LightProbeSampleCountMultiplier: 4
91100
m_LightingDataAsset: {fileID: 0}
92-
m_UseShadowmask: 1
101+
m_LightingSettings: {fileID: 4890085278179872738, guid: e997a83e1f1b5d64baa705f6789eaa95,
102+
type: 2}
93103
--- !u!196 &4
94104
NavMeshSettings:
95105
serializedVersion: 2
@@ -109,86 +119,11 @@ NavMeshSettings:
109119
manualTileSize: 0
110120
tileSize: 256
111121
accuratePlacement: 0
122+
maxJobWorkers: 0
123+
preserveTilesOutsideBounds: 0
112124
debug:
113125
m_Flags: 0
114126
m_NavMeshData: {fileID: 0}
115-
--- !u!1 &401042762
116-
GameObject:
117-
m_ObjectHideFlags: 0
118-
m_CorrespondingSourceObject: {fileID: 0}
119-
m_PrefabInstance: {fileID: 0}
120-
m_PrefabAsset: {fileID: 0}
121-
serializedVersion: 6
122-
m_Component:
123-
- component: {fileID: 401042766}
124-
- component: {fileID: 401042765}
125-
- component: {fileID: 401042764}
126-
m_Layer: 0
127-
m_Name: Battlefield
128-
m_TagString: Untagged
129-
m_Icon: {fileID: 0}
130-
m_NavMeshLayer: 0
131-
m_StaticEditorFlags: 0
132-
m_IsActive: 1
133-
--- !u!23 &401042764
134-
MeshRenderer:
135-
m_ObjectHideFlags: 0
136-
m_CorrespondingSourceObject: {fileID: 0}
137-
m_PrefabInstance: {fileID: 0}
138-
m_PrefabAsset: {fileID: 0}
139-
m_GameObject: {fileID: 401042762}
140-
m_Enabled: 1
141-
m_CastShadows: 1
142-
m_ReceiveShadows: 1
143-
m_DynamicOccludee: 1
144-
m_MotionVectors: 1
145-
m_LightProbeUsage: 1
146-
m_ReflectionProbeUsage: 1
147-
m_RenderingLayerMask: 1
148-
m_RendererPriority: 0
149-
m_Materials:
150-
- {fileID: 2100000, guid: ea86c1b73dd97f04f9da616aff12971d, type: 2}
151-
m_StaticBatchInfo:
152-
firstSubMesh: 0
153-
subMeshCount: 0
154-
m_StaticBatchRoot: {fileID: 0}
155-
m_ProbeAnchor: {fileID: 0}
156-
m_LightProbeVolumeOverride: {fileID: 0}
157-
m_ScaleInLightmap: 1
158-
m_PreserveUVs: 0
159-
m_IgnoreNormalsForChartDetection: 0
160-
m_ImportantGI: 0
161-
m_StitchLightmapSeams: 0
162-
m_SelectedEditorRenderState: 3
163-
m_MinimumChartSize: 4
164-
m_AutoUVMaxDistance: 0.5
165-
m_AutoUVMaxAngle: 89
166-
m_LightmapParameters: {fileID: 0}
167-
m_SortingLayerID: 0
168-
m_SortingLayer: 0
169-
m_SortingOrder: 0
170-
--- !u!33 &401042765
171-
MeshFilter:
172-
m_ObjectHideFlags: 0
173-
m_CorrespondingSourceObject: {fileID: 0}
174-
m_PrefabInstance: {fileID: 0}
175-
m_PrefabAsset: {fileID: 0}
176-
m_GameObject: {fileID: 401042762}
177-
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
178-
--- !u!4 &401042766
179-
Transform:
180-
m_ObjectHideFlags: 0
181-
m_CorrespondingSourceObject: {fileID: 0}
182-
m_PrefabInstance: {fileID: 0}
183-
m_PrefabAsset: {fileID: 0}
184-
m_GameObject: {fileID: 401042762}
185-
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
186-
m_LocalPosition: {x: 0, y: 0, z: 0}
187-
m_LocalScale: {x: 1, y: 1, z: 1}
188-
m_Children: []
189-
m_Father: {fileID: 0}
190-
m_RootOrder: 3
191-
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
192127
--- !u!1 &826587778
193128
GameObject:
194129
m_ObjectHideFlags: 0
@@ -199,7 +134,7 @@ GameObject:
199134
m_Component:
200135
- component: {fileID: 826587779}
201136
m_Layer: 0
202-
m_Name: Units
137+
m_Name: Units parent
203138
m_TagString: Untagged
204139
m_Icon: {fileID: 0}
205140
m_NavMeshLayer: 0
@@ -215,9 +150,10 @@ Transform:
215150
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
216151
m_LocalPosition: {x: 0, y: 0, z: 0}
217152
m_LocalScale: {x: 1, y: 1, z: 1}
153+
m_ConstrainProportionsScale: 0
218154
m_Children: []
219155
m_Father: {fileID: 0}
220-
m_RootOrder: 4
156+
m_RootOrder: 3
221157
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
222158
--- !u!1 &1033359570
223159
GameObject:
@@ -244,12 +180,14 @@ Light:
244180
m_PrefabAsset: {fileID: 0}
245181
m_GameObject: {fileID: 1033359570}
246182
m_Enabled: 1
247-
serializedVersion: 8
183+
serializedVersion: 10
248184
m_Type: 1
185+
m_Shape: 0
249186
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
250187
m_Intensity: 1
251188
m_Range: 10
252189
m_SpotAngle: 30
190+
m_InnerSpotAngle: 21.80208
253191
m_CookieSize: 10
254192
m_Shadows:
255193
m_Type: 2
@@ -259,19 +197,41 @@ Light:
259197
m_Bias: 0.05
260198
m_NormalBias: 0.4
261199
m_NearPlane: 0.2
200+
m_CullingMatrixOverride:
201+
e00: 1
202+
e01: 0
203+
e02: 0
204+
e03: 0
205+
e10: 0
206+
e11: 1
207+
e12: 0
208+
e13: 0
209+
e20: 0
210+
e21: 0
211+
e22: 1
212+
e23: 0
213+
e30: 0
214+
e31: 0
215+
e32: 0
216+
e33: 1
217+
m_UseCullingMatrixOverride: 0
262218
m_Cookie: {fileID: 0}
263219
m_DrawHalo: 0
264220
m_Flare: {fileID: 0}
265221
m_RenderMode: 0
266222
m_CullingMask:
267223
serializedVersion: 2
268224
m_Bits: 4294967295
225+
m_RenderingLayerMask: 1
269226
m_Lightmapping: 4
270227
m_LightShadowCasterMode: 0
271228
m_AreaSize: {x: 1, y: 1}
272229
m_BounceIntensity: 1
273230
m_ColorTemperature: 6570
274231
m_UseColorTemperature: 0
232+
m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0}
233+
m_UseBoundingSphereOverride: 0
234+
m_UseViewFrustumForShadowCasterCull: 1
275235
m_ShadowRadius: 0
276236
m_ShadowAngle: 0
277237
--- !u!4 &1033359572
@@ -284,6 +244,7 @@ Transform:
284244
m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261}
285245
m_LocalPosition: {x: 0, y: 500, z: 0}
286246
m_LocalScale: {x: 1, y: 1, z: 1}
247+
m_ConstrainProportionsScale: 0
287248
m_Children: []
288249
m_Father: {fileID: 0}
289250
m_RootOrder: 1
@@ -326,9 +287,10 @@ Camera:
326287
m_ClearFlags: 1
327288
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0}
328289
m_projectionMatrixMode: 1
290+
m_GateFitMode: 2
291+
m_FOVAxisMode: 0
329292
m_SensorSize: {x: 36, y: 24}
330293
m_LensShift: {x: 0, y: 0}
331-
m_GateFitMode: 2
332294
m_FocalLength: 50
333295
m_NormalizedViewPortRect:
334296
serializedVersion: 2
@@ -366,6 +328,7 @@ Transform:
366328
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
367329
m_LocalPosition: {x: 25, y: 765, z: 25}
368330
m_LocalScale: {x: 1, y: 1, z: 1}
331+
m_ConstrainProportionsScale: 0
369332
m_Children: []
370333
m_Father: {fileID: 0}
371334
m_RootOrder: 0
@@ -397,6 +360,7 @@ Transform:
397360
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
398361
m_LocalPosition: {x: 0, y: 0, z: 0}
399362
m_LocalScale: {x: 1, y: 1, z: 1}
363+
m_ConstrainProportionsScale: 0
400364
m_Children: []
401365
m_Father: {fileID: 0}
402366
m_RootOrder: 2
@@ -413,7 +377,6 @@ MonoBehaviour:
413377
m_Script: {fileID: 11500000, guid: a5b106aed7d115048a91fdfcbb84005a, type: 3}
414378
m_Name:
415379
m_EditorClassIdentifier:
416-
battlefieldObj: {fileID: 401042762}
417380
unitPrefab: {fileID: 6404582697255075593, guid: c2075d714081c9944b2ae26c74b36e85,
418381
type: 3}
419382
unitParentTrans: {fileID: 826587779}

0 commit comments

Comments
 (0)