From 448c52083e0b72aee5825079d85d73dbba72a970 Mon Sep 17 00:00:00 2001 From: huqibin <762612145@qq.com> Date: Mon, 26 May 2025 17:26:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=94=A5=E5=89=94=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Resources/Prefabs/高粱预制体.prefab | 8 +- .../UI/UI_Panel/UI_PPTForPracticePanel.prefab | 10 +- Assets/Scripts/HQB/SorghumFieldController.cs | 146 +++++++++++++----- Assets/Scripts/Project/Manager/GameManager.cs | 4 +- 4 files changed, 120 insertions(+), 48 deletions(-) diff --git a/Assets/Resources/Prefabs/高粱预制体.prefab b/Assets/Resources/Prefabs/高粱预制体.prefab index f1958b2..baa1e98 100644 --- a/Assets/Resources/Prefabs/高粱预制体.prefab +++ b/Assets/Resources/Prefabs/高粱预制体.prefab @@ -244,7 +244,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: -7511558181221131132, guid: 51c3521496de1854697d5682cc43afe5, type: 3} propertyPath: m_Enabled - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 919132149155446097, guid: 51c3521496de1854697d5682cc43afe5, type: 3} propertyPath: m_Name @@ -252,7 +252,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 919132149155446097, guid: 51c3521496de1854697d5682cc43afe5, type: 3} propertyPath: m_IsActive - value: 1 + value: 0 objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 51c3521496de1854697d5682cc43afe5, type: 3} @@ -319,7 +319,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: -7511558181221131132, guid: 0fc9aae39f3b8fd45959b9f2e382a854, type: 3} propertyPath: m_Enabled - value: 0 + value: 1 objectReference: {fileID: 0} - target: {fileID: 919132149155446097, guid: 0fc9aae39f3b8fd45959b9f2e382a854, type: 3} propertyPath: m_Name @@ -327,7 +327,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 919132149155446097, guid: 0fc9aae39f3b8fd45959b9f2e382a854, type: 3} propertyPath: m_IsActive - value: 1 + value: 0 objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 0fc9aae39f3b8fd45959b9f2e382a854, type: 3} diff --git a/Assets/Resources/UI/UI_Panel/UI_PPTForPracticePanel.prefab b/Assets/Resources/UI/UI_Panel/UI_PPTForPracticePanel.prefab index e786061..d846639 100644 --- a/Assets/Resources/UI/UI_Panel/UI_PPTForPracticePanel.prefab +++ b/Assets/Resources/UI/UI_Panel/UI_PPTForPracticePanel.prefab @@ -130,7 +130,7 @@ RectTransform: m_AnchorMin: {x: 0, y: 1} m_AnchorMax: {x: 1, y: 1} m_AnchoredPosition: {x: 0, y: 0.00018310547} - m_SizeDelta: {x: 0, y: 0} + m_SizeDelta: {x: -236, y: 12} m_Pivot: {x: 0, y: 1} --- !u!114 &235241195840539889 MonoBehaviour: @@ -633,7 +633,7 @@ RectTransform: m_AnchorMin: {x: 0, y: 1} m_AnchorMax: {x: 1, y: 1} m_AnchoredPosition: {x: 0, y: 0} - m_SizeDelta: {x: 0, y: 0} + m_SizeDelta: {x: 17, y: 940} m_Pivot: {x: 0, y: 1} --- !u!114 &6919009669462823272 MonoBehaviour: @@ -900,9 +900,9 @@ RectTransform: m_Father: {fileID: 5924775712395334843} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 0} - m_AnchorMax: {x: 0, y: 0} - m_AnchoredPosition: {x: 0, y: 0} + m_AnchorMin: {x: 0, y: 1} + m_AnchorMax: {x: 0, y: 1} + m_AnchoredPosition: {x: 804, y: 0} m_SizeDelta: {x: 1608, y: 940} m_Pivot: {x: 0.5, y: 1} --- !u!222 &6109062945893642881 diff --git a/Assets/Scripts/HQB/SorghumFieldController.cs b/Assets/Scripts/HQB/SorghumFieldController.cs index 32daa6d..b172b61 100644 --- a/Assets/Scripts/HQB/SorghumFieldController.cs +++ b/Assets/Scripts/HQB/SorghumFieldController.cs @@ -5,68 +5,87 @@ using UnityEngine; public class SorghumFieldController : MonoBehaviour { + // ԭֶαֲ public MeshRenderer fieldArea; - //public GameObject SorghumPrefab; public bool randomRotate = false; public float rowDistance = 1; public float strainDistance = 1; public float diseasedRate = 0; - public void InitSorghum(float RowDistance, float StrainDistance, float DiseasedRate, bool RandomRotate = false) - { + // ̬Ⱦֶ + [Header("Dynamic Rendering")] + public int chunksPerAxis = 10; // ÿ + private Dictionary> chunkMap = new Dictionary>(); + private float chunkSize; + private Bounds fieldBounds; - GameObject SorghumPrefab = Resources.Load("Prefabs/Ԥ"); - if (SorghumPrefab == null || fieldArea == null) + public void InitSorghum(float rowDistance, float strainDistance, float diseasedRate, bool randomRotate = false) + { + GameObject sorghumPrefab = Resources.Load("Prefabs/Ԥ"); + if (sorghumPrefab == null || fieldArea == null) { Debug.LogError("Ԥũδ䣡"); return; } - // ȡũİΧϢ BoxCollider fieldCollider = fieldArea.GetComponent(); if (fieldCollider == null) { Debug.LogError("ũȱBoxCollider"); return; } - Bounds bounds = fieldCollider.bounds; - // ũ߽ - float minX = bounds.min.x; - float maxX = bounds.max.x; - float minZ = bounds.min.z; - float maxZ = bounds.max.z; + // ʼũ + fieldBounds = fieldCollider.bounds; + chunkSize = Mathf.Max(fieldBounds.size.x, fieldBounds.size.z) / chunksPerAxis; - // ȡũ߶ȣΪƽ棩 - float groundY = fieldArea.transform.position.y; + // Ԥи䵽 + GenerateSorghumGrid(sorghumPrefab, rowDistance, strainDistance); + ProcessDiseasedPlants(diseasedRate, randomRotate); - // ũ - for (float x = minX; x <= maxX; x += RowDistance) + // ʼи + SetAllChunksVisibility(false); + } + + void GenerateSorghumGrid(GameObject prefab, float rowDist, float strainDist) + { + for (float x = fieldBounds.min.x; x <= fieldBounds.max.x; x += rowDist) { - // ֲɵ - for (float z = minZ; z <= maxZ; z += StrainDistance) + for (float z = fieldBounds.min.z; z <= fieldBounds.max.z; z += strainDist) { - // λãԶ߶ȣ - Vector3 spawnPos = new Vector3(x, groundY, z); - - // ʵصũ - GameObject obj = Instantiate( - SorghumPrefab, - spawnPos, - Quaternion.identity, - fieldArea.transform - ); + Vector3 spawnPos = new Vector3(x, fieldBounds.min.y, z); + GameObject obj = Instantiate(prefab, spawnPos, Quaternion.identity, fieldArea.transform); + + + AddToChunk(obj); } } + } + void AddToChunk(GameObject sorghum) + { + Vector2Int chunkCoord = new Vector2Int( + Mathf.FloorToInt((sorghum.transform.position.x - fieldBounds.min.x) / chunkSize), + Mathf.FloorToInt((sorghum.transform.position.z - fieldBounds.min.z) / chunkSize) + ); + + if (!chunkMap.ContainsKey(chunkCoord)) + { + chunkMap[chunkCoord] = new List(); + } + chunkMap[chunkCoord].Add(sorghum); + } + + void ProcessDiseasedPlants(float diseasedRate, bool randomRotate) + { SorghumController[] sorghumControllers = FindObjectsOfType(); - int DisCount = (int)(sorghumControllers.Length * DiseasedRate); + int DisCount = (int)(sorghumControllers.Length * diseasedRate); for (int i = 0; i < DisCount; i++) { int iter = (int)(sorghumControllers.Length * UnityEngine.Random.value); if (iter >= sorghumControllers.Length) iter = sorghumControllers.Length - 1; if (iter < 0) iter = 0; - sorghumControllers[iter].Init(SorghumState.Diseased, RandomRotate); + sorghumControllers[iter].Init(SorghumState.Diseased, randomRotate); sorghumControllers[iter] = sorghumControllers[sorghumControllers.Length - 1]; SorghumController[] newArry = new SorghumController[sorghumControllers.Length - 1]; Array.Copy(sorghumControllers, newArry, sorghumControllers.Length - 1); @@ -74,20 +93,73 @@ public class SorghumFieldController : MonoBehaviour foreach (var item in sorghumControllers) { - item.Init(SorghumState.Healthy, RandomRotate); + item.Init(SorghumState.Healthy, randomRotate); + item.gameObject.GetComponentInChildren().enabled = false; // ʼ } } + void SetAllChunksVisibility(bool visible) + { + foreach (var chunk in chunkMap.Values) + { + foreach (GameObject plant in chunk) + { + plant.GetComponentInChildren().enabled = visible; + } + } + } - //// Start is called before the first frame update void Start() { InitSorghum(rowDistance, strainDistance, diseasedRate, randomRotate); + StartCoroutine(VisibilityUpdateRoutine()); } - //// Update is called once per frame - //void Update() - //{ + IEnumerator VisibilityUpdateRoutine() + { + WaitForSeconds wait = new WaitForSeconds(0.1f); // ÿ0.1һ + Plane[] frustumPlanes = new Plane[6]; + Camera mainCam = Camera.main; - //} -} + while (true) + { + if (mainCam != null) + { + frustumPlanes = GeometryUtility.CalculateFrustumPlanes(mainCam); + + foreach (var chunk in chunkMap) + { + Bounds chunkBounds = CalculateChunkBounds(chunk.Key); + bool shouldVisible = GeometryUtility.TestPlanesAABB(frustumPlanes, chunkBounds); + SetChunkVisibility(chunk.Key, shouldVisible); + } + } + yield return wait; + } + } + + Bounds CalculateChunkBounds(Vector2Int coord) + { + Vector3 min = new Vector3( + fieldBounds.min.x + coord.x * chunkSize, + fieldBounds.min.y, + fieldBounds.min.z + coord.y * chunkSize + ); + + return new Bounds( + min + new Vector3(chunkSize / 2, 0, chunkSize / 2), + new Vector3(chunkSize, fieldBounds.size.y, chunkSize) + ); + } + + void SetChunkVisibility(Vector2Int coord, bool visible) + { + if (chunkMap.TryGetValue(coord, out List plants)) + { + foreach (GameObject plant in plants) + { + plant.GetComponentInChildren().enabled = visible; + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Project/Manager/GameManager.cs b/Assets/Scripts/Project/Manager/GameManager.cs index f475689..c0c0dd0 100644 --- a/Assets/Scripts/Project/Manager/GameManager.cs +++ b/Assets/Scripts/Project/Manager/GameManager.cs @@ -135,7 +135,7 @@ public class GameManager : SingletonAutoMono /// /// PPT管理类 /// - public static PPTManager PPTManager { get; private set; } + //public static PPTManager PPTManager { get; private set; } #endregion /// @@ -165,7 +165,7 @@ public class GameManager : SingletonAutoMono WorkorderMgr = WorkorderMgr.Instance; //工单管理初始化 FaultManager = FaultManager.Instance; MissionMgr = MissionMgr.Instance; - PPTManager = PPTManager.Instance; + //PPTManager = PPTManager.Instance; DataMgr.Init(); WorkorderMgr.Init(); NetMgr.Init(SendGet);