团结:将网格柔和地包裹在其他网格上吗? [英] Unity: Wrap mesh softly around other mesh?

查看:83
本文介绍了团结:将网格柔和地包裹在其他网格上吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个网格(如左侧的立方体对象)和另一个自定义球状网格(如右侧;如果更容易,则可以是另一种形状),Unity& C#在运行时将第二个网格柔和地包裹在第一个网格周围?谢谢!

Given one mesh (like the cubic object on the left) and another custom sphere-like mesh (on the right; it could be another shape if easier), how would one in Unity & C# during runtime softly wrap the second mesh around the first? Thanks!

推荐答案

以下方法,由于使用VirtualMethodStudio作为指针,因此采用了包装球体,然后为其中的每个顶点向内投射光线,并将该顶点调整为命中点:

The following approach, with thanks to VirtualMethodStudio for the pointer, takes a wrapper sphere which then for each vertice in it casts a ray inwards, and adjusts that vertex to the hit point:

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();
    }

}

然后可以通过上述替代方法是使用Collider.ClosestPoint(vertexPoint).

An alternative to above is to use Collider.ClosestPoint(vertexPoint).

这篇关于团结:将网格柔和地包裹在其他网格上吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆