Объявление
public void ReadPixels(Rect source, int destX, int destY, bool recalculateMipMaps = true);Параметры
source | Область цели рендеринга для чтения. |
destX | Горизонтальное положение пикселя в текстуре для записи пикселей. |
destY | Положение пикселя по вертикали в текстуре для записи пикселей. |
recalculateMipMaps | Если этот параметр имеет значение true , Unity автоматически пересчитывает MIP-карты для текстуры после записи данных пикселей. В противном случае Unity не делает этого автоматически. |
Описание
Считывает пиксели из текущей цели рендеринга (экран или RenderTexture) и записывает их в текстуру.
Этот метод копирует прямоугольную область пикселя из текущей активной цели рендеринга и записывает ее в текстуру в позиции, определенной destX
и пункт назначения
. Обе координаты используют пространство в пикселях: (0,0) находится внизу слева.
Примечание. Если вы просто хотите скопировать пиксели из одной текстуры в другую и вам не нужно манипулировать данными пикселей на ЦП, быстрее выполнить копирование с одного графического процессора на другой. пиксельных данных с помощью Graphics.CopyTexture, CommandBuffer.CopyTexture или Графика.Blit.
Для успешного выполнения этой функции Texture.isReadable должно быть true
, цель рендеринга и текстура должны использовать один и тот же формат, и формат должен поддерживаться на устройстве как для рендеринга, так и для выборки.
Если вы решите не обновлять MIP-карты автоматически как часть этой операции, вы можете сделать это вручную при вызове Texture2D.Apply.
В следующем примере кода показано, как использовать ReadPixels
во встроенном конвейере рендеринга. В Scriptable Render Pipeline Camera.onPostRender недоступен, но вы можете использовать RenderPipelineManager.endCameraRendering аналогичным образом.
using UnityEngine;
public class ReadPixelsExample : MonoBehaviour
{
// Set this reference to a GameObject that has a Renderer component,
// and a material that displays a texure (such as the Default material).
// A standard Cube or other primitive works for the purposes of this example.
public Renderer screenGrabRenderer;
private Texture2D destinationTexture;
private bool isPerformingScreenGrab;
void Start()
{
// Create a new Texture2D with the width and height of the screen, and cache it for reuse
destinationTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
// Make screenGrabRenderer display the texture.
screenGrabRenderer.material.mainTexture = destinationTexture;
// Add the onPostRender callback
Camera.onPostRender += OnPostRenderCallback;
}
void Update()
{
// When the user presses the space key, perform the screen grab operation
if (Input.GetKeyDown(KeyCode.Space))
{
isPerformingScreenGrab = true;
}
}
void OnPostRenderCallback(Camera cam)
{
if (isPerformingScreenGrab)
{
// Check whether the Camera that has just finished rendering is the one you want to take a screen grab from
if (cam == Camera.main)
{
// Define the parameters for the ReadPixels operation
Rect regionToReadFrom = new Rect(0, 0, Screen.width, Screen.height);
int xPosToWriteTo = 0;
int yPosToWriteTo = 0;
bool updateMipMapsAutomatically = false;
// Copy the pixels from the Camera's render target to the texture
destinationTexture.ReadPixels(regionToReadFrom, xPosToWriteTo, yPosToWriteTo, updateMipMapsAutomatically);
// Upload texture data to the GPU, so the GPU renders the updated texture
// Note: This method is costly, and you should call it only when you need to
// If you do not intend to render the updated texture, there is no need to call this method at this point
destinationTexture.Apply();
// Reset the isPerformingScreenGrab state
isPerformingScreenGrab = false;
}
}
}
// Remove the onPostRender callback
void OnDestroy()
{
Camera.onPostRender -= OnPostRenderCallback;
}
}
Смотрите так же: ImageConversion.EncodeToPNG.