Skip to content

Instantly share code, notes, and snippets.

@leventeren
Created November 6, 2023 22:00
Show Gist options
  • Save leventeren/9585f1015659ebbc6092c8607733f879 to your computer and use it in GitHub Desktop.
Save leventeren/9585f1015659ebbc6092c8607733f879 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
public class PointTracker : MonoBehaviour
{
private MeshFilter meshFilter;
private Transform[] markers;
void Start()
{
meshFilter = gameObject.GetComponent<MeshFilter>();
markers = new Transform[meshFilter.mesh.vertexCount];
Debug.Log(string.Format(
"Tracking {0} vertices in \"{1}\"",
meshFilter.mesh.vertexCount,
transform.name));
for (int i = 0; i < meshFilter.mesh.vertexCount; i++)
{
markers[i] = ObjectPool.Instance.GetObject().transform;
markers[i].name = string.Format("{0} marker{1}", transform.name, i);
}
}
void Update()
{
// make a locally scoped copy of mesh vertices -- if we directly access
// vertices using meshFilter.mesh.vertices[i] inside of the for() loop
// we create a tremendous amount of GC cleanup (~113MB...)
Vector3[] vertices = meshFilter.mesh.vertices;
for (int i = 0; i < meshFilter.mesh.vertexCount; i++)
markers[i].position = TransformVertex(vertices[i], transform);
}
private Vector3 TransformVertex(Vector3 vertex, Transform parent)
{
return parent.localToWorldMatrix.MultiplyPoint3x4(vertex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment