# Repeating texture?

**URL:** <https://forum.defold.com/t/repeating-texture/49201>\
**Category:** Questions\
**Created:** [April 13, 2019, 12:21pm UTC](https://forum.defold.com/t/repeating-texture/49201 "2019-04-13T12:21:02Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Enigmanark](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/enigmanark/32/37904_2.png) [@Enigmanark](https://forum.defold.com/u/Enigmanark)\
**Post date:** [April 13, 2019, 12:21pm UTC](https://forum.defold.com/t/repeating-texture/49201/1 "2019-04-13T12:21:02Z")

</div>

Is there a way to somehow expand the size of a sprite (or some other component) and have the texture repeat inside the expanded area?

---

<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:** [April 13, 2019, 4:20pm UTC](https://forum.defold.com/t/repeating-texture/49201/2 "2019-04-13T16:20:19Z")

</div>

Yes, this is possible. Maybe someone else has a more elegant way of doing it.

You want to have an atlas per tiled texture you want to use. The important thing is that there is no gap around the tiled texture which means nothing else can be on it and it must be a power of 2.

 ![2019-04-13%2009_20_40-Window](https://forum-defold-uploads.b-cdn.net/original/2X/7/7028f139e44f3caf6577a30362f0a5fc16cdb537.png)

Then you want a sprite material with wrap mode repeat.

 ![2019-04-13%2009_13_28-Window](https://forum-defold-uploads.b-cdn.net/original/2X/9/92c067481adb3c46414876d109da53e711cf8516.png)

Inside this shader you can have code such as

```lua
varying mediump vec2 var_texcoord0;

uniform lowp sampler2D texture_sampler;
uniform lowp vec4 tint;
uniform lowp vec4 scale;

void main()
{
    // Pre-multiply alpha since all runtime textures already are
    lowp vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
    lowp vec2 uv = vec2(var_texcoord0.x * scale.x, var_texcoord0.y * scale.y);
    gl_FragColor = texture2D(texture_sampler, uv.xy) * tint_pm;
}

```

Then you update the scale constant with a script

```lua
function init(self)
	local scale = go.get_scale()
	sprite.set_constant("#sprite", "scale", vmath.vector4(scale.x, scale.y, 1, 1))
	
end

```

In editor your tiled thing will look wrong, but in engine it should look right

 ![2019-04-13%2009_14_32-Window](https://forum-defold-uploads.b-cdn.net/original/2X/3/36ec22a0d051940ab99e9043397de7852ca996d4.png)

 ![2019-04-13%2009_15_49-Window](https://forum-defold-uploads.b-cdn.net/original/2X/2/274d95d657da1893ad9520e5ce58e867cc155e03.png)

If you want like a looping repeating background which moves in some direction that is possible to add too.

```lua
function init(self)
	local scale = go.get_scale()
	sprite.set_constant("#sprite", "scale", vmath.vector4(scale.x, scale.y, 1, 1))
	self.offset = {x = 0, y = 0}
end

function update(self, dt)
	self.offset.x = self.offset.x + dt
	self.offset.y = self.offset.y + dt / 2

	sprite.set_constant("#sprite", "offset", vmath.vector4(self.offset.x, self.offset.y, 0, 0))
end

```

 ![2019-04-13%2009_19_10-Window](https://forum-defold-uploads.b-cdn.net/original/2X/e/e32cbbf9c95d820b7b67d235dabf12c6c2735963.png)

```lua
varying mediump vec2 var_texcoord0;

uniform lowp sampler2D texture_sampler;
uniform lowp vec4 tint;
uniform lowp vec4 scale;
uniform lowp vec4 offset;

void main()
{
    // Pre-multiply alpha since all runtime textures already are
    lowp vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
    lowp vec2 uv = vec2(var_texcoord0.x * scale.x + offset.x, var_texcoord0.y * scale.y + offset.y);
    gl_FragColor = texture2D(texture_sampler, uv.xy) * tint_pm;
}

```

If you’ve seen pixel art games with repeating background patterns this can be a way to accomplish that. You can have multiple layers with parallax pretty easily too if your textures have transparency. When multi texturing comes it will be easier probably.

[https://forum-defold-uploads.b-cdn.net/original/2X/9/90f3dc2263b31138e835a6a2ed8473aac8c77db4.mp4](https://forum-defold-uploads.b-cdn.net/original/2X/9/90f3dc2263b31138e835a6a2ed8473aac8c77db4.mp4)

---

<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:** [June 24, 2019, 3:24pm UTC](https://forum.defold.com/t/repeating-texture/49201/3 "2019-06-24T15:24:02Z")

</div>

Great writeup. Would this approach work with flip book animations?

---

<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:** [June 24, 2019, 4:25pm UTC](https://forum.defold.com/t/repeating-texture/49201/4 "2019-06-24T16:25:59Z")

</div>

Assuming we get relative UV spaces sometime in the future that will be useful for using this method with animations. You could probably change the texture manually to animate it.

---

<div class="post-metadata">

**Author:** ![gianmichele](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/gianmichele/32/24028_2.png) [@gianmichele](https://forum.defold.com/u/gianmichele)\
**Post date:** [March 7, 2020, 11:53am UTC](https://forum.defold.com/t/repeating-texture/49201/5 "2020-03-07T11:53:16Z")

</div>

Nice one!

Is there any way to not have to use one atlas per texture that we want to wrap?

---

<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:** [March 7, 2020, 9:16pm UTC](https://forum.defold.com/t/repeating-texture/49201/6 "2020-03-07T21:16:19Z")

</div>

We still don’t have local UV space so I don’t see a way to do that well. Maybe there is a way to easily do it though.

---

<div class="post-metadata">

**Author:** ![egorderg](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/egorderg/32/29895_2.png) [@egorderg](https://forum.defold.com/u/egorderg)\
**Post date:** [October 7, 2022, 10:41pm UTC](https://forum.defold.com/t/repeating-texture/49201/7 "2022-10-07T22:41:43Z")

</div>

I tried the example above but i’ve got only a stretched texture. Is something outdated? I’am using the latest stable version of the editor and dmengine.

---

<div class="post-metadata">

**Author:** ![egorderg](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/egorderg/32/29895_2.png) [@egorderg](https://forum.defold.com/u/egorderg)\
**Post date:** [October 7, 2022, 11:05pm UTC](https://forum.defold.com/t/repeating-texture/49201/8 "2022-10-07T23:05:59Z")

</div>

Okay, i found the error. go.set(‘#sprite’, ‘scale’, …) is reserved or something and i couldn’t use it in the fragment program. After i changed it to ‘scale\_repeat’ and updated the fragment it worked.

---

<div class="post-metadata">

**Author:** ![jhonny.goransson](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jhonny.goransson/32/3864_2.png) [@jhonny.goransson](https://forum.defold.com/u/jhonny.goransson)\
**Post date:** [October 8, 2022, 12:23pm UTC](https://forum.defold.com/t/repeating-texture/49201/9 "2022-10-08T12:23:24Z")

</div>

> [@Pkeod](#):
>
> We still don’t have local UV space so I don’t see a way to do that well. Maybe there is a way to easily do it though.

What do you mean? Do you have a ticket for this? Repeating textures should be doable with atlases, but I haven’t looked at it

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [October 8, 2022, 2:31pm UTC](https://forum.defold.com/t/repeating-texture/49201/10 "2022-10-08T14:31:48Z")

</div>

They’re talking about repeating the sprite images within the atlas. They don’t have access to the ”local” uv coord, which might be another thing we might want to add in a future vertex format update.

---

<div class="post-metadata">

**Author:** ![jhonny.goransson](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jhonny.goransson/32/3864_2.png) [@jhonny.goransson](https://forum.defold.com/u/jhonny.goransson)\
**Post date:** [October 8, 2022, 3:12pm UTC](https://forum.defold.com/t/repeating-texture/49201/11 "2022-10-08T15:12:56Z")

</div>

Ah right, I thought we had that but I’m probably thinking of labels “recip” uniform. But yeah, sounds like a thing we might want. Haven’t seen a ticket about it though

---

<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:** [October 9, 2022, 1:44am UTC](https://forum.defold.com/t/repeating-texture/49201/12 "2022-10-09T01:44:19Z")

</div>

[https://github.com/defold/defold/issues/7045](https://github.com/defold/defold/issues/7045)

Added one, maybe it was an issue in the older git that never got migrated.

---

<div class="post-metadata">

**Author:** ![AlterTinkerer](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/altertinkerer/32/33477_2.png) [@AlterTinkerer](https://forum.defold.com/u/AlterTinkerer)\
**Post date:** [April 10, 2024, 8:31pm UTC](https://forum.defold.com/t/repeating-texture/49201/14 "2024-04-10T20:31:37Z")

</div>

I’m trying to reproduce the example, but also getting stretched texture. Any chances you could post a sample project, please ?

---

<div class="post-metadata">

**Author:** ![Dragosha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dragosha/32/5103_2.png) [@Dragosha](https://forum.defold.com/u/Dragosha)\
**Post date:** [April 11, 2024, 5:21am UTC](https://forum.defold.com/t/repeating-texture/49201/15 "2024-04-11T05:21:11Z")

</div>

> [@AlterTinkerer](#):
>
> sample project

Try this one: [Tiling (repeat) texture shader for a sprite and GUI box node](https://forum.defold.com/t/tiling-repeat-sprite-texture-shader/76424)

---

<div class="post-metadata">

**Author:** ![AlterTinkerer](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/altertinkerer/32/33477_2.png) [@AlterTinkerer](https://forum.defold.com/u/AlterTinkerer)\
**Post date:** [April 11, 2024, 6:45pm UTC](https://forum.defold.com/t/repeating-texture/49201/16 "2024-04-11T18:45:05Z")

</div>

Thank you, @Dragosha , I actually found it while looking for a solution. It looks great, and I consider using it, but I also would like to figure out why what I did didn’t work.
