1

在我的代码中,我使用以下代码创建了 2 个矩阵来存储有关两个不同网格的信息:

Mesh mesh;
MeshFilter filter;

public SphereMesh SphereMesh;
public CubeMesh CubeMesh;

public Material pointMaterial;
public Mesh pointMesh;

public List<Matrix4x4> matrices1 = new List<Matrix4x4>();
public List<Matrix4x4> matrices2 = new List<Matrix4x4>();

[Space]
public float normalOffset = 0f;
public float globalScale = 0.1f;
public Vector3 scale = Vector3.one;

public int matricesNumber = 1; //determines which matrices to store info in

public void StorePoints()
{
    Vector3[] vertices = mesh.vertices;
    Vector3[] normals = mesh.normals;
    Vector3 scaleVector = scale * globalScale;

    // Initialize chunk indexes.
    int startIndex = 0;
    int endIndex = Mathf.Min(1023, mesh.vertexCount);
    int pointCount = 0;

    while (pointCount < mesh.vertexCount)
    {
        // Create points for the current chunk.
        for (int i = startIndex; i < endIndex; i++)
        {
            var position = transform.position + transform.rotation * vertices[i] + transform.rotation * (normals[i].normalized * normalOffset);
            var rotation = Quaternion.identity;
            pointCount++;

            if (matricesNumber == 1)
            {
                matrices1.Add(Matrix4x4.TRS(position, rotation, scaleVector));
            }

            if (matricesNumber == 2)
            {
                matrices2.Add(Matrix4x4.TRS(position, rotation, scaleVector));
            }
            rotation = transform.rotation * Quaternion.LookRotation(normals[i]);
        }

        // Modify start and end index to the range of the next chunk.
        startIndex = endIndex;
        endIndex = Mathf.Min(startIndex + 1023, mesh.vertexCount);
    }
}

GameObject 以立方体网格开始,此代码将有关网格的信息存储在matrices1. 在未显示代码的其他地方,我有它,以便网格更改为球体,然后更改matricesnumber为 2,然后触发上面的代码将新球体网格的信息存储在matrices2.

这似乎是有效的,因为我可以使用像Graphics.DrawMesh(pointMesh, matrices1[i], pointMaterial, 0); 立方体网格的每个顶点绘制 1 个网格这样的代码。我可以使用同一条线(但使用matrices2[i])为 Sphere 网格的每个顶点绘制 1 个网格。

问题:如何matrices1在屏幕上为立方体网格的每个顶点Lerp(信息存储在matrices2

我正试图用类似的东西来破解它

float t = Mathf.Clamp((Time.time % 2f) / 2f, 0f, 1f);
matrices1.position.Lerp(matrices1.position, matrices2.position, t);

但显然这是无效代码。解决方案可能是什么?谢谢。

4

1 回答 1

3

Matrix4x4通常只在特殊情况下用于直接计算变换矩阵。

你很少在脚本中使用矩阵;最常使用Vector3s,类的Quaternions功能Transform更直接。普通矩阵用于特殊情况,例如设置非标准相机投影。

在您的情况下,在我看来,您实际上只需要以某种方式存储和访问position,rotationscale.

我建议根本不使用Matrix4x4,而是使用一些简单的东西,例如

// The Serializable makes them actually appear in the Inspector
// which might be very helpful in you case
[Serializable]
public class TransformData 
{ 
    public Vector3 position;
    public Quaternion rotation; 
    public Vector3 scale; 
}

然后与

public List<TransformData> matrices1 = new List<TransformData>();
public List<TransformData> matrices2 = new List<TransformData>();

你可以简单地越过

var lerpedPosition = Vector3.Lerp(matrices1[index].position, matrices2[index].position, lerpFactor);

如果您随后需要它,Matrix4x4您仍然可以临时创建它,例如

var lerpedMatrix = Matrix4x4.Translation(lerpedPosition);

或者您想使用这些值。

于 2019-06-13T11:48:25.770 回答