Blit
Uses a shader to copy the pixel data from a texture into a render target.
Read time 58 minutesLast updated 5 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
Blit(Texture, RenderTexture)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, RenderTexture dest)
Parameters
Remarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, RenderTexture, int, int)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, RenderTexture dest, int sourceDepthSlice, int destDepthSlice)
Parameters
The source texture.
The destination RenderTexture or GraphicsTexture.
The element in the source texture to copy from, for example the texture in a texture array. You can't use to specify a face in a Cubemap.
sourceDepthSliceThe element in the destination texture to copy from, for example the texture in a texture array. You can't use to specify a face in a Cubemap.
destDepthSliceRemarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, RenderTexture, Vector2, Vector2)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, RenderTexture dest, Vector2 scale, Vector2 offset)
Parameters
The source texture.
The destination RenderTexture or GraphicsTexture.
The scale to apply to the source texture. Unity uses this scale value to multiply the UVs it uses to sample the source texture.
The offset to apply to the source texture. Unity adds this offset value to the UVs before sampling the source texture. The offset is independent from the scale as Unity adds the offset only after it applies the scale.
Remarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, RenderTexture, Vector2, Vector2, int, int)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, RenderTexture dest, Vector2 scale, Vector2 offset, int sourceDepthSlice, int destDepthSlice)
Parameters
The source texture.
The destination RenderTexture or GraphicsTexture.
The scale to apply to the source texture. Unity uses this scale value to multiply the UVs it uses to sample the source texture.
The offset to apply to the source texture. Unity adds this offset value to the UVs before sampling the source texture. The offset is independent from the scale as Unity adds the offset only after it applies the scale.
The element in the source texture to copy from, for example the texture in a texture array. You can't use to specify a face in a Cubemap.
sourceDepthSliceThe element in the destination texture to copy from, for example the texture in a texture array. You can't use to specify a face in a Cubemap.
destDepthSliceRemarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, RenderTexture, Material, int)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, RenderTexture dest, Material mat, int pass)
Parameters
The source texture.
The destination RenderTexture or GraphicsTexture.
The material to use. If you don't provide , Unity uses a default material.
matIf the value is , Unity draws all the passes in . Otherwise, Unity draws only the pass you set to. The default value is .
-1matpass-1Remarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, RenderTexture, Material, int, int)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, RenderTexture dest, Material mat, int pass, int destDepthSlice)
Parameters
The source texture.
The destination RenderTexture or GraphicsTexture.
The material to use. If you don't provide , Unity uses a default material.
matIf the value is , Unity draws all the passes in . Otherwise, Unity draws only the pass you set to. The default value is .
-1matpass-1The element in the destination texture to copy from, for example the texture in a texture array. You can't use to specify a face in a Cubemap.
destDepthSliceRemarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, RenderTexture, Material)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, RenderTexture dest, Material mat)
Parameters
The source texture.
The destination RenderTexture or GraphicsTexture.
The material to use. If you don't provide , Unity uses a default material.
matRemarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, Material, int)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, Material mat, int pass)
Parameters
Remarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, Material, int, int)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, Material mat, int pass, int destDepthSlice)
Parameters
The source texture.
The material to use. If you don't provide , Unity uses a default material.
matIf the value is , Unity draws all the passes in . Otherwise, Unity draws only the pass you set to. The default value is .
-1matpass-1The element in the destination texture to copy from, for example the texture in a texture array. You can't use to specify a face in a Cubemap.
destDepthSliceRemarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, Material)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, Material mat)
Parameters
Remarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, GraphicsTexture)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, GraphicsTexture dest)
Parameters
Remarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, GraphicsTexture, int, int)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, GraphicsTexture dest, int sourceDepthSlice, int destDepthSlice)
Parameters
The source texture.
The destination RenderTexture or GraphicsTexture.
The element in the source texture to copy from, for example the texture in a texture array. You can't use to specify a face in a Cubemap.
sourceDepthSliceThe element in the destination texture to copy from, for example the texture in a texture array. You can't use to specify a face in a Cubemap.
destDepthSliceRemarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, GraphicsTexture, Vector2, Vector2)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, GraphicsTexture dest, Vector2 scale, Vector2 offset)
Parameters
The source texture.
The destination RenderTexture or GraphicsTexture.
The scale to apply to the source texture. Unity uses this scale value to multiply the UVs it uses to sample the source texture.
The offset to apply to the source texture. Unity adds this offset value to the UVs before sampling the source texture. The offset is independent from the scale as Unity adds the offset only after it applies the scale.
Remarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, GraphicsTexture, Vector2, Vector2, int, int)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, GraphicsTexture dest, Vector2 scale, Vector2 offset, int sourceDepthSlice, int destDepthSlice)
Parameters
The source texture.
The destination RenderTexture or GraphicsTexture.
The scale to apply to the source texture. Unity uses this scale value to multiply the UVs it uses to sample the source texture.
The offset to apply to the source texture. Unity adds this offset value to the UVs before sampling the source texture. The offset is independent from the scale as Unity adds the offset only after it applies the scale.
The element in the source texture to copy from, for example the texture in a texture array. You can't use to specify a face in a Cubemap.
sourceDepthSliceThe element in the destination texture to copy from, for example the texture in a texture array. You can't use to specify a face in a Cubemap.
destDepthSliceRemarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, GraphicsTexture, Material, int)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, GraphicsTexture dest, Material mat, int pass)
Parameters
The source texture.
The destination RenderTexture or GraphicsTexture.
The material to use. If you don't provide , Unity uses a default material.
matIf the value is , Unity draws all the passes in . Otherwise, Unity draws only the pass you set to. The default value is .
-1matpass-1Remarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, GraphicsTexture, Material, int, int)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, GraphicsTexture dest, Material mat, int pass, int destDepthSlice)
Parameters
The source texture.
The destination RenderTexture or GraphicsTexture.
The material to use. If you don't provide , Unity uses a default material.
matIf the value is , Unity draws all the passes in . Otherwise, Unity draws only the pass you set to. The default value is .
-1matpass-1The element in the destination texture to copy from, for example the texture in a texture array. You can't use to specify a face in a Cubemap.
destDepthSliceRemarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.
Blit(Texture, GraphicsTexture, Material)
Uses a shader to copy the pixel data from a texture into a render target.
public static void Blit(Texture source, GraphicsTexture dest, Material mat)
Parameters
The source texture.
The destination RenderTexture or GraphicsTexture.
The material to use. If you don't provide , Unity uses a default material.
matRemarks
This method copies pixel data from a texture on the GPU to a render texture or graphics texture on the GPU. This is one of the fastest ways to copy a texture.
When you use , Unity does the following:
Graphics.Blit- Sets the active render target to the texture. (See RenderTexture.active and GraphicsTexture.active.)
dest - Passes to the
sourcematerial as thematproperty._MainTex - Uses the material's shader to draw a full-screen surface from the texture to the
sourcetexture.dest
If you provide a material that doesn't have a property, doesn't use . If you provide a GraphicsTexture as , it must have GraphicsTextureDescriptorFlags.RenderTarget enabled in its GraphicsTextureDescriptor.flags.
mat_MainTexBlitsourcedestYou can use to create post-processing effects, by setting to a material with a custom shader.
Graphics.BlitmatBlitBlitAvoid setting and to the same texture, as this may cause undefined behaviour. Use Custom Render Textures with double buffering instead, or use two render textures and alternate between them to implement double buffering manually.
sourcedestIn linear color space, set GL.sRGBWrite before using , to make sure the sRGB-to-linear color conversion is what you expect.
BlitTo blit to the screen in the Built-in Render Pipeline, follow these steps:
- Set to
dest. Unity now usesnullas the destination texture.Camera.main.targetTexture - Set the Camera.targetTexture property of Camera.main to .
null
To blit to the screen in the Universal Render Pipeline (URP) or the High Definition Render Pipeline (HDRP), you must call Graphics.Blit or CommandBuffer.Blit inside a method that you call from the RenderPipelineManager.endContextRendering callback.
If you want to use a depth or stencil buffer that is part of the (Render)texture, or blit to a subregion of a texture, you have to manually write an equivalent of the Graphics.Blit function - i.e. Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).
sourceAdditional Resources: Graphics.BlitMultiTap, Post-processing effects.
using UnityEngine;public class Example : MonoBehaviour{ Texture aTexture; RenderTexture rTex; void Start() { if (!aTexture || !rTex) { Debug.LogError("A texture or a render texture are missing, assign them."); } } void Update() { Graphics.Blit(aTexture, rTex); }}
Copies aTexture to rTex and displays it in all cameras.
using UnityEngine;public class Fisheye : MonoBehaviour{ public Texture sourceTexture; public RenderTexture targetRenderTexture; public Material fishEyeMaterial; void Update() { // Apply the fish-eye effect every frame Graphics.Blit(sourceTexture, targetRenderTexture, fishEyeMaterial); }}
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the field.
fishEyeMaterialShader "Custom/FishEyeShader"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _Distortion; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } float4 frag(v2f i) : COLOR { float2 uv = i.uv; float2 center = float2(0.5, 0.5); // Center of the texture // Fisheye distortion float2 diff = uv - center; float dist = length(diff); diff *= pow(_Distortion, dist); uv = center + diff; // Apply distortion return tex2D(_MainTex, uv); } ENDCG } }}
Shader code used for the fish eye effect.