Blitting between two textures?

Is there a way to efficiently copy a rectangular subImage of a sourceTexture into a corresponding rectangular area of a destinationTexture?

I have found this thread re. texSubImage2D() support in Three.js:

and also a corresponding example on its use.

But these only cover the “destination end” of the intended copy operation, filling/replacing a rectangular area of the destination texture. A data buffer holding the source pixels still has to be filled using brute force, so to speak. I’m asking for the extraction from an existing source texture, too.

In essence, I’d like to see functionality like this, on a texture-to-texture level:
https://www.khronos.org/opengl/wiki/GLAPI/glBlitFramebuffer

Use case: extracting from an existing sprite sheet and compositing into a destination texture.

The most efficient and fastest way is Canvas API.

The image data are stored as Uint8ClampedArray. The array has a length of width*height*4.
Every four cell represent a pixel [ r, g, b, a ] ranging from 0 to 255.

MDN has a good tutorial on this subject.

1 Like

I have taken a deeper look into this. Since I would like to do the compositing invisibly to the user, the use of an offscreen canvas came to my mind. Unfortunately I encountered this:

And in fact, the current browser support is too thin to further consider that idea at this point in time.

Any other ideas?

I have, btw., preliminarily solved my problem by using a dedicated PlaneMesh() for each sprite component, translating each to its final position and grouping them into a THREE.Group(). It works, but is obviously a little brute force, and will probably not scale well.

The extraction part from the source texture is currently accomplished through application of suitable uv-coordinates, relative to the source texture, and texturing the destination mesh with the source(!) material/texture.

JavaScript doesn’t care about how the element is styled or if it is append to DOM or not.
But, it does care about Object Property Height and Width, since that determines the size of the buffer.

The offscreen canvas was purposed to work with workers to not block the main thread. Mainly used to work with videos or very Hi Res images.

You are just fine using regular canvas.

1 Like