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.
After submitting this render request, the ObjectIdRequest._result field will be filled, and each pixel in ObjectIdRequest._destination will contain a 32-bit color that can be decoded to a 32-bit integer index using ObjectIdResult.DecodeIdFromColor.
This 32-bit index can then be looked up in ObjectIdResult._idToObjectMapping 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 ObjectIdResult._idToObjectMapping) 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.
See Also: Camera.SubmitRenderRequest, RenderPipeline.SubmitRenderRequest, ObjectIdResult.
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; } }
destination | 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 and then by looking this index up in ObjectIdResult._idToObjectMapping. |
face | Target Cubemap face to store the rendering result. |
mipLevel | Target mipLevel to store the rendering output. |
result | A result field that is filled when the render request has been submitted and completed, containing the ObjectIdResult._idToObjectMapping that is needed to interpret the color-encoded object IDs that are rendered in the ObjectIdRequest._destination RenderTexture. |
slice | Target slice to store the rendering output. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.