img01 preview

Exchanging data between Unity and Grasshopper via UDP

  |   2015, research

The game-developer environment Unity is a great tool for creating not just games but also user-friendly interactive architectural designs. It however lacks a precise set of tools for physics simulation or geometrical transformations. Grasshopper, the plugin for McNeel Rhino, on the other hand has tons of add-ons that do exactly this — one or another kind of geometrically and performatively precise simulations or transformations. One look at Food4Rhino is enough to grasp the multiplicity of options.

So naturally, we’d want to use Unity to create a good looking, user-friendly interactive application and use Grasshopper for all the needed calculations running in the background. Which is why a fast, reliable method for exchange between the two would come in handy.

In this post we show you how to send and receive data from Unity to Grasshopper via UDP (short from User Datagram Protocol).

As an example we created the following interaction. The user sees a cube which they can rotate by holding the right mouse button and moving the mouse around. When the user hovers over the cube with the mouse cursor, the cube gets split into 4 smaller boxes. The dimensions of the boxes are such that their bases are the 4 rectangles resulting from splitting the side of the cube the mouse is on with the point where the mouse is, and their height is such that each new box’s volume will be a quarter of the cube’s volume.

The method consists of the following basic steps:

1. Generate geometry in Unity (in our case a simple cube).

2. Get the key values of the geometry you need to send to Grasshopper — in our case, the position and scale of the cube and the point on it where the user has hovered with their mouse cursor.

3. Encode these values as one message text string, adding them one after the other so you can unpack them later in Grasshopper (GH).

4. Send the message text string via the UDP client built in .NET over to Grasshopper by giving the IP and port on which GH is “listening”.

5. In GH, once a message is received, parse it, which unpacks the string into the original key values — the position coordinates, the scale values for X, Y and Z and the mouse-over point.

6. Use the values to reconstruct the geometry in Grasshopper, building the cube as a NURBS box and placing the mouse point.

7. Post-process or change the geometry in some way — in our case we use the mouse-over point and the side of the box it is on to split the box in 4 smaller boxes each having 25% of the original volume.

8. Get the key values of the changed geometry and collect them in one string message. This is similar to step 2 and 3 but now done in GH — in our case we prepare for sending the positions and dimensions for the 4 new smaller boxes.

9. Send the string message using UDP to the IP and port where Unity is “listening”.

10. In Unity, parse the message and use the values to reconstruct the 4 boxes, giving the user a live preview of how their cursor is splitting the original cube in 4 smaller boxes.

In Grasshopper we used the add-on gHowl to receive and send UDP messages. The C# UDP code in Unity is based on the code we found here.

Here are the GH definition and the Unity package for you to download, try and modify. Don’t forget to edit the IP addresses in both Unity and Grasshopper with the ones from you computer.

We’ll be happy to see how you used this exchange method in your work, so share in the comments bellow. If you like to receive updates for the stuff we do, go sign up for our mailing list.

img01 preview

img02 show

img03 unity-gh

img04 gh-unity