Displacing a mesh based on a texture using Python

Hello, first post, but a lurker for a while. Been using for Blender for under a year, and I’m trying to get my feet wet in the game engine and Python. It’s probably straightforward, but I can’t seem to find an answer. I have an icosphere, and I have used the displace modifier to, well, displace it. That’s working fine, but I want to take it a step further and take anything below the midlevel (or some threshold) and merge them at the center. I would think the easiest way to go about this is to take anything from the texture that is below that threshold and change it’s location (to 0,0,0), via Python, but I’m not sure how to go about doing it.

I’m not asking anyone to write my code for me; I would really rather learn instead of just plugging something in, but any guidance, or pointing me in the right direction would be really great!

Many apologies if this was posted elsewhere or in the wrong forum.


Is this really a game engine question? Why do you need to do the change in real-time?

I’m afraid the answer is not as straight forward as you probably expect. The modifiers don’t exist in game engine and their effects are only applied on the meshes prior to running the game. Moreover BGE isn’t too sophisticated in modifying meshes (compared to Blender’s edit mode). Everything is in tris and vertices aren’t merged.

  1. This means it’s usually hard to find the vertices you want and keep things intact although if you just want to grab them via distance check it shouldn’t be very hard. You can start your search from here: http://www.blender.org/documentation/blender_python_api_2_71_release/bge.types.KX_VertexProxy.html#bge.types.KX_VertexProxy

  2. I would still recommend a shape key driven by action for the effect you’re describing and for any other effect that modifies a set group of vertexes in a linear way.

  3. If it’s just a cosmetic change it could be also best to modify the mesh via vertex shader in which case you should be heading here: http://en.wikibooks.org/wiki/GLSL_Programming/Blender

I do not need to do the change in real-time, actually, sorry if I mislead you. I only want to do this once, and preferably before runtime.

This game I am making takes place on a spherical, planet-type map. I have a heavily subdivided icosphere to represent that map, and it is displaced with a texture. Basically, everywhere where there would be an ocean, I just want it to drop to the center of the planet. Does this clarify anything?

So 1 sounds like it’s on the right track for me. I would go about this by looping through every vertex in the mesh, determining the distance from the center, and if the distance is less than a certain value, move the vertex to 0,0,0? And then to get the physics to correspond, I call… kx.obj.reinstancePhysicsMesh()? This seems to be an inefficient way of doing it.

For 2, if I’m not mistaken, I think would only apply to if I wanted to do it in real-time. Right?

And 3, it isn’t just a cosmetic change.

Even if you move all the vertices to the center, the polygons will still exist (IE they won’t merge despite being infinitely small)- this means you won’t get any performance benefit. Unless you’re going for a visual style where the landmasses are cone-like shapes that extend from the center point, I can’t see any benefit to moving underwater vertices to the center of the planet.

However, if you are going for that visual style, suggestion 1 would be the best way to do it, certainly simpler than reading the texture and most likely more efficient (although if you are only running it once at the beginning of the game, the efficiency gain would be negligible)

I’m not sure what look you’re going for, but you might also be able to get away with not using a separate object for the ocean- instead of moving vertices below a surface, you could consider using a node material modified by vertex colors to apply an ocean material to underwater parts of the world- then all you have to do is use a script to change the vertex colors of anything that should be underwater, and the ocean material will appear there. An advantage of this technique is you could have the borders of all the oceans be a foamy material, which would be much more difficult to do if the ocean is a separate intersecting sphere.

(I can make an example file when I get home if you want)

Actually, I was going for a visual style where all the landmasses are cone like shapes - just trying to avoid the typical Earth-like atmosphere. I’ll give it a go and see how I fare. I’m not sure what I’ll wind up doing in the end, but I’ve definitely some questions answered and more options to think about now. No doubt I’ll have questions in the future.