FlexBatch: Sprites with effects / batch anything

Any community contributions to libgdx go here! Some may get included in the core API when permission is granted.

FlexBatch: Sprites with effects / batch anything

Postby tenfour04 » Wed Jan 25, 2017 3:16 am

I put this library together because I wanted a way to easily draw bump-mapped sprites (or sprites with other types of effects), and SpriteBatch is too specialized for doing that easily. And then I realized there was a need for a tool that is flexible enough to easily tinker with different effects.

Image

FlexBatch works similarly to SpriteBatch, but it lets you easily draw sprites with custom vertex attributes and multi-texturing. The flexibility comes by subclassing a Batchable class to define the vertex attributes, textures, and OpenGL state. FlexBatch then sets itself up to batch that kind of object specifically.

There is a bit more method call overhead if you use the built-in batchable types as compared to SpriteBatch, but in most cases this is unlikely to be the bottleneck. And it's set up so you can come back later and write an optimized version of your Batchable implementation that doesn't have the extra method calls (if the need arises).

SpriteBatch has a lot of overloaded methods. FlexBatch instead uses method chaining to set Batchables' parameters, since it doesn't make sense to create a multitude of overloaded methods to cover all the possible combinations of parameters a Batchable might have.

Code: Select all
flexBatch.setProjectionMatrix(cam.combined);
flexBatch.begin();
flexBatch.draw().textureRegion(brickRegion).textureRegion(brickRegionNormals).position(5, 4).rotation(30);
flexBatch.end();


Since it's flexible, FlexBatch can also be used as a stand-in for DecalBatch (so you can draw 3D sprites with custom effects). There is also a special version of FlexBatch called CompliantBatch that can be used as a stand-in for SpriteBatch/PolygonSpriteBatch so it is compatible with the various LibGDX classes like Stage, BitmapFont, NinePatch, etc.

You can add it to your core gradle dependencies with:

Code: Select all
compile "com.cyphercove.gdx:flexbatch:1.0-SNAPSHOT"


The usage instructions are here. I'm sure this isn't quite adequately tested yet, so I'd love some feedback.
Last edited by tenfour04 on Sun Oct 01, 2017 1:29 am, edited 1 time in total.
tenfour04
 
Posts: 1112
Joined: Sat Jun 18, 2011 3:24 pm

Re: FlexBatch: Sprites with effects / batch anything

Postby evilentity » Wed Jan 25, 2017 10:16 am

Looks cool!
Looking for a freelancer? PM me!
evilentity
 
Posts: 3967
Joined: Wed Aug 24, 2011 11:37 am

Re: FlexBatch: Sprites with effects / batch anything

Postby slebed » Thu Jan 26, 2017 6:49 pm

This looks great TenFour04! Can't wait to use this in my project
slebed
 
Posts: 189
Joined: Fri Dec 28, 2012 3:29 am

Re: FlexBatch: Sprites with effects / batch anything

Postby tenfour04 » Mon Mar 13, 2017 2:08 am

I released version 1.0 to Maven Central, so there's a firm base for usage.
tenfour04
 
Posts: 1112
Joined: Sat Jun 18, 2011 3:24 pm

Re: FlexBatch: Sprites with effects / batch anything

Postby tenfour04 » Sun Oct 01, 2017 1:26 am

I wanted to show off a couple of things I built with FlexBatch!

First, I rewrote the particles code in Digital Embers so I could give the particles 3D motion and depth of field (bokeh). The way I implemented depth of field was first by pre-rendering a set of progressively more blurry versions of each possible particle texture. These are all in my atlas. I use FlexBatch to give one additional vertex attribute to the particles, representing the amount the sprite is blurred. From the extra attribute, the shader calculates texture coordinates for two regions to interpolate between to show the right amount of blur (I used actual lens equations to get the size transitions and brightness right). This could theoretically be done in Java and the extra texture coordinates passed as attributes, but there are a lot of particles and it seems to perform better doing the calculation in the vertex shader, even though it must be done four times per sprite.

Image
Image

Next is my new Sequin Flip live wallpaper. This one has sequins that flip over when you swipe on them. In this case, the "sprites" need vertex attributes for normals (for lighting) and a parameter for calculating shadows. DecalBatch wasn't quite suitable for this because it lacks enough vertex attributes for the shadows. Also, I could optimize it more using the Batchable interface. For instance, I know the top two vertices of each sprite never actually move, but the bottom two do to create the rotation of the sequin.

Image
tenfour04
 
Posts: 1112
Joined: Sat Jun 18, 2011 3:24 pm

Re: FlexBatch: Sprites with effects / batch anything

Postby evilentity » Sun Oct 01, 2017 11:27 am

Really cool examples of what can be done with some clever tricks!
Looking for a freelancer? PM me!
evilentity
 
Posts: 3967
Joined: Wed Aug 24, 2011 11:37 am


Return to Libgdx Contributions

Who is online

Users browsing this forum: No registered users and 1 guest