Using multiple textures in materials

Hello,
I would like to assign a secondary texture programmatically to the sprite’s material by calling sprite.set_constant function.
Is that possible? or How could I do that?

How could I use an image from an atlas as a texture?

Thanks in advance

2 Likes

Yes, I think this is possible. @sven or @sicher, can you explain this please?

1 Like

It’s possible to set secondary textures programmatically, but only textures that you create programmatically, something we call “render targets”. By this I mean textures created with render.render_target(...), see: http://www.defold.com/ref/render/#render.render_target

A short example of a render_script:

function init(self)
    ...

    -- create a fancy render target
    local rt_prop_depth = {
        format = render.FORMAT_DEPTH,
        width = render.get_window_width(),
        height = render.get_window_height(),
        u_wrap = render.WRAP_CLAMP_TO_EDGE,
        v_wrap = render.WRAP_CLAMP_TO_EDGE,
        min_filter = render.FILTER_NEAREST,
        mag_filter = render.FILTER_NEAREST,
    }
    local rt_prop_color = {
        format = render.FORMAT_RGBA,
        width = render.get_window_width(),
        height = render.get_window_height(),
        u_wrap = render.WRAP_CLAMP_TO_EDGE,
        v_wrap = render.WRAP_CLAMP_TO_EDGE,
        min_filter = render.FILTER_NEAREST,
        mag_filter = render.FILTER_NEAREST,
    }
    local rt_prop = {[render.BUFFER_COLOR_BIT] = rt_prop_color, [render.BUFFER_DEPTH_BIT] = rt_prop_depth}
    self.composite_rt = render.render_target("comp", rt_prop)
end

function update(self)
    -- set as current render target and render scene:
    render.enable_render_target(composite_rt)

    -- setup viewport and render stuff here!
    ...

    -- disable render target means we are rendering to the back buffer again
    render.disable_render_target(composite_rt)

    -- setup viewport etc
    ...

    -- render our usual predicates but with a second texture
    render.enable_texture(1, composite_rt, render.BUFFER_COLOR_BIT)
    render.draw(self.fullscreen_pred)
    render.disable_texture(1, composite_rt, render.BUFFER_COLOR_BIT)
end

(Please note that this is untested code! :slight_smile: )

Hi!
And what should be in fragment shader in this case (2 textures)?
As it always uses only the first texture for me :frowning:

-thanks

It’s still unclear how to set the 2nd texture for the Sprite through the settings of the Sprite itself (or GO).
It would be nice to have this opportunity.
Then you could control the use for the sprite several textures at once.
This is necessary for making the alpha channel separate from the RGB.

2 Likes

@sven
So what I’m doing wrong?
I declare 2 samplers in fp, e.g.:
uniform sampler2D tex0;
uniform sampler2D tex1;

and then

gl_FragColor = texture2D(tex1, var_texcoord0);

takes data from the first texture - set with render.enable_texture(0 … )

You should supply the index of the sampler to use here. E.g. 0 will enable the first sampler, 1 the second sampler and so on.

That will only sample data from one of the textures… What do you want to accomplish with multi-texturing? Start with something like this:

vec4 c0 = texture2D(tex0, var_texcoord0);
vec4 c1 = texture2D(tex1, var_texcoord0);
float b = 0.5;
gl_FragColor = c0 * b + (1.0 - b) * c1;

(Just mixing the two textures 50% each.) To make sure you are getting both textures into the shader.

And as @Johan_Beck-Noren said, you probably need to verify what sampler number you are using with render.enable_texture, it needs to match the sampler name in the material the objects are rendered with.

@sven
Yes, i do it as you described, but it’s not working for me.
texture2D() always takes the first texture (with “0” index)

I just trying to get the data from second texture, without any color blending.
Both textures are created with render_target, but have different size and filtering

Oh, I found it!
I forgot to add tex0 \ tex1 in “Samples” of material properties!

3 Likes

Does anyone have a proper example project of using two textures with build in sprites? I’ll be glad if anyone could share or explain it.

5 Likes

I did this today on a model, maybe the same method could be used on sprites?

The material:

This change to the renderscript allows proper alpha to transparent;

Original:

render.set_blend_func(render.BLEND_SRC_ALPHA, render.BLEND_ONE_MINUS_SRC_ALPHA)

Changed:

render.set_blend_func(render.BLEND_ONE, render.BLEND_ONE_MINUS_SRC_ALPHA)

Example project:
AlphaBlendedTextures.zip (58.6 KB)

2 Likes

Multi texturing for sprites would allow this but afaik it’s not a feature but is/was planned.

Only reliable way I’ve found to do it with sprites was to use tilesheets with offsets.

1 Like