ezyslice 切割代码记录

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using EzySlice;

public class SlicerManager : MonoBehaviour

{

    //public GameObject source;

    public Transform T;

    public Vector3 t1, t2;

    public LayerMask drawLayer;

    public LayerMask objLayer;

    public List<GameObject> sources = new List<GameObject>();              //正在切割的物体集合

    public List<GameObject> allObj = new List<GameObject>();                //所有被切割物体集合

    public Material crossMat;

    public Vector3 originPos;

    public Quaternion originRot;

    public GameObject sphere;

    public bool  isSlice;              //根据这个值来执行切割/组合

    private void Awake()

    {

        originPos = sphere.transform.position;              //初始化原始网格初始位置,combine时需用到

        originRot = sphere.transform.rotation;

    }

    private void Update()

    {

        if (isSlice) SlicePrep();

        else Combine();

    }

    /// <summary>

    /// 定义需要切割的网格,及网格切割位置

    /// </summary>

    private void SlicePrep()

    {


        if (Input.GetMouseButtonDown(0))

        {

            RaycastHit hit;

            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000, drawLayer))

            {

              // t1 = hit.point;

            }

        }

        if (Input.GetMouseButton(0))

        {

                RaycastHit hit;

                if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000,objLayer))

                {

                if (hit.collider.CompareTag("Slicer"))

                {

                    if (!sources.Contains(hit.collider.gameObject))

                    {

                        t1 = hit.point;

                        sources.Add(hit.collider.gameObject);

                    }

                    t2 = hit.point;

                }

                else if(sources.Count > 0)

                {

                    Slice();

                }

                }

            else if (sources.Count > 0)

            {

                Slice();

            }

        }

        if (Input.GetMouseButtonUp(0))

        {

            RaycastHit hit;

            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000, drawLayer))

            {

              // t2 = hit.point;

            }

            Slice();

        }

    }

    /// <summary>

    /// 切割网格

    /// </summary>

    private void Slice()

    {

        foreach (var source in sources)

        {

            T.position = (t1 + t2) / 2;

            T.right = t2 - t1;

            SlicedHull hull = source.Slice(T.position,T.up);

            if (hull != null)

            {

                GameObject u = hull.CreateUpperHull(source, crossMat);

                GameObject l = hull.CreateLowerHull(source, crossMat);

                u.AddComponent<Rigidbody>();

                l.AddComponent<Rigidbody>();

                u.AddComponent<MeshCollider>().convex = true;

                l.AddComponent<MeshCollider>().convex = true;

                u.tag = "Slicer";

                l.tag = "Slicer";

                u.layer = LayerMask.NameToLayer("SliceObj");

                l.layer = LayerMask.NameToLayer("SliceObj"); ;

                u.GetComponent<Rigidbody>().AddForce(Vector3.up * 400);

                u.transform.parent = transform;

                l.transform.parent = transform;

                // source.SetActive(false);

                Destroy(source);

                if (allObj.Contains(source))

                {

                    allObj.Remove(source);

                }

                allObj.Add(u);

                allObj.Add(l);

            }

        }

        sources.Clear();            //每次切割完后清楚当前需切割网格List

    }

    /// <summary>

    /// 网格组合

    /// </summary>

    private void Combine()

    {


        foreach (var item in allObj)

        {

            if (Vector3.Distance(item.transform.position, originPos) > 0.02f)

            {

                item.transform.position = Vector3.Lerp(item.transform.position,originPos,0.02f);


            }

            if (!Quaternion.Equals(item.transform.rotation,originRot))

            {

                item.transform.rotation = Quaternion.Lerp(item.transform.rotation, originRot, 0.02f);

            }

        }

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容