Skip to content

Commit

Permalink
Update Documentation
Browse files Browse the repository at this point in the history
- Removed the constructor of CullingGroupKeyDefinition.
- Renamed from CameraSourceMode to CameraReferenceMode.
- Updated some documents.
  • Loading branch information
mackysoft committed Mar 16, 2021
1 parent 60803e5 commit 0b0a162
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public override void OnGUI (Rect position,SerializedProperty property,GUIContent
SerializedProperty mode = property.FindPropertyRelative("m_Mode");

if (!mode.hasMultipleDifferentValues) {
switch ((CameraSourceMode)mode.enumValueIndex) {
case CameraSourceMode.TaggedCamera:
switch ((CameraReferenceMode)mode.enumValueIndex) {
case CameraReferenceMode.TaggedCamera:
position.width *= 0.5f;
EditorGUI.PropertyField(new Rect(position.xMin,position.yMin,position.width,position.height),mode,GUIContent.none);
SerializedProperty tag = property.FindPropertyRelative("m_Tag");
Expand All @@ -24,7 +24,7 @@ public override void OnGUI (Rect position,SerializedProperty property,GUIContent
tag.stringValue = EditorGUI.TagField(new Rect(position.xMin + position.width,position.yMin,position.width,position.height),GUIContent.none,tag.stringValue);
EditorGUI.showMixedValue = false;
break;
case CameraSourceMode.Custom:
case CameraReferenceMode.Custom:
position.width *= 0.5f;
EditorGUI.PropertyField(new Rect(position.xMin,position.yMin,position.width,position.height),mode,GUIContent.none);
SerializedProperty customCamera = property.FindPropertyRelative("m_CustomCamera");
Expand Down
19 changes: 11 additions & 8 deletions Assets/MackySoft/MackySoft.Vision/Runtime/CullingGroupKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

namespace MackySoft.Vision {

/// <summary>
/// <para> Definition of <see cref="CullingGroupKey"/>. It can be defined in <see cref="VisionSettings"/>. </para>
/// <para> Basically, it is not used directly by the user. </para>
/// </summary>
[Serializable]
public class CullingGroupKeyDefinition {

Expand All @@ -13,19 +17,18 @@ public class CullingGroupKeyDefinition {

public string Name { get => m_Name; set => m_Name = value; }

public CullingGroupKeyDefinition () : this(string.Empty) {

}

public CullingGroupKeyDefinition (string name) {
m_Name = name;
}

}

/// <summary>
/// <para> Key to find the <see cref="CullingGroupProxy"/>. The essence of a key is an index. </para>
/// <para> The keys are defined in <see cref="VisionSettings"/>. </para>
/// </summary>
[Serializable]
public struct CullingGroupKey : IEquatable<CullingGroupKey> {

/// <summary>
/// Key with index -1.
/// </summary>
public static readonly CullingGroupKey None = new CullingGroupKey(-1);

[SerializeField]
Expand Down
22 changes: 21 additions & 1 deletion Assets/MackySoft/MackySoft.Vision/Runtime/CullingGroupProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,14 @@ public float[] BoundingDistances {
}
}

/// <summary>
/// All culling targets.
/// </summary>
public IReadOnlyList<ICullingTarget> Targets => m_Targets;

/// <summary>
/// BoundingSphere of all culling targets.
/// </summary>
public IReadOnlyList<BoundingSphere> BoundingSpheres => m_BoundingSpheres;

#endregion
Expand Down Expand Up @@ -331,16 +337,30 @@ public void UpdateDynamicBoundingSphereTransforms () {
}
}
}


/// <summary>
/// Get the enabled CullingGroupProxy with key.
/// </summary>
/// <param name="key"> Key to get the CullingGroupProxy. </param>
/// <returns> Returns a CullingGroupProxy with the same key as the specified key. </returns>
public static CullingGroupProxy GetGroup (CullingGroupKey key) {
return (key >= 0) ? m_Groups[key] : null;
}

/// <summary>
/// Try to get the enabled CullingGroupProxy with key.
/// </summary>
/// <param name="key"> Key to get the CullingGroupProxy. </param>
/// <param name="result"> The CullingGroupProxy with the same key as the specified key. </param>
/// <returns> True if the CullingGroupProxy could be got, False otherwise. </returns>
public static bool TryGetGroup (CullingGroupKey key,out CullingGroupProxy result) {
result = GetGroup(key);
return result != null;
}

/// <summary>
/// Call the <see cref="UpdateTargets"/> method of all CullingGroupProxy's.
/// </summary>
public static void UpdateAllGroupTargets () {
for (int i = 0;m_Groups.Length > i;i++) {
CullingGroupProxy group = m_Groups[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public TransformUpdateMode BoundingSphereUpdateMode {

#region Events

/// <summary>
/// Sets the callback that will be called when a sphere visibility and/or distance state has changed.
/// </summary>
public CullingGroup.StateChanged OnStateChanged { get; set; }

#endregion
Expand Down Expand Up @@ -115,10 +118,18 @@ void OnDisable () {

#endregion

/// <summary>
/// Returns true if the bounding sphere is currently visible from any of the contributing cameras.
/// </summary>
/// <returns> True if the bounding sphere is visible, false if it is invisible. </returns>
public bool IsVisible () {
return (m_Group == null) || m_Group.IsVisible(this);
}

/// <summary>
/// Get the current distance band index of the bounding sphere.
/// </summary>
/// <returns> The sphere current distance band index. </returns>
public int GetDistance () {
return (m_Group != null) ? m_Group.GetDistance(this) : -1;
}
Expand Down
12 changes: 11 additions & 1 deletion Assets/MackySoft/MackySoft.Vision/Runtime/ICullingTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

namespace MackySoft.Vision {

/// <summary>
/// <para> When to update the transform. </para>
/// </summary>
public enum TransformUpdateMode {
/// <summary>
/// Transform an always updated.
/// </summary>
Dynamic = 0,

/// <summary>
/// Transform is updated by the first update or by explicitly calling the update method.
/// </summary>
Static = 1
}

public interface ICullingTarget {

/// <summary>
/// <para> When to update the bounding sphere transform. </para>
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@

namespace MackySoft.Vision.Utilities {

public enum CameraSourceMode {
public enum CameraReferenceMode {
CurrentCamera = 0,
MainCamera = 1,
TaggedCamera = 2,
Custom = 3
}

/// <summary>
/// <para> Advanced camera reference. </para>
/// <para> Useful when referencing camera from multiple scenes and prefabs. </para>
/// </summary>
[Serializable]
public class CameraReference {

[SerializeField]
CameraSourceMode m_Mode = CameraSourceMode.MainCamera;
CameraReferenceMode m_Mode = CameraReferenceMode.MainCamera;

[SerializeField]
string m_Tag = "Untagged";
Expand All @@ -26,7 +30,10 @@ public class CameraReference {

Camera m_CachedCamera;

public CameraSourceMode Mode {
/// <summary>
/// Method of referring to the camera.
/// </summary>
public CameraReferenceMode Mode {
get => m_Mode;
set {
if (m_Mode != value) {
Expand All @@ -36,28 +43,34 @@ public CameraSourceMode Mode {
}
}

/// <summary>
/// A tag for referencing a camera when <see cref="Mode"/> is <see cref="CameraReferenceMode.TaggedCamera"/>.
/// </summary>
public string Tag {
get => m_Tag;
set {
if (m_Mode == CameraSourceMode.TaggedCamera) {
if (m_Mode == CameraReferenceMode.TaggedCamera) {
ClearCache();
}
m_Tag = value;
}
}

/// <summary>
/// Reference to the camera when <see cref="Mode"/> is <see cref="CameraReferenceMode.Custom"/>.
/// </summary>
public Camera CustomCamera {
get => m_CustomCamera;
set {
if (m_Mode == CameraSourceMode.Custom) {
if (m_Mode == CameraReferenceMode.Custom) {
ClearCache();
}
m_CustomCamera = value;
}
}

/// <summary>
/// <para> Returns a camera based on the <see cref="CameraSourceMode"/>. </para>
/// <para> Returns a camera based on the <see cref="CameraReferenceMode"/>. </para>
/// <para> The retrieved camera will be cached. </para>
/// </summary>
public Camera Camera {
Expand All @@ -66,11 +79,11 @@ public Camera Camera {
return m_CachedCamera;
}
switch (m_Mode) {
case CameraSourceMode.CurrentCamera:
case CameraReferenceMode.CurrentCamera:
return m_CachedCamera = Camera.current;
case CameraSourceMode.MainCamera:
case CameraReferenceMode.MainCamera:
return m_CachedCamera = Camera.main;
case CameraSourceMode.TaggedCamera:
case CameraReferenceMode.TaggedCamera:
Camera[] allCameras = Camera.allCameras;
for (int i = 0;allCameras.Length > i;i++) {
Camera camera = allCameras[i];
Expand All @@ -79,29 +92,29 @@ public Camera Camera {
}
}
return m_CachedCamera = null;
case CameraSourceMode.Custom:
case CameraReferenceMode.Custom:
return m_CachedCamera = m_CustomCamera;
default:
throw new NotImplementedException();
}
}
}

public CameraReference () : this(CameraSourceMode.MainCamera) {
public CameraReference () : this(CameraReferenceMode.MainCamera) {

}

public CameraReference (CameraSourceMode source) {
m_Mode = source;
public CameraReference (CameraReferenceMode mode) {
m_Mode = mode;
}

public CameraReference (Camera customCamera) {
m_Mode = CameraSourceMode.Custom;
m_Mode = CameraReferenceMode.Custom;
m_CustomCamera = customCamera;
}

public CameraReference (string tag) {
m_Mode = CameraSourceMode.TaggedCamera;
m_Mode = CameraReferenceMode.TaggedCamera;
m_Tag = tag;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,61 @@

namespace MackySoft.Vision.Utilities {

/// <summary>
/// A useful component for linking <see cref="CullingGroupProxy"/> and camera that exist in separate scenes.
/// </summary>
[AddComponentMenu("MackySoft/Vision/Culling Group Target Camera Setter")]
[HelpURL("https://github.com/mackysoft/Vision")]
public class CullingGroupTargetCameraSetter : MonoBehaviour {

[Tooltip("Key to reference the CullingGroupProxy")]
[SerializeField]
CullingGroupKey m_GroupKey = CullingGroupKey.None;

[Tooltip("A reference to the camera you want to set to CullingGroupProxy.TargetCamera")]
[SerializeField]
CameraReference m_TargetCamera = new CameraReference(CameraSourceMode.MainCamera);
CameraReference m_TargetCamera = new CameraReference(CameraReferenceMode.MainCamera);

void Start () {
[SerializeField]
bool m_LinkOnEnableOrStart = true;

/// <summary>
/// Key to reference the <see cref="CullingGroupProxy"/>.
/// </summary>
public CullingGroupKey GroupKey { get => m_GroupKey; set => m_GroupKey = value; }

/// <summary>
/// A reference to the camera you want to set to <see cref="CullingGroupProxy.TargetCamera"/>.
/// </summary>
public CameraReference TargetCamera { get => m_TargetCamera; set => m_TargetCamera = value; }

public bool LinkOnEnableOrStart { get => m_LinkOnEnableOrStart; set => m_LinkOnEnableOrStart = value; }

/// <summary>
/// <para> Set the specified camera to the <see cref="CullingGroupProxy.TargetCamera"/> of the specified <see cref="CullingGroupProxy"/>. </para>
/// <para> See: <see cref="GroupKey"/>, <see cref="TargetCamera"/> </para>
/// </summary>
public void Link () {
if ((m_TargetCamera.Camera != null) && CullingGroupProxy.TryGetGroup(m_GroupKey,out CullingGroupProxy group)) {
group.TargetCamera = m_TargetCamera;
}
}

void OnEnable () {
Link();
}

void Start () {
Link();
}

#if UNITY_EDITOR
void Reset () {
if (TryGetComponent(out CullingGroupProxy group)) {
m_GroupKey = group.Key;
}
if (TryGetComponent(out Camera camera)) {
m_TargetCamera.Mode = CameraSourceMode.Custom;
m_TargetCamera.Mode = CameraReferenceMode.Custom;
m_TargetCamera.CustomCamera = camera;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class CullingTargetRenderers : MonoBehaviour {

ICullingTarget m_CullingTarget;

/// <summary>
/// A renderer's to enable/disable.
/// </summary>
public Renderer[] Renderers {
get => m_Renderers;
set => m_Renderers = value;
Expand Down
Loading

0 comments on commit 0b0a162

Please sign in to comment.