-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add camera control and state importance system
- Loading branch information
jeickhoff
committed
May 30, 2023
1 parent
5445f43
commit ab0914d
Showing
17 changed files
with
254 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Unity.Burst; | ||
using Unity.Collections; | ||
using Unity.Entities; | ||
using Unity.Mathematics; | ||
using Unity.NetCode; | ||
|
||
[BurstCompile] | ||
[RequireMatchingQueriesForUpdate] | ||
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)] | ||
//[UpdateAfter(typeof(PlayerMovementSystem))] | ||
public partial struct ConnectionPositionSystem : ISystem | ||
{ | ||
public void OnCreate(ref SystemState state) | ||
{ | ||
var grid = state.EntityManager.CreateEntity(); | ||
var m_ScaleFunctionPointer = GhostDistanceImportance.ScaleFunctionPointer; | ||
state.EntityManager.SetName(grid, "GhostImportanceSingleton"); | ||
state.EntityManager.AddComponentData(grid, new GhostDistanceData | ||
{ | ||
TileSize = new int3(5, 5, 5), | ||
TileCenter = new int3(0, 0, 0), | ||
TileBorderWidth = new float3(1f, 1f, 1f), | ||
}); | ||
state.EntityManager.AddComponentData(grid, new GhostImportance | ||
{ | ||
ScaleImportanceFunction = m_ScaleFunctionPointer, | ||
GhostConnectionComponentType = ComponentType.ReadOnly<GhostConnectionPosition>(), | ||
GhostImportanceDataType = ComponentType.ReadOnly<GhostDistanceData>(), | ||
GhostImportancePerChunkDataType = ComponentType.ReadOnly<GhostDistancePartitionShared>(), | ||
}); | ||
} | ||
|
||
[BurstCompile] | ||
public void OnUpdate(ref SystemState state) | ||
{ | ||
foreach (var (nID, ghostConnPos) in SystemAPI.Query<NetworkId, RefRW<GhostConnectionPosition>>()) | ||
{ | ||
foreach (var player in SystemAPI.Query<PlayerAspect>().WithAll<Simulate>()) | ||
{ | ||
if (player.OwnerNetworkId == nID.Value) | ||
{ | ||
ghostConnPos.ValueRW.Position = player.Transform.ValueRO.Position; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using UnityEngine; | ||
|
||
public class CameraSingleton : MonoBehaviour | ||
{ | ||
public static Camera Instance; | ||
|
||
void Awake() | ||
{ | ||
Instance = GetComponent<Camera>(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Unity.Burst; | ||
using Unity.Entities; | ||
using Unity.Mathematics; | ||
using Unity.NetCode; | ||
using Unity.Transforms; | ||
|
||
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)] | ||
[UpdateInGroup(typeof(PresentationSystemGroup))] | ||
[BurstCompile] | ||
partial struct CameraSystem : ISystem | ||
{ | ||
public static readonly float3 k_CameraOffset = new float3(0, 2, -5); | ||
|
||
[BurstCompile] | ||
public void OnCreate(ref SystemState state) | ||
{ | ||
state.RequireForUpdate<NetworkStreamInGame>(); | ||
state.RequireForUpdate<Player>(); | ||
} | ||
|
||
public void OnUpdate(ref SystemState state) | ||
{ | ||
var camera = CameraSingleton.Instance; | ||
//We need to access the LocalToWorld matrix to match the position of the player in term of presentation. | ||
//Because Physics can be either Interpolated or Predicted, we the LocalToWorld can be different than the real world position | ||
//of the entity. | ||
foreach (var (localToWorld, input) in SystemAPI.Query<RefRO<LocalToWorld>, RefRO<PlayerInput>>().WithAll<GhostOwnerIsLocal>()) | ||
{ | ||
camera.transform.rotation = math.mul(quaternion.RotateY(input.ValueRO.Yaw), quaternion.RotateX(-input.ValueRO.Pitch)); | ||
var offset = math.rotate(camera.transform.rotation, k_CameraOffset); | ||
camera.transform.position = localToWorld.ValueRO.Position + offset; | ||
} | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters