Wednesday, August 31, 2016

Compositing rendered object onto a photograph - part 1

How to composite a 3D model onto a photograph
(and get away with it)

Part 1 - Preparation and Scene building

A friend of mine asked me how am I making those composite renders, and since it seems that I've only been doing those lately, I decided to write a small tutorial on the subject. I'll be showing you my method, which is slightly different than what Andrew Price showed on his Blenderguru site. A good thing in doing CG work is that there's a multiple ways to achieve the same result, so consider this as just another way to do so. In the end you're free to use whatever method you find most comfortable to work with, as long you're happy with the result.

            When compositing rendered 3D models onto a photograph there are a few things you need to consider to get a believable result. I tend to think of all of them to be important (more or less), so in that order, here they are:
-          lighting
-          perspective
-          shadow
-          color match
-          noise


            This one is pretty obvious. Set wrong light angle and/or direction and it'll stick like a sore thumb. That's also the case with intensity and contrast, but that can be tweaked in post-processing phase. Nevertheless, the more you can match right away the less work you're going to need to do later.


            Perspective is also important in a way if you don't match it correctly it'll be obvious that the rendered object doesn't belong into the scene. While you can get away with only close approximation it's always good to try and match the perspective as close as possible.

I use two ways to do that: first is EXIF information in a photo that I'm compositing into: camera type (for sensor size) and focal length (you can also find aperture size, ISO and shutter speed useful if you're using physical camera add-on, but it's not crucial). Then I enter those values into camera properties in Blender. After that, I manually position the camera where I need it to be.

Second is BLAM camera add-on, which uses grease pencil to draw perspective lines on the photo which are going to bee used to calculate focal length. It also positions camera accordingly in world space (vertical and horizontal rotation), but I found it to bee hit and miss. For best result use uncorrected photos, otherwise calculation will not be accurate. I'll be showing you both methods in this tutorial.


Also, very important, but not that hard to do. Once you get the light direction right, it's just about catching that shadow for compositing.

Color Match

            Rendered element needs to match the rest of the photo colorwise. Discrepancy can occur if you're using an HDRI which has slightly different lighting than the photo. So, for instance, the photo could have reddish overall color, and the rendered element ends up with slightly bluish tint. For more immersion you're going to need to reduce blue color on the rendered element to match the photo. This is easily done in post-process phase.


            I add noise for even more realism, because nothing screams CGI more than perfectly sharp and clean photo. I found a good and simple method to add noise that matches that in the photograph, and it can be tweaked as desired.


            Before starting, obviously you need to have an idea what you want to do. Right now I'm into airplanes so I'll be making a scene featuring one. So, first, we need to find a model we want/like. For this tutorial (because I like it A LOT) I'll be using Chris Kuhn's newest F-14D fighter plane model. Since it's a paid model, you can download his free F-18 model from Blendswap (or any other you'd like).

            Second, we need a scene. Because aerial composite wouldn't feature a shadow (duh!) we'll use a ground photo, preferably that of an airport or something. I have found a good photo that matches my idea using It's a search engine for free stock stuff, like images, sounds, etc... that can be used for commercial and non-commercial purposes. Check it out...

             And this is the photo I found: 

Photo credit: Willtron via / CC BY-SA

            It's a Gibraltar airport runway. It's nice that there was an airport name next to the photo, because it'll help us to match the size of the airplane to the size of the runway. With no close objects for reference you can easily end up with an RC model on the tarmac instead of full sized plane... ;-)
                 You'll also need some HDRI that matches the photo. This one looks close enough:

            It's not perfect, there are more clouds in the sky here, but because the airplane is not as reflective as a car for example, we can get away with it. I think the sun's angle and the sky color are OK.

            Finally we can start creating our scene...


            Now we're coming to the real stuff. We're going to match the perspective of the photo and set our camera first, so we can position the airplane to match our photo. Add a background photo you downloaded and set its view to camera. Then add a camera and switch to camera view. You can rotate the camera to be parallel with Y-axis right now, since the one on the photo is also parallel to runway. Also, set render resolution to match that of the photo to avoid distortions:

            Now I'll show you both methods I use for matching the perspective. I always use the EXIF information first, whenever I can. If there's no EXIF embedded in a photo only then I use the BLAM add-on. So first, the EXIF method. For this particular photo the EXIF information looks like this:

            We can see the photo was taken with Nikon D40 camera at 18mm focal length. Quick google search tells us D40's sensor size is 23.7 x 15.5 mm. Now we'll enter those values into camera properties:

            The perspective was OK, I just needed to rotate the camera manually so the grid lines matched the markings on the tarmac. You can see green Y-line near the right hand side black line. Perfect match.

NOTE: If you're using a physical camera add-on you can also enter shutter speed, ISO and F-number values, which should help with exposure. Without that we'll just eyeball the lighting intensity.

            Now the BLAM method. You can find the add-on here: Right now it isn't really needed, but eventually you'll come across the image with no EXIF info and this add-on will help a lot.

            A few things to consider: BLAM uses X and Y-axis on the photo to determine relations to X and Y-axis in Blender scene. That's why we need to draw 2 lines for X and 2 lines for Y-axis on the photo which BLAM will use when calculating perspective. Because of that it's important that you find lines in the photo that you know/can tell are perpendicular in real life, otherwise calculation will be off.

            First, go into Movie Clip Editor in Blender and load our background image. Under T-toolbar -> misc there are BLAM settings. In N-toolbar, scroll down to Grease Pencil and add a new layer, change stroke color to red (for X-axis) and draw two lines on the photo that match X-axis. Do the same for Y-axis: add new layer, change color to green and find two lines to draw on. When you're done, hit „Calibrate active camera“ in BLAM settings in T-toolbar.

            The add-on will automatically enter calculated focal length into camera properties (fig. 3.). As you can see, it calculated focal length pretty accurately (accuracy will depend on how precise you draw the lines, whether they're perpendicular in real life and if the photo has significant lens distortion). Obviously EXIF data will give you exact values, but without those BLAM should get you close enough without tedious eyeballing.

Just to get the scale right, I googled the runway size of Gibraltar airport (46m) and added a plane with the width of the runway. Next, moving only camera, I positioned calibration plane to sit between the runway edges on our photo.

That put blender camera at around 1.75m height. I know that center of the runway is slightly elevated compared to its sides, to facilitate rain drainage, so taking that into account I could lower both the camera and the runway edges cca 10cm down which would make things even more accurate but the difference doesn't really show here so I left the camera as it is.

Now we are going to import our model, so we have something to use as a reference when matching the light. I decided to add two airplanes in really low flight above the ground coming toward the camera. Looks better than featuring parked plane with nose or exhaust tubes looking at the camera. Sideway shot would be out of place at this spot on runway, IMHO.

            I made one of the airplanes break off formation, just to add some action to the scene.

            Now we set up the lighting. In environment tab add 3k HDRI from downloaded ZIP, and set up light intensity control in node editor like this.

After some experimenting it was clear that with only HDRI we wouldn't be able to get strong shadows and the overall light had too strong bluish hue, so I had to add the sun lamp as well. I used shadows on the building on the left hand side of the photo to determine the sun's position. The way I see it, it's coming from behind on the left, at roughly 50° angle. Luckily, sun on the HDRI is at similar angle, and it was just a matter of rotating HDRI to correct position. Use „World background“ under Display properties in N-toolbar to see the environment texture in viewport.

I adjusted HDRI strength to be just enough to get some environmental light on the airplanes and not to be too strong. All of this tweaking will depend on the lighting on the photo you're compositing to, and the HDRI you're using. The sun ended up with strength of 10. The ground will probably be overblown but it looks right on airplanes so for now I left it that way.

            Before we finalize the lighting part, we'll add a ground plane to provide bounced light from the ground to light the airplanes from underneath more realistically, and later to serve as a shadow catcher. The way I like to do it is, I add a plane large enough to cover the ground around the airplanes, UV unwrap it from view and use our background photo as a texture in color slot to make bounced light have color of the ground, instead of just giving it flat dark gray color. Here, we could probably get away with it, but when you're going to have more varied ground in terms of color and details, flat color just won't cut it, especially when rendering highly reflective object like a car.

            Now add large plane, apply scale, in edit mode subdivide it at least 40 times (W -> subdivide), and unwrap it from view. The reason we're subdividing ground plane is, for some reason Blender distorts the texture if the plane isn't subdivided. I don't know why is that happening but you can see bellow how it looks with and without subdivision. If the texture still looks distorted subdivide it some more and again unwrap from view. Here, I used 60 subdivisions.

            It's still a bit distorted, but it doesn't matter that much. I also added glossy shader to make the ground some more reflective. This step is probably overkill but I still like to do it for added realism. This is my final setup:

            As I thought, the ground was to bright, so I had to lover the texture's value to more reasonable level. Now reflected light looks good on airplanes and the ground plane won't be rendered anyway so I consider this done. We're ready to render the airplanes!!

Monday, August 22, 2016

F-14D Super Tomcat

I made these renders for Chris Kuhn, the creator of this awesome piece of modeling. His attention to details is mind blowing. Check for more images and WIPs on his Facebook page.

Rendered in Blender, composited in Photoshop