Skip to content

Commit

Permalink
feat(snapzones): added new editor visulization to snapzones
Browse files Browse the repository at this point in the history
feat(snapzones): added gizmo to snapzones
feat(snapzones): automatically created snapzones only snap the correct object

fix(snapzones): snapping not working correctly with offset items
fix(snapzones): improved rendering of highlights for snapzones
fix(snapzones): fast forward snappables now snap immediately (#44)
  • Loading branch information
SimonTheSourcerer committed Sep 1, 2020
1 parent 3a95551 commit 1e5d783
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 39 deletions.
4 changes: 4 additions & 0 deletions Editor/Interaction/SnapZoneEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Innoactive.CreatorEditor.XRInteraction
[CustomEditor(typeof(SnapZone))]
internal class SnapZoneEditor : Editor
{
private SerializedProperty showHighlightInEditor;
private SerializedProperty shownHighlightObject;
private SerializedProperty shownHighlightObjectColor;

Expand All @@ -33,13 +34,15 @@ private static class Tooltips
public static readonly GUIContent AttachTransform = new GUIContent("Attach Transform", "Attach Transform to use for this Interactor. Will create empty GameObject if none set.");
public static readonly GUIContent StartingSelectedInteractable = new GUIContent("Starting Selected Interactable", "Interactable that will be selected upon start.");

public static readonly GUIContent ShowHighlightInEditor = new GUIContent("Show Highlight in Editor", "Enable this to show how the object will be positioned later on.");
public static readonly GUIContent ShownHighlightObject = new GUIContent("Shown Highlight Object", "The game object whose mesh is drawn to emphasize the position of the snap zone. If none is supplied, no highlight object is shown.");
public static readonly GUIContent ShownHighlightObjectColor = new GUIContent("Shown Highlight Object Color", "The color of the material used to draw the \"Shown Highlight Object\". Use the alpha value to specify the degree of transparency.");
public static readonly GUIContent InteractableHoverMeshMaterial = new GUIContent("Validation Hover Material", "Material used for rendering interactable meshes on hover (a default material will be created if none is supplied).");
}

private void OnEnable()
{
showHighlightInEditor = serializedObject.FindProperty("ShowHighlightInEditor");
shownHighlightObject = serializedObject.FindProperty("shownHighlightObject");
shownHighlightObjectColor = serializedObject.FindProperty("shownHighlightObjectColor");

Expand Down Expand Up @@ -72,6 +75,7 @@ public override void OnInspectorGUI()
EditorGUILayout.Space();
EditorGUILayout.LabelField("Snap Zone", EditorStyles.boldLabel);

EditorGUILayout.PropertyField(showHighlightInEditor, Tooltips.ShowHighlightInEditor);
EditorGUILayout.PropertyField(shownHighlightObject, Tooltips.ShownHighlightObject);
EditorGUILayout.PropertyField(shownHighlightObjectColor, Tooltips.ShownHighlightObjectColor);
EditorGUILayout.PropertyField(interactableHoverMeshMaterial, Tooltips.InteractableHoverMeshMaterial);
Expand Down
21 changes: 20 additions & 1 deletion Editor/Properties/SnappablePropertyEditor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Innoactive.Creator.BasicInteraction.Validation;
using Innoactive.Creator.Core.SceneObjects;
using Innoactive.Creator.XRInteraction;
using Innoactive.Creator.XRInteraction.Properties;
using UnityEditor;
Expand Down Expand Up @@ -54,8 +55,24 @@ private void CreateSnapZone(SnappableProperty snappable)
// Adds a Snap Zone component to our new object.
SnapZone snapZone = snapObject.AddComponent<SnapZoneProperty>().SnapZone;
snapZone.ShownHighlightObject = snapZonePrefab;
IsTrainingSceneObjectValidation validation = snapZone.gameObject.AddComponent<IsTrainingSceneObjectValidation>();
validation.AddTrainingSceneObject(snappable.GetComponent<TrainingSceneObject>());

settings.ApplySettingsToSnapZone(snapZone);

GameObject snapPoint = new GameObject("SnapPoint");
snapPoint.transform.SetParent(snapZone.transform);
snapPoint.transform.localPosition = Vector3.zero;
snapPoint.transform.localScale = Vector3.one;
snapPoint.transform.localRotation = Quaternion.identity;
SnapZonePreviewDrawer drawer = snapPoint.AddComponent<SnapZonePreviewDrawer>();
drawer.Parent = snapZone;

SerializedObject snapZoneSerialization = new SerializedObject(snapZone);
SerializedProperty property = snapZoneSerialization.FindProperty("m_AttachTransform");
property.objectReferenceValue = snapPoint.transform;
snapZoneSerialization.ApplyModifiedPropertiesWithoutUndo();

// Calculates the volume of the Snap Zone out of the snappable object.
Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);

Expand All @@ -72,6 +89,8 @@ private void CreateSnapZone(SnappableProperty snappable)

// Disposes the cloned object.
DestroyImmediate(snapZoneBlueprint);

Selection.activeGameObject = snapZone.gameObject;
}

private GameObject DuplicateObject(GameObject originalObject, Transform parent = null)
Expand Down
40 changes: 38 additions & 2 deletions Editor/SnapZone/SnapZoneSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,39 @@ public class SnapZoneSettings : ScriptableObject
private Material highlightMaterial;

/// <summary>
/// This color is used when an <see cref="InteractableObject"/> is hovering a <see cref="SnapZone"/>.
/// This color is used when a valid <see cref="InteractableObject"/> is hovering a <see cref="SnapZone"/>.
/// </summary>
[Tooltip("This color is used when a valid object is hovering the snap zone.")]
public Color ValidationColor = new Color32(0, 255, 0, 50);

/// <summary>
/// This color is used when an invalid <see cref="InteractableObject"/> is hovering a <see cref="SnapZone"/>.
/// </summary>
[Tooltip("This color is used when an invalid object is hovering the snap zone.")]
public Color InvalidColor = new Color32(255, 0, 0, 50);

[SerializeField]
[Tooltip("The material shown when a valid object is hovering the snap zone. Should be transparent.\n\n[This field overrides 'ValidHighlightColor']")]
private Material validationMaterial;

[SerializeField]
[Tooltip("The material shown when an invalid object is hovering the snap zone. Should be transparent.\n\n[This field overrides 'InvalidHighlightColor']")]
private Material invalidMaterial;

/// <summary>
/// The material used for drawing when an <see cref="InteractableObject"/> is hovering a <see cref="SnapZone"/>. Should be transparent.
/// </summary>
public Material HighlightMaterial => SetupHighlightMaterial();

/// <summary>
/// The material used for the highlight object. Should be transparent.
/// The material used for the highlight object, when a valid object is hovering. Should be transparent.
/// </summary>
public Material ValidationMaterial => SetupValidationMaterial();

/// <summary>
/// The material used for the highlight object, when an invalid object is hovering. Should be transparent.
/// </summary>
public Material InvalidMaterial => SetupInvalidMaterial();

/// <summary>
/// Loads the first existing <see cref="SnapZoneSettings"/> found in the project.
Expand All @@ -64,6 +79,7 @@ public void ApplySettingsToSnapZone(SnapZone snapZone)
snapZone.InteractionLayerMask = InteractionLayerMask;
snapZone.ShownHighlightObjectColor = HighlightColor;
snapZone.ValidationMaterial = ValidationMaterial;
snapZone.InvalidMaterial = InvalidMaterial;
}

private static SnapZoneSettings RetrieveSnapZoneSettings()
Expand Down Expand Up @@ -121,6 +137,26 @@ private Material SetupHighlightMaterial()
return highlightMaterial;
}

private Material SetupInvalidMaterial()
{
if (invalidMaterial == null)
{
invalidMaterial = CreateMaterial();
invalidMaterial.name = "SnapZoneInvalidMaterial";

if (Directory.Exists(MaterialsPath) == false)
{
Directory.CreateDirectory(MaterialsPath);
}

AssetDatabase.CreateAsset(invalidMaterial, $"{MaterialsPath}/{invalidMaterial.name}.mat");
AssetDatabase.Refresh();
}

invalidMaterial.color = InvalidColor;
return invalidMaterial;
}

private Material SetupValidationMaterial()
{
if (validationMaterial == null)
Expand Down
2 changes: 2 additions & 0 deletions Resources/[XR_Setup].prefab

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Runtime/Interaction/InteractableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ public virtual void ForceUse()
protected override void OnSelectEnter(XRBaseInteractor interactor)
{
base.OnSelectEnter(interactor);

if (IsInSocket == false)
{
XRSocketInteractor socket = interactor.GetComponent<XRSocketInteractor>();
Expand Down
Loading

0 comments on commit 1e5d783

Please sign in to comment.