本文整理汇总了C#中UnityEngine.Texture2D.GetPixelBilinear方法的典型用法代码示例。如果您正苦于以下问题:C# Texture2D.GetPixelBilinear方法的具体用法?C# Texture2D.GetPixelBilinear怎么用?C# Texture2D.GetPixelBilinear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Texture2D
的用法示例。
在下文中一共展示了Texture2D.GetPixelBilinear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetChannels
private static Vector4 GetChannels(Texture2D tex, float u, float v, int miplevel, int w, int h, float rangeX, float rangeY) {
if (tex == null)
return Vector4.zero;
if (miplevel == 0) {
return tex.GetPixelBilinear(u, v);
}
// Averages the result over the area within the 'pixel' for this mip level
// this is similar, but not quite exactly the same as trilinear filtering.
Vector4 value = Vector4.zero;
for (int x = -miplevel; x < miplevel; x++) {
for (int y = -miplevel; y < miplevel; y++) {
float um = u + (x * rangeX);
float vm = v - (y * rangeY);
value += (Vector4) tex.GetPixelBilinear(um, vm);
}
}
int t = miplevel * 2;
value /= t * t;
return value;
}
示例2: GetChannel
private static float GetChannel(Texture2D tex, float val, bool linear, float u, float v, int mipLevel, float rangeX,
float rangeY, int channel) {
float value = 0.0f;
bool isAlpha = channel == 3; //TODO: Check format to ensure alpha?
if (tex == null) {
value = val;
} else if (mipLevel == 0) {
value = tex.GetPixelBilinear (u, v) [channel];
} else {
// Averages the result over the area within the 'pixel' for this mip level
// this is similar, but not quite exactly the same as trilinear filtering.
for (int x = -mipLevel; x < mipLevel; ++x) {
for (int y = -mipLevel; y < mipLevel; ++y) {
float um = u + (x * rangeX);
float vm = v - (y * rangeY);
value += tex.GetPixelBilinear (um, vm) [channel];
}
}
int t = mipLevel * 2;
value /= t * t;
}
// Gamma correct here
// If editor is in linear, texture was not bypassing srgb
if (linear && !isAlpha) {
value = Mathf.LinearToGammaSpace(value);
}
return value;
}
示例3: DrawBar
private void DrawBar(float value, Rect square, Texture2D image)
{
GUI.DrawTexture(square, backgroundTexture);
square.width *= value;
GUI.color = image.GetPixelBilinear(value, 0.5f);
GUI.DrawTexture(square, foregroundTexture);
GUI.color = Color.white;
}
示例4: Load
public bool Load(Texture2D brushTex, int size)
{
if (this.m_Brush == brushTex && size == this.m_Size && this.m_Strength != null)
{
return true;
}
if (brushTex != null)
{
float num = (float)size;
this.m_Size = size;
this.m_Strength = new float[this.m_Size * this.m_Size];
if (this.m_Size > 3)
{
for (int i = 0; i < this.m_Size; i++)
{
for (int j = 0; j < this.m_Size; j++)
{
this.m_Strength[i * this.m_Size + j] = brushTex.GetPixelBilinear(((float)j + 0.5f) / num, (float)i / num).a;
}
}
}
else
{
for (int k = 0; k < this.m_Strength.Length; k++)
{
this.m_Strength[k] = 1f;
}
}
UnityEngine.Object.DestroyImmediate(this.m_Preview);
this.m_Preview = new Texture2D(this.m_Size, this.m_Size, TextureFormat.ARGB32, false);
this.m_Preview.hideFlags = HideFlags.HideAndDontSave;
this.m_Preview.wrapMode = TextureWrapMode.Repeat;
this.m_Preview.filterMode = FilterMode.Point;
Color[] array = new Color[this.m_Size * this.m_Size];
for (int l = 0; l < array.Length; l++)
{
array[l] = new Color(1f, 1f, 1f, this.m_Strength[l]);
}
this.m_Preview.SetPixels(0, 0, this.m_Size, this.m_Size, array, 0);
this.m_Preview.Apply();
if (this.m_BrushProjector == null)
{
this.CreatePreviewBrush();
}
this.m_BrushProjector.material.mainTexture = this.m_Preview;
this.m_Brush = brushTex;
return true;
}
this.m_Strength = new float[1];
this.m_Strength[0] = 1f;
this.m_Size = 1;
return false;
}
示例5: Load
public bool Load(Texture2D brushTex, int size)
{
if (((this.m_Brush == brushTex) && (size == this.m_Size)) && (this.m_Strength != null))
{
return true;
}
if (brushTex != null)
{
float num = size;
this.m_Size = size;
this.m_Strength = new float[this.m_Size * this.m_Size];
if (this.m_Size > 3)
{
for (int j = 0; j < this.m_Size; j++)
{
for (int k = 0; k < this.m_Size; k++)
{
this.m_Strength[(j * this.m_Size) + k] = brushTex.GetPixelBilinear((k + 0.5f) / num, ((float) j) / num).a;
}
}
}
else
{
for (int m = 0; m < this.m_Strength.Length; m++)
{
this.m_Strength[m] = 1f;
}
}
UnityEngine.Object.DestroyImmediate(this.m_Preview);
this.m_Preview = new Texture2D(this.m_Size, this.m_Size, TextureFormat.ARGB32, false);
this.m_Preview.hideFlags = HideFlags.HideAndDontSave;
this.m_Preview.wrapMode = TextureWrapMode.Repeat;
this.m_Preview.filterMode = UnityEngine.FilterMode.Point;
Color[] colors = new Color[this.m_Size * this.m_Size];
for (int i = 0; i < colors.Length; i++)
{
colors[i] = new Color(1f, 1f, 1f, this.m_Strength[i]);
}
this.m_Preview.SetPixels(0, 0, this.m_Size, this.m_Size, colors, 0);
this.m_Preview.Apply();
if (this.m_BrushProjector == null)
{
this.CreatePreviewBrush();
}
this.m_BrushProjector.material.mainTexture = this.m_Preview;
this.m_Brush = brushTex;
return true;
}
this.m_Strength = new float[] { 1f };
this.m_Size = 1;
return false;
}
示例6: ScaleTexture
/*
* http://jon-martin.com/?p=114
*/
public Texture2D ScaleTexture(Texture2D source,int targetWidth,int targetHeight) {
Texture2D result=new Texture2D(targetWidth,targetHeight,source.format,true);
Color[] rpixels=result.GetPixels(0);
float incX=((float)1/source.width)*((float)source.width/targetWidth);
float incY=((float)1/source.height)*((float)source.height/targetHeight);
for(int px=0; px<rpixels.Length; px++) {
rpixels[px] = source.GetPixelBilinear(incX*((float)px%targetWidth),
incY*((float)Mathf.Floor(px/targetWidth)));
}
result.SetPixels(rpixels,0);
result.Apply();
return result;
}
示例7: ScaleTexture
public static void ScaleTexture(Texture2D tex, int width, int height)
{
var newPixels = new Color[width * height];
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
newPixels[y * width + x] = tex.GetPixelBilinear(((float)x) / width, ((float)y) / height);
}
}
tex.Resize(width, height);
tex.SetPixels(newPixels);
tex.Apply();
}
示例8: Resize
public static void Resize(Texture2D texture, int width, int height)
{
Color[] pixelArray = new Color[width * height];
float incX = (1.0f / (float)width);
float incY = (1.0f / (float)height);
for (int px = 0; px < pixelArray.Length; px++)
{
pixelArray[px] = texture.GetPixelBilinear(incX * ((float)px % width), incY * ((float)Mathf.Floor(px / width)));
}
texture.Resize(width, height);
texture.SetPixels(pixelArray, 0);
texture.Apply();
}
示例9: Update
public void Update(Texture2D tex)
{
Vector3 point = particle.transform.parent.parent.InverseTransformPoint(particle.transform.position).normalized;
float u = (float)(.5 + (Mathf.Atan2(point.z, point.x) / (2f * Mathf.PI)));
float v = Mathf.Acos(-point.y) / Mathf.PI;
Color color = tex.GetPixelBilinear(u, v);
MeshFilter filter = particle.GetComponent<MeshFilter>();
Mesh mesh = filter.mesh;
mesh.colors = new Color[4]
{
new Color(color.r, color.g, color.b, color.a),
new Color(color.r, color.g, color.b, color.a),
new Color(color.r, color.g, color.b, color.a),
new Color(color.r, color.g, color.b, color.a)
};
}
示例10: ScaleTexture
private static Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight)
{
Texture2D result = new Texture2D(targetWidth, targetHeight, source.format, false);
/*float incX = (1.0f / (float)targetWidth);
float incY = (1.0f / (float)targetHeight);*/
for (int i = 0; i < result.height; ++i)
{
for (int j = 0; j < result.width; ++j)
{
Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);
result.SetPixel(j, i, newColor);
}
}
result.Apply();
return result;
}
示例11: ApplyHeightMap
public static void ApplyHeightMap(GameObject sphere, float scaleFactor, Texture2D heightTex, float cutoff = 0f)
{
MeshFilter filter = sphere.GetComponent<MeshFilter>();
Mesh mesh = filter.mesh;
Vector3[] vertices = mesh.vertices;
Vector2[] uvs = mesh.uv;
Vector3[] normals = mesh.normals;
for (int i = 0; i < vertices.Length; i++) {
float grayscale = heightTex.GetPixelBilinear(uvs[i].x, uvs[i].y).grayscale;
if (grayscale >= cutoff) {
vertices[i] += normals[i] * grayscale * scaleFactor;
}
}
filter.mesh.vertices = vertices;
}
示例12: Load
public bool Load(Texture2D brushTex, int size)
{
if ((Object) this.m_Brush == (Object) brushTex && size == this.m_Size && this.m_Strength != null)
return true;
if ((Object) brushTex != (Object) null)
{
float num = (float) size;
this.m_Size = size;
this.m_Strength = new float[this.m_Size * this.m_Size];
if (this.m_Size > 3)
{
for (int index1 = 0; index1 < this.m_Size; ++index1)
{
for (int index2 = 0; index2 < this.m_Size; ++index2)
this.m_Strength[index1 * this.m_Size + index2] = brushTex.GetPixelBilinear(((float) index2 + 0.5f) / num, (float) index1 / num).a;
}
}
else
{
for (int index = 0; index < this.m_Strength.Length; ++index)
this.m_Strength[index] = 1f;
}
Object.DestroyImmediate((Object) this.m_Preview);
this.m_Preview = new Texture2D(this.m_Size, this.m_Size, TextureFormat.ARGB32, false);
this.m_Preview.hideFlags = HideFlags.HideAndDontSave;
this.m_Preview.wrapMode = TextureWrapMode.Repeat;
this.m_Preview.filterMode = UnityEngine.FilterMode.Point;
Color[] colors = new Color[this.m_Size * this.m_Size];
for (int index = 0; index < colors.Length; ++index)
colors[index] = new Color(1f, 1f, 1f, this.m_Strength[index]);
this.m_Preview.SetPixels(0, 0, this.m_Size, this.m_Size, colors, 0);
this.m_Preview.Apply();
if ((Object) this.m_BrushProjector == (Object) null)
this.CreatePreviewBrush();
this.m_BrushProjector.material.mainTexture = (Texture) this.m_Preview;
this.m_Brush = brushTex;
return true;
}
this.m_Strength = new float[1];
this.m_Strength[0] = 1f;
this.m_Size = 1;
return false;
}
示例13: GenerateNormalsMap
private void GenerateNormalsMap(Texture2D horizontals, Texture2D verticals)
{
// Create the asset if not existing yet
if (OutputTexture == null)
{
OutputTexture = new Texture2D(Mathf.Max(horizontals.width, verticals.width), Mathf.Max(horizontals.height, verticals.height), TextureFormat.RGBA32, false);
}
// Actual map generation
Color[] outputPixels = new Color[OutputTexture.width * OutputTexture.height];
float uNormalized = 0.0f, vNormalized = 0.0f;
Color ch, cv;
float r, g, b;
Vector2 xyNormal;
Vector3 normal;
for (int v = 0; v < OutputTexture.height; ++v)
{
vNormalized = (float)v / OutputTexture.height;
for (int u = 0; u < OutputTexture.width; ++u)
{
uNormalized = (float)u / OutputTexture.width;
ch = horizontals.GetPixelBilinear(uNormalized, vNormalized);
r = ch.r * 2.0f - 1.0f;
cv = verticals.GetPixelBilinear(uNormalized, vNormalized);
g = cv.g * 2.0f - 1.0f;
xyNormal = Vector2.ClampMagnitude(new Vector2(r, g), 0.999f);
r = xyNormal.x;
g = xyNormal.y;
b = (float)Math.Sqrt(1.0f - (double)(r * r) - (double)(g * g)); // z = sqrt (1 - x^2 - y^2)
normal = new Vector3(r, g, b).normalized;
outputPixels[u + v * OutputTexture.width] = new Color(normal.x * 0.5f + 0.5f, normal.y * 0.5f + 0.5f, normal.z * 0.5f + 0.5f, 1.0f);
}
}
OutputTexture.SetPixels(outputPixels);
OutputTexture.Apply();
File.WriteAllBytes(Path.GetDirectoryName(Application.dataPath) + "/Assets/SpriteNormals.png", OutputTexture.EncodeToPNG());
AssetDatabase.ImportAsset("Assets/SpriteNormals.png");
OutputTexture = AssetDatabase.LoadMainAssetAtPath("Assets/SpriteNormals.png") as Texture2D;
}
示例14: RescaleTexture
// Rescale a texture
// Only supports
public static Texture2D RescaleTexture(Texture2D texture, float scale) {
// If globalTextureRescale is 0.5 or 0.25, average pixels from the larger image. Otherwise just pick one pixel, and look really bad
int niceRescaleK = NiceRescaleK( scale );
bool niceRescale = niceRescaleK != 0;
if (texture != null) {
int k = niceRescaleK;
int srcW = texture.width, srcH = texture.height;
int dstW = niceRescale ? ((srcW + k - 1) / k) : (int)(srcW * scale);
int dstH = niceRescale ? ((srcH + k - 1) / k) : (int)(srcH * scale);
Texture2D dstTex = new Texture2D(dstW, dstH);
for (int dstY = 0; dstY < dstH; ++dstY) {
for (int dstX = 0; dstX < dstW; ++dstX) {
if (niceRescale) {
Color sumColor = new Color(0, 0, 0, 0);
float w = 0.0f;
for (int dy = 0; dy < k; ++dy) {
int srcY = dstY * k + dy;
if (srcY >= srcH) continue;
for (int dx = 0; dx < k; ++dx) {
int srcX = dstX * k + dx;
if (srcX >= srcW) continue;
w += 1.0f;
Color srcColor = texture.GetPixel(srcX, srcY);
sumColor += srcColor;
}
}
dstTex.SetPixel(dstX, dstY, (w > 0.0f) ? (sumColor * (1.0f / w)) : Color.black);
} else {
dstTex.SetPixel(dstX, dstY, texture.GetPixelBilinear((float)dstX / (float)dstW, (float)dstY / (float)dstH));
}
}
}
dstTex.Apply();
return dstTex;
}
else {
return null;
}
}
示例15: DrawTexture
public static void DrawTexture (Texture2D texture, Rect previewArea, float textureSize, bool useAlpha = false)
{
int width = (int)textureSize;
int height = (int)textureSize;
Vector2 blockSize = new Vector2 (previewArea.width / (float)(width), previewArea.height / (float)(height));
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
float horizontalPercent = (float)x / (textureSize - 1f);
float verticalPercent = (float)y / (textureSize - 1f);
Rect blockRect = new Rect (previewArea.x + blockSize.x * x, previewArea.y + blockSize.y * y, blockSize.x, blockSize.y);
Color blockColor = texture.GetPixelBilinear(horizontalPercent, verticalPercent);
if (!useAlpha)
{
blockColor.a = 1f;
}
EditorGUI.DrawRect(blockRect, blockColor);
}
}
}