initial upload
This commit is contained in:
@ -0,0 +1,92 @@
|
||||
#if AR_FOUNDATION_PRESENT
|
||||
using UnityEngine.XR.ARSubsystems;
|
||||
using UnityEngine.XR.ARFoundation;
|
||||
using UnityEngine.XR.Interaction.Toolkit.Samples.StarterAssets;
|
||||
|
||||
namespace UnityEngine.XR.Interaction.Toolkit.Samples.ARStarterAssets
|
||||
{
|
||||
/// <summary>
|
||||
/// Spawns an object on physics trigger enter with an <see cref="ARPlane"/>, at the point of contact on the plane.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class ARContactSpawnTrigger : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
[Tooltip("The behavior to use to spawn objects.")]
|
||||
ObjectSpawner m_ObjectSpawner;
|
||||
|
||||
/// <summary>
|
||||
/// The behavior to use to spawn objects.
|
||||
/// </summary>
|
||||
public ObjectSpawner objectSpawner
|
||||
{
|
||||
get => m_ObjectSpawner;
|
||||
set => m_ObjectSpawner = value;
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("Whether to require that the AR Plane has an alignment of horizontal up to spawn on it.")]
|
||||
bool m_RequireHorizontalUpSurface;
|
||||
|
||||
/// <summary>
|
||||
/// Whether to require that the <see cref="ARPlane"/> has an alignment of <see cref="PlaneAlignment.HorizontalUp"/> to spawn on it.
|
||||
/// </summary>
|
||||
public bool requireHorizontalUpSurface
|
||||
{
|
||||
get => m_RequireHorizontalUpSurface;
|
||||
set => m_RequireHorizontalUpSurface = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// See <see cref="MonoBehaviour"/>.
|
||||
/// </summary>
|
||||
void Start()
|
||||
{
|
||||
if (m_ObjectSpawner == null)
|
||||
#if UNITY_2023_1_OR_NEWER
|
||||
m_ObjectSpawner = FindAnyObjectByType<ObjectSpawner>();
|
||||
#else
|
||||
m_ObjectSpawner = FindObjectOfType<ObjectSpawner>();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// See <see cref="MonoBehaviour"/>.
|
||||
/// </summary>
|
||||
void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (!TryGetSpawnSurfaceData(other, out var surfacePosition, out var surfaceNormal))
|
||||
return;
|
||||
|
||||
var infinitePlane = new Plane(surfaceNormal, surfacePosition);
|
||||
var contactPoint = infinitePlane.ClosestPointOnPlane(transform.position);
|
||||
m_ObjectSpawner.TrySpawnObject(contactPoint, surfaceNormal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the surface position and normal from an object to potentially spawn on.
|
||||
/// </summary>
|
||||
/// <param name="objectCollider">The collider of the object to potentially spawn on.</param>
|
||||
/// <param name="surfacePosition">The potential world position of the spawn surface.</param>
|
||||
/// <param name="surfaceNormal">The potential normal of the spawn surface.</param>
|
||||
/// <returns>Returns <see langword="true"/> if <paramref name="objectCollider"/> is a valid spawn surface,
|
||||
/// otherwise returns <see langword="false"/>.</returns>
|
||||
public bool TryGetSpawnSurfaceData(Collider objectCollider, out Vector3 surfacePosition, out Vector3 surfaceNormal)
|
||||
{
|
||||
surfacePosition = default;
|
||||
surfaceNormal = default;
|
||||
|
||||
var arPlane = objectCollider.GetComponent<ARPlane>();
|
||||
if (arPlane == null)
|
||||
return false;
|
||||
|
||||
if (m_RequireHorizontalUpSurface && arPlane.alignment != PlaneAlignment.HorizontalUp)
|
||||
return false;
|
||||
|
||||
surfaceNormal = arPlane.normal;
|
||||
surfacePosition = arPlane.center;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4440a5b98326c3846b042c0c85fb1d0d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,115 @@
|
||||
#if AR_FOUNDATION_PRESENT
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.XR.ARFoundation;
|
||||
|
||||
namespace UnityEngine.XR.Interaction.Toolkit.Samples.ARStarterAssets
|
||||
{
|
||||
/// <summary>
|
||||
/// This plane visualizer demonstrates the use of a feathering effect
|
||||
/// at the edge of the detected plane, which reduces the visual impression
|
||||
/// of a hard edge.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(ARPlaneMeshVisualizer), typeof(MeshRenderer), typeof(ARPlane))]
|
||||
public class ARFeatheredPlaneMeshVisualizer : MonoBehaviour
|
||||
{
|
||||
[Tooltip("The width of the texture feathering (in world units).")]
|
||||
[SerializeField]
|
||||
float m_FeatheringWidth = 0.2f;
|
||||
|
||||
/// <summary>
|
||||
/// The width of the texture feathering (in world units).
|
||||
/// </summary>
|
||||
public float featheringWidth
|
||||
{
|
||||
get { return m_FeatheringWidth; }
|
||||
set { m_FeatheringWidth = value; }
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
m_PlaneMeshVisualizer = GetComponent<ARPlaneMeshVisualizer>();
|
||||
m_FeatheredPlaneMaterial = GetComponent<MeshRenderer>().material;
|
||||
m_Plane = GetComponent<ARPlane>();
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
m_Plane.boundaryChanged += ARPlane_boundaryUpdated;
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
m_Plane.boundaryChanged -= ARPlane_boundaryUpdated;
|
||||
}
|
||||
|
||||
void ARPlane_boundaryUpdated(ARPlaneBoundaryChangedEventArgs eventArgs)
|
||||
{
|
||||
GenerateBoundaryUVs(m_PlaneMeshVisualizer.mesh);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate UV2s to mark the boundary vertices and feathering UV coords.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The <c>ARPlaneMeshVisualizer</c> has a <c>meshUpdated</c> event that can be used to modify the generated
|
||||
/// mesh. In this case we'll add UV2s to mark the boundary vertices.
|
||||
/// This technique avoids having to generate extra vertices for the boundary. It works best when the plane is
|
||||
/// is fairly uniform.
|
||||
/// </remarks>
|
||||
/// <param name="mesh">The <c>Mesh</c> generated by <c>ARPlaneMeshVisualizer</c></param>
|
||||
void GenerateBoundaryUVs(Mesh mesh)
|
||||
{
|
||||
int vertexCount = mesh.vertexCount;
|
||||
|
||||
// Reuse the list of UVs
|
||||
s_FeatheringUVs.Clear();
|
||||
if (s_FeatheringUVs.Capacity < vertexCount) { s_FeatheringUVs.Capacity = vertexCount; }
|
||||
|
||||
mesh.GetVertices(s_Vertices);
|
||||
|
||||
Vector3 centerInPlaneSpace = s_Vertices[s_Vertices.Count - 1];
|
||||
Vector3 uv = new Vector3(0, 0, 0);
|
||||
float shortestUVMapping = float.MaxValue;
|
||||
|
||||
// Assume the last vertex is the center vertex.
|
||||
for (int i = 0; i < vertexCount - 1; i++)
|
||||
{
|
||||
float vertexDist = Vector3.Distance(s_Vertices[i], centerInPlaneSpace);
|
||||
|
||||
// Remap the UV so that a UV of "1" marks the feathering boudary.
|
||||
// The ratio of featherBoundaryDistance/edgeDistance is the same as featherUV/edgeUV.
|
||||
// Rearrange to get the edge UV.
|
||||
float uvMapping = vertexDist / Mathf.Max(vertexDist - featheringWidth, 0.001f);
|
||||
uv.x = uvMapping;
|
||||
|
||||
// All the UV mappings will be different. In the shader we need to know the UV value we need to fade out by.
|
||||
// Choose the shortest UV to guarentee we fade out before the border.
|
||||
// This means the feathering widths will be slightly different, we again rely on a fairly uniform plane.
|
||||
if (shortestUVMapping > uvMapping) { shortestUVMapping = uvMapping; }
|
||||
|
||||
s_FeatheringUVs.Add(uv);
|
||||
}
|
||||
|
||||
m_FeatheredPlaneMaterial.SetFloat("_ShortestUVMapping", shortestUVMapping);
|
||||
|
||||
// Add the center vertex UV
|
||||
uv.Set(0, 0, 0);
|
||||
s_FeatheringUVs.Add(uv);
|
||||
|
||||
mesh.SetUVs(1, s_FeatheringUVs);
|
||||
mesh.UploadMeshData(false);
|
||||
}
|
||||
|
||||
static List<Vector3> s_FeatheringUVs = new List<Vector3>();
|
||||
|
||||
static List<Vector3> s_Vertices = new List<Vector3>();
|
||||
|
||||
ARPlaneMeshVisualizer m_PlaneMeshVisualizer;
|
||||
|
||||
ARPlane m_Plane;
|
||||
|
||||
Material m_FeatheredPlaneMaterial;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4694583967bd9483f9841409d278f46f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,209 @@
|
||||
#if AR_FOUNDATION_PRESENT
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.XR.ARFoundation;
|
||||
using UnityEngine.XR.ARSubsystems;
|
||||
using UnityEngine.XR.Interaction.Toolkit.Inputs.Readers;
|
||||
using UnityEngine.XR.Interaction.Toolkit.Interactors;
|
||||
using UnityEngine.XR.Interaction.Toolkit.Samples.StarterAssets;
|
||||
|
||||
namespace UnityEngine.XR.Interaction.Toolkit.Samples.ARStarterAssets
|
||||
{
|
||||
/// <summary>
|
||||
/// Spawns an object at an <see cref="IARInteractor"/>'s raycast hit position when a trigger is activated.
|
||||
/// </summary>
|
||||
public class ARInteractorSpawnTrigger : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// The type of trigger to use to spawn an object.
|
||||
/// </summary>
|
||||
public enum SpawnTriggerType
|
||||
{
|
||||
/// <summary>
|
||||
/// Spawn an object when the interactor activates its select input
|
||||
/// but no selection actually occurs.
|
||||
/// </summary>
|
||||
SelectAttempt,
|
||||
|
||||
/// <summary>
|
||||
/// Spawn an object when an input is performed.
|
||||
/// </summary>
|
||||
InputAction,
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("The AR ray interactor that determines where to spawn the object.")]
|
||||
XRRayInteractor m_ARInteractor;
|
||||
|
||||
/// <summary>
|
||||
/// The AR ray interactor that determines where to spawn the object.
|
||||
/// </summary>
|
||||
public XRRayInteractor arInteractor
|
||||
{
|
||||
get => m_ARInteractor;
|
||||
set => m_ARInteractor = value;
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("The behavior to use to spawn objects.")]
|
||||
ObjectSpawner m_ObjectSpawner;
|
||||
|
||||
/// <summary>
|
||||
/// The behavior to use to spawn objects.
|
||||
/// </summary>
|
||||
public ObjectSpawner objectSpawner
|
||||
{
|
||||
get => m_ObjectSpawner;
|
||||
set => m_ObjectSpawner = value;
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("Whether to require that the AR Interactor hits an AR Plane with a horizontal up alignment in order to spawn anything.")]
|
||||
bool m_RequireHorizontalUpSurface;
|
||||
|
||||
/// <summary>
|
||||
/// Whether to require that the <see cref="IARInteractor"/> hits an <see cref="ARPlane"/> with an alignment of
|
||||
/// <see cref="PlaneAlignment.HorizontalUp"/> in order to spawn anything.
|
||||
/// </summary>
|
||||
public bool requireHorizontalUpSurface
|
||||
{
|
||||
get => m_RequireHorizontalUpSurface;
|
||||
set => m_RequireHorizontalUpSurface = value;
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("The type of trigger to use to spawn an object, either when the Interactor's select action occurs or " +
|
||||
"when a button input is performed.")]
|
||||
SpawnTriggerType m_SpawnTriggerType;
|
||||
|
||||
/// <summary>
|
||||
/// The type of trigger to use to spawn an object.
|
||||
/// </summary>
|
||||
public SpawnTriggerType spawnTriggerType
|
||||
{
|
||||
get => m_SpawnTriggerType;
|
||||
set => m_SpawnTriggerType = value;
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
XRInputButtonReader m_SpawnObjectInput = new XRInputButtonReader("Spawn Object");
|
||||
|
||||
/// <summary>
|
||||
/// The input used to trigger spawn, if <see cref="spawnTriggerType"/> is set to <see cref="SpawnTriggerType.InputAction"/>.
|
||||
/// </summary>
|
||||
public XRInputButtonReader spawnObjectInput
|
||||
{
|
||||
get => m_SpawnObjectInput;
|
||||
set => XRInputReaderUtility.SetInputProperty(ref m_SpawnObjectInput, value, this);
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("When enabled, spawn will not be triggered if an object is currently selected.")]
|
||||
bool m_BlockSpawnWhenInteractorHasSelection = true;
|
||||
|
||||
/// <summary>
|
||||
/// When enabled, spawn will not be triggered if an object is currently selected.
|
||||
/// </summary>
|
||||
public bool blockSpawnWhenInteractorHasSelection
|
||||
{
|
||||
get => m_BlockSpawnWhenInteractorHasSelection;
|
||||
set => m_BlockSpawnWhenInteractorHasSelection = value;
|
||||
}
|
||||
|
||||
bool m_AttemptSpawn;
|
||||
bool m_EverHadSelection;
|
||||
|
||||
/// <summary>
|
||||
/// See <see cref="MonoBehaviour"/>.
|
||||
/// </summary>
|
||||
void OnEnable()
|
||||
{
|
||||
m_SpawnObjectInput.EnableDirectActionIfModeUsed();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// See <see cref="MonoBehaviour"/>.
|
||||
/// </summary>
|
||||
void OnDisable()
|
||||
{
|
||||
m_SpawnObjectInput.DisableDirectActionIfModeUsed();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// See <see cref="MonoBehaviour"/>.
|
||||
/// </summary>
|
||||
void Start()
|
||||
{
|
||||
if (m_ObjectSpawner == null)
|
||||
#if UNITY_2023_1_OR_NEWER
|
||||
m_ObjectSpawner = FindAnyObjectByType<ObjectSpawner>();
|
||||
#else
|
||||
m_ObjectSpawner = FindObjectOfType<ObjectSpawner>();
|
||||
#endif
|
||||
|
||||
if (m_ARInteractor == null)
|
||||
{
|
||||
Debug.LogError("Missing AR Interactor reference, disabling component.", this);
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// See <see cref="MonoBehaviour"/>.
|
||||
/// </summary>
|
||||
void Update()
|
||||
{
|
||||
// Wait a frame after the Spawn Object input is triggered to actually cast against AR planes and spawn
|
||||
// in order to ensure the touchscreen gestures have finished processing to allow the ray pose driver
|
||||
// to update the pose based on the touch position of the gestures.
|
||||
if (m_AttemptSpawn)
|
||||
{
|
||||
m_AttemptSpawn = false;
|
||||
|
||||
// Cancel the spawn if the select was delayed until the frame after the spawn trigger.
|
||||
// This can happen if the select action uses a different input source than the spawn trigger.
|
||||
if (m_ARInteractor.hasSelection)
|
||||
return;
|
||||
|
||||
// Don't spawn the object if the tap was over screen space UI.
|
||||
var isPointerOverUI = EventSystem.current != null && EventSystem.current.IsPointerOverGameObject(-1);
|
||||
if (!isPointerOverUI && m_ARInteractor.TryGetCurrentARRaycastHit(out var arRaycastHit))
|
||||
{
|
||||
if (!(arRaycastHit.trackable is ARPlane arPlane))
|
||||
return;
|
||||
|
||||
if (m_RequireHorizontalUpSurface && arPlane.alignment != PlaneAlignment.HorizontalUp)
|
||||
return;
|
||||
|
||||
m_ObjectSpawner.TrySpawnObject(arRaycastHit.pose.position, arPlane.normal);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var selectState = m_ARInteractor.logicalSelectState;
|
||||
|
||||
if (m_BlockSpawnWhenInteractorHasSelection)
|
||||
{
|
||||
if (selectState.wasPerformedThisFrame)
|
||||
m_EverHadSelection = m_ARInteractor.hasSelection;
|
||||
else if (selectState.active)
|
||||
m_EverHadSelection |= m_ARInteractor.hasSelection;
|
||||
}
|
||||
|
||||
m_AttemptSpawn = false;
|
||||
switch (m_SpawnTriggerType)
|
||||
{
|
||||
case SpawnTriggerType.SelectAttempt:
|
||||
if (selectState.wasCompletedThisFrame)
|
||||
m_AttemptSpawn = !m_ARInteractor.hasSelection && !m_EverHadSelection;
|
||||
break;
|
||||
|
||||
case SpawnTriggerType.InputAction:
|
||||
if (m_SpawnObjectInput.ReadWasPerformedThisFrame())
|
||||
m_AttemptSpawn = !m_ARInteractor.hasSelection && !m_EverHadSelection;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba63293f961b46a1a5b648e4ba02cfb5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user