SpriteBatching with Shaders

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

SpriteBatching with Shaders

Postby angelus » Sun Jul 22, 2012 10:13 am

I'm trying to do the next (it seems to be done by a partner here):

I want to make a game where i can change color pixels of textures (Arcade retro ... ) , but i have a big stick problem:
I have one big texture ( 256 * 4096 (1024 x 1024 equivalent) and i need to extract from there parts (regions) , and change the color of them with a palette in order to use it in the game.

We have some alternatives :

- We can throw out game color change and make each sprite with its colors (pain and memory headeach)
- We can continues with our idea , then we have this alternatives:

* Use a Pixmap for all the game (256x224 arcade size)
The problem which comes with this is that i must extract region and throw it to a pixmap ,and Pixmaps doesn't work with Pixmaps , then i must copy each texture .
* Use shaders with an uniform var palette (png)
I have tried this way , but spritebatch doesn't goes well when you must atach another shader with vars you must send.
I's ok , but i couldn't make it (i have the shaders done) using spritebatch , the only way is using mesh render to do it.
* Having indexed pngs
I think this is not supported by libgdx , then i can't think going this way.

My problem , i don't know what is the best option , and fighting with shaders , anyone can show me an example of passing uniforms and using spritebatch? (no mesh rendering directly)

Any help? , new ideas are wellcome , and i'll be greated for your time.

Sorry for my bad english
angelus
 
Posts: 9
Joined: Sun Feb 05, 2012 9:16 pm

Re: SpriteBatching with Shaders

Postby kalle_h » Fri Aug 03, 2012 9:59 pm

Make custom shader that can handle normal spriteBatch functionality but on fragment side use texture color as index.
So your color palette is 256x1 texture.
To make batching possible you could use 256x256 texture that can contain 256 different palettes.

Your main texture can be 8-bit alpha channel texture. This is used as index.

For selecting which palette you use you can use per sprite vertex color. Let just pick alpha channel as index there.

Example palette indexing shader.
Code: Select all
"#ifdef GL_ES\n" //
                        + "#define LOWP lowp\n" //
                        + "precision mediump float;\n" //
                        + "#else\n" //
                        + "#define LOWP \n" //
                        + "#endif\n" //
                        + "varying LOWP vec4 v_color;\n" //
                        + "varying vec2 v_texCoords;\n" //
                        + "uniform sampler2D u_texture;\n" //
                        + "void main()\n"//
                        + "{\n" //
                        + "  LOWP float colorIndex = texture2D(u_texture, v_texCoords).a;\n" //
                        + "  LOWP vec2 uv = vec2(colorIndex, v_color.a);\n" //
                        + "  gl_FragColor = texture2D(u_texture1, uv);\n" //
                        + "}";



To make this work just copy the spritebatch normal vertex shader from the source and use this as fragment shader.
After creating this shader send uniformi parameter 1 for sampler u_texture1. Also bind your palette texture to texture unit 1 and remember to bind current unit back to 0.

Then your only problem is how to make that palette texture but that should be breeze.
kalle_h
 
Posts: 636
Joined: Thu Dec 29, 2011 9:50 pm

Re: SpriteBatching with Shaders

Postby angelus » Wed Aug 08, 2012 7:49 am

Thank you for your efforts , i'll try it and tell you next month (now i have some tests :D)
angelus
 
Posts: 9
Joined: Sun Feb 05, 2012 9:16 pm

Re: SpriteBatching with Shaders

Postby Varriount » Wed Aug 08, 2012 8:44 am

Couldn't you just make some texture regions from that big texture and use the opengl color functions to tint when creating them to the screen?

Alternately, you could use a combination of the stencil buffer and blending. Turn the color buffer off, render the regions you want to the stencil buffer (using alpha blending to filter out unwanted pixels), and then turn color back on and render a color out to the regions, using blending.
Think of the unanswered posters : search.php?search_id=unanswered
If I'm you and you're me, then who's he?
Varriount
 
Posts: 249
Joined: Tue Jun 05, 2012 2:48 pm


Return to General Development

Who is online

Users browsing this forum: Bing [Bot] and 2 guests