From ab0914d40d2a27a487f1f2f74c3b93721ad7811f Mon Sep 17 00:00:00 2001 From: jeickhoff Date: Tue, 30 May 2023 20:37:01 +0200 Subject: [PATCH] Add camera control and state importance system --- Assets/Materials/DefaultBoxMaterial.mat | 3 +- Assets/Meshes.meta | 8 ++ Assets/Meshes/plane.fbx | Bin 0 -> 11612 bytes Assets/Meshes/plane.fbx.meta | 109 ++++++++++++++++++ Assets/Prefabs/TerrainFace.prefab | 6 +- Assets/Scenes/SampleScene.unity | 13 +++ .../Scenes/SampleScene/SampleSubScene.unity | 2 +- .../Networking/ConnectionPositionSystem.cs | 48 ++++++++ .../ConnectionPositionSystem.cs.meta | 3 + Assets/Scripts/Networking/GoInGame.cs | 1 + Assets/Scripts/Player/CameraSingleton.cs | 11 ++ Assets/Scripts/Player/CameraSingleton.cs.meta | 3 + Assets/Scripts/Player/CameraSystem.cs | 35 ++++++ Assets/Scripts/Player/CameraSystem.cs.meta | 3 + Assets/Scripts/Player/PlayerInputSystem.cs | 9 +- Assets/Scripts/Player/PlayerSpawnerSystem.cs | 3 +- .../Scripts/Rendering/RenderTerrainSystem.cs | 9 +- 17 files changed, 254 insertions(+), 12 deletions(-) create mode 100644 Assets/Meshes.meta create mode 100644 Assets/Meshes/plane.fbx create mode 100644 Assets/Meshes/plane.fbx.meta create mode 100644 Assets/Scripts/Networking/ConnectionPositionSystem.cs create mode 100644 Assets/Scripts/Networking/ConnectionPositionSystem.cs.meta create mode 100644 Assets/Scripts/Player/CameraSingleton.cs create mode 100644 Assets/Scripts/Player/CameraSingleton.cs.meta create mode 100644 Assets/Scripts/Player/CameraSystem.cs create mode 100644 Assets/Scripts/Player/CameraSystem.cs.meta diff --git a/Assets/Materials/DefaultBoxMaterial.mat b/Assets/Materials/DefaultBoxMaterial.mat index c06f717c..95166066 100644 --- a/Assets/Materials/DefaultBoxMaterial.mat +++ b/Assets/Materials/DefaultBoxMaterial.mat @@ -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: [] @@ -48,6 +48,7 @@ Material: m_Offset: {x: 0, y: 0} m_Ints: [] m_Floats: + - _Cull: 2 - _Metallic: 0 - _Smoothness: 0 - _WorkflowMode: 0 diff --git a/Assets/Meshes.meta b/Assets/Meshes.meta new file mode 100644 index 00000000..12897f35 --- /dev/null +++ b/Assets/Meshes.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9630c5592e3e140499df0381fad26035 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Meshes/plane.fbx b/Assets/Meshes/plane.fbx new file mode 100644 index 0000000000000000000000000000000000000000..3714e5e733896c9124f1491d345aa18eaa1cfb2f GIT binary patch literal 11612 zcmcgydu$xV8DB#jJ5C5B>PoMXd@I72*#XRA>nWsi{z#($dH8H#2)Td%oSf z*iuJc@9uo_{bs($eBaF64%qWq+qc{My02^Rc3j(^Z*RBGwT_-=S$DNq<+2v^eqm>Y zzjrPWZqf1F&w$v3s~Oj6W%8u(Nld+avSrN~E!xl`q!IkEL0i4o&G_?$0DR-~1(5>D z`VygaS<~YSTT-38NC$Sl@EcIVzFW$$=<<&SVcGS<_ET>|iVuz2<{{4wW>XyX8bE|7 zC->U(sgI#@qfxo6-z)j4Q&GFys9iqfxTQd()}a2=W_?;@JU5$KhWfy$UpeIDa!yeZ zUjiXgp4BZcMQw`|)brDu@Y0}B}etF0C9UWJE z9Ri+&_8W1nNN0pAQtq^uJ_E1&a^94k*D@+UZBnn) zR4S{i<}6;8L)Bqv9TI^}tZZEC&l`m1aVN-$G@0)-s$0^fsr@1oNW#MgVR=_6n1x2j z@C&29bwJGD?D^TEq`YBL#>7E~J}4<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[BurstCompile] + public void OnUpdate(ref SystemState state) + { + foreach (var (nID, ghostConnPos) in SystemAPI.Query>()) + { + foreach (var player in SystemAPI.Query().WithAll()) + { + if (player.OwnerNetworkId == nID.Value) + { + ghostConnPos.ValueRW.Position = player.Transform.ValueRO.Position; + break; + } + } + } + } +} diff --git a/Assets/Scripts/Networking/ConnectionPositionSystem.cs.meta b/Assets/Scripts/Networking/ConnectionPositionSystem.cs.meta new file mode 100644 index 00000000..4636b0f3 --- /dev/null +++ b/Assets/Scripts/Networking/ConnectionPositionSystem.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d45ae935f176423ca59c54efdfd29c9a +timeCreated: 1685446831 \ No newline at end of file diff --git a/Assets/Scripts/Networking/GoInGame.cs b/Assets/Scripts/Networking/GoInGame.cs index fb12be05..da2fb960 100644 --- a/Assets/Scripts/Networking/GoInGame.cs +++ b/Assets/Scripts/Networking/GoInGame.cs @@ -71,6 +71,7 @@ public void OnUpdate(ref SystemState state) foreach (var (reqSrc, reqEntity) in SystemAPI.Query>().WithAll().WithEntityAccess()) { commandBuffer.AddComponent(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!"); diff --git a/Assets/Scripts/Player/CameraSingleton.cs b/Assets/Scripts/Player/CameraSingleton.cs new file mode 100644 index 00000000..65d74b0f --- /dev/null +++ b/Assets/Scripts/Player/CameraSingleton.cs @@ -0,0 +1,11 @@ +using UnityEngine; + +public class CameraSingleton : MonoBehaviour +{ + public static Camera Instance; + + void Awake() + { + Instance = GetComponent(); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Player/CameraSingleton.cs.meta b/Assets/Scripts/Player/CameraSingleton.cs.meta new file mode 100644 index 00000000..f9149a58 --- /dev/null +++ b/Assets/Scripts/Player/CameraSingleton.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0b52457eaabc47a3a7737f7a651c57b9 +timeCreated: 1685469994 \ No newline at end of file diff --git a/Assets/Scripts/Player/CameraSystem.cs b/Assets/Scripts/Player/CameraSystem.cs new file mode 100644 index 00000000..d7d9b79d --- /dev/null +++ b/Assets/Scripts/Player/CameraSystem.cs @@ -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(); + state.RequireForUpdate(); + } + + 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>().WithAll()) + { + 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; + } + } +} + diff --git a/Assets/Scripts/Player/CameraSystem.cs.meta b/Assets/Scripts/Player/CameraSystem.cs.meta new file mode 100644 index 00000000..7ed27535 --- /dev/null +++ b/Assets/Scripts/Player/CameraSystem.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e45aebd6bb3e43d584b5c2eb62784aa3 +timeCreated: 1685466057 \ No newline at end of file diff --git a/Assets/Scripts/Player/PlayerInputSystem.cs b/Assets/Scripts/Player/PlayerInputSystem.cs index 0df6a5cb..feef9a75 100644 --- a/Assets/Scripts/Player/PlayerInputSystem.cs +++ b/Assets/Scripts/Player/PlayerInputSystem.cs @@ -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); diff --git a/Assets/Scripts/Player/PlayerSpawnerSystem.cs b/Assets/Scripts/Player/PlayerSpawnerSystem.cs index 26f0b579..a0877eb6 100644 --- a/Assets/Scripts/Player/PlayerSpawnerSystem.cs +++ b/Assets/Scripts/Player/PlayerSpawnerSystem.cs @@ -7,6 +7,7 @@ using Unity.Collections; using Unity.Logging; using Unity.Networking.Transport; +using UnityEngine.UIElements; public struct SpawnPlayerRequest : IRpcCommand { @@ -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(reqSrc.ValueRO.SourceConnection); diff --git a/Assets/Scripts/Rendering/RenderTerrainSystem.cs b/Assets/Scripts/Rendering/RenderTerrainSystem.cs index 7d7bfbbe..b44ee738 100644 --- a/Assets/Scripts/Rendering/RenderTerrainSystem.cs +++ b/Assets/Scripts/Rendering/RenderTerrainSystem.cs @@ -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); }