def region_2d_to_vector_3d(region, rv3d, coord):
    """
    Return a direction vector from the viewport at the specific 2d region
    coordinate.

    :arg region: region of the 3D viewport, typically bpy.context.region.
    :type region: :class:`bpy.types.Region`
    :arg rv3d: 3D region data, typically bpy.context.space_data.region_3d.
    :type rv3d: :class:`bpy.types.RegionView3D`
    :arg coord: 2d coordinates relative to the region:
       (event.mouse_region_x, event.mouse_region_y) for example.
    :type coord: 2d vector
    :return: normalized 3d vector.
    :rtype: :class:`mathutils.Vector`
    """
    from mathutils import Vector
    viewinv = rv3d.view_matrix.inverted()
    if rv3d.is_perspective:
        persinv = rv3d.perspective_matrix.inverted()
        out = Vector((2.0 * coord[0] / region.width - 1.0, 2.0 * coord[1] /
            region.height - 1.0, -0.5))
        w = out.dot(persinv[3].xyz) + persinv[3][3]
        view_vector = persinv * out / w - viewinv.translation
    else:
        view_vector = -viewinv.col[2].xyz
    view_vector.normalize()
    return view_vector
