Skip to content
This repository has been archived by the owner on Nov 30, 2020. It is now read-only.

Feature: Use child colliders #227

Open
wants to merge 4 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions PostProcessing/Editor/PostProcessVolumeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public sealed class PostProcessVolumeEditor : BaseEditor<PostProcessVolume>

SerializedProperty m_IsGlobal;
SerializedProperty m_BlendRadius;
SerializedProperty m_UseChildColliders;
SerializedProperty m_Weight;
SerializedProperty m_Priority;

Expand All @@ -22,6 +23,7 @@ void OnEnable()

m_IsGlobal = FindProperty(x => x.isGlobal);
m_BlendRadius = FindProperty(x => x.blendDistance);
m_UseChildColliders = FindProperty (x => x.useChildColliders);
m_Weight = FindProperty(x => x.weight);
m_Priority = FindProperty(x => x.priority);

Expand All @@ -48,8 +50,11 @@ public override void OnInspectorGUI()

EditorGUILayout.PropertyField(m_IsGlobal);

if (!m_IsGlobal.boolValue) // Blend radius is not needed for global volumes
EditorGUILayout.PropertyField(m_BlendRadius);
if (!m_IsGlobal.boolValue)
{ // Blend radius is not needed for global volumes
EditorGUILayout.PropertyField (m_BlendRadius);
EditorGUILayout.PropertyField (m_UseChildColliders);
}

EditorGUILayout.PropertyField(m_Weight);
EditorGUILayout.PropertyField(m_Priority);
Expand Down
16 changes: 13 additions & 3 deletions PostProcessing/Runtime/PostProcessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ public void GetActiveVolumes(PostProcessLayer layer, List<PostProcessVolume> res

// If volume isn't global and has no collider, skip it as it's useless
var colliders = m_TempColliders;
volume.GetComponents(colliders);

if (volume.useChildColliders)
volume.GetComponentsInChildren (colliders);
else
volume.GetComponents(colliders);

if (colliders.Count == 0)
continue;

Expand Down Expand Up @@ -322,8 +327,13 @@ internal void UpdateSettings(PostProcessLayer postProcessLayer)

// If volume isn't global and has no collider, skip it as it's useless
var colliders = m_TempColliders;
volume.GetComponents(colliders);
if (colliders.Count == 0)

if (volume.useChildColliders)
volume.GetComponentsInChildren (colliders);
else
volume.GetComponents(colliders);

if (colliders.Count == 0)
continue;

// Find closest distance to volume, 0 means it's inside it
Expand Down
22 changes: 15 additions & 7 deletions PostProcessing/Runtime/PostProcessVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public sealed class PostProcessVolume : MonoBehaviour
[Min(0f), Tooltip("Outer distance to start blending from. A value of 0 means no blending and the volume overrides will be applied immediatly upon entry.")]
public float blendDistance = 0f;

[Tooltip("Should volumes be calculated based on colliders from children objects as well?")]
public bool useChildColliders = false;

[Range(0f, 1f), Tooltip("Total weight of this volume in the scene. 0 means it won't do anything, 1 means full effect.")]
public float weight = 1f;

Expand Down Expand Up @@ -156,7 +159,11 @@ void Update()
void OnDrawGizmos()
{
var colliders = m_TempColliders;
GetComponents(colliders);

if (useChildColliders)
GetComponentsInChildren (colliders);
else
GetComponents(colliders);

if (isGlobal || colliders == null)
return;
Expand All @@ -171,16 +178,16 @@ void OnDrawGizmos()
}
#endif

var scale = transform.localScale;
var invScale = new Vector3(1f / scale.x, 1f / scale.y, 1f / scale.z);
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, scale);

// Draw a separate gizmo for each collider
foreach (var collider in colliders)
{
if (!collider.enabled)
continue;

var scale = collider.transform.lossyScale;
var invScale = new Vector3(1f / scale.x, 1f / scale.y, 1f / scale.z);
Gizmos.matrix = Matrix4x4.TRS(collider.transform.position, collider.transform.rotation, scale);

// We'll just use scaling as an approximation for volume skin. It's far from being
// correct (and is completely wrong in some cases). Ultimately we'd use a distance
// field or at least a tesselate + push modifier on the collider's mesh to get a
Expand All @@ -193,8 +200,9 @@ void OnDrawGizmos()
if (type == typeof(BoxCollider))
{
var c = (BoxCollider)collider;
Gizmos.DrawCube(c.center, c.size);
Gizmos.DrawWireCube(c.center, c.size + invScale * blendDistance * 4f);

Gizmos.DrawCube(c.center, c.size);
Gizmos.DrawWireCube(c.center, c.size + invScale * blendDistance * 4f);
}
else if (type == typeof(SphereCollider))
{
Expand Down