1
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
@@ -8,32 +11,62 @@ namespace BITKit
|
||||
{
|
||||
public class LocationAdditive : MonoBehaviour
|
||||
{
|
||||
[SerializeReference,SubclassSelector] internal IReference autoReference;
|
||||
|
||||
[SerializeField] private bool disabledRotation;
|
||||
[SerializeField] private bool disabledPosition;
|
||||
|
||||
[SerializeField] private Vector3 positionWeight=Vector3.one;
|
||||
[SerializeField] private Vector3 rotationWeight=Vector3.one;
|
||||
|
||||
public Vector3 GlobalPosition { get; private set; }
|
||||
public Vector3 GlobalEuler { get; private set; }
|
||||
public Vector3 GlobalRotation { get; private set; }
|
||||
|
||||
[SerializeField] private bool debug;
|
||||
|
||||
[SerializeField,ReadOnly(HideLabel = true)] private string debugInfo;
|
||||
|
||||
private Vector3 currentPosition;
|
||||
private Vector3 currentEuler;
|
||||
private Quaternion currentRotation=Quaternion.identity;
|
||||
|
||||
private Vector3 globalPosition;
|
||||
private Quaternion globalRotation;
|
||||
private readonly Optional<Quaternion> globalRotation = new(Quaternion.identity);
|
||||
|
||||
public void AddPosition(Vector3 value)
|
||||
private Transform _transform;
|
||||
|
||||
private readonly StringBuilder reportBuilder = new();
|
||||
|
||||
public Vector3 Position
|
||||
{
|
||||
get => _transform.localPosition;
|
||||
set => _transform.localPosition = value;
|
||||
}
|
||||
public Quaternion Rotation
|
||||
{
|
||||
get => _transform.localRotation;
|
||||
set => _transform.localRotation = value;
|
||||
}
|
||||
|
||||
public void AddPosition(Vector3 value,string info=null)
|
||||
{
|
||||
currentPosition += value;
|
||||
if (debug)
|
||||
{
|
||||
reportBuilder.AppendLine($"{info}:{value}");
|
||||
}
|
||||
}
|
||||
|
||||
public void AddEuler(Vector3 value)
|
||||
// public void AddEuler(Vector3 value)
|
||||
// {
|
||||
// //currentEuler += value;
|
||||
// currentRotation *= Quaternion.Euler(value);
|
||||
// }
|
||||
public void AddRotation(Quaternion value, string info = null)
|
||||
{
|
||||
currentEuler += value;
|
||||
currentRotation *= value;
|
||||
if (debug)
|
||||
{
|
||||
reportBuilder.AppendLine($"{info}:{value}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void SetGlobalPosition(Vector3 value)
|
||||
{
|
||||
globalPosition = value;
|
||||
@@ -41,29 +74,34 @@ namespace BITKit
|
||||
|
||||
public void SetGlobalRotation(Quaternion value)
|
||||
{
|
||||
globalRotation = value;
|
||||
globalRotation.SetValueThenAllow(value);
|
||||
}
|
||||
|
||||
public void LateUpdate()
|
||||
private void Start()
|
||||
{
|
||||
_transform = transform;
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
currentEuler = Vector3.Scale(currentEuler, rotationWeight);
|
||||
currentPosition = Vector3.Scale(currentPosition, positionWeight);
|
||||
|
||||
|
||||
currentPosition = Vector3.ClampMagnitude(currentPosition, 4096);
|
||||
|
||||
if (disabledRotation is false)
|
||||
switch (currentEuler, globalRotation)
|
||||
{
|
||||
if (globalRotation.Allow)
|
||||
{
|
||||
case var (euler, rotation) when euler.IsDefault() is false && rotation.IsDefault():
|
||||
transform.localRotation = Quaternion.Euler(euler);
|
||||
break;
|
||||
case var (euler, rotation) when euler.IsDefault() && rotation.IsDefault() is false:
|
||||
transform.rotation = globalRotation = rotation;
|
||||
break;
|
||||
case var (euler, rotation) when euler.IsDefault() is false && rotation.IsDefault() is false:
|
||||
transform.rotation = globalRotation = rotation * Quaternion.Euler(euler);
|
||||
break;
|
||||
transform.rotation =globalRotation * currentRotation;
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.localRotation = currentRotation;
|
||||
}
|
||||
}
|
||||
|
||||
if (disabledPosition is false)
|
||||
{
|
||||
switch (currentPosition, globalPosition)
|
||||
{
|
||||
case var (position, global) when position.IsDefault() is false && global.IsDefault():
|
||||
@@ -76,9 +114,17 @@ namespace BITKit
|
||||
transform.position = globalPosition = global + position;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
currentEuler = Vector3.zero;
|
||||
currentRotation = Quaternion.identity;
|
||||
|
||||
currentPosition = Vector3.zero;
|
||||
|
||||
if (debug)
|
||||
{
|
||||
debugInfo = reportBuilder.ToString();
|
||||
reportBuilder.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,42 +1,71 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using JetBrains.Annotations;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public class LocationAdditiveElement : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private bool useLateUpdate;
|
||||
[SerializeField] private LocationAdditive locationAdditive;
|
||||
[SerializeField] private Vector3 positionWeight = Vector3.one;
|
||||
[SerializeField] private Vector3 eulerWeight = Vector3.one;
|
||||
|
||||
[SerializeReference,SubclassSelector] private IReference autoReference;
|
||||
[SerializeReference,SubclassSelector] private IReference nameReference;
|
||||
//[SerializeField] private Vector3 rotationAdditive = Vector3.one;
|
||||
private Transform Transform;
|
||||
|
||||
private string nameCache;
|
||||
private void Start()
|
||||
{
|
||||
nameCache = nameReference is not null?nameReference.Value:gameObject.name;
|
||||
Transform = transform;
|
||||
var components =
|
||||
GetComponentsInParent<LocationAdditive>(true)
|
||||
.SelectMany(x => x.GetComponentsInChildren<LocationAdditive>(true)).Distinct().ToArray();
|
||||
try
|
||||
{
|
||||
if (!locationAdditive)
|
||||
locationAdditive =components
|
||||
.First(x => x.autoReference is not null && x.autoReference.Value.Equals(autoReference.Value))!;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
StringBuilder reportBuilder = new();
|
||||
foreach (var x in components)
|
||||
{
|
||||
reportBuilder.AppendLine(x.autoReference is not null ? $"{x.autoReference.Value}@{x.transform.name}" : x.name);
|
||||
}
|
||||
BIT4Log.Log<LocationAdditiveElement>($"找不到对应的LocationAdditive:{autoReference.Value} \n{reportBuilder}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (useLateUpdate) return;
|
||||
Tick();
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (!useLateUpdate) return;
|
||||
Tick();
|
||||
}
|
||||
|
||||
private void Tick()
|
||||
{
|
||||
if (!locationAdditive) return;
|
||||
var localPosition = Transform.localPosition;
|
||||
var localEulerAngles = Transform.localEulerAngles;
|
||||
//locationAdditive.AddPosition(transform.localPosition);
|
||||
//locationAdditive.AddEuler(transform.localEulerAngles);
|
||||
locationAdditive.AddPosition(
|
||||
new Vector3
|
||||
{
|
||||
x = localPosition.x * positionWeight.x,
|
||||
y = localPosition.y * positionWeight.y,
|
||||
z = localPosition.z * positionWeight.z
|
||||
});
|
||||
locationAdditive.AddEuler(
|
||||
new Vector3
|
||||
{
|
||||
x = localEulerAngles.x * eulerWeight.x,
|
||||
y = localEulerAngles.y * eulerWeight.y,
|
||||
z = localEulerAngles.z * eulerWeight.z
|
||||
});
|
||||
|
||||
locationAdditive.AddPosition(Vector3.Scale(localPosition, positionWeight), nameReference?.Value);
|
||||
//locationAdditive.AddRotation(Transform.localRotation * Quaternion.Euler(rotationAdditive));
|
||||
locationAdditive.AddRotation(Transform.localRotation, nameCache);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user