Course contentsShow
← Unity Game Development
Lesson 679 of 82810. VFX and Post-ProcessingFree lesson

Passing Data Between VFX and Compute

Establish data pipelines to pass buffer information from compute shaders into the VFX graph to influence particle behavior in real-time.

In this lesson, you will learn how to synchronize data pipelines between Compute Shaders and the VFX Graph to drive particle behavior with high-performance GPU calculations.

The Pipeline Bridge

When working with the VFX Graph, you are operating entirely on the GPU. If you need to perform complex physics simulations, fluid dynamics, or custom grid-based logic that doesn't fit into standard VFX nodes, a Compute Shader is your best tool. The challenge is "handshaking" that data: how do we send the results of a Compute Shader directly into a particle attribute?

The secret lies in the GraphicsBuffer. Instead of reading data back to the CPU (which is slow), we keep the data on the GPU memory and map it to a property in the VFX Graph.

Implementation Workflow

To pass data, your Compute Shader must write its results into a StructuredBuffer<T>. In your C# controller script, you create a ComputeBuffer that matches the structure of your data.

// Inside your C# controller
ComputeBuffer particleDataBuffer = new ComputeBuffer(count, stride);
computeShader.SetBuffer(kernelIndex, "ResultBuffer", particleDataBuffer);
// Dispatch your shader...
// Assign to VFX Graph
vfx.SetGraphicsBuffer("InputBuffer", particleDataBuffer);

Once the buffer is assigned, you access this data within the VFX Graph using a Custom HLSL Block. Inside the block, you define the buffer as a StructuredBuffer to read the specific values you calculated in the shader, such as custom position offsets or velocity vectors.

Bridging the Gap

By using a StructuredBuffer, the VFX Graph can index into your computed array using the particleID. This ensures that each particle reads the specific calculation meant for it. Because both systems are writing to and reading from VRAM, you bypass the CPU bottleneck entirely, allowing you to influence millions of particles simultaneously based on complex, frame-by-frame compute logic.

Key Takeaway: By binding a ComputeBuffer directly to the VFX Graph via SetGraphicsBuffer, you create a high-speed data pipeline that allows custom compute logic to drive particle attributes in real-time without CPU intervention.