当前位置:网站首页>Unity method for setting material and shader
Unity method for setting material and shader
2022-06-26 21:56:00 【Peter_ Gao_】
The basic method of setting up maps
// Set the map
Texture mTexture = Resources.Load("myTexture", typeof(Texture )) as Texture;
material.SetTexture("_MainTex", mTexture );
// Set the integer
material.SetInt("_Int", 1);
// Set floating point
material.SetFloat("_Float", 0.1f);
// Set the color rgba
material.SetColor("_Color", Color.white);
// Set vector xyzw
material.SetVector("_Vector", new Vector4());
// shader There is no normal interface for bool Type of . But through #pragma multi_compile __ UNITY_name Can achieve
// Set up shader Of UNITY_name by true
material.EnableKeyword("UNITY_name");
// Set up shader Of UNITY_name by true
material.DisableKeyword("UNITY_name");
Set controls shader Several basic methods of the design
1 Set up directly Material file
Use unity The editor directly locates the containing target shader Of Material, And access... In the script . All import this Material All objects will change .
1, Create a public in the script Material Parameters
2, Bind a script to an object
3, stay unity edtor It contains Shader Of Material File and drag it to the corresponding parameter of the script
public class shaderController : MonoBehaviour
{
public Material material;
void Update()
{
material.SetFloat("_Float", Mathf.Sin(Time.frameCount * 0.01f));
}
}
2 Set... In resources Material file
Set up directly Material In the document Shader, All import this Material All objects will change
The goal is Material Must be in Asset/Resources Under the folder .
public class shaderController : MonoBehaviour
{
public Material material;
void Start()
{
// typeof(Material) To prevent different types of files from having the same name
material = Resources.Load("rshader", typeof(Material)) as Material;
}
void Update()
{
// Set floating point value
material.SetFloat("_Float", Mathf.Sin(Time.frameCount * 0.01f));
}
}
3 By designation shader Set up
This setting only affects the currently created Material The object , It will not affect others with this shader The object .
1, Read shader resources
2, Create... In memory Material And set relevant parameters
3, Will the new Material Assign to the target object
public class shaderController : MonoBehaviour
{
public Material material;
void Start()
{
// Get the object MeshRenderer Components
MeshRenderer mr = GameObject.Find("Cube222").GetComponent<MeshRenderer> ();
// Find the renderer according to the renderer path . It has nothing to do with the resource directory
Shader shader = Shader.Find("Custom/3d");
// newly build Material
material = new Material(shader);
// Assign a new material to an object
mr.material = material;
}
void Update()
{
material.SetFloat("_Float", Mathf.Sin(Time.frameCount * 0.1f));
}
}
4 Set directly through the target object , Affect all objects
This setting affects all references that are the same as the object Material The object
1, Get the target object
2, Get objects sharedMaterial attribute
3, Set up
public class shaderController : MonoBehaviour
{
public MeshRenderer mr;
void Start()
{
// Get contains Material Of MeshRenderer Components
mr = GameObject.Find("Cube222").GetComponent<MeshRenderer> ();
}
void Update()
{
//sharedMaterial Stands for setting the original Material, All objects that reference this material are affected .
//material Represents the present Material, In fact, it is equivalent to creating a new one in memory Mertairl. If it is not released manually, it will cause memory leakage . This change only affects the current object
mr.sharedMaterial.SetFloat("_Float", Mathf.Sin(Time.frameCount * 0.1f));
}
}Note that : sharedMaterial
5 Set directly through the target object , Only the current object is affected
This setting only affects the current setting Material The object
1, Get the target object
2, Get objects Material attribute
3, Set up
public class shaderController : MonoBehaviour
{
public MeshRenderer mr;
void Start()
{
// Get contains Material Of MeshRenderer Components
mr = GameObject.Find("Cube222").GetComponent<MeshRenderer> ();
}
void Update()
{
//sharedMaterial Stands for setting the original Material, All objects that reference this material are affected .
//material Represents the present Material, In fact, it is equivalent to creating a new one in memory Mertairl. If it is not released manually, it will cause memory leakage . This change only affects the current object
//material The process of assignment :
//1.Material m = mr.materia;
//2. Set up m.set....;
//3.mr.material = m;
mr.material.SetFloat("_Float", Mathf.Sin(Time.frameCount * 0.1f));
}
}Note that : material
If the setting is invalid, you should pay attention to
The goal is shader Whether the renderer path of has a duplicate name .
If it is read from a resource file , You need to make sure it's in Asset/Resources Under the folder
distinguish materil and sharemateril The difference between . In fact, set it directly materil It is equivalent to instantiating a new materil And to the object , If you do not destroy it manually , This new materil Will always reside in memory . and sharemateril Is to modify the existing materil, But this change will change all references to the materil The object of . This must be made clear .
边栏推荐
- AI intelligent matting tool - hair can be seen
- 买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股
- Is it safe to open a stock account with the QR code given by the CICC securities manager? I want to open an account
- How SAP Spartacus default routing configuration works
- Word chess based on heuristic search
- Module 5 operation
- Hands on deep learning pytorch version 3 - Data Preprocessing
- SAP commerce cloud project Spartacus getting started
- 会计要素包括哪些内容
- Release of dolphin scheduler video tutorial in Shangsi Valley
猜你喜欢

leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】

经典Wide & Deep模型介绍及tensorflow 2代码实现

Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)

How to analyze financial expenses

会计要素包括哪些内容
![[solution] sword finger offer 15 Number of 1 in binary (C language)](/img/ab/149775ae8ed94464efdf6921c1022a.png)
[solution] sword finger offer 15 Number of 1 in binary (C language)

矩阵求导及其链式法则
![[fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]](/img/8a/f5847eef7318f3db54aa2d4405b06a.jpg)
[fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]

俞敏洪:新东方并不存在倒下再翻身,摧毁又雄起的逆转

Web crawler 2: crawl the user ID and home page address of Netease cloud music reviews
随机推荐
[solution] sword finger offer 15 Number of 1 in binary (C language)
打新债注册开户有没有什么风险?安全吗?
Configuring assimp Library in QT environment (MinGW compiler)
Shiniman household sprint A shares: annual revenue of nearly 1.2 billion red star Macalline and incredibly home are shareholders
聊聊我的远程工作体验 | 社区征文
The network connection is disconnected. Please refresh and try again
BN(Batch Normalization) 的理论理解以及在tf.keras中的实际应用和总结
How to enable Hana cloud service on SAP BTP platform
Introduction to operator
[mathematical modeling] spanning tree based on Matlab GUI random nodes [including Matlab source code 1919]
关于appium踩坑 :Encountered internal error running command: Error: Cannot verify the signature of (已解决)
Godson China Science and technology innovation board is listed: the market value is 35.7 billion yuan, becoming the first share of domestic CPU
Unity: 脚本缺失 “The referenced script (Unknown) on this Behaviour is missing!“
leetcode:141. 环形链表【哈希表 + 快慢指针】
Which securities company is the most convenient, safe and reliable for opening an account
Icml2022 | neurotoxin: a lasting back door to federal learning
KDD2022 | 基于知识增强提示学习的统一会话推荐系统
How SAP Spartacus default routing configuration works
Installation avec homebrew dans un environnement Mac OS [email protected]
Listing of maolaiguang discipline on the Innovation Board: it is planned to raise 400million yuan. Fanyi and fanhao brothers are the actual controllers