Material這個需要結合shader來講,計算機圖形學裏本身就沒有Material這個東西,引擎加入這個其實是在shader和主程序之間搭建了一座橋樑,可以說Material是一個着色器管理器,所以很多接口都是對shader的控制。
using System;
using System.Runtime.CompilerServices;
using UnityEngine.Internal;
namespace UnityEngine
{
public
class Material : Object
{
public
extern Shader shader
{
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
get;
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
set;
}
//這個是針對該material鎖控制的shader中_Color變量操作,獲取該顏色或設置該顏色
public Color color
{
get
{
return
this.GetColor("_Color");
}
set
{
this.SetColor("_Color",
value);
}
}
//針對該material鎖控制的shader中_MainTex變量操作,獲取該紋理或設置該紋理
public Texture mainTexture
{
get
{
return
this.GetTexture("_MainTex");
}
set
{
this.SetTexture("_MainTex",
value);
}
}
//shader中_MainTex的紋理偏移量
public Vector2 mainTextureOffset
{
get
{
return
this.GetTextureOffset("_MainTex");
}
set
{
this.SetTextureOffset("_MainTex",
value);
}
}
//_MainTex的紋理座標縮放,編輯器上就是對應Tiling這個屬性
public Vector2 mainTextureScale
{
get
{
return
this.GetTextureScale("_MainTex");
}
set
{
this.SetTextureScale("_MainTex",
value);
}
}
//shader中pass的數量
public
extern int passCount
{
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
//設置該材質的渲染隊列,對應了shader中的Queue,表示unity在什麼時候渲染自己,可選值:Background(1000), Geometry(2000), AlphaTest(2450), Transparent(3000), Overlay(4000)
public
extern int renderQueue
{
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
get;
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
set;
}
//這相當於是c語言中的宏定義列表,通過EnableKeyword,DisableKeyword決定使用不使用這些宏來控制shader執行哪部分代碼
public
extern string[] shaderKeywords
{
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
get;
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
set;
}
//使用shader裏面字符串創建一個材質
public
Material(string contents)
{
Material.Internal_CreateWithString(this, contents);
}
//使用shader創建一個材質
public
Material(Shader shader)
{
Material.Internal_CreateWithShader(this, shader);
}
//深拷貝一個材質
public
Material(Material source)
{
Material.Internal_CreateWithMaterial(this, source);
}
//設置shader中名爲propertyName的變量的值
public
void SetColor(string propertyName, Color color)
{
this.SetColor(Shader.PropertyToID(propertyName), color);
}
public
void SetColor(int nameID, Color color)
{
Material.INTERNAL_CALL_SetColor(this, nameID,
ref color);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void INTERNAL_CALL_SetColor(Material self,
int nameID,
ref Color color);
public Color
GetColor(string propertyName)
{
return
this.GetColor(Shader.PropertyToID(propertyName));
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern Color GetColor(int nameID);
//設置shader中名爲propertyName的變量的值
public
void SetVector(string propertyName, Vector4 vector)
{
this.SetColor(propertyName,
new
Color(vector.x, vector.y, vector.z, vector.w));
}
public
void SetVector(int nameID, Vector4 vector)
{
this.SetColor(nameID,
new
Color(vector.x, vector.y, vector.z, vector.w));
}
//設置shader中名爲propertyName的變量的值
public Vector4
GetVector(string propertyName)
{
Color color = this.GetColor(propertyName);
return
new Vector4(color.r, color.g, color.b, color.a);
}
public Vector4
GetVector(int nameID)
{
Color color = this.GetColor(nameID);
return
new Vector4(color.r, color.g, color.b, color.a);
}
//設置shader中名爲propertyName的變量的值
public
void SetTexture(string propertyName, Texture texture)
{
this.SetTexture(Shader.PropertyToID(propertyName), texture);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern void
SetTexture(int nameID, Texture texture);
public Texture
GetTexture(string propertyName)
{
return
this.GetTexture(Shader.PropertyToID(propertyName));
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern Texture GetTexture(int nameID);
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void Internal_GetTextureOffset(Material mat,
string name, out Vector2 output);
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void Internal_GetTextureScale(Material mat,
string name, out Vector2 output);
public
void SetTextureOffset(string propertyName, Vector2 offset)
{
Material.INTERNAL_CALL_SetTextureOffset(this, propertyName,
ref offset);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void INTERNAL_CALL_SetTextureOffset(Material self,
string propertyName,
ref Vector2 offset);
public Vector2
GetTextureOffset(string propertyName)
{
Vector2 result;
Material.Internal_GetTextureOffset(this, propertyName,
out result);
return result;
}
public
void SetTextureScale(string propertyName, Vector2 scale)
{
Material.INTERNAL_CALL_SetTextureScale(this, propertyName,
ref scale);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void INTERNAL_CALL_SetTextureScale(Material self,
string propertyName,
ref Vector2 scale);
public Vector2
GetTextureScale(string propertyName)
{
Vector2 result;
Material.Internal_GetTextureScale(this, propertyName,
out result);
return result;
}
//設置shader中名爲propertyName的變量的值爲矩陣
public
void SetMatrix(string propertyName, Matrix4x4 matrix)
{
this.SetMatrix(Shader.PropertyToID(propertyName), matrix);
}
public
void SetMatrix(int nameID, Matrix4x4 matrix)
{
Material.INTERNAL_CALL_SetMatrix(this, nameID,
ref matrix);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void INTERNAL_CALL_SetMatrix(Material self,
int nameID,
ref Matrix4x4 matrix);
//獲得shader中名爲propertyName的變量的值爲矩陣
public Matrix4x4
GetMatrix(string propertyName)
{
return
this.GetMatrix(Shader.PropertyToID(propertyName));
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern Matrix4x4 GetMatrix(int nameID);
public
void SetFloat(string propertyName,
float
value)
{
this.SetFloat(Shader.PropertyToID(propertyName),
value);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern void
SetFloat(int nameID,
float
value);
public
float GetFloat(string propertyName)
{
return
this.GetFloat(Shader.PropertyToID(propertyName));
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern float
GetFloat(int nameID);
public
void SetInt(string propertyName,
int
value)
{
this.SetFloat(propertyName, (float)value);
}
public
void SetInt(int nameID,
int
value)
{
this.SetFloat(nameID, (float)value);
}
public
int GetInt(string propertyName)
{
return (int)this.GetFloat(propertyName);
}
public
int GetInt(int nameID)
{
return (int)this.GetFloat(nameID);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern void
SetBuffer(string propertyName, ComputeBuffer buffer);
//shader是否存在propertyName這個變量
public
bool HasProperty(string propertyName)
{
return
this.HasProperty(Shader.PropertyToID(propertyName));
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern bool
HasProperty(int nameID);
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern string
GetTag(string tag,
bool searchFallbacks, [DefaultValue("\"\"")]
string defaultValue);
[ExcludeFromDocs]
public
string GetTag(string tag,
bool searchFallbacks)
{
string empty =
string.Empty;
return
this.GetTag(tag, searchFallbacks, empty);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern void
Lerp(Material start, Material end,
float t);
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern bool
SetPass(int pass);
[Obsolete("Use the Material constructor instead.")]
public
static Material Create(string scriptContents)
{
return
new Material(scriptContents);
}
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void Internal_CreateWithString([Writable] Material mono,
string contents);
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void Internal_CreateWithShader([Writable] Material mono, Shader shader);
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
private
static extern
void Internal_CreateWithMaterial([Writable] Material mono, Material source);
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern void
CopyPropertiesFromMaterial(Material mat);
//通過EnableKeyword,DisableKeyword決定使用不使用這些宏來控制shader執行哪部分代碼
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern void
EnableKeyword(string keyword);
//通過EnableKeyword,DisableKeyword決定使用不使用這些宏來控制shader執行哪部分代碼
[WrapperlessIcall]
[MethodImpl(MethodImplOptions.InternalCall)]
public
extern void
DisableKeyword(string keyword);
}
}