Walk Mode - Changing Up & Down walk behaviour

By default, in walk-mode, Blender moves the camera/view along the model Z axis for up/down movement. But when the camera is looking straight down / straight up, this movement resembles zooming in / zooming out as the up/down movement is not relative to the view.

How can I change this behavior so that WALK_BIT_UP and WALK_BIT_DOWN moves the view up/down relative to the view?

My understanding is that it requires changes in the walkApply() function inside “source\blender\editors\space_view3d\view3d_walk.c”;

I believe the part of code that needs to be modified is:

        if ((walk->active_directions & WALK_BIT_UP)) {
          direction -= 1;

        if ((walk->active_directions & WALK_BIT_DOWN)) {
          direction = 1;

        *copy_v3_fl3(dvec_tmp, 0.0f, 0.0f, direction);* //THIS IS WHAT NEEDS TO BE CHANGED
        add_v3_v3(dvec, dvec_tmp);

However, this seems to require an understanding of model view matrices, and I’m totally lost. Any help WRT exactly what change needs to be done here to achieve the required behavior (shown in above image) will be much appreciated.

EDIT: changed Fig 2 heading in the above image;


Replace this:
copy_v3_fl3(dvec_tmp, 0.0f, 0.0f, direction);

float ztrans = rv3d->viewinv[1][2];
float shadow = ((ztrans <= 1) && (ztrans >= -1)) ? sqrtf(1.0f - (ztrans * ztrans)) : 1;
float azimuth = (rv3d->viewinv[2][2] > 0) ? 1.0f : -1.0f;
float xtrans = -rv3d->viewinv[0][1] * shadow * azimuth * direction;
float ytrans = rv3d->viewinv[0][0] * shadow * azimuth * direction;
copy_v3_fl3(dvec_tmp, xtrans, ytrans, ztrans * direction);

Perhaps somebody with more experience can find a more computationally efficient method to perform the same calculations. But this gets the job done.