Author Topic: Changing terrain texture (ie. changing texture for part of a 3d object)  (Read 4775 times)

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
I have a 3D terrain object that is currently made up of lots of joined together squares (ie. it's drawn as one 3D object by my code). Currently I set my texture then draw the land object once per game loop.
Originally all the squares have the same texture, but over time I want to change some of them (eg. make some squares muddy).
Any idea what the best way to accomplish changing part of the texture of a 3D object is?

The only way I've come up with that would work : My terrain is currently 128x128 squares (ie. 32768 triangles) and I'm using x_collisionray to determine which grid location is under the cursor. I could draw each grid square as a separate 3D object and texture them separately but that's a lot of 3D objects draw every frame, and 32768 x_collisionray calls per frame (or even once a second) I'm guessing would make the program really slow.

A bit of googling initially led me to think that a vertex shader is what I'm after but now I'm not so sure - besides which I've got no idea how to write/implement one (assuming that IS what I'm looking for!)

If anyone has any great ideas I'd love to hear them!!!
« Last Edit: 2012-Jun-09 by FutureCow »

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3375
  • Integrated Brain
    • View Profile
Hi future, I thinked about this... I don´t know if can works... if I don´t understand bad you need change only a pieze whit X_collision ray, but how you have a lot of rectangles, you have the check the great number of tetures each sencond... well I thinked about this... if you divide the complete mesh in sectors more small, imagine a matrix of 128x128 divided in example  (32x4)x(32x4), making some rutine you detect in the quadrant you are actualy, and you only have to check (32x4)x(32x4) for the textures, if you make more small the quadrants , less calculations... the quadrants not actives in these same time have the default texture...

I don´t know if I expressed correctly, but I think this have to boost your application...

PS: Sometime I forgot to use... remenber XCullmode, because if you don´t use you draw twice faces of the triangles, and the performance goes slow  a great amount...


Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3375
  • Integrated Brain
    • View Profile
Another idea, just comes to my brain....

And if you make a rectangle whit the primary texture, and depend de coordinates you point whit xcullmode, draw another rectangle over the principal mesh... whit this mode you don´t have to texturing 1000 rectangles per second, only the huge rectange, and the little rectangle, a bit upper the great mesh...

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1545
    • View Profile
Use one object for the terrain mesh grid and just multiple split your texture image, for example set four quadrant textures on one sprite with grass, water, sand, mud and offset half of default when creating the mesh vertexes.  :good: It's basic 2d titling technique, right, or is there more to this? :-[

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Thanks for the replies.
Metalthink - I can't split it up into sections easily. I have a camera that can rotate/zoom in any direction so depending on how you rotate and zoom the camera, you could have just 1 of the smaller meshes on the screen, or some of them, or all of them. The landscape is mountains, where parts of it can hide other parts of the landscape, so working out which meshes are on the screen would be a nightmare.

Matchy, I think I understand what you're saying but unfortunately that's a bit different to what I'm trying to do. To use the technique I think you're suggesting I need to be able to update the texture offset of each vertext. I can't find any command that allows me to do that which is what the problem is.

Just to clarify : My idea is that at the start all 128x128 tiles on my landscape are, for example, grass. A short while later, I might have to set, for example, 1352 of those squares to mud, 15 to snow, 823 to sand. Then a short while later, some of the snow might become ice, other snow ones may become grass again, some that were mud are now grass again, some previously untouched grass ones have become swampy etc. Being able to change the texture of each individual 3D tile is what I need to do.

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1545
    • View Profile
Easy or I don't understand exactly what you mean. Like in Minecraft? The terrain is constantly being recreated to shift the texture offsets. I'm happy to provide sample code as long I am clear on what you really want and as far as I understand, even with animated texture (like for water).


Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
I'll put some images in to show what I mean.

Assume this is the initial landscape (it's not, I stole my image from another thread :D ) - also assume they are just textures and not flat colours. The landscape is a solid mesh where each vertex (tile corner) is (currently) an offset into a texture image that has images of snow, grass, mud etc textures.


Then you change only some of the tiles textures (again, assume the tile has a texture and not a flat colour). Note that not all the ones that were light blue have changed to purple, only one. Also one that was red is now purple.


Does that make it clearer?
« Last Edit: 2012-Jun-10 by FutureCow »

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1545
    • View Profile
I understand that but I'm still a little confused with the demo image. Can you have it so it looks like a landscape rather than a checkerboard?

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Here's an actual screenshot. At the moment I've got it coloured based on height. I'm not sure why the colours are in bands running in 1 direction, it should be colouring in 2 directions. That's just temporary anyway until I get textures sorted. The theory is that every tile (tree's are every 2 tiles at the moment so that gives you an idea of the size tiles I'm working with) should be able to be changed based on how grassy it is. Lots of grass should be bright green. As it dries out, each tile will go towards dirt, and it should be able to have snow per tile etc.



It's exactly the same as the image from the previous post though, it should be able to be textured like a checkerboard potentially.

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1545
    • View Profile
Okay, so now you want to mapped out streams, lakes, sand, whatever....something that resembles the surface of a landscape.

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
But importantly it has to be able to change reasonably frequently - I can make individual tiles a river now for example, but don't want to have them have to be a river for the whole game. I have to be able to make each tile change its texture while still having it part of the same mesh so I can run x_collisionray against it.

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1545
    • View Profile
As I mentioned before, you just recreated the mesh and shift the texture offset in cycle according to a new tile map with a change from the ray collisions coordinates and click. So I imagine you click on a facet and it's quad texture is remapped to water/lake.

Why can't you do that?

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Ah okay, when you were talking about the land being recreated before it didn't click to me that you were suggesting recreating the whole mesh each time to change the texture offsets.
I suppose that's doable. I was hoping more for a way to change one vertext without recreating the whole mesh to save the computational overhead. Failing that though recreating the whole mesh may be the way it'll have to go. Thanks Matchy!

I'll go with that unless anyone can suggest a way to change vertex information.

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1545
    • View Profile
You simple can't modify a 3d object's vertices.  :zzz:

The technique is used for water and wave flags, regardless of texture mapping but then without changing the mesh an alternative is to simply redraw the sprite texture as a low res tile-map from a as many as like based textures 8x8. 1024 sprite / 128 tile = 8x8map to resemble grass, water, etc.  8)
« Last Edit: 2012-Jun-10 by matchy »

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4396
    • View Profile
    • FUED
snif snif, funk funk, smells like powermonger...I´m getting excited with it! :-[