# Sun with HLSL

This topic is 3169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hiya, thanks for your time. I've got it in my head that I want to render a sun in my skydome using only a single point to represent the position of the sun and then handling the appearance of the sun completely in the shader. Of course it doesn't work. Damn...thought I was finally getting my head around some of this 3d math. For now I'm simply declaring a static 3d point in my skydome mod.... Vector3 Sun=new Vector3(0,500,0); Keep in mind this is a typical skydome, ala Reimer, that moves with the camera along X and Z BUT NOT ALONG THE Y AXIS. So, (I think) that x=0 and z=0 means that 0 of the skydomes coordinate system (hope this makes sense). If I move the skydome with the cam along the Y axis the sun renders perfectly, but this is not acceptable. The idea is to do a distance calculation on the sun vector vs the pixel being rendered and color the pixel yellow (for now) if it is within a given distance of the sun vector. Anyway I send my sun vector to my effect, and the Vertex Shader and Pixel Shader are listed below. Problem is that if my camera is at ground level the sun renders correctly, but if I move the camera towards the sun it rapidly gets smaller (the opposite of what I would expect) and then disappears. I'm confused because the sun vector never moves...WHY does the camera position effect its size? It seems to me that the sun vector should be calculated just like every other pixel position in the skydome. SHADERS: SkydomeVertexToPixel SkydomeVS( float4 inPos : POSITION, float3 inNormal: NORMAL, float2 inTexCoords: TEXCOORD0) { SkydomeVertexToPixel Output = (SkydomeVertexToPixel)0; float4x4 preViewProjection = mul (camView, camProjection); float4x4 preWorldViewProjection = mul (World, preViewProjection); Output.Position = mul(inPos, preWorldViewProjection); Output.Sun = mul(Sun, preViewProjection); Output.TextureCoords = inTexCoords; Output.Position3d = Output.Position; float3 Normal = normalize(mul(normalize(inNormal), World)); return Output; } PixelToFrame SkydomePS(SkydomeVertexToPixel PSIn) { PixelToFrame Output = (PixelToFrame)0; int size=75; float edgecolor=0.8; float dist=sqrt( ((PSIn.Position3d.x-PSIn.Sun.x) * (PSIn.Position3d.x-PSIn.Sun.x)) + ((PSIn.Position3d.y-PSIn.Sun.y) * (PSIn.Position3d.y-PSIn.Sun.y)) + ((PSIn.Position3d.z-PSIn.Sun.z) * (PSIn.Position3d.z-PSIn.Sun.z)) ); if (dist < size) { Output.Color.r=1; Output.Color.g=1; Output.Color.a=1; Output.Color.b=0; } return Output; }

##### Share on other sites
I did something similar to what you're doing. A few blog posts are here.

I used a slightly different method to fade the sun color down from the actual sun point to the surrounding pixels though. You create two rays, one from the sun point to the camera position, and another from the pixel you're drawing to the camera position. Visualize that, and look at the angle between those rays. Basically, the smaller that angle (the closer the pixel is to the center of the sun) the brighter the pixel should be. There's a few other factors in the actual equation I used that can make the sun bigger or smaller or have a larger haze, etc.

It also looks like only the first 6 lines of the scrap of code I posted deal with the sun color. The last bit is just doing a linear interpolation between the horizon color and the high noon sky color, my first attempt at simulating atmospheric scattering.

I never got to the part that alters the parameters as a function of sun height in the sky though to achieve nice huge red sunsets, or clear smaller suns when its high in the sky.

Hopefully some of that gets you on the right path. I might be able to dig up some more code I wrote if you're curious about the rest of it.

##### Share on other sites
Cool Matt, thanks!

I actually did solve the immediate problem, but it sounds like your blog will address some other walls that I'm planning on banging my against very soon.

For anybody who's interested:

You have to incorporate the camera view into the sun position:

per frame:
Sun.X=0; Sun.Z=0; Sun.Y=((sizeofskydome / 2)-camera.Position.Y) - horizonoffset;

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633714
• Total Posts
3013490
×