Disabling AR plane detection and Changing AR plane texture in Unity

This tutorial is the continuation of our getting started with AR foundation tutorials. In this tutorial, we will learn how to disable AR plane detection and change the default AR plane’s material. If you have not read the other tutorials then you can check them out from the link below.

  1. AR project setup and plane detection.
  2. AR light estimation.
  3. AR object placement.
  4. AR pinch to Zoom tutorial.

Both AR Raycast and AR plane detection are very performance expensive operation. So its a good practice to disable plane detection and Raycast when not needed.

We can disable plane detection and Raycast by disabling the AR plane Manager component and the AR Raycast Manager component.

When you disable the AR plane manager, the already detected planes will remain. But no new planes will be detected.

Steps to Disable Plane detection and AR Raycast

  1. Declare variables for AR plane manager and AR Raycast Manager.
  2. Get both components in the start function.
  3. After object placement set both AR plane Manager and AR Raycast manager to disabled.

Lets modify the Pinch to Zoom script to disable plane detection and AR Raycast

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

public class Raycast_script : MonoBehaviour
{
    public GameObject spawn_prefab;
    GameObject spawned_object;
    bool object_spawned;
    ARRaycastManager arrayman;
    ARPlaneManager arplneman;
    Vector2 First_touch;
    Vector2 second_touch;
    float distance_current;
    float distance_previous;
    bool first_pinch=true;
    List <ARRaycastHit> hits=new List<ARRaycastHit>();
    // Start is called before the first frame update
    void Start()
    {
        object_spawned=false;
        arrayman=GetComponent<ARRaycastManager>();
        arplneman=GetComponent<ARPlaneManager>();
        
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.touchCount>0 && !object_spawned)
        {
            if(arrayman.Raycast(Input.GetTouch(0).position,hits,TrackableType.PlaneWithinPolygon))
            {
                var hitpose=hits[0].pose;
                spawned_object=Instantiate(spawn_prefab,hitpose.position,hitpose.rotation);
                object_spawned=true;
                arrayman.enabled=false;
                arplneman.enabled=false;

            }
        }
        if(Input.touchCount>1 && object_spawned)
        {
            First_touch=Input.GetTouch(0).position;
            second_touch=Input.GetTouch(1).position;
            distance_current=second_touch.magnitude-First_touch.magnitude;
            if(first_pinch)
            {
                distance_previous=distance_current;
                first_pinch=false;
            }
            if(distance_current!=distance_previous)
            {
                Vector3 scale_value=spawned_object.transform.localScale*(distance_current/distance_previous);
                spawned_object.transform.localScale=scale_value;
                distance_previous=distance_current;

            }

        }
        else{
            first_pinch=true;
        }
        
    }
}

Changing the Default AR plane texture

First Create a new material and set your custom texture to that material.

Select the default AR plane prefab and go to the inspector window.

Find the material property under mesh renderer.

Set the material to the new material you have created.

If you wish to use an already existing plane. Then you need to add the following components

  1. AR Plane
  2. AR Plane Mesh Visualizer.

Save the scene and project.

Build and run the project to test the app.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading