-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathRTOGridOrigin.cs
More file actions
94 lines (79 loc) · 2.46 KB
/
RTOGridOrigin.cs
File metadata and controls
94 lines (79 loc) · 2.46 KB
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
//Copyright SimBlocks LLC 2016-2022
//https://www.simblocks.io/
//The source code in this file is licensed under the MIT License. See the LICENSE text file for full terms.
using sbio.Core.Math;
using UnityEngine;
namespace sbio.owsdk.Unity
{
/// <summary>
/// Controls the RTO context by dividing the world into a 3D grid and
/// putting the origin at the closest grid position to this transform
/// </summary>
public sealed class RTOGridOrigin : MonoBehaviour
{
#region MonoBehaviour
public WorldContext WorldContext;
public Transform Origin;
public int GridSize = 4000;
public void OnEnable()
{
CheckWorldOrigin();
}
public void LateUpdate()
{
CheckWorldOrigin();
}
public void OnDisable()
{
WorldContext.WorldOrigin = Vec3LeftHandedGeocentric.Zero;
}
#endregion
private void CalculateGridOffset(Vector3 pos, out int xOffset, out int yOffset, out int zOffset)
{
xOffset = yOffset = zOffset = 0;
var gridExtent = GridSize / 2;
if(pos.x > gridExtent)
{
xOffset = GridSize + (int)(pos.x / GridSize) * GridSize;
pos.x = -GridSize + pos.x % GridSize;
}
else if(pos.x < -gridExtent)
{
xOffset = -GridSize + (int)(pos.x / GridSize) * GridSize;
pos.x = GridSize + pos.x % GridSize;
}
if (pos.y > gridExtent)
{
yOffset = GridSize + (int)(pos.y / GridSize) * GridSize;
pos.y = -GridSize + pos.y % GridSize;
}
else if (pos.y < -gridExtent)
{
yOffset = -GridSize + (int)(pos.y / GridSize) * GridSize;
pos.y = GridSize + pos.y % GridSize;
}
if (pos.z > gridExtent)
{
zOffset = GridSize + (int)(pos.z / GridSize) * GridSize;
pos.z = -GridSize + pos.z % GridSize;
}
else if (pos.z < -gridExtent)
{
zOffset = -GridSize + (int)(pos.z / GridSize) * GridSize;
pos.z = GridSize + pos.z % GridSize;
}
}
private void CheckWorldOrigin()
{
int xOffset, yOffset, zOffset;
CalculateGridOffset(Origin.position, out xOffset, out yOffset, out zOffset);
if (xOffset != 0 || yOffset != 0 || zOffset != 0)
{
WorldContext.WorldOrigin += new Vec3LeftHandedGeocentric(xOffset, yOffset, zOffset);
}
}
}
}
//Copyright SimBlocks LLC 2016-2022
//https://www.simblocks.io/
//The source code in this file is licensed under the MIT License. See the LICENSE text file for full terms.