Unity - How can I transform the teapot

April 22, 2013 - 7:43am #1

Thsi article shows how the teapot (or other 3D augmentation model attached to an Image Target) can be scaled, rotated and translated with respect to the base reference frame of its parent Image Target. This article refers to the Unity extension of Vuforia and assumes you are at least familiar with the ImageTargets sample for Unity.

Steps to follow:

  • create a project by importing the ImageTargets sample package
  • select (double-click) the ImageTargets scene package, so that the targets appear in your scene view, each target having a teapot attached
  • for simplicity, remove the "Stones" and the "Tarmac" targets (so that your scene only contains the "chips" teapot) 
  • create a C# script, called TargetTransformer
  • edit the script code and copy this code:
using UnityEngine;
using System.Collections;

public class TargetTransformer : MonoBehaviour {
	
	private Transform mTeapotTransform = null;
	
	// Use this for initialization
	void Start () 
	{
		GameObject teapot = GameObject.Find("teapot");
		if (teapot != null)
		{
			Debug.Log("Teapot found");
			mTeapotTransform = teapot.transform;
		}
	}
	
	// Update is called once per frame
	void Update () 
	{
	}
	
	void OnGUI()
	{
		if (mTeapotTransform == null) return;
		
		if (GUI.Button(new Rect(50,0,140,70), "Scale Up"))
		{
			float oldScale = mTeapotTransform.localScale.x;
			float newScale = oldScale * 1.5f;
			mTeapotTransform.localScale = new Vector3(newScale, newScale, newScale);
		}
		if (GUI.Button(new Rect(50,100,140,70), "Scale Down"))
		{
			float oldScale = mTeapotTransform.localScale.x;
			float newScale = oldScale / 1.5f;
			mTeapotTransform.localScale = new Vector3(newScale, newScale, newScale);
		}
		if (GUI.Button(new Rect(50,200,140,70), "Rotate CW"))
		{
			Quaternion oldRotation = mTeapotTransform.localRotation;
			mTeapotTransform.localRotation = Quaternion.AngleAxis(30, Vector3.up) * oldRotation;	
		}
		if (GUI.Button(new Rect(50,300,140,70), "Rotate CCW"))
		{
			Quaternion oldRotation = mTeapotTransform.localRotation;
			mTeapotTransform.localRotation = Quaternion.AngleAxis(-30, Vector3.up) * oldRotation;	
		}
		if (GUI.Button(new Rect(50,400,140,70), "Translate Y"))
		{
			Vector3 localPos = mTeapotTransform.localPosition;
			localPos.y += 0.1f;
			mTeapotTransform.localPosition = localPos;
		}
		if (GUI.Button(new Rect(50,500,140,70), "Translate -Y"))
		{
			Vector3 localPos = mTeapotTransform.localPosition;
			localPos.y -= 0.1f;
			mTeapotTransform.localPosition = localPos;
		}
	}
}
  • drag the TargetTransformer script onto your ARCamera

Build and run the App. The app should show various buttons in overlay, to scale, rotate and translate your target.

 

Topic locked