🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Adjusting normal coordinates when drawing circle

Started by
5 comments, last by Thaumaturge 2 years, 2 months ago

Wasn't sure what title to put here but I have a bit of a poser that I've not been able to figure out yet.

I'm doing some non-realistic atmospheric scattering using a post processor in my [DirectX11] engine and part of my process is to compute a “halo” around the sun. I am dealing in normalised screen coordinates (X and Y between -1 and 1) for both the pixel position and the sun position. I pass the sun position into the shader by computing it using the view projection matrix in the program. That part is fine, the sun coordinates match up fine.

What I'm having a problem with is creating my halo. I'm using the distance keyword to compute the distance between the pixel position and the sun position and then I do some powering to blend it out into a nice halo. This looks great but only when the screen ratio is 1, i.e. the width of the render window is the same as the height. When I render in a screen which is wider than it is high, the halo stretches to match the ratio. I thought all I'd need to do is multiply the pixel position by the screen ratio and that does give me a circular halo whatever the screen ratio is, but it displays in the wrong position. This is obviously because I'm changing the position of the pixel regardless of how close it is to the centre of the sun position.

Been trying to figure this out for a while but haven't got very far yet, I'm finding it a bit of a brain melter to be honest. Anyone done anything similar? Thing is, I also want to draw a ‘sprite’ using this shader for some fake sun flares so I'll need to work out ratio-altered pixel coordinates.

Advertisement

I think that it might work better to instead scale the reference distance for the halo: the distance should be smaller the wider the window, and the closer to horizontal is the vector between sun position and pixel-position, I believe.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Thanks - I did try this but I ended up with slight artifacts. I get the vector (x and y only) from the sun position to the pixel position. I then dot that with a vector that's 0, 1 (up vector). I find the absolute value of that which gives me a value of 1 if the direction to the sun is straight up but also 1 if it's straight down. I then invert it so basically, the closer the vector between sun and pixel position is to horizontal, the higher the value (between 0 and 1). I then multiply the pixel distance by a lerp between 1 and the screen ratio using the modified dot as the interpolator. I end up with artifacts like this:

Slight artifact on the horizontal (when screen ratio is wider than height)

Slight artifacts on both horizontal and vertical (when screen is much higher than width)

Hmm… My first instinct would be, I think, to check that all of the intervening steps are producing the expected output. (By simply altering the shader's output colour to be nothing but a given value in a single channel--something like: color = vec3(0, dotProductResult, 0); )

Another thought might be to try just using the x-component of the normalised vector, instead of the dot-product--perhaps that might produce better results.

Indeed, examining the first screenshot closely, it seems to me that even though the horizontal has what seems to be an "indent", the actual colour-value for a point samples there is slightly brighter than that at a corresponding point on the vertical. Thus I suspect that the technique is working, but that the interpolation wants for modification.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

I've just figured it out a different way. Multiplying both the x coordinate of the pixel position and the x coordinate of the sun position by the ratio lines everything up and gives a nice circular halo regardless of the screen ratio!

Thanks for your input.

Ah, I'm glad that you found a method that works! And indeed, a nicely-simple one, too! ^_^

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

This topic is closed to new replies.

Advertisement