Unity 如何标定物体网格(Mesh)上的顶点?

前言

  • 为了确定“手术刀”的网格上的边界点(切割线,用一个小面片来模拟),有两种途径,第一种:控制一个带颜色的小球的position来显示顶点的位置;第二种,利用OnDrawGizmos函数来做标记。

代码实现

  • 由于前者实用性不高,不做过多讲解。采用这样样的方式进行操控就可以

    public GameObject ball;
    ball.transform.position = transform.InverseTransformPoint(
    		transform.GetComponent<MeshFilter>().sharedMesh.vertices[10]);//10为随便举的例子
    

OnDrawGizmos函数

  • 该函数每帧运行 且不需要运行Unity函数 主要用途是作为调试,值得注意的是,一般情况该函数绘制点不会出现在Scene中,但可以在该处进行设置
    在这里插入图片描述
    选中该选项,Scene中也将可见!!!
  • 请注意,OnDrawGizmos将使用相对于“场景视图”的鼠标位置。如果组件在检查器中折叠,
    则不会调用此函数。选择游戏对象后,使用OnDrawGizmosSelected绘制小控件。
  • 实现代码
void OnDrawGizmos()
    {
        Debug.Log("beging to draw");
        Gizmos.color = Color.red;//设置颜色
        /*
        * 在起初找点的范围时 
        * 采用for循环 确定某一个区域(不断缩小区域) 
        * 然后再精确的查找
        */
        for (int i = 729; i < 730; i += 2)
        {
           Gizmos.DrawSphere(transform.TransformPoint(
           			gameObject.GetComponent<MeshFilter>().sharedMesh.vertices[i - 1]), 0.001f);
            Gizmos.DrawSphere(transform.TransformPoint(
            		gameObject.GetComponent<MeshFilter>().sharedMesh.vertices[i]), 0.001f);
        }
       Gizmos.DrawSphere(transform.TransformPoint(
       			gameObject.GetComponent<MeshFilter>().sharedMesh.vertices[761]), 0.001f);
    }
  • 需要注意的是:标注的坐标位置应转化为世界坐标,效果如图:
    在这里插入图片描述

OnDrawGizmosSelected函数

  • 与OnDrawGizmos函数相似,但该函数仅在物体被选中时被执行,即:其挂载的物体被选中,执行该函数。

  • 官方示例:

    using UnityEngine;
    
    public class gizmoTest : MonoBehaviour
    {
        public float explosionRadius = 5.0f;
    
        void OnDrawGizmosSelected()
        {
            // Display the explosion radius when selected
            Gizmos.color = new Color(1, 1, 0, 0.75F);
            Gizmos.DrawSphere(transform.position, explosionRadius);
        }
    }
    

Gizmos类

  • 静态方法

    函数名解释
    DrawCube绘制实心框. Draw a solid box with center and size.
    DrawFrustum绘制摄影机视锥体小控件. Draw a camera frustum using the currently set Gizmos.matrix for it’s location and rotation.
    DrawGUITexture绘制纹理 .Draw a texture in the Scene.
    DrawIcon绘制小图标.Draw an icon at a position in the Scene view.
    DrawLineDraws a line starting at from towards to.
    DrawMeshDraws a mesh.
    DrawRayDraws a ray starting at from to from + direction.
    DrawSphere球.Draws a solid sphere with center and radius.
    DrawWireCube立方体.Draw a wireframe box with center and size.
    DrawWireMesh空心网格.Draws a wireframe mesh.
    DrawWireSphere空心球. Draws a wireframe sphere with center and radius.
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值