# Using multiple textures in materials

**URL:** <https://forum.defold.com/t/using-multiple-textures-in-materials/1673>\
**Category:** Questions\
**Created:** [April 24, 2016, 11:46pm UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673 "2016-04-24T23:46:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tolgahan\_Albayrak](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/tolgahan_albayrak/32/1050_2.png) [@Tolgahan\_Albayrak](https://forum.defold.com/u/Tolgahan_Albayrak)\
**Post date:** [April 24, 2016, 11:46pm UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/1 "2016-04-24T23:46:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [April 25, 2016, 8:42am UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/2 "2016-04-25T08:42:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sven](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sven/32/1086_2.png) [@sven](https://forum.defold.com/u/sven)\
**Post date:** [April 26, 2016, 6:56am UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/3 "2016-04-26T06:56:01Z")

</div>

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: [API reference (Render)](http://www.defold.com/ref/render/#render.render_target)

A short example of a render\_script:

```lua
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! 🙂 )

---

<div class="post-metadata">

**Author:** ![Ivan\_Lytkin](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ivan_lytkin/32/2654_2.png) [@Ivan\_Lytkin](https://forum.defold.com/u/Ivan_Lytkin)\
**Post date:** [August 2, 2016, 11:58am UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/4 "2016-08-02T11:58:32Z")

</div>

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

-thanks

---

<div class="post-metadata">

**Author:** ![morgerion](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/morgerion/32/6641_2.png) [@morgerion](https://forum.defold.com/u/morgerion)\
**Post date:** [June 28, 2017, 8:58am UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/5 "2017-06-28T08:58:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ivan\_Lytkin](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ivan_lytkin/32/2654_2.png) [@Ivan\_Lytkin](https://forum.defold.com/u/Ivan_Lytkin)\
**Post date:** [July 19, 2018, 7:15am UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/6 "2018-07-19T07:15:31Z")

</div>

@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 … )

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [July 19, 2018, 7:18am UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/7 "2018-07-19T07:18:13Z")

</div>

> [@Ivan\_Lytkin](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![sven](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sven/32/1086_2.png) [@sven](https://forum.defold.com/u/sven)\
**Post date:** [July 19, 2018, 7:46am UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/8 "2018-07-19T07:46:52Z")

</div>

> [@Ivan\_Lytkin](#):
>
> `gl_FragColor = texture2D(tex1, var_texcoord0);`

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

```C++
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.

---

<div class="post-metadata">

**Author:** ![Ivan\_Lytkin](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ivan_lytkin/32/2654_2.png) [@Ivan\_Lytkin](https://forum.defold.com/u/Ivan_Lytkin)\
**Post date:** [July 19, 2018, 7:54am UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/9 "2018-07-19T07:54:06Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![Ivan\_Lytkin](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ivan_lytkin/32/2654_2.png) [@Ivan\_Lytkin](https://forum.defold.com/u/Ivan_Lytkin)\
**Post date:** [July 19, 2018, 8:08am UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/10 "2018-07-19T08:08:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [March 30, 2019, 11:54am UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/11 "2019-03-30T11:54:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [December 17, 2021, 10:36am UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/12 "2021-12-17T10:36:56Z")

</div>

> [@selimanac](#):
>
> Does anyone have a proper example project of using two textures with build in sprites?

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

 ![Screenshot 2021-12-17 at 10.26.25](https://forum-defold-uploads.b-cdn.net/original/3X/8/e/8e76f4c4616c8ec0c9c0c0b6afb634ac952cca56.jpeg)

The material:

 ![Screenshot 2021-12-17 at 10.33.05](https://forum-defold-uploads.b-cdn.net/original/3X/6/a/6ab80bf1a92733f5592ea8794d5e3899a4c679d2.png)

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](https://forum.defold.com/uploads/short-url/9mcHfxVLKoaqC9gpTJOYZz8EyEL.zip) (58.6 KB)

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [December 17, 2021, 6:51pm UTC](https://forum.defold.com/t/using-multiple-textures-in-materials/1673/13 "2021-12-17T18:51:39Z")

</div>

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.
