Are dual-texture SpriteBatches possible?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Klowner
Prole
Posts: 7
Joined: Thu Mar 13, 2014 8:23 pm

Are dual-texture SpriteBatches possible?

Post by Klowner »

From what I can tell, the documentation makes it pretty clear that a SpriteBatch is associated with a single source texture, but I was curious if there is any way to bind a secondary texture so I could use texture samplers in a Shader while drawing the SpriteBatch.

The specifics of what I'm attempting to do are bind a regular texture for diffuse coloring as well as a matching normal map texture for lighting, ideally allowing me to reuse the same SpriteBatch and render all the Quads in one pass.

I suppose my other option would be to bind "color" Canvas, batch:setImage(colortiles), draw scene, bind second "normal map" Canvas, batch:setImage(normaltiles), draw batch, then combine the canvases at the end?

Any ideas would be greatly appreciated!

-Klowner
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Are dual-texture SpriteBatches possible?

Post by micha »

You are correct in that you can only attach one image to a spriteBatch.

I don't know much about normal maps and lighting, but depending on what you mean by "combine the canvases" you can possibly do this without canvases: batch:setImage(colortiles), draw scene, batch:setImage(normaltiles) draw batch with lighting shader.
User avatar
Klowner
Prole
Posts: 7
Joined: Thu Mar 13, 2014 8:23 pm

Re: Are dual-texture SpriteBatches possible?

Post by Klowner »

That basically sounds like what I need! Thank you. I was also skimming the docs and noticed there is a Shader:send(name, image, ...), I imagine I can use that to access additional textures from within a Shader during a SpriteBatch draw.
User avatar
slime
Solid Snayke
Posts: 3160
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Are dual-texture SpriteBatches possible?

Post by slime »

Klowner wrote:From what I can tell, the documentation makes it pretty clear that a SpriteBatch is associated with a single source texture, but I was curious if there is any way to bind a secondary texture so I could use texture samplers in a Shader while drawing the SpriteBatch.

The specifics of what I'm attempting to do are bind a regular texture for diffuse coloring as well as a matching normal map texture for lighting, ideally allowing me to reuse the same SpriteBatch and render all the Quads in one pass.
All draw operations in love use up to one main texture, and anything more than that is up to the shader you use.

For example:

Code: Select all

image = {
    diffuse = love.graphics.newImage("diffuse.png"),
    normals = love.graphics.newImage("normals.png"),
}

spritebatch = love.graphics.newSpriteBatch(image.diffuse)
spritebatch:add(100, 100)

shader = love.graphics.newShader[[
extern Image normalmap;

vec4 effect(vec4 vcolor, Image diffuse, vec2 texcoord, vec2 pixcoord)
{
    vec4 diffusecolor = Texel(diffuse, texcoord) * vcolor;
    vec3 normal = normalize(2.0 * Texel(normalmap).xyz - 1.0);
    // etc.
    vec4 finalcolor = /* whatever */;
    return finalcolor;
}
]]

shader:send("normalmap", image.normals)

function love.draw()
    love.graphics.setShader(shader)
    love.graphics.draw(spritebatch, 0, 0)
    love.graphics.setShader()
end
User avatar
Klowner
Prole
Posts: 7
Joined: Thu Mar 13, 2014 8:23 pm

Re: Are dual-texture SpriteBatches possible?

Post by Klowner »

Thank you, slime! That's exactly what I needed. :awesome:
Post Reply

Who is online

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