Skip to content

Commit

Permalink
blender: Extremely hacky fix for shaded viewport zoom
Browse files Browse the repository at this point in the history
PERSP now works perfectly, assuming your viewport is either a square, or
wider than it is tall.
CAMERA perspective is still very wrong, and I have absolutely no clue
how to fix it. I trawled through Blender's code, found a weird 'magic
zoom' formula, asked the Blender devs about that, looked at some more
code, and I still have no clue how one is supposed to compute valid
camera parameters from everything correctly.
It now sort of, kind of, works. And the perspective type works
perfectly, as long as you size your viewport correctly.
Good enough for now!
  • Loading branch information
vkoskiv committed Jan 10, 2024
1 parent 0d08d1c commit 1025c3b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion bindings/blender_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ class CrayRender(bpy.types.RenderEngine):
cr_renderer = None
old_mtx = None
old_dims = None
old_zoom = None

def __init__(self):
print("c-ray initialized")
Expand Down Expand Up @@ -363,10 +364,14 @@ def render(self, depsgraph):
self.display_bitmap(bm)

def view_draw(self, context, depsgraph):
zoom = context.region_data.view_camera_zoom
mtx = context.region_data.view_matrix.inverted()
if not self.old_mtx or mtx != self.old_mtx:
self.old_mtx = mtx
self.tag_update()
if not self.old_zoom or zoom != self.old_zoom:
self.old_zoom = zoom
self.tag_update()
self.old_mtx = mtx
new_dims = (context.region.width, context.region.height)
if not self.old_dims or self.old_dims != new_dims:
cr_cam = self.cr_scene.cameras['Camera']
Expand Down Expand Up @@ -398,6 +403,15 @@ def view_update(self, context, depsgraph):
mtx = context.region_data.view_matrix.inverted()
euler = mtx.to_euler('XYZ')
loc = mtx.to_translation()
new_fov = math.degrees(context.space_data.camera.data.angle)
# I haven't the faintest clue why an offset of 32 degrees makes this match perfectly.
# If you know, please do let me know.
if context.region_data.view_perspective == 'PERSP':
new_fov += 32
else:
if self.old_zoom:
new_fov -= (self.old_zoom - 32)
cr_cam.opts.fov = new_fov
cr_cam.opts.pose_x = loc[0]
cr_cam.opts.pose_y = loc[1]
cr_cam.opts.pose_z = loc[2]
Expand Down

0 comments on commit 1025c3b

Please sign in to comment.