4

So I am trying to build a 3D based world consisting of tiles.

I have successfully managed to do this using the plane geometry and height values etc. But now I have come to a point where I possibly have to change everything.

The problem is that I want a tile to have multiple textures (using a shader because I want to blend them). I was able to do this globally (so each tile would have same textures + using some uv mapping).

However I fail to understand how I would be able to specify which tile has which textures (and I have about a 100 textures), since the plane geometry has only 1 shader material. And I am also not sure if it is a good idea to send 100 textures through a shader?

So my questions basically boil down to this:

Is there a decent/performant way to link the tile/vertices to the textures, so I can keep the plane geometry.

- If yes: how?

- if no: Should I create each tile separately (so a plane of 1x1) and merge them somehow together (performance/vertex blending?) so it acts as a single plane (in this case the merged plane consists of many 1x1 planes) and use the shader per tile (a 1x1 plane)?

How are these things generally done?

Edit:

Some extra information because it seems that my question is not really clear:

What I want is that a tile (2 faces) has multiple "materialIndexes" to say so. Currently I need to have 1 tile to have 3 textures, so I can blend them in a shader with a specific algorithm.

For example I want to have a heart shape (red heart/black background) as texture and than based on the colors I want to blend/change the other 2 textures so I can get for example wooden heart and a blue background. Or for example I should be able to blend 4 textures evenly on the square, each take 1/4 of the square. But the point here is not what has to be done with the textures, but how that i can specify such 3, 4, or more textures for my faces/tiles.

Wilt
  • 41,477
  • 12
  • 152
  • 203
Captain Obvious
  • 745
  • 3
  • 17
  • 39

2 Answers2

7

I think you have to take a look at what is called a multi material object.

THREE.SceneUtils.createMultiMaterialObject( geometry, materials );

If you google for those you find several examples. Like this answer on a similar question.


Or take a look at THREE.MeshFaceMaterial. You can use it to assign multiple materials for the same geometry.

var mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( materials ) );

Where materials is an array of materials and the faces use a materialIndex paramater to get appointed the right material

Similar questions here and here


EDIT:

Here is a fiddle to demonstrate that it is possible. I used the code from one of the links.

Community
  • 1
  • 1
Wilt
  • 41,477
  • 12
  • 152
  • 203
  • I don't think neither of them can help me.. what i need to know is how i can say which tile has which textures in a plane of lets say 64*64 tiles.. so i only have one shader where i need to pass attributes? to tell the shader which tile/vertexes which textures has but i dont know how to do this or if this is even possible.. maybe i need to split the plane in many 1x1 planes? – Captain Obvious Oct 02 '14 at 10:39
  • @CaptainObvious I added a fiddle to show you that it is possible. You have to use the `materialIndex` property to appoint the material to your faces. – Wilt Oct 02 '14 at 11:14
  • Hi thanks for the effort, although i think you do not understand me correctly. What i want is that a tile (2faces) has multiple materialIndexes to say so. Currently i need to have 1 tile to have 3 textures, so i can blend them in a shader with a specific algorithm. For example i want to have a heart shape (red heart/Black background) as texture and than based on the colors of it i want to blend/change the other 2 textures so i can get for example wooden heart and a blue background. Or for example i should be able to blend 4 textures evenly on the square, each take 1/4 of the square. – Captain Obvious Oct 02 '14 at 17:08
  • But the point here is not what is done with the textures, but how that i can specify such 3,4,.. whatever textures for my faces/tiles – Captain Obvious Oct 02 '14 at 17:11
0

If your textures have all the same size, you can do the following:

  1. Create a texture 10x the size of an individual texture (you can do that automatically, create a canvas, draw the texture file on the canvas at the correct coordinates, get the texture from the canvas). This allows for 100 (10x10) textures.

  2. Assign to the tiles a base uv coordinates in the range 0 - 0.1 (since a single texture occupies 1/10th of the global texture),

  3. Add to the previous uv values the offset of the individual texture (for the second texture, offset of 0.1 in u and 0 in v, for the 3rd texture 0.2 and 0; for the 10th 0 and 0.1.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
vals
  • 61,425
  • 11
  • 89
  • 138
  • Hi, thanks. That would be a good idea so i don't have to send 100 textures to the shader. However than i still need to know which faces/vertices/xy have which textures.. :) – Captain Obvious Oct 02 '14 at 17:35
  • Since you are only needing 3 textures, one posible solution would be to use face colors, and then use r component to encode the first texture, the g component the second and the b component the third – vals Oct 03 '14 at 17:23
  • well that is one option, but this rather seems to be a hack instead of a general way to do it :) – Captain Obvious Oct 04 '14 at 13:22