refactor: initial step rewrite user frustum drawing code to debug xr camera position

This commit is contained in:
Swann Martinez
2022-03-27 17:33:13 +02:00
parent 629f2e1cdb
commit 1b614f4fb6
4 changed files with 85 additions and 68 deletions

View File

@ -26,7 +26,7 @@ import bgl
import blf
import bpy
import gpu
import mathutils
from mathutils import Vector, Matrix
from bpy_extras import view3d_utils
from gpu_extras.batch import batch_for_shader
from replication.constants import (STATE_ACTIVE, STATE_AUTH, STATE_CONFIG,
@ -136,7 +136,7 @@ def bbox_from_obj(obj: bpy.types.Object, index: int = 1) -> list:
(-radius, +radius, +radius), (+radius, +radius, +radius)]
base = obj.matrix_world
bbox_corners = [base @ mathutils.Vector(corner) for corner in coords]
bbox_corners = [base @ Vector(corner) for corner in coords]
vertex_pos = [(point.x, point.y, point.z) for point in bbox_corners]
@ -159,39 +159,12 @@ def bbox_from_instance_collection(ic: bpy.types.Object, index: int = 0) -> list:
vertex_pos += vertex_pos_temp
vertex_indices += vertex_indices_temp
bbox_corners = [ic.matrix_world @ mathutils.Vector(vertex) for vertex in vertex_pos]
bbox_corners = [ic.matrix_world @ Vector(vertex) for vertex in vertex_pos]
vertex_pos = [(point.x, point.y, point.z) for point in bbox_corners]
return vertex_pos, vertex_indices
def generate_user_camera() -> list:
""" Generate a basic camera represention of the user point of view
:return: list of 7 points
"""
area, region, rv3d = view3d_find()
v1 = v2 = v3 = v4 = v5 = v6 = v7 = [0, 0, 0]
if area and region and rv3d:
width = region.width
height = region.height
v1 = project_to_viewport(region, rv3d, (0, 0))
v3 = project_to_viewport(region, rv3d, (0, height))
v2 = project_to_viewport(region, rv3d, (width, height))
v4 = project_to_viewport(region, rv3d, (width, 0))
v5 = project_to_viewport(region, rv3d, (width/2, height/2))
v6 = list(rv3d.view_location)
v7 = project_to_viewport(
region, rv3d, (width/2, height/2), distance=-.8)
coords = [v1, v2, v3, v4, v5, v6, v7]
return coords
def project_to_screen(coords: list) -> list:
""" Project 3D coordinate to 2D screen coordinates
@ -219,10 +192,10 @@ def get_bb_coords_from_obj(object: bpy.types.Object, instance: bpy.types.Object
base = object.matrix_world
if instance:
scale = mathutils.Matrix.Diagonal(object.matrix_world.to_scale())
scale = Matrix.Diagonal(object.matrix_world.to_scale())
base = instance.matrix_world @ scale.to_4x4()
bbox_corners = [base @ mathutils.Vector(
bbox_corners = [base @ Vector(
corner) for corner in object.bound_box]
@ -267,9 +240,14 @@ class Widget(object):
class UserFrustumWidget(Widget):
# Camera widget indices
indices = ((1, 3), (2, 1), (3, 0),
(2, 0), (4, 5), (1, 6),
(2, 6), (3, 6), (0, 6))
camera_vertex = ((0, 0, 1),
(-1, -0.5, -1), (1, -0.5, -1), (1, 0.5, -1), (-1, 0.5, -1),
(0, 1, -1),
(-0.5, 0.6, -1), (0.5, 0.6, -1))
camera_indices = ((0, 1), (0, 2), (0, 3), (0, 4),
(1, 2), (2, 3), (3, 4), (4, 1),
(5, 6), (6, 7), (7, 5))
def __init__(
self,
@ -290,27 +268,25 @@ class UserFrustumWidget(Widget):
return False
scene_current = self.data.get('scene_current')
view_corners = self.data.get('view_corners')
view_matrix = self.data.get('view_matrix')
return (scene_current == bpy.context.scene.name or
self.settings.presence_show_far_user) and \
view_corners and \
view_matrix and \
self.settings.presence_show_user and \
self.settings.enable_presence
def draw(self):
location = self.data.get('view_corners')
shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
positions = [tuple(coord) for coord in location]
if len(positions) != 7:
return
view_matrix = Matrix(self.data.get('view_matrix')).inverted()
coords = [view_matrix @ Vector(vertex) for vertex in self.camera_vertex]
batch = batch_for_shader(
shader,
'LINES',
{"pos": positions},
indices=self.indices)
{"pos": coords},
indices=self.camera_indices)
shader.bind()
shader.uniform_float("color", self.data.get('color'))
@ -405,19 +381,18 @@ class UserNameWidget(Widget):
return False
scene_current = self.data.get('scene_current')
view_corners = self.data.get('view_corners')
view_matrix = self.data.get('view_matrix')
return (scene_current == bpy.context.scene.name or
self.settings.presence_show_far_user) and \
view_corners and \
view_matrix and \
self.settings.presence_show_user and \
self.settings.enable_presence
def draw(self):
view_corners = self.data.get('view_corners')
position = Matrix(self.data.get('view_matrix')).inverted().to_translation()
color = self.data.get('color')
position = [tuple(coord) for coord in view_corners]
coords = project_to_screen(position[1])
coords = project_to_screen(position)
if coords:
blf.position(0, coords[0], coords[1]+10, 0)