1
This commit is contained in:
@@ -16,31 +16,6 @@ namespace BITKit.OpenWorld
|
||||
base.OnLodChanged(oldLod, newLod);
|
||||
OnLodChangedEvent?.Invoke(oldLod,newLod);
|
||||
}
|
||||
|
||||
[BIT]
|
||||
private void CalculateBounds()
|
||||
{
|
||||
if(TryGetComponent<Collider>(out var _collider))
|
||||
{
|
||||
var bounds1 = _collider.bounds;
|
||||
size = bounds1.size;
|
||||
offset = bounds1.center - transform.position;
|
||||
|
||||
return;
|
||||
}
|
||||
Bounds bounds = new();
|
||||
foreach (var x in GetComponentsInChildren<Collider>())
|
||||
{
|
||||
bounds.Encapsulate(x.bounds);
|
||||
if (x.bounds.size.sqrMagnitude > 64)
|
||||
{
|
||||
Debug.LogWarning($"{x.gameObject.name}:Size is too large, please check the size of the collider");
|
||||
}
|
||||
}
|
||||
size = bounds.size;
|
||||
offset = bounds.center - transform.position;
|
||||
EditorUtility.SetDirty(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,55 +1,97 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.NetworkInformation;
|
||||
using Quadtree;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.OpenWorld
|
||||
{
|
||||
public class ChunkObject : MonoBehaviour,IWorldChunkObject
|
||||
public class ChunkObject : MonoBehaviour, IWorldChunkObject
|
||||
{
|
||||
[SerializeField,ReadOnly] private int _lod = -1;
|
||||
[SerializeField,ReadOnly] private int id;
|
||||
[SerializeField] protected Vector3 size = Vector3.one;
|
||||
[SerializeField] protected Vector3 offset = Vector3.one * 0.5f;
|
||||
[SerializeField, ReadOnly] private int _lod = -1;
|
||||
[SerializeField, ReadOnly] private int id;
|
||||
public Bounds GetBounds() => _bounds;
|
||||
private Bounds _bounds;
|
||||
public Node<IWorldChunkObject> ParentNode { get; set; }
|
||||
|
||||
public void QuadTree_Root_Initialized(IQuadtreeRoot<IWorldChunkObject, Node<IWorldChunkObject>> root)
|
||||
{
|
||||
}
|
||||
|
||||
public int Id
|
||||
{
|
||||
get=>id;
|
||||
set=>id = value;
|
||||
get => id;
|
||||
set => id = value;
|
||||
}
|
||||
|
||||
public int Lod
|
||||
{
|
||||
get=>_lod;
|
||||
set => OnLodChanged(_lod,_lod=value);
|
||||
get => _lod;
|
||||
set => OnLodChanged(_lod, _lod = value);
|
||||
}
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
_bounds= new Bounds(transform.position+transform.rotation * offset,transform.rotation * size);
|
||||
CalculateBounds();
|
||||
UnityWorldChunkService.Singleton.Register(this);
|
||||
destroyCancellationToken.Register(Dispose);
|
||||
}
|
||||
|
||||
private void Dispose()
|
||||
{
|
||||
UnityWorldChunkService.Singleton.Unregister(this);
|
||||
}
|
||||
|
||||
protected virtual void OnLodChanged(int oldLod, int newLod)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
var bounds = GetBounds();
|
||||
if (_bounds.size.magnitude is 0)
|
||||
CalculateBounds();
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawWireCube(bounds.center, bounds.size);
|
||||
Gizmos.DrawWireCube(_bounds.center, _bounds.size);
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
[BIT]
|
||||
#endif
|
||||
private void CalculateBounds()
|
||||
{
|
||||
if (TryGetComponent<Collider>(out var _collider))
|
||||
{
|
||||
_bounds = _collider.bounds;
|
||||
return;
|
||||
}
|
||||
|
||||
_bounds = new Bounds();
|
||||
var colliders = GetComponentsInChildren<Collider>();
|
||||
|
||||
if (colliders.Length is 0)
|
||||
{
|
||||
Debug.LogWarning($"{gameObject.name}:No collider found");
|
||||
return;
|
||||
}
|
||||
|
||||
var reference = colliders.First();
|
||||
_bounds = reference.bounds;
|
||||
foreach (var x in colliders)
|
||||
{
|
||||
_bounds.Encapsulate(x.bounds);
|
||||
if (x.bounds.size.sqrMagnitude > 64)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"{x.gameObject.name}:Size is too large, please check the size of the collider");
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.EditorUtility.SetDirty(this);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -16,10 +16,15 @@ namespace BITKit.OpenWorld
|
||||
{
|
||||
[SerializeField] private Collider[] colliders;
|
||||
[SerializeField] private ChunkBehaviour chunkBehaviour;
|
||||
[SerializeField] private bool getCollidersOnStart;
|
||||
private void Start()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (getCollidersOnStart)
|
||||
{
|
||||
GetCollidersInChildren();
|
||||
}
|
||||
chunkBehaviour.OnLodChangedEvent += OnLodChanged;
|
||||
if(colliders is {Length:0})colliders = GetComponentsInChildren<Collider>();
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BITKit.SceneManagement;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Quadtree;
|
||||
@@ -56,7 +57,6 @@ namespace BITKit.OpenWorld
|
||||
await UniTask.NextFrame();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Dispose()
|
||||
{
|
||||
sceneService?.UnRegisterLoadTaskAsync(LoadTask);
|
||||
@@ -64,6 +64,7 @@ namespace BITKit.OpenWorld
|
||||
_registerQueue.Clear();
|
||||
_unregisterQueue.Clear();
|
||||
}
|
||||
protected virtual int CalculateLod(IWorldChunkObject value,Vector3 cameraPosition,int lod)=>lod;
|
||||
protected virtual async void OnTick(float deltaTime)
|
||||
{
|
||||
if (!enabled) return;
|
||||
@@ -88,7 +89,8 @@ namespace BITKit.OpenWorld
|
||||
var cycle = 0;
|
||||
|
||||
await UniTask.SwitchToThreadPool();
|
||||
|
||||
|
||||
var items = _quadtree.Find(new Bounds(cameraPosition, Vector3.one * (lodDistances[^1] + lodDistances[0])));
|
||||
foreach (var chunkObject in _quadtree.Find(new Bounds(cameraPosition, Vector3.one * (lodDistances[^1]+lodDistances[0]))))
|
||||
{
|
||||
if (cycle++ > 64)
|
||||
@@ -100,17 +102,25 @@ namespace BITKit.OpenWorld
|
||||
|
||||
var distance = Vector3.Distance(cameraPosition, chunkObject.GetBounds().center);
|
||||
//var distance = Vector3.Distance(cameraPosition, chunkObject.GetBounds().ClosestPoint(cameraPosition));
|
||||
if (cacheList.Contains(chunkObject.Id)) continue;
|
||||
cacheList.Add(chunkObject.Id);
|
||||
if (!cacheList.Add(chunkObject.Id)) continue;
|
||||
|
||||
var lod = -1;
|
||||
|
||||
for (var i = 0; i < lodDistances.Length; i++)
|
||||
if (chunkObject.GetBounds().Contains(cameraPosition))
|
||||
{
|
||||
if (!(distance < lodDistances[i])) continue;
|
||||
lod = i;
|
||||
break;
|
||||
lod = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < lodDistances.Length; i++)
|
||||
{
|
||||
if (!(distance < lodDistances[i])) continue;
|
||||
lod = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
lod = CalculateLod(chunkObject,cameraPosition,lod);
|
||||
|
||||
if(chunkObject.Lod==lod)continue;
|
||||
//chunkObject.Lod = lod;
|
||||
|
@@ -3,7 +3,9 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Quadtree;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor.SceneManagement;
|
||||
#endif
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using YooAsset;
|
||||
@@ -16,9 +18,7 @@ namespace BITKit.OpenWorld
|
||||
[SerializeField] private Vector3 size;
|
||||
[SerializeField] private Vector3 position;
|
||||
[SerializeField, ReadOnly] private int lod = -1;
|
||||
|
||||
private SceneHandle _sceneHandle;
|
||||
|
||||
[BIT]
|
||||
public async void Load()
|
||||
{
|
||||
|
@@ -6,5 +6,11 @@ namespace BITKit.OpenWorld
|
||||
{
|
||||
public class WorldTerrainService : WorldChunkService<WorldTerrainService>
|
||||
{
|
||||
protected override int CalculateLod(IWorldChunkObject value, Vector3 cameraPosition, int lod)
|
||||
{
|
||||
if (lod is not 0) return lod;
|
||||
|
||||
return value.GetBounds().Contains(cameraPosition) ? 0 : 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user