Skip to content

Commit

Permalink
Add camera control and state importance system
Browse files Browse the repository at this point in the history
  • Loading branch information
jeickhoff committed May 30, 2023
1 parent 5445f43 commit ab0914d
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Assets/Materials/DefaultBoxMaterial.mat
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Material:
m_Name: DefaultBoxMaterial
m_Shader: {fileID: 4800000, guid: 933532a4fcc9baf4fa0491de14d08ed7, type: 3}
m_Parent: {fileID: 2100000, guid: 40e6df53a7ed41a44866ad9827990809, type: 2}
m_ModifiedSerializedProperties: 0
m_ModifiedSerializedProperties: 8
m_ValidKeywords:
- _SPECULAR_SETUP
m_InvalidKeywords: []
Expand All @@ -48,6 +48,7 @@ Material:
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats:
- _Cull: 2
- _Metallic: 0
- _Smoothness: 0
- _WorkflowMode: 0
Expand Down
8 changes: 8 additions & 0 deletions Assets/Meshes.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Meshes/plane.fbx
Binary file not shown.
109 changes: 109 additions & 0 deletions Assets/Meshes/plane.fbx.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Assets/Prefabs/TerrainFace.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Transform:
m_GameObject: {fileID: 8123702849073069357}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0.1, y: 0.1, z: 0.1}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
Expand All @@ -42,7 +42,7 @@ MeshFilter:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8123702849073069357}
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
m_Mesh: {fileID: -462981019419857548, guid: b783776a69528e745a7fc3b886c598ab, type: 3}
--- !u!23 &1616570936911530320
MeshRenderer:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -106,7 +106,7 @@ MeshCollider:
serializedVersion: 5
m_Convex: 0
m_CookingOptions: 30
m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0}
m_Mesh: {fileID: -462981019419857548, guid: b783776a69528e745a7fc3b886c598ab, type: 3}
--- !u!114 &-7653684817171243984
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
13 changes: 13 additions & 0 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ GameObject:
- component: {fileID: 330585545}
- component: {fileID: 330585544}
- component: {fileID: 330585547}
- component: {fileID: 330585548}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
Expand Down Expand Up @@ -260,6 +261,18 @@ MonoBehaviour:
mipBias: 0
varianceClampScale: 0.9
contrastAdaptiveSharpening: 0
--- !u!114 &330585548
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 330585543}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0b52457eaabc47a3a7737f7a651c57b9, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &367044023
GameObject:
m_ObjectHideFlags: 0
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scenes/SampleScene/SampleSubScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ MonoBehaviour:
x: 1
y: 1
z: 1
maxChunkSpawnsPerTick: 8
maxChunkSpawnsPerTick: 10
blocksPerChunkSide: 4
YBounds:
x: 0
Expand Down
48 changes: 48 additions & 0 deletions Assets/Scripts/Networking/ConnectionPositionSystem.cs
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;
}
}
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Networking/ConnectionPositionSystem.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/Scripts/Networking/GoInGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void OnUpdate(ref SystemState state)
foreach (var (reqSrc, reqEntity) in SystemAPI.Query<RefRO<ReceiveRpcCommandRequest>>().WithAll<GoInGameRequest>().WithEntityAccess())
{
commandBuffer.AddComponent<NetworkStreamInGame>(reqSrc.ValueRO.SourceConnection);
commandBuffer.AddComponent(reqSrc.ValueRO.SourceConnection, new GhostConnectionPosition{ Position = new float3() });
var networkId = networkIdFromEntity[reqSrc.ValueRO.SourceConnection];

UnityEngine.Debug.Log($"'{worldName}' setting connection '{networkId.Value}' to in game!");
Expand Down
11 changes: 11 additions & 0 deletions Assets/Scripts/Player/CameraSingleton.cs
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>();
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Player/CameraSingleton.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Assets/Scripts/Player/CameraSystem.cs
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;
}
}
}

3 changes: 3 additions & 0 deletions Assets/Scripts/Player/CameraSystem.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions Assets/Scripts/Player/PlayerInputSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ public void OnUpdate(ref SystemState state)
}
commandBuffer.Playback(state.EntityManager);
}

bool left = Input.GetKey("left");
bool right = Input.GetKey("right");
bool down = Input.GetKey("down");
bool up = Input.GetKey("up");
bool left = Input.GetKey("a");
bool right = Input.GetKey("d");
bool down = Input.GetKey("s");
bool up = Input.GetKey("w");
bool space = Input.GetKeyDown("space");
bool mouse1 = Input.GetKeyDown(KeyCode.Mouse0);
bool mouse2 = Input.GetKeyDown(KeyCode.Mouse1);
Expand Down
3 changes: 2 additions & 1 deletion Assets/Scripts/Player/PlayerSpawnerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Unity.Collections;
using Unity.Logging;
using Unity.Networking.Transport;
using UnityEngine.UIElements;

public struct SpawnPlayerRequest : IRpcCommand
{
Expand Down Expand Up @@ -62,7 +63,7 @@ public void OnUpdate(ref SystemState state)
}

connectionsSeen[index] = reqSrc.ValueRO.SourceConnection;
var networkId = networkIdFromEntity[reqSrc.ValueRO.SourceConnection];
NetworkId networkId = networkIdFromEntity[reqSrc.ValueRO.SourceConnection];

commandBuffer.AddComponent<PlayerSpawned>(reqSrc.ValueRO.SourceConnection);

Expand Down
9 changes: 8 additions & 1 deletion Assets/Scripts/Rendering/RenderTerrainSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,14 @@ private int3 GetBlockNeighboringAreas(int3 location)
private void SpawnFace(int3 location, float3 direction)
{
Entity newFace = ecb.Instantiate(1, face);
quaternion rotation = quaternion.LookRotationSafe( math.down(), direction);
// Returns identity if colinear, so math.up as direction functions as intended if the mesh is already facing up :)
quaternion rotation = quaternion.LookRotationSafe( math.up(), direction);
if (direction.Equals(math.down()))
{
// Rotations are in radians
rotation = quaternion.RotateZ(math.PI);
}

LocalTransform lt = new LocalTransform() { Position = location + 0.5f * direction + new float3(0.5f), Rotation = rotation, Scale = 1.0f};
ecb.SetComponent(1, newFace, lt);
}
Expand Down

0 comments on commit ab0914d

Please sign in to comment.