zoukankan      html  css  js  c++  java
  • Unity 中关于SubMesh的拾取问题

    问题背景

    最近在开发一个功能,钻孔功能,每一层(段)都需要单独拾取,显示不同的颜色,使用不同材质

    问题分析

    对于这个功能,由于上述需求,很容易想到用submesh实现,但是主要问题是在于对于Submesh的拾取,如何知道拾取到那一段?

    解决方案:

    通过Unity中自带的RaycastHit解决,里面的触碰的三角形索引,根据三角面判断所在Submesh。

    上代码

    获取钻孔Mesh

     1         /// <summary>
     2         /// 获取gameobject的mesh
     3         /// </summary>
     4         /// <param name="gameobject">目标物体</param>
     5         /// <returns></returns>
     6         private Mesh GetMesh(GameObject gameobject)
     7         {
     8             if (gameobject)
     9             {
    10                 MeshFilter meshFilter = gameobject.GetComponent<MeshFilter>();
    11                 if (meshFilter)
    12                 {
    13                     Mesh mesh = meshFilter.sharedMesh;
    14                     if (!mesh) { mesh = meshFilter.mesh; }
    15                     if (mesh)
    16                     {
    17                         return mesh;
    18                     }
    19                 }
    20             }
    21             return (Mesh)null;
    22         }
    View Code

    获取拾取Submesh索引逻辑

     1    /// <summary>
     2         /// 获取拾取的submesh索引
     3         /// </summary>
     4         /// <param name="mesh"></param>
     5         /// <returns></returns>
     6         private int GetSubmeshIndex(Mesh mesh, RaycastHit hit)
     7         {
     8             int index = 0;
     9             if (mesh)
    10             {
    11                 int[] hittedTriangle = new int[]
    12                 {
    13                      mesh.triangles[hit.triangleIndex * 3],
    14                      mesh.triangles[hit.triangleIndex * 3 + 1],
    15                      mesh.triangles[hit.triangleIndex * 3 + 2]
    16                 };
    17                 for (int i = 0; i < mesh.subMeshCount; i++)
    18                 {
    19                     int[] subMeshTris = mesh.GetTriangles(i);
    20                     for (int j = 0; j < subMeshTris.Length; j += 3)
    21                     {
    22                         if (subMeshTris[j] == hittedTriangle[0] &&
    23                             subMeshTris[j + 1] == hittedTriangle[1] &&
    24                             subMeshTris[j + 2] == hittedTriangle[2])
    25                         {
    26                             Debug.Log(string.Format("triangle index:{0} submesh index:{1} submesh triangle index:{2}", hit.triangleIndex, i, j / 3));
    27                         }
    28                     }
    29                 }
    30             }
    31             return index;
    32         }
    View Code

    就这样,欢迎指正。

  • 相关阅读:
    洛谷 P3392 涂国旗
    CODEVS 1066/洛谷 P1514引水入城
    POJ 1286 Necklace of Beads(项链的珠子)
    CODEVS 1138 聪明的质监员
    洛谷 P1241 括号序列
    C++之路进阶——codevs2313(星际竞速)
    C++之路进阶——codevs2366(朋友圈)
    c++之路进阶——hdu3507(Print Article)
    C++之路进阶——codevs2404(糖果)
    C++之路进阶——codevs4655(序列终结者)
  • 原文地址:https://www.cnblogs.com/answer-yj/p/12450156.html
Copyright © 2011-2022 走看看