2025-07-22 20:36:15 +08:00

117 lines
3.8 KiB
C#

using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
namespace X.Rendering.Feature
{
public class XRenderFeatureManager : MonoBehaviour
{
public static XRenderFeatureManager Instance
{
get; private set;
}
#region CapsuleAO
public NativeArray<Character> CharacterArray;
public NativeArray<Capsule> CapsuleArray;
public Transform TestLightTrans;
public Transform TestCharacterTrans;
public float TestW;
private Dictionary<Transform, CapsuleCollider[]> characterTransform2CapsuleCollider = new();
private int capsuleColliderCount = 0;
public void AddCapsuleAOCharacter(Transform transform)
{
var oldCapsuleArray = CapsuleArray;
var oldCharacterArray = CharacterArray;
var capsules = transform.GetComponentsInChildren<CapsuleCollider>(true);
characterTransform2CapsuleCollider.Add(transform, capsules);
capsuleColliderCount += capsules.Length;
CapsuleArray = new (capsuleColliderCount, Allocator.Persistent);
CharacterArray = new (characterTransform2CapsuleCollider.Count, Allocator.Persistent);
if (oldCharacterArray.IsCreated)
{
oldCharacterArray.Dispose();
oldCapsuleArray.Dispose();
}
}
public void RemoveCapsuleAOCharacter(Transform transform)
{
capsuleColliderCount -= characterTransform2CapsuleCollider[transform].Length;
characterTransform2CapsuleCollider.Remove(transform);
var oldCapsuleArray = CapsuleArray;
var oldCharacterArray = CharacterArray;
CapsuleArray = new(capsuleColliderCount, Allocator.Persistent);
CharacterArray = new(characterTransform2CapsuleCollider.Count, Allocator.Persistent);
if (oldCharacterArray.IsCreated)
{
oldCharacterArray.Dispose();
oldCapsuleArray.Dispose();
}
}
#endregion
private void Awake()
{
Instance = this;
}
private void Start()
{
AddCapsuleAOCharacter(TestCharacterTrans);
}
private void Update()
{
Transform transform = null;
int capsuleArrayIndex = 0;
int characterArrayIndex = 0;
int startID = 0;
Instance = this;
foreach (var item in characterTransform2CapsuleCollider)
{
for (int i = 0; i < item.Value.Length; i++)
{
var capsule = item.Value[i];
//capsule.direction
var a = capsule.center - new Vector3(0, capsule.height / 2, 0);
var b = capsule.center + new Vector3(0, capsule.height / 2, 0);
a = capsule.transform.TransformPoint(a);
b = capsule.transform.TransformPoint(b);
CapsuleArray[capsuleArrayIndex++] = new()
{
a = a,
b = b,
radius = capsule.radius,
};
}
if (transform != item.Key)
{
transform = item.Key;
CharacterArray[characterArrayIndex++] = new()
{
position = transform.position,
radius = 5,
lightDir = -new Vector4(TestLightTrans.position.x, TestLightTrans.position.y, TestLightTrans.position.z, -TestW),
startID = startID,
endID = capsuleArrayIndex,
};
startID = capsuleArrayIndex;
}
}
}
}
}