From a6254cb262e6e86acdb58dfd079358fe966d3ad1 Mon Sep 17 00:00:00 2001 From: tetreum Date: Sun, 28 Feb 2021 11:52:56 +0100 Subject: [PATCH] + Marry building system with world terrain - phase 1 --- Assets/Materials/TransparentMaterial.mat | 2 +- Assets/{ => Prefabs}/FPSController.prefab | 198 +++++++++- .../{ => Prefabs}/FPSController.prefab.meta | 0 Assets/Prefabs/Server.prefab | 1 + Assets/Scenes/SampleScene.unity | 216 ----------- Assets/Scenes/WorldGenerationTest.unity | 228 ++++++++++- Assets/Scripts/BrickCollisionDetector.cs | 67 +++- Assets/Scripts/Player/Player.cs | 13 +- Assets/Scripts/Server.cs | 5 + Assets/Scripts/Utils/CameraController.cs | 3 + Assets/Scripts/World/Chunk.cs | 22 +- Assets/Scripts/World/ChunkGenManager.cs | 21 +- Assets/Scripts/World/ChunkRenderer.cs | 362 ++---------------- Assets/Scripts/World/ChunkSlice.cs | 2 +- Assets/Scripts/World/WorldBehaviour.cs | 98 ++--- 15 files changed, 602 insertions(+), 636 deletions(-) rename Assets/{ => Prefabs}/FPSController.prefab (64%) rename Assets/{ => Prefabs}/FPSController.prefab.meta (100%) diff --git a/Assets/Materials/TransparentMaterial.mat b/Assets/Materials/TransparentMaterial.mat index c18bfdb..57f650a 100644 --- a/Assets/Materials/TransparentMaterial.mat +++ b/Assets/Materials/TransparentMaterial.mat @@ -250,7 +250,7 @@ Material: - _ZTestTransparent: 4 - _ZWrite: 0 m_Colors: - - _BaseColor: {r: 1, g: 1, b: 1, a: 1} + - _BaseColor: {r: 1, g: 0, b: 0, a: 1} - _BaseColorMap_MipInfo: {r: 0, g: 0, b: 0, a: 0} - _Color: {r: 1, g: 1, b: 1, a: 1} - _DiffusionProfileAsset: {r: 0, g: 0, b: 0, a: 0} diff --git a/Assets/FPSController.prefab b/Assets/Prefabs/FPSController.prefab similarity index 64% rename from Assets/FPSController.prefab rename to Assets/Prefabs/FPSController.prefab index f3e370c..17c5757 100644 --- a/Assets/FPSController.prefab +++ b/Assets/Prefabs/FPSController.prefab @@ -1,5 +1,64 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: +--- !u!1 &1396293389 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1396293390} + - component: {fileID: 1396293392} + - component: {fileID: 1396293393} + m_Layer: 1 + m_Name: TriggerDetector + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1396293390 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1396293389} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 4573967098094624498} + m_RootOrder: 1 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!136 &1396293392 +CapsuleCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1396293389} + m_Material: {fileID: 0} + m_IsTrigger: 1 + m_Enabled: 1 + m_Radius: 0.35 + m_Height: 1.8 + m_Direction: 1 + m_Center: {x: 0, y: 0, z: 0} +--- !u!114 &1396293393 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1396293389} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 8e5c79ebb3775174eb3267455201fb5d, type: 3} + m_Name: + m_EditorClassIdentifier: + waterBlocks: [] --- !u!1 &4573967098094624393 GameObject: m_ObjectHideFlags: 0 @@ -13,6 +72,7 @@ GameObject: - component: {fileID: 4573967098094624396} - component: {fileID: 4573967098094624399} - component: {fileID: 4573967098094624398} + - component: {fileID: 1964356915} m_Layer: 2 m_Name: FPSController m_TagString: Player @@ -32,6 +92,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - {fileID: 4573967099470554939} + - {fileID: 1396293390} m_Father: {fileID: 0} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 180, z: 0} @@ -69,7 +130,7 @@ MonoBehaviour: m_WalkSpeed: 2 m_RunSpeed: 5 m_RunstepLenghten: 0.7 - m_JumpSpeed: 3 + m_JumpSpeed: 5 m_StickToGroundForce: 10 m_GravityMultiplier: 2 m_MouseLook: @@ -287,6 +348,22 @@ AudioSource: m_PreInfinity: 2 m_PostInfinity: 2 m_RotationOrder: 0 +--- !u!114 &1964356915 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4573967098094624393} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: de849660fe1ceb242a3431e8327c72af, type: 3} + m_Name: + m_EditorClassIdentifier: + inventorySlots: 36 + isOnWater: 0 + firstPersonController: {fileID: 0} + triggerDetector: {fileID: 0} --- !u!1 &4573967099470554938 GameObject: m_ObjectHideFlags: 0 @@ -299,6 +376,7 @@ GameObject: - component: {fileID: 4573967099470554916} - component: {fileID: 4573967099470554919} - component: {fileID: 4573967099470554918} + - component: {fileID: 1791073987} m_Layer: 0 m_Name: FirstPersonCharacter m_TagString: MainCamera @@ -356,8 +434,8 @@ Camera: m_TargetTexture: {fileID: 0} m_TargetDisplay: 0 m_TargetEye: 3 - m_HDR: 1 - m_AllowMSAA: 1 + m_HDR: 0 + m_AllowMSAA: 0 m_AllowDynamicResolution: 0 m_ForceIntoRT: 0 m_OcclusionCulling: 1 @@ -379,3 +457,117 @@ Behaviour: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 4573967099470554938} m_Enabled: 1 +--- !u!114 &1791073987 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4573967099470554938} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 23c1ce4fb46143f46bc5cb5224c934f6, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Version: 7 + m_ObsoleteRenderingPath: 0 + m_ObsoleteFrameSettings: + overrides: 0 + enableShadow: 0 + enableContactShadows: 0 + enableShadowMask: 0 + enableSSR: 0 + enableSSAO: 0 + enableSubsurfaceScattering: 0 + enableTransmission: 0 + enableAtmosphericScattering: 0 + enableVolumetrics: 0 + enableReprojectionForVolumetrics: 0 + enableLightLayers: 0 + enableExposureControl: 1 + diffuseGlobalDimmer: 0 + specularGlobalDimmer: 0 + shaderLitMode: 0 + enableDepthPrepassWithDeferredRendering: 0 + enableTransparentPrepass: 0 + enableMotionVectors: 0 + enableObjectMotionVectors: 0 + enableDecals: 0 + enableRoughRefraction: 0 + enableTransparentPostpass: 0 + enableDistortion: 0 + enablePostprocess: 0 + enableOpaqueObjects: 0 + enableTransparentObjects: 0 + enableRealtimePlanarReflection: 0 + enableMSAA: 0 + enableAsyncCompute: 0 + runLightListAsync: 0 + runSSRAsync: 0 + runSSAOAsync: 0 + runContactShadowsAsync: 0 + runVolumeVoxelizationAsync: 0 + lightLoopSettings: + overrides: 0 + enableDeferredTileAndCluster: 0 + enableComputeLightEvaluation: 0 + enableComputeLightVariants: 0 + enableComputeMaterialVariants: 0 + enableFptlForForwardOpaque: 0 + enableBigTilePrepass: 0 + isFptlEnabled: 0 + clearColorMode: 0 + backgroundColorHDR: {r: 0.025, g: 0.07, b: 0.19, a: 0} + clearDepth: 1 + volumeLayerMask: + serializedVersion: 2 + m_Bits: 1 + volumeAnchorOverride: {fileID: 0} + antialiasing: 0 + SMAAQuality: 2 + dithering: 0 + stopNaNs: 0 + taaSharpenStrength: 0.6 + TAAQuality: 1 + taaHistorySharpening: 0.35 + taaAntiFlicker: 0.5 + taaMotionVectorRejection: 0 + taaAntiHistoryRinging: 0 + physicalParameters: + m_Iso: 200 + m_ShutterSpeed: 0.005 + m_Aperture: 16 + m_BladeCount: 5 + m_Curvature: {x: 2, y: 11} + m_BarrelClipping: 0.25 + m_Anamorphism: 0 + flipYMode: 0 + xrRendering: 1 + fullscreenPassthrough: 0 + allowDynamicResolution: 0 + customRenderingSettings: 0 + invertFaceCulling: 0 + probeLayerMask: + serializedVersion: 2 + m_Bits: 4294967295 + hasPersistentHistory: 0 + exposureTarget: {fileID: 0} + m_RenderingPathCustomFrameSettings: + bitDatas: + data1: 70300024700749 + data2: 4539628425463136256 + lodBias: 1 + lodBiasMode: 0 + lodBiasQualityLevel: 0 + maximumLODLevel: 0 + maximumLODLevelMode: 0 + maximumLODLevelQualityLevel: 0 + sssQualityMode: 0 + sssQualityLevel: 0 + sssCustomSampleBudget: 20 + materialQuality: 0 + renderingPathCustomFrameSettingsOverrideMask: + mask: + data1: 0 + data2: 0 + defaultFrameSettings: 0 diff --git a/Assets/FPSController.prefab.meta b/Assets/Prefabs/FPSController.prefab.meta similarity index 100% rename from Assets/FPSController.prefab.meta rename to Assets/Prefabs/FPSController.prefab.meta diff --git a/Assets/Prefabs/Server.prefab b/Assets/Prefabs/Server.prefab index c8b3fe4..3ff078c 100644 --- a/Assets/Prefabs/Server.prefab +++ b/Assets/Prefabs/Server.prefab @@ -51,3 +51,4 @@ MonoBehaviour: - {fileID: 7820642324698506490, guid: 2afc2f2af1b4e904ab398c38b1182927, type: 3} - {fileID: 7206315527319244451, guid: 46a7727c4f018494e94698435337a070, type: 3} - {fileID: 6066566645834451869, guid: 71921c3e1effe564ea7dc3ced05ba291, type: 3} + playerPrefab: {fileID: 4573967098094624393, guid: ca86112991f492f448b9242dfb373cbd, type: 3} diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 6091e84..966186e 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -453,65 +453,6 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 51.612, y: 54.243, z: 130.659} ---- !u!1 &1396293389 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 1396293390} - - component: {fileID: 1396293392} - - component: {fileID: 1396293393} - m_Layer: 1 - m_Name: TriggerDetector - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!4 &1396293390 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1396293389} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} - m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: [] - m_Father: {fileID: 1964356920} - m_RootOrder: 1 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!136 &1396293392 -CapsuleCollider: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1396293389} - m_Material: {fileID: 0} - m_IsTrigger: 1 - m_Enabled: 1 - m_Radius: 0.35 - m_Height: 1.8 - m_Direction: 1 - m_Center: {x: 0, y: 0, z: 0} ---- !u!114 &1396293393 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1396293389} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 8e5c79ebb3775174eb3267455201fb5d, type: 3} - m_Name: - m_EditorClassIdentifier: - waterBlocks: [] --- !u!1001 &1791073985 PrefabInstance: m_ObjectHideFlags: 0 @@ -523,10 +464,6 @@ PrefabInstance: propertyPath: m_Name value: FPSController objectReference: {fileID: 0} - - target: {fileID: 4573967098094624396, guid: ca86112991f492f448b9242dfb373cbd, type: 3} - propertyPath: m_JumpSpeed - value: 5 - objectReference: {fileID: 0} - target: {fileID: 4573967098094624498, guid: ca86112991f492f448b9242dfb373cbd, type: 3} propertyPath: m_RootOrder value: 0 @@ -571,161 +508,8 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} - - target: {fileID: 4573967099470554916, guid: ca86112991f492f448b9242dfb373cbd, type: 3} - propertyPath: m_HDR - value: 0 - objectReference: {fileID: 0} - - target: {fileID: 4573967099470554916, guid: ca86112991f492f448b9242dfb373cbd, type: 3} - propertyPath: m_AllowMSAA - value: 0 - objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: ca86112991f492f448b9242dfb373cbd, type: 3} ---- !u!1 &1791073986 stripped -GameObject: - m_CorrespondingSourceObject: {fileID: 4573967099470554938, guid: ca86112991f492f448b9242dfb373cbd, type: 3} - m_PrefabInstance: {fileID: 1791073985} - m_PrefabAsset: {fileID: 0} ---- !u!114 &1791073987 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1791073986} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 23c1ce4fb46143f46bc5cb5224c934f6, type: 3} - m_Name: - m_EditorClassIdentifier: - m_Version: 7 - m_ObsoleteRenderingPath: 0 - m_ObsoleteFrameSettings: - overrides: 0 - enableShadow: 0 - enableContactShadows: 0 - enableShadowMask: 0 - enableSSR: 0 - enableSSAO: 0 - enableSubsurfaceScattering: 0 - enableTransmission: 0 - enableAtmosphericScattering: 0 - enableVolumetrics: 0 - enableReprojectionForVolumetrics: 0 - enableLightLayers: 0 - enableExposureControl: 1 - diffuseGlobalDimmer: 0 - specularGlobalDimmer: 0 - shaderLitMode: 0 - enableDepthPrepassWithDeferredRendering: 0 - enableTransparentPrepass: 0 - enableMotionVectors: 0 - enableObjectMotionVectors: 0 - enableDecals: 0 - enableRoughRefraction: 0 - enableTransparentPostpass: 0 - enableDistortion: 0 - enablePostprocess: 0 - enableOpaqueObjects: 0 - enableTransparentObjects: 0 - enableRealtimePlanarReflection: 0 - enableMSAA: 0 - enableAsyncCompute: 0 - runLightListAsync: 0 - runSSRAsync: 0 - runSSAOAsync: 0 - runContactShadowsAsync: 0 - runVolumeVoxelizationAsync: 0 - lightLoopSettings: - overrides: 0 - enableDeferredTileAndCluster: 0 - enableComputeLightEvaluation: 0 - enableComputeLightVariants: 0 - enableComputeMaterialVariants: 0 - enableFptlForForwardOpaque: 0 - enableBigTilePrepass: 0 - isFptlEnabled: 0 - clearColorMode: 0 - backgroundColorHDR: {r: 0.025, g: 0.07, b: 0.19, a: 0} - clearDepth: 1 - volumeLayerMask: - serializedVersion: 2 - m_Bits: 1 - volumeAnchorOverride: {fileID: 0} - antialiasing: 0 - SMAAQuality: 2 - dithering: 0 - stopNaNs: 0 - taaSharpenStrength: 0.6 - TAAQuality: 1 - taaHistorySharpening: 0.35 - taaAntiFlicker: 0.5 - taaMotionVectorRejection: 0 - taaAntiHistoryRinging: 0 - physicalParameters: - m_Iso: 200 - m_ShutterSpeed: 0.005 - m_Aperture: 16 - m_BladeCount: 5 - m_Curvature: {x: 2, y: 11} - m_BarrelClipping: 0.25 - m_Anamorphism: 0 - flipYMode: 0 - xrRendering: 1 - fullscreenPassthrough: 0 - allowDynamicResolution: 0 - customRenderingSettings: 0 - invertFaceCulling: 0 - probeLayerMask: - serializedVersion: 2 - m_Bits: 4294967295 - hasPersistentHistory: 0 - exposureTarget: {fileID: 0} - m_RenderingPathCustomFrameSettings: - bitDatas: - data1: 70300024700749 - data2: 4539628425463136256 - lodBias: 1 - lodBiasMode: 0 - lodBiasQualityLevel: 0 - maximumLODLevel: 0 - maximumLODLevelMode: 0 - maximumLODLevelQualityLevel: 0 - sssQualityMode: 0 - sssQualityLevel: 0 - sssCustomSampleBudget: 20 - materialQuality: 0 - renderingPathCustomFrameSettingsOverrideMask: - mask: - data1: 0 - data2: 0 - defaultFrameSettings: 0 ---- !u!1 &1964356914 stripped -GameObject: - m_CorrespondingSourceObject: {fileID: 4573967098094624393, guid: ca86112991f492f448b9242dfb373cbd, type: 3} - m_PrefabInstance: {fileID: 1791073985} - m_PrefabAsset: {fileID: 0} ---- !u!114 &1964356915 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1964356914} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: de849660fe1ceb242a3431e8327c72af, type: 3} - m_Name: - m_EditorClassIdentifier: - inventorySlots: 36 - isOnWater: 0 - firstPersonController: {fileID: 0} - triggerDetector: {fileID: 0} ---- !u!4 &1964356920 stripped -Transform: - m_CorrespondingSourceObject: {fileID: 4573967098094624498, guid: ca86112991f492f448b9242dfb373cbd, type: 3} - m_PrefabInstance: {fileID: 1791073985} - m_PrefabAsset: {fileID: 0} --- !u!1 &2092671485 GameObject: m_ObjectHideFlags: 1 diff --git a/Assets/Scenes/WorldGenerationTest.unity b/Assets/Scenes/WorldGenerationTest.unity index f8c97f0..06e834e 100644 --- a/Assets/Scenes/WorldGenerationTest.unity +++ b/Assets/Scenes/WorldGenerationTest.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} - m_IndirectSpecularColor: {r: 2156.1572, g: 2260.3867, b: 3299.3516, a: 1} + m_IndirectSpecularColor: {r: 2161.1426, g: 2269.287, b: 3322.5312, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -219,6 +219,72 @@ MeshFilter: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 76730909} m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0} +--- !u!1 &77350768 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 77350771} + - component: {fileID: 77350770} + - component: {fileID: 77350769} + m_Layer: 0 + m_Name: EventSystem + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &77350769 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 77350768} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4f231c4fb786f3946a6b90b886c48677, type: 3} + m_Name: + m_EditorClassIdentifier: + m_HorizontalAxis: Horizontal + m_VerticalAxis: Vertical + m_SubmitButton: Submit + m_CancelButton: Cancel + m_InputActionsPerSecond: 10 + m_RepeatDelay: 0.5 + m_ForceModuleActive: 0 +--- !u!114 &77350770 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 77350768} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 76c392e42b5098c458856cdf6ecaaaa1, type: 3} + m_Name: + m_EditorClassIdentifier: + m_FirstSelected: {fileID: 0} + m_sendNavigationEvents: 1 + m_DragThreshold: 10 +--- !u!4 &77350771 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 77350768} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 8 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &136659992 GameObject: m_ObjectHideFlags: 0 @@ -675,6 +741,103 @@ MeshFilter: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 454871188} m_Mesh: {fileID: 10209, guid: 0000000000000000e000000000000000, type: 0} +--- !u!1001 &518071539 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_Pivot.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_Pivot.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_RootOrder + value: 6 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_AnchorMax.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_AnchorMax.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_AnchorMin.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_AnchorMin.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_SizeDelta.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_SizeDelta.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_LocalPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_AnchoredPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_AnchoredPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392598, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 6359582169700392682, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} + propertyPath: m_Name + value: Canvas + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: 8ec3b7e304d024247a1acea0b0a6ac3f, type: 3} --- !u!1 &625603110 GameObject: m_ObjectHideFlags: 0 @@ -720,6 +883,67 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 5 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1001 &709979601 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 5696849023657060160, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: playerPrefab + value: + objectReference: {fileID: 4573967098094624393, guid: ca86112991f492f448b9242dfb373cbd, type: 3} + - target: {fileID: 5696849023657060161, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: m_RootOrder + value: 7 + objectReference: {fileID: 0} + - target: {fileID: 5696849023657060161, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: m_LocalPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5696849023657060161, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5696849023657060161, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5696849023657060161, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 5696849023657060161, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5696849023657060161, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5696849023657060161, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5696849023657060161, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5696849023657060161, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5696849023657060161, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 5696849023657060162, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} + propertyPath: m_Name + value: Server + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: f95b4ecb0498be44db5c16f2b6e58c74, type: 3} --- !u!1 &790548551 GameObject: m_ObjectHideFlags: 0 @@ -1027,7 +1251,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!81 &1823688465 AudioListener: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/BrickCollisionDetector.cs b/Assets/Scripts/BrickCollisionDetector.cs index 71c7857..36730fa 100644 --- a/Assets/Scripts/BrickCollisionDetector.cs +++ b/Assets/Scripts/BrickCollisionDetector.cs @@ -1,5 +1,6 @@ using UnityEngine; using Brickcraft.UI; +using Brickcraft.World; namespace Brickcraft { @@ -102,6 +103,13 @@ namespace Brickcraft // ignore collisions with studs if (other.name.StartsWith("GridStud")) { return; + } else if (other.name.StartsWith("ChunkSlice")) { + /* + RaycastHit hit; + Physics.Raycast(transform.position, other.transform.position, out hit); + var pos = other.gameObject.GetComponent().ClosestPointOnBounds(transform.position); + Debug.Log(other.transform.InverseTransformPoint(pos) + "-" + transform.InverseTransformPoint(pos)); + */ } // prevent resetting those vars each frame @@ -123,8 +131,14 @@ namespace Brickcraft StudInfo stud = new StudInfo(); // get grid dimensions - string[] tmp = hit.collider.name.Replace("GridStud ", "").Replace("GridStudBottom ", "").Split('x'); - stud.gridDimensions = new Vector2Int(int.Parse(tmp[0]), int.Parse(tmp[1])); + if (hit.collider.name.StartsWith("GridStud")) { + string[] tmp = hit.collider.name.Replace("GridStud ", "").Replace("GridStudBottom ", "").Split('x'); + stud.gridDimensions = new Vector2Int(int.Parse(tmp[0]), int.Parse(tmp[1])); + } else { + // is looking at world stud + // so grid is a 16x16 (chunk slice) made by 2x2 bricks + stud.gridDimensions = new Vector2Int(Chunk.SliceHeight * 2, Chunk.SliceHeight * 2); + } // 1x1 are easy xD if (stud.gridDimensions.x == 1 && stud.gridDimensions.y == 1) { @@ -132,7 +146,12 @@ namespace Brickcraft } // convert world coords to local ones - var localHitpoint = hit.collider.transform.InverseTransformPoint(hit.point); + Vector3 localHitpoint = hit.collider.transform.InverseTransformPoint(hit.point); + + if (hit.collider.name.StartsWith("ChunkSlice")) { // chunkSlices have center wrongly set + Vector3 localCenter = hit.collider.transform.InverseTransformPoint(hit.collider.transform.GetComponent().bounds.center); + localHitpoint -= localCenter; + } // since localHitpoint is based on the center of the object, we need to sum half // of it's size @@ -157,6 +176,32 @@ namespace Brickcraft return stud; } + public bool isLookingAtWorldStud (RaycastHit hit) { + Vector3[] vertices = getTriangleVertices(hit.transform.GetComponent().sharedMesh, hit.triangleIndex); + + Vector3Int equal = new Vector3Int( + vertices[0].x == vertices[1].x && vertices[1].x == vertices[2].x ? 1 : 0, + vertices[0].y == vertices[1].y && vertices[1].y == vertices[2].y ? 1 : 0, + vertices[0].z == vertices[1].z && vertices[1].z == vertices[2].z ? 1 : 0 + ); + + // y is the same OR there are no equal values (round triangle) then we know that is top or bottom face + return vertices[0].y == vertices[1].y && vertices[1].y == vertices[2].y || equal == Vector3Int.zero; + } + + Vector3[] getTriangleVertices(Mesh mesh, int triangleIndex) { + Vector3[] vertices = mesh.vertices; + int[] triangles = mesh.triangles; + + Vector3[] output = new Vector3[3]; + + output[0] = vertices[triangles[triangleIndex * 3 + 0]]; + output[1] = vertices[triangles[triangleIndex * 3 + 1]]; + output[2] = vertices[triangles[triangleIndex * 3 + 2]]; + + return output; + } + public void lookingAtStud(RaycastHit hit) { // player is not holding a brick if (PlayerPanel.Instance.selectedItem == null || PlayerPanel.Instance.selectedItem.item.type != Item.Type.Brick) { @@ -168,9 +213,8 @@ namespace Brickcraft if (hit.transform == latestStudGrid && stud.gridPosition == latestStud) { return; } - GameObject brickObj = hit.collider.transform.parent.gameObject; - Quaternion rot = pivot.rotation; // kepp current rotation + Quaternion rot = pivot.rotation; // keep current rotation // if old stud and new stud don't have the same rotation, // pivot rotation will be invalid, so we reset it to new stud's rotation @@ -182,15 +226,23 @@ namespace Brickcraft latestStud = stud.gridPosition; Vector3 studPos = hit.collider.transform.TransformPoint(stud.center); + // height needs to be corrected for bottom studs if (hit.collider.name.Contains("Bottom")) { studPos.y -= PlayerPanel.Instance.selectedItem.item.brickModel.heightInPlates * Server.plateHeight; + } else if (hit.collider.name.StartsWith("ChunkSlice")) { // chunkSlices have center wrongly set + Vector3 localCenter = hit.collider.transform.InverseTransformPoint(hit.collider.transform.GetComponent().bounds.center); + studPos += localCenter; + studPos.y = hit.point.y; + studPos.y -= 0.089f; // supper ugly hack } - + currentStud = studPos; - if (!Server.bricks.ContainsKey(brickObj.name)) { + GameObject brickObj = hit.collider.transform.parent.gameObject; + + if (!Server.bricks.ContainsKey(brickObj.name) && !hit.collider.name.StartsWith("ChunkSlice")) { Debug.LogError("Brick not found in server list " + brickObj.name); return; } @@ -216,6 +268,7 @@ namespace Brickcraft pivot.position = pos; pivot.rotation = rotation; transform.localRotation = Quaternion.identity; // localy reset child rotation as it should always be identity + isColliding = false; setValid(true); lastPos = pos; } diff --git a/Assets/Scripts/Player/Player.cs b/Assets/Scripts/Player/Player.cs index c14dac7..beafc79 100644 --- a/Assets/Scripts/Player/Player.cs +++ b/Assets/Scripts/Player/Player.cs @@ -42,6 +42,12 @@ namespace Brickcraft private void Start() { PlayerPanel.Instance.reload(); + + // temporal for testing + addItem(new UserItem() { + id = 1, + quantity = 100, + }); } private void Update() { @@ -68,7 +74,12 @@ namespace Brickcraft //Debug.DrawRay(ray.origin, ray.direction * rayLength, Color.red); if (Physics.Raycast(ray, out latestHit, rayLength)) { - if (latestHit.collider.name.StartsWith("GridStud")) { + if (latestHit.collider.name.StartsWith("ChunkSlice")) { + if (BrickCollisionDetector.Instance != null && BrickCollisionDetector.Instance.isLookingAtWorldStud(latestHit)) { + BrickCollisionDetector.Instance.lookingAtStud(latestHit); + } + lookedBrick = latestHit.transform.parent.gameObject; + } else if (latestHit.collider.name.StartsWith("GridStud")) { if (BrickCollisionDetector.Instance != null) { BrickCollisionDetector.Instance.lookingAtStud(latestHit); } diff --git a/Assets/Scripts/Server.cs b/Assets/Scripts/Server.cs index b619a0f..91276d0 100644 --- a/Assets/Scripts/Server.cs +++ b/Assets/Scripts/Server.cs @@ -83,6 +83,7 @@ namespace Brickcraft public const float brickWidth = 0.796f; // 2x2 public GameObject[] prefabs; + public GameObject playerPrefab; void Awake() { Instance = this; @@ -97,6 +98,10 @@ namespace Brickcraft } } + public void spawnPlayer () { + Instantiate(playerPrefab, new Vector3(0, 160, 0), Quaternion.identity); + } + void processPrefabs() { foreach (var prefab in prefabs) { brickPrefabs.Add(prefab.name, prefab); diff --git a/Assets/Scripts/Utils/CameraController.cs b/Assets/Scripts/Utils/CameraController.cs index 178f31e..64654a3 100644 --- a/Assets/Scripts/Utils/CameraController.cs +++ b/Assets/Scripts/Utils/CameraController.cs @@ -11,6 +11,9 @@ public class CameraController : MonoBehaviour } void Update() { + if (!enabled) { + return; + } movementSpeed = Mathf.Max(movementSpeed += Input.GetAxis("Mouse ScrollWheel"), 0.0f); if (Input.GetAxis("Vertical") != 0) { transform.Translate(Vector3.forward * movementSpeed * Input.GetAxis("Vertical")); diff --git a/Assets/Scripts/World/Chunk.cs b/Assets/Scripts/World/Chunk.cs index f9e1df2..cacb634 100644 --- a/Assets/Scripts/World/Chunk.cs +++ b/Assets/Scripts/World/Chunk.cs @@ -46,7 +46,7 @@ namespace Brickcraft.World { ChunkObject = new GameObject(String.Format("X {0} Z {1}", X, Z)); - ChunkObject.transform.position = new Vector3(X*16,0,Z*16); + ChunkObject.transform.position = new Vector3(X * 16, 0, Z * 16); } public void InitRenderableSlices() @@ -64,6 +64,7 @@ namespace Brickcraft.World meshRenderer.material.mainTexture = WorldBehaviour.AtlasTexture; meshRenderer.sharedMaterial = WorldBehaviour.BlockMaterial; newObject.AddComponent(typeof(MeshFilter)); + newObject.AddComponent(); Vector3 pos = new Vector3(ChunkObject.transform.position.x, ChunkObject.transform.position.y + (i * Chunk.SliceHeight), ChunkObject.transform.position.z); if (WorldBehaviour.mode == 3) { @@ -376,12 +377,12 @@ namespace Brickcraft.World public void RecalculateHeight() { LowestY = 255; - HeightMap = new byte[16, 16]; - for (int x = 0; x < 16; x++) - { - for (int z = 0; z < 16; z++) - RecalculateHeight(x, z); - } + HeightMap = new byte[16, 16]; + for (int x = 0; x < 16; x++) + { + for (int z = 0; z < 16; z++) + RecalculateHeight(x, z); + } MinSliceIndex = (LowestY / Chunk.SliceHeight) - 1; } @@ -393,8 +394,8 @@ namespace Brickcraft.World for (height = 127; height > 0 && (GetBlockType(x, height - 1, z) == 0 || (blockType = GetBlockType(x, height - 1, z)) == BlockType.Leaves || blockType == BlockType.Water || blockType == BlockType.Still_Water); height--) ; HeightMap[x, z] = (byte)height; - if (height < LowestY) - LowestY = height; + if (height < LowestY) + LowestY = height; } public void ClearDirtySlices() @@ -406,5 +407,8 @@ namespace Brickcraft.World slice.ClearDirtyLight(); } } + public static int CorrectBlockCoordinate(int axis) { + return axis >= 0 ? axis : (axis + Chunk.SliceHeight); + } } } diff --git a/Assets/Scripts/World/ChunkGenManager.cs b/Assets/Scripts/World/ChunkGenManager.cs index d540db1..cdf6781 100644 --- a/Assets/Scripts/World/ChunkGenManager.cs +++ b/Assets/Scripts/World/ChunkGenManager.cs @@ -53,18 +53,17 @@ namespace Brickcraft.World genWait[i] = new ManualResetEvent(false); ChunkGenThreadEntry chunkGenEntry = new ChunkGenThreadEntry(currStartX, currEndX, GenPool, (chunkGen, x) => { - int xComponent = (x + Math.Abs(fromChunkX)) * (toChunkX - fromChunkX); - for(int z = fromChunkX; z < toChunkX; ++z) - { - ushort index = WorldBehaviour.ChunkIndexFromCoords(x,z); - Chunk chunk = new Chunk(x,z, worldManager, (z + x) % 2 == 0? Color.black : Color.gray); - chunkGen.GenerateChunk(chunk, x, z); + int xComponent = (x + Math.Abs(fromChunkX)) * (toChunkX - fromChunkX); + for(int z = fromChunkX; z < toChunkX; ++z) + { + ushort index = WorldBehaviour.ChunkIndexFromCoords(x, z); + Chunk chunk = new Chunk(x,z, worldManager, (z + x) % 2 == 0 ? Color.black : Color.gray); + chunkGen.GenerateChunk(chunk, x, z); - int entryIndex = xComponent + (z + Math.Abs(fromChunkX)); - chunkEntries[entryIndex] = new ChunkMeshThreadEntry(chunk); - WorldBehaviour.ChunksMap[index] = chunk; - } - + int entryIndex = xComponent + (z + Math.Abs(fromChunkX)); + chunkEntries[entryIndex] = new ChunkMeshThreadEntry(chunk); + WorldBehaviour.ChunksMap[index] = chunk; + } }); ThreadPool.QueueUserWorkItem(new WaitCallback(chunkGenEntry.ThreadCallback), genWait[i]); diff --git a/Assets/Scripts/World/ChunkRenderer.cs b/Assets/Scripts/World/ChunkRenderer.cs index 19e9d2b..902e984 100644 --- a/Assets/Scripts/World/ChunkRenderer.cs +++ b/Assets/Scripts/World/ChunkRenderer.cs @@ -504,6 +504,23 @@ namespace Brickcraft.World throw new System.Exception("wrong face name: " + face); } + // temporal | testing + Vector2 xt = new Vector2(0, 0); + Vector2 zt = new Vector2(0, 0); + + foreach (Vector3 vertice in faceMap.vertices) { + if (vertice.x > xt.x) { + xt.x = vertice.x; + } else if (vertice.x < xt.y) { + xt.y = vertice.x; + } + if (vertice.z > zt.x) { + zt.x = vertice.z; + } else if (vertice.x < zt.y) { + zt.y = vertice.z; + } + } + foreach (Vector3 vertice in faceMap.vertices) { Vector3 pos = new Vector3( vertice.x + (x * Server.brickWidth), @@ -512,7 +529,12 @@ namespace Brickcraft.World ); vertices.Add(pos); colors.Add(color); - uvs.Add(new Vector2(vertice.x, vertice.z)); + + if (face == "top" || face == "bottom") { + uvs.Add(new Vector2(xt.x, zt.x)); + } else { + uvs.Add(new Vector2(xt.y, zt.y)); + } } foreach (int index in faceMap.triangles) { triangles.Add(index + vertexIndex); @@ -530,343 +552,5 @@ namespace Brickcraft.World meshes[block].Add(pos); } - - public void RenderSliceLight(ChunkSlice chunkSlice) - { - - Chunk parentChunk = chunkSlice.ParentChunk; - MeshFilter meshFilter = parentChunk.ChunkSliceObjects[chunkSlice.Index].GetComponent(); - - if(meshFilter.mesh.vertexCount == 0) - { - chunkSlice.ClearDirtyLight(); - return; - } - - - WorldBehaviour world = chunkSlice.ParentChunk.World; - - int minHeight = parentChunk.MinSliceIndex == chunkSlice.Index ? (parentChunk.LowestY & Chunk.SliceHeightLimit) : 0; - - colors.Clear(); - vertices.Clear(); - uvs.Clear(); - triangles.Clear(); - - float epsilon = 0; - - for (int x = 0; x < 16; x++) - { - for (int z = 0; z < 16; z++) - { - for (int y = Chunk.SliceHeight - 1; y >= 0 && y >= minHeight; --y) - { - byte block = chunkSlice[x, y, z]; - int light = 0; - byte top; - - if(block == 0) - continue; - - if(y + 1 > Chunk.SliceHeightLimit) - { - if(chunkSlice.Index + 1 > Chunk.MaxSliceIndex) - top = 0; - else - { - int worldY = (chunkSlice.Index * ChunkSlice.SizeY) + (y + 1); - top = (byte)parentChunk.GetBlockType(x, worldY, z); - light = parentChunk.GetSkylight(x, worldY, z); - } - } - else - { - top = chunkSlice[x, y + 1, z]; - light = chunkSlice.GetSkylight(x, y + 1, z); - } - - // we are checking the top face of the block, so see if the top is exposed - if (top == 0) - { - int vertexIndex = vertices.Count; - vertices.Add(new Vector3(x, y + 1, z)); - vertices.Add(new Vector3(x, y + 1, z + 1)); - vertices.Add(new Vector3(x + 1, y + 1, z + 1)); - vertices.Add(new Vector3(x + 1, y + 1, z)); - - triangles.Add(vertexIndex); - triangles.Add(vertexIndex+1); - triangles.Add(vertexIndex+2); - - triangles.Add(vertexIndex+2); - triangles.Add(vertexIndex+3); - triangles.Add(vertexIndex); - - - float attenuation = (light / 15.0f); - Color withLight = new Color(topColor.r * attenuation, topColor.g * attenuation, topColor.b * attenuation, 1); - colors.Add(withLight); - colors.Add(withLight); - colors.Add(withLight); - colors.Add(withLight); - - Rect coords = BlockUVs.GetUVFromTypeAndFace((BlockType)block, BlockFace.Top); - - float yMax = coords.y + coords.height - epsilon; - float xMax = coords.x + coords.width - epsilon; - float xMin = coords.x + epsilon; - float yMin = coords.y + epsilon; - - uvs.Add(new Vector2(xMin, yMax)); - uvs.Add(new Vector2(xMin, yMin)); - uvs.Add(new Vector2(xMax, yMin)); - uvs.Add(new Vector2(xMax, yMax)); - } - - int front; - if(z - 1 < 0) - { - int worldX = (parentChunk.X << 4) + x; - int worldZ = (parentChunk.Z << 4) - 1; - int worldY = (chunkSlice.Index * ChunkSlice.SizeY) + y; - - front = (byte)world.GetBlockType(worldX, worldY, worldZ); - } - else - front = chunkSlice[x, y, z - 1]; - - if (front == 0) - { - int vertexIndex = vertices.Count; - vertices.Add(new Vector3(x, y, z)); - vertices.Add(new Vector3(x, y + 1, z)); - vertices.Add(new Vector3(x + 1, y + 1, z)); - vertices.Add(new Vector3(x + 1, y, z)); - - triangles.Add(vertexIndex); - triangles.Add(vertexIndex+1); - triangles.Add(vertexIndex+2); - - triangles.Add(vertexIndex+2); - triangles.Add(vertexIndex+3); - triangles.Add(vertexIndex); - - colors.Add(firstSideColor); - colors.Add(firstSideColor); - colors.Add(firstSideColor); - colors.Add(firstSideColor); - - Rect coords = BlockUVs.GetUVFromTypeAndFace((BlockType)block, BlockFace.Side); - - float yMax = coords.y + coords.height - epsilon; - float xMax = coords.x + coords.width - epsilon; - float xMin = coords.x + epsilon; - float yMin = coords.y + epsilon; - - uvs.Add(new Vector2(xMin, yMin)); - uvs.Add(new Vector2(xMin, yMax)); - uvs.Add(new Vector2(xMax, yMax)); - uvs.Add(new Vector2(xMax, yMin)); - } - - int right; - - if(x + 1 > 15) - { - int worldX = (parentChunk.X << 4) + 16; - int worldZ = (parentChunk.Z << 4) + z; - int worldY = (chunkSlice.Index * ChunkSlice.SizeY) + y; - - right = (byte)world.GetBlockType(worldX, worldY, worldZ); - } - else - right = chunkSlice[x + 1, y, z]; - - if (right == 0) - { - int vertexIndex = vertices.Count; - vertices.Add(new Vector3(x + 1, y, z)); - vertices.Add(new Vector3(x + 1, y + 1, z)); - vertices.Add(new Vector3(x + 1, y + 1, z + 1)); - vertices.Add(new Vector3(x + 1, y, z + 1)); - - triangles.Add(vertexIndex); - triangles.Add(vertexIndex+1); - triangles.Add(vertexIndex+2); - - triangles.Add(vertexIndex+2); - triangles.Add(vertexIndex+3); - triangles.Add(vertexIndex); - - colors.Add(secondSideColor); - colors.Add(secondSideColor); - colors.Add(secondSideColor); - colors.Add(secondSideColor); - - Rect coords = BlockUVs.GetUVFromTypeAndFace((BlockType)block, BlockFace.Side); - - float yMax = coords.y + coords.height - epsilon; - float xMax = coords.x + coords.width - epsilon; - float xMin = coords.x + epsilon; - float yMin = coords.y + epsilon; - - uvs.Add(new Vector2(xMin, yMin)); - uvs.Add(new Vector2(xMin, yMax)); - uvs.Add(new Vector2(xMax, yMax)); - uvs.Add(new Vector2(xMax, yMin)); - } - - int back; - - if(z + 1 > 15) - { - int worldX = (parentChunk.X << 4) + x; - int worldZ = (parentChunk.Z << 4) + 16; - int worldY = (chunkSlice.Index * ChunkSlice.SizeY) + y; - - back = (byte)world.GetBlockType(worldX, worldY, worldZ); - } - else - back = chunkSlice[x, y, z + 1]; - - if (back == 0) - { - int vertexIndex = vertices.Count; - vertices.Add(new Vector3(x + 1, y, z + 1)); - vertices.Add(new Vector3(x + 1, y + 1, z + 1)); - vertices.Add(new Vector3(x, y + 1, z + 1)); - vertices.Add(new Vector3(x, y, z + 1)); - - triangles.Add(vertexIndex); - triangles.Add(vertexIndex+1); - triangles.Add(vertexIndex+2); - - triangles.Add(vertexIndex+2); - triangles.Add(vertexIndex+3); - triangles.Add(vertexIndex); - - colors.Add(firstSideColor); - colors.Add(firstSideColor); - colors.Add(firstSideColor); - colors.Add(firstSideColor); - - Rect coords = BlockUVs.GetUVFromTypeAndFace((BlockType)block, BlockFace.Side); - - float yMax = coords.y + coords.height - epsilon; - float xMax = coords.x + coords.width - epsilon; - float xMin = coords.x + epsilon; - float yMin = coords.y + epsilon; - - uvs.Add(new Vector2(xMin, yMin)); - uvs.Add(new Vector2(xMin, yMax)); - uvs.Add(new Vector2(xMax, yMax)); - uvs.Add(new Vector2(xMax, yMin)); - } - - int left; - - if(x - 1 < 0) - { - int worldX = (parentChunk.X << 4) - 1; - int worldZ = (parentChunk.Z << 4) + z; - int worldY = (chunkSlice.Index * ChunkSlice.SizeY) + y; - - left = (byte)world.GetBlockType(worldX, worldY, worldZ); - } - else - left = chunkSlice[x - 1, y, z]; - - if (left == 0) - { - int vertexIndex = vertices.Count; - vertices.Add(new Vector3(x, y, z + 1)); - vertices.Add(new Vector3(x, y + 1, z + 1)); - vertices.Add(new Vector3(x, y + 1, z)); - vertices.Add(new Vector3(x, y, z)); - - triangles.Add(vertexIndex); - triangles.Add(vertexIndex+1); - triangles.Add(vertexIndex+2); - - triangles.Add(vertexIndex+2); - triangles.Add(vertexIndex+3); - triangles.Add(vertexIndex); - - colors.Add(secondSideColor); - colors.Add(secondSideColor); - colors.Add(secondSideColor); - colors.Add(secondSideColor); - - Rect coords = BlockUVs.GetUVFromTypeAndFace((BlockType)block, BlockFace.Side); - - float yMax = coords.y + coords.height - epsilon; - float xMax = coords.x + coords.width - epsilon; - float xMin = coords.x + epsilon; - float yMin = coords.y + epsilon; - - uvs.Add(new Vector2(xMin, yMin)); - uvs.Add(new Vector2(xMin, yMax)); - uvs.Add(new Vector2(xMax, yMax)); - uvs.Add(new Vector2(xMax, yMin)); - } - - byte bottom; - - if(y - 1 < 0) - bottom = 1; - else - bottom = chunkSlice[x, y - 1, z]; - - if (bottom == 0) - { - int vertexIndex = vertices.Count; - vertices.Add(new Vector3(x, y, z + 1)); - vertices.Add(new Vector3(x, y, z)); - vertices.Add(new Vector3(x + 1, y, z)); - vertices.Add(new Vector3(x + 1, y, z + 1)); - - triangles.Add(vertexIndex); - triangles.Add(vertexIndex+1); - triangles.Add(vertexIndex+2); - - triangles.Add(vertexIndex+2); - triangles.Add(vertexIndex+3); - triangles.Add(vertexIndex); - - /*triangles.Add(vertexIndex); - triangles.Add(vertexIndex+1); - triangles.Add(vertexIndex+2); - triangles.Add(vertexIndex+3);*/ - - colors.Add(bottomColor); - colors.Add(bottomColor); - colors.Add(bottomColor); - colors.Add(bottomColor); - - Rect coords = BlockUVs.GetUVFromTypeAndFace((BlockType)block, BlockFace.Bottom); - float yMax = coords.y + coords.height - epsilon; - float xMax = coords.x + coords.width - epsilon; - float xMin = coords.x + epsilon; - float yMin = coords.y + epsilon; - - uvs.Add(new Vector2(xMin, yMin)); - uvs.Add(new Vector2(xMin, yMax)); - uvs.Add(new Vector2(xMax, yMax)); - uvs.Add(new Vector2(xMax, yMin)); - } - } - } - } - - //meshFilter.mesh.Clear(); - - //meshFilter.mesh.vertices = vertices.ToArray(); - if(colors.Count != meshFilter.mesh.vertices.Length) - UnityEngine.Debug.Log("vertices: " + meshFilter.mesh.vertices.Length + " colors: " + colors.Count); - //meshFilter.mesh.triangles = triangles.ToArray(); - //meshFilter.mesh.uv = uvs.ToArray(); - meshFilter.mesh.colors = colors.ToArray(); - chunkSlice.ClearDirtyLight(); - } } } diff --git a/Assets/Scripts/World/ChunkSlice.cs b/Assets/Scripts/World/ChunkSlice.cs index 1309072..bb54dfa 100644 --- a/Assets/Scripts/World/ChunkSlice.cs +++ b/Assets/Scripts/World/ChunkSlice.cs @@ -12,7 +12,7 @@ namespace Brickcraft.World public static readonly short TotalBlockNumber = (short)(SizeX * SizeY * SizeZ); - private byte[] _Types = new byte[SizeX*SizeY*SizeZ]; + private byte[] _Types = new byte[SizeX * SizeY * SizeZ]; private NibbleArray _Skylight = new NibbleArray(TotalBlockNumber / 2); private short solidBlocks; diff --git a/Assets/Scripts/World/WorldBehaviour.cs b/Assets/Scripts/World/WorldBehaviour.cs index 58f0a7b..b528fdf 100644 --- a/Assets/Scripts/World/WorldBehaviour.cs +++ b/Assets/Scripts/World/WorldBehaviour.cs @@ -60,9 +60,6 @@ namespace Brickcraft.World Debug.Log("Terrain texture not loaded"); } - GameObject camera = Camera.main.gameObject; - camera.transform.position = new Vector3(0,160,0); - ChunkMeshThreadEntry[] chunkEntries = new ChunkMeshThreadEntry[ChunksNum]; ChunkGenManager chunkGenManager = new ChunkGenManager(MapMinChunkX, MapMaxChunkX + 1, 6, this, 13284938921, chunkEntries); @@ -84,51 +81,24 @@ namespace Brickcraft.World { return (ushort)((x + 127) << 8 | (z + 127)); } - - private void CalculateStartLight() - { - for(int x = MapMinChunkX; x < MapMaxChunkX + 1; ++x) - { - for(int z = MapMinChunkX; z < MapMaxChunkX + 1; ++z) - { - Chunk chunk = ChunksMap[ChunkIndexFromCoords(x,z)]; - chunk.RecalculateSkyLight(); - } - } - } - - private void CalculateTestLight() - { - for(int x = MapMinChunkX; x < MapMaxChunkX; ++x) - { - for(int z = MapMinChunkX; z < MapMaxChunkX; ++z) - { - Chunk chunk = ChunksMap[ChunkIndexFromCoords(x,z)]; - chunk.RecalculateTestSkyLight(); - } - } - } - - void Update () { - /* - foreach (Chunk chunk in ChunksMap) { - if (chunk == null) { - continue; - } - foreach (ChunkSlice slice in chunk.Slices) { - if (slice.IsEmpty) { + + /* + void Update () { + + foreach (Chunk chunk in ChunksMap) { + if (chunk == null) { continue; } - slice.FrustrumCulling(); + foreach (ChunkSlice slice in chunk.Slices) { + if (slice.IsEmpty) { + continue; + } + slice.FrustrumCulling(); + } } - }*/ - - if (Input.GetKeyUp(KeyCode.Escape)) - Application.Quit(); - else if(Input.GetKeyUp(KeyCode.L)) - CalculateTestLight(); - } - + } + */ + void FixedUpdate() { ++accumulator; @@ -146,6 +116,10 @@ namespace Brickcraft.World { ChunkSliceBuildEntry chunkEntry = ChunkSlicesWorkingQueue.Dequeue(); BuildChunkSliceMesh(chunkEntry); + + if (ChunkSlicesWorkingQueue.Count == 0) { + Server.Instance.spawnPlayer(); + } } lock(SliceLock) @@ -247,6 +221,7 @@ namespace Brickcraft.World mesh.RecalculateBounds(); filter.mesh = mesh; + chunkSliceObject.GetComponent().sharedMesh = mesh; } chunkEntry.ParentChunk.ClearDirtySlices(); } @@ -266,7 +241,38 @@ namespace Brickcraft.World public Chunk GetChunk(int x, int z) { - return ChunksMap[ChunkIndexFromCoords(x,z)]; + return ChunksMap[ChunkIndexFromCoords(x, z)]; + } + + public static Chunk WorldCoordinateToChunk(Vector3 coordinates) { + coordinates /= Chunk.SliceHeight; + return ChunksMap[ChunkIndexFromCoords(Mathf.RoundToInt(coordinates.x), Mathf.RoundToInt(coordinates.z))]; + } + + public static BlockType WorldCoordinateToBlock(Vector3 coordinates) { + Chunk chunk = WorldCoordinateToChunk(coordinates); + coordinates /= Chunk.SliceHeight; + + if (chunk == null) { + return BlockType.NULL; // We return NULL so that is different from air and we don't build side faces of the blocks + } + + return chunk.GetBlockType(Mathf.RoundToInt(coordinates.x) & 0xF, Mathf.RoundToInt(coordinates.y), Mathf.RoundToInt(coordinates.z) & 0xF); + + /* + return new Vector3Int( + Chunk.CorrectBlockCoordinate(Mathf.RoundToInt(coordinates.x % Chunk.SliceHeight)), + Chunk.CorrectBlockCoordinate(Mathf.RoundToInt(coordinates.y % Chunk.SliceHeight)), + Chunk.CorrectBlockCoordinate(Mathf.RoundToInt(coordinates.z % Chunk.SliceHeight)) + );*/ + } + + public static Vector3 WorldCoordinateToBlockCoordinate(Vector3 coordinates) { + return new Vector3( + Mathf.RoundToInt(coordinates.x) * Server.brickWidth, + Mathf.RoundToInt(coordinates.y) * Server.brickHeight, + Mathf.RoundToInt(coordinates.z) * Server.brickWidth + ); } } }