Hello!
From your PG:
But the console is not!
@sebavan allow me to handle others console errors for you! I bet you are maxed out by some other tasks! 
@Khanon first of all you should check your console in case there is something not working for you. That’s the most crucial step in debugging.
Voila!
The first error instantly reveals what issue are we facing here!
Now let’s try to debug ourselves what the heck is wrong:
We get this:

65580 bytes
Let’s count! width * height = 800 * 533 * 4 (RGBA) = 1 705 600 bytes
The buffer is much more smaller than one you tried to use in to create the RawTexture
. That’s why we get the first error telling us:

We can be sure that the loadBuffer
function is buggy. Let’s fetch the binary data in a bit more professional way. Fetch a blob:
async function binaryLoad(url) {
const data = await fetch(url)
const blob = await data.blob()
return buffer
}
We get this and it reveals that your file is a jpeg
not a png
:

fileUrl = 'https://images.rawpixel.com/image_png_800/czNmcy1wcml2YXRlL3Jhd3BpeGVsX2ltYWdlcy93ZWJzaXRlX2NvbnRlbnQvZnJob3JzZV9nYWxsb3BfY2FudGVyX21hcmUtaW1hZ2Utcm01MDMtbDBqOXJmcmgucG5n.png'
← png in filename
We could use this code to fetch the binary data without using a Blob
but as we all know jpeg
and png
is not a RAW fileformat so we won’t use this approach and I’m happy to announce neither the Blob
approach (which you could but you’ll have to use canvas2d
)
async function binaryLoad(url) {
const data = await fetch(url)
const buffer = await data.arrayBuffer()
return buffer
}
So we will simply load the texture directly. Let’s change the fileUrl
to a real png
:
const fileUrl = "https://playgrounds.babylonjs.xyz/flowers.png"
const texture = new BABYLON.Texture(fileUrl)
Let babylon.js do the image decoding and when we are ready we can get the RAW pixel data like this:
scene.onReadyObservable.addOnce(async () => {
const pixelData = await texture.readPixels()
})
Your texture is stored in memory. You can do whatever you want with it.
Be always sure to use the correct file extension for a given image when creating a Texture
.
Answer to your original question is:
const rawData = new Uint8Array([
0, 240, 232,
236, 0, 242,
0, 240, 232,
0, 37, 245,
])
const texture = new BABYLON.RawTexture(
rawData,
rawData.length / 3,
1,
BABYLON.Engine.TEXTUREFORMAT_RGB,
scene,
false,
true,
BABYLON.Engine.TEXTURE_LINEAR_LINEAR
)
texture.wrapU = BABYLON.RawTexture.WRAP_ADDRESSMODE
texture.name = 'color-texture'