c# - 统一: Wrap mesh softly around other mesh?

标签 c# unity3d mesh

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

enter image description here

最佳答案

以下方法,感谢 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();
    }

}

enter image description here

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

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

关于c# - 统一: Wrap mesh softly around other mesh?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51022070/

相关文章:

c# - Debug.Log 会导致 Unity 构建延迟吗?

Android OpenGL .OBJ 文件加载器

c# - 从 sendMessage 返回值或在 unity webgl 中交替

android - unity 中的 fatal error checkdisalowAllocation

c# - 进程是否在远程机器上运行?

c# - 如何在类(class)内共享 IDisposable 资源?

c# - 实时切割网格

javascript - MeshLab Quadric Edge Collapse Decimation 在 JavaScript、PHP 或 Python 中保留纹理?

c# - 旋转 Microsoft.XNA.Framework.Rectangle 并根据该旋转创建一个矩形?

c# - 抛出 'Foundation.You_Should_Not_Call_base_In_This_Method' 类型的异常