-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjustTerrainDetail.cs
More file actions
112 lines (95 loc) · 4.54 KB
/
AdjustTerrainDetail.cs
File metadata and controls
112 lines (95 loc) · 4.54 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using UnityEngine;
using System.Collections.Generic;
public class AdjustTerrainDetail : MonoBehaviour
{
#region Configuration
// Minimum values for detailObjectDistance and heightmapPixelError
public float minDetailObjectDistance = 30;
public float minHeightmapPixelError = 3;
// List of tuples to store the default values for detailObjectDistance and heightmapPixelError for each terrain
private readonly List<(float, float)> defaultValues = new List<(float, float)>();
#endregion
#region Object References
// List to store references to all the terrains in the scene
[System.NonSerialized]
List<Terrain> terrains;
// Reference to the player's transform
[System.NonSerialized]
Transform playerTransform;
#endregion
#region MonoBehaviours
private void Start()
{
// Find all terrains in the scene
terrains = new List<Terrain>(FindObjectsOfType<Terrain>());
// Find the player's transform
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
// Store the default values for detailObjectDistance and heightmapPixelError for each terrain
foreach (Terrain terrain in terrains)
{
defaultValues.Add((terrain.detailObjectDistance, terrain.heightmapPixelError));
}
}
// Calculate the distance between the player and the nearest edge of the terrain they are on
private void Update()
{
// Find the terrain that the player is on
Terrain terrain = FindTerrain(playerTransform.position);
// Calculate the distance from the player to the nearest edge of the terrain
float distanceToEdge = CalculateDistanceToEdge(playerTransform.position, terrain);
// Update the terrain detail settings based on the player's position
terrain.detailObjectDistance = Mathf.Max(distanceToEdge * 0.5f, minDetailObjectDistance);
terrain.heightmapPixelError = Mathf.Max(distanceToEdge * 0.1f, minHeightmapPixelError);
}
private void OnDestroy()
{
// Loop through all the terrains in the scene
for (int i = 0; i < terrains.Count; i++)
{
// Reset the detailObjectDistance and heightmapPixelError values for the terrain to their default values
terrains[i].detailObjectDistance = defaultValues[i].Item1;
terrains[i].heightmapPixelError = defaultValues[i].Item2;
}
}
#endregion
#region Custom Functions
// This function finds the terrain that the specified position is on
private Terrain FindTerrain(Vector3 position)
{
// Loop through all the terrains in the scene
foreach (Terrain terrain in terrains)
{
Vector3 center = terrain.transform.position + new Vector3(terrain.terrainData.size.x / 2, 0, terrain.terrainData.size.z / 2);
// Check if the position is within the bounds of the terrain
if (position.x < center.x + terrain.terrainData.size.x / 2 &&
position.x > center.x - terrain.terrainData.size.x / 2 &&
position.y < center.y + terrain.terrainData.size.y / 2 &&
position.y > center.y - terrain.terrainData.size.y / 2 &&
position.z < center.z + terrain.terrainData.size.z / 2 &&
position.z > center.z - terrain.terrainData.size.z / 2)
{
// Position is within the bounds of the terrain, so return a reference to the terrain
return terrain;
}
}
// No terrain was found at the specified position, so return null
return null;
}
// This function calculates the distance between the specified position and the nearest edge of the specified terrain
private float CalculateDistanceToEdge(Vector3 position, Terrain terrain)
{
// Calculate the distance to the nearest edge along the x axis
float distanceToEdgeX = Mathf.Min(
position.x - (terrain.transform.position.x - terrain.terrainData.size.x / 2),
(terrain.transform.position.x + terrain.terrainData.size.x / 2) - position.x
);
// Calculate the distance to the nearest edge along the z axis
float distanceToEdgeZ = Mathf.Min(
position.z - (terrain.transform.position.z - terrain.terrainData.size.z / 2),
(terrain.transform.position.z + terrain.terrainData.size.z / 2) - position.z
);
// Return the minimum distance to the nearest edge along either axis
return Mathf.Min(distanceToEdgeX, distanceToEdgeZ);
}
#endregion
}