Bootcamp

From idea to product, one lesson at a time. To submit your story: https://tinyurl.com/bootspub1

Generative AI for Node-Based Shaders

--

Creating visual effects in games often feels like a delicate balance between technical precision and artistic vision. I’ve spent the past year exploring how artificial intelligence can bridge this gap, making shader development more intuitive and efficient for both artists and programmers.

Showcase of my AI assistant for Shader Graph

Note: This research was conducted as part of a commercial project, and while I’ll be sharing our findings and methodology, the actual implementation and code remain proprietary.

The Challenge with Traditional Shader Development

If you’ve ever worked with Unity’s Shader Graph or similar node-based systems, you know the drill: hours spent connecting nodes, tweaking parameters, and iterating until you achieve the desired effect. While node-based systems have made shader development more accessible than pure code, they still present a significant learning curve and can be time-consuming to work with. To make matters more challenging, each platform’s node system comes with its own naming conventions — what Unity calls a “Split” node is labeled as “Separate RGB” in Blender or “Break Out Components” in Unreal, forcing developers to mentally translate between different terminologies when switching environments.

Unity Shader Graph Simple example

Consider Unity’s VFX Graph for particle effects — even a basic explosion requires configuring numerous parameters: particle lifetime, spawn rate, initial velocity, size over time, color gradients, emission shape, and various force fields.

Enter AI: A New Approach to Shader Creation

(Ah yes, AI — the solution to everything™!) What if you could simply describe the effect you want and have AI generate the appropriate node structure? This was the driving question behind my research. Rather than reinventing the wheel, we chose to build upon existing node-based shader systems, leveraging their proven strengths: visual debugging, real-time feedback, and the ability to make quick adjustments through node parameters. The goal was to enhance, not replace, these powerful visual tools.

The system works by:

  1. Taking natural language descriptions of a desired effect
  2. Processing them through advanced language models ( In our case GPT-4o and Gemini 1.5 pro)
  3. Generating appropriate shader code
  4. Converting that code into Unity-compatible node graphs
Package interface and graph window

How It Works Under the Hood

The magic happens through a combination of prompt engineering, retrieval-augmented generation (RAG), and careful parsing of the generated code. When you input a prompt like “Create a pulsating red circle,” the system:

  1. Retrieves relevant examples from its knowledge base
  2. Generates optimized function-based shader code
  3. Parses them into the Unity Shader Graph JSON format
  4. Provides options for refinement and iteration
// Example of generated code for a particle system
float lifetime;
RandomInRange(2, 5, seed, lifetime);
vec3 position, direction;
RandomPositionInSphere(0.5, position, direction);
// ... additional code ...

The Main Challenge: Constraining AI to Node-Based Functions

One of the most significant hurdles in this project wasn’t just getting the AI to generate shader code — it was getting it to think exclusively in terms of predefined nodes. Even for basic operations like adding two numbers, the system needed to use specific function calls (e.g., Add(arg1, arg2, result)) instead of natural arithmetic operators (a + b).

Why was this so important? Node-based systems work with a fixed set of visual nodes that represent operations. There’s no way to use direct mathematical operations — everything must be a node. This meant our AI needed to translate even the simplest calculations into their node-based equivalents:

# What the AI wants to write:
finalColor = baseColor + offsetColor;

# What it needs to write instead:
Add(baseColor, offsetColor, finalColor);

Initially, we explored Supervised Fine-Tuning for the language models. However, with limited shader examples available, this approach didn’t yield satisfactory results as the model became too biased toward reproducing only those specific examples, losing its ability to generate creative solutions. Note that we didn’t experiment with newer techniques like reinforcement learning-based fine-tuning, which might offer better results.

Instead, we achieved decent results through careful prompt engineering. We provided the AI with a comprehensive list of available nodes and their exact function signatures, along with explicit instructions and examples showing correct and incorrect usage. The prompt essentially taught the model to “think” in nodes rather than traditional programming constructs.

This constraint was crucial for our parser to successfully convert the generated code into actual Shader Graph nodes, ensuring that what the AI generated could be directly used in Unity’s visual shader editor.

You might wonder why a parser is needed. In engines like Unity, there’s no straightforward API to generate a Shader Graph file (as I found through research and inquiries). I ended up analyzing Unity’s Shader Graph JSON structure and parse the code into that format. If we had more access to the engine’s API, we could bypass the parser and directly call the graph functions to create the nodes. However, node connections and positions should be managed.

Real-World Results: From Simple to Complex

Testing revealed interesting insights about AI’s capabilities in node-based shader development. To evaluate the results, I used a similarity score (ranging from 1–10) that measures how well the generated shader matched the user’s intent. A score of 1–3 indicates significant deviation from the desired effect, 4–7 suggests the shader needs minor adjustments but provides a good starting point, and 8–10 represents a highly satisfactory match to the intended outcome.

Here’s what we found:

Task-Based Prompts

Specific and sequential requests like “create a rotating gradient” achieved nearly perfect results (9.6/10 similarity score). Whether the task is described in a short prompt (“rotating gradient”) or with detailed specifications (“create a gradient that rotates clockwise, completing one rotation every 2 seconds”), the system consistently generates efficient, well-structured node graphs. For users who understand what nodes they need but want to skip the manual setup process, this capability can significantly accelerate their workflow, turning minutes of node connection and parameter setup into seconds of prompt writing.

Creative Descriptions

More abstract prompts like “create an aurora borealis effect” showed mixed results but often led to unexpected and creative solutions. While the similarity scores were lower (around 5.2/10), the generated shaders often solid starting points for further refinement.

Initial graphs for the Aurora Borealis prompt. Gemini uses a more creative and accurate approach to handling descriptive prompts

Particle Systems

The system showed particular promise with particle effects, generating both spawn and simulation code that could create complex behaviors like “swarms of dancing fireflies” or “colorful tornados.”

The Power of Iteration

One of the system’s strongest features is its ability to refine and modify existing shaders. Users can request changes like “make it more chaotic” or “slow down the animation,” and the AI adjusts the node graph accordingly. This iterative approach allows for rapid prototyping and experimentation.

Refined graph for Aurora Borealis effect with edge masking and adding a custom mesh

Current Limitations and Future Potential

While the results are promising, it’s important to acknowledge the system’s current limitations:

  • Generation time averages around 8–9 seconds, which might be too slow for real-time applications
  • Abstract descriptions can lead to varied results, requiring more specific prompts for consistency
  • Complex effects might need breaking down into smaller, more manageable requests

Looking Forward

This research represents just the beginning of AI’s potential in shader development. As language models continue to evolve and our understanding of prompt engineering deepens, we can expect even more sophisticated tools that bridge the gap between creative vision and technical implementation.

The future of shader development might not be about replacing human creativity but rather augmenting it — providing artists and developers with powerful tools that turn their ideas into reality more quickly and efficiently than ever before.

--

--

Bootcamp
Bootcamp

Published in Bootcamp

From idea to product, one lesson at a time. To submit your story: https://tinyurl.com/bootspub1

Ahmad Erfani
Ahmad Erfani

Written by Ahmad Erfani

I dabble in coding, art, game design, and computer graphics. Always learning and creating. Find me at ahmaderfani.com.

No responses yet