How to build a complex sprite character by pieces (body and nodes)?

I want to build a complex character made by sprites, which has a body, and some nodes attached to it like arms, head, legs.

Is there a way to do it in native Babylon.js?

In case not, what would the best way to implement it?

I’m thinking in three ways:

  • Implement a node system for sprites, updating each node transform frame by frame accoring to the body transform. Problem: can affect the performance if there are many nodes in screen.

  • Use the physics engine to attach nodes to the body. Problem: I’d been forcing the user to use the physics engine.

  • Associate the sprite body to a rectangle mesh and attach rectangle mesh nodes to the body. Each rectangle mesh would contain a sprite. Problem: It can drive to complex scene compositions.

We do not support this with sprites.

I kind of like the first way :slight_smile:

1 Like

Ok thanks.

I was thinking to go for that way at the moment and not to make the scene more complex.

1 Like

Dragon bones is a great option for 2d animation (skeleton) and produces sprite sheets :

Thanks, but the solution has to work only with Babylon.

why ?

It is for an engine that only works with babylon.

Another method would be to pre-calculate the sprite movements and later retrieve them from a single graphic and simply exchange the images. Like in this example from @Pryme8

Galaga Clone thingy - Demos and projects - Babylon.js (babylonjs.com)

2 Likes

Sorry, I’m not sure if I’m wrong, but in that example all graphics seem to be individual sprites.
Where is the composition of an actor by more than one sprite?

Before I switched to Babylon I used Phaser (a 2D Babylon), where I used the approach suggested above.

It works by layering sprites. So per outfit part (or body part) you have one spritesheet. Like they do here: Universal LPC Sprite Sheet Character Generator (CC-BY-SA!!)

3 Likes

You can use phaser stuff with bjs. That would probably be the best option for a full 2d rigging system.

1 Like

Thank you all guys, but it is for an engine that only works with Bjs. The implementation has to be native to the engine or native to bjs.

I will just update the sprite nodes frame by frame.

I’m sure those options will work for anybody looking to do something similar :slight_smile:

1 Like

I see you’re mentioning a node system, are you perhaps referring to a node material? If not, I would recommend looking into using a Node Material.

With a node material, you can layer any number of textures, which you can swap out at runtime.

Here’s a basic setup with support for 3 layers:

If you’re using pixel art, make sure to set the textures to use the “nearest” sampling mode, so that it doesn’t blur the pixels.

This setup assumes that the opacity in each texture is either fully transparent or fully opaque. Because the “greater than” node is used to check each pixel in the layer for a non-transparent pixel. If it is a non-transparent pixel, then draw that pixel on top of the existing pixels.

Depending on the art style you’re going for, you could also add support for different damage states, by simply adding an overlay texture, on top of each layer, which you can have only appear over transparent pixels in the layer.

Finally, if you want the character to flash white after taking damage, and other effects like that, it’s very simple to add.

1 Like

The system is for Sprites, not textures, but I appreciate your answer.
That’s accurated in case using textures.

Nevertheless I’d consider using that system if it is possible to animate textures using spritesheets.
Is that possible?

I have not looked into how sprites work in Babylonjs, but I know in other 3D renderers that sprites are simply just textures rendered to quads. So, I would assume that node material also works for sprites. But if someone could correct me on that, that would be great.

As for using spritesheets, that is definitely possible as well.

In the graph, you can see a UV node. This node outputs a Vector 2, from 0 to 1 in both X and Y. So to use spritesheet, you just need to offset the UV value based on the bottom left corner of the sprite in the sheet, then multiply the UV value based on the width and height of the sprite.

Then to animate it at runtime, you just need to update 2 Vector2 Input blocks, one controlling the bottom left corner position, the other controlling the size.

For tutorials on how to create a node material, you can also look up videos on shader graph for Unity, as many of the same nodes are available in both.

I think you could precalculate some offset tables to make things faster.

1 Like

If that’s possible and I can use animated textures in the same way as sprites, I will go for that option for the engine. The thing here is being able to build it using code. The node material editor would be a secondary option.

@sebavan What do you think about using textures and material nodes instead sprites for the 2D game engine side?
@Evgeni_Popov Would using textures avoid the need to rearrange sprites depth (textures in this case) every time the camera distance changes?

I think you could attach sprites to bones as I did here (you just have to do it in 2D):

Could you show me a simple example in PG of how to attach two sprites to a bone?

Here is the full repo.

1 Like