Robotlegs Away3D 4 Broomstick integration with a wink at Unity

Since the beginning of this year I got in touch with Robotlegs thanks to Erik van Nieuwburg. In case you have’t heard of Robotlegs or haven’t used it, you should really check it out sometime soon. It is a micro framework that helps separating your application tiers in a very elegant way. I’ve developed a couple of projects myself using Robotlegs and I really have to say that I’m loving it. Once you know the trick, it seems like a natural way develop your applications like this. It makes your life as a developer a lot easier.

Once I got this into my fingers I suddenly realized that how Robotlegs handles views with mediators is somehow similar to how Unity works with game objects (models aka views in Robotlegs) and components (scripts/predefined functionality aka mediators in Robotlegs). The big difference between Unity components and mediators in Robotlegs is that Unity can work with multiple components on an object and Robotlegs doesn’t.

With the recent public beta release of Flash Player 11 (Incubator) and SDK with the Molehill functionality (native 3D), the alpha release of Away3D 4 “Broomstick” and the announcement by Unity regarding Flash publishing, it became time to get started with this experiment.

Basically I wanted to achieve two seperate things:

  1. Marrying Robotlegs to Away3D
  2. Add multiple mediators to 3D objects similar to script components in Unity3D

In order to achieve this I had to make small adjustments to Away3D in order te get an event dispatched as soon as a model is added to the scene. By default no event is fired once a model is added to the Context3D (Aka 3D stage). This is because the 3D stage doesn’t have a display list. So I forked Away3D on GitHub and applied the needed changes and send out a pull request so that these changes could be implemented by the Away3D team. At time of writing this request is still pending, but I’m sure the Away3D team will merge this into their code anytime soon.

A new GitHub repository has been created under the name of “Robotlegs-Away3D-Extension“, which introduces several new classes. The most important once are

  • Context3D (not to be confused with native Flash 11 Context3D)
    You need to extend this class and use it as the context of your application
  • Mediator3D
    Similar to a mediator as we normally use in Robotlegs, only this type is for 3D objects and not for view components.
  • Mediator3DMap
    As you probably have guessed it, this is similar to the MediatorMap, but maps zer0, one or multiple Mediator3D instances to a 3D object that is present in a scene.

So with this available you can do things in your context like:

threeDeeMap.mapObject3D(Car3D, [BoxCollider, SimpleSteerBehaviour]);

Which says in fact that whenever a Car3D is added to the scene, we need to create an accompanying BoxCollider and a SimpleSteerBehaviour. The latter two are extended Mediator3D classes and are connected to the Car3D. This separates views from functionality and goes even a step further than Robotlegs does by default, as we can have multiple mediators for one single thing we want to display and is consistant with the “Single Responsibility Principle (SRP)” where one class is only responsible for doing one type of actions. In this case functionality of steering has nothing to do with detecting collisions. Although you possibly want to use both, they both do other types of operations and should not be merged into a single class. As this will make classes longer and harder to understand.

As I mentioned earlier, this work is available on GitHub, as well as an example project on how you could use this. The work is far from being perfect and should be improved when I decide to further develop on this (or when someone forks it and continues development :-) ). What interests me the most at this stage is to hear from others whether this approach would make any sense or not and how it could be improved concept wise.
Besides, and here comes an extra wink at Unity, I hope that Unity gets inspired by this approach and possibly even adopt the use of Robotlegs. As if Unity publishing to Flash isn’t awesome enough, I think such approach has the potential to become a mind blowing awesome combination. This also means that I am voting for creating a code based build instead of just compiling a SWF file. With code publishing you leave so much more power in the hands of the developers to integrate the 3D content with 2D content. Also integrating external tools such as FDT, Flash Builder or Flex builder and the Flash IDE itself would an interesting approach.

So lets finally sum up some relevant links:

And again please, if you have any thoughts on this that you’d like to share. Please let me know. I think it would be good to get the debate going and find out what would be a powerful and elegant way to manage your 3D content with Robotlegs.