返回介绍

Texture2D.ReadPixels 读取像素

发布于 2019-12-18 15:38:39 字数 4118 浏览 2297 评论 0 收藏 0

JavaScript => public function ReadPixels(source: Rect, destX: int, destY: int, recalculateMipMaps: bool = true): void;
C# => public void ReadPixels(Rect source, int destX, int destY, bool recalculateMipMaps = true);

Parameters 参数

sourceRectangular region of the view to read from. Pixels are read from current render target.
视图的矩形区域,从当前渲染目标读取像素。
destXHorizontal pixel position in the texture to place the pixels that are read.
纹理的水平位置,要放置读取的像素。
destYVertical pixel position in the texture to place the pixels that are read.\\纹理的垂直位置,要放置读取的像素。
recalculateMipMapsShould the texture's mipmaps be recalculated after reading?
读取之后,纹理的Mipmap重新计算?

Description 描述

Read pixels from screen into the saved texture data.

读取屏幕像素信息并存储为纹理数据

This will copy a rectangular pixel area from the currently active RenderTexture or the view (specified by the source parameter) into the position defined by destX and destY. Both coordinates use pixel space - (0,0) is lower left.

这将从当前处于激活状态的 RenderTexture 或视图(由/source/指定)复制一个由destX和destY指定的矩形像素区域。这两个坐标使用像素空间坐标 (0,0)是屏幕左下角。

If recalculateMipMaps is set to true, the mip maps of the texture will also be updated. If recalculateMipMaps is set to false, you must call Apply to recalculate them.

如果 recalculateMipMaps 设置为真,这个贴图的mipmaps就会更新,如果 recalculateMipMaps设置为假,你需要调用Apply重新计算它们。

This function works on ARGB32 and RGB24 texture formats, when render target is of a similar format too (e.g. usual 32 or 16 bit render texture). Reading from a HDR render target (ARGBFloat or ARGBHalf render texture formats) into HDR texture formats (RGBAFloat or RGBAHalf) is supported too.

该函数工作于ARGB32和RGB24纹理格式,读取HDR渲染目标(ARGBFloat或ARGBHalf渲染纹理格式)到HDR纹理格式(RGBAFloat或RGBAHalf),也支持。

The texture also has to have Read/Write Enabled flag set in the import settings.

纹理必须开启导入设置中的Read/Write Enabled标识,否则该函数会产生错误。

JavaScript:

	// This script should be attached to a camera.
 
	// Grab the camera's view when this variable is true.
	var grab: boolean;
 
	// The "display" is the object whose texture will be set
	// to the captured image.
	var display: Renderer;
 
 
	function OnPostRender() {
		if (grab) {
			// Make a new texture of the right size and
			// read the camera image into it.
			var tex = new Texture2D(128, 128);
			tex.ReadPixels(new Rect(0, 0, 128, 128), 0, 0);
			tex.Apply();
 
			// Set the display texture to the newly captured image.
			display.material.mainTexture = tex;
 
			// Reset the grab variable to avoid making multiple
			// captures.
			grab = false;
		}
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public bool grab;
    public Renderer display;
    void OnPostRender() {
        if (grab) {
            Texture2D tex = new Texture2D(128, 128);
            tex.ReadPixels(new Rect(0, 0, 128, 128), 0, 0);
            tex.Apply();
            display.material.mainTexture = tex;
            grab = false;
        }
    }
}

See Also: EncodeToPNG.

Texture2D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文