2017年11月26日 星期日

擴增實境名片(Augmented Reality Business Card)程式解析


擴增實境技術是很棒的智慧行銷工具,一個業務出門,往往需要帶一大堆型錄和自己的名片,假如可以用自己的名片來代替型錄,而這個型錄又是3D而且有動畫功能,這應讓是很棒的工具,今天要跟大介紹就是擴增實境名片(Augmented Reality Business Card)。

教學部落格:Augmented Reality Business Card

教學影片:


下載原始程式:Download AR Business Card Tutorial Project Files (the result to test it) (*.rar file)

解開壓縮檔後,在Unity打開,在Project/AppContent/Script目錄下,可以找兩個程式為ModelRotation.cs和SwipeImage.cs。

SwipeImage.cs的原始程式列表:
using UnityEngine;
using System.Collections;

[RequireComponent (typeof (BoxCollider))] //or other collider
public class ModelRotation : MonoBehaviour {

public float rotationSpeed;
public bool rightSide = false;
private bool onOff = false;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if(onOff && !rightSide)
transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime, Space.World);
else if(onOff && rightSide)
transform.Rotate(Vector3.up, -rotationSpeed * Time.deltaTime, Space.World);
}

void OnMouseDown() {
if(onOff)
onOff = false;
else
onOff = true;
}
}

SwipeImage.cs是內嵌在動畫角色上,因此可以點選HatsuneRumba角色就可以查看程式內全域變數,可以看下圖右下角看到rotationSpeed和rightSide,就能改變其速度和旋轉方向。


ModelRotation.cs的全數變數在下圖右下方,有Page、Image、Sliding Speed、Model分別表示型錄頁數和型錄平面圖、滑動速度、以及3D模型。

詳細程式如下:
// based on: http://forum.unity3d.com/threads/swipe-in-all-directions-touch-and-mouse.165416/
// add video: https://developer.vuforia.com/forum/faq/unity-how-do-i-create-simple-videoplayback-app
// and: https://developer.vuforia.com/forum/faq/unity-how-do-i-play-video-url
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class SwipeImage : MonoBehaviour
{
public GameObject[] page;

Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;

Vector2 deltaPosition;
Vector2 currentPosition;

int amount = 0;
int i = 0;
float accumulatePosition = 0.0f;
public Image[] image;
public float SlidingSpeed = 2.0f;

public GameObject[] model;

//Vector3 originalPosition = new Vector3 (-200,0,-6);


// Use this for initialization
void Start ()
{
amount = GameObject.FindGameObjectsWithTag("page").Length; // amount of models
page = new GameObject[amount];
foreach(GameObject gameObj in GameObject.FindGameObjectsWithTag("page"))  // iterate through model names
{
Debug.Log(gameObj);
page[i] = gameObj;
// page[i].GetComponent().localPosition = new Vector3 (originalPosition.x,originalPosition.y,originalPosition.z); // set to original position
page[i].SetActive(false);
model[i].SetActive (false);
i++;
}
i = 0;
page [0].SetActive (true);
model[0].SetActive (true);
Debug.Log(amount);
for(int j = 0; j < amount; j++ ) {
Debug.Log (j + ": " + page[j]);
}
}

// Update is called once per frame
void Update ()
{
Swipe ();
}

//inside class


public void Swipe ()
{
if (Input.GetMouseButtonDown (0)) {
//save began touch 2d point
firstPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
}
if (Input.GetMouseButton(0)) {
//Vector2 touchDeltaPosition = Input.mousePosition;
//lastMousePosition +=touchDeltaPosition.x;
currentPosition = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
//create vector from the two points
//deltaPosition = new Vector2 (currentPosition.x - lastPosition.x, currentPosition.y - lastPosition.y);
deltaPosition = new Vector2 (currentPosition.x - firstPressPos.x, currentPosition.y - firstPressPos.y);
//if (deltaPosition.x<0 nbsp="" p=""> accumulatePosition += deltaPosition.x;

//if (deltaPosition.x>0)
// accumulatePosition -= deltaPosition.x;
//Debug.Log ("accumulateDelta: " + accumulatePosition + " deltaPosition: " + deltaPosition.x);
float val = System.Math.Abs(deltaPosition.x);
float remap = val.Remap(0, 200, 1, 0);
Debug.Log ("ABS" + val + " REMAP: " + remap);
image[i].color =  new Color(1.0f,1.0f,1.0f,remap);

if(deltaPosition.x < 0.0f){
page[i].GetComponent().localPosition = new Vector3(deltaPosition.x * SlidingSpeed, 0, -6); // page[i].GetComponent().localPosition.x + 
//page[i].transform.Translate(Vector3.left  * 500 * Time.deltaTime);
} else if (deltaPosition.x > 0.0f) {
page[i].GetComponent().localPosition = new Vector3(deltaPosition.x * SlidingSpeed, 0, -6); // page[i].GetComponent().localPosition.x +
//page[i].transform.Translate(Vector3.right * 500 * Time.deltaTime);
}
}

if (Input.GetMouseButtonUp (0)) {
accumulatePosition = 0;
//save ended touch 2d point
secondPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);

//create vector from the two points
currentSwipe = new Vector2 (secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
//Debug.Log ("delta" + (secondPressPos.x - firstPressPos.x));
//normalize the 2d vector
currentSwipe.Normalize ();

//swipe upwards
if (currentSwipe.y > 0 && currentSwipe.x >-0.5f && currentSwipe.x < 0.5f) {
Debug.Log ("up swipe");
}
//swipe down
if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) {
Debug.Log ("down swipe");
}
//swipe left
if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) {
Debug.Log ("left swipe");

page[i].GetComponent().localPosition = new Vector3 (0,0,-6); // set to original position
page[i].SetActive (false);
model[i].SetActive (false);
image[i].color =  new Color(1.0f,1.0f,1.0f,1.0f);
i--;

if (i < 0)
i = amount-1;

model[i].SetActive (true);
page[i].SetActive (true);
}
//swipe right
if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) {
Debug.Log ("right swipe");
page[i].GetComponent().localPosition = new Vector3 (0,0,-6); // set to original position
page[i].SetActive (false);
model[i].SetActive (false);
image[i].color =  new Color(1.0f,1.0f,1.0f,1.0f);
i++;

if (i > (amount-1))
i=0;

page[i].SetActive (true);
model[i].SetActive (true);
}
if ( (currentSwipe.y > 0 && currentSwipe.x >-0.5f && currentSwipe.x < 0.5f) == false && (currentSwipe.y < 0 && currentSwipe.x >-0.5f && currentSwipe.x <0 -0.5f="" .5f="" 0.5f="" 0="" amp="" currentswipe.x="" currentswipe.y="" false="" gt="" lt="" nbsp="" p=""> Debug.Log("Click");
//if (i==0)
// Application.OpenURL("https://www.youtube.com/EdgarasArt");
//if (i==1)
// Application.OpenURL("https://www.ourtechart.com");
//if(i==2)
// Application.OpenURL("https://www.youtube.com/EdgarasArt");
}
}
}



}

public static class ExtensionMethods {

public static float Remap (this float value, float from1, float to1, float from2, float to2) {
return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
}

}

沒有留言:

張貼留言