TouchDesigner 如何傳送數值給 Unity?

發表於

TouchDesigner

在這篇文章中,我們主要聚焦在 TouchDesigner 傳輸數值給 Unity 這一塊的技術。我們會先在 TouchDesigner 產生一個持續變化的數值,再通過 OSC CHOP,將數值以 UDP 方式傳輸至 Unity 中,最後用 Unity 的 UI 即時顯示接收到的資料。

TouchDesigner 是一個使用視覺化程式語言進行開發或視覺創作的軟體,由於它不太需要手動編寫程式,且含有豐富的可自定義控制面板,受到了視覺設計者的青睞。

雖然我主要是使用 Unity 進行專案開發,但 TouchDesigner 仍然是個對於原型開發來說很方便的工具。它內建了常用的 Hokuyo、Kinect 等模組,讓我能夠在 Unity 中完成硬體串接之前可以先使用 TouchDesigner 進行原型開發。

基礎的架構如下圖:

touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 01

操作步驟

一、TouchDesigner 產生與命名數值

啟動 TouchDesigner,預設應會開啟一個範例專案。由於其中的 Noise 我們可以拿來當作數值產生器,所以請直接在 Noise 後面連接 Rename 與 OSC Out 兩個 CHOP。

touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 02

點擊 Rename CHOP,調整屬性 To 為 a1 或你想命名的名稱。這個 OP 的功能是重新命名數值,在這個步驟它將能將 Noise 產生的 chan1 數值重新命名為 a1。

touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 03

最後點擊 OSC Out CHOP,所有設定保持預設即可,我們需要將 Network Address 與 Network Port 屬性記錄下來,讓待會的 Unity 可以與 TouchDesigner 對接成功。

touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 04

二、Unity 安裝 OSC Jack 套件

首先開啟 Unity 專案(2021.3 以上版本),點擊 Edit 開啟 Project Settings。

touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 05

切換到 Package Manager 分頁,於 Scoped Registries 中註冊一個套件來源,屬性內容如下表,輸入完成後點擊 Save 儲存並關閉 Project Settings 視窗。

NameKeijiro
URLhttps://registry.npmjs.com
Scope(s)jp.keijiro
touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 06

點擊 Window 開啟 Package Manager。

touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 07

先切換套件來源為 My Registries,再從中找到 OSC Jack 並點擊 Install 安裝套件。

touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 08

三、Unity 接收與顯示數值

於 Canvas 中建立一個 Text (Legacy) 物件,調整文字顏色與大小讓它明顯一些。

touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 09

在 Project 的 Assets 資料夾上點擊右鍵 > Create > ScriptableObjects > OSC Jack > Connection,建立 OscConnection 設定檔。

touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 10

點擊 OscConnection,將 Port 改成先前於 TouchDesigner 建立 OSC Out 時的設定。由於我們建立 OSC Out CHOP 時沒有更改預設設定,所以在這邊我們可設定 OscConnection 的 Port 為 10000。

touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 11

建立一個空物件 GameManager,加入 OSC Jack 的 Event Receiver 腳本,並依序進行以下操作:

  1. 把剛才新增的 OscConnection 設定檔拉入 Connection 屬性。
  2. OSC Address 輸入「/a1」以接收我們於 TouchDesigner 產生的數值。
  3. 修改 Data Type 為 String。
  4. 於 String Event 建立事件監聽,拉入剛才建立的 Text (Legacy) 物件,並選擇 Dynamic string 內的 text 屬性。
touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 12
touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 13

執行測試

執行專案,TouchDesigner 產生的數值將能即時同步到 Unity 中。

touchdesigner-send-value-to-unity-with-osc - touchdesigner send value to unity with osc 14

在本篇文章中,我們完成了從 TouchDesigner 傳輸數值到 Unity 的作業。在下一篇文章中,我們將反過來實作,將 Unity 的數值傳輸至 TouchDesigner

參考資料