【问题标题】:Unity: Wrap mesh softly around other mesh?Unity:将网格轻轻包裹在其他网格周围?
【发布时间】:2018-06-25 11:09:20
【问题描述】:

给定一个网格(如左侧的立方体对象)和另一个自定义类球体网格(右侧;如果更简单,它可能是另一种形状),Unity 和 C# 中的一个如何在运行时软包裹第二个网格围绕第一个?谢谢!

【问题讨论】:

    标签: c# unity3d mesh


    【解决方案1】:

    以下方法,感谢 VirtualMethodStudio 的指针,采用一个包装球体,然后为其中的每个顶点向内投射一条射线,并将该顶点调整为命中点:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ShrinkWrapSphere : MonoBehaviour {
    
        void Start() {
            Debug.Log("Starting...");
    
            MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
            Mesh mesh = meshFilter.mesh;
    
            Vector3[] vertices = new Vector3[mesh.vertices.Length];
            System.Array.Copy(mesh.vertices, vertices, vertices.Length);
    
            for (int i = 0; i < vertices.Length; i++) {
                Vector3 rayDirection = -mesh.normals[i];
    
                RaycastHit hit;
                if ( Physics.Raycast( vertices[i], rayDirection, out hit, 100f ) ) {
                    vertices[i] = hit.point * 2f;
                }
                else {
                    vertices[i] = Vector3.zero;
                }
            }
    
            mesh.vertices = vertices;
    
            Debug.Log("Done. Vertices count " + vertices.Length);
    
            // mesh.RecalculateBounds();
            // mesh.RecalculateNormals();
            // mesh.RecalculateTangents();
        }
    
    }
    

    然后可以通过this asset 的简化功能进一步简化生成的网格。

    上面的替代方法是使用 Collider.ClosestPoint(vertexPoint)。

    【讨论】:

      猜你喜欢
      • 2015-11-05
      • 2014-05-28
      • 2020-07-11
      • 2013-09-28
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多