One big mesh with many different materials

Hi!



Is there any possibility to attach different materials to each triangle in one big mesh?

I have analyzed the GeometryBatchFactory class and I see that there are created bigger meshes for each material - is this the only option that JME is giving to user? Is there any workoround for this problem? I want to mention that I am not interested in TextureAtlas - as far as I can see it is helpfull only for using more than one texture (not for “multimaterials”).



Thanks for any help.

Regards,

That depends on what you mean by materials, but the short answer is no…unless all you need is vertex colouring which is supported.



The longer answer is, maybe - by writing your own shader combining all of the materials then storing the data of what material to use somewhere. That would be a lot of work though.





What are you trying to achieve?

A mesh can only have one material since a material does more than just apply textures to the triangles. Think glow/bloom and other techniques on a mesh, also additional render states such as wireframe and depth testing, face culling…

GeometryBatchFactory is the opposite, it groups meshes by material so the geometry becomes one (still keeping its texture coordinates) and uses the same, shared, material.



What effect are you trying to achieve?

Thanks for fast replay :slight_smile:



I have a bunch of cubes with some transparent materials added (for now without texturing, only colours). Each cube has its own material (different transparency level, different colour and maybe later - different texture). I am merging this cubes in bigger meshes (each 16x16x16=4096 cubes in one mesh) but for now it is possible only when all cubes have the same material. So I suppose that there is no solution for this? Only GeometryBatchFactory?

Are you making a block world by any chance? (note: block worlds are not made of blocks)



You may need to look into texture atlases. That’s a way to have one “material” but have many textures.

Transparency, colour and texture could all be achieved with texture atlas and/or vertex colours though so I don’t see the problem?

zarch - Thanks. I will check this funcionalities more carefully this time.

pspeed - for now I’am not doing anything special… just playing with this engine :slight_smile: And lerning how to do different things wih graphics.

@arczi



you can have many materials, but it split it into meshes contained into Node(when you export VIA OGRE). and it work similar like one mesh.



otherwise: its possible to have many textures(not materials) via TextureAtlas.

oxplay2 - I know how GeometryBatchFactory is working… it produce list of meshes with different material attached. But I don’t want to have separate mesh for each material. I want to have one big merged mesh. Thats why I have to check how it could be achieved: "Transparency, colour and texture could all be achieved with texture atlas … " - and I think it is what I want right now.

If you know how to code everything you need into one shader you can do anything using maps to mark where to apply what kind of “material”. The binding of meshes and shaders is implicit in OpenGL.

Thanks normen :slight_smile: I see that I have a lot of learning (more than I was thinking before).

I’am looking in JME something like this in Unity engine:



http://answers.unity3d.com/questions/51665/colors-do-not-work-with-a-mesh-created-in-script.html



mesh.colors = new Color[] { new Color(0,0,0),

new Color(0,0,0),

new Color(0,0,0) };



I know that in JME we can specify buffors for vertexes and for normals… but how we can specify buffor for vertex colors? It is normal thing that in OpenGL we can do it after glBegin() and before glEnd() block (I mean we can use glColor() there). Is it possible that it is not included in JME?

@arczi said:
I know that in JME we can specify buffors for vertexes and for normals... but how we can specify buffor for vertex colors? It is normal thing that in OpenGL we can do it after glBegin() and before glEnd() block (I mean we can use glColor() there). Is it possible that it is not included in JME?

zarch already wrote hat down in the first post:
unless all you need is vertex colouring which is supported.

There is a buffer you can use like this:
[java] mesh.setBuffer(Type.Color, 2, BufferUtils.createFloatBuffer(colors));[/java]

dont listen to them, what you want is done in 1 line of code.



Use jme3test.tools.TestTextureAtlas example, it will convert your 100 materials into into 1.

enjoy 2500 fps instead 220.

@tralala said:
dont listen to them, what you want is done in 1 line of code.

Use jme3test.tools.TestTextureAtlas example, it will convert your 100 materials into into 1.
enjoy 2500 fps instead 220.


Except he asked about colors so it's good that answer was given. Whether a texture atlas is also appropriate is very dependent on the results required.

polygnome - thanks… it is exactly what I was looking for.



tralala - I have modified TextureAtlas to meet my requirements… this conversion of 100 materials into 1 material is not working as I expected before ananlysing this class. (After my modifications of TextureAtlas IN MY PROGRAM I have x2 more fps than before modifications).

i find it strange that texture atlas is slower. maybe you tell us the methods that can be improved.

@tralala said:
i find it strange that texture atlas is slower. maybe you tell us the methods that can be improved.


Hmmm... I read it as his performance doubles.

i mean his modified texture atlas is faster than jme’s original texture atlas.

Yes… you can ask :slight_smile: But as I said it is only for my purpose - I think my performance changes will not be helpfull for others (thats why i have written it in this way: IN MY PROGRAM )

Because I don’t whant to use more than one material I have it static in my Atlas class (I don’t initialize it ever time I rebuild my chunk of boxes). And only one time I am building this big texture in atlas and reuse it for my meshes… I’am preaty often rebuild chunks so it is good solution for me to have it once builded at the beginning. It works in my case very good (as I said before - aproximatly x2 faster in my case). And something more I have changed there… dont remember right now what… but all this works very fast. TextureAtlas is one big piece of good code.