Apply Material to a Mesh Dynamically

At run-time, I would like to apply materials to each vert of a mesh individually. I can already get the vert list for the mesh, I would just like to know if and how you can apply a material to each vert.

What I have is a terrain model that is dynamically altered at run-time using armatures (random terrain). What I would like to do is apply a material to each vert based on the general slope of the area. For example, an area where a mountain is rising might change from grassy to stone-dirt with sporadic shrubs.

It is, tentatively, important that I use verts to apply materials to the ground model as I intend to also apply materials based on environment type (in the future I’ll be placing trees, for instance, where snow fall might be minimal and will adjust the texture under the tree accordingly, using the vert that the tree is placed on as the center).

Update
As CoDEmanX pointed out, materials can’t be assigned to verts. So would it be possible to get all faces a vert is used in and apply a material that way? Or a better method would be to select all verts in a radius around the target vert and add them to a vert group.

For example, I have a vert that is to be along the center of a road. There would be a string of these verts drawing a path, but we’ll focus on just one. All verts within a 3 meter radius would be selected and added to a vert group called “Dirt Road Verts” and apply a material that way. Would this be possible?

To clarify, I need to figure out how to select groups of verts based on various criteria including height data (which is only determined at run-time), so that the texture/material can be then applied. I would prefer materials so that I can use logic to change things like foot-fall sounds (touching material: stone -> playsound: hard-foot-fall).

I said below that I’d post a video of my method of terrain deformation in action so…
[video]http://www.dan.gslyr.com/blender/random.avi[/video]

materials can only be assigned per face, not vertex. You could use vertex colors, but that’s probably not what you want.

*updated OP - I’ve seen some great work, such as this http://www.blenderartists.org/forum/showthread.php?220878-Island-BGE-Demo-Scene&highlight=Island, but unfortunately that guy never posted a blend file. Maybe it’s shaders I’m looking for? I saw that he was able to assign colors to verts, and it looked like he applied textures to color channels or some such? I don’t want anyone to think I have any idea what he was doing there. I don’t have a clue. :stuck_out_tongue:

Just found another great example of real-time texture application. This guy paints it as if he was in the editor. http://www.youtube.com/watch?v=KxW1Tb5s62U&feature=youtu.be

That seems to work well, except that I don’t have clearly defined areas before run-time. In this scene the terrain model is randomly deformed to produce random terrain. It’s fairly effective, I’ll post a link to the video in the OP. Anyway, since I don’t have clearly defined areas like you might have for a character’s shirt and pants and gloves etc, I can’t apply this method. Yet.

To clarify, I need to figure out how to select groups of verts based on various criteria including height data (which is only determined at run-time), so that the texture/material can be then applied. I would prefer materials so that I can use logic to change things like foot-fall sounds (touching material: stone -> playsound: hard-foot-fall).

Hi Scutt this is Corey,

I saw you've commented on my youtube video asking me how I was able to apply textures to a randomly generated map. Unfortunately I haven't been able to figure out how to apply vertex texture painting to a model in real-time...this is very sad, in the video I was just changing the vertex color of vertexes. I wish I knew how to vertex texture paint in real time in blender but I haven't been able to figure out how to do it...Although vertex texture painting is nearly impossible in blender(I say nearly because someone might have a solution that I doubt they will have) you can try changing to a different game engine that supports this kind of ability, but for Blender only, you can try changing the face materials in real time. That would probably be your best bet, but you wont have that smooth blend between materials.

I really sorry to give you this conclusion but I’ve tried to do exactly what youre asking for and I haven’t been able to do it.

Sounds like a long weekend of trying to create a solution… :-p

you know spending an entire weekend to accomplish one task is exactly what scripting is all about lol. If scripting was easy than everybody would do it. We have that special skill to stare at a computer screen and work on one thing for hours on end without breaks or socializing with people. We fail a lot more than we accomplish, which is amazing…Use this power wisely hahahah XD.