Unity - Scripting API: Graphics.Blit
Success!
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
Submission failed
For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Declaration
public static void Blit(Texture source, RenderTexture dest, int sourceDepthSlice, int destDepthSlice);
Parameters
| Parameter | Description |
|---|---|
| source | The source texture. |
| dest | The destination RenderTexture or GraphicsTexture. |
| mat | The material to use. If you don't provide mat, Unity uses a default material. |
| pass | If the value is -1, Unity draws all the passes in mat. Otherwise, Unity draws only the pass you set pass to. The default value is -1. |
| scale | The scale to apply to the source texture. Unity uses this scale value to multiply the UVs it uses to sample the source texture. |
| offset | 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. |
| sourceDepthSlice | The element in the source texture to copy from, for example the texture in a texture array. You can't use sourceDepthSlice to specify a face in a Cubemap. |
| destDepthSlice | The element in the destination texture to copy from, for example the texture in a texture array. You can't use destDepthSlice to specify a face in a Cubemap. |
Description
Uses a shader to copy the pixel data from a texture into a render target.
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 Graphics.Blit, Unity does the following:
- Sets the active render target to the
desttexture. (See RenderTexture.active and GraphicsTexture.active.) - Passes
sourceto thematmaterial as the_MainTexproperty. - Uses the material's shader to draw a full-screen surface from the
sourcetexture to thedesttexture.
- Set
desttonull. Unity now usesCamera.main.targetTextureas the destination texture. - Set the Camera.targetTexture property of Camera.main to
null.
Copies aTexture to rTex and displays it in all cameras.
Execute a Blit from the source texture to the destination using a custom blit shader assigned to the fishEyeMaterial field.
Shader "Custom/FishEyeShader" { Properties { _MainTex ("Texture", 2D) = "white" {} _Distortion ("Distortion Strength", Range(0, 1)) = 0.5 } SubShader { Pass { ZTest Always Cull Off ZWrite OffCGPROGRAM #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.