根据屏幕上的触摸生成平面方程

其实看过我前面两篇文章的朋友应该知道这篇文章的意义,就是为切割模型做准备的,其实我是想有空就慢慢把切割模型整个工程写一下的(要是某天懒惰战胜了勤奋就没办法了),原理虽然已经了然,写出来完整版也不是个简单的事。这篇文章相对来说简单点,就是一些数学运算。如需下载完整插件,请到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);
        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁工123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值