- Home /
Procedural Mesh Texturing
So i have a mesh being procedurally generated by Graphics.DrawMesh() i pass it a mesh and a material. I have many textures for the mesh that i need to procedurally place on it but i cant figure out how to put more than 1 texture on the mesh id prefer to do this without a custom shader. Any ideas?
Is this a $$anonymous$$esh with multiple $$anonymous$$aterials, or do you just need multiple textures to fill various segments of the uv layout?
If it's the latter, you're going to need a custom shader to mix multiple textures together.
Answer by BastianUrbach · Apr 18, 2018 at 01:14 PM
These are the ways I see:
Multiple submeshes
Both Mesh.SetTriangles and Graphics.DrawMesh have an overload where you can specify a material index. That way you can make a mesh with multiple materials and then have a different Graphics.DrawMesh call for each material, allowing you to use different materials and textures for them.
Atlas textures
Use an atlas texture (a texture that actually contains many textures side by side) and set the uv coordinates accordingly.
Texture blending
Use terrain-like texture blending. That would require a custom shader but not really a difficult one. This is the only way I see to have smooth transitions between textures in case that's a requirement. You can go through the terrain shader (available from the download section on Unity's website) and see how they did it.
For the texture atlas is it possible to combine many textures into an atlas at runtime?
Certainly, here is a simple example that assumes all textures are the same size:
var atlas = new Texture2D(size * textures.Length, size); for (int i = 0; i < textures.Length; i++) { atlas.SetPixels(size * i, 0, size, size, textures[i].GetPixels()); } atlas.Apply(true, true);Note that the textures must be readable (Read/Write enabled in the texture import settings).
Your answer
Follow this Question
Related Questions
Is it possible to make an image sequence into an animated normal map? 0 Answers
Accidentally Clicked Refresh Assets and This Happened. How Do I Fix It? 0 Answers
My material doesn't look as it should 1 Answer
use RenderTexture on a sphere correctly? 1 Answer
Is it possible to get png files out of an .sbsar file? 0 Answers