├── README.md └── VFXGraphOscControl.cs /README.md: -------------------------------------------------------------------------------- 1 | # VFXGraphOSCControl 2 | A quick script to control VFX parameters using OSC messages: 3 | 4 | 5 | 1. Import OscJack (https://github.com/keijiro/OscJack/releases) 6 | 2. Expose some float parameters using VFX graph's blackboard 7 | 3. Add this script under your VisualEffects object 8 | 4. Set OSC port, obviously. 9 | 5. Add the names of the parameters to the parameters array in the inspector. (Use the EXACT same name in the VFX graph) 10 | 6. Send your OSC messages from your preferred application using messages with the structure / . 11 | Eg. "/emission_rate 10000" (emission_rate has to be the exact same name in script and blackboard). 12 | 13 |
14 | 15 | 16 |

17 | -------------------------------------------------------------------------------- /VFXGraphOscControl.cs: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | A quick script for controlling VFX parameters using OSC messages 4 | 5 | 1. Import OscJack (https://github.com/keijiro/OscJack/releases) 6 | 2. Expose some float parameters using VFX graph's blackboard 7 | 3. Add this script under your VisualEffects object 8 | 4. Set OSC port, obviously. 9 | 5. Add the names of the parameters to the parameters array in the inspector. (Use the EXACT same name in the VFX graph) 10 | 6. Send your OSC messages from your preferred application using the messages with the structure / . 11 | Eg. "/emission_rate 10000" (emission_rate has to be the exact same name in script and blackboard). 12 | 13 | 14 | */ 15 | 16 | 17 | 18 | using System.Collections; 19 | using System.Collections.Generic; 20 | using UnityEngine; 21 | 22 | using UnityEngine.VFX; 23 | using OscJack; 24 | 25 | [RequireComponent(typeof(VisualEffect))] 26 | public class VFXGraphOscControl : MonoBehaviour 27 | { 28 | 29 | public int port = 9001; 30 | 31 | OscServer server; 32 | 33 | VisualEffect visualEffect; 34 | 35 | 36 | //[Tooltip("Add a name for each (float) exposed parameter on your VFX graph")] 37 | public string [] parameters; 38 | 39 | private Dictionary updateFlags; 40 | private Dictionary valueHolder; 41 | 42 | 43 | void Start() 44 | { 45 | server = new OscServer( port); 46 | 47 | updateFlags = new Dictionary(); 48 | 49 | valueHolder = new Dictionary(); 50 | 51 | visualEffect = GetComponent(); 52 | 53 | foreach(string parameter in parameters) 54 | { 55 | string callbackName = "/" + parameter; 56 | 57 | updateFlags.Add(parameter, false); 58 | 59 | valueHolder.Add(parameter, 0f); 60 | 61 | server.MessageDispatcher.AddCallback(callbackName, 62 | (string address, OscDataHandle data) => 63 | { 64 | string parameterName = address.Substring(1); 65 | 66 | updateFlags[parameterName] = true; 67 | 68 | valueHolder[parameterName] = data.GetElementAsFloat(0); 69 | 70 | } 71 | ); 72 | 73 | Debug.Log("Added callback for " + callbackName); 74 | } 75 | 76 | 77 | } 78 | 79 | void Update() 80 | { 81 | 82 | foreach (string parameter in parameters) { 83 | 84 | if (updateFlags[parameter] == true) { 85 | 86 | visualEffect.SetFloat(parameter, valueHolder[parameter]); 87 | 88 | updateFlags[parameter] = false; 89 | 90 | //Debug.Log("setting " + parameter); 91 | 92 | 93 | } 94 | 95 | } 96 | 97 | } 98 | 99 | 100 | void OnDisable() { 101 | 102 | server.Dispose(); 103 | 104 | } 105 | } 106 | --------------------------------------------------------------------------------