Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@fuqunaga
Last active December 19, 2019 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuqunaga/c0aa186f85f3ffccf340cc8e45e9f2df to your computer and use it in GitHub Desktop.
Save fuqunaga/c0aa186f85f3ffccf340cc8e45e9f2df to your computer and use it in GitHub Desktop.
vtx to texture
static Texture2D MeshToMap(Mesh mesh)
{
var vertices = mesh.vertices;
var count = vertices.Count();
float r = Mathf.Sqrt(count);
var width = (int)Mathf.Ceil(r);
var height = width;
var positions = vertices.Select(vtx => new Color(vtx.x, vtx.y, vtx.z));
var tex = CreateMap(positions, width, height);
return tex;
}
static Texture2D CreateMap(IEnumerable<Color> colors, int width, int height)
{
var tex = new Texture2D(width, height, TextureFormat.RGBAFloat, false);
tex.filterMode = FilterMode.Point;
tex.wrapMode = TextureWrapMode.Clamp;
var buf = new Color[width * height];
var idx = 0;
foreach (var color in colors)
{
buf[idx] = color;
idx++;
}
tex.SetPixels(buf);
tex.Apply();
return tex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment