This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Disable instantiated prefabs that are no longer being actively tracked | |
foreach (var trackedImage in eventArgs.updated) { | |
_instantiatedPrefabs[trackedImage.referenceImage.name] | |
.SetActive(trackedImage.trackingState == TrackingState.Tracking); | |
} | |
// Remove is called if the subsystem has given up looking for the trackable again. | |
// (If it's invisible, its tracking state would just go to limited initially). | |
// Note: ARCore doesn't seem to remove these at all; if it does, it would delete our child GameObject | |
// as well. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs) | |
{ | |
// Go through all tracked images that have been added | |
// (-> new markers detected) | |
foreach (var trackedImage in eventArgs.added) | |
{ | |
// Get the name of the reference image to search for the corresponding prefab | |
var imageName = trackedImage.referenceImage.name; | |
foreach (var curPrefab in ArPrefabs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ARAnchor CreateAnchor(in ARRaycastHit hit) | |
{ | |
ARAnchor anchor; | |
// ... here, we'll place the plane anchoring code! | |
// If we hit a plane, try to "attach" the anchor to the plane | |
if (hit.trackable is ARPlane plane) | |
{ | |
var planeManager = GetComponent<ARPlaneManager>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ARAnchor CreateAnchor(in ARRaycastHit hit) | |
{ | |
ARAnchor anchor; | |
// ... here, we'll place the plane anchoring code! | |
// Otherwise, just create a regular anchor at the hit pose | |
// Note: the anchor can be anywhere in the scene hierarchy | |
var instantiatedObject = Instantiate(_prefabToPlace, hit.pose.position, hit.pose.rotation); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Update() | |
{ | |
// Only consider single-finger touches that are beginning | |
Touch touch; | |
if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { return; } | |
// Perform AR raycast to any kind of trackable | |
if (_raycastManager.Raycast(touch.position, Hits, TrackableType.AllTypes)) | |
{ | |
// Raycast hits are sorted by distance, so the first one will be the closest hit. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ARPlaceHologram : MonoBehaviour | |
{ | |
// The prefab to instantiate on touch. | |
[SerializeField] | |
private GameObject _prefabToPlace; | |
// Cache ARRaycastManager GameObject from ARCoreSession | |
private ARRaycastManager _raycastManager; | |
// List for raycast hits is re-used by raycast manager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.XR.ARFoundation; | |
public class PointCloudInfo : MonoBehaviour | |
{ | |
// The AR Foundation PointCloud script | |
private ARPointCloud _pointCloud; | |
// Reference to logging UI element in the canvas | |
public UnityEngine.UI.Text Log; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define number of choices available. | |
// Generated options: 0.. < num_max | |
const num_max = 5; | |
// Randomize array in-place using Durstenfeld shuffle algorithm | |
// Based on: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array | |
function shuffleArray(array) { | |
for (var i = array.length - 1; i > 0; i--) { | |
var j = Math.floor(Math.random() * (i + 1)); | |
var temp = array[i]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get the aspect ratio for the current texture. | |
var textureAspectRatio = (float)texture.width / texture.height; | |
// Determine the raw image rectSize preserving the texture aspect ratio, matching the screen orientation, | |
// and keeping a minimum dimension size. | |
const float minDimension = 480.0f; | |
var maxDimension = Mathf.Round(minDimension * textureAspectRatio); | |
var rectSize = new Vector2(maxDimension, minDimension); | |
//var rectSize = new Vector2(minDimension, maxDimension); //Portrait | |
rawImage.rectTransform.sizeDelta = rectSize; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: https://github.com/Unity-Technologies/arfoundation-samples/blob/6296272a416925b56ce85470e0c7bef5c913ec0c/Assets/Scripts/CpuImageSample.cs | |
private static void UpdateRawImage(RawImage rawImage, XRCpuImage cpuImage) | |
{ | |
// Get the texture associated with the UI.RawImage that we wish to display on screen. | |
var texture = rawImage.texture as Texture2D; | |
// If the texture hasn't yet been created, or if its dimensions have changed, (re)create the texture. | |
// Note: Although texture dimensions do not normally change frame-to-frame, they can change in response to | |
// a change in the camera resolution (for camera images) or changes to the quality of the human depth | |
// and human stencil buffers. |
NewerOlder