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;? ? ? ? ? ? ? //根據(jù)這個值來執(zhí)行切割/組合
? ? private void Awake()
? ? {
? ? ? ? originPos = sphere.transform.position;? ? ? ? ? ? ? //初始化原始網(wǎng)格初始位置胚嘲,combine時需用到
? ? ? ? originRot = sphere.transform.rotation;
? ? }
? ? private void Update()
? ? {
? ? ? ? if (isSlice) SlicePrep();
? ? ? ? else Combine();
? ? }
? ? /// <summary>
? ? /// 定義需要切割的網(wǎng)格勉盅,及網(wǎng)格切割位置
? ? /// </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>
? ? /// 切割網(wǎng)格
? ? /// </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();? ? ? ? ? ? //每次切割完后清楚當(dāng)前需切割網(wǎng)格List
? ? }
? ? /// <summary>
? ? /// 網(wǎng)格組合
? ? /// </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);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}