Forgot password?
 立即注册
EasyAR Portal Tutorial View Content

EasyAR developing skills----Model interaction

AlbertLee 2017-8-3 15:04

Common interactive function in AR developing

We discribed basic EasyAR functions in the beginner tutorial guide. It is awesome to display model via AR. Scanning the target image displays a static model. It is not satisfied experience. Here I sum up several common AR model interaction based on popular AR Apps in the market. We develop App based on HelloAR provided that you have taken command of how to bulid EasyAR+unity. If you do not understand, you can click here:


code : https://drive.google.com/open?id=0B6vjKDkZUGVsbW1TdllDRUpKM2c

Step 1:Import project

Our developing is based on EasyAR official project  "HelloAR" . Firstly, download it from our official website and import it into Unity.



Open HelloAR in Unity after downloading


After that , open"HelloAR" and get a key from official website.




Step 2:Click model for interaction

Preview

Function:Click Cube(When we scan target image ,there appears a Cube) for changing its color.

We delete these two unnecesary target images. We only use "ImageTarget-JsonFile-idback" .


Create Material and name it "blue"


Create a script "ChangeColor" in Cube and click for changing cube’s color. This script is basic unit of Unity, which defines two Material. We can modify and replace Material in OnMouseDown) .  Meanwhile we also define Boolean.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeColor : MonoBehaviour {
    public Material blue;
    public Material id;
    private bool isClick = false;
    // Use this for initialization
    void Start () {    
    }
    // Update is called once per frame
    void Update () {   
    }
    void OnMouseDown()
    {
        if (!isClick) {
            this.gameObject.GetComponent<MeshRenderer> ().material = blue;
            isClick = true;
        } else {
            this.gameObject.GetComponent<MeshRenderer> ().material = id;
            isClick = false;
        }
    }
}




Step 2:hand zoom
Preview:


In developing AR App, hand zoom is common function and natural interation. It it easy to achieve hand zoom. We add 


a script in Cube and name it "Gesture"


code : https://drive.google.com/open?id=0B6vjKDkZUGVsMVQ3TzlDOUdzUUE


using UnityEngine;
using System.Collections;
public class Gesture : MonoBehaviour {
    private Touch oldTouch1;  
    private Touch oldTouch2;  
    void Update()
    {
       
        if (Input.touchCount <= 0)
        {
            return;
        }     
       
        Touch newTouch1 = Input.GetTouch(0);
        Touch newTouch2 = Input.GetTouch(1);
        
        if (newTouch2.phase == TouchPhase.Began)
        {
            oldTouch2 = newTouch2;
            oldTouch1 = newTouch1;
            return;
        }
        
        float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
        float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
        
        float offset = newDistance - oldDistance;
        
        float scaleFactor = offset / 100f;
        Vector3 localScale = transform.localScale;
        Vector3 scale = new Vector3(localScale.x + scaleFactor,
            localScale.y + scaleFactor,
            localScale.z + scaleFactor);
        
        if (scale.x >= 0.05f && scale.y >=0.05f && scale.z >= 0.05f)
        {
            transform.localScale = scale;
        }
       
        oldTouch1 = newTouch1;
        oldTouch2 = newTouch2;
    }
}

Step 3:Drag as you like
Preview:


It is rare for AR App in the market to achive this function but Sight Plus App can drag the model into anywhere you like. Also, we create a script in Cube and name it "Drag"


code: https://drive.google.com/open?id=0B6vjKDkZUGVsRlJCWm1kMWNhNDA


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Drag : MonoBehaviour {
    private Vector3 _vec3TargetScreenSpace;

    private Vector3 _vec3TargetWorldSpace;

    private Transform _trans;

    private Vector3 _vec3MouseScreenSpace;

    private Vector3 _vec3Offset;

    void Awake() { _trans = transform; }

    IEnumerator OnMouseDown()

    {

        

        _vec3TargetScreenSpace = Camera.main.WorldToScreenPoint(_trans.position);

        
        _vec3MouseScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _vec3TargetScreenSpace.z);

       

        _vec3Offset = _trans.position - Camera.main.ScreenToWorldPoint(_vec3MouseScreenSpace);

        

        while (Input.GetMouseButton(0))

        {

            

            _vec3MouseScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _vec3TargetScreenSpace.z);

          

            _vec3TargetWorldSpace = Camera.main.ScreenToWorldPoint(_vec3MouseScreenSpace) + _vec3Offset;

            

            _trans.position = _vec3TargetWorldSpace;

            

            yield return new WaitForFixedUpdate();

        }

    }
}


  • ljimenez29 2017-10-11 03:08
    Hello
    does the Drag script only work with the Cube object?