Unity 使用 EzySlice 实现模型切割

Unity 使用 EzySlice 实现模型切割

老规矩,直接上代码:
EzySlice 插件 在下面的链接工程中。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EzySlice;

public class Splitter_ZH : MonoBehaviour
{
    //切割预制体材质
    public Material _NewMaterial;

    //被切割预制体数组
    public  List <GameObject> _ListGamPreFab;

    
    void Update()
    {
        float _Mx = Input.GetAxis("Mouse X");

        transform.Rotate(0, 0, _Mx);

        //当点击鼠标左键时
        if (Input.GetMouseButtonDown(0))
        {
            //创建忽略切割对象
            Collider[] _Colliders = Physics.OverlapBox(transform.position, new Vector3(4, 0.00005f , 4), transform.rotation, ~LayerMask.GetMask("Solid"));

            foreach (var item in _Colliders)
            {
                Destroy(item.gameObject);

                //切割
                //GameObject[] _objs GameObject[] _objs = item.gameObject.SliceInstantiate(transform.position, transform.up);

                //切割出现的物体
                SlicedHull _SlicedHull = item.gameObject.Slice(transform.position, transform.up);
                if (_SlicedHull != null)
                {
                    //切割下半部分部分  物体
                    GameObject _Lower = _SlicedHull.CreateLowerHull(item.gameObject, _NewMaterial);

                    //切割上半部分部分  物体
                    GameObject _Upper = _SlicedHull.CreateUpperHull(item.gameObject, _NewMaterial);

                    GameObject[] _objs = new GameObject[] { _Lower, _Upper };

                    for (int i = 0; i < _objs.Length; i++)
                    {
                        _objs[i].AddComponent<Rigidbody>();
                        _objs[i].AddComponent<MeshCollider>().convex = true;
                    }
                }
            }
        }


        //物体生成
        if (Input.GetMouseButtonDown(1))
        {
            GameObject _GamPrefab = _ListGamPreFab[Random.Range(0, _ListGamPreFab.Count - 1)];

            if (_GamPrefab.GetComponent<Rigidbody>())
            {
                GameObject _NewGamPrefab= Instantiate(_GamPrefab);
                _NewGamPrefab.GetComponent<Rigidbody>().AddForce(Vector3.up * 500);
                
            }
            else
            {
                _GamPrefab.AddComponent<Rigidbody>();
                
                GameObject _NewGamPrefab = Instantiate(_GamPrefab);
                _NewGamPrefab.GetComponent<Rigidbody>().AddForce(Vector3.up * 500);
            }
        }

    }
}

暂时先这样吧,如果实在看不明白就留言,看到我会回复的。
路长远兮,哈哈哈,与君共勉。

GitHub: EzySlice 工程

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
var target1 : Transform; var target1C : Transform; var target2 : Transform; var target2C : Transform; var mousePos1 : Vector3; var mousePos2 : Vector3; var cursorImage : Texture; var Mouse : GUISkin; private var MouseImg : boolean = false; function Update() { if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { if(Input.touchCount == 1) { mousePos1 = Input.touches[0].position; } else if(Input.touchCount == 2) { mousePos1 = Input.touches[0].position; mousePos2 = Input.touches[1].position; } } else { mousePos1 = Input.mousePosition; } target1.position = camera.ScreenToWorldPoint (Vector3(mousePos1.x,mousePos1.y,1)); target2.position = camera.ScreenToWorldPoint (Vector3(mousePos2.x,mousePos2.y,1)); } function LateUpdate() { if(Input.GetKey(KeyCode.Escape)) { Application.Quit(); } } function OnGUI() { if(MouseImg) { GUI.skin = Mouse; var windowRect : Rect = Rect (mousePos1.x - cursorImage.width/2, Screen.height - mousePos1.y - cursorImage.height/2, cursorImage.width, cursorImage.height); windowRect = GUI.Window (0, windowRect, DoMyWindow, "My Window"); } if(GUILayout.Button("PlanA")) { Screen.showCursor = !Screen.showCursor; target1.gameObject.active = !target1.gameObject.active; target1C.gameObject.active = target1.gameObject.active; if(Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) { target2.gameObject.active = !target2.gameObject.active; target2C.gameObject.active = target2.gameObject.active; } } else if(GUILayout.Button("PlanB")) { Screen.showCursor = !Screen.showCursor; MouseImg = !MouseImg; } else if(GUILayout.Button("Restart")) { Application.LoadLevel(0); } if(GUI.Button(new Rect(Screen.width-120,Screen.height-40,120,30),"Click to YUHUA!")) {
以下是一个简单的 Unity C# 脚本,用于实现模型切割(剖切): ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; public class ModelCutter : MonoBehaviour { public GameObject modelToCut; // 需要剪切的模型 public GameObject cutPlane; // 用于剪切的平面 public Material cutMaterial; // 用于剪切的材质 private Renderer modelRenderer; // 模型的渲染器 private Material[] originalMaterials; // 模型的原始材质 private Material[] cutMaterials; // 剪切后的材质 void Start() { modelRenderer = modelToCut.GetComponent<Renderer>(); originalMaterials = modelRenderer.materials; cutMaterials = new Material[originalMaterials.Length]; for (int i = 0; i < originalMaterials.Length; i++) { cutMaterials[i] = new Material(cutMaterial); } } void Update() { Plane cuttingPlane = new Plane(cutPlane.transform.up, cutPlane.transform.position); bool isCutting = false; for (int i = 0; i < modelRenderer.bounds.max.x; i++) { Vector3 point = new Vector3(i, modelRenderer.bounds.center.y, modelRenderer.bounds.center.z); if (cuttingPlane.GetSide(point)) { isCutting = true; break; } } if (isCutting) { modelRenderer.materials = cutMaterials; } else { modelRenderer.materials = originalMaterials; } } } ``` 使用方法: 1. 将该脚本挂载到场景中的一个空 GameObject 上。 2. 将需要剪切的模型和用于剪切的平面分别赋值给 `modelToCut` 和 `cutPlane` 变量。 3. 创建一个材质,用于剪切的效果,将该材质赋值给 `cutMaterial` 变量。 4. 运行场景即可看到模型被剖切的效果。 需要注意的是,该脚本只是一个简单的实现,可能会存在一些问题,如性能问题、材质问题等,需要根据具体情况进行优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Maddie_Mo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值