# ObjectIdRequest

> ObjectId request that can be used to determine the object corresponding to each pixel. Can be submitted using either Camera.SubmitRenderRequest or RenderPipeline.SubmitRenderRequest, and the results can be used either on the CPU in C# or the GPU in a shader.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine.Rendering](/engine/6000.7/script-reference/unityengine/rendering.md)
* **Assembly:** UnityEngine.CoreModule

```csharp
public class ObjectIdRequest
```

## Remarks

After submitting this render request, the [\_result](/engine/6000.7/script-reference/unityengine/rendering/objectidrequest/result.md) field will be filled, and each pixel in [\_destination](/engine/6000.7/script-reference/unityengine/rendering/objectidrequest/destination.md) will contain a 32-bit color that can be decoded to a 32-bit integer index using [ObjectIdResult.DecodeIdFromColor](/engine/6000.7/script-reference/unityengine/rendering/objectidresult/decodeidfromcolor.md). This 32-bit index can then be looked up in [\_idToObjectMapping](/engine/6000.7/script-reference/unityengine/rendering/objectidresult/idtoobjectmapping.md) to determine the object corresponding to each pixel. The C# code example below demonstrates how this render request can be used to determine the object corresponding to each pixel.

Although there is no built-in functionality for decoding the object ID (the index into [\_idToObjectMapping](/engine/6000.7/script-reference/unityengine/rendering/objectidresult/idtoobjectmapping.md)) from a color on the GPU, the following HLSL shader function can be used for this purpose: @@int decodeObjectIdInShader(float4 color) \{ return (int)(color.r \* 255) + ((int)(color.g \* 255) \<\<  8) + ((int)(color.b \* 255) \<\< 16) + ((int)(color.a \* 255) \<\< 24); }@@

SRP Compatibility:

* URP: Supported by falling back on the Built-In Render Pipeline implementation when called in the editor.
* HDRP: Supported by falling back on the Built-In Render Pipeline implementation when called in the editor.

This render request is not currently supported outside of the editor.

Additional Resources: [Camera.SubmitRenderRequest](/engine/6000.7/script-reference/unityengine/camera/submitrenderrequest.md), [RenderPipeline.SubmitRenderRequest](/engine/6000.7/script-reference/unityengine/rendering/renderpipeline/submitrenderrequest.md), [ObjectIdResult](/engine/6000.7/script-reference/unityengine/rendering/objectidresult.md).

## Examples

```csharp
using UnityEngine;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;

// Snapshot containing the object at each pixel from the perspective of the camera.
// The snapshot is taken when this class is constructed.
class ObjectIdSnapshot
{
    Texture2D m_ObjectIdTexture;
    Object[] m_IdToObjectMapping;

    public ObjectIdSnapshot(Camera camera)
    {
        var cameraTargetTexture = camera.targetTexture;
        var renderTexture = new RenderTexture(
            width: cameraTargetTexture.width,
            height: cameraTargetTexture.height,
            colorFormat: GraphicsFormat.R8G8B8A8_UNorm,
            depthStencilFormat: GraphicsFormat.D32_SFloat);

        var objectIdRequest = new ObjectIdRequest(destination: renderTexture);
        camera.SubmitRenderRequest(objectIdRequest);
        m_ObjectIdTexture = ConvertTexture(objectIdRequest.destination);
        m_IdToObjectMapping = objectIdRequest.result.idToObjectMapping;
    }

    public GameObject GetGameObjectAtPixel(int x, int y)
    {
        var color = m_ObjectIdTexture.GetPixel(x, y);
        var id = ObjectIdResult.DecodeIdFromColor(color);
        var obj = m_IdToObjectMapping[id];
        return GetParentGameObject(obj);
    }

    static GameObject GetParentGameObject(Object obj)
    {
        if (obj is null)
        {
            return null;
        }
        if (obj is GameObject gameObject)
        {
            return gameObject;
        }
        if (obj is MonoBehaviour component)
        {
            return component.gameObject;
        }
        return null;
    }

    static Texture2D ConvertTexture(RenderTexture renderTexture)
    {
        var previousActiveRenderTexture = RenderTexture.active;
        RenderTexture.active = renderTexture;
        var cpuTexture = new Texture2D(renderTexture.width, renderTexture.height, renderTexture.graphicsFormat, TextureCreationFlags.None);
        cpuTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
        cpuTexture.Apply();
        RenderTexture.active = previousActiveRenderTexture;
        return cpuTexture;
    }
}
```

## Properties

| Property                                                                                            | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| --------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [destination](/engine/6000.7/script-reference/unityengine/rendering/objectidrequest/destination.md) | RenderTexture to store the rendering result of the request. The colors in this RenderTexture can be decoded to determine the object that was rendered at each pixel, first by decoding the color to an index using [ObjectIdResult.DecodeIdFromColor](/engine/6000.7/script-reference/unityengine/rendering/objectidresult/decodeidfromcolor.md) and then by looking this index up in [\_idToObjectMapping](/engine/6000.7/script-reference/unityengine/rendering/objectidresult/idtoobjectmapping.md). |
| [face](/engine/6000.7/script-reference/unityengine/rendering/objectidrequest/face.md)               | Target Cubemap face to store the rendering result.                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| [mipLevel](/engine/6000.7/script-reference/unityengine/rendering/objectidrequest/miplevel.md)       | Target mipLevel to store the rendering output.                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| [result](/engine/6000.7/script-reference/unityengine/rendering/objectidrequest/result.md)           | A result field that is filled when the render request has been submitted and completed, containing the [\_idToObjectMapping](/engine/6000.7/script-reference/unityengine/rendering/objectidresult/idtoobjectmapping.md) that is needed to interpret the color-encoded object IDs that are rendered in the [\_destination](/engine/6000.7/script-reference/unityengine/rendering/objectidrequest/destination.md) RenderTexture.                                                                          |
| [slice](/engine/6000.7/script-reference/unityengine/rendering/objectidrequest/slice.md)             | Target slice to store the rendering output.                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
