# Trying to Use the Alpha Channel When Writing to an ArrayTexture

**URL:** <https://openrndr.discourse.group/t/trying-to-use-the-alpha-channel-when-writing-to-an-arraytexture/477>\
**Category:** How to?\
**Created:** [December 26, 2022, 11:56am UTC](https://openrndr.discourse.group/t/trying-to-use-the-alpha-channel-when-writing-to-an-arraytexture/477 "2022-12-26T11:56:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asmodaios](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/asmodaios/32/455_2.png) [@Asmodaios](https://openrndr.discourse.group/u/Asmodaios)\
**Post date:** [December 26, 2022, 11:56am UTC](https://openrndr.discourse.group/t/trying-to-use-the-alpha-channel-when-writing-to-an-arraytexture/477/1 "2022-12-26T11:56:49Z")

</div>

Hello everyone,

I’m trying to create an ArrayTexture by writing the values to a ByteBuffer as suggested by [the guide](https://guide.openrndr.org/advancedDrawing/arrayTextures.html), but it seems that the alpha channel is only binary, i.e. when I draw the texture as image, it is completely transparent if the value is zero and completely opaque if it’s a positive number. I’m using the defaults for ColorFormat, i.e. RGBa, etc. when creating the ArrayTexture.

Is the problem in the texture or could it be in the way that I draw it? Thanks in advance for any support!

---

<div class="post-metadata">

**Author:** ![Alessandro](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/alessandro/32/412_2.png) [@Alessandro](https://openrndr.discourse.group/u/Alessandro)\
**Post date:** [December 26, 2022, 12:06pm UTC](https://openrndr.discourse.group/t/trying-to-use-the-alpha-channel-when-writing-to-an-arraytexture/477/2 "2022-12-26T12:06:26Z")

</div>

Hello there, and welcome! 🙂  
If possible could you past the part of the code used to fill the ArrayTexture? I can then try to look into it and try to reproduce what is happening.

---

<div class="post-metadata">

**Author:** ![Asmodaios](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/asmodaios/32/455_2.png) [@Asmodaios](https://openrndr.discourse.group/u/Asmodaios)\
**Post date:** [December 26, 2022, 12:15pm UTC](https://openrndr.discourse.group/t/trying-to-use-the-alpha-channel-when-writing-to-an-arraytexture/477/3 "2022-12-26T12:15:00Z")

</div>

Sure, thanks for the fast reply! This is my code for creating the texture:

```auto
        val arrayTexture = arrayTexture(width, height,1)
        val buffer = ByteBuffer.allocateDirect(width * height * 4)

        for (y in 0 until height) {
            for (x in 0 until width) {
                val color = getColor(x, y)
                buffer.put((color.r * 255).toInt().toByte())
                buffer.put((color.g * 255).toInt().toByte())
                buffer.put((color.b * 255).toInt().toByte())
                buffer.put((color.alpha * 255).toInt().toByte())
            }
        }
        buffer.rewind()
        arrayTexture.write(0, buffer)

```

`getColor` returns a ColorRGBa, obviously. Instead of using the color’s alpha channel, I also tried `(y % 256)`, which lead me to the discovery that the alpha channel seems to be interpreted binary.  
For drawing the texture, I just invoke `drawer.image`.

---

<div class="post-metadata">

**Author:** ![Alessandro](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/alessandro/32/412_2.png) [@Alessandro](https://openrndr.discourse.group/u/Alessandro)\
**Post date:** [December 26, 2022, 12:31pm UTC](https://openrndr.discourse.group/t/trying-to-use-the-alpha-channel-when-writing-to-an-arraytexture/477/4 "2022-12-26T12:31:28Z")

</div>

It seems to be an issue with `drawer.image` rather than ArrayTexture: could you substitute that line with the following bit of code and see how it goes?

```kotlin
    drawer.stroke = null
    drawer.shadeStyle = shadeStyle {
                fragmentTransform = """
                    vec2 uv = c_boundsPosition.xy;
                    uv.y = 1.0 - uv.y;
                    vec4 col = texture(p_img, vec3(uv, 0.0));
                    x_fill = col;
                """.trimIndent()
                parameter("img", arrayTexture)
            }

            drawer.rectangle(drawer.bounds)
```

---

<div class="post-metadata">

**Author:** ![Asmodaios](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/asmodaios/32/455_2.png) [@Asmodaios](https://openrndr.discourse.group/u/Asmodaios)\
**Post date:** [December 26, 2022, 12:36pm UTC](https://openrndr.discourse.group/t/trying-to-use-the-alpha-channel-when-writing-to-an-arraytexture/477/5 "2022-12-26T12:36:44Z")

</div>

Yes, that fixed it! Thanks a lot! 🎉

I guess in order to understand the `fragmentTransform` code I need to learn GLSL, right? 😅

---

<div class="post-metadata">

**Author:** ![Alessandro](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/alessandro/32/412_2.png) [@Alessandro](https://openrndr.discourse.group/u/Alessandro)\
**Post date:** [December 26, 2022, 12:39pm UTC](https://openrndr.discourse.group/t/trying-to-use-the-alpha-channel-when-writing-to-an-arraytexture/477/6 "2022-12-26T12:39:24Z")

</div>

> [@Asmodaios](#):
>
> Yes, that fixed it! Thanks a lot! 🎉

You are welcome. 🙂

> [@Asmodaios](#):
>
> I guess in order to understand the `fragmentTransform` code I need to learn GLSL, right? 😅

Kinda, yeah 😅 May I ask why you want to use an ArrayTexture? From the example I can’t appreciate it, but possibly there is some context that I am missing.

---

<div class="post-metadata">

**Author:** ![Asmodaios](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/asmodaios/32/455_2.png) [@Asmodaios](https://openrndr.discourse.group/u/Asmodaios)\
**Post date:** [December 26, 2022, 12:46pm UTC](https://openrndr.discourse.group/t/trying-to-use-the-alpha-channel-when-writing-to-an-arraytexture/477/7 "2022-12-26T12:46:27Z")

</div>

> May I ask why you want to use an ArrayTexture? From the example I can’t appreciate it, but possibly there is some context that I am missing.

Sure! I’m working on a Mastodon bot for procedural image generation (a successor of [this Twitter bot](https://twitter.com/kosmik_art)), and I use textures generated from OpenSimplex noise for various purposes, e.g. cosmic nebulae and moon surfaces. I got the impression that `ArrayTexture` is an appropriate (and fast) way to generate the textures.

---

<div class="post-metadata">

**Author:** ![Alessandro](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/alessandro/32/412_2.png) [@Alessandro](https://openrndr.discourse.group/u/Alessandro)\
**Post date:** [December 26, 2022, 12:55pm UTC](https://openrndr.discourse.group/t/trying-to-use-the-alpha-channel-when-writing-to-an-arraytexture/477/8 "2022-12-26T12:55:20Z")

</div>

> [@Asmodaios](#):
>
> Sure! I’m working on a Mastodon bot for procedural image generation (a successor of [this Twitter bot](https://twitter.com/kosmik_art)), and I use textures generated from OpenSimplex noise for various purposes, e.g. cosmic nebulae and moon surfaces. I got the impression that `ArrayTexture` is an appropriate (and fast) way to generate the textures.

I see, interesting. 🙂  
In my experience, I prefer using ArrayTexture when I need to switch between textures in real-time (video effects, etc.), since that reduces the uploading/downloading to GPU costs. In batched situations, i.e. no real-time needed, though, I usually stick with colorBuffers.

---

<div class="post-metadata">

**Author:** ![Asmodaios](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/asmodaios/32/455_2.png) [@Asmodaios](https://openrndr.discourse.group/u/Asmodaios)\
**Post date:** [December 26, 2022, 12:57pm UTC](https://openrndr.discourse.group/t/trying-to-use-the-alpha-channel-when-writing-to-an-arraytexture/477/9 "2022-12-26T12:57:49Z")

</div>

> [@Alessandro](#):
>
> In batched situations, i.e. no real-time needed, though, I usually stick with colorBuffers.

Good to know! I just started using openrndr on Friday, so I’m pretty new to all the APIs, but I will look into color buffers if those are easier to use or in any other way better suited for what I need.
