翼度科技»论坛 编程开发 .net 查看内容

Unity Shader编辑器工具类ShaderUtil 常用函数和用法

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
Unity Shader编辑器工具类ShaderUtil 常用函数和用法


Unity的Shader编辑器工具类ShaderUtil提供了一系列函数,用于编译、导入和管理着色器。本文将介绍ShaderUtil类中的常用函数和用法。
编译和导入函数

CompileShader

函数签名:public static bool CompileShader(string source, out string error);
CompileShader函数用于编译一个着色器。它接受一个字符串参数source,表示要编译的着色器代码,返回一个布尔值,表示编译是否成功。如果编译失败,可以通过out参数error获取错误信息。
以下是一个使用CompileShader函数的示例:
  1. string shaderCode = @"
  2.     Shader ""CustomShader"" {
  3.         // 着色器代码
  4.     }
  5. ";
  6. string error;
  7. bool success = ShaderUtil.CompileShader(shaderCode, out error);
  8. if (success)
  9. {
  10.     Debug.Log("Shader compiled successfully!");
  11. }
  12. else
  13. {
  14.     Debug.LogError("Shader compilation failed: " + error);
  15. }
复制代码
OpenCompiledShader

函数签名:public static void OpenCompiledShader(string pathName, int line);
OpenCompiledShader函数用于在外部程序中打开已编译的着色器文件。它接受两个参数,pathName表示要打开的着色器文件的路径,line表示要跳转到的行数。
以下是一个使用OpenCompiledShader函数的示例:
  1. string shaderPath = "Assets/Shaders/CustomShader.shader";
  2. int line = 10;
  3. ShaderUtil.OpenCompiledShader(shaderPath, line);
复制代码
属性和关键字函数

GetPropertyCount

函数签名:public static int GetPropertyCount(Shader shader);
这个函数用于获取指定着色器中属性的数量。它接受一个Shader对象作为参数,并返回该着色器中属性的数量。属性是在Shader编辑器中定义的变量,用于控制材质的外观和行为。
以下是一个示例代码,演示了如何使用GetPropertyCount函数获取着色器中属性的数量:
  1. Shader shader = Shader.Find("MyShader");
  2. int propertyCount = ShaderUtil.GetPropertyCount(shader);
  3. Debug.Log("Property count: " + propertyCount);
复制代码
GetPropertyName

函数签名:public static string GetPropertyName(Shader shader, int propertyIndex);
这个函数用于获取指定着色器中属性的名称。它接受一个Shader对象和属性的索引作为参数,并返回该属性的名称。
以下是一个示例代码,演示了如何使用GetPropertyName函数获取着色器中属性的名称:
  1. Shader shader = Shader.Find("MyShader");
  2. int propertyIndex = 0;
  3. string propertyName = ShaderUtil.GetPropertyName(shader, propertyIndex);
  4. Debug.Log("Property name: " + propertyName);
复制代码
GetShaderKeywords

函数签名:public static string[] GetShaderKeywords(Shader shader);
这个函数用于获取指定着色器的关键字。关键字是在Shader编辑器中定义的标识符,用于控制着色器的不同功能和效果。
以下是一个示例代码,演示了如何使用GetShaderKeywords函数获取着色器的关键字:
  1. Shader shader = Shader.Find("MyShader");
  2. string[] keywords = ShaderUtil.GetShaderKeywords(shader);
  3. Debug.Log("Shader keywords: " + string.Join(", ", keywords));
复制代码
SetShaderKeywords

函数签名:
public static void SetShaderKeywords(Shader shader, string[] keywords);
这个函数用于设置指定着色器的关键字。它接受一个Shader对象和一个关键字数组作为参数,并将这些关键字应用到着色器上。
以下是一个示例代码,演示了如何使用SetShaderKeywords函数设置着色器的关键字:
  1. Shader shader = Shader.Find("MyShader");
  2. string[] keywords = new string[] { "KEYWORD1", "KEYWORD2" };
  3. ShaderUtil.SetShaderKeywords(shader, keywords);
复制代码
渲染纹理函数

1. GetRenderTextureFormat
  1. public static RenderTextureFormat GetRenderTextureFormat(string name);
复制代码
该函数用于获取指定名称的渲染纹理格式。它接受一个字符串参数name,表示渲染纹理的名称,返回一个RenderTextureFormat枚举值,表示对应的渲染纹理格式。
2. GetRenderTargetCount
  1. public static int GetRenderTargetCount();
复制代码
该函数用于获取当前渲染目标的数量。它返回一个整数值,表示当前渲染目标的数量。
3. GetRenderTargetName
  1. public static string GetRenderTargetName(int index);
复制代码
该函数用于获取指定索引的渲染目标的名称。它接受一个整数参数index,表示渲染目标的索引,返回一个字符串值,表示对应渲染目标的名称。
4. GetRenderTargetPropertyCount
  1. public static int GetRenderTargetPropertyCount(int index);
复制代码
该函数用于获取指定渲染目标的属性数量。它接受一个整数参数index,表示渲染目标的索引,返回一个整数值,表示对应渲染目标的属性数量。
5. GetRenderTargetPropertyName
  1. public static string GetRenderTargetPropertyName(int index, int propIndex);
复制代码
该函数用于获取指定渲染目标的属性名称。它接受两个整数参数index和propIndex,分别表示渲染目标的索引和属性的索引,返回一个字符串值,表示对应属性的名称。
6. GetRenderTargetPropertyType
  1. public static ShaderUtil.ShaderPropertyType GetRenderTargetPropertyType(int index, int propIndex);
复制代码
该函数用于获取指定渲染目标的属性类型。它接受两个整数参数index和propIndex,分别表示渲染目标的索引和属性的索引,返回一个ShaderUtil.ShaderPropertyType枚举值,表示对应属性的类型。
7. GetRenderTargetPropertyValue
  1. public static float GetRenderTargetPropertyValue(int index, int propIndex);
复制代码
该函数用于获取指定渲染目标的属性值。它接受两个整数参数index和propIndex,分别表示渲染目标的索引和属性的索引,返回一个浮点数值,表示对应属性的值。
8. SetRenderTargetPropertyCount
  1. public static void SetRenderTargetPropertyCount(int index, int count);
复制代码
该函数用于设置指定渲染目标的属性数量。它接受两个整数参数index和count,分别表示渲染目标的索引和属性的数量。
9. SetRenderTargetPropertyName
  1. public static void SetRenderTargetPropertyName(int index, int propIndex, string name);
复制代码
该函数用于设置指定渲染目标的属性名称。它接受三个参数index、propIndex和name,分别表示渲染目标的索引、属性的索引和属性的名称。
10. SetRenderTargetPropertyType
  1. public static void SetRenderTargetPropertyType(int index, int propIndex, ShaderUtil.ShaderPropertyType type);
复制代码
该函数用于设置指定渲染目标的属性类型。它接受三个参数index、propIndex和type,分别表示渲染目标的索引、属性的索引和属性的类型。
11. SetRenderTargetPropertyValue
  1. public static void SetRenderTargetPropertyValue(int index, int propIndex, float value);
复制代码
该函数用于设置指定渲染目标的属性值。它接受三个参数index、propIndex和value,分别表示渲染目标的索引、属性的索引和属性的值。
示例代码
  1. using UnityEngine;
  2. public class ShaderUtilExample : MonoBehaviour
  3. {
  4.     void Start()
  5.     {
  6.         // 获取渲染纹理格式
  7.         RenderTextureFormat format = ShaderUtil.GetRenderTextureFormat("MyRenderTexture");
  8.         Debug.Log("RenderTexture format: " + format);
  9.         // 获取渲染目标数量
  10.         int targetCount = ShaderUtil.GetRenderTargetCount();
  11.         Debug.Log("Render target count: " + targetCount);
  12.         // 获取渲染目标名称
  13.         string targetName = ShaderUtil.GetRenderTargetName(0);
  14.         Debug.Log("Render target name: " + targetName);
  15.         // 获取渲染目标属性数量
  16.         int propertyCount = ShaderUtil.GetRenderTargetPropertyCount(0);
  17.         Debug.Log("Render target property count: " + propertyCount);
  18.         // 获取渲染目标属性名称
  19.         string propertyName = ShaderUtil.GetRenderTargetPropertyName(0, 0);
  20.         Debug.Log("Render target property name: " + propertyName);
  21.         // 获取渲染目标属性类型
  22.         ShaderUtil.ShaderPropertyType propertyType = ShaderUtil.GetRenderTargetPropertyType(0, 0);
  23.         Debug.Log("Render target property type: " + propertyType);
  24.         // 获取渲染目标属性值
  25.         float propertyValue = ShaderUtil.GetRenderTargetPropertyValue(0, 0);
  26.         Debug.Log("Render target property value: " + propertyValue);
  27.         // 设置渲染目标属性数量
  28.         ShaderUtil.SetRenderTargetPropertyCount(0, 2);
  29.         // 设置渲染目标属性名称
  30.         ShaderUtil.SetRenderTargetPropertyName(0, 0, "Property1");
  31.         ShaderUtil.SetRenderTargetPropertyName(0, 1, "Property2");
  32.         // 设置渲染目标属性类型
  33.         ShaderUtil.SetRenderTargetPropertyType(0, 0, ShaderUtil.ShaderPropertyType.Float);
  34.         ShaderUtil.SetRenderTargetPropertyType(0, 1, ShaderUtil.ShaderPropertyType.Color);
  35.         // 设置渲染目标属性值
  36.         ShaderUtil.SetRenderTargetPropertyValue(0, 0, 1.0f);
  37.         ShaderUtil.SetRenderTargetPropertyValue(0, 1, Color.red);
  38.     }
  39. }
复制代码
其他函数

GetMaterial
  1. public static Material GetMaterial(Renderer renderer, int materialIndex);
复制代码
该函数用于获取指定渲染器(Renderer)上指定索引(materialIndex)的材质(Material)实例。在Unity中,渲染器可以包含多个材质,每个材质对应一个子网格(SubMesh)。以下是一个使用示例:
  1. Renderer renderer = GetComponent<Renderer>();
  2. Material material = ShaderUtil.GetMaterial(renderer, 0);
复制代码
GetMaterialProperty
  1. public static float GetMaterialProperty(Material material, int propertyIndex);
  2. public static Vector4 GetMaterialProperty(Material material, int propertyIndex, bool isBatchable);
复制代码
这个函数用于获取材质(Material)上指定索引(propertyIndex)的属性(Property)的值。属性可以是浮点数、向量等类型。以下是一个使用示例:
  1. Material material = GetComponent<Renderer>().material;
  2. float floatValue = ShaderUtil.GetMaterialProperty(material, 0);
  3. Vector4 vectorValue = ShaderUtil.GetMaterialProperty(material, 1);
复制代码
GetMaterialPropertyIndex
  1. public static int GetMaterialPropertyIndex(Material material, string propertyName);
复制代码
该函数用于获取材质(Material)上指定属性(propertyName)的索引(propertyIndex)。以下是一个使用示例:
  1. Material material = GetComponent<Renderer>().material;
  2. int propertyIndex = ShaderUtil.GetMaterialPropertyIndex(material, "_Color");
复制代码
GetMaterialPropertyType
  1. public static ShaderUtil.ShaderPropertyType GetMaterialPropertyType(Material material, int propertyIndex);
复制代码
这个函数用于获取材质(Material)上指定索引(propertyIndex)的属性(Property)的类型(ShaderPropertyType)。以下是一个使用示例:
  1. Material material = GetComponent<Renderer>().material;
  2. ShaderUtil.ShaderPropertyType propertyType = ShaderUtil.GetMaterialPropertyType(material, 0);
复制代码
GetMaterialPropertyDefaultValue
  1. public static float GetMaterialPropertyDefaultValue(Material material, int propertyIndex);
  2. public static Vector4 GetMaterialPropertyDefaultValue(Material material, int propertyIndex, bool isBatchable);
复制代码
该函数用于获取材质(Material)上指定索引(propertyIndex)的属性(Property)的默认值。以下是一个使用示例:
  1. Material material = GetComponent<Renderer>().material;
  2. float defaultValue = ShaderUtil.GetMaterialPropertyDefaultValue(material, 0);
  3. Vector4 defaultVector = ShaderUtil.GetMaterialPropertyDefaultValue(material, 1);
复制代码
GetMaterialPropertyTextureDimension
  1. public static TextureDimension GetMaterialPropertyTextureDimension(Material material, int propertyIndex);
复制代码
这个函数用于获取材质(Material)上指定索引(propertyIndex)的纹理属性(Texture Property)的维度(TextureDimension)。以下是一个使用示例:
  1. Material material = GetComponent<Renderer>().material;
  2. TextureDimension dimension = ShaderUtil.GetMaterialPropertyTextureDimension(material, 0);
复制代码
GetMaterialPropertyTextureDefaultName
  1. public static string GetMaterialPropertyTextureDefaultName(Material material, int propertyIndex);
复制代码
该函数用于获取材质(Material)上指定索引(propertyIndex)的纹理属性(Texture Property)的默认名称。以下是一个使用示例:
  1. Material material = GetComponent<Renderer>().material;
  2. string defaultName = ShaderUtil.GetMaterialPropertyTextureDefaultName(material, 0);
复制代码
GetGlobalFloat
  1. public static float GetGlobalFloat(string name);
复制代码
这个函数用于获取全局浮点数属性(Global Float Property)的值。以下是一个使用示例:
  1. float globalFloat = ShaderUtil.GetGlobalFloat("_MyGlobalFloat");
复制代码
GetGlobalInt
  1. public static int GetGlobalInt(string name);
复制代码
该函数用于获取全局整数属性(Global Int Property)的值。以下是一个使用示例:
  1. int globalInt = ShaderUtil.GetGlobalInt("_MyGlobalInt");
复制代码
GetGlobalVector
  1. public static Vector4 GetGlobalVector(string name);
复制代码
这个函数用于获取全局向量属性(Global Vector Property)的值。以下是一个使用示例:
  1. Vector4 globalVector = ShaderUtil.GetGlobalVector("_MyGlobalVector");
复制代码
GetGlobalMatrix
  1. public static Matrix4x4 GetGlobalMatrix(string name);
复制代码
该函数用于获取全局矩阵属性(Global Matrix Property)的值。以下是一个使用示例:
  1. Matrix4x4 globalMatrix = ShaderUtil.GetGlobalMatrix("_MyGlobalMatrix");
复制代码
GetGlobalTexture
  1. public static Texture GetGlobalTexture(string name);
复制代码
这个函数用于获取全局纹理属性(Global Texture Property)的值。以下是一个使用示例:
  1. Texture globalTexture = ShaderUtil.GetGlobalTexture("_MyGlobalTexture");
复制代码
SetGlobalFloat
  1. public static void SetGlobalFloat(string name, float value);
复制代码
该函数用于设置全局浮点数属性(Global Float Property)的值。以下是一个使用示例:
  1. ShaderUtil.SetGlobalFloat("_MyGlobalFloat", 1.5f);
复制代码
这将把名为"_MyGlobalFloat"的全局浮点数属性设置为1.5。
SetGlobalInt
  1. public static void SetGlobalInt(string name, int value);
复制代码
这个函数用于设置全局整数属性(Global Int Property)的值。以下是一个使用示例:
  1. ShaderUtil.SetGlobalInt("_MyGlobalInt", 10);
复制代码
这将把名为"_MyGlobalInt"的全局整数属性设置为10。
SetGlobalVector
  1. public static void SetGlobalVector(string name, Vector4 value);
复制代码
该函数用于设置全局向量属性(Global Vector Property)的值。以下是一个使用示例:
  1. ShaderUtil.SetGlobalVector("_MyGlobalVector", new Vector4(1, 2, 3, 4));
复制代码
这将把名为"_MyGlobalVector"的全局向量属性设置为(1, 2, 3, 4)。
SetGlobalMatrix
  1. public static void SetGlobalMatrix(string name, Matrix4x4 value);
复制代码
这个函数用于设置全局矩阵属性(Global Matrix Property)的值。以下是一个使用示例:
  1. Matrix4x4 matrix = Matrix4x4.identity;
  2. ShaderUtil.SetGlobalMatrix("_MyGlobalMatrix", matrix);
复制代码
这将把名为"_MyGlobalMatrix"的全局矩阵属性设置为单位矩阵。
SetGlobalTexture
  1. public static void SetGlobalTexture(string name, Texture value);
复制代码
该函数用于设置全局纹理属性(Global Texture Property)的值。以下是一个使用示例:
  1. Texture texture = Resources.Load<Texture>("MyTexture");
  2. ShaderUtil.SetGlobalTexture("_MyGlobalTexture", texture);
复制代码
这将把名为"_MyGlobalTexture"的全局纹理属性设置为名为"MyTexture"的资源纹理。
我对技术文章中可能存在的错误向您表示诚挚的歉意。我努力确保提供准确可靠的信息,但由于技术领域的不断变化,错误难以避免。如果您发现了错误或有任何疑问,请与我联系。我将竭尽全力纠正错误并提供更准确的信息。
再次向您表示最诚挚的歉意,我将更加谨慎地审查和更新文章,以提供更好的阅读体验和准确的技术信息。
谢谢您的理解和支持。
            本文作者:                            Blank                            
        本文链接:        
        版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
        声援博主:如果您觉得文章对您有帮助,可以点击文章右下角            【                推荐】                    一下。您的鼓励是博主的最大动力!

来源:https://www.cnblogs.com/alianblank/archive/2023/08/03/17604767.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具