RenderTexture
Render textures are textures that can be rendered to.
Read time 9 minutesLast updated 2 days ago
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
- Inherits from: Texture
public class RenderTexture : Texture
Remarks
They can be used to implement image based rendering effects, dynamic shadows, projectors, reflections or surveillance cameras.
One typical usage of render textures is setting them as the "target texture" property of a Camera (Camera.targetTexture), this will make a camera render into a texture instead of rendering to the screen.
Keep in mind that render texture contents can become "lost" on certain events, like loading a new level, system going to a screensaver mode, in and out of fullscreen and so on. When that happens, your existing render textures will become "not yet created" again, you can check for that with RenderTexture.IsCreated function.
As with other "native engine object" types, it is important to pay attention to the lifetime of any render textures and release them when you are finished using them with the RenderTexture.Release function, as they will not be garbage collected like normal managed types.
A render texture only has a data representation on the GPU and you need to use Texture2D.ReadPixels to transfer its contents to CPU memory.
The initial contents of a newly created render texture are undefined. On some platforms and APIs the contents will default to black, but you shouldn't depend on this. You can use LoadStoreActionDebugModeSettings to highlight undefined areas of the display, to help you debug rendering problems in your built application.
Additional Resources: Camera.targetTexture.
Constructors
Constructor | Description |
|---|---|
| RenderTexture() | Creates a new RenderTexture object. |
| RenderTexture(System.Int32,System.Int32,System.Int32,UnityEngine.RenderTextureFormat,UnityEngine.RenderTextureReadWrite) | Creates a new RenderTexture object. |
| RenderTexture(UnityEngine.RenderTexture) | Creates a new RenderTexture object. |
| RenderTexture(UnityEngine.RenderTextureDescriptor) | Creates a new RenderTexture object. |
Properties
Property | Description |
|---|---|
| antiAliasing | The antialiasing level for the RenderTexture. |
| autoGenerateMips | Mipmap levels are generated automatically when this flag is set. |
| bindTextureMS | If true and RenderTexture.antiAliasing is greater than 1, the render texture will not be resolved by default. Use this if the render texture needs to be bound as a multisampled texture in a shader. |
| colorBuffer | Color buffer of the render texture (Read Only). |
| depth | The precision of the render texture's depth buffer in bits (0, 16, 24 and 32 are supported). |
| depthBuffer | Depth/stencil buffer of the render texture (Read Only). |
| depthStencilFormat | The format of the depth/stencil buffer. |
| descriptor | This struct contains all the information required to create a RenderTexture. It can be copied, cached, and reused to easily create RenderTextures that all share the same properties. |
| dimension | Dimensionality (type) of the render texture. |
| enableRandomWrite | Enable random access write into this render texture on Shader Model 5.0 level shaders. |
| graphicsFormat | The color format of the render texture. You can set the color format to None to achieve depth-only rendering. |
| height | The height of the render texture in pixels. |
| memorylessMode | The render texture memoryless mode property. |
| sRGB | Does this render texture use sRGB read/write conversions? (Read Only). |
| stencilFormat | The format of the stencil data that you can encapsulate within a RenderTexture. |
| useDynamicScale | When this flag is set to true, render texture is set to be used by the Dynamic Resolution system. |
| useDynamicScaleExplicit | When this flag is set to true, render texture is set to be used by the Dynamic Resolution system. Scale is applied with an explicit call to RenderTexture.ApplyDynamicScale |
| useMipMap | Render texture has mipmaps when this flag is set. |
| volumeDepth | Volume extent of a 3D render texture or number of slices of array texture. |
| vrUsage | If this RenderTexture is a VR eye texture used in stereoscopic rendering, this property decides what special rendering occurs, if any. |
| width | The width of the render texture in pixels. |
Static Properties
Property | Description |
|---|---|
| active | Currently active render texture. |
Methods
Method | Description |
|---|---|
| ApplyDynamicScale | Applies the Dynamic Resolution system scale. |
| ConvertToEquirect | Converts the render texture to equirectangular format (both stereoscopic or monoscopic equirect). The left eye will occupy the top half and the right eye will occupy the bottom. The monoscopic version will occupy the whole texture. Texture dimension must be of type TextureDimension.Cube. |
| Create | Actually creates the RenderTexture. |
| DiscardContents | Hint the GPU driver that the contents of the RenderTexture will not be used. |
| GenerateMips | Generate mipmap levels of a render texture. |
| GetNativeDepthBufferPtr | Retrieve a native (underlying graphics API) pointer to the depth buffer resource. |
| IsCreated | Is the render texture actually created? |
| Release | Releases the RenderTexture. |
| ResolveAntiAliasedSurface | Force an antialiased render texture to be resolved. |
| SetGlobalShaderProperty | Assigns this RenderTexture as a global shader property named propertyName. |
Static Methods
Method | Description |
|---|---|
| GetTemporary | Allocate a temporary render texture. |
| ReleaseTemporary | Release a temporary texture allocated with RenderTexture.GetTemporary. |
| SupportsStencil | Does a RenderTexture have stencil buffer? |
Inheritance
Operators
Operator | Description |
|---|---|
| operator == | Compares two object references to see if they refer to the same object. |
| bool | Determines whether the object exists. |
| operator != | Compares if two objects refer to a different object. |