返回介绍

Texture2D.GetPixelBilinear 获取双线性像素颜色

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

JavaScript => public function GetPixelBilinear(u: float, v: float): Color;
C# => public Color GetPixelBilinear(float u, float v);

Description 描述

Returns filtered pixel color at normalized coordinates (u, v).

返回归一化纹理坐标(u,v)处的已过滤像素颜色。

Coordinates u and v go from 0.0 to 1.0, just like UV coordinates in meshes. If coordinates are out of bounds (larger than 1.0 or smaller than 0.0), they will be clamped or repeated based on the texture's wrap mode.

坐标U和V是从0.0到1.0的值,就像是网格模型上的UV坐标。如果坐标超出边界(大于1.0或小于0.0),它将基于纹理的包裹重复方式进行钳制或重复。

Texture coordinates start at lower left corner. UV of (0,0) lands exactly on the bottom left texel; and UV of ((width-1)/width, (height-1)/height) lands exactly on the top right texel.

Returned pixel color is bilinearly filtered.

返回双线性过滤的像素颜色。

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

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

See Also: GetPixel.

JavaScript:

// "Warp" a texture by squashing its pixels to one side.
// This involves sampling the image at non-integer pixel
// positions to ensure a smooth effect.
 
// Source image.
var sourceTex: Texture2D;
 
// Amount of "warping".
var warpFactor = 1.0;
 
private var destTex: Texture2D;
private var destPix: Color[];
 
 
function Start () {
	// Set up a new texture with the same dimensions as the original.
	destTex = new Texture2D(sourceTex.width, sourceTex.height);
	destPix = new Color[destTex.width * destTex.height];
 
	// For each pixel in the destination texture...
	for (var y = 0; y < destTex.height; y++) {
		for (var x = 0; x < destTex.width; x++) {
			// Calculate the fraction of the way across the image
			// that this pixel positon corresponds to.
			var xFrac = x * 1.0 / (destTex.width - 1);
			var yFrac = y * 1.0 / (destTex.height - 1);
 
			// Take the fractions (0..1)and raise them to a power to apply
			// the distortion.
			var warpXFrac = Mathf.Pow(xFrac, warpFactor);
			var warpYFrac = Mathf.Pow(yFrac, warpFactor);
 
			// Get the non-integer pixel positions using GetPixelBilinear.
			destPix[y * destTex.width + x] = sourceTex.GetPixelBilinear(warpXFrac, warpYFrac);
		}
	}
 
	// Copy the pixel data to the destination texture and apply the change.
	destTex.SetPixels(destPix);
	destTex.Apply();
 
	// Set our object's texture to the newly warped image.
	GetComponent.<Renderer>().material.mainTexture = destTex;
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public Texture2D sourceTex;
    public float warpFactor = 1.0F;
    private Texture2D destTex;
    private Color[] destPix;
    void Start() {
        destTex = new Texture2D(sourceTex.width, sourceTex.height);
        destPix = new Color[destTex.width * destTex.height];
        int y = 0;
        while (y < destTex.height) {
            int x = 0;
            while (x < destTex.width) {
                float xFrac = x * 1.0F / (destTex.width - 1);
                float yFrac = y * 1.0F / (destTex.height - 1);
                float warpXFrac = Mathf.Pow(xFrac, warpFactor);
                float warpYFrac = Mathf.Pow(yFrac, warpFactor);
                destPix[y * destTex.width + x] = sourceTex.GetPixelBilinear(warpXFrac, warpYFrac);
                x++;
            }
            y++;
        }
        destTex.SetPixels(destPix);
        destTex.Apply();
        GetComponent<Renderer>().material.mainTexture = destTex;
    }
}

Texture2D

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

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

发布评论

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