UnityAPI.Material材质(Yanlz+Unity+API+Material+color+mainTexture+shader+Lerp+SetColor+SetTexture+立钻哥哥)

UnityAPI.Material材质

版本

作者

参与者

完成日期

备注

UnityAPI_Material_V01_1.0

严立钻

 

2018.08.18

 

 

 

 

 

 

 

 

 

#《UnityAPI.Material材质》发布说明:

++++“UnityAPI.Material材质”是对UnityAPIMaterial类的剖析和拓展;

 

 

 

 

 

#Material材质

#Material材质

#Material材质

++++A、Description描述

++++B、Variables变量

++++C、Public Function共有函数

++++D、Message消息

 

 

 

 

 

#A、Description描述

#A、Description描述

++A、Description描述

++++A.1、材质类

++++A.2、此类暴露了一个材质的所有属性,允许对它们进行动画处理;(也可以使用它来设置自定义的Shader属性,但是无法通过检视面板访问属性(例如矩阵))

++++A.3、为了获得一个对象使用的材质,可以使用Renderer.material属性

 

 

 

 

 

 

 

#B、Variables变量

#B、Variables变量

++B、Variables变量

++++B.1、color 使用GetColor或SetColor设置_Color是一样的

++++B.2、globalIlluminationFlags 定义材质应与光照贴图和灯光探测器怎样相互作用

++++B.3、mainTexture 与使用GetTexture或SetTexture设置_MainTex是一样的

++++B.4、mainTextureOffset 主纹理的纹理偏移值

++++B.5、mainTextureScale 主纹理的纹理缩放值

++++B.6、passCount 在这个材质中有多少pass(只读)

++++B.7、renderQueue 该材质的渲染队列

++++B.8、shader 用于材质的着色器

++++B.9、shaderKeywords 由该材质设置的其他着色器关键词

 

 

 

 

##B.1、color 使用GetColor或SetColor设置_Color是一样的

##B.1、color 使用GetColor或SetColor设置_Color是一样的

++B.1、color 使用GetColor或SetColor设置_Color是一样的

++++立钻哥哥:Material.color 颜色

public Color color;

 

++Description描述

++++立钻哥哥:主材质的颜色

++++使用GetColorSetColor设置_Color是一样的;

using UnityEngine;

using System.Collection;

 

public class MyMaterialClass : MonoBehaviour{

    public Color colorStart = Color.red;

    public Color colorEnd = Color.green;

    public float duration = 1.0F;

    public Renderer rend;

 

    void Start(){

        rend = GetComponent<Renderer>();

    }

 

    void Update(){

        float lerp = Mathf.PingPong(Time.time, duration) / duration;

        rend.material.color = Color.Lerp(colorStart, colorEnd, lerp);

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##B.2、globalIlluminationFlags 定义材质应与光照贴图和灯光探测器怎样相互作用

##B.2、globalIlluminationFlags 定义材质应与光照贴图和灯光探测器怎样相互作用

++B.2、globalIlluminationFlags 定义材质应与光照贴图和灯光探测器怎样相互作用

++++立钻哥哥:Material.globalIlluminationFlags 全局光照表示

public MaterialGlobalIlluminationFlags gloablIlluminationFlags;

 

++Description描述

++++立钻哥哥:定义材质应与光照贴图和灯光探测器怎样相互作用

++++一个自定义的着色器GUI通常会基于输入设置这些值;

namespace UnityEngine{

    [Flags]

    public enum MaterialGlobalIlluminationFlags{

        None = 0,

        RealtimeEmissive = 1,

        BackedEmissive = 2,

        EmissiveIsBlack = 4

    }    //立钻哥哥:public enum MaterialGlobalIlluminationFlags{}

 

}    //立钻哥哥:namespace UnityEngine{}

 

 

 

 

 

##B.3、mainTexture 与使用GetTexture或SetTexture设置_MainTex是一样的

##B.3、mainTexture 与使用GetTexture或SetTexture设置_MainTex是一样的

++B.3、mainTexture 与使用GetTexture或SetTexture设置_MainTex是一样的

++++立钻哥哥:Material.mainTexture 主纹理

public Texture mainTexture;

++Description描述

++++立钻哥哥:主材质的纹理

++++与使用GetTextureSetTexture设置_MainTex是一样的;

//Change renderers texture each changeInterval seconds from the texture array defined in the inspector.

using UnityEngine;

using System.Collections;

 

public class MyMaterialClass : MonoBehaviour{

    public Texture[] textures;

    public float changeInterval = 0.33F;

    public Renderer rend;

 

    void Start(){

        rend = GetComponent<Renderer>();

    }

 

    void Update(){

        if(textures.Length == 0){

            return;

        }

 

        int index = Mathf.FloorToInt(Time.time / changeInterval);

        index = index % textures.Length;

        rend.material.mainTexture = textures[index];

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##B.4、mainTextureOffset 主纹理的纹理偏移值

##B.4、mainTextureOffset 主纹理的纹理偏移值

++B.4、mainTextureOffset 主纹理的纹理偏移值

++++立钻哥哥:Material.mainTextureOffset 主纹理偏移

public Vector2 mainTextureOffset;

++Description描述

++++立钻哥哥:主纹理的纹理偏移值

++++使用GetTextureOffsetSetTextureOffset设置_MainTex是一样的;

using UnityEngine;

using System.Collections;

 

public class MyMaterialClass : MonoBehaviour{

    public float scrollSpeed = 0.5F;

    public Renderer rend;

 

    void Start(){

        rend = GetComponent<Renderer>();

    }

 

    void Update(){

        float offset = Time.time * scrollSpeed;

        rend.material.mainTextureOffset = new Vector2(offset, 0);

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##B.5、mainTextureScale 主纹理的纹理缩放值

##B.5、mainTextureScale 主纹理的纹理缩放值

++B.5、mainTextureScale 主纹理的纹理缩放值

++++立钻哥哥:Material.mainTextureScale 主纹理缩放

public Vector2 mainTextureScale;

++Description描述

++++立钻哥哥:主纹理的纹理缩放值

++++GetTextureScaleSetTextureScale设置_MainTex是一样的;

using UntiyEngine;

using System.Collections;

 

public class MyMaterialClass : MonoBehaviour{

    public Renderer rend;

 

    void Start(){

        rend = GetComponent<Renderer>();

    }

 

    void Update(){

        float scaleX = Mathf.Cos(Time.time) * 0.5F + 1;

        float scaleY = Mathf.Sin(Time.time) * 0.5F + 1;

        rend.material.mainTextureScale = new Vector2(scaleX, scaleY);

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##B.6、passCount 在这个材质中有多少pass(只读)

##B.6、passCount 在这个材质中有多少pass(只读)

++B.6、passCount 在这个材质中有多少pass(只读)

++++立钻哥哥:Material.passCount 材质通道数

public int passCount;

 

++Description描述

++++立钻哥哥:在这个材质中有多少pass(只读)

++++这主要用于使用GL类直接绘制代码中。

--例如,ImageEffects使用材质实现屏幕后期处理;

--在材质中的每一个Pass都会**并绘制全屏四边形;

 

 

 

 

 

##B.7、renderQueue 该材质的渲染队列

##B.7、renderQueue 该材质的渲染队列

++B.7、renderQueue 该材质的渲染队列

++++立钻哥哥:Material.renderQueue 渲染队列

public int renderQueue;

 

++Description描述

++++立钻哥哥:该材质的渲染队列

++++默认的材质使用shader的渲染队列,可以使用这个变量重写渲染队列,但是请注意:一旦在材质上设置了渲染队列,即使shader在将来被改为不同的值,它仍会保持原来设置的值;

++++渲染队列的值必须是正值才能正常工作;(如是-1使用shader的渲染队列)

 

 

 

 

 

 

##B.8、shader 用于材质的着色器

##B.8、shader 用于材质的着色器

++B.8、shader 用于材质的着色器

++++立钻哥哥:Material.shader 着色器

public Shader shader;

++Description描述

++++立钻哥哥:用于材质的着色器

using UnityEngine;

using System.Collections;

 

public class MyMaterialClass : MonoBehaviour{

    public Shader shader1;

    public Shader shader2;

    public Renderer rend;

 

    void Start(){

        rend = GetComponent<Renderer>();

        shader1 = Shader.Find(Diffuse);

        shader2 = Shader.Find(Transparent/Diffuse);

    }

 

    void Update(){

        if(Input.GetButtonDown(Jump)){

            if(rend.material.shader == shader1){

                rend.material.shader = shader2;

            }else{

                rend.material.shader = shader1;

            }

        }

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##B.9、shaderKeywords 由该材质设置的其他着色器关键词

##B.9、shaderKeywords 由该材质设置的其他着色器关键词

++B.9、shaderKeywords 由该材质设置的其他着色器关键词

++++立钻哥哥:Material.shaderKeywords 着色器关键词列表

public string[] shaderKeywords;

++Description描述

++++立钻哥哥:由该材质设置的其他着色器关键词

 

 

 

 

 

 

 

 

#C、Public Function共有函数

#C、Public Function共有函数

++C、Public Function共有函数

++++C.1、CopyPropertiesFromMaterial() 从其他材质复制属性到该材质

++++C.2、DisableKeyword() 不设置一个着色器关键词

++++C.3、EnableKeyword() 设置一个由该材质启用的着色器关键词

++++C.4、GetColor() 获取已命名的颜色值

++++C.5、GetFloat() 获取已命名的浮点数

++++C.6、GetInt() 获取已命名的整数值

++++C.7、GetMatrix() 从这个shader中获取命名矩阵的值

++++C.8、GetTag() 获取材质着色器的标签值

++++C.9、GetTexture() 获取已命名的纹理

++++C.10、GetTextureOffset() 获取纹理propertyName的偏移值

++++C.11、GetTextureScale() 获取propertyName纹理的位置缩放值

++++C.12、GetVector() 获取已命名的向量值

++++C.13、HasProperty() 检查材质的shader是否有给定名称的属性

++++C.14、IsKeywordEnabled() 该材质的Shader关键词是否启用

++++C.15、Lerp() 在两个材质之间插值

++++C.16、SetBuffer() 设置ComputeBuffer的值

++++C.17、SetColor() 设置已命名的颜色值

++++C.18、SetFloat() 设置已命名的浮点数值

++++C.19、SetInt() 设置已命名的整数值

++++C.20、SetMatrix() 为这个shader设置一个命名矩阵

++++C.21、SetOverrideTag() 设置该材质的重写标签

++++C.22、SetPass() 为渲染**给定的pass

++++C.23、SetTexture() 设置已命名的纹理

++++C.24、SetTextureOffset() 设置纹理propertyName的偏移量

++++C.25、SetTextureScale() 设置纹理propertyName的缩放量

++++C.26、SetVector() 设置已命名的向量值

 

 

 

 

##C.1、CopyPropertiesFromMaterial() 从其他材质复制属性到该材质

##C.1、CopyPropertiesFromMaterial() 从其他材质复制属性到该材质

++C.1、CopyPropertiesFromMaterial() 从其他材质复制属性到该材质

++++立钻哥哥:Material.CopyPropertiesFromMaterial 从材质复制属性

public void CopyPropertiesFromMaterial(Material mat);

 

++Description描述

++++立钻哥哥:从其他材质复制属性到该材质

++++这个函数从其他材质复制属性值(序列化,并在运行时设置),以及着色器关键词、渲染队列和全局光照标识;(材质的着色器不会被改变)

using UnityEngine;

using System.Collections;

 

public class MyMaterialClass : MonoBehaviour{

    public Material mat;

 

    void Start(){

        GetComponent<Renderer>().material.CopyPropertiesFromMaterial(mat);

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##C.2、DisableKeyword() 不设置一个着色器关键词

##C.2、DisableKeyword() 不设置一个着色器关键词

++C.2、DisableKeyword() 不设置一个着色器关键词

++++立钻哥哥:Material.DisableKeyword 禁用关键词

public void DisableKeyword(string keyword);

 

++Description描述

++++立钻哥哥:不设置一个着色器关键词

++++着色器可以在内部编译成多个变体,然后基于材质关键词挑选匹配的(EnableKeyword and DisableKeyword),或全局设置着色器关键词(Shader.EnableKeyword and Shader.DisableKeyword

 

 

 

 

 

##C.3、EnableKeyword() 设置一个由该材质启用的着色器关键词

##C.3、EnableKeyword() 设置一个由该材质启用的着色器关键词

++C.3、EnableKeyword() 设置一个由该材质启用的着色器关键词

++++立钻哥哥:Material.EnableKeyword 启用关键词

public void EnableKeyword(string keyword);

 

++Description描述

++++立钻哥哥:设置一个由该材质启用的着色器关键词

 

 

 

 

 

##C.4、GetColor() 获取已命名的颜色值

##C.4、GetColor() 获取已命名的颜色值

++C.4、GetColor() 获取已命名的颜色值

++++立钻哥哥:Material.GetColor 获取颜色

public Color GetColor(string propertyName);

public Color GetColor(int nameID);

 

++Description描述

++++立钻哥哥:获取已命名的颜色值

++++许多着色器使用超过一种颜色;(使用GetColor来获得名propertyName的颜色)

++++Unity内置着色器使用的通用的颜色名称:

--“_Color”是材质的主颜色,也可以通过color属性访问;

--“_SpecColor”是材质的高光颜色(使用在specular/glossy/vertexlit着色器上);

--“_Emission”是材质的散射色(使用在vertexlit着色器上);

--“_ReflectColor”是材质的反射颜色(使用在reflective着色器上);

 

 

 

 

 

##C.5、GetFloat() 获取已命名的浮点数

##C.5、GetFloat() 获取已命名的浮点数

++C.5、GetFloat() 获取已命名的浮点数

++++立钻哥哥:Material.GetFloat 获取浮点数

public float GetFloat(string propertyName);

public float GetFloat(int nameID);

 

++Description描述

++++立钻哥哥:获取一个命名的浮点数

using UnityEngine;

using System.Collections;

 

public class MyMaterialClass : MonoBehaviour{

    void Start(){

        Renderer rend = GetComponent<Renderer>();

        rend.material.shader = Shader.Find(Specular);

 

        print(rend.material.GetFloat(_Shininess));

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##C.6、GetInt() 获取已命名的整数值

##C.6、GetInt() 获取已命名的整数值

++C.6、GetInt() 获取已命名的整数值

++++立钻哥哥:Material.GetInt 获取整数

public int GetInt(string propertyName);

public int GetInt(int nameID);

 

++Description描述

++++立钻哥哥:获取已命名的整数值

++++这个函数仅是获取GetFloat结果值变为整数;

 

 

 

 

 

##C.7、GetMatrix() 从这个shader中获取命名矩阵的值

##C.7、GetMatrix() 从这个shader中获取命名矩阵的值

++C.7、GetMatrix() 从这个shader中获取命名矩阵的值

++++立钻哥哥:Material.GetMatrix 获取矩阵

public Matrix4x4 GetMatrix(string propertyName);

public Matrix4x4 GetMatrix(int nameID);

 

++Description描述

++++立钻哥哥:从这个shader中获取命名矩阵的值

++++这主要用于自定义着色器需要额外矩阵参数。(矩阵参数不会将其公开在材质的检视面板中,但可以在脚本中通过SetMatrixGetMatrix设置和查询)

 

 

 

 

 

##C.8、GetTag() 获取材质着色器的标签值

##C.8、GetTag() 获取材质着色器的标签值

++C.8、GetTag() 获取材质着色器的标签值

++++立钻哥哥:Material.GetTag 获取标签

public string GetTag(string tag, bool searchFallbacks, string defaultValue = “”);

 

++Description描述

++++立钻哥哥:获取材质着色器的标签值

++++如果材质的着色器未定义标签,则返回默认值;

++++如果searchFallbackstrue,此函数将查找所有的subshader和所有的后备标签;(如果searchFallbacksfalse,此函数仅在当前使用的subshader中查询标签)

++++使用没有后备搜索的GetTag,检测当前正在使用的subshader:将一个自定义的标签添加到每个具有不同的值的subshader和查询在运行时的值;

--例如Unity的水使用这个函数来检测着色器何时将退回无反射,并使用此函数在这种情况下关闭反射相机;

using UnityEngine;

using System.Collections;

 

public class MyMaterialClass : MonoBehaviour{

    public string materialTag = RenderType;

 

    void Start(){

        Renderer rend = GetComponent<Renderer>();

        string result = rend.material.GetTag(materialTag, true, Nothing);

    

        if(result == Nothing){

            Debug.LogError(materialTag +  not found in  + rend.material.shader.name);

        }else{

            Debug.Log(立钻哥哥:Tag found! Its value:  + result);

        }

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##C.9、GetTexture() 获取已命名的纹理

##C.9、GetTexture() 获取已命名的纹理

++C.9、GetTexture() 获取已命名的纹理

++++立钻哥哥:Material.GetTexture 获取纹理

public Texture GetTexture(string propertyName);

public Texture GetTexture(int nameID);

 

++Description描述

++++立钻哥哥:获取已命名的纹理

++++大部分shader使用不只一个纹理;(使用GetTexture来获取名为propertyName的纹理)

++++Unity内置的shader通用纹理名有:

--“_MainTex”是主要的漫反射纹理,也能通过mainTexture属性访问;

--“_BumpMap”是法线贴图;

--“_Cube”是反射cubemap(立方体贴图)

 

 

 

 

 

##C.10、GetTextureOffset() 获取纹理propertyName的偏移值

##C.10、GetTextureOffset() 获取纹理propertyName的偏移值

++C.10、GetTextureOffset() 获取纹理propertyName的偏移值

++++立钻哥哥:Material.GetTextureOffset 获取纹理偏移

public Vector2 GetTextureOffset(string propertyName);

 

++Description描述

++++立钻哥哥:获取纹理propertyName的偏移值

++++在Unity着色器中使用的通用的纹理名称:

--“_MainTex”是主要的漫反射纹理,也能通过mainTextureOffset属性访问;

--“_BumpMap”是法线贴图;

--“_Cube”是反射cubemap(立方体贴图)

 

 

 

 

 

##C.11、GetTextureScale() 获取propertyName纹理的位置缩放值

##C.11、GetTextureScale() 获取propertyName纹理的位置缩放值

++C.11、GetTextureScale() 获取propertyName纹理的位置缩放值

++++立钻哥哥:Material.GetTextureScale 获取纹理缩放

public Vector2 GetTextureScale(string propertyName);

++Description描述

++++立钻哥哥:获取propertyName纹理的位置缩放值

++++Unity内置的shader通用纹理名有:

--“_MainTex”是主要的漫反射纹理,也能通过mainTexture属性访问;

--“_BumpMap”是法线贴图;

--“_Cube”是反射cubemap(立方体贴图)

 

 

 

 

 

##C.12、GetVector() 获取已命名的向量值

##C.12、GetVector() 获取已命名的向量值

++C.12、GetVector() 获取已命名的向量值

++++立钻哥哥:Material.GetVector 获取向量

public Vector4 GetVector(string propertyName);

public Vector4 GetVector(int nameID);

 

++Description描述

++++立钻哥哥:获取已命名的向量值

++++在Unity着色器中四维向量和颜色的结果是相同的;

    --GetVectorGetColor的不同之处在于只是输入的数据类型不同(xyzw向量转换成rgba颜色);

 

 

 

 

 

##C.13、HasProperty() 检查材质的shader是否有给定名称的属性

##C.13、HasProperty() 检查材质的shader是否有给定名称的属性

++C.13、HasProperty() 检查材质的shader是否有给定名称的属性

++++立钻哥哥:Material.HasProperty 是否有属性

public bool HasProperty(string propertyName);

public bool HasProperty(int nameID);

 

++Description描述

++++立钻哥哥:检查材质的shader是否有给定名称的属性

 

 

 

 

 

##C.14、IsKeywordEnabled() 该材质的Shader关键词是否启用

##C.14、IsKeywordEnabled() 该材质的Shader关键词是否启用

++C.14、IsKeywordEnabled() 该材质的Shader关键词是否启用

++++立钻哥哥:Material.IsKeywordEnabled 是否启用关键词

public bool IsKeywordEnable(string keyword);

 

++Description描述

++++立钻哥哥:该材质的shader关键字是否启用

 

 

 

 

 

##C.15、Lerp() 在两个材质之间插值

##C.15、Lerp() 在两个材质之间插值

++C.15、Lerp() 在两个材质之间插值

++++立钻哥哥:Material.Lerp 插值

public void Lerp(Material start, Material end, float t);

++Description描述

++++立钻哥哥:在两个材质之间插值

++++给一个材质所有颜色和浮点值作一个插值,从差值开始到结束都基于时间t;

++++时间t为0时,所有值为开始;

++++时间t为1时,所有值为结束;

++++通常,想要插值的两个材质是相同的(使用相同的着色器和纹理),除了颜色和浮点数;

--然后可以使用Lerp做它们之间的混合;

using UnityEngine;

using System.Collections;

 

public class MyMaterialClass : MonoBehaviour{

    public Material material1;

    public Material material2;

    public float duration = 2.0F;

    public Renderer rend;

 

    void Start(){

        rend = GetComponent<Renderer>();

        rend.material = material1;

    }

 

    void Update(){

        float lerp = Mathf.PingPong(Time.time, duration) / duration;

        rend.material.Lerp(material1, material2, lerp);

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##C.16、SetBuffer() 设置ComputeBuffer的值

##C.16、SetBuffer() 设置ComputeBuffer的值

++C.16、SetBuffer() 设置ComputeBuffer的值

++++立钻哥哥:Material.SetBuffer 设置缓冲区

public void SetBuffer(string propertyName, ComputeBuffer buffer);

 

++Description描述

++++立钻哥哥:设置ComputeBuffer的值

 

 

 

 

 

##C.17、SetColor() 设置已命名的颜色值

##C.17、SetColor() 设置已命名的颜色值

++C.17、SetColor() 设置已命名的颜色值

++++立钻哥哥:Material.SetColor 设置颜色

public void SetColor(string propertyName, Color color);

public void SetColor(int nameID, Color color);

 

++Description描述

++++立钻哥哥:设置已命名的颜色值

++++许多着色器使用超过一种颜色;(使用SetColor来更改propertyName颜色)

++++当设置材质使用Standard Shader的颜色值时,应该意识到,需要使用EnableKeyword来启用以前未使用过的shader的属性;

++++Unity内置着色器使用的共同的颜色名称:

--“_Color”是材质的主颜色,也可以通过color属性访问;

--“_SpecColor”是材质的高光颜色(使用在specular/glossy/vertexlit着色器上);

--“_Emission”是材质的散射色(使用在vertexlit着色器上);

--“_ReflectColor”是材质的反射颜色(使用在reflective着色器上);

 

 

 

 

 

##C.18、SetFloat() 设置已命名的浮点数值

##C.18、SetFloat() 设置已命名的浮点数值

++C.18、SetFloat() 设置已命名的浮点数值

++++立钻哥哥:Material.SetFloat 设置浮点数

public void SetFloat(string propertyName, float value);

public void SetFloat(int nameID, float value);

 

++Description描述

++++立钻哥哥:设置已命名的浮点数值

++++当设置材质使用Standard Shader的颜色值时,应意识到,需要使用EnableKeyword来启用以前未使用过的shader的属性;

using UnityEngine;

using System.Collections;

 

public class MyMaterialClass : MonoBehaviour{

    public Renderer rend;

 

    void Start(){

        rend = GetComponent<Renderer>();

        rend.material.shader = Shader.Find(Specular);

    }

 

    void Update(){

        float shininess = Mathf.PingPong(Time.time, 1.0F);

        rend.material.SetFloat(_Shininess, shininess);

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##C.19、SetInt() 设置已命名的整数值

##C.19、SetInt() 设置已命名的整数值

++C.19、SetInt() 设置已命名的整数值

++++立钻哥哥:Material.SetInt 设置整数

public void SetInt(string propertyName, int value);

public void SetInt(int nameID, int value);

 

++Description描述

++++立钻哥哥:设置已命名的整数值

++++当设置材质使用Standard Shader的颜色值时,应该意识到,需要使用EnableKeyword来启用以前未使用过的shader的属性;

++++这个函数仅是SetFloat传递的浮点数值;

 

 

 

 

 

##C.20、SetMatrix() 为这个shader设置一个命名矩阵

##C.20、SetMatrix() 为这个shader设置一个命名矩阵

++C.20、SetMatrix() 为这个shader设置一个命名矩阵

++++立钻哥哥:Material.SetMatrix 设置矩阵

public void SetMatrix(string propertyName, Matrix4x4 matrix);

public void SetMatrix(int nameID, Matrix4x4 matrix);

 

++Description描述

++++立钻哥哥:为这个shader设置一个命名矩阵

++++这主要用于自定义着色器需要额外矩阵参数;

--矩阵参数不会将其公开在材质的检视面板中,但可以在脚本中通过SetMatrixGetMatrix设置和查询;

using UnityEngine;

 

public class MyMaterialClass : MonoBehaviour{

    //Attach to an object that has a Renderer component, and use material with the shader below

    public float rotateSpeed = 30f;

 

    public void Update(){

        //Construct a rotation matrix and set it for the shader

        var rot = Quaternion.Euler(0, 0, Time.time * rotateSpeed);

        var m = Matrix4x4.TRS(Vector3.zero, rot, Vector3.one);

        GetComponent<Renderer>().material.SetMatrix(_TextureRotation, m);

    }

    

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

//Use this shader on an object together with the above example scripte.

//The shader transforms texture coordinates with a matrix set from a script.

Shader 立钻哥哥/RotatingTexture{

    Properties{

        _MainTex(Base (RGB), 2D) = white{}

    }

 

    SubShader{

        Pass{

            CGPROGRAM

            #pragam vertex vert

            #pargam fragment frag

        

            struct v2f{

                float2 uv : TEXCOORD0;

                float4 pos : SV_POSITION;

            };

 

            float4x4 _TextureRotation;

 

            v2f vert(float4 pos : POSITION, float2 uv : TEXCOORD0){

                v2f o;

                o.pos = mul(UNITY_MATRIX_MVP, pos);

                o.uv = mul(_TextureRotation, float4(uv, 0, 1)).xy;

                return o;

            }

 

            sampler2D _MainTex;

            fixed4 frag(v2f i) : SV_Target{

                return tex2D(_MainTex, i.uv);

            }

 

            ENDCG

        }    //立钻哥哥:Pass{}

 

    }    //立钻哥哥:SubShader{}

 

}    //立钻哥哥:Shader 立钻哥哥/RotatingTexture{}

 

 

 

 

 

##C.21、SetOverrideTag() 设置该材质的重写标签

##C.21、SetOverrideTag() 设置该材质的重写标签

++C.21、SetOverrideTag() 设置该材质的重写标签

++++立钻哥哥:Material.SetOverrideTag 设置重写标签

public void SetOverrideTag(string tag, string val);

++++[tag]:设置标签的名称;

++++[val]:设置值的名称;

 

++ Description描述

++++立钻哥哥:设置该材质的重写标签

++++这个会设置该材质的一个标签,从shader重写标签表示的值;

--这用来确保替换shader(如渲染深度法线),即使原始shader仅支持某一渲染类型;

--例如,如果shader仅支持一个指定的渲染类型,但使用关键词渲染多种方法,SetOverrideTag能使用自定义的材质检视面板来确保材质正确渲染,即使shader被替换;

public class MyMaterialClass : MonBehaviour{

    private Material material;

 

    public void SetupBlendMode(BlendMode blendMode){

        switch(blendMode){

            case BlendMode.Opaque:{

                material.SetOverrideTag(RenderType, “”);

                material.DisableKeyword(_ALPHATEST_ON);

                material.renderQueue = -1;

                break;

            }

 

            case BlendMode.Cutout:{

                material.SetOverrideTag(RenderType, TransparentCutout);

                material.EnableKeyword(_ALPHATEST_ON);

                material.renderQueue = 2450;

                break;

            }

        }

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##C.22、SetPass() 为渲染**给定的pass

##C.22、SetPass() 为渲染**给定的pass

++C.22、SetPass() 为渲染**给定的pass

++++立钻哥哥:Material.SetPass 设置通道

public bool SetPass(int pass);

 

++Description描述

++++立钻哥哥:为渲染**给定的pass

++++传递从0开始最大到(但不包括)passCount的索引;

++++这主要用于使用GL类直接绘图代码中(Unity专业版);

--比如图像效果使用执行屏幕后处理的材质;

--在材质中的每一个Pass都会**并绘制全屏四边形;

++++如果SetPass返回false,不会渲染任何东西;(在这种情况下指定的通道类型意味着不渲染,像GrabPass

using UnityEngine;

 

//A Script that when attached to the camera, makes the resulting colors inverted. See its effect in play mode

public class MyMaterialClass : MonoBehaviour{

    private Material mat;

 

    //Will be called from camera after regular rendering is done

    public void OnPostRender(){

        if(!mat){

            //Unity has a built-in shader that is useful for drawing simple colored things. In this case, we just want to use a blend mode that inverts destination colors.

            var shader = Shader.Find(Hidden/Internal-Colored);

            mat = new Material(shader);

            mat.hideFlags = HideFlags.HideAndDontSave;

 

            //Set blend mode to invert destination colors.

            mat.SetInt(_SrcBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusDstColor);

            mat.SetInt(_DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);

 

            //Turn off backface culling, depth writes, depth test.

            mat.SetInt(_Cull, (int)UnityEngine.Rendering.CullMode.Off);

            mat.SetInt(_ZWrite, 0);

            mat.SetInt(_ZTest, (int)UnityEngine.Rendering.CompareFunction.Always);

        }

 

        GL.PushMatrix();

        GL.LoadOrtho();

 

        //activate the first shader pass(in this case we know it is the only pass)

        mat.SetPass(0);

 

        //draw a quad over whole screen

        GL.Begin(GL.QUADS);

        GL.Vertex3(0, 0, 0);

        GL.Vertex3(1, 0, 0);

        GL.Vertex3(1, 1, 0);

        GL.Vertex3(0, 1, 0);

        GL.End();

 

        GL.PopMatrix();

 

    }    //立钻哥哥:public void OnPostRender(){}

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##C.23、SetTexture() 设置已命名的纹理

##C.23、SetTexture() 设置已命名的纹理

++C.23、SetTexture() 设置已命名的纹理

++++立钻哥哥:Material.SetTexture 设置纹理

public void SetTexture(string propertyName, Texture texture);

public void SetTexture(int nameID, Texture texture);

 

++Description描述

++++立钻哥哥:设置已命名的纹理

++++许多着色器都使用超过一张纹理,可以使用SetTexture改变propertyName的纹理;

++++当设置材质使用Standard Shader的颜色值时,应该意识到,需要使用EnableKeyword来启用以前未使用过的shader属性;

++++在Unity着色器中使用的统一的纹理名称:

--“_MainTex”是主要的漫反射纹理,也能通过mainTexture属性访问;

--“_BumpMap”是法线贴图;

--“_Cube”是反射cubempa(立方体贴图)

 

 

 

 

 

##C.24、SetTextureOffset() 设置纹理propertyName的偏移量

##C.24、SetTextureOffset() 设置纹理propertyName的偏移量

++C.24、SetTextureOffset() 设置纹理propertyName的偏移量

++++立钻哥哥:Material.SetTextureOffset 设置纹理偏移

public void SetTextureOffset(string propertyName, Vector2 offset);

 

++Description描述

++++立钻哥哥:设置纹理propertyName的偏移量

++++在Unity着色器中使用统一的纹理名称:

--“_MainTex”是主要的漫反射纹理,也能通过mainTextureOffset属性访问;

--“_BumpMap”是法线贴图;

--“_Cube”是反射cubemap(立方体贴图)

using UnityEngine;

using System.Collections;

 

public class MyMaterialClass : MonoBehaviour{

    public float scrollSpeed = 0.5F;

    public Renderer rend;

 

    void Start(){

        rend = GetComponent<Renderer>();

    }

 

    void Update(){

        float offset = Time.time * scrollSpeed;

        rend.material.SetTextureOffset(_MainTex, new Vector2(offset, 0));

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##C.25、SetTextureScale() 设置纹理propertyName的缩放量

##C.25、SetTextureScale() 设置纹理propertyName的缩放量

++C.25、SetTextureScale() 设置纹理propertyName的缩放量

++++立钻哥哥:Material.SetTextureScale 设置纹理缩放

public void SetTextureScale(string propertyName, Vector2 scale);

 

++Description描述

++++立钻哥哥:设置纹理propertyName的缩放量

++++在Unity着色器中使用统一的纹理名称:

--“_MainTex”是主要的漫反射纹理,也能通过mainTextureScale属性访问;

--“_BumpMap”是法线贴图;

--“_Cube”是反射cubemap(盒子贴图)

using UnityEngine;

using System.Collections;

 

public class MyMaterialClass : MonoBehaviour{

    public float scrollSpeed = 0.5F;

    public Renderer rend;

 

    void Start(){

        rend = GetComponent<Renderer>();

    }

 

    void Update(){

        float scaleX = Mathf.Cos(Time.time) * 0.5F + 1;

        float scaleY = Mathf.Sin(Time.time) * 0.5F + 1;

        rend.material.SetTextureScale(_MainTex, new Vector2(scaleX, scaleY));

    }

 

}    //立钻哥哥:public class MyMaterialClass:MonoBehaviour{}

 

 

 

 

 

##C.26、SetVector() 设置已命名的向量值

##C.26、SetVector() 设置已命名的向量值

++C.26、SetVector() 设置已命名的向量值

++++立钻哥哥:Material.SetVector 设置向量

public void SetVector(string propertyName, Vector4 vector);

public void SetVector(int nameID, Vector4 vector);

 

++Description描述

++++立钻哥哥:设置已命名的向量值

++++在Unity着色器中四维向量和颜色的结果是相同的;

--SetVectorSetColor的不同之处在于只是输入的数据类型不同(xyzw向量转换成rgba颜色);

 

 

 

 

 

 

 

#D、Message消息

#D、Message消息

 

 

 

 

 

 

 

 

 

++立钻哥哥推荐的拓展学习链接(Link_Url)

++++立钻哥哥Unity 学习空间: http://blog.csdn.net/VRunSoftYanlz/

++++Unity5.x用户手册http://www.javashuo.com/article/p-ufnzpmga-s.html

++++Unity面试题ABChttp://www.javashuo.com/article/p-mwacxwca-gm.html

++++Unity面试题Dhttp://www.javashuo.com/article/p-wuwcrclr-s.html

++++Unity面试题Ehttp://www.javashuo.com/article/p-hmabbtmc-ba.html

++++Unity面试题Fhttp://www.javashuo.com/article/p-olslkfao-cq.html

++++Unity知识点0001http://www.javashuo.com/article/p-ryvdxxjr-ep.html

++++Unity知识点0008http://www.javashuo.com/article/p-kxgstxls-gu.html

++++Unity引擎基础http://www.javashuo.com/article/p-beommoeb-ka.html

++++Unity面向组件开发http://www.javashuo.com/article/p-eigmuvut-dt.html

++++Unity物理系统http://www.javashuo.com/article/p-nqvvciwv-kd.html

++++Unity2D平台开发http://www.javashuo.com/article/p-ycaagdtj-hs.html

++++UGUI基础http://www.javashuo.com/article/p-rukxwckw-mc.html

++++UGUI进阶http://www.javashuo.com/article/p-wcatruhq-gt.html

++++UGUI综合http://www.javashuo.com/article/p-dkccmqii-gg.html

++++Unity动画系统基础http://www.javashuo.com/article/p-mbrdouxy-dq.html

++++Unity动画系统进阶http://www.javashuo.com/article/p-aqaqpbkh-bp.html

++++Navigation导航系统http://www.javashuo.com/article/p-dswwllas-t.html

++++Unity特效渲染http://www.javashuo.com/article/p-ckojjyfj-bp.html

++++Unity数据存储http://www.javashuo.com/article/p-bvlzynso-m.html

++++Unity中Sqlite数据库http://www.javashuo.com/article/p-ejutsbxl-ca.html

++++WWW类和协程http://www.javashuo.com/article/p-dbwmhsav-cy.html

++++Unity网络http://www.javashuo.com/article/p-sqrlntgh-dw.html

++++C#事件http://www.javashuo.com/article/p-zmwruvql-gm.html

++++C#委托http://www.javashuo.com/article/p-uozpymaf-gh.html

++++C#集合http://www.javashuo.com/article/p-sfqfdqsf-ex.html

++++C#泛型http://www.javashuo.com/article/p-xrttqngo-ee.html

++++C#接口http://www.javashuo.com/article/p-vhlfplgv-dm.html

++++C#静态类https://blog.csdn.net/vrunsoftyanlz/article/details/78630979

++++C#中System.String类http://www.javashuo.com/article/p-olslkfao-cq.html

++++C#数据类型http://www.javashuo.com/article/p-hmabbtmc-ba.html

++++Unity3D默认的快捷键http://www.javashuo.com/article/p-wuwcrclr-s.html

++++游戏相关缩写http://www.javashuo.com/article/p-mwacxwca-gm.html

++++设计模式简单整理http://www.javashuo.com/article/p-rngqugib-hg.html

++++专题:设计模式(精华篇)http://www.javashuo.com/article/p-nbohnaya-hw.html

++++U3D小项目参考https://blog.csdn.net/vrunsoftyanlz/article/details/80141811

++++UML类图http://www.javashuo.com/article/p-sxberuew-bm.html

++++U3D_Shader编程(第一篇:快速入门篇)http://www.javashuo.com/article/p-kyppgrac-gz.html

++++U3D_Shader编程(第二篇:基础夯实篇)http://www.javashuo.com/article/p-qkyowtli-hv.html

++++框架知识点http://www.javashuo.com/article/p-eufbowgf-u.html

++++游戏框架(UI框架夯实篇)http://www.javashuo.com/article/p-cvemoigb-cu.html

++++游戏框架(初探篇)http://www.javashuo.com/article/p-zfpoilbc-hy.html

++++Lua快速入门篇(基础概述)http://www.javashuo.com/article/p-shernvtt-u.html

++++Lua快速入门篇(XLua教程):http://www.javashuo.com/article/p-pduvmusb-ho.html

++++Lua快速入门篇(Xlua拓展):http://www.javashuo.com/article/p-rrszijom-cm.html

++++UnityAPI.Rigidbody刚体http://www.javashuo.com/article/p-phaztrtw-w.html

++++UnityAPI.Material材质http://www.javashuo.com/article/p-ntyoqcng-q.html

++++立钻哥哥Unity 学习空间: http://blog.csdn.net/VRunSoftYanlz/

--_--VRunSoft:Lovezuanzuan--_--