1

私のコードでは、次のコードを使用して、2 つの異なるメッシュに関する情報を格納する 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 は Cube メッシュとして開始され、このコードはメッシュに関する情報を に保存しmatrices1ます。表示されていないコードの他の場所では、メッシュが Sphere に変更matricesnumberされてから 2 に変更され、上記のコードをトリガーして新しい Sphere メッシュの情報を に保存するようにしていmatrices2ます。

Graphics.DrawMesh(pointMesh, matrices1[i], pointMaterial, 0); Cube メッシュの頂点ごとに 1 つのメッシュを描画するようなコードを使用できるため、これは機能しているようです。そして、同じ線 (ただしmatrices2[i]) を使用して、球体メッシュの頂点ごとに 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