触摸旋转和缩放3D模型
2013-01-17 15:24
447 查看
using UnityEngine; using System.Collections; public class SmoothController : MonoBehaviour { public Transform Target = null; public Camera MainCamera = null; private float XAngle = 0f; private float YAngle = 0f; private float XSpeed = 250f; private float YSpeed = 120f; private float Distance = 50f; private float DisSpeed = 10f; private float MinDistance = 0f; private float MaxDistance = 50f; private Vector2 Vec2Pos1 = Vector2.zero; private Vector2 Vec2Pos2 = Vector2.zero; void Start() { MainCamera = Camera.main; } void OnGUI() { if (GUI.Button(new Rect(0f, 0f, 100f, 50f), "ReSet")) { Distance = 50f; Target.transform.rotation = Quaternion.identity; } } void Update() { if (Input.touchCount == 1) { if (Input.GetTouch(0).phase == TouchPhase.Moved) { XAngle += Input.GetAxis("Mouse X") * XSpeed * 0.02f; YAngle -= Input.GetAxis("Mouse Y") * YSpeed * 0.02f; } } if (Input.touchCount > 1) { if (Input.GetTouch(1).phase == TouchPhase.Began) { Vec2Pos1 = Input.GetTouch(0).position; Vec2Pos2 = Input.GetTouch(1).position; } else 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; float deltaDis = DeltaDistance(Vec2Pos1, Vec2Pos2, tempPosition1, tempPosition2); // 如果在扩大 if (deltaDis < 0f) Distance += deltaDis * DisSpeed; else Distance += deltaDis * DisSpeed; // 记录旧的位置 Vec2Pos1 = tempPosition1; Vec2Pos2 = tempPosition2; } } } void LateUpdate() { // 旋转的更新 ClampXY(); Target.transform.Rotate(-YAngle, -XAngle, 0f, Space.World); XAngle = 0f; YAngle = 0f; // 摄像机位置的更新 ClampDistance(); Vector3 cameraPos = MainCamera.transform.position; cameraPos.z = -Distance; MainCamera.transform.position = cameraPos; } bool IsEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2) { float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y)); float leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y)); if (leng1 < leng2) return true; else return false; } float DeltaDistance(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2) { float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y)); float leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y)); return leng1 - leng2; } void ClampXY() { if (XAngle < -360) XAngle += 360; if (XAngle > 360) XAngle -= 360; if (YAngle < -360) YAngle += 360; if (YAngle > 360) YAngle -= 360; } void ClampDistance() { Distance = Mathf.Clamp(Distance, MinDistance, MaxDistance); } }
相关文章推荐
- 在Unity中通过触摸屏幕实现模型的旋转和缩放
- 在Unity中通过触摸屏幕实现模型的旋转和缩放
- Vuforia SDK---- AR开发旋转缩放模型功能
- ogre3D程序实例解析1-平移旋转与缩放
- openGL 创建一个旋转的简单的3D模型
- 触摸缩放、抖动、闪烁、旋转、换色
- Unity3d物体模型(实现旋转缩放平移自动旋转)
- 摄像机、投影、3D旋转、缩放
- 通过Flick手势来旋转3D模型的windows phone应用
- Java3D读取3DMax模型并实现鼠标拖拽、旋转、滚轮缩放等功能
- Vuforia AR SDK对模型进行控制(平移、旋转、缩放)
- 问一下大家个3D问题:旋转和缩放以及平移中如何插值矩阵?
- 【J2me3D系列学习文章之三】(立即模式)对立方体进行变换操作-旋转、缩放、平移
- Unity3D游戏开发之鼠标旋转、缩放实现3D物品展示
- 3D中矩阵旋转、缩放及投影的公式
- [OpenGL ES 04]3D变换实践篇:平移,旋转,缩放
- Imode平台下使用原生数据创建3D模型_旋转
- OpenGL实现3D模型自由旋转——之代码解析
- 转载:摄像机、投影、3D旋转、缩放
- 【J2me3D系列学习文章之三】(立即模式)对立方体进行变换操作-旋转、缩放、平移