返回介绍

Texture2D.GetPixels 获取像素块颜色

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

JavaScript => public function GetPixels(miplevel: int = 0): Color[];
C# => public Color[] GetPixels(int miplevel = 0);

Description 描述

Get a block of pixel colors.

获取像素块颜色。

This function returns an array of pixel colors of the whole mip level of the texture.

函数返回该纹理整个mipmap级别的一个像素颜色组。

The returned array is a flattened 2D array, where pixels are laid out left to right, bottom to top (i.e. row after row). Array size is width by height of the mip level used. The default mip level is zero (the base texture) in which case the size is just the size of the texture. In general case, mip level size is mipWidth=max(1,width>>miplevel) and similarly for height.

返回的数组放在2D的数组中,像素被从左到右,从上到下的方式排列(行序)。数组的大小是这个mip等级的宽乘以高,默认的mip级别是0(基本纹理)这种情况下大小仅为纹理大小,mip等级大小是mipWidth=max(1,width>>miplevel) 高度也类似。

The texture must have the Read/Write Enabled flag set in the import settings, otherwise this function will fail.

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

Using GetPixels can be faster than calling GetPixel repeatedly, especially for large textures. In addition, GetPixels can access individual mipmap levels. For most textures, even faster is to use GetPixels32 which returns low precision color data without costly integer-to-float conversions.

使用GetPixels可以比重复调用 GetPixel ,特别是在纹理较大的情况下调用 GetPixel 快得多,另外GetPixels可以访问单个mip级别。
大多数纹理,使用getpixels32返回低精度颜色数据更快,无需昂贵的整型到浮点数转换。

See Also: GetPixels32.

JavaScript:

// Get a rectangular area of a texture and place it into a new
// texture the size of the rectangle.
 
// Source texture and the rectangular area we want to extract.
var sourceTex: Texture2D;
var sourceRect: Rect;
 
 
function Start () {
	// The values in the Rect structure are floats, so round them
	// down to the nearest integer.
	var x = Mathf.FloorToInt(sourceRect.x);
	var y = Mathf.FloorToInt(sourceRect.x);
	var width = Mathf.FloorToInt(sourceRect.width);
	var height = Mathf.FloorToInt(sourceRect.height);
 
	// Get the pixel block and place it into a new texture.
	var pix = sourceTex.GetPixels(x, y, width, height);
	var destTex = new Texture2D(width, height);
	destTex.SetPixels(pix);
	destTex.Apply();
 
	// Set the current object's texture to show the
	// extracted rectangle.
	GetComponent.<Renderer>().material.mainTexture = destTex;
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public Texture2D sourceTex;
    public Rect sourceRect;
    void Start() {
        int x = Mathf.FloorToInt(sourceRect.x);
        int y = Mathf.FloorToInt(sourceRect.x);
        int width = Mathf.FloorToInt(sourceRect.width);
        int height = Mathf.FloorToInt(sourceRect.height);
        Color[] pix = sourceTex.GetPixels(x, y, width, height);
        Texture2D destTex = new Texture2D(width, height);
        destTex.SetPixels(pix);
        destTex.Apply();
        GetComponent<Renderer>().material.mainTexture = destTex;
    }
}

See Also: SetPixels, mipmapCount.


JavaScript => public function GetPixels(x: int, y: int, blockWidth: int, blockHeight: int, miplevel: int = 0): Color[];
C# => public Color[] GetPixels(int x, int y, int blockWidth, int blockHeight, int miplevel = 0);

Description 描述

Get a block of pixel colors.

获得像素块的颜色

This function is an extended version of GetPixels above; it does not return the whole mip level but only blockWidth by blockHeight region starting at x,y. The block must fit into the used mip level. The returned array is blockWidth*blockHeight size.

这个函数是上面GetPixels函数的扩展;它不会返回整个mip等级,而只是从x,y点开始的blockWidth(块的宽)乘以blockHeight(块的高)的区域。该块必须适配使用的mip级别。返回的数组是blockWidth*blockHeight的大小。

Texture2D

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

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

发布评论

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