How to integrate VTK with Unity

Hello there, pretty new to the forum and VTK

I’m working on a project that need advance rendering in Unity. I’ve thought this would be a achievable through VTK and i still think it can’t be that hard but i’m stuck.

There is the VTK External Rendering module and Unity provides a low level plugin interface where C++ can be used. There’s an example that uses OpenGL calls to manipulate vertex buffers and textures in Unity’s OpenGL context. I’m following the approach described in this paper Virtual Interaction and Visualisation of 3D Medical Imaging Data with VTK and Unity

So, I made a Unity script that uses a command buffer that calls a vtk rendering function every frame at (I think) the correct state of OpenGL rendering. This function is imported from a dynamic library which has the VTK code.

Unity code:

private static extern IntPtr GetRenderVTKFunc ();

CommandBuffer buf = null;

// Start is called before the first frame update

IEnumerator Start() {

    buf = new CommandBuffer(); = "VTK CommandBuffer";

    Camera.main.AddCommandBuffer(CameraEvent.BeforeImageEffects, buf);

    yield return StartCoroutine("callPluginEndOfFrame");

private IEnumerator callPluginEndOfFrame() {

    while (true) {
        yield return new WaitForEndOfFrame();

        buf.IssuePluginEvent(GetRenderVTKFunc(), 1);         

Fragment of VTK code:


vtkNew cs;
vtkNew mapper;

m_CubeActor->GetProperty()->SetColor(1, 1, 0.2);
m_CubeActor->SetPosition(0.5, 0, 0);

// create the VTK external renderer
vtkNew renWin;
m_externalVtkCamera = vtkSmartPointer(

//Function that is called by Unity

void RenderAPI_OpenGLCoreES::UpdateVtk(const double viewMatrix[16], const double projectionMatrix[16]) //std::string &debugStr)



When executing this the following OpenGL error pop up

OPENGL NATIVE PLUG-IN ERROR: GL_INVALID_OPERATION: Operation illegal in current state

Any thoughts would be appreciated

Thanks in advance!


My guess is that Unity is expecting the external plugin to create the OpenGL context, but I don’t see that happening here in the VTK code. Could you please link the relevant Unity documentation?

Cc: @ken-martin

Unity low level interface plugin doc: Low-Level interface plugin

Thanks for your response! It’s a possibility but I don´t think thats the case because in the example provided by Unity in the documentation the c++ code only draws (through glew) on the OpenGL context created by Unity

Low-level interface plugin example (non VTK rendering)

I have a little bit more info about this. In case someone can help me.

I’ve been doing some debugging and i get this error:

glDrawBuffer failed because ‘0x405’ is not a compatible or supported buffer type under the current framebuffer configuration (GL_INVALID_OPERATION)

This error is triggered by the glDrawBuffer in the render function where doublebuffer is active in vtkExternalOpenGLCamera. There:

if (ren->GetRenderWindow()->GetDoubleBuffer())

  // Reading back buffer means back left. see OpenGL spec.
  // because one can write to two buffers at a time but can only read from
  // one buffer at a time.

I’ve doing some researching about framebuffers, back and front buffer but i’m struggling to understand it properly.

Thanks in advance

I’m out of my depth at this at this point too. @ken-martin or @utkarshayachit?

Hi Oscar!
I am integrating the VTK with Unity, any luck you have already solve this issue?


I am one of the authors of the paper on the first post -just wanted to inform that we have released part of the code open source and can be found here:
If you have any problems please let us know through the issue system in gitlab, or here and we will try to help.


Hi Alberto, I just wanted to ask, with this framework, is it possible to load .vtp (poly data) files in Unity?

Yes it is.

If you have further question, please open your own topic.