adding basic scene with marker detection

This commit is contained in:
Nicolas SANS
2023-04-11 07:57:25 +02:00
parent a6abcd4fe1
commit 13b0bdcfb0
193 changed files with 20224 additions and 0 deletions

View File

@ -0,0 +1,110 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
/// <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;
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: de9919161fbca46b79a86ecacee38c30
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,95 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
//
// This script allows us to create anchors with
// a prefab attached in order to visbly discern where the anchors are created.
// Anchors are a particular point in space that you are asking your device to track.
//
[RequireComponent(typeof(ARAnchorManager))]
[RequireComponent(typeof(ARRaycastManager))]
[RequireComponent(typeof(ARPlaneManager))]
public class AnchorCreator : MonoBehaviour
{
// This is the prefab that will appear every time an anchor is created.
[SerializeField]
GameObject m_AnchorPrefab;
public GameObject AnchorPrefab
{
get => m_AnchorPrefab;
set => m_AnchorPrefab = value;
}
// Removes all the anchors that have been created.
public void RemoveAllAnchors()
{
foreach (var anchor in m_AnchorPoints)
{
Destroy(anchor);
}
m_AnchorPoints.Clear();
}
// On Awake(), we obtains a reference to all the required components.
// The ARRaycastManager allows us to perform raycasts so that we know where to place an anchor.
// The ARPlaneManager detects surfaces we can place our objects on.
// The ARAnchorManager handles the processing of all anchors and updates their position and rotation.
void Awake()
{
m_RaycastManager = GetComponent<ARRaycastManager>();
m_AnchorManager = GetComponent<ARAnchorManager>();
m_PlaneManager = GetComponent<ARPlaneManager>();
m_AnchorPoints = new List<ARAnchor>();
}
void Update()
{
// If there is no tap, then simply do nothing until the next call to Update().
if (Input.touchCount == 0)
return;
var touch = Input.GetTouch(0);
if (touch.phase != TouchPhase.Began)
return;
if (m_RaycastManager.Raycast(touch.position, s_Hits, TrackableType.PlaneWithinPolygon))
{
// Raycast hits are sorted by distance, so the first one
// will be the closest hit.
var hitPose = s_Hits[0].pose;
var hitTrackableId = s_Hits[0].trackableId;
var hitPlane = m_PlaneManager.GetPlane(hitTrackableId);
// This attaches an anchor to the area on the plane corresponding to the raycast hit,
// and afterwards instantiates an instance of your chosen prefab at that point.
// This prefab instance is parented to the anchor to make sure the position of the prefab is consistent
// with the anchor, since an anchor attached to an ARPlane will be updated automatically by the ARAnchorManager as the ARPlane's exact position is refined.
var anchor = m_AnchorManager.AttachAnchor(hitPlane, hitPose);
Instantiate(m_AnchorPrefab, anchor.transform);
if (anchor == null)
{
Debug.Log("Error creating anchor.");
}
else
{
// Stores the anchor so that it may be removed later.
m_AnchorPoints.Add(anchor);
}
}
}
static List<ARRaycastHit> s_Hits = new List<ARRaycastHit>();
List<ARAnchor> m_AnchorPoints;
ARRaycastManager m_RaycastManager;
ARAnchorManager m_AnchorManager;
ARPlaneManager m_PlaneManager;
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 788587e994f1943f3856c1c332db429b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: