Last active
December 19, 2019 07:35
-
-
Save fuqunaga/c0aa186f85f3ffccf340cc8e45e9f2df to your computer and use it in GitHub Desktop.
vtx to texture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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