如何在Unity中缩放网格中孔的顶点

问题描述 投票:0回答:1

我正在制作关于洞的游戏,

游戏的网格中有洞,物体从洞中掉落。就像Hole.io游戏一样。 我已经实现了通过顶点移动网格中的孔,并且孔是根据我的愿望重新定位或移动。 我通过获取网格中孔的顶点来移动孔,然后在下面给出的脚本中移动或重新定位它。 我有 2 个问题需要帮助。

1- 我无法通过网格中孔的顶点来使孔变大或变小。 2-我无法在网格的孔中添加更多顶点。

有没有人可以帮助该图像,下面给出了网格的代码

图片:

public MeshFilter meshFilter;

public MeshCollider meshCollider;

public Vector2 moveLimits;

//Hole vertices radius from the hole's center
public float radius;

public Transform holeCenter;
public Transform rotatingCircle;

public float moveSpeed;

Mesh mesh;
List<int> holeVertices;
//hole vertices offsets from hole center
List<Vector3> offsets;
int holeVerticesCount;

float x, y;
Vector3 touch,
targetPos;


void Start()
{
    RotateCircleAnim();

    //Initializing lists
    holeVertices = new List<int>();
    offsets = new List<Vector3>();

    //get the meshFilter's mesh
    mesh = meshFilter.mesh;

    //Find Hole vertices on the mesh
    FindHoleVertices();
}

void RotateCircleAnim()
{
    rotatingCircle.DORotate(new Vector3(90f, 0f, -90f), .2f).SetEase(Ease.Linear).From(new Vector3(90f, 0f, 0f)).SetLoops(-1, LoopType.Incremental);
}

void Update()
{
    MoveHole();
    //Update hole vertices
    UpdateHoleVerticesPosition();

}

void MoveHole()
{
    x = Input.GetAxis("Mouse X");
    y = Input.GetAxis("Mouse Y");

    //lerp (smooth) movement
    touch = Vector3.Lerp(holeCenter.position, holeCenter.position + new Vector3(x, 0f, y), moveSpeed * Time.deltaTime);

    targetPos = new Vector3(Mathf.Clamp(touch.x, -moveLimits.x, moveLimits.x), touch.y, Mathf.Clamp(touch.z, -moveLimits.y, moveLimits.y));

    holeCenter.position = targetPos;
}

void UpdateHoleVerticesPosition()
{
    //FindHoleVertices();
    //Move hole vertices
    Vector3[] vertices = mesh.vertices;

    for (int i = 0; i < holeVerticesCount; i++)
    {
        vertices[holeVertices[i]] = holeCenter.position + offsets[i];
    }

    //update mesh vertices
    mesh.vertices = vertices;
    //update meshFilter's mesh
    meshFilter.mesh = mesh;
    //update collider
    meshCollider.sharedMesh = mesh;
}

void FindHoleVertices()
{
    for (int i = 0; i < mesh.vertices.Length; i++)
    {
        //Calculate distance between holeCenter & each Vertex

        float distance = Vector3.Distance(holeCenter.position, mesh.vertices[i]);
        
        if (distance < radius)
        {
            //this vertex belongs to the Hole
            holeVertices.Add(i);
            //offset: how far the Vertex from the HoleCenter
            offsets.Add(mesh.vertices[i] - holeCenter.position);
        }
    }

    //save hole vertices count
    holeVerticesCount = holeVertices.Count; 
}


//Visualize Hole vertices Radius in the Scene view
void OnDrawGizmos()
{
    Gizmos.color = Color.yellow;
    Gizmos.DrawWireSphere(holeCenter.position, radius);
}
c# unity-game-engine mesh
1个回答
0
投票

也许使用着色器(模板缓冲区)创建孔效果会更容易?

© www.soinside.com 2019 - 2024. All rights reserved.