Programatically adding textures

Hello,
Is there any way to put multiple textures at different positions on a mesh ?

Do you mean something like this?

what do you mean ?

Sorry forgot the link Use Multi-Materials - Babylon.js Documentation

1 Like

Yes, something like that, I will try and implement it tomorrow. Thank you very much !

I’ve tried doing this on an Ground from heightmap, but I can’t understand how the vertices are organized. I need to split it in 40x40 squares and add different textures.

Ahh! Using a heighmap and multimaterials is a step too far for me.

To understand the organisation of the vertices have a look at
https://doc.babylonjs.com/how_to/updating_vertices
https://doc.babylonjs.com/how_to/custom
https://doc.babylonjs.com/resources/normals

Also worth a read How to Use Dynamic Terrain - Babylon.js Documentation

For ordinary ground you could consider

https://doc.babylonjs.com/how_to/set_shapes#tiled-ground
https://doc.babylonjs.com/how_to/tiled

1 Like

Thank you for the answer, I was trying to draw some streets. The easiest way (also the least computationally intensive way) seemed to be to just draw another ground * with less triangles * a little higher than the normal ground with the street textures.
But now I see I will be better off with some custom meshes (maybe getting the vertex data or throwing some rays from the ends of the street pieces and generating new hights).

Hi guys! Recently, we had some success with using BABYLON.GUI.AdvancedDynamicTexture.CreateForMesh(ground2); … on heightMaps.

https://www.babylonjs-playground.com/#14MFVB#12

Currently, the map is sized 70% width/height of terrain… and is mouse-pointer-draggable.

This uses 2 identical heightMap terrains… ground and ground2. I think it has to be that way, because primary ground is textured. But maybe… GUI advancedDynamicTexture uses material.emissiveTexture, and maybe brown ground texture could use material.diffuseTexture. I haven’t tried to use a single ground, yet. All experimental.

AND, I don’t know if “live-swapping” GUI 2D imageControls (entire map textures) to change your maps/roadways… is plausible for your project… or not. It’s a different kind of programatic control.

Very sloppy Wingnut code… global vars and other bad coding, but it could be cleaned up.

I just thought I should show you what is possible with GUI ImageControls laid-atop AdvancedDynamicTexture.CreateForMesh layer(s). Currently, it’s about the closest thing we have to a movable/scalable decal. Tile-able when using GUI GridContainer, I suspect.

Yes, I thought of generating the street textures on the server, but I’m trying to lower the strain on the server (as the streets are dynamic, they refresh every second).
Also globals aren’t that sloppy, I use several events out of sync with globals (the buildings are generated every second, players move at 60 fps, sun moves every minute etc.) and it’s not that bad, but indeed the code is a complete chaos.

1 Like

I went with creating custom geometry (with new BABYLON.Mesh), but now I have problems attaching a texture to the tiles

Hi G. Can you create a playground demo that shows the issue? That would help.

I believe your home-grown mesh “tiles?” will need some UV-kind data, if you haven’t yet installed such.

As you may/may-not know, UVs sort of tell textures how to map-onto the mesh. UVs are 2-values each, and they represent the X/Y of the texture… in values 0-1 inclusive.

Easier said, when a vertex has a UV of .25, .35, the texture will map its 25% X and 35% Y position (as measured from texture lower left corner) to THAT vertex.

https://www.babylonjs-playground.com/#1UHFAP#248 There is a “constructed” (plotted) Babylon Mesh (a slightly sub-divided plane with material/texture). Lines 84-94 set the UVs. Particularly notice lines 87-91… .33 = 33% “up the y-axis”… and .66 = 66% “up the y-axis”, etc. If you disable line 89 and activate line 90 (and re-RUN)… you can see the texture get an abnormal “stretch” near vertex #4.

Please ignore any axis-indicators painted on the texture. That texture was designed for grounds, and not for vertical planes and UVs work. :slight_smile: Pretend Z is actually Y (in UVs land). :slight_smile:

With precise UV “management”, the texturing for many many “tiles”… can be placed onto a single texture file (similar to a “sprite-map” and sometimes called an “atlas”, perhaps).

You probably know all about UVs already. If so, sorry for my yapping. UVs can be a bit of a challenge to set. I hope I have been helpful and on-topic.

2 Likes

That’s perfect, thank you very much.

1 Like

For future reference, setting the normals fixed the texture problem (on the custom meshes).