Everyday 3D

Creative use of technology // A blog about 3D Flash and Actionscript by Bartek Drozdz

Tool Xmas Card – realtime video compositing in WebGL

This Christmas, at Tool we wanted to create a small interactive experience to share with our friends and clients. Since lately I did experiment with compositing WebGL objets on a video [1, 2] I thought this is a cool technique that we can use.

The idea was simple enough: we would shoot a Christmas tree in a nicely decorated room and composite-in a gift box that the user can interact with while watching the video. All this is rendered with WebGL – the video runs in the background and the 3d interactive content on top, both layers are matched in perspective and movement.

To achieve this effect I had to use quite a lot of different pieces of software. Here’s a breakdown of what it took to build it:

Cinema4D. First of all, I needed to match the perspective of the camera in the footage and that of the camera in the 3D scene. There is no exact science in doing that, so the best way is to take a frame from the video, use it as background in C4D and try to match manually.

It’s a trial and error technique and adjusting the details can be quite a challenge. Fortunately, I found a good book about matchmoving with some very useful tips… like that one about writing down what lens was used during filming (I forgot about that, of course… :)

Mocha. After matching comes tracking. At first I wanted to do full camera solve, but it turned out to be quite complex and not necessary in this case, so I went with 2D tracking instead. A very good tool to do this is the Mocha AE Plugin – it is easy to use, accurate and fast.

Using 2D tracking means that we only track movement on the XY plane. We also do not account for any rotation of the camera. For handheld shots, where there’s only slight camera movement, it is good enough. Of course this would never work for tracking or panning shots – these require full camera solve.

Once the tracking is done and tweaked, Mocha allows to export the tracking data into a text-based format. After this, all I needed was a simple Python script to turn this data into a nicely formated JSON.

Unity3D. Once the 3D model of the gift was in place and all the camera angles were matched in C4D, I exported the whole thing to Unity3D. The main reason for this, is that I wanted to take advantage of the Unity/WebGL exporter to get that to WebGL.

I also used Unity’s animation editor to create the movement of the box when the cap flies off and the nutcracker pops out. Do do that, I added some functionality to J3D to support animations. It can be found in the v2 branch of J3D. One of the main changes in the engine I had to make, was to switch from Euler angles to Quaternions.

FFMpeg. In order to correctly track video and overlay any elements with precision, I needed to know the frame the video is at at any time during playback. The easiest way is to take the current time and divide it by the duration of a single frame (ex. 1/24 seconds). Unfortunately it is not that simple! If you want an in-depth look why it is so complicated please read the excellent article by Zeh Fernando. Even though his article talks about Flash, same thing applies to HTML5 video.

Long story short, each video used in a tracked shot needs to have the number of the frame encoded into it. The best way to do this: encode the frame number as binary marker somewhere in the video. To see how it looks, try playing one of the videos directly in your browser. See those white boxes on the bottom? This is it!

Python/PIL. Adding the binary marker to the video can be painful if done manually. This is where Python comes in. Using a library called PIL (Python Imaging Library) I wrote a simple script that does the following:

  • decompose a video clip into a sequence of PNGs (using FFMPEG)
  • manipulate each image by adding the binary frame number at the bottom
  • encode the frames back into a video optimized for HTML5 (FFMPEG again)

On the Javascript side, I use a simple technique of copying part of the video into a canvas and reading the color of the pixels to determine what frame we are at. And of course I make sure to mask the marker.

Video encoding tip. One thing to consider when adding the frame number marker is that video playback is optimal if the pixel size of the video is modulo 16, for example 1024 x 576. Influxis posted a list of all the optimal video dimensions (again, Flash or HTML5 – same rules apply).

Now, if your video has an optimized pixel dimension it would be a shame to add a few pixels with the binary marker and get a resulting video that is no longer optimized. It’s usually better to add the marker over of the video. You will need to sacrifice a few pixels of the footage, but a better playback speed will make up for that.

WebGL. With the Unity exporter getting the scene to render in WebGL was simple. The one big thing left at this point were custom shaders that would make the gift box blend well with the video. In fact is was the most challenging part od the project! The shader I ended up using on the box has diffuse and specular lighting with a specular map, reflections with a reflection map and a normal map to make the gift wrap look as realistic as possible. Finally, I added a bit of personalization – if you type your name after the # in the URL it will be rendered on the label on the box (example).

Sound. Last but not least: our friends at Plan8 added some great interactive sound FX that, as always, add a lot to the final effect.

It was fun to create this. I feel like this technique can definitely have some interesting uses. Of course, nowadays, anything that requires WebGL is treated as experimental, but once the majority of browsers can render that… What do you think?

A day in L.A.

Recently I have been dealing a lot with video. Tool, the company I work for here in LA, specializes in live action content. We do many different interactive projects, but video always plays a role in it.

I have never been too much into film, but when in Rome… so I started by looking at different techniques to blend video with interactive – like applying filters in realtime, tracking motion in 2D and 3D and compositing video with realtime content. I love to learn new stuff and some of the software used in VFX is pure magic – like ex. Mocha. I hope that I’ll have time to post some demos soon.

Meanwhile, I realized that I never made a single video myself and that it might be worth trying doing just that: to grab a camera, go out and shoot something, then edit it and put together a short piece. I decided to skip the interactive part altogether for the moment and just focus on filming.

It turned out to be a pretty cool experience. You can watch the humble results above. If nothing else, it is a great way to move away from the keyboard for a while, go out there and see some live humans… I highly recommend!

User stream (Webcam) + WebGL

HTML5 is full of surprises these days! I had no clue that there are browsers out there that already support user stream (i.e. webcam). This was always something missing in HTML5 compared to what Flash could do.

Fortunately, Kevin Sweeney posted a great article on Creative.js on that subject. After reading this I thought that there must be a way to use the webcam image as texture in WebGL. And there is! :)

To run the demo above you will need Chrome Canary with MediaStream flag enabled (detailed instructions can be found here). I was also able to run this demo in Opera Labs Camera build, which you can grab here. In both cases the performance was great!

Super cool updates

Welcome back everyone!

I haven’t posted for a while, but the reason is simple: I’ve been busy. I don’t find enough time these days to write long articles like I used to. I have a lot of things to post about though, so my plan is to make my articles shorter :) For start, here’s a quick update of what I have been doing since last July.

WebGL and J3D

I have been working a lot with WebGL. I created my own engine – J3D. I was very excited to see that it aroused some interest in the community, but I also found out that building a 3d engine is a huge task, especially if this is only a side project.

Currently, I push updates to J3D on a regular basis but I hope other developers will want to contribute as well. Besides more features, it needs some refactoring, optimization and documentation, all of which I can’t do on my own in a reasonable amount of time. You’re more than welcome to fork it on Github!

OnGameStart

Last September, I had the privilege to speak at my first HTML5/JS conference – onGameStart. It took place in my hometown, Warsaw.

It was an exceptional conference for me. I had the occasion to meet a lot of great developers from the HTML/JS community and also show my 3d engine for the first time. Here’s a video from my presentation. onGameStart will be back in 2012, so be sure to check it out – it’s really worth it!

Wait, there’s more!

In the last 4 years I lived in Stockholm and most of this time I have been freelancing. It has been a great time. I had the chance to work with many great companies, including North Kingdom, with which I did two projects that I’m really proud of – the Woodbot Pilots and Rome.

Things always change however and I felt like it was time to move on. So, last fall, I left Stockholm for sunny Los Angeles and I took a full time job at a digital production company called Tool.

Forever 3D? Probably not

This blog has always been focusing on realtime 3d graphics. I started with Papervision, moved over to Away3D, then to Unity and finally to WebGL. Along the way, I had the chance to learn a lot about 3d programming and the chance to teach myself some 3d modeling as well. All this was a great experience!

Lately, I’ve been involved in other areas of interactive design, like 2d, animation, sound and networking and there is a lot of interesting things going on there, especially on the web standards side of things. So you may expect some diversification in topics from now on!

It’s good to be back.

WebGL experiments cont.

WebGL never cease to amaze me. The combination of great performance, powerful features (even if they’re not safe :) and the immediate mode of development makes it the perfect tool for joyful tinkering with code, mathematical formulas and visual effects. Here are some of the experiments I did recently:

You’ll need Firefox 4/5 or Chrome and a GPU that supports WebGL to run the examples.

There is more here. I keep adding new stuff almost every week so be sure to check that page from time to time. All the demos are made using a little framework of mine called J3D (didn’t I mention it already?) It is available on github along with the source code of all the demos.

Instead of writing a long article about WebGL pros, cons or whatever, let me just say that if you ever had any interest in realtime graphics, make yourself a favor and try WebGL right now!

ROME “3 Dreams of Black”

I’m happy to announce that “3 Dreams of Black”, a project I was working on for the last 5 months has been released last week.

“3 Dreams of Black” is an interactive music video directed by Chris Milk for Danger Mouse and Daniele Luppi. It features a song from their latest album ROME, performed by Norah Jones. It’s part of the Chrome Experiments series and was created entirely with HTML5 and WebGL.

This is the second time I collaborated with North Kingdom. It was a big project, and other parties involved were the team from Google Creative Labs, including Aaron Koblin and Mrdoob as well as the teams from Mirada L.A. and Radical Media. I would like to take the occasion and say thanks to guys at North Kingdom for letting me be part of it. I have never been working with such talented group of people!

The project was a big challenge and an incredible learning experience. I intend to write a longer post with more details as soon as I get some time. Meanwhile go to ro.me and enjoy the show!

After that, be sure to check the tech page which features information on the technology we used and a very cool model viewer. You can download the entire code base from there too – the project is open source! Mirada prepared a great case study that is worth reading as well.



  • FATC2011


  • FITC2011


  • FITC2010


  • FITC2010