Skip to content

Commit

Permalink
Fix for 'Orthographic camera in final render does not match 3D view' …
Browse files Browse the repository at this point in the history
…issue #181
  • Loading branch information
neo2068 committed Jun 24, 2018
1 parent 464604f commit ac6ed67
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 8 additions & 4 deletions export/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ def convert(exporter, scene, context=None, is_camera_moving=False):
def _view_ortho(scene, context, definitions):
cam_matrix = Matrix(context.region_data.view_matrix).inverted()
lookat_orig, lookat_target, up_vector = _calc_lookat(cam_matrix, scene)
world_scale = utils.get_worldscale(scene, False)

definitions["type"] = "orthographic"
zoom = 0.915 * context.region_data.view_distance*35/context.space_data.lens
zoom = 0.915 * world_scale * context.region_data.view_distance*35/context.space_data.lens

# Move the camera origin away from the viewport center to avoid clipping
origin = Vector(lookat_orig)
Expand Down Expand Up @@ -77,16 +78,18 @@ def _view_persp(scene, context, definitions):
def _view_camera(scene, context, definitions):
camera = scene.camera
lookat_orig, lookat_target, up_vector = _calc_lookat(camera.matrix_world, scene)
world_scale = utils.get_worldscale(scene, False)

definitions["lookat.orig"] = lookat_orig
definitions["lookat.target"] = lookat_target
definitions["up"] = up_vector

# Magic zoom formula for camera viewport zoom from Cycles export code
zoom = 4 / ((math.sqrt(2) + context.region_data.view_camera_zoom / 50) ** 2)
zoom = 4 / ((math.sqrt(2) + (context.region_data.view_camera_zoom * world_scale) / 50) ** 2)

if camera.data.type == "ORTHO":
definitions["type"] = "orthographic"
zoom *= 0.5 * camera.data.ortho_scale
zoom *= 0.5 * world_scale * camera.data.ortho_scale
elif camera.data.type == "PANO":
definitions["type"] = "environment"
elif camera.data.type == "PERSP":
Expand All @@ -103,14 +106,15 @@ def _view_camera(scene, context, definitions):
def _final(scene, definitions):
camera = scene.camera
lookat_orig, lookat_target, up_vector = _calc_lookat(camera.matrix_world, scene)
world_scale = utils.get_worldscale(scene, False)
definitions["lookat.orig"] = lookat_orig
definitions["lookat.target"] = lookat_target
definitions["up"] = up_vector
zoom = 1

if camera.data.type == "ORTHO":
cam_type = "orthographic"
zoom = 0.5 * camera.data.ortho_scale
zoom = 0.5 * world_scale * camera.data.ortho_scale

elif camera.data.type == "PANO":
cam_type = "environment"
Expand Down
8 changes: 5 additions & 3 deletions utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def calc_filmsize_raw(scene, context=None):
def calc_filmsize(scene, context=None):
border_min_x, border_max_x, border_min_y, border_max_y = calc_blender_border(scene, context)
width_raw, height_raw = calc_filmsize_raw(scene, context)
world_scale = get_worldscale(scene, False)

if context:
# Viewport render
Expand All @@ -187,7 +188,7 @@ def calc_filmsize(scene, context=None):
height = int(height_raw * border_max_y) - int(height_raw * border_min_y)
else:
# Camera viewport
zoom = 0.25 * ((math.sqrt(2) + context.region_data.view_camera_zoom / 50) ** 2)
zoom = 0.25 * ((math.sqrt(2) + context.region_data.view_camera_zoom * world_scale / 50) ** 2)
aspectratio, aspect_x, aspect_y = calc_aspect(scene.render.resolution_x * scene.render.pixel_aspect_x,
scene.render.resolution_y * scene.render.pixel_aspect_y,
scene.camera.data.sensor_fit)
Expand Down Expand Up @@ -252,11 +253,12 @@ def calc_screenwindow(zoom, shift_x, shift_y, scene, context=None):

width_raw, height_raw = calc_filmsize_raw(scene, context)
border_min_x, border_max_x, border_min_y, border_max_y = calc_blender_border(scene, context)
world_scale = get_worldscale(scene, False)

# Following: Black Magic
scale = 1
if scene.camera and scene.camera.data.type == "ORTHO":
scale = 0.5 * scene.camera.data.ortho_scale
scale = 0.5 * scene.camera.data.ortho_scale * world_scale

offset_x = 0
offset_y = 0
Expand All @@ -275,7 +277,7 @@ def calc_screenwindow(zoom, shift_x, shift_y, scene, context=None):
scene.camera.data.sensor_fit)

if scene.camera and scene.camera.data.type == "ORTHO":
zoom = 0.5 * scene.camera.data.ortho_scale
zoom = 0.5 * scene.camera.data.ortho_scale * world_scale
else:
# No border
aspectratio, xaspect, yaspect = calc_aspect(width_raw, height_raw, scene.camera.data.sensor_fit)
Expand Down

0 comments on commit ac6ed67

Please sign in to comment.