Spritebatch extension

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
cval
Citizen
Posts: 58
Joined: Sun Apr 20, 2014 2:15 pm
Location: Ukraine

Spritebatch extension

Post by cval » Wed Apr 27, 2016 8:45 pm

I don't know if anyone did this already, but in my previous thread i've come with a way to change vertex data of a spritebatch. It allows user to change individual sprite geometry and vertex colors (and change color of already existing sprites), and as a bonus - change sprite drawing order.

https://github.com/mkdxdx/Augmented-spritebatch

I've asked moderators to close or move previous thread, but i think it's kind of messy so excuse me if this one is considered out of forum rules.
sprbatch_vertattr.love
(10.85 KiB) Downloaded 61 times
This is example of what it looks like:
love 2016-04-27 23-55-32-03.gif
love 2016-04-27 23-55-32-03.gif (281.46 KiB) Viewed 1594 times
getVertex/setVertex allows you to change individual vertex data and setSpriteColor allows you to change sprite color. However after you do any transofmation (move, scale or angle) geometry will return back to its rectangular format because of vertex recalculation.

What can this be used for? Imagine you want to make spiderweb in your game. It is a static texture, and probably is going to be drawn alot of times but for it to have variety you should distort it's geometry a bit. Another example is where you want to change sprites with each other (characters become in front or back of each other etc.), you name it.

User avatar
HDPLocust
Prole
Posts: 33
Joined: Thu Feb 19, 2015 10:56 pm
Location: CENTER OF THE RED LANDS
Contact:

Re: Spritebatch extension

Post by HDPLocust » Fri Jun 24, 2016 6:09 am

Realy intresting, but i care for perfomance and memory.
Testing in progress!
Мой английский не очень хорош, заранее прошу простить. Любви каждому.
If you see it, you may will sheet bricks :3

cval
Citizen
Posts: 58
Joined: Sun Apr 20, 2014 2:15 pm
Location: Ukraine

Re: Spritebatch extension

Post by cval » Fri Jun 24, 2016 5:33 pm

Well, you will have a bottleneck if your sprites are going to be updated every frame, but in my application where i have, say, 20k of static sprites and around 500 dynamic, constantly updated sprites in a separate spritebatch, i don't really see much of a drop below 60 fps.
But i imagine someone having serious cringe while looking at how i rewrote "set" method as an override for everything to work.

Also if you really care about perfomance while trying to apply optimizations in your love2d projects, consider looking through a discussion, maybe you're worrying too much (=

User avatar
slime
Solid Snayke
Posts: 2770
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Spritebatch extension

Post by slime » Sat Jun 25, 2016 6:50 pm

[wiki]SpriteBatch:attachAttribute[/wiki] lets you attach a vertex attribute from any mesh to any SpriteBatch.

You can even override existing attributes used by the SpriteBatch like colors (the "VertexColor" attribute), so you can implement custom per-vertex colors, texture coordinates, positions, or anything else in a spritebatch just by having certain attribute values come from mesh data instead of from the original SpriteBatch. No need to completely emulate what SpriteBatches already do to accomplish that.

cval
Citizen
Posts: 58
Joined: Sun Apr 20, 2014 2:15 pm
Location: Ukraine

Re: Spritebatch extension

Post by cval » Sun Jun 26, 2016 9:59 am

slime wrote:[wiki]SpriteBatch:attachAttribute[/wiki] lets you attach a vertex attribute from any mesh to any SpriteBatch.

You can even override existing attributes used by the SpriteBatch like colors (the "VertexColor" attribute), so you can implement custom per-vertex colors, texture coordinates, positions, or anything else in a spritebatch just by having certain attribute values come from mesh data instead of from the original SpriteBatch. No need to completely emulate what SpriteBatches already do to accomplish that.
Well, the whole thing basically revolves around this function. And at that moment when i found out what it allows to do i was too excited about it so i couldn't stop myself from building something around it, even while realising that i'm rewriting already existing features ^_^ . I think i did it so that i could conviniently call just one "attachAttribute" just before i draw spritebatch instead of calling it after each transformation an object that uses sprite requires (like, change positions etc). And to educate myself on vertex order each sprite uses.

User avatar
HDPLocust
Prole
Posts: 33
Joined: Thu Feb 19, 2015 10:56 pm
Location: CENTER OF THE RED LANDS
Contact:

Re: Spritebatch extension

Post by HDPLocust » Mon Jun 27, 2016 2:32 am

Hm. I found spriteBatch:setColor() for each next SpriteBatch:add/set().
And SpriteBatch:attachAttribute() too (for vertex shaders).
For transformation, also uses sx, sy, ox, oy, kx, ky into SpriteBatch:set()

I want to get additional api for spitebatches, like attached attributes for fragment shaders or setting different shaders for groups of sprites into one spritebatch, but it's hard, i think.
Мой английский не очень хорош, заранее прошу простить. Любви каждому.
If you see it, you may will sheet bricks :3

cval
Citizen
Posts: 58
Joined: Sun Apr 20, 2014 2:15 pm
Location: Ukraine

Re: Spritebatch extension

Post by cval » Mon Jun 27, 2016 10:13 am

HDPLocust wrote: I want to get additional api for spitebatches, like attached attributes for fragment shaders or setting different shaders for groups of sprites into one spritebatch, but it's hard, i think.
You can access sprite vertex attributes in a vertex shader for future calculations and then use varying variable to pass it to fragment shader. But considering the fact that fragment shaders work with fragments/texels/pixels it's going to be nearly impossible to process vertex data in fragment shader directly without first transfering vertex data to separate FBO as pixel color data (mind the texture format). You can also combine shader calls with shader:send function calls to process and then also rendering to separate FBO (like it is done at each pass in Light vs Shadow library). It also depends on what you are trying to achieve, maybe there are ways that it can be done simplier.

Oh and one more thing on the subject - this "extension" was born because of my inattention to wiki. I thought that setColor only works for spritebatch:add and not for set operation, and then i decided to make my own, LOL.

User avatar
modiX
Citizen
Posts: 86
Joined: Mon Dec 04, 2017 1:47 am
Location: Germany

Re: Spritebatch extension

Post by modiX » Fri Jul 20, 2018 8:29 pm

Great work, thanks so much!

This piece is a gem for me. It just not only allows to define each color of the vertices for each quad, it even allows modifying the texture mapping of those 4 vertices to be able to make a trapeze out of a simple sprite, neat. Unfortunately, you do not modify the mesh data regarding shearing factor parameters, which would make shearing easier.

It still seems to require us to maintain a separate mesh that will be used instead, causing a bottleneck when having a "stream" usage SpriteBatch which will be cleared and refilled with quads on every loop. It's a myth to me why there is no API on the SpriteBatch to access the underlying mesh, directly. Instead we are left with two shearing factor parameters that limits us. I think such API to access the mesh within the SpriteBatch should be implemented by the LÖVE devs to give the single SpriteBatch approach even greater possibilities, without having to maintain a different mesh.

Btw: SpriteBatch:setColor should be wrapped by that vaspritebatch.lua, too.
Visual Studio Code TemplateRichLÖVE Mobile (AdMob+UnityAds)Add me on Discord

───▄▀▀▀▄▄▄▄▄▄▄▀▀▀▄───
───█▒▒░░░░░░░░░▒▒█───
────█░░░░░░░░░█────
▄▄──█░░░▀█▀░░░█──▄▄
█░░█▀▄░░░░░░░▄▀█░░█

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests