Operation Model AR + Vuforia +

tag and Layer models are Mode

 

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

public class ARModelOperator : MonoBehaviour {

    private Camera aRCamera;
//	public string TargetName;
    private enum TouchType
    {
        DefaultType = 0,
        ShortClick = 1,
        LongClick = 2,
        DoubleClick = 3,
        RightClick = 4,
    };

    private bool onDrag = false;
    private Vector2 startPos;
    TouchType operatorType = TouchType.DefaultType;
    float speed =250f;
    Vector2 oldPosition1, oldPosition2;
    float distance = 30f;
    float scaleRatio=0.1f;
   = BOOL isAnimation to false public; 
        {
    the Start void () 
    { 
        aRCamera = GameObject.Find ( "RenderCamera") GetComponent <Camera> ();. 
    } 
    // enlarged function returns true, false returns to narrow 
    bool isEnlarge (Vector2 oP1, Vector2 oP2 , Vector2 nP1, Vector2 nP2 ) 
    { 
        // on the function passed one touch position and the position of two touch points according to the present calculated user's gesture 
        var leng1 = Mathf.Sqrt ((oP1.x - oP2.x) * (oP1.x - oP2 .x) + (oP1.y - oP2.y) * (oP1.y - oP2.y)); 
        var leng2 Mathf.Sqrt = ((nP1.x - nP2.x) * (nP1.x - nP2.x ) + (nP1.y - nP2.y) * (nP1.y - nP2.y)); 
        IF (leng1 <leng2) 
        { 
            // zoom gesture 
            return to true; 
        } 
        the else 
            // shrink gesture 
            return to false; 
        }
    }

    IEnumerator OnMouseDownClick()
    {
        Vector3 screenSpace = aRCamera.WorldToScreenPoint(transform.position);
        Vector3 offset = transform.position - aRCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
        while (Input.GetMouseButton(0))
        {
            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
            Vector3 curPosition = aRCamera.ScreenToWorldPoint(curScreenSpace) + offset;
            transform.position = curPosition;
            yield return new WaitForFixedUpdate();
        }
    }

    bool CheckIsDoubleTouch(Vector2 mousePos)
    {
        ray = aRCamera.ScreenPointToRay(mousePos);
        return Physics.Raycast(ray, out hit);
    }

    float intervalTime = 0f;
    RaycastHit hit;
    Ray ray;
    bool doubleTouch = false;
    float smooth =0.1f;
    void Update()
    {
       
        ///鼠标滚轮控制模型缩放
        var sc1 = transform.localScale;
        //Debug.LogError("distance " + distance);
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            if (distance > 18.5f)
            {
				distance -= ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).InitScale*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).MaxScalingMultiplier;
				if (sc1.x < 0.4*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).MaxScalingMultiplier&& sc1.x > 0)
                {
					sc1 += 2*Vector3.one * smooth * ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).scalingMultiplier*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).MaxScalingMultiplier;
                }
            }

        }
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            if (distance <= 34f)
            {
                //Debug.LogError(hit.collider.name);

                distance += ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).InitScale*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).MaxScalingMultiplier;
				if (sc1.x > 0.8*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).scalingMultiplier *ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).MaxScalingMultiplier)
                {
					sc1 -= 2 * Vector3.one * smooth * ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).scalingMultiplier*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).MaxScalingMultiplier;
                }
            }
        }
        transform.localScale = sc1;

        if (Input.touchCount > 1)
        {//双指
            if (!doubleTouch)
            {
                doubleTouch = CheckIsDoubleTouch(Input.touches[0].position) || CheckIsDoubleTouch(Input.touches[1].position);
            }
            if (doubleTouch)
            {
                if (hit.collider.tag == "Mode")
                {

                    if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
                    {
                        var tempPosition1 = Input.GetTouch(0).position;
                        var tempPosition2 = Input.GetTouch(1).position;
                        var sc = transform.localScale;
                        if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
                        {
                            if (distance > 18.5f)
                            {
								distance -= ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).InitScale*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).MaxScalingMultiplier;
								if (sc.x <=0.4*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).MaxScalingMultiplier&&sc.x>0)
                                {
									sc +=2 * Vector3.one * smooth*ARModelData.Instance.GetItem(hit.collider.name).scalingMultiplier*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).MaxScalingMultiplier;
                                }
                            }
                        }
                        else
                        {
                            if (distance < 34f)
                            {
								distance += ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).InitScale*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).MaxScalingMultiplier;
								if (sc.x >0.8*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).scalingMultiplier*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).MaxScalingMultiplier)
                                { 
									sc -=2*Vector3.one * smooth * ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).scalingMultiplier*ARModelData.Instance.GetItem(ARGlobalData.CurrentTargetModeName).MaxScalingMultiplier;
                                }
                            }
                        }
                        transform.localScale = sc;
                        oldPosition1 = tempPosition1;
                        oldPosition2 = tempPosition2;
                    }
                }
            }
            if (Input.GetTouch(1).phase == TouchPhase.Ended)
            {
                doubleTouch = false;
            }
        }
        else
        {
            if (Input.GetMouseButton(0))
            {
                if (Input.GetMouseButtonDown(0))// || Input.touches[0].phase == TouchPhase.Began)
                {
                    startPos = Input.mousePosition;
                    ray = aRCamera.ScreenPointToRay(startPos);
                    onDrag = Physics.Raycast(ray, out hit);
                    intervalTime = Time.realtimeSinceStartup;
                    isAnimation = true;
                }

                if (onDrag)
                {

                    if (hit.collider.tag == "Mode")
                    {
                        if (operatorType == TouchType.DefaultType)
                        {
                            if (Time.realtimeSinceStartup - intervalTime > 0.5f)//长按
                            {
                                //Debug.LogError("animales");
                                isAnimation = false;

                                operatorType = TouchType.LongClick;
                                StartCoroutine(OnMouseDownClick());
                            }
                            else
                            {
                                Vector2 curPos = Input.mousePosition;
                                if (Mathf.Abs(curPos.x - startPos.x) > Mathf.Abs(curPos.y - startPos.y) && Mathf.Abs(curPos.x - startPos.x) > 5f)
                                {
                                    isAnimation = false;

                                    operatorType = TouchType.ShortClick;//旋转
                                }
                                else
                                {
                                    isAnimation = true;

                                }
                            }
                        }
                        else if (operatorType == TouchType.ShortClick)
                        {
                            if (startPos.x > Input.mousePosition.x)//向左滑动
                            {
                                transform.Rotate(Vector3.up * Time.deltaTime * speed);
                            }
                            else if (startPos.x < Input.mousePosition.x)//向右滑动
                            {
                                transform.Rotate(Vector3.down * Time.deltaTime * speed);
                            }
                            startPos = Input.mousePosition;
                        }
                    }
                }
            }
            else if (Input.GetMouseButton(1))
            {
                operatorType = TouchType.RightClick;
                StartCoroutine(OnMouseDownClick());
            }
        }
        if (Input.GetMouseButtonUp(0))// || Input.touches[0].phase == TouchPhase.Ended)
        {
            onDrag = false;
            operatorType = TouchType.DefaultType;
        }
    }
}

  

Guess you like

Origin www.cnblogs.com/WalkingSnail/p/11304115.html