Openframeworks inside Unity3d

Hey guys,
Lately, I have been trying to implement Openframeworks inside Unity as a Plug-in, so I searched around for a solution, and the only thing I found out was Vade’s implementation for Quartz Composer, which is really outdated. So I decided to compile Openframeworks inside a .bundle, so I could implement into Unity. I am sharing my first attempts and I will be happy, if somebody wants to upgrade on my source, because I am not really a hardcode c++ programmer. The problem I have right now is how to implement the Openframeworks pipeline (setup, update,exit) to sync with Unity, and how to share textures and etc. I would really love some input from someone who already has done this.

Here is a link to the XCode project: www.kamend.com/devel/OFUnity/OF0071Bundle.zip

You will probably have to fix some paths, to the Linked libraries, but it should work. After compiling, you should be able to add the .bundle file inside a Plugins folder in your Unity projects. There here is a sample C# script, that will get a basic functions running.

  
using UnityEngine;  
using System;  
using System.Collections;  
using System.Runtime.InteropServices;  
  
public class OFBundle : MonoBehaviour {  
	  
	[DllImport("OFBundle")]  
	private static extern float noiseX(float x);  
	  
	private float scaleNoise;  
	  
	// Use this for initialization  
	void Start () {  
		scaleNoise = UnityEngine.Random.Range(2,10);  
		  
	}  
	  
	// Update is called once per frame  
	void Update () {  
		scaleNoise += 0.02f;  
		  
		float nX = noiseX (scaleNoise);  
	  
		transform.localScale += new Vector3(nX,nX,nX);  
		  
		if(scaleNoise > 20) scaleNoise = 2;  
	}  
}  
  

Let me know, if you have any questions or input.

Cheerz,
K

1 Like

Just curious about this, can you elaborate a bit on how you are using both OF and Unity in your projects.

Hi.

Getting that OF -> QC thing working was a real pain in the ass. Unity is going to be trickier because of all of the OpenGL state management you are going to have to do.

What I had to do for OF / QC was to make a shim ofWindow implementation that ‘reset’ GL state before calling into the testApps implementations of update and draw.

With unity you’re going to be dealing with the fact that its pipeline is super optimized, and its going to be binding things like index buffers, VBO’s, textures and shaders and expecting them to stay bound. So you need to track what state is prior to you modifying it, then re-setting it.

Sadly the only way to do that (that I am aware of )is via glGet’s which generally stall the GL pipeline as they are a sync point.

Middle ware is hard. Way harder when working with black box situations too.

Check out : https://code.google.com/p/ofxqcplugin/source/browse/trunk/ofxQCPlugin/ofxQCBaseWindowProxy.cpp

esp neat line 58.

Looking at that now, its unsafe because update could very well be fucking with GL State.

Anyway, yea, pain in the fucking ass. Good luck. Getting something as simple as Syphon to play nice with Unity was a real pain in the ass, and its a moving target as they add features, optimize, etc, and Syphon does very little to GL state internally, whereas OF is all over the place, and not designed for middle ware at all (IE, setup state, leave it, fire forget mentality).

Oh, and if you want to run on the GL thread (which is not the main thread), you’re going to have to serialize access to your context. Sorry, not trying to say its unworkable, you should go for it, it will be a good challenge.

I never actually used this inside a real project and at the end I realized that it does not make a lot of sense, Openframeworks is too heavy to be used as a plug-in.

@Mat , I never really got further than the example I posted.

@vade Thanks for the reply, actually the QC Plugin example is what I used, when I was playing around.

Well, I dont know about ‘heavy’, but definitely mired with edge cases and not designed to do it. If you are paranoid about state management it should be workable, but yea.