Free Python module to create vectorfields, flowmaps for Unreal & Unity

Hi! A few weeks ago I wrote a small python module to create vector-fields and flow maps that can export to formats that Unreal Engine (FGA) and Unity (VF) use. I wanted to share it with you in hopes that it may be useful to someone.

I admit it’s not very artist-friendly because you have to use python commands to provide formulas for the evaluation of the vectors over a grid. This is because at the time I just needed a simple method of getting a vector-field for an electric dipole, without any additional software licenses or complicated setup.

If you have a python installation, you can simply install the module with pip install vectorfields. (If you use Anaconda and create a new environment, make sure to use conda install numpy beforehand.)

I already provided a few special classes like Vortex2D or ElectricDipole2D, but the classes you would be using most often are CustomUV2D and CustomUVW for which you provide functions. The 2D classes have a resolution of 1 on the Z-axis. You can get a cubic field from them with the .to_3d() method. Use the .save(<filepath>) method to export to either FGA, VF or a supported image format.
Find out more in the short Documentation.

Here are some examples:
Twirlflow
Electric Dipole
"Flowers"

I’d be happy if you want to contribute to the github repository. But feel free to use the code in your own projects.

And I would also love to see your creations using this.

19 Likes

Just a quick note for Unreal Engine Niagara users: For vectorfields to work with Niagara you should use the latest version 4.22, currently in preview. Otherwise you’re stepping into a world of pain.
Belt2D UE4 Example

1 Like

This is really cool! Will give it a try tonight :slight_smile:

If you have any suggestions or ideas, feel free to ask.
I’d like to see, if you come up with something. As you see, I haven’t explored the possibilities in parametric 3D vector fields yet or really exploited the flowmap export.

HI I have imported I am not getting any idea how to use. I need .FGA file for use in UNITY vectrorfield plugin. I have UVW but don’t know the structure of FGA so finding it difficult to realise in UNITY. Currently I am calculating field with meshgrid coordinates but when I create FGA using a class my 1000 points in three columns do not match the order as required by UNITY. i tried to use your module but not getting how to save and use

Thanks for trying out this module and I’m sorry it didn’t yet work out for you. I’ll try to help.
Are you not able to load the file at all or is the order of values just scrambled?

When I wrote this, the .vf binary format was thought to be used with the visual effects graph in the Unity editor and the .fga format was meant for use in the Unreal Engine 4.
I don’t have the JangaFX Plugin that converts FGA files for use in the Unity editor, so unfortunately I can’t test that.
If the plugin you use isn’t doing anything else than just converting a FGA formatted vectorfield to another format, you could try forgoing the plugin and exporting directly to VF format for use in VFX graph. There’s also a Houdini plugin for VF export.

The FGA (Fluid Grid ASCII) is a very simple, human readable format. The first line contains the resolution for each axis, the second and third lines contain the minimum and maximum size of the volume for each axis. The rest is just xyz values for each cell of that grid.

When I’m not mistaken Unity expects vectorfields to be cubic, so the resolution in each dimension would have be the same.

With my python module you have 2 options for the generation of the 3d vectorfield values. Either subclass Vectorfield and override the _set_uvw function, or use the CustomUVW class.
Here’s an example (haven’t tested this though!):

import numpy as np
from vectorfields import CustomUVW
ufunc = lambda x,y,z: np.ones(x.shape)  # Flow in one direction. Y and Z aren't used in this example.
vfunc = lambda x,y,z: np.sin(x)
wfunc = lambda x,y,z: np.zeros(z.shape)  # No flow in this dimension
vf = CustomUVW(ufunc, vfunc, wfunc, size=8, resolution=32)
vf.save('path/to/file.vf')  # Or use .fga extension, or even save_fga/save_vf functions.

Let me know if this was of any help.

Yes your code is working but I have not taken it into UNity but i have multiple lines of code to arrive at u,v,w So trying to figure out how to write my code in your lambda language.

You may not need to use CustomUVW class and think of some functions for its arguments. The lambdas are just anonymous functions.

When you already have your values for UVW, it’ll propably be easier to do something like this:
(I dare to post untested code again)

import numpy as np
from vectorfields import Vectorfield

class MyVectorfield(Vectorfield):
    def _set_uvw(self):
        # Do some preparations however you like.
        sq_sum = np.square(self.grid_x) + np.square(self.grid_y)
        divisor = np.sqrt(sq_sum)
        factor = np.exp(-sq_sum)
        # The important part is to set uvw.
        self.u = factor * self.grid_y / divisor - 0.5 * self.grid_x  # Replace this,
        self.v = factor * -self.grid_x / divisor - 0.5 * self.grid_y  # and this
        self.w = np.zeros(self.resolution)  # ...and this.

vf = MyVectorfield(size=8, resolution=32)
vf.save('path/to/file.vf')

Ok yesterday I tried with CustomUVW for magnetic field around straight current which basically involves constant rotation around wire.


When I used save_fga and tried in unity I didn’t get desired result.

Basically the arrangement of points in FGA file provided in Unity is not matching

Today I installed the latest stable versions of Unity and UE4 to have a look at the issue. Indeed, there’s something wrong in Unity. I suspect it to be the difference in coordinate systems. At a first glance it looked like the x and y axes have to be switched.

I exported the formula you provided as vf format and scaled the x-dimension of the emitter by -1 to correct the direction of flow because of the xy issue. I also set the tiling mode to clamp for the 3d texture asset.

I encourage you to try the VF format in Unity instead of FGA. I intended FGA to be used in the Unreal Engine 4 and not in Unity. I don’t know what the plugin you’re using is doing to it.
This is what I would expect:


In Visual Effects Graph, which you can add with the package manager in Unity, you can set up the particle system similar to this:

There wasn’t much rotation though. The particles just got propelled out of the bounding box of the emitter. Maybe it would work better with a higher resolution.

Unfortunately I don’t have much time right now to look into it more. I’ll have to pick that up later if nobody else is contributing to the code.

Ok I will try VF too

I tried this code using a same function I sent you as image but now the VF file is not readable and if I save as FGA all values except header are zero