Unity Shader編輯器工具類ShaderUtil 常用函數和用法

来源:https://www.cnblogs.com/alianblank/archive/2023/08/03/17604767.html
-Advertisement-
Play Games

# Unity Shader編輯器工具類ShaderUtil 常用函數和用法 ![Unity Shader](https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569_960_720.jpg) Unity的Shader編輯器工具 ...


Unity Shader編輯器工具類ShaderUtil 常用函數和用法

Unity Shader

Unity的Shader編輯器工具類ShaderUtil提供了一系列函數,用於編譯、導入和管理著色器。本文將介紹ShaderUtil類中的常用函數和用法。

編譯和導入函數

CompileShader

函數簽名:public static bool CompileShader(string source, out string error);

CompileShader函數用於編譯一個著色器。它接受一個字元串參數source,表示要編譯的著色器代碼,返回一個布爾值,表示編譯是否成功。如果編譯失敗,可以通過out參數error獲取錯誤信息。

以下是一個使用CompileShader函數的示例:

string shaderCode = @"
    Shader ""CustomShader"" {
        // 著色器代碼
    }
";

string error;
bool success = ShaderUtil.CompileShader(shaderCode, out error);

if (success)
{
    Debug.Log("Shader compiled successfully!");
}
else
{
    Debug.LogError("Shader compilation failed: " + error);
}

OpenCompiledShader

函數簽名:public static void OpenCompiledShader(string pathName, int line);

OpenCompiledShader函數用於在外部程式中打開已編譯的著色器文件。它接受兩個參數,pathName表示要打開的著色器文件的路徑,line表示要跳轉到的行數。

以下是一個使用OpenCompiledShader函數的示例:

string shaderPath = "Assets/Shaders/CustomShader.shader";
int line = 10;

ShaderUtil.OpenCompiledShader(shaderPath, line);

屬性和關鍵字函數

GetPropertyCount

函數簽名:public static int GetPropertyCount(Shader shader);

這個函數用於獲取指定著色器中屬性的數量。它接受一個Shader對象作為參數,並返回該著色器中屬性的數量。屬性是在Shader編輯器中定義的變數,用於控制材質的外觀和行為。

以下是一個示例代碼,演示瞭如何使用GetPropertyCount函數獲取著色器中屬性的數量:

Shader shader = Shader.Find("MyShader");
int propertyCount = ShaderUtil.GetPropertyCount(shader);
Debug.Log("Property count: " + propertyCount);

GetPropertyName

函數簽名:public static string GetPropertyName(Shader shader, int propertyIndex);

這個函數用於獲取指定著色器中屬性的名稱。它接受一個Shader對象和屬性的索引作為參數,並返回該屬性的名稱。

以下是一個示例代碼,演示瞭如何使用GetPropertyName函數獲取著色器中屬性的名稱:

Shader shader = Shader.Find("MyShader");
int propertyIndex = 0;
string propertyName = ShaderUtil.GetPropertyName(shader, propertyIndex);
Debug.Log("Property name: " + propertyName);

GetShaderKeywords

函數簽名:public static string[] GetShaderKeywords(Shader shader);

這個函數用於獲取指定著色器的關鍵字。關鍵字是在Shader編輯器中定義的標識符,用於控制著色器的不同功能和效果。

以下是一個示例代碼,演示瞭如何使用GetShaderKeywords函數獲取著色器的關鍵字:

Shader shader = Shader.Find("MyShader");
string[] keywords = ShaderUtil.GetShaderKeywords(shader);
Debug.Log("Shader keywords: " + string.Join(", ", keywords));

SetShaderKeywords

函數簽名:
public static void SetShaderKeywords(Shader shader, string[] keywords);

這個函數用於設置指定著色器的關鍵字。它接受一個Shader對象和一個關鍵字數組作為參數,並將這些關鍵字應用到著色器上。

以下是一個示例代碼,演示瞭如何使用SetShaderKeywords函數設置著色器的關鍵字:

Shader shader = Shader.Find("MyShader");
string[] keywords = new string[] { "KEYWORD1", "KEYWORD2" };
ShaderUtil.SetShaderKeywords(shader, keywords);

渲染紋理函數

1. GetRenderTextureFormat

public static RenderTextureFormat GetRenderTextureFormat(string name);

該函數用於獲取指定名稱的渲染紋理格式。它接受一個字元串參數name,表示渲染紋理的名稱,返回一個RenderTextureFormat枚舉值,表示對應的渲染紋理格式。

2. GetRenderTargetCount

public static int GetRenderTargetCount();

該函數用於獲取當前渲染目標的數量。它返回一個整數值,表示當前渲染目標的數量。

3. GetRenderTargetName

public static string GetRenderTargetName(int index);

該函數用於獲取指定索引的渲染目標的名稱。它接受一個整數參數index,表示渲染目標的索引,返回一個字元串值,表示對應渲染目標的名稱。

4. GetRenderTargetPropertyCount

public static int GetRenderTargetPropertyCount(int index);

該函數用於獲取指定渲染目標的屬性數量。它接受一個整數參數index,表示渲染目標的索引,返回一個整數值,表示對應渲染目標的屬性數量。

5. GetRenderTargetPropertyName

public static string GetRenderTargetPropertyName(int index, int propIndex);

該函數用於獲取指定渲染目標的屬性名稱。它接受兩個整數參數index和propIndex,分別表示渲染目標的索引和屬性的索引,返回一個字元串值,表示對應屬性的名稱。

6. GetRenderTargetPropertyType

public static ShaderUtil.ShaderPropertyType GetRenderTargetPropertyType(int index, int propIndex);

該函數用於獲取指定渲染目標的屬性類型。它接受兩個整數參數index和propIndex,分別表示渲染目標的索引和屬性的索引,返回一個ShaderUtil.ShaderPropertyType枚舉值,表示對應屬性的類型。

7. GetRenderTargetPropertyValue

public static float GetRenderTargetPropertyValue(int index, int propIndex);

該函數用於獲取指定渲染目標的屬性值。它接受兩個整數參數index和propIndex,分別表示渲染目標的索引和屬性的索引,返回一個浮點數值,表示對應屬性的值。

8. SetRenderTargetPropertyCount

public static void SetRenderTargetPropertyCount(int index, int count);

該函數用於設置指定渲染目標的屬性數量。它接受兩個整數參數index和count,分別表示渲染目標的索引和屬性的數量。

9. SetRenderTargetPropertyName

public static void SetRenderTargetPropertyName(int index, int propIndex, string name);

該函數用於設置指定渲染目標的屬性名稱。它接受三個參數index、propIndex和name,分別表示渲染目標的索引、屬性的索引和屬性的名稱。

10. SetRenderTargetPropertyType

public static void SetRenderTargetPropertyType(int index, int propIndex, ShaderUtil.ShaderPropertyType type);

該函數用於設置指定渲染目標的屬性類型。它接受三個參數index、propIndex和type,分別表示渲染目標的索引、屬性的索引和屬性的類型。

11. SetRenderTargetPropertyValue

public static void SetRenderTargetPropertyValue(int index, int propIndex, float value);

該函數用於設置指定渲染目標的屬性值。它接受三個參數index、propIndex和value,分別表示渲染目標的索引、屬性的索引和屬性的值。

示例代碼

using UnityEngine;

public class ShaderUtilExample : MonoBehaviour
{
    void Start()
    {
        // 獲取渲染紋理格式
        RenderTextureFormat format = ShaderUtil.GetRenderTextureFormat("MyRenderTexture");
        Debug.Log("RenderTexture format: " + format);

        // 獲取渲染目標數量
        int targetCount = ShaderUtil.GetRenderTargetCount();
        Debug.Log("Render target count: " + targetCount);

        // 獲取渲染目標名稱
        string targetName = ShaderUtil.GetRenderTargetName(0);
        Debug.Log("Render target name: " + targetName);

        // 獲取渲染目標屬性數量
        int propertyCount = ShaderUtil.GetRenderTargetPropertyCount(0);
        Debug.Log("Render target property count: " + propertyCount);

        // 獲取渲染目標屬性名稱
        string propertyName = ShaderUtil.GetRenderTargetPropertyName(0, 0);
        Debug.Log("Render target property name: " + propertyName);

        // 獲取渲染目標屬性類型
        ShaderUtil.ShaderPropertyType propertyType = ShaderUtil.GetRenderTargetPropertyType(0, 0);
        Debug.Log("Render target property type: " + propertyType);

        // 獲取渲染目標屬性值
        float propertyValue = ShaderUtil.GetRenderTargetPropertyValue(0, 0);
        Debug.Log("Render target property value: " + propertyValue);

        // 設置渲染目標屬性數量
        ShaderUtil.SetRenderTargetPropertyCount(0, 2);

        // 設置渲染目標屬性名稱
        ShaderUtil.SetRenderTargetPropertyName(0, 0, "Property1");
        ShaderUtil.SetRenderTargetPropertyName(0, 1, "Property2");

        // 設置渲染目標屬性類型
        ShaderUtil.SetRenderTargetPropertyType(0, 0, ShaderUtil.ShaderPropertyType.Float);
        ShaderUtil.SetRenderTargetPropertyType(0, 1, ShaderUtil.ShaderPropertyType.Color);

        // 設置渲染目標屬性值
        ShaderUtil.SetRenderTargetPropertyValue(0, 0, 1.0f);
        ShaderUtil.SetRenderTargetPropertyValue(0, 1, Color.red);
    }
}

其他函數

GetMaterial

public static Material GetMaterial(Renderer renderer, int materialIndex);

該函數用於獲取指定渲染器(Renderer)上指定索引(materialIndex)的材質(Material)實例。在Unity中,渲染器可以包含多個材質,每個材質對應一個子網格(SubMesh)。以下是一個使用示例:

Renderer renderer = GetComponent<Renderer>();
Material material = ShaderUtil.GetMaterial(renderer, 0);

GetMaterialProperty

public static float GetMaterialProperty(Material material, int propertyIndex);
public static Vector4 GetMaterialProperty(Material material, int propertyIndex, bool isBatchable);

這個函數用於獲取材質(Material)上指定索引(propertyIndex)的屬性(Property)的值。屬性可以是浮點數、向量等類型。以下是一個使用示例:

Material material = GetComponent<Renderer>().material;
float floatValue = ShaderUtil.GetMaterialProperty(material, 0);
Vector4 vectorValue = ShaderUtil.GetMaterialProperty(material, 1);

GetMaterialPropertyIndex

public static int GetMaterialPropertyIndex(Material material, string propertyName);

該函數用於獲取材質(Material)上指定屬性(propertyName)的索引(propertyIndex)。以下是一個使用示例:

Material material = GetComponent<Renderer>().material;
int propertyIndex = ShaderUtil.GetMaterialPropertyIndex(material, "_Color");

GetMaterialPropertyType

public static ShaderUtil.ShaderPropertyType GetMaterialPropertyType(Material material, int propertyIndex);

這個函數用於獲取材質(Material)上指定索引(propertyIndex)的屬性(Property)的類型(ShaderPropertyType)。以下是一個使用示例:

Material material = GetComponent<Renderer>().material;
ShaderUtil.ShaderPropertyType propertyType = ShaderUtil.GetMaterialPropertyType(material, 0);

GetMaterialPropertyDefaultValue

public static float GetMaterialPropertyDefaultValue(Material material, int propertyIndex);
public static Vector4 GetMaterialPropertyDefaultValue(Material material, int propertyIndex, bool isBatchable);

該函數用於獲取材質(Material)上指定索引(propertyIndex)的屬性(Property)的預設值。以下是一個使用示例:

Material material = GetComponent<Renderer>().material;
float defaultValue = ShaderUtil.GetMaterialPropertyDefaultValue(material, 0);
Vector4 defaultVector = ShaderUtil.GetMaterialPropertyDefaultValue(material, 1);

GetMaterialPropertyTextureDimension

public static TextureDimension GetMaterialPropertyTextureDimension(Material material, int propertyIndex);

這個函數用於獲取材質(Material)上指定索引(propertyIndex)的紋理屬性(Texture Property)的維度(TextureDimension)。以下是一個使用示例:

Material material = GetComponent<Renderer>().material;
TextureDimension dimension = ShaderUtil.GetMaterialPropertyTextureDimension(material, 0);

GetMaterialPropertyTextureDefaultName

public static string GetMaterialPropertyTextureDefaultName(Material material, int propertyIndex);

該函數用於獲取材質(Material)上指定索引(propertyIndex)的紋理屬性(Texture Property)的預設名稱。以下是一個使用示例:

Material material = GetComponent<Renderer>().material;
string defaultName = ShaderUtil.GetMaterialPropertyTextureDefaultName(material, 0);

GetGlobalFloat

public static float GetGlobalFloat(string name);

這個函數用於獲取全局浮點數屬性(Global Float Property)的值。以下是一個使用示例:

float globalFloat = ShaderUtil.GetGlobalFloat("_MyGlobalFloat");

GetGlobalInt

public static int GetGlobalInt(string name);

該函數用於獲取全局整數屬性(Global Int Property)的值。以下是一個使用示例:

int globalInt = ShaderUtil.GetGlobalInt("_MyGlobalInt");

GetGlobalVector

public static Vector4 GetGlobalVector(string name);

這個函數用於獲取全局向量屬性(Global Vector Property)的值。以下是一個使用示例:

Vector4 globalVector = ShaderUtil.GetGlobalVector("_MyGlobalVector");

GetGlobalMatrix

public static Matrix4x4 GetGlobalMatrix(string name);

該函數用於獲取全局矩陣屬性(Global Matrix Property)的值。以下是一個使用示例:

Matrix4x4 globalMatrix = ShaderUtil.GetGlobalMatrix("_MyGlobalMatrix");

GetGlobalTexture

public static Texture GetGlobalTexture(string name);

這個函數用於獲取全局紋理屬性(Global Texture Property)的值。以下是一個使用示例:

Texture globalTexture = ShaderUtil.GetGlobalTexture("_MyGlobalTexture");

SetGlobalFloat

public static void SetGlobalFloat(string name, float value);

該函數用於設置全局浮點數屬性(Global Float Property)的值。以下是一個使用示例:

ShaderUtil.SetGlobalFloat("_MyGlobalFloat", 1.5f);

這將把名為"_MyGlobalFloat"的全局浮點數屬性設置為1.5。

SetGlobalInt

public static void SetGlobalInt(string name, int value);

這個函數用於設置全局整數屬性(Global Int Property)的值。以下是一個使用示例:

ShaderUtil.SetGlobalInt("_MyGlobalInt", 10);

這將把名為"_MyGlobalInt"的全局整數屬性設置為10。

SetGlobalVector

public static void SetGlobalVector(string name, Vector4 value);

該函數用於設置全局向量屬性(Global Vector Property)的值。以下是一個使用示例:

ShaderUtil.SetGlobalVector("_MyGlobalVector", new Vector4(1, 2, 3, 4));

這將把名為"_MyGlobalVector"的全局向量屬性設置為(1, 2, 3, 4)。

SetGlobalMatrix

public static void SetGlobalMatrix(string name, Matrix4x4 value);

這個函數用於設置全局矩陣屬性(Global Matrix Property)的值。以下是一個使用示例:

Matrix4x4 matrix = Matrix4x4.identity;
ShaderUtil.SetGlobalMatrix("_MyGlobalMatrix", matrix);

這將把名為"_MyGlobalMatrix"的全局矩陣屬性設置為單位矩陣。

SetGlobalTexture

public static void SetGlobalTexture(string name, Texture value);

該函數用於設置全局紋理屬性(Global Texture Property)的值。以下是一個使用示例:

Texture texture = Resources.Load<Texture>("MyTexture");
ShaderUtil.SetGlobalTexture("_MyGlobalTexture", texture);

這將把名為"_MyGlobalTexture"的全局紋理屬性設置為名為"MyTexture"的資源紋理。


我對技術文章中可能存在的錯誤向您表示誠摯的歉意。我努力確保提供準確可靠的信息,但由於技術領域的不斷變化,錯誤難以避免。如果您發現了錯誤或有任何疑問,請與我聯繫。我將竭盡全力糾正錯誤並提供更準確的信息。

再次向您表示最誠摯的歉意,我將更加謹慎地審查和更新文章,以提供更好的閱讀體驗和準確的技術信息。

謝謝您的理解和支持。

__EOF__

本文作者 Blank
本文鏈接
版權聲明:本博客所有文章除特別聲明外,均採用 BY-NC-SA 許可協議。轉載請註明出處!
聲援博主:如果您覺得文章對您有幫助,可以點擊文章右下角 推薦 一下。您的鼓勵是博主的最大動力!

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ## Spring MVC Spring MVC是Spring框架的一部分,是一個Web應用程式框架。它旨在使用Model-View-Controller(MVC)設計模式輕鬆構建Web應用程式。 在Spring MVC中,應用程式被分為三個主要組件:Model、View和Controller。Mo ...
  • ## jsp ​ servlet 是無法將後端獲取的數據傳遞給html 頁面的,無法再servlet 中通過轉發或者是重定向的方式,給html 頁面傳遞響應的後端數據,servlet 中由於拼接過於繁瑣,是不適合寫html 的因此引入了 jsp ,既可以編寫 html標簽,也可以寫 Java 代碼, ...
  • # Go 語言入門指南: 環境搭建、基礎語法和常用特性解析 | 青訓營 ## 從零開始 ### Go 語言簡介 ![img](https://img2023.cnblogs.com/blog/2724888/202308/2724888-20230803143447307-285055892.png ...
  • 本文主要從源碼的角度解析了 ThreadLocal,並分析了發生記憶體泄漏的原因及正確用法,最後對它的應用場景進行了簡單介紹。 ...
  • 拆分列是`pandas`中常用的一種數據操作,它可以將一個包含多個值的列按照指定的規則拆分成多個新列,方便進行後續的分析和處理。拆分列的使用場景比較廣泛,以下是一些常見的應用場景: 1. 處理日期數據:在日期數據中,經常會將年、月、日等信息合併成一列,通過拆分列可以將其拆分成多個新列,方便進行時間序 ...
  • ## 教程簡介 JUnit是一個Java語言的單元測試框架。它由Kent Beck和Erich Gamma建立,逐漸成為源於Kent Beck的sUnit的xUnit家族中最為成功的一個。 JUnit有它自己的JUnit擴展生態圈。多數Java的開發環境都已經集成了JUnit作為單元測試的工具。JU ...
  • > 服務發現與負載均衡。 # 一、背景 在微服務架構中,這裡以開發環境「Dev」為基礎來描述,在K8S集群中通常會開放:路由網關、註冊中心、配置中心等相關服務,可以被集群外部訪問; ![](https://img2023.cnblogs.com/blog/1691717/202308/1691717 ...
  • ### 歡迎訪問我的GitHub > 這裡分類和彙總了欣宸的全部原創(含配套源碼):[https://github.com/zq2599/blog_demos](https://github.com/zq2599/blog_demos) ### 本篇概覽 - 本文是《quarkus依賴註入》系列的第 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...