Setting the material of a face in a mesh

Hello! I want to make a voxel game, but before I move forward, how would I be able to dynamically generate each chunks textures?

Let me elaborate: When a face is generated (like how in Minecraft only exposed faces are rendered) how do you assign a particular material to the face to represent a block surface? To make an efficient chunk system, I want each chunk to be its own mesh. From my experience in Playcanvas, you can only assign a single material to a single mesh, but is there a better way to do that in scripts for separate faces?

Thank you.

Hi @Kurios,

You can easily do that if you prepare your model for that.

You can do it in Blender for example, assign a unique material on each side of a cube. When you export that cube to FBX and import in PlayCanvas you will get six material slots, one for each side of the cube.

From there you can drag and drop any material on any face.

Here is an example FBX:

cube.zip (4.3 KB)

Thank you @Leonidas , however, how do you do that with a procedural mesh created in-game? In this situation, you start with an empty entity, generate the voxel data, build the mesh from the data, and texture it along the way. Does playcanvas support that?

I think in that case instead of thinking in cubes, you just need to think in planes.

Generate a mesh for the grass faces, another mesh for dirt, another for water etc.

But if you plan to edit the voxels on runtime that maybe too complex/slow to run per frame. It may be better to use plane primitive objects and use something like HW instancing to easily add/remove instances and their position/rotation.

I did some research into this, but this is out of my league unfortunately. Although batching may work…? Thank you @Leonidas.

Batching will definitely work for a static mesh, since you will place all the plane/cube faces and you will call the batcher.

But if you plan to edit them, then static batching will have too big of an overhead. Using dynamic batching so you can enable/disable (reposition) the planes can work but only for a small scene. For a bigger scene the overhead of having many entities will be big.

I see. Could you lead me to some resources that can help me understand Hardware Instancing? Thank you!

Sure check this manual page:

https://developer.playcanvas.com/en/user-manual/optimization/hardware-instancing/

Example:

https://playcanvas.github.io/#/graphics/hardware-instancing

WAIT! @Leonidas! What if I have a single image texture that has all the voxel textures tiled in one, and when I generate the chunk mesh, create custom UVs for each of the faces so that they display the texture I want them to? Could that work?

Sure, that will work just fine too, it’s a common practice to use a texture atlas. If you can set the right UVs for each triangle it will work as expected.

Great! Thank you for helping me figure this out. I will try to implement the best optimization possible!

1 Like