It took me a while to grasp how gradients in Cg/HLSL work. For some reason, all the tutorials I watched/read do not teach you the basics, or at least, they do not tell you why they suddenly do this and that, and magic gradients happen.
The following is not a tutorial since I’m just a beginner. I’d like to share what I learnt in a way how I am learning. There is a logical structure, and once I got it, it all makes sense. To be honest, I’m not that interested in the visual output but in the algorithms. I don’t know if everything I’m writing here is correct but when I spot an error, I’ll edit this post.