Rasagar/Library/PackageCache/com.unity.visualeffectgraph/Runtime/Utilities/PropertyBinding/Implementation/VFXSphereBinder.cs
2024-08-26 23:07:20 +03:00

75 lines
2.5 KiB
C#

#if VFX_HAS_PHYSICS
using UnityEngine.VFX;
namespace UnityEngine.VFX.Utility
{
[AddComponentMenu("VFX/Property Binders/Sphere Collider Binder")]
[VFXBinder("Collider/Sphere")]
class VFXSphereBinder : VFXSpaceableBinder
{
public string Property { get { return (string)m_Property; } set { m_Property = value; UpdateSubProperties(); } }
[VFXPropertyBinding("UnityEditor.VFX.Sphere", "UnityEditor.VFX.TSphere"), SerializeField, UnityEngine.Serialization.FormerlySerializedAs("m_Parameter")]
protected ExposedProperty m_Property = "Sphere";
public SphereCollider Target = null;
private ExposedProperty m_Old_Center;
private ExposedProperty m_New_Center;
private ExposedProperty m_Radius;
protected override void OnEnable()
{
base.OnEnable();
UpdateSubProperties();
}
void OnValidate()
{
UpdateSubProperties();
}
void UpdateSubProperties()
{
//Support the new "TSphere" structure and the previous "Sphere" type
m_Old_Center = m_Property + "_center";
m_New_Center = m_Property + "_transform_position";
m_Radius = m_Property + "_radius";
}
public override bool IsValid(VisualEffect component)
{
return Target != null
&& (component.HasVector3(m_New_Center) || component.HasVector3(m_Old_Center))
&& component.HasFloat(m_Radius);
}
public override void UpdateBinding(VisualEffect component)
{
var actualCenterProperty = m_New_Center;
if (!component.HasVector3(m_New_Center))
actualCenterProperty = m_Old_Center;
ApplySpaceTS(component, actualCenterProperty, Target.transform, out var transformCenter, out var transformScale);
var center = transformCenter + Target.center;
if (m_New_Center == actualCenterProperty)
component.SetVector3(m_New_Center, center);
else
component.SetVector3(m_Old_Center, center);
component.SetFloat(m_Radius, Target.radius * GetSphereColliderScale(transformScale));
}
public float GetSphereColliderScale(Vector3 scale)
{
return Mathf.Max(scale.x, Mathf.Max(scale.y, scale.z));
}
public override string ToString()
{
return string.Format("Sphere : '{0}' -> {1}", m_Property, Target == null ? "(null)" : Target.name);
}
}
}
#endif