using UnityEngine; using System.Collections; public class TEST : 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) { // 这里为何减1?没有搞明白 float xFrac = x * 1.0F / (destTex.width - 1); float yFrac = y * 1.0F / (destTex.height - 1); //Mathf.Pow(float f, float p) 计算并返回 f 的 p 次方。 float warpXFrac = Mathf.Pow(xFrac, warpFactor); float warpYFrac = Mathf.Pow(yFrac, warpFactor); /** * 返回在正规化纹理坐标(u,v)处的过滤的像素颜色。 * 坐标U和V 是从0.0到1.0的值,就像是网格模型上的UV坐标。若是坐标超出边界(大于1.0或小于0.0),它将基于纹理的包裹重复方式进行限制或重复。 * 返回双线性过滤的像素颜色 * 这个纹理须要在导入设置里设置为Is Readable(可读),不然此函数将会无效。 */ destPix[y * destTex.width + x] = sourceTex.GetPixelBilinear(warpXFrac, warpYFrac); x++; } y++; } //设置一块像素的颜色。 destTex.SetPixels(destPix); //只有执行Apply()后,颜色变化才会更新 destTex.Apply(); //设置物体的材质贴图 renderer.material.mainTexture = destTex; } }