遊戲中圖形的表現大多來自於紋理,紋理使用最多的是Texture2D,Texture2D繼承自Texture,Texture裏面大多是對紋理的一些參數的設置,那些設置對於2d3d紋理是通用的,本篇就來看看texture2D具體是如何工作的。
using System;
using System.Runtime.CompilerServices;
using UnityEngine.Internal;
namespace UnityEngine
{
public
sealed class Texture2D : Texture
{
獲取紋理的級數,這個級數首先是開啓了mipmap,讀取一張圖片時,計算機會生成多張紋理,根據觀察者與物體的遠近選擇不同的紋理。
public
extern int mipmapCount
{
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
獲取紋理格式,比如RGBA32什麼的
public
extern TextureFormat format
{
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
這是一個靜態方法,獲取一個純白色的紋理,測試了一下,該紋理的尺寸爲(0.3,0.3)
public
static extern Texture2D whiteTexture
{
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
也是個靜態方法,獲取一個純黑色紋理,注意的是透明度也是0
public
static extern Texture2D blackTexture
{
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
創建一個自定義尺寸的紋理,當然其他都是默認值
public
Texture2D(int width,
int height)
{
Texture2D.Internal_Create(this, width, height, TextureFormat.ARGB32,
true,
false, IntPtr.Zero);
}
通過限制尺寸,像素格式,是否多級別紋理來創建一個紋理
public
Texture2D(int width,
int height, TextureFormat format,
bool mipmap)
{
Texture2D.Internal_Create(this, width, height, format, mipmap,
false, IntPtr.Zero);
}
通過限制尺寸,像素格式,是否多級別紋理,是否使用線性濾波方式來創建一個紋理
public
Texture2D(int width,
int height, TextureFormat format,
bool mipmap,
bool linear)
{
Texture2D.Internal_Create(this, width, height, format, mipmap, linear, IntPtr.Zero);
}
與上面的創建方式不一樣的地方是,傳入了一個本地的紋理,利用本地紋理創建一個新的紋理,而這個nativeTex可以通過Texture中GetNativeTextureID獲得
internal
Texture2D(int width,
int height, TextureFormat format,
bool mipmap,
bool linear, IntPtr nativeTex)
{
Texture2D.Internal_Create(this, width, height, format, mipmap, linear, nativeTex);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void Internal_Create([Writable] Texture2D mono,
int width,
int height, TextureFormat format,
bool mipmap, bool linear, IntPtr nativeTex);
public
static Texture2D CreateExternalTexture(int width,
int height, TextureFormat format,
bool mipmap,
bool linear, IntPtr nativeTex)
{
return
new Texture2D(width, height, format, mipmap, linear, nativeTex);
}
利用紋理id來更新當前紋理
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern void
UpdateExternalTexture(IntPtr nativeTex);
設置紋理中指定座標的顏色值,注意之後必須調用apply才起作用
public
void SetPixel(int x,
int y, Color color)
{
Texture2D.INTERNAL_CALL_SetPixel(this, x, y,
ref color);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void INTERNAL_CALL_SetPixel(Texture2D self,
int x,
int y, ref Color color);
獲取像素值
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern Color GetPixel(int x,
int y);
通過uv座標值獲取像素
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern Color GetPixelBilinear(float u,
float v);
[ExcludeFromDocs]
public
void SetPixels(Color[] colors)
{
int miplevel =
0;
this.SetPixels(colors, miplevel);
}
public
void SetPixels(Color[] colors, [DefaultValue("0")]
int miplevel)
{
int num =
this.width >> miplevel;
if (num <
1)
{
num = 1;
}
int num2 =
this.height >> miplevel;
if (num2 <
1)
{
num2 = 1;
}
this.SetPixels(0,
0, num, num2, colors, miplevel);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern void
SetPixels(int x,
int y,
int blockWidth, int blockHeight, Color[] colors, [DefaultValue("0")]
int miplevel);
[ExcludeFromDocs]
public
void SetPixels(int x,
int y,
int blockWidth, int blockHeight, Color[] colors)
{
int miplevel =
0;
this.SetPixels(x, y, blockWidth, blockHeight, colors, miplevel);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern void
SetPixels32(Color32[] colors, [DefaultValue("0")]
int miplevel);
[ExcludeFromDocs]
public
void SetPixels32(Color32[] colors)
{
int miplevel =
0;
this.SetPixels32(colors, miplevel);
}
通過讀取文件數據來加載紋理數據
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern bool
LoadImage(byte[] data);
通過讀取文件數據來加載紋理數據
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern void
LoadRawTextureData(byte[] data);
[ExcludeFromDocs]
public Color[]
GetPixels()
{
int miplevel =
0;
return
this.GetPixels(miplevel);
}
public Color[]
GetPixels([DefaultValue("0")]
int miplevel)
{
int num =
this.width >> miplevel;
if (num <
1)
{
num = 1;
}
int num2 =
this.height >> miplevel;
if (num2 <
1)
{
num2 = 1;
}
return
this.GetPixels(0,
0, num, num2, miplevel);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern Color[] GetPixels(int x,
int y,
int blockWidth, int blockHeight, [DefaultValue("0")]
int miplevel);
[ExcludeFromDocs]
public Color[]
GetPixels(int x,
int y,
int blockWidth, int blockHeight)
{
int miplevel =
0;
return
this.GetPixels(x, y, blockWidth, blockHeight, miplevel);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern Color32[] GetPixels32([DefaultValue("0")]
int miplevel);
[ExcludeFromDocs]
public Color32[]
GetPixels32()
{
int miplevel =
0;
return
this.GetPixels32(miplevel);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern void
Apply([DefaultValue("true")]
bool updateMipmaps, [DefaultValue("false")]
bool makeNoLongerReadable);
[ExcludeFromDocs]
public
void Apply(bool updateMipmaps)
{
bool makeNoLongerReadable =
false;
this.Apply(updateMipmaps, makeNoLongerReadable);
}
[ExcludeFromDocs]
public
void Apply()
{
bool makeNoLongerReadable =
false;
bool updateMipmaps =
true;
this.Apply(updateMipmaps, makeNoLongerReadable);
}
重新設定紋理參數
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern bool
Resize(int width,
int height, TextureFormat format,
bool hasMipMap);
public
bool Resize(int width,
int height)
{
return
this.Internal_ResizeWH(width, height);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
extern bool
Internal_ResizeWH(int width,
int height);
決定紋理的質量
public
void Compress(bool highQuality)
{
Texture2D.INTERNAL_CALL_Compress(this, highQuality);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void INTERNAL_CALL_Compress(Texture2D self,
bool highQuality);
傳進來一個紋理集合,把多個紋理打包爲當前紋理
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern Rect[] PackTextures(Texture2D[] textures,
int padding, [DefaultValue("2048")]
int maximumAtlasSize, [DefaultValue("false")]
bool makeNoLongerReadable);
傳進來一個紋理集合,把多個紋理打包爲當前紋理
[ExcludeFromDocs]
public Rect[]
PackTextures(Texture2D[] textures,
int padding, int maximumAtlasSize)
{
bool makeNoLongerReadable =
false;
return
this.PackTextures(textures, padding, maximumAtlasSize, makeNoLongerReadable);
}
傳進來一個紋理集合,把多個紋理打包爲當前紋理
[ExcludeFromDocs]
public Rect[]
PackTextures(Texture2D[] textures,
int padding)
{
bool makeNoLongerReadable =
false;
int maximumAtlasSize =
2048;
return
this.PackTextures(textures, padding, maximumAtlasSize, makeNoLongerReadable);
}
public
void ReadPixels(Rect source,
int destX,
int destY, [DefaultValue("true")]
bool recalculateMipMaps)
{
Texture2D.INTERNAL_CALL_ReadPixels(this,
ref source, destX, destY, recalculateMipMaps);
}
[ExcludeFromDocs]
public
void ReadPixels(Rect source,
int destX,
int destY)
{
bool recalculateMipMaps =
true;
Texture2D.INTERNAL_CALL_ReadPixels(this,
ref source, destX, destY, recalculateMipMaps);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void INTERNAL_CALL_ReadPixels(Texture2D self,
ref Rect source,
int destX, int destY,
bool recalculateMipMaps);
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern byte[]
EncodeToPNG();
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern byte[]
EncodeToJPG(int quality);
public
byte[] EncodeToJPG()
{
return
this.EncodeToJPG(75);
}
}
}