I want to implement an extrude functionality like the one in Blender, but with vertices I draw in a draw handler.
I have the 2d position of the mouse at which the extrusion of my vertices starts and the current mouse position. I also know the direction as 3d normalized vector in which to extrude.
I tried to calculate the 3d locations for mouse positions to get the extrusion length like this
dir = self.get_dir()
mouse_3d = region_2d_to_location_3d(self._view_context.region, self._view_context, mouse_pos_2d, dir)
ext_3d = region_2d_to_location_3d(self._view_context.region, self._view_context, self._extrude_pos, dir)
diff_vec = (ext_3d - mouse_3d)
self._extrusion = diff_vec.length
self.extrude_vertices(context)
# Extrusion of vertices
def extrude_vertices(self, context):
dir = self.get_dir() * self._extrusion
for index, vertex3d in enumerate(self._vertices):
self._vertices_extruded[index] = vertex3d + dir
Problems:
- I cant extrude negative direction, it's always positive
- I want to include the direction of the extrusion into the extrusion amount (like the extrude in Blender: When the mouse is moved along the direction it extrudes more, if not (e.g. perpendicular to extrude direction) we get less extrusion.
So what is needed is to find the amount of extrusion for the direction vector d.
Any help is appreciated.
Update:
I added this, I guess it must have something to do with the dot product:
diff_vec = (ext_3d - mouse_3d)
dot_prod = diff_vec.dot(dir.orthogonal())
self._extrusion = dot_prod
But the amount is not correct.