其实看过我前面两篇文章的朋友应该知道这篇文章的意义,就是为切割模型做准备的,其实我是想有空就慢慢把切割模型整个工程写一下的(要是某天懒惰战胜了勤奋就没办法了),原理虽然已经了然,写出来完整版也不是个简单的事。这篇文章相对来说简单点,就是一些数学运算。如需下载完整插件,请到http://www.demodashi.com/demo/11343.html
/*
* @authors: liangjian
* @desc:
*/
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.EventSystems;
public class TouchToPlane : MonoBehaviour {
private Vector3 m_TouchBeganPos;
// Use this for initialization
void Start () {
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
m_TouchBeganPos = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
Vector3 touchBeganPoint_screen = new Vector3(m_TouchBeganPos.x, m_TouchBeganPos.y, Camera.main.nearClipPlane);
Vector3 touchEndPoint_screen = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
Vector3 normalEndPoint_screen = new Vector3(m_TouchBeganPos.x, m_TouchBeganPos.y, Camera.main.nearClipPlane + 10);
ClipMesh(touchBeganPoint_screen, touchEndPoint_screen, normalEndPoint_screen);
}
}
void ClipMesh(Vector3 touchBeganPoint_sceen, Vector3 touchEndPoint_sceen, Vector3 normalEndPoint_screen)
{
//转世界坐标
Vector3 touchBeganPoint_world = Camera.main.ScreenToWorldPoint(touchBeganPoint_sceen);
Vector3 touchEndPoint_world = Camera.main.ScreenToWorldPoint(touchEndPoint_sceen);
Vector3 noramlEndPoint_world = Camera.main.ScreenToWorldPoint(normalEndPoint_screen);
//世界坐标转本地坐标
Vector3 touchBeginPoint_local = transform.worldToLocalMatrix.MultiplyPoint(touchBeganPoint_world);
Vector3 touchEndPoint_local = transform.worldToLocalMatrix.MultiplyPoint(touchEndPoint_world);
Vector3 normalEngPoint_local = transform.worldToLocalMatrix.MultiplyPoint(noramlEndPoint_world);
Vector3 planeNormal = Vector3.Cross(normalEngPoint_local - touchBeginPoint_local, touchEndPoint_local - touchBeginPoint_local).normalized;
Debug.Log("触摸决定的平面的法向量为:" + planeNormal + "; 平面上的点有:" + touchBeginPoint_local + "," + touchEndPoint_local);
}
}