Rendering Plugins with Surfaces

Okay, I’m curious to get feedback including thoughts from @matterhorn103 and @brockdyer03 in particular.

I wanted to add a ray-tracing option for an Avogadro plugin and found Mitsuba - a “path based” render code that’s on PyPI.

The problem is that surfaces aren’t sent along with the CJSON sent to plugins at the moment. In general, for export to various rendering formats, we’ve used a “Visitor” class in C++ that gets all the render bits (e.g., draw a red sphere at this position, radius, etc.) including surface meshes.

I’d really like to export surface meshes. One option is to add an option to the plugins to request a scene export, e.g.

[[tool.avogadro.menu-commands]]
identifier = "render"
menu = "File"
submenu = "Export"
item = "Path-Traced Image (Mitsuba)..."
input-format = "cjson"
# Avogadro 2.2 adds a glTF "scene" to the input (older versions ignore this)
input-scene = "glb"

I think this is a good backwards-compatible way to allow rendering commands - Avogadro will export the scene through the glb / glTF format including surfaces and colors and then the render plugin can do whatever it wants.

Thoughts? Better ideas?

Sounds very cool, and if Avogadro can already produce a gLTF or glb, hopefully fairly straightforward to implement as well.

A few points/questions:

  • I think it’d open up even more opportunities for plugins to do useful stuff if the plugin can also send a surface back after editing it. Then plugins could be written that change the general rendering of Avogadro. Is that feasible?
  • When a plugin receives the scene, can it distinguish between the different kinds of surface (the atoms/bonds, any orbitals, etc.)? If not, would it be an idea to let the plugin choose which kinds of surfaces it processes via a given menu command?
  • Is there any particular reason to have the value of the input-scene key be a string rather than just true?

It’s a format. In principle, there could be other formats, e.g. PLY, etc.

It gets the atoms and bonds separately (e.g., CJSON) and it gets the surfaces that are currently in the scene. So I’m not sure why you’d need a menu command - it processes whatever surface / cube the user has generated.

Hmm. I think we’ll need some benchmarking. The reason I’ve avoided having meshes is because they can get really big depending on resolution and the size of the molecule. Some of the rendering benchmarks are showing me that we’re uploading ~1GB for rendering protein surfaces.

It’s a reasonable long-term question though. I can imagine “reduce the mesh” or analysis options could be useful (surface area, volume, etc.)

I’ll push the change for now and we can see where it leads before v2.2 release. :slight_smile:

But the atom spheres are also amongst the surfaces that are exported, no? Or is it only the extra ones created via Generate Surfaces?

Fair enough. Even if it’s not possible to “change the general rendering of Avogadro” though, it would still be useful to let plugins change the way the current surface is rendered, no? And it not be a permanent thing, just a one-time action that only lasts until Avogadro has to re-render the surface for whatever reason? Because from your description it sounds like the only things plugins can do with a surface they receive is render it externally to Avogadro, that they can’t change it and pass it back.

What’s the use case you have in mind? I can potentially see some use in modifying the mesh, but as I said, it’s a lot of data in many cases going each direction.

You understand that can happen 60 fps… Things that affect the rendering pipeline need to be really efficient. That’s why I’ve been working with Claude and other LLM (Google Gemini this week) to profile, test and optimize the rendering speed.

It’s not that I’m opposed to allowing a plugin to modify the mesh - it just helps to know a use case vs. “run ELF analysis on the current electron density and return a cube”

It’s entirely possible I’m completely misunderstanding the rendering pipeline here and what’s actually being proposed.

I was assuming that Avogadro doesn’t generate a surface from scratch (e.g. from a density or from orbital coefficients and atomic orbitals) every time the view of the scene needs to be re-rendered. I assumed that the surface is calculated once, then stored as some sort of intermediate representation of the surface itself, and that is used as the starting point for rendering it in each frame.

I also figured that this intermediate representation was what you’re proposing passing to a plugin, and that this intermediate representation already includes properties of the surface, not just its shape. I was conceptualizing glTF as being basically like an SVG, in that it contains both mathematical descriptions of objects (paths in SVG, meshes here, I suppose) and their properties like colours etc.

Your proposal seems to mainly be about enabling plugins to overtake the rendering step, by handing them the intermediate representation, from which they can then produce a 2D image to save to file.

I think that you think that I’m suggesting we make it possible for a plugin to handle the rendering step for Avogadro – the plugin takes a glTF in, renders it to a bitmap, hands it back, Avogadro displays it – all the time, for every frame. That’s why you’re talking about performance, because naturally a plugin couldn’t really do that quick enough.

What I actually meant was that if you are giving the intermediate representation to a plugin, how about letting it return an alternative intermediate representation that it has modified in some way? And it’s Avogadro that continues to handle the rendering.

Reducing the mesh would be one thing that could be done like this, but like I say, I imagined that this intermediate representation already included things like colours, opacity, reflectivity, etc. and that it might be useful to have plugins that change those things.

Naturally if all that understanding is nonsense, the suggestion doesn’t make any sense at all :sweat_smile: