返回介绍

Texture2D.SetPixels32 设置32位像素块

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

JavaScript => public function SetPixels32(colors: Color[], miplevel: int = 0): void;
C# => public void SetPixels32(Color[] colors, int miplevel = 0);

Description 描述

Set a block of pixel colors.

设置像素块的颜色。

This function takes a Color32 array and changes the pixel colors of the whole mip level of the texture. Call Apply to actually upload the changed pixels to the graphics card.

该函数获取一个Color32数组,并更改整个纹理mip级别的像素颜色。调用Apply函数才实际上传改变后的像素数据到显卡上。

The colors array is a flattened 2D array, where pixels are laid out left to right, bottom to top (i.e. row after row). Array size must be at least 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.

colors数组是个偏平数组,像素布局从左到右,从下到上(例如一行行)。数组的大小是使用mip级别的宽乘以高,默认的mip级别是0(基本纹理)这种情况下大小为纹理大小,带mip级别是mipWidth=max(1,width>>miplevel) 高度也类似。

This function works only on ARGB32 texture formats. For other formats SetPixels32 is ignored. The texture also has to have Is Readable flag set in the import settings.

该函数仅工作于ARGB32、RGB24 和Alpha8格式。其他格式SetPixels32忽略。该纹理必须在导入设置启用Read/Write Enabled标识。

Using SetPixels32 is faster than calling SetPixels.

使用SetPixels32比调用SetPixels更快,尤其是较大的纹理。此外,SetPixels可访问单个mipmap级别。

See Also: SetPixels, GetPixels32, GetPixels, Apply, LoadRawTextureData, mipmapCount.

JavaScript:

#pragma strict
function Start() {
	var rend: Renderer = GetComponent.<Renderer>();
	// duplicate the original texture and assign to the material
	var texture: var = Instantiate(rend.material.mainTexture) as Texture2D;
	rend.material.mainTexture = texture;
	// colors used to tint the first 3 mip levels
	var colors: Color[] = new Color[3];
	colors[0] = Color.red;
	colors[1] = Color.green;
	colors[2] = Color.blue;
	var mipCount: int = Mathf.Min(3, texture.mipmapCount);
	// tint each mip level
	for (var mip: var = 0; mip < mipCount; ++mip) {
		var cols: var = texture.GetPixels(mip);
		for (var i: var = 0; i < cols.Length; ++i) {
			cols[i] = Color.Lerp(cols[i], colors[mip], 0.33f);
		}
		texture.SetPixels(cols, mip);
	}
	// actually apply all SetPixels, don't recalculate mip levels
	texture.Apply(false);
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Start() {
        Renderer rend = GetComponent<Renderer>();
 
        // duplicate the original texture and assign to the material
        var texture = Instantiate(rend.material.mainTexture) as Texture2D;
        rend.material.mainTexture = texture;
 
        // colors used to tint the first 3 mip levels
        Color[] colors = new Color[3];
        colors[0] = Color.red;
        colors[1] = Color.green;
        colors[2] = Color.blue;
        int mipCount = Mathf.Min(3, texture.mipmapCount);
 
       // tint each mip level
       for( var mip = 0; mip < mipCount; ++mip ) {
	var cols = texture.GetPixels( mip );
	for( var i = 0; i < cols.Length; ++i ) {
		cols[i] = Color.Lerp(cols[i], colors[mip], 0.33f);
	}
	texture.SetPixels( cols, mip );
        }
        // actually apply all SetPixels, don't recalculate mip levels
        texture.Apply(false);
    }
}

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

Description 描述

Set a block of pixel colors.

设置像素颜色块。

This function is an extended version of SetPixels above; it does not modify the whole mip level but modifies only blockWidth by blockHeight region starting at x,y. The colors array must be blockWidth*blockHeight size, and the modified block must fit into the used mip level.

该函数是SetPixels的扩展版本,不修改整个mip级别,仅修改起始于xy坐标blockWidth*blockHeight的区域。该颜色数组必须是blockWidth*blockHeight的大小,修改的块必须适配使用的mip级别。

Texture2D

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

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

发布评论

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