Skip to content

Instantly share code, notes, and snippets.

View andijakl's full-sized avatar

Andreas Jakl andijakl

View GitHub Profile
@andijakl
andijakl / ARPlaceTrackedImages.cs
Last active August 13, 2021 07:22
Reacting to updated and removed tracked images in AR Foundation (part 2)
// 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.
@andijakl
andijakl / ARPlaceTrackedImages.cs
Created August 11, 2021 12:36
Reacting to new tracked images in AR Foundation (part 1)
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)
@andijakl
andijakl / ARPlaceHologram.cs
Created August 10, 2021 12:48
Code part to attach an anchor to a plane in AR Foundation
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>();
@andijakl
andijakl / ARPlaceHologram.cs
Created August 10, 2021 12:33
First version of the code to create an anchor in AR Foundation based on the ARRaycastHit.
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);
@andijakl
andijakl / ARPlaceHologram.cs
Last active August 10, 2021 13:09
Part 2 of the script to place holograms based on raycasts with AR Foundation
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.
@andijakl
andijakl / ARPlaceHologram.cs
Created August 10, 2021 11:15
Part 1 of the script to place holograms based on raycasts with AR Foundation
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
@andijakl
andijakl / PointCloudInfo.cs
Created August 9, 2021 14:59
AR Foundation Point Cloud Logging
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;
@andijakl
andijakl / voiceflow_random_number.js
Created June 18, 2021 09:44
Generate unique random numbers in a custom code block for Voiceflow
// 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];
@andijakl
andijakl / DepthImageVisualizer.cs
Created November 27, 2020 10:28
Adapt the RawImage size in Unity to the depth map
// 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;
@andijakl
andijakl / DepthImageVisualizer.cs
Created November 27, 2020 10:21
Convert the XRCpuImage to a Texture2D for AR Foundation.
// 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.