Performance Optimization
Improved volumetric flow performance by orders of magnitude Cleaned up old raymarching support code Moved asteroid height calculation to vertex shader Manual culling for asteroid field render update
This commit is contained in:
parent
2e0af36eef
commit
be5e7618d6
52 changed files with 1150 additions and 967 deletions
|
|
@ -58,7 +58,7 @@ public class MiningTool : Behavior
|
|||
var belt = Entity.Zone.AsteroidBelts[AsteroidBelt];
|
||||
if (AsteroidBelt != Guid.Empty &&
|
||||
Entity.Zone.AsteroidExists(AsteroidBelt, Asteroid) &&
|
||||
length(Entity.Position.xz - belt.Positions[Asteroid].xz) - belt.Scales[Asteroid] < Range)
|
||||
length(Entity.Position.xz - belt.Transforms[Asteroid].xy) - belt.Transforms[Asteroid].w < Range)
|
||||
{
|
||||
Entity.Zone.MineAsteroid(
|
||||
Entity,
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ public class ResourceScanner : Behavior, IAlwaysUpdatedBehavior
|
|||
{
|
||||
if(Asteroid > -1 &&
|
||||
Asteroid < beltData.Asteroids.Length &&
|
||||
length(Entity.Position.xz - Entity.Zone.AsteroidBelts[ScanTarget].Positions[Asteroid].xz) < Range)
|
||||
length(Entity.Position.xz - Entity.Zone.AsteroidBelts[ScanTarget].Transforms[Asteroid].xy) < Range)
|
||||
{
|
||||
_scanTime += dt;
|
||||
if (_scanTime > ScanDuration)
|
||||
|
|
|
|||
|
|
@ -138,11 +138,9 @@ public class Zone
|
|||
|
||||
foreach (var belt in AsteroidBelts)
|
||||
{
|
||||
Array.Copy(belt.Value.NewPositions, belt.Value.Positions, belt.Value.Positions.Length);
|
||||
Array.Copy(belt.Value.NewVelocities, belt.Value.Velocities, belt.Value.Velocities.Length);
|
||||
Array.Copy(belt.Value.NewScales, belt.Value.Scales, belt.Value.Scales.Length);
|
||||
Array.Copy(belt.Value.NewRotations, belt.Value.Rotations, belt.Value.Rotations.Length);
|
||||
BeltUpdates.Add(Task.Run(() => UpdateAsteroidTransformsAsync(belt.Key)));
|
||||
Array.Copy(belt.Value.NewTransforms, belt.Value.Transforms, belt.Value.Transforms.Length);
|
||||
belt.Value.OrbitPosition = belt.Value.NewOrbitPosition;
|
||||
BeltUpdates.Add(Task.Run(() => UpdateAsteroidTransforms(belt.Key)));
|
||||
}
|
||||
|
||||
foreach(var agent in Agents)
|
||||
|
|
@ -200,7 +198,7 @@ public class Zone
|
|||
{
|
||||
var beltData = Planets[planetDataID] as AsteroidBeltData;
|
||||
|
||||
var asteroidPositions = AsteroidBelts[planetDataID].Positions;
|
||||
var asteroidPositions = AsteroidBelts[planetDataID].Transforms;
|
||||
|
||||
int nearest = 0;
|
||||
float nearestDistance = Single.MaxValue;
|
||||
|
|
@ -219,14 +217,14 @@ public class Zone
|
|||
|
||||
public bool AsteroidExists(Guid planetDataID, int asteroid) => ((AsteroidBeltData) Planets[planetDataID]).Asteroids.Length > asteroid && asteroid >= 0;
|
||||
|
||||
private void UpdateAsteroidTransformsAsync(Guid planetDataID)
|
||||
private void UpdateAsteroidTransforms(Guid planetDataID)
|
||||
{
|
||||
var beltData = Planets[planetDataID] as AsteroidBeltData;
|
||||
|
||||
var belt = AsteroidBelts[planetDataID];
|
||||
|
||||
var orbitData = Orbits[beltData.Orbit].Data;
|
||||
var orbitPosition = GetOrbitPosition(orbitData.Parent);
|
||||
belt.NewOrbitPosition = GetOrbitPosition(orbitData.Parent);
|
||||
for (var i = 0; i < beltData.Asteroids.Length; i++)
|
||||
{
|
||||
float size;
|
||||
|
|
@ -239,12 +237,11 @@ public class Zone
|
|||
}
|
||||
else size = Settings.AsteroidSize.Evaluate(beltData.Asteroids[i].Size);
|
||||
|
||||
belt.NewRotations[i] = (float) (_time * beltData.Asteroids[i].RotationSpeed % (PI * 2));
|
||||
belt.NewScales[i] = size;
|
||||
var rot = (float) (_time * beltData.Asteroids[i].RotationSpeed % (PI * 2));
|
||||
var pos = OrbitData.Evaluate((float) frac(_time / Settings.OrbitPeriod.Evaluate(beltData.Asteroids[i].Distance) +
|
||||
beltData.Asteroids[i].Phase)) * beltData.Asteroids[i].Distance + orbitPosition;
|
||||
belt.NewVelocities[i] = pos - belt.Positions[i].xz;
|
||||
belt.NewPositions[i] = float3(pos.x, GetHeight(pos) + Settings.AsteroidVerticalOffset, pos.y);
|
||||
beltData.Asteroids[i].Phase)) * beltData.Asteroids[i].Distance + belt.NewOrbitPosition;
|
||||
//belt.NewPositions[i] = float3(pos.x, GetHeight(pos) + Settings.AsteroidVerticalOffset, pos.y);
|
||||
belt.NewTransforms[i] = float4(pos.x, pos.y, rot, size);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -470,14 +467,11 @@ public class Sun : GasGiant
|
|||
public class AsteroidBelt
|
||||
{
|
||||
public AsteroidBeltData Data;
|
||||
public float3[] Positions; // x, y, rotation, scale
|
||||
public float3[] NewPositions; // x, y, rotation, scale
|
||||
public float2[] Velocities;
|
||||
public float2[] NewVelocities;
|
||||
public float[] Rotations;
|
||||
public float[] NewRotations;
|
||||
public float[] Scales;
|
||||
public float[] NewScales;
|
||||
public float4[] Transforms; // x, y, rotation, scale
|
||||
public float4[] NewTransforms; // x, y, rotation, scale
|
||||
public float Radius { get; }
|
||||
public float2 OrbitPosition;
|
||||
public float2 NewOrbitPosition;
|
||||
public Dictionary<int, float> RespawnTimers = new Dictionary<int, float>();
|
||||
public Dictionary<int, float> Damage = new Dictionary<int, float>();
|
||||
public Dictionary<(Entity, int), float> MiningAccumulator = new Dictionary<(Entity, int), float>();
|
||||
|
|
@ -485,14 +479,9 @@ public class AsteroidBelt
|
|||
public AsteroidBelt(AsteroidBeltData data)
|
||||
{
|
||||
Data = data;
|
||||
Positions = new float3[data.Asteroids.Length];
|
||||
NewPositions = new float3[data.Asteroids.Length];
|
||||
Velocities = new float2[data.Asteroids.Length];
|
||||
NewVelocities = new float2[data.Asteroids.Length];
|
||||
Rotations = new float[data.Asteroids.Length];
|
||||
NewRotations = new float[data.Asteroids.Length];
|
||||
Scales = new float[data.Asteroids.Length];
|
||||
NewScales = new float[data.Asteroids.Length];
|
||||
Transforms = new float4[data.Asteroids.Length];
|
||||
NewTransforms = new float4[data.Asteroids.Length];
|
||||
Radius = data.Asteroids.Max(a => a.Distance);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ using float2 = Unity.Mathematics.float2;
|
|||
|
||||
public class ZoneRenderer : MonoBehaviour
|
||||
{
|
||||
public Camera MainCamera;
|
||||
public Transform WormholePrefab;
|
||||
public float EntityFadeTime;
|
||||
public Transform FogCameraParent;
|
||||
|
|
@ -169,6 +170,8 @@ public class ZoneRenderer : MonoBehaviour
|
|||
|
||||
void Start()
|
||||
{
|
||||
var bigBounds = new Bounds(Vector3.zero, Vector3.one * 1024);
|
||||
foreach (var mesh in AsteroidMeshes) mesh.Mesh.bounds = bigBounds;
|
||||
ViewDistance = Settings.DefaultViewDistance;
|
||||
MinimapDistance = Settings.MinimapZoomLevels[Settings.DefaultMinimapZoom];
|
||||
|
||||
|
|
@ -462,29 +465,42 @@ public class ZoneRenderer : MonoBehaviour
|
|||
// {
|
||||
// SlimeRenderer.SpawnPositions[i] = _suns[i].Body.transform.position.Flatland();
|
||||
// }
|
||||
|
||||
Shader.SetGlobalFloat("_AsteroidVerticalOffset", ActionGameManager.Instance.Settings.PlanetSettings.AsteroidVerticalOffset);
|
||||
|
||||
foreach (var belt in Zone.AsteroidBelts)
|
||||
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(MainCamera);
|
||||
bool isVisible(Bounds bounds) => GeometryUtility.TestPlanesAABB(planes, bounds);
|
||||
|
||||
foreach (var (key, belt) in Zone.AsteroidBelts)
|
||||
{
|
||||
var meshes = _beltMeshes[belt.Key];
|
||||
var count = belt.Value.Positions.Length / meshes.Length;
|
||||
for (int i = 0; i < meshes.Length; i++)
|
||||
var height = Zone.GetHeight(belt.OrbitPosition);
|
||||
if(isVisible(new Bounds(
|
||||
new Vector3(belt.OrbitPosition.x,height,belt.OrbitPosition.y),
|
||||
new Vector3(belt.Radius * 2,100,belt.Radius * 2))))
|
||||
{
|
||||
for (int t = 0; t < _beltMatrices[belt.Key][i].Length; t++)
|
||||
var meshes = _beltMeshes[key];
|
||||
var matrices = _beltMatrices[key];
|
||||
var count = belt.Transforms.Length / meshes.Length;
|
||||
for (int i = 0; i < meshes.Length; i++)
|
||||
{
|
||||
var tx = t + i * count;
|
||||
_beltMatrices[belt.Key][i][t] = Matrix4x4.TRS(belt.Value.Positions[tx],
|
||||
Quaternion.Euler(
|
||||
cos(belt.Value.Rotations[tx] + (float) i / meshes.Length) * 100,
|
||||
sin(belt.Value.Rotations[tx] + (float) i / meshes.Length) * 100,
|
||||
(float) tx / belt.Value.Positions.Length * 360),
|
||||
Vector3.one * belt.Value.Scales[tx]);
|
||||
}
|
||||
for (int t = 0; t < matrices[i].Length; t++)
|
||||
{
|
||||
var tx = t + i * count;
|
||||
var transform = belt.Transforms[tx];
|
||||
matrices[i][t] = Matrix4x4.TRS(new Vector3(transform.x,0,transform.y),
|
||||
Quaternion.Euler(
|
||||
cos(transform.z + (float)i / meshes.Length) * 100,
|
||||
sin(transform.z + (float)i / meshes.Length) * 100,
|
||||
(float)tx / belt.Transforms.Length * 360),
|
||||
Vector3.one * transform.w);
|
||||
}
|
||||
|
||||
Graphics.DrawMeshInstanced(meshes[i].Mesh, 0, meshes[i].Material, _beltMatrices[belt.Key][i]);
|
||||
Graphics.DrawMeshInstanced(meshes[i].Mesh, 0, meshes[i].Material, matrices[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if(_showAsteroidUI)
|
||||
_beltObjects[belt.Key].Update(belt.Value.Positions);
|
||||
_beltObjects[key].Update(belt.Transforms, height);
|
||||
}
|
||||
|
||||
foreach (var planet in Planets)
|
||||
|
|
@ -661,17 +677,18 @@ public class AsteroidBeltUI
|
|||
//_collider.sharedMesh = _mesh;
|
||||
}
|
||||
|
||||
public void Update(float3[] positions)
|
||||
public void Update(float4[] transforms, float height)
|
||||
{
|
||||
var parentPosition = _zone.GetOrbitPosition(_orbitParent);
|
||||
for (var i = 0; i < _belt.Data.Asteroids.Length; i++)
|
||||
var parentPosition = _belt.OrbitPosition;
|
||||
for (var i = 0; i < transforms.Length; i++)
|
||||
{
|
||||
var rotation = Quaternion.Euler(90, _belt.Rotations[i], 0);
|
||||
//var position = new Vector3(_belt.Transforms[i].x, _zone.GetHeight(parentPosition), _belt.Transforms[i].y);
|
||||
_vertices[i * 4] = rotation * new Vector3(-_belt.Scales[i] * _scale, -_belt.Scales[i] * _scale, 0) + (Vector3) positions[i];
|
||||
_vertices[i * 4 + 1] = rotation * new Vector3(-_belt.Scales[i] * _scale, _belt.Scales[i] * _scale, 0) + (Vector3) positions[i];
|
||||
_vertices[i * 4 + 2] = rotation * new Vector3(_belt.Scales[i] * _scale, _belt.Scales[i] * _scale, 0) + (Vector3) positions[i];
|
||||
_vertices[i * 4 + 3] = rotation * new Vector3(_belt.Scales[i] * _scale, -_belt.Scales[i] * _scale, 0) + (Vector3) positions[i];
|
||||
var transform = transforms[i];
|
||||
var rotation = Quaternion.Euler(90, transform.z, 0);
|
||||
var position = new Vector3(transform.x, height, transform.y);
|
||||
_vertices[i * 4] = rotation * new Vector3(-transform.w * _scale, -transform.w * _scale, 0) + position;
|
||||
_vertices[i * 4 + 1] = rotation * new Vector3(-transform.w * _scale, transform.w * _scale, 0) + position;
|
||||
_vertices[i * 4 + 2] = rotation * new Vector3(transform.w * _scale, transform.w * _scale, 0) + position;
|
||||
_vertices[i * 4 + 3] = rotation * new Vector3(transform.w * _scale, -transform.w * _scale, 0) + position;
|
||||
}
|
||||
|
||||
_mesh.bounds = new Bounds(new Vector3(parentPosition.x, 0, parentPosition.y), Vector3.one * (_size * 2));
|
||||
|
|
|
|||
Loading…
Reference in a new issue