- Home /
Question by
K-Anator · May 17, 2020 at 07:14 PM ·
materialsmeshfiltermesh renderermaterial renderer
Setting material based on vertex?
I'm using a modified version of https://github.com/darkfall/MagicaVoxelUnity to import Voxel models in to my project. I've figured out how to make it create material assets for colors that don't exist, and that's working well. I even managed to assign those materials to the model as long as it's imported as individual voxels.
The problem is, some models I'd like to bring in merged. I can still create the necessary materials, but I have no idea how to apply them to the specific faces based on their color.
Here's the section of code near the bottom of MVImporter.cs that creates my textures, as well as the last bit of information it passes.
if (verts.Count > 0)
{
Mesh mesh = new Mesh ();
mesh.vertices = verts.ToArray();
mesh.colors = colors.ToArray();
mesh.normals = normals.ToArray();
mesh.triangles = indicies.ToArray ();
mesh.uv = uvs.ToArray();
result.Add (mesh);
totalQuadCount += currentQuadCount;
}
for (int i = 0; i < colors.Count; i++)
{
Color color = colors[i];
Material tempMat = (Material)AssetDatabase.LoadAssetAtPath("Assets/MagicaVoxel/Materials/" + ColorUtility.ToHtmlStringRGBA(color) + ".mat", typeof(Material));
if (tempMat == null)
{
Material material = new Material((Material)AssetDatabase.LoadAssetAtPath("Assets/MagicaVoxel/Materials/_Default.mat", typeof(Material)));
material.SetColor("_BaseColor", color);
AssetDatabase.CreateAsset(material, "Assets/MagicaVoxel/Materials/" + ColorUtility.ToHtmlStringRGBA(color) + ".mat");
Debug.Log("Created a new material for palette colour: " + ColorUtility.ToHtmlStringRGBA(color));
}
}
Debug.Log (string.Format ("[MVImport] Mesh generated, total quads {0}", totalQuadCount));
return result.ToArray();
Comment