It's not a simple task to duplicate a texture, and success is not guaranteed.
To duplicate a texture, you must first render it to another texture. This involves setting up a shader with attributes and uniforms to draw the source texture, attaching the destination texture to a framebuffer, binding the framebuffer, and then drawing.
Another method is using copyTexImage2D. This function copies from either the current framebuffer or canvas if no framebuffer is bound. In this case, you would attach your source texture to a framebuffer, bind the framebuffer, bind your destination texture, and call copyTexImage2D
.
There are limitations to both of these methods:
Not every texture format can be attached to a framebuffer. For example, in WebGL1, only a texture with format/type RGBA/UNSIGNED_BYTE can be guaranteed to be attached to a framebuffer.
You cannot query the size of a texture in WebGL1, so you will need to keep track of that information yourself.
In WebGL1, you cannot query the internal format, format, or type of a texture. You would need to save this information. In WebGL2, each mip level can have different sizes and internal formats, so saving dimensions and internal format for each mip level would be necessary for generic copying.
You cannot easily copy the mip levels even if you know how many there are.
Method 1 won't work for copying mips because you can't specify a specific mip as the destination. Method 2 requires rendering the mip level to the canvas or another texture before calling copyTexImage2D.
If you're not concerned about the contents of the mips, you can simply copy level 0 and call generateMipmap
.
You cannot copy a texture if it is not in a renderable state.
For instance, in WebGL1, a non-power-of-2 texture with filtering set to use mips or repeat, or if the mips have incorrect sizes or different formats.
You can use gl.getTextureParameter
to retrieve parameters like TEXTURE_MIN_FILTER
and TEXTURE_WRAP_S
from one texture to apply to another, or save them manually as mentioned in issues 2 and 3 above.