返回介绍

Texture2D.SetPixels 设置像素块

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

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

Description 描述

Set a block of pixel colors.

设置像素块的颜色。

This function takes a color 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.

该函数获取一个color数组,并更改整个纹理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, RGB24 and Alpha8 texture formats. For other formats SetPixels is ignored. The texture also has to have Is Readable flag set in the import settings.

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

Using SetPixels can be faster than calling SetPixel repeatedly, especially for large textures. In addition, SetPixels can access individual mipmap levels.

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

See Also: GetPixels, SetPixels32, 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);
    }
}

Texture2D

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

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

发布评论

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