def matrix(self): """Get the camera matrix to convert object from world coordinate to camera coordinate """ if self.__matrix is not None: return self.__matrix # calculate matrices translation = [ [1, 0, 0, -self.position.x], [0, 1, 0, -self.position.y], [0, 0, 1, -self.position.z], [0, 0, 0, 1] ] trans_mat = Matrix.new(translation) camera_zaxis = self.facing_dir.unit_vector camera_yaxis = self.up_dir.unit_vector camera_xaxis = camera_yaxis.cross(camera_zaxis).unit_vector alignment = [ [*camera_xaxis.to_list(), 0], [*camera_yaxis.to_list(), 0], [*camera_zaxis.to_list(), 0], [0, 0, 0, 1] ] alignment_mat = Matrix.new(alignment) # now, we have our world coordinate aligned with camera coordinate # calculate projection matrix proj_mat = self.projection.matrix self.__matrix = proj_mat * alignment_mat * trans_mat return self.__matrix
def transform_point(self, point3d): pointarr = [*point3d.to_list(), 1.] pointmatrix = Matrix.new([[x] for x in pointarr]) result = self.camera.matrix * pointmatrix z = result.array[-1][0] x = result.array[0][0] y = result.array[1][0] try: return [x/z, y/z] except Exception as e: # DivisionByZero in fact return None
def matrix(self): if self.__matrix is not None: return self.__matrix d = self.plane_distance projection = [ [d, 0, 0, 0], [0, d, 0, 0], [0, 0, d, 0], # we want w coordinate of projected point to be the z coordinate [0, 0, 1, 0], ] self.__matrix = Matrix.new(projection) return self.__matrix