See T48453 for details and test scenes
The main drawback that I can see is two switches for nm->space that are confusing. And I think that convertion can be simplified.
These glsl functions came from Сycles logic? Or it's just heuristics?
thanks for the review!
I tried to address your comments by rearranging the logic into a more readable form. Now there is a clear separation by shading type and I also got rid of the second switch by nm->space.
This function exists because in Cycles, the y component of the normal is not flipped like in BI.
Rearranged and fixed, thanks!