CopyTexture
Copies pixel data from one texture to another.
Read time 57 minutesLast updated 5 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
CopyTexture(Texture, Texture)
Copies pixel data from one texture to another.
public static void CopyTexture(Texture src, Texture dst)
Parameters
The source texture to copy from, specified as a Texture or as a GraphicsTexture.
The destination texture to copy to, specified as a Texture or as a GraphicsTexture.
Remarks
This method copies pixel data from one texture to another on the GPU. If Texture.isReadable is for both and , the method also attempts to copy pixel data on the CPU by running a method such as Texture2D.CopyPixels. If Texture.isReadable is for either or , or they are provided as type GraphicsTexture, no pixel data will be copied on the CPU and becomes one of the fastest ways to copy a texture. Warning: Be careful when using an method such as Texture2D.Apply after , because you might copy old or undefined CPU texture data to the GPU.
truesrcdstCopyPixelsfalsesrcdstCopyTextureApplyCopyTextureTo use , the following must be the same in both the source and destination texture areas:
CopyTexture- Format. You can also use two compatible formats - for example, TextureFormat.ARGB32 and RenderTextureFormat.ARGB32.
- Size.
- RenderTexture.antiAliasing or GraphicsTextureDescriptor.numSamples values, if the textures are render targets.
You might be able to copy between incompatible formats depending on your graphics API. For example, on some APIs you can copy between formats with the same bit width.
Depending on your graphics API, you might not be able to copy between different types of textures. For more information on compatibility, see SystemInfo.copyTextureSupport and CopyTextureSupport.
If is a depth-only render target, you must copy the whole texture, not part of it. A depth-only render texture has its color buffer set to a color format of and its depth buffer set to a valid RenderTexture.depthStencilFormat. A depth graphics texture has its format set to a valid GraphicsFormat with a depth component.
srcNoneWhen you use QualitySettings.globalTextureMipmapLimit, Texture2D.mipmapLimitGroup and Texture2D.ignoreMipmapLimit, textures can have a variety of mipmap limit settings. This means you may not be able to copy from one mipmap level to another, because for example the source texture is limited to half resolution and the destination texture is limited to quarter resolution. Note that this does not apply when and are set to graphics textures, because graphics textures already have their mipmap count limited, and only represent the mipmap levels that are currently uploaded.
srcdstIf you need to copy a mip between textures with different mipmap limit settings, use the region-based overload. This overload adjusts the source rectangle based on the source texture's mipmap limit settings, and the destination offset based on the destination's mipmap limit settings. For example, copying a 128x128 area from position 16,16 to position 32,32 results in a variety of behaviors based on the global texture mipmap limit, the source texture type, and the destination texture type:
- When the global texture mipmap limit is 0, and both textures are subject to it, Unity performs all copies as expected.
- When the global texture mipmap limit is 2, and both textures are subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, then changes the destination offset to 8x8. Unity then performs all copies as expected and ignores all mipmap limit settings.
- When the global texture mipmap limit is 2, the source is a texture subject to it, and the destination is a texture not subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, but doesn't change the destination offset, which remains 32x32.
The following sample shows how you can copy to or from a Texture2DArray, when Texture2Ds have mipmap limits:
using UnityEngine;public class CopyTextureSample : MonoBehaviour{ // Sample to copy all available mips: inTex -> outArray (no mipmap limits) -> outTex [SerializeField] Texture2D inTex; [SerializeField] Texture2DArray outArray; [SerializeField] Texture2D outTex; [SerializeField] bool considerMipmapLimits; void Start() { int width = inTex.width; int height = inTex.width; outArray = new Texture2DArray(width, height, 1, inTex.format, true); outTex = new Texture2D(width, height, inTex.format, true); if (!considerMipmapLimits) { // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => copies into mips that are now too large; will copy each mip of inTex into a quarter of outArray for (int mip = 0; mip < inTex.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(inTex, 0, mip, 0, 0, copyWidth, copyHeight, outArray, 0, mip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => errors, since we try to copy into mips that are now too small for (int mip = 0; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(outArray, 0, mip, 0, 0, copyWidth, copyHeight, outTex, 0, mip, 0, 0); } } else // considering mipmap limits { int globalMipmapLimit = QualitySettings.globalTextureMipmapLimit; // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not written to, other mips copy as expected // (ALTERNATIVE: if outArray creation already considered globalMipmapLimit for its dimensions, // the CopyTexture call can ignore globalMipmapLimit since the mips will line up again) for (int mip = 0; mip < inTex.mipmapCount - globalMipmapLimit; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip + globalMipmapLimit; Graphics.CopyTexture(inTex, 0, srcMip, 0, 0, copyWidth, copyHeight, outArray, 0, dstMip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not copied (but outTex does not upload it to GPU anyway) for (int mip = globalMipmapLimit; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip - globalMipmapLimit; Graphics.CopyTexture(outArray, 0, srcMip, 0, 0, copyWidth, copyHeight, outTex, 0, dstMip, 0, 0); } } }}
Mipmap level arguments always refer to a texture's currently loaded mipmap levels (which can be found via GraphicsTextureDescriptor.mipCount for that texture's Texture.graphicsTexture). For example, if the global texture mipmap limit is set to 0, mip 1 of a 256x256 texture refers to a 128x128 mip. However, for that same texture, if the global texture mipmap limit is set to 1, mip 1 refers to a 64x64 mip. If the global texture mipmap limit is set to 2, mip 1 of that same texture would refer to a 32x32 mip (and so on).
This means that when you use , you do not need to take mipmap limit settings into account in your calls in most cases. However, if you use Textures and the source and destination's mipmap limit settings differ, you need to adjust for those settings. Keep in mind that non-2D texture types are always uploaded to the GPU at full resolution, because they do not support mipmap limit settings of any sort.
CopyTextureCompressed texture formats add some restrictions to with a region variant. For example, PVRTC formats are not supported since they are not block-based (for these formats you can only copy whole texture or whole mip level). For block-based formats (e.g. DXT, BCn, ETC), the region size and coordinates must be a multiple of compression block size (4 pixels for DXT).
CopyTextureNote: PVRTC format is deprecated. Use ASTC or ETC format instead.
Even if Texture.isReadable is , doesn't copy pixel data on the CPU if you copy only a region of a compressed texture.
trueCopyTextureAdditional Resources: CopyTextureSupport, Texture2D.CopyPixels.
CopyTexture(Texture, int, Texture, int)
Copies pixel data from one texture to another.
public static void CopyTexture(Texture src, int srcElement, Texture dst, int dstElement)
Parameters
The source texture to copy from, specified as a Texture or as a GraphicsTexture.
The element in the source texture to copy from. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to if is a 2D texture.
0srcThe destination texture to copy to, specified as a Texture or as a GraphicsTexture.
The element in the destination texture to copy to. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to if is a 2D texture.
0dstRemarks
This method copies pixel data from one texture to another on the GPU. If Texture.isReadable is for both and , the method also attempts to copy pixel data on the CPU by running a method such as Texture2D.CopyPixels. If Texture.isReadable is for either or , or they are provided as type GraphicsTexture, no pixel data will be copied on the CPU and becomes one of the fastest ways to copy a texture. Warning: Be careful when using an method such as Texture2D.Apply after , because you might copy old or undefined CPU texture data to the GPU.
truesrcdstCopyPixelsfalsesrcdstCopyTextureApplyCopyTextureTo use , the following must be the same in both the source and destination texture areas:
CopyTexture- Format. You can also use two compatible formats - for example, TextureFormat.ARGB32 and RenderTextureFormat.ARGB32.
- Size.
- RenderTexture.antiAliasing or GraphicsTextureDescriptor.numSamples values, if the textures are render targets.
You might be able to copy between incompatible formats depending on your graphics API. For example, on some APIs you can copy between formats with the same bit width.
Depending on your graphics API, you might not be able to copy between different types of textures. For more information on compatibility, see SystemInfo.copyTextureSupport and CopyTextureSupport.
If is a depth-only render target, you must copy the whole texture, not part of it. A depth-only render texture has its color buffer set to a color format of and its depth buffer set to a valid RenderTexture.depthStencilFormat. A depth graphics texture has its format set to a valid GraphicsFormat with a depth component.
srcNoneWhen you use QualitySettings.globalTextureMipmapLimit, Texture2D.mipmapLimitGroup and Texture2D.ignoreMipmapLimit, textures can have a variety of mipmap limit settings. This means you may not be able to copy from one mipmap level to another, because for example the source texture is limited to half resolution and the destination texture is limited to quarter resolution. Note that this does not apply when and are set to graphics textures, because graphics textures already have their mipmap count limited, and only represent the mipmap levels that are currently uploaded.
srcdstIf you need to copy a mip between textures with different mipmap limit settings, use the region-based overload. This overload adjusts the source rectangle based on the source texture's mipmap limit settings, and the destination offset based on the destination's mipmap limit settings. For example, copying a 128x128 area from position 16,16 to position 32,32 results in a variety of behaviors based on the global texture mipmap limit, the source texture type, and the destination texture type:
- When the global texture mipmap limit is 0, and both textures are subject to it, Unity performs all copies as expected.
- When the global texture mipmap limit is 2, and both textures are subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, then changes the destination offset to 8x8. Unity then performs all copies as expected and ignores all mipmap limit settings.
- When the global texture mipmap limit is 2, the source is a texture subject to it, and the destination is a texture not subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, but doesn't change the destination offset, which remains 32x32.
The following sample shows how you can copy to or from a Texture2DArray, when Texture2Ds have mipmap limits:
using UnityEngine;public class CopyTextureSample : MonoBehaviour{ // Sample to copy all available mips: inTex -> outArray (no mipmap limits) -> outTex [SerializeField] Texture2D inTex; [SerializeField] Texture2DArray outArray; [SerializeField] Texture2D outTex; [SerializeField] bool considerMipmapLimits; void Start() { int width = inTex.width; int height = inTex.width; outArray = new Texture2DArray(width, height, 1, inTex.format, true); outTex = new Texture2D(width, height, inTex.format, true); if (!considerMipmapLimits) { // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => copies into mips that are now too large; will copy each mip of inTex into a quarter of outArray for (int mip = 0; mip < inTex.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(inTex, 0, mip, 0, 0, copyWidth, copyHeight, outArray, 0, mip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => errors, since we try to copy into mips that are now too small for (int mip = 0; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(outArray, 0, mip, 0, 0, copyWidth, copyHeight, outTex, 0, mip, 0, 0); } } else // considering mipmap limits { int globalMipmapLimit = QualitySettings.globalTextureMipmapLimit; // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not written to, other mips copy as expected // (ALTERNATIVE: if outArray creation already considered globalMipmapLimit for its dimensions, // the CopyTexture call can ignore globalMipmapLimit since the mips will line up again) for (int mip = 0; mip < inTex.mipmapCount - globalMipmapLimit; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip + globalMipmapLimit; Graphics.CopyTexture(inTex, 0, srcMip, 0, 0, copyWidth, copyHeight, outArray, 0, dstMip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not copied (but outTex does not upload it to GPU anyway) for (int mip = globalMipmapLimit; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip - globalMipmapLimit; Graphics.CopyTexture(outArray, 0, srcMip, 0, 0, copyWidth, copyHeight, outTex, 0, dstMip, 0, 0); } } }}
Mipmap level arguments always refer to a texture's currently loaded mipmap levels (which can be found via GraphicsTextureDescriptor.mipCount for that texture's Texture.graphicsTexture). For example, if the global texture mipmap limit is set to 0, mip 1 of a 256x256 texture refers to a 128x128 mip. However, for that same texture, if the global texture mipmap limit is set to 1, mip 1 refers to a 64x64 mip. If the global texture mipmap limit is set to 2, mip 1 of that same texture would refer to a 32x32 mip (and so on).
This means that when you use , you do not need to take mipmap limit settings into account in your calls in most cases. However, if you use Textures and the source and destination's mipmap limit settings differ, you need to adjust for those settings. Keep in mind that non-2D texture types are always uploaded to the GPU at full resolution, because they do not support mipmap limit settings of any sort.
CopyTextureCompressed texture formats add some restrictions to with a region variant. For example, PVRTC formats are not supported since they are not block-based (for these formats you can only copy whole texture or whole mip level). For block-based formats (e.g. DXT, BCn, ETC), the region size and coordinates must be a multiple of compression block size (4 pixels for DXT).
CopyTextureNote: PVRTC format is deprecated. Use ASTC or ETC format instead.
Even if Texture.isReadable is , doesn't copy pixel data on the CPU if you copy only a region of a compressed texture.
trueCopyTextureAdditional Resources: CopyTextureSupport, Texture2D.CopyPixels.
CopyTexture(Texture, int, int, Texture, int, int)
Copies pixel data from one texture to another.
public static void CopyTexture(Texture src, int srcElement, int srcMip, Texture dst, int dstElement, int dstMip)
Parameters
The source texture to copy from, specified as a Texture or as a GraphicsTexture.
The element in the source texture to copy from. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to if is a 2D texture.
0srcThe mipmap level to copy from. The range is through the texture's Texture.mipmapCount or GraphicsTextureDescriptor.mipCount. The default value is .
00The destination texture to copy to, specified as a Texture or as a GraphicsTexture.
The element in the destination texture to copy to. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to if is a 2D texture.
0dstThe mipmap level to write to. The range is through the texture's Texture.mipmapCount or GraphicsTextureDescriptor.mipCount. The default value is .
00Remarks
This method copies pixel data from one texture to another on the GPU. If Texture.isReadable is for both and , the method also attempts to copy pixel data on the CPU by running a method such as Texture2D.CopyPixels. If Texture.isReadable is for either or , or they are provided as type GraphicsTexture, no pixel data will be copied on the CPU and becomes one of the fastest ways to copy a texture. Warning: Be careful when using an method such as Texture2D.Apply after , because you might copy old or undefined CPU texture data to the GPU.
truesrcdstCopyPixelsfalsesrcdstCopyTextureApplyCopyTextureTo use , the following must be the same in both the source and destination texture areas:
CopyTexture- Format. You can also use two compatible formats - for example, TextureFormat.ARGB32 and RenderTextureFormat.ARGB32.
- Size.
- RenderTexture.antiAliasing or GraphicsTextureDescriptor.numSamples values, if the textures are render targets.
You might be able to copy between incompatible formats depending on your graphics API. For example, on some APIs you can copy between formats with the same bit width.
Depending on your graphics API, you might not be able to copy between different types of textures. For more information on compatibility, see SystemInfo.copyTextureSupport and CopyTextureSupport.
If is a depth-only render target, you must copy the whole texture, not part of it. A depth-only render texture has its color buffer set to a color format of and its depth buffer set to a valid RenderTexture.depthStencilFormat. A depth graphics texture has its format set to a valid GraphicsFormat with a depth component.
srcNoneWhen you use QualitySettings.globalTextureMipmapLimit, Texture2D.mipmapLimitGroup and Texture2D.ignoreMipmapLimit, textures can have a variety of mipmap limit settings. This means you may not be able to copy from one mipmap level to another, because for example the source texture is limited to half resolution and the destination texture is limited to quarter resolution. Note that this does not apply when and are set to graphics textures, because graphics textures already have their mipmap count limited, and only represent the mipmap levels that are currently uploaded.
srcdstIf you need to copy a mip between textures with different mipmap limit settings, use the region-based overload. This overload adjusts the source rectangle based on the source texture's mipmap limit settings, and the destination offset based on the destination's mipmap limit settings. For example, copying a 128x128 area from position 16,16 to position 32,32 results in a variety of behaviors based on the global texture mipmap limit, the source texture type, and the destination texture type:
- When the global texture mipmap limit is 0, and both textures are subject to it, Unity performs all copies as expected.
- When the global texture mipmap limit is 2, and both textures are subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, then changes the destination offset to 8x8. Unity then performs all copies as expected and ignores all mipmap limit settings.
- When the global texture mipmap limit is 2, the source is a texture subject to it, and the destination is a texture not subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, but doesn't change the destination offset, which remains 32x32.
The following sample shows how you can copy to or from a Texture2DArray, when Texture2Ds have mipmap limits:
using UnityEngine;public class CopyTextureSample : MonoBehaviour{ // Sample to copy all available mips: inTex -> outArray (no mipmap limits) -> outTex [SerializeField] Texture2D inTex; [SerializeField] Texture2DArray outArray; [SerializeField] Texture2D outTex; [SerializeField] bool considerMipmapLimits; void Start() { int width = inTex.width; int height = inTex.width; outArray = new Texture2DArray(width, height, 1, inTex.format, true); outTex = new Texture2D(width, height, inTex.format, true); if (!considerMipmapLimits) { // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => copies into mips that are now too large; will copy each mip of inTex into a quarter of outArray for (int mip = 0; mip < inTex.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(inTex, 0, mip, 0, 0, copyWidth, copyHeight, outArray, 0, mip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => errors, since we try to copy into mips that are now too small for (int mip = 0; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(outArray, 0, mip, 0, 0, copyWidth, copyHeight, outTex, 0, mip, 0, 0); } } else // considering mipmap limits { int globalMipmapLimit = QualitySettings.globalTextureMipmapLimit; // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not written to, other mips copy as expected // (ALTERNATIVE: if outArray creation already considered globalMipmapLimit for its dimensions, // the CopyTexture call can ignore globalMipmapLimit since the mips will line up again) for (int mip = 0; mip < inTex.mipmapCount - globalMipmapLimit; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip + globalMipmapLimit; Graphics.CopyTexture(inTex, 0, srcMip, 0, 0, copyWidth, copyHeight, outArray, 0, dstMip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not copied (but outTex does not upload it to GPU anyway) for (int mip = globalMipmapLimit; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip - globalMipmapLimit; Graphics.CopyTexture(outArray, 0, srcMip, 0, 0, copyWidth, copyHeight, outTex, 0, dstMip, 0, 0); } } }}
Mipmap level arguments always refer to a texture's currently loaded mipmap levels (which can be found via GraphicsTextureDescriptor.mipCount for that texture's Texture.graphicsTexture). For example, if the global texture mipmap limit is set to 0, mip 1 of a 256x256 texture refers to a 128x128 mip. However, for that same texture, if the global texture mipmap limit is set to 1, mip 1 refers to a 64x64 mip. If the global texture mipmap limit is set to 2, mip 1 of that same texture would refer to a 32x32 mip (and so on).
This means that when you use , you do not need to take mipmap limit settings into account in your calls in most cases. However, if you use Textures and the source and destination's mipmap limit settings differ, you need to adjust for those settings. Keep in mind that non-2D texture types are always uploaded to the GPU at full resolution, because they do not support mipmap limit settings of any sort.
CopyTextureCompressed texture formats add some restrictions to with a region variant. For example, PVRTC formats are not supported since they are not block-based (for these formats you can only copy whole texture or whole mip level). For block-based formats (e.g. DXT, BCn, ETC), the region size and coordinates must be a multiple of compression block size (4 pixels for DXT).
CopyTextureNote: PVRTC format is deprecated. Use ASTC or ETC format instead.
Even if Texture.isReadable is , doesn't copy pixel data on the CPU if you copy only a region of a compressed texture.
trueCopyTextureAdditional Resources: CopyTextureSupport, Texture2D.CopyPixels.
CopyTexture(Texture, int, int, int, int, int, int, Texture, int, int, int, int)
Copies pixel data from one texture to another.
public static void CopyTexture(Texture src, int srcElement, int srcMip, int srcX, int srcY, int srcWidth, int srcHeight, Texture dst, int dstElement, int dstMip, int dstX, int dstY)
Parameters
The source texture to copy from, specified as a Texture or as a GraphicsTexture.
The element in the source texture to copy from. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to if is a 2D texture.
0srcThe mipmap level to copy from. The range is through the texture's Texture.mipmapCount or GraphicsTextureDescriptor.mipCount. The default value is .
00The starting x coordinate of to copy a region from. Accepted values range from to the width of at the specified . is the left of the texture.
src0srcsrcMip0The starting y coordinate of to copy a region from. Accepted values range from to the height of at the specified . is the bottom of the texture.
src0srcsrcMip0The width of the region to copy. Accepted values are greater than or equal to and ensure that the region fits within at the specified considering offset , and within at the specified considering offset .
0srcsrcMip(srcX, srcY)dstdstMip(dstX, dstY)The height of the region to copy. Accepted values are greater than or equal to and ensure that the region fits within at the specified considering offset , and within at the specified considering offset .
0srcsrcMip(srcX, srcY)dstdstMip(dstX, dstY)The destination texture to copy to, specified as a Texture or as a GraphicsTexture.
The element in the destination texture to copy to. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to if is a 2D texture.
0dstThe mipmap level to write to. The range is through the texture's Texture.mipmapCount or GraphicsTextureDescriptor.mipCount. The default value is .
00The starting x coordinate of to copy a region to. Accepted values range from to the width of at the specified . is the left of the texture.
dst0dstdstMip0The starting y coordinate of to copy a region to. Accepted values range from to the height of at the specified . is the bottom of the texture.
dst0dstdstMip0Remarks
This method copies pixel data from one texture to another on the GPU. If Texture.isReadable is for both and , the method also attempts to copy pixel data on the CPU by running a method such as Texture2D.CopyPixels. If Texture.isReadable is for either or , or they are provided as type GraphicsTexture, no pixel data will be copied on the CPU and becomes one of the fastest ways to copy a texture. Warning: Be careful when using an method such as Texture2D.Apply after , because you might copy old or undefined CPU texture data to the GPU.
truesrcdstCopyPixelsfalsesrcdstCopyTextureApplyCopyTextureTo use , the following must be the same in both the source and destination texture areas:
CopyTexture- Format. You can also use two compatible formats - for example, TextureFormat.ARGB32 and RenderTextureFormat.ARGB32.
- Size.
- RenderTexture.antiAliasing or GraphicsTextureDescriptor.numSamples values, if the textures are render targets.
You might be able to copy between incompatible formats depending on your graphics API. For example, on some APIs you can copy between formats with the same bit width.
Depending on your graphics API, you might not be able to copy between different types of textures. For more information on compatibility, see SystemInfo.copyTextureSupport and CopyTextureSupport.
If is a depth-only render target, you must copy the whole texture, not part of it. A depth-only render texture has its color buffer set to a color format of and its depth buffer set to a valid RenderTexture.depthStencilFormat. A depth graphics texture has its format set to a valid GraphicsFormat with a depth component.
srcNoneWhen you use QualitySettings.globalTextureMipmapLimit, Texture2D.mipmapLimitGroup and Texture2D.ignoreMipmapLimit, textures can have a variety of mipmap limit settings. This means you may not be able to copy from one mipmap level to another, because for example the source texture is limited to half resolution and the destination texture is limited to quarter resolution. Note that this does not apply when and are set to graphics textures, because graphics textures already have their mipmap count limited, and only represent the mipmap levels that are currently uploaded.
srcdstIf you need to copy a mip between textures with different mipmap limit settings, use the region-based overload. This overload adjusts the source rectangle based on the source texture's mipmap limit settings, and the destination offset based on the destination's mipmap limit settings. For example, copying a 128x128 area from position 16,16 to position 32,32 results in a variety of behaviors based on the global texture mipmap limit, the source texture type, and the destination texture type:
- When the global texture mipmap limit is 0, and both textures are subject to it, Unity performs all copies as expected.
- When the global texture mipmap limit is 2, and both textures are subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, then changes the destination offset to 8x8. Unity then performs all copies as expected and ignores all mipmap limit settings.
- When the global texture mipmap limit is 2, the source is a texture subject to it, and the destination is a texture not subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, but doesn't change the destination offset, which remains 32x32.
The following sample shows how you can copy to or from a Texture2DArray, when Texture2Ds have mipmap limits:
using UnityEngine;public class CopyTextureSample : MonoBehaviour{ // Sample to copy all available mips: inTex -> outArray (no mipmap limits) -> outTex [SerializeField] Texture2D inTex; [SerializeField] Texture2DArray outArray; [SerializeField] Texture2D outTex; [SerializeField] bool considerMipmapLimits; void Start() { int width = inTex.width; int height = inTex.width; outArray = new Texture2DArray(width, height, 1, inTex.format, true); outTex = new Texture2D(width, height, inTex.format, true); if (!considerMipmapLimits) { // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => copies into mips that are now too large; will copy each mip of inTex into a quarter of outArray for (int mip = 0; mip < inTex.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(inTex, 0, mip, 0, 0, copyWidth, copyHeight, outArray, 0, mip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => errors, since we try to copy into mips that are now too small for (int mip = 0; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(outArray, 0, mip, 0, 0, copyWidth, copyHeight, outTex, 0, mip, 0, 0); } } else // considering mipmap limits { int globalMipmapLimit = QualitySettings.globalTextureMipmapLimit; // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not written to, other mips copy as expected // (ALTERNATIVE: if outArray creation already considered globalMipmapLimit for its dimensions, // the CopyTexture call can ignore globalMipmapLimit since the mips will line up again) for (int mip = 0; mip < inTex.mipmapCount - globalMipmapLimit; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip + globalMipmapLimit; Graphics.CopyTexture(inTex, 0, srcMip, 0, 0, copyWidth, copyHeight, outArray, 0, dstMip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not copied (but outTex does not upload it to GPU anyway) for (int mip = globalMipmapLimit; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip - globalMipmapLimit; Graphics.CopyTexture(outArray, 0, srcMip, 0, 0, copyWidth, copyHeight, outTex, 0, dstMip, 0, 0); } } }}
Mipmap level arguments always refer to a texture's currently loaded mipmap levels (which can be found via GraphicsTextureDescriptor.mipCount for that texture's Texture.graphicsTexture). For example, if the global texture mipmap limit is set to 0, mip 1 of a 256x256 texture refers to a 128x128 mip. However, for that same texture, if the global texture mipmap limit is set to 1, mip 1 refers to a 64x64 mip. If the global texture mipmap limit is set to 2, mip 1 of that same texture would refer to a 32x32 mip (and so on).
This means that when you use , you do not need to take mipmap limit settings into account in your calls in most cases. However, if you use Textures and the source and destination's mipmap limit settings differ, you need to adjust for those settings. Keep in mind that non-2D texture types are always uploaded to the GPU at full resolution, because they do not support mipmap limit settings of any sort.
CopyTextureCompressed texture formats add some restrictions to with a region variant. For example, PVRTC formats are not supported since they are not block-based (for these formats you can only copy whole texture or whole mip level). For block-based formats (e.g. DXT, BCn, ETC), the region size and coordinates must be a multiple of compression block size (4 pixels for DXT).
CopyTextureNote: PVRTC format is deprecated. Use ASTC or ETC format instead.
Even if Texture.isReadable is , doesn't copy pixel data on the CPU if you copy only a region of a compressed texture.
trueCopyTextureAdditional Resources: CopyTextureSupport, Texture2D.CopyPixels.
CopyTexture(GraphicsTexture, GraphicsTexture)
Copies pixel data from one texture to another.
public static void CopyTexture(GraphicsTexture src, GraphicsTexture dst)
Parameters
The source texture to copy from, specified as a Texture or as a GraphicsTexture.
The destination texture to copy to, specified as a Texture or as a GraphicsTexture.
Remarks
This method copies pixel data from one texture to another on the GPU. If Texture.isReadable is for both and , the method also attempts to copy pixel data on the CPU by running a method such as Texture2D.CopyPixels. If Texture.isReadable is for either or , or they are provided as type GraphicsTexture, no pixel data will be copied on the CPU and becomes one of the fastest ways to copy a texture. Warning: Be careful when using an method such as Texture2D.Apply after , because you might copy old or undefined CPU texture data to the GPU.
truesrcdstCopyPixelsfalsesrcdstCopyTextureApplyCopyTextureTo use , the following must be the same in both the source and destination texture areas:
CopyTexture- Format. You can also use two compatible formats - for example, TextureFormat.ARGB32 and RenderTextureFormat.ARGB32.
- Size.
- RenderTexture.antiAliasing or GraphicsTextureDescriptor.numSamples values, if the textures are render targets.
You might be able to copy between incompatible formats depending on your graphics API. For example, on some APIs you can copy between formats with the same bit width.
Depending on your graphics API, you might not be able to copy between different types of textures. For more information on compatibility, see SystemInfo.copyTextureSupport and CopyTextureSupport.
If is a depth-only render target, you must copy the whole texture, not part of it. A depth-only render texture has its color buffer set to a color format of and its depth buffer set to a valid RenderTexture.depthStencilFormat. A depth graphics texture has its format set to a valid GraphicsFormat with a depth component.
srcNoneWhen you use QualitySettings.globalTextureMipmapLimit, Texture2D.mipmapLimitGroup and Texture2D.ignoreMipmapLimit, textures can have a variety of mipmap limit settings. This means you may not be able to copy from one mipmap level to another, because for example the source texture is limited to half resolution and the destination texture is limited to quarter resolution. Note that this does not apply when and are set to graphics textures, because graphics textures already have their mipmap count limited, and only represent the mipmap levels that are currently uploaded.
srcdstIf you need to copy a mip between textures with different mipmap limit settings, use the region-based overload. This overload adjusts the source rectangle based on the source texture's mipmap limit settings, and the destination offset based on the destination's mipmap limit settings. For example, copying a 128x128 area from position 16,16 to position 32,32 results in a variety of behaviors based on the global texture mipmap limit, the source texture type, and the destination texture type:
- When the global texture mipmap limit is 0, and both textures are subject to it, Unity performs all copies as expected.
- When the global texture mipmap limit is 2, and both textures are subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, then changes the destination offset to 8x8. Unity then performs all copies as expected and ignores all mipmap limit settings.
- When the global texture mipmap limit is 2, the source is a texture subject to it, and the destination is a texture not subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, but doesn't change the destination offset, which remains 32x32.
The following sample shows how you can copy to or from a Texture2DArray, when Texture2Ds have mipmap limits:
using UnityEngine;public class CopyTextureSample : MonoBehaviour{ // Sample to copy all available mips: inTex -> outArray (no mipmap limits) -> outTex [SerializeField] Texture2D inTex; [SerializeField] Texture2DArray outArray; [SerializeField] Texture2D outTex; [SerializeField] bool considerMipmapLimits; void Start() { int width = inTex.width; int height = inTex.width; outArray = new Texture2DArray(width, height, 1, inTex.format, true); outTex = new Texture2D(width, height, inTex.format, true); if (!considerMipmapLimits) { // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => copies into mips that are now too large; will copy each mip of inTex into a quarter of outArray for (int mip = 0; mip < inTex.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(inTex, 0, mip, 0, 0, copyWidth, copyHeight, outArray, 0, mip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => errors, since we try to copy into mips that are now too small for (int mip = 0; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(outArray, 0, mip, 0, 0, copyWidth, copyHeight, outTex, 0, mip, 0, 0); } } else // considering mipmap limits { int globalMipmapLimit = QualitySettings.globalTextureMipmapLimit; // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not written to, other mips copy as expected // (ALTERNATIVE: if outArray creation already considered globalMipmapLimit for its dimensions, // the CopyTexture call can ignore globalMipmapLimit since the mips will line up again) for (int mip = 0; mip < inTex.mipmapCount - globalMipmapLimit; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip + globalMipmapLimit; Graphics.CopyTexture(inTex, 0, srcMip, 0, 0, copyWidth, copyHeight, outArray, 0, dstMip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not copied (but outTex does not upload it to GPU anyway) for (int mip = globalMipmapLimit; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip - globalMipmapLimit; Graphics.CopyTexture(outArray, 0, srcMip, 0, 0, copyWidth, copyHeight, outTex, 0, dstMip, 0, 0); } } }}
Mipmap level arguments always refer to a texture's currently loaded mipmap levels (which can be found via GraphicsTextureDescriptor.mipCount for that texture's Texture.graphicsTexture). For example, if the global texture mipmap limit is set to 0, mip 1 of a 256x256 texture refers to a 128x128 mip. However, for that same texture, if the global texture mipmap limit is set to 1, mip 1 refers to a 64x64 mip. If the global texture mipmap limit is set to 2, mip 1 of that same texture would refer to a 32x32 mip (and so on).
This means that when you use , you do not need to take mipmap limit settings into account in your calls in most cases. However, if you use Textures and the source and destination's mipmap limit settings differ, you need to adjust for those settings. Keep in mind that non-2D texture types are always uploaded to the GPU at full resolution, because they do not support mipmap limit settings of any sort.
CopyTextureCompressed texture formats add some restrictions to with a region variant. For example, PVRTC formats are not supported since they are not block-based (for these formats you can only copy whole texture or whole mip level). For block-based formats (e.g. DXT, BCn, ETC), the region size and coordinates must be a multiple of compression block size (4 pixels for DXT).
CopyTextureNote: PVRTC format is deprecated. Use ASTC or ETC format instead.
Even if Texture.isReadable is , doesn't copy pixel data on the CPU if you copy only a region of a compressed texture.
trueCopyTextureAdditional Resources: CopyTextureSupport, Texture2D.CopyPixels.
CopyTexture(GraphicsTexture, int, GraphicsTexture, int)
Copies pixel data from one texture to another.
public static void CopyTexture(GraphicsTexture src, int srcElement, GraphicsTexture dst, int dstElement)
Parameters
The source texture to copy from, specified as a Texture or as a GraphicsTexture.
The element in the source texture to copy from. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to if is a 2D texture.
0srcThe destination texture to copy to, specified as a Texture or as a GraphicsTexture.
The element in the destination texture to copy to. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to if is a 2D texture.
0dstRemarks
This method copies pixel data from one texture to another on the GPU. If Texture.isReadable is for both and , the method also attempts to copy pixel data on the CPU by running a method such as Texture2D.CopyPixels. If Texture.isReadable is for either or , or they are provided as type GraphicsTexture, no pixel data will be copied on the CPU and becomes one of the fastest ways to copy a texture. Warning: Be careful when using an method such as Texture2D.Apply after , because you might copy old or undefined CPU texture data to the GPU.
truesrcdstCopyPixelsfalsesrcdstCopyTextureApplyCopyTextureTo use , the following must be the same in both the source and destination texture areas:
CopyTexture- Format. You can also use two compatible formats - for example, TextureFormat.ARGB32 and RenderTextureFormat.ARGB32.
- Size.
- RenderTexture.antiAliasing or GraphicsTextureDescriptor.numSamples values, if the textures are render targets.
You might be able to copy between incompatible formats depending on your graphics API. For example, on some APIs you can copy between formats with the same bit width.
Depending on your graphics API, you might not be able to copy between different types of textures. For more information on compatibility, see SystemInfo.copyTextureSupport and CopyTextureSupport.
If is a depth-only render target, you must copy the whole texture, not part of it. A depth-only render texture has its color buffer set to a color format of and its depth buffer set to a valid RenderTexture.depthStencilFormat. A depth graphics texture has its format set to a valid GraphicsFormat with a depth component.
srcNoneWhen you use QualitySettings.globalTextureMipmapLimit, Texture2D.mipmapLimitGroup and Texture2D.ignoreMipmapLimit, textures can have a variety of mipmap limit settings. This means you may not be able to copy from one mipmap level to another, because for example the source texture is limited to half resolution and the destination texture is limited to quarter resolution. Note that this does not apply when and are set to graphics textures, because graphics textures already have their mipmap count limited, and only represent the mipmap levels that are currently uploaded.
srcdstIf you need to copy a mip between textures with different mipmap limit settings, use the region-based overload. This overload adjusts the source rectangle based on the source texture's mipmap limit settings, and the destination offset based on the destination's mipmap limit settings. For example, copying a 128x128 area from position 16,16 to position 32,32 results in a variety of behaviors based on the global texture mipmap limit, the source texture type, and the destination texture type:
- When the global texture mipmap limit is 0, and both textures are subject to it, Unity performs all copies as expected.
- When the global texture mipmap limit is 2, and both textures are subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, then changes the destination offset to 8x8. Unity then performs all copies as expected and ignores all mipmap limit settings.
- When the global texture mipmap limit is 2, the source is a texture subject to it, and the destination is a texture not subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, but doesn't change the destination offset, which remains 32x32.
The following sample shows how you can copy to or from a Texture2DArray, when Texture2Ds have mipmap limits:
using UnityEngine;public class CopyTextureSample : MonoBehaviour{ // Sample to copy all available mips: inTex -> outArray (no mipmap limits) -> outTex [SerializeField] Texture2D inTex; [SerializeField] Texture2DArray outArray; [SerializeField] Texture2D outTex; [SerializeField] bool considerMipmapLimits; void Start() { int width = inTex.width; int height = inTex.width; outArray = new Texture2DArray(width, height, 1, inTex.format, true); outTex = new Texture2D(width, height, inTex.format, true); if (!considerMipmapLimits) { // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => copies into mips that are now too large; will copy each mip of inTex into a quarter of outArray for (int mip = 0; mip < inTex.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(inTex, 0, mip, 0, 0, copyWidth, copyHeight, outArray, 0, mip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => errors, since we try to copy into mips that are now too small for (int mip = 0; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(outArray, 0, mip, 0, 0, copyWidth, copyHeight, outTex, 0, mip, 0, 0); } } else // considering mipmap limits { int globalMipmapLimit = QualitySettings.globalTextureMipmapLimit; // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not written to, other mips copy as expected // (ALTERNATIVE: if outArray creation already considered globalMipmapLimit for its dimensions, // the CopyTexture call can ignore globalMipmapLimit since the mips will line up again) for (int mip = 0; mip < inTex.mipmapCount - globalMipmapLimit; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip + globalMipmapLimit; Graphics.CopyTexture(inTex, 0, srcMip, 0, 0, copyWidth, copyHeight, outArray, 0, dstMip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not copied (but outTex does not upload it to GPU anyway) for (int mip = globalMipmapLimit; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip - globalMipmapLimit; Graphics.CopyTexture(outArray, 0, srcMip, 0, 0, copyWidth, copyHeight, outTex, 0, dstMip, 0, 0); } } }}
Mipmap level arguments always refer to a texture's currently loaded mipmap levels (which can be found via GraphicsTextureDescriptor.mipCount for that texture's Texture.graphicsTexture). For example, if the global texture mipmap limit is set to 0, mip 1 of a 256x256 texture refers to a 128x128 mip. However, for that same texture, if the global texture mipmap limit is set to 1, mip 1 refers to a 64x64 mip. If the global texture mipmap limit is set to 2, mip 1 of that same texture would refer to a 32x32 mip (and so on).
This means that when you use , you do not need to take mipmap limit settings into account in your calls in most cases. However, if you use Textures and the source and destination's mipmap limit settings differ, you need to adjust for those settings. Keep in mind that non-2D texture types are always uploaded to the GPU at full resolution, because they do not support mipmap limit settings of any sort.
CopyTextureCompressed texture formats add some restrictions to with a region variant. For example, PVRTC formats are not supported since they are not block-based (for these formats you can only copy whole texture or whole mip level). For block-based formats (e.g. DXT, BCn, ETC), the region size and coordinates must be a multiple of compression block size (4 pixels for DXT).
CopyTextureNote: PVRTC format is deprecated. Use ASTC or ETC format instead.
Even if Texture.isReadable is , doesn't copy pixel data on the CPU if you copy only a region of a compressed texture.
trueCopyTextureAdditional Resources: CopyTextureSupport, Texture2D.CopyPixels.
CopyTexture(GraphicsTexture, int, int, GraphicsTexture, int, int)
Copies pixel data from one texture to another.
public static void CopyTexture(GraphicsTexture src, int srcElement, int srcMip, GraphicsTexture dst, int dstElement, int dstMip)
Parameters
The source texture to copy from, specified as a Texture or as a GraphicsTexture.
The element in the source texture to copy from. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to if is a 2D texture.
0srcThe mipmap level to copy from. The range is through the texture's Texture.mipmapCount or GraphicsTextureDescriptor.mipCount. The default value is .
00The destination texture to copy to, specified as a Texture or as a GraphicsTexture.
The element in the destination texture to copy to. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to if is a 2D texture.
0dstThe mipmap level to write to. The range is through the texture's Texture.mipmapCount or GraphicsTextureDescriptor.mipCount. The default value is .
00Remarks
This method copies pixel data from one texture to another on the GPU. If Texture.isReadable is for both and , the method also attempts to copy pixel data on the CPU by running a method such as Texture2D.CopyPixels. If Texture.isReadable is for either or , or they are provided as type GraphicsTexture, no pixel data will be copied on the CPU and becomes one of the fastest ways to copy a texture. Warning: Be careful when using an method such as Texture2D.Apply after , because you might copy old or undefined CPU texture data to the GPU.
truesrcdstCopyPixelsfalsesrcdstCopyTextureApplyCopyTextureTo use , the following must be the same in both the source and destination texture areas:
CopyTexture- Format. You can also use two compatible formats - for example, TextureFormat.ARGB32 and RenderTextureFormat.ARGB32.
- Size.
- RenderTexture.antiAliasing or GraphicsTextureDescriptor.numSamples values, if the textures are render targets.
You might be able to copy between incompatible formats depending on your graphics API. For example, on some APIs you can copy between formats with the same bit width.
Depending on your graphics API, you might not be able to copy between different types of textures. For more information on compatibility, see SystemInfo.copyTextureSupport and CopyTextureSupport.
If is a depth-only render target, you must copy the whole texture, not part of it. A depth-only render texture has its color buffer set to a color format of and its depth buffer set to a valid RenderTexture.depthStencilFormat. A depth graphics texture has its format set to a valid GraphicsFormat with a depth component.
srcNoneWhen you use QualitySettings.globalTextureMipmapLimit, Texture2D.mipmapLimitGroup and Texture2D.ignoreMipmapLimit, textures can have a variety of mipmap limit settings. This means you may not be able to copy from one mipmap level to another, because for example the source texture is limited to half resolution and the destination texture is limited to quarter resolution. Note that this does not apply when and are set to graphics textures, because graphics textures already have their mipmap count limited, and only represent the mipmap levels that are currently uploaded.
srcdstIf you need to copy a mip between textures with different mipmap limit settings, use the region-based overload. This overload adjusts the source rectangle based on the source texture's mipmap limit settings, and the destination offset based on the destination's mipmap limit settings. For example, copying a 128x128 area from position 16,16 to position 32,32 results in a variety of behaviors based on the global texture mipmap limit, the source texture type, and the destination texture type:
- When the global texture mipmap limit is 0, and both textures are subject to it, Unity performs all copies as expected.
- When the global texture mipmap limit is 2, and both textures are subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, then changes the destination offset to 8x8. Unity then performs all copies as expected and ignores all mipmap limit settings.
- When the global texture mipmap limit is 2, the source is a texture subject to it, and the destination is a texture not subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, but doesn't change the destination offset, which remains 32x32.
The following sample shows how you can copy to or from a Texture2DArray, when Texture2Ds have mipmap limits:
using UnityEngine;public class CopyTextureSample : MonoBehaviour{ // Sample to copy all available mips: inTex -> outArray (no mipmap limits) -> outTex [SerializeField] Texture2D inTex; [SerializeField] Texture2DArray outArray; [SerializeField] Texture2D outTex; [SerializeField] bool considerMipmapLimits; void Start() { int width = inTex.width; int height = inTex.width; outArray = new Texture2DArray(width, height, 1, inTex.format, true); outTex = new Texture2D(width, height, inTex.format, true); if (!considerMipmapLimits) { // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => copies into mips that are now too large; will copy each mip of inTex into a quarter of outArray for (int mip = 0; mip < inTex.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(inTex, 0, mip, 0, 0, copyWidth, copyHeight, outArray, 0, mip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => errors, since we try to copy into mips that are now too small for (int mip = 0; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(outArray, 0, mip, 0, 0, copyWidth, copyHeight, outTex, 0, mip, 0, 0); } } else // considering mipmap limits { int globalMipmapLimit = QualitySettings.globalTextureMipmapLimit; // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not written to, other mips copy as expected // (ALTERNATIVE: if outArray creation already considered globalMipmapLimit for its dimensions, // the CopyTexture call can ignore globalMipmapLimit since the mips will line up again) for (int mip = 0; mip < inTex.mipmapCount - globalMipmapLimit; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip + globalMipmapLimit; Graphics.CopyTexture(inTex, 0, srcMip, 0, 0, copyWidth, copyHeight, outArray, 0, dstMip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not copied (but outTex does not upload it to GPU anyway) for (int mip = globalMipmapLimit; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip - globalMipmapLimit; Graphics.CopyTexture(outArray, 0, srcMip, 0, 0, copyWidth, copyHeight, outTex, 0, dstMip, 0, 0); } } }}
Mipmap level arguments always refer to a texture's currently loaded mipmap levels (which can be found via GraphicsTextureDescriptor.mipCount for that texture's Texture.graphicsTexture). For example, if the global texture mipmap limit is set to 0, mip 1 of a 256x256 texture refers to a 128x128 mip. However, for that same texture, if the global texture mipmap limit is set to 1, mip 1 refers to a 64x64 mip. If the global texture mipmap limit is set to 2, mip 1 of that same texture would refer to a 32x32 mip (and so on).
This means that when you use , you do not need to take mipmap limit settings into account in your calls in most cases. However, if you use Textures and the source and destination's mipmap limit settings differ, you need to adjust for those settings. Keep in mind that non-2D texture types are always uploaded to the GPU at full resolution, because they do not support mipmap limit settings of any sort.
CopyTextureCompressed texture formats add some restrictions to with a region variant. For example, PVRTC formats are not supported since they are not block-based (for these formats you can only copy whole texture or whole mip level). For block-based formats (e.g. DXT, BCn, ETC), the region size and coordinates must be a multiple of compression block size (4 pixels for DXT).
CopyTextureNote: PVRTC format is deprecated. Use ASTC or ETC format instead.
Even if Texture.isReadable is , doesn't copy pixel data on the CPU if you copy only a region of a compressed texture.
trueCopyTextureAdditional Resources: CopyTextureSupport, Texture2D.CopyPixels.
CopyTexture(GraphicsTexture, int, int, int, int, int, int, GraphicsTexture, int, int, int, int)
Copies pixel data from one texture to another.
public static void CopyTexture(GraphicsTexture src, int srcElement, int srcMip, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsTexture dst, int dstElement, int dstMip, int dstX, int dstY)
Parameters
The source texture to copy from, specified as a Texture or as a GraphicsTexture.
The element in the source texture to copy from. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to if is a 2D texture.
0srcThe mipmap level to copy from. The range is through the texture's Texture.mipmapCount or GraphicsTextureDescriptor.mipCount. The default value is .
00The starting x coordinate of to copy a region from. Accepted values range from to the width of at the specified . is the left of the texture.
src0srcsrcMip0The starting y coordinate of to copy a region from. Accepted values range from to the height of at the specified . is the bottom of the texture.
src0srcsrcMip0The width of the region to copy. Accepted values are greater than or equal to and ensure that the region fits within at the specified considering offset , and within at the specified considering offset .
0srcsrcMip(srcX, srcY)dstdstMip(dstX, dstY)The height of the region to copy. Accepted values are greater than or equal to and ensure that the region fits within at the specified considering offset , and within at the specified considering offset .
0srcsrcMip(srcX, srcY)dstdstMip(dstX, dstY)The destination texture to copy to, specified as a Texture or as a GraphicsTexture.
The element in the destination texture to copy to. For example, the CubemapFace in a Cubemap or the slice in a texture array. Set the value to if is a 2D texture.
0dstThe mipmap level to write to. The range is through the texture's Texture.mipmapCount or GraphicsTextureDescriptor.mipCount. The default value is .
00The starting x coordinate of to copy a region to. Accepted values range from to the width of at the specified . is the left of the texture.
dst0dstdstMip0The starting y coordinate of to copy a region to. Accepted values range from to the height of at the specified . is the bottom of the texture.
dst0dstdstMip0Remarks
This method copies pixel data from one texture to another on the GPU. If Texture.isReadable is for both and , the method also attempts to copy pixel data on the CPU by running a method such as Texture2D.CopyPixels. If Texture.isReadable is for either or , or they are provided as type GraphicsTexture, no pixel data will be copied on the CPU and becomes one of the fastest ways to copy a texture. Warning: Be careful when using an method such as Texture2D.Apply after , because you might copy old or undefined CPU texture data to the GPU.
truesrcdstCopyPixelsfalsesrcdstCopyTextureApplyCopyTextureTo use , the following must be the same in both the source and destination texture areas:
CopyTexture- Format. You can also use two compatible formats - for example, TextureFormat.ARGB32 and RenderTextureFormat.ARGB32.
- Size.
- RenderTexture.antiAliasing or GraphicsTextureDescriptor.numSamples values, if the textures are render targets.
You might be able to copy between incompatible formats depending on your graphics API. For example, on some APIs you can copy between formats with the same bit width.
Depending on your graphics API, you might not be able to copy between different types of textures. For more information on compatibility, see SystemInfo.copyTextureSupport and CopyTextureSupport.
If is a depth-only render target, you must copy the whole texture, not part of it. A depth-only render texture has its color buffer set to a color format of and its depth buffer set to a valid RenderTexture.depthStencilFormat. A depth graphics texture has its format set to a valid GraphicsFormat with a depth component.
srcNoneWhen you use QualitySettings.globalTextureMipmapLimit, Texture2D.mipmapLimitGroup and Texture2D.ignoreMipmapLimit, textures can have a variety of mipmap limit settings. This means you may not be able to copy from one mipmap level to another, because for example the source texture is limited to half resolution and the destination texture is limited to quarter resolution. Note that this does not apply when and are set to graphics textures, because graphics textures already have their mipmap count limited, and only represent the mipmap levels that are currently uploaded.
srcdstIf you need to copy a mip between textures with different mipmap limit settings, use the region-based overload. This overload adjusts the source rectangle based on the source texture's mipmap limit settings, and the destination offset based on the destination's mipmap limit settings. For example, copying a 128x128 area from position 16,16 to position 32,32 results in a variety of behaviors based on the global texture mipmap limit, the source texture type, and the destination texture type:
- When the global texture mipmap limit is 0, and both textures are subject to it, Unity performs all copies as expected.
- When the global texture mipmap limit is 2, and both textures are subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, then changes the destination offset to 8x8. Unity then performs all copies as expected and ignores all mipmap limit settings.
- When the global texture mipmap limit is 2, the source is a texture subject to it, and the destination is a texture not subject to it, Unity adjusts the source rectangle to 32x32 and its offset to 4,4, but doesn't change the destination offset, which remains 32x32.
The following sample shows how you can copy to or from a Texture2DArray, when Texture2Ds have mipmap limits:
using UnityEngine;public class CopyTextureSample : MonoBehaviour{ // Sample to copy all available mips: inTex -> outArray (no mipmap limits) -> outTex [SerializeField] Texture2D inTex; [SerializeField] Texture2DArray outArray; [SerializeField] Texture2D outTex; [SerializeField] bool considerMipmapLimits; void Start() { int width = inTex.width; int height = inTex.width; outArray = new Texture2DArray(width, height, 1, inTex.format, true); outTex = new Texture2D(width, height, inTex.format, true); if (!considerMipmapLimits) { // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => copies into mips that are now too large; will copy each mip of inTex into a quarter of outArray for (int mip = 0; mip < inTex.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(inTex, 0, mip, 0, 0, copyWidth, copyHeight, outArray, 0, mip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => errors, since we try to copy into mips that are now too small for (int mip = 0; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; Graphics.CopyTexture(outArray, 0, mip, 0, 0, copyWidth, copyHeight, outTex, 0, mip, 0, 0); } } else // considering mipmap limits { int globalMipmapLimit = QualitySettings.globalTextureMipmapLimit; // Texture2D -> Texture2DArray // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not written to, other mips copy as expected // (ALTERNATIVE: if outArray creation already considered globalMipmapLimit for its dimensions, // the CopyTexture call can ignore globalMipmapLimit since the mips will line up again) for (int mip = 0; mip < inTex.mipmapCount - globalMipmapLimit; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip + globalMipmapLimit; Graphics.CopyTexture(inTex, 0, srcMip, 0, 0, copyWidth, copyHeight, outArray, 0, dstMip, 0, 0); } // Texture2DArray -> Texture2D // Global Mipmap Limit: "1: Half Resolution" => mip0 of outArray is not copied (but outTex does not upload it to GPU anyway) for (int mip = globalMipmapLimit; mip < outArray.mipmapCount; ++mip) { int copyWidth = width >> mip; int copyHeight = height >> mip; int srcMip = mip; int dstMip = mip - globalMipmapLimit; Graphics.CopyTexture(outArray, 0, srcMip, 0, 0, copyWidth, copyHeight, outTex, 0, dstMip, 0, 0); } } }}
Mipmap level arguments always refer to a texture's currently loaded mipmap levels (which can be found via GraphicsTextureDescriptor.mipCount for that texture's Texture.graphicsTexture). For example, if the global texture mipmap limit is set to 0, mip 1 of a 256x256 texture refers to a 128x128 mip. However, for that same texture, if the global texture mipmap limit is set to 1, mip 1 refers to a 64x64 mip. If the global texture mipmap limit is set to 2, mip 1 of that same texture would refer to a 32x32 mip (and so on).
This means that when you use , you do not need to take mipmap limit settings into account in your calls in most cases. However, if you use Textures and the source and destination's mipmap limit settings differ, you need to adjust for those settings. Keep in mind that non-2D texture types are always uploaded to the GPU at full resolution, because they do not support mipmap limit settings of any sort.
CopyTextureCompressed texture formats add some restrictions to with a region variant. For example, PVRTC formats are not supported since they are not block-based (for these formats you can only copy whole texture or whole mip level). For block-based formats (e.g. DXT, BCn, ETC), the region size and coordinates must be a multiple of compression block size (4 pixels for DXT).
CopyTextureNote: PVRTC format is deprecated. Use ASTC or ETC format instead.
Even if Texture.isReadable is , doesn't copy pixel data on the CPU if you copy only a region of a compressed texture.
trueCopyTextureAdditional Resources: CopyTextureSupport, Texture2D.CopyPixels.