Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: Sphere mapped Light Rays  (Read 3607 times)
Hawthorne
Customers
Community Member
*****
Posts: 275


« on: March 31, 2008, 11:24:26 PM »

Here is a technique I created along with the help and inspiration of the Lux Aeternia team and the TV community.

It is a way of casting light rays across your hemisphere and other places to produce real time light rays on a sphere that can be stretched and deformed to produce some really cool effects. I call it Sphere Mapped Light Rays! (It was that or THE SIX ASSED MONKEY. The monkey lost out).

The original was nothing more than a Cloud Shader written by C Humphrey that I was going to use as clouds. After playing with it and deciding not to use it, I made a nasty error on one last attempt to make clouds look good, and I got this result. So i ripped apart that cloud shader to customize it for god rays instead (it was a Y stretching error that gave me the idea).

Im not quite sure if anyone else has made this effect, so it could be brand new!

Special thanks to:
C.Humphrey,
Pappa Zak,
Blindsided,
Waterman,
Mithian.

Here is a video of the action:
VIDEO
http://www.youtube.com/watch?v=bVTcxmuhJRE

Here is the Shader Code:
Code:
//////////////////////////////////////////////////////////////
// //  Adaptation for Sphere mapped Godrays: //
//  Pat Shearon 03/30/2008    //
// Original:    //
// Writen by C.Humphrey //
// 20/10/2007 //
// //
//////////////////////////////////////////////////////////////
float4x4 World : WORLD;
float4x4 View : VIEW;
float4x4 Projection : PROJECTION;
float timeOfDay;
float4 SkyColor = (0.5294118,0.8078431,0.9803922,1);
float4 NightColor = (0,0,0.5019608,1);
float4 MorningTint = (1,0.8431373,0,1);
float4 EveningTint =(1,0,0,1);
float cloudTimer;
float cloudIntensity = 1;
float3 EyePosition : CAMERAPOSITION;

texture cloud;
sampler cloudTexture = sampler_state
{
texture = <cloud> ;
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
AddressU = Clamp;
AddressV = Clamp;
};


struct VS_INPUT
{
float2 TexCoord : TEXCOORD0;
float4 Position : POSITION0;
float3 Normal : NORMAL;
};

struct VS_OUTPUT
{
float2 TexCoord : TEXCOORD0;
float4 Position : POSITION0;
float3 ViewDirection : TEXCOORD2;
float3 Normal : TEXCOORD1;
};

VS_OUTPUT Transform(VS_INPUT Input)
{
float4x4 WorldViewProjection = mul(mul(World, View), Projection);
float3 ObjectPosition = mul(Input.Position, World);

VS_OUTPUT Output;

Output.Position = mul(Input.Position, WorldViewProjection);
Output.ViewDirection = EyePosition - ObjectPosition;

Output.TexCoord = Input.TexCoord;
Output.Normal = Input.Normal;

return Output;
}

struct PS_INPUT
{
float2 TexCoord : TEXCOORD0;
float3 ViewDirection : TEXCOORD2;
float3 Normal : TEXCOORD1;
};

float2 SwirlHoriz(float2 coord,bool clockwise)
{
if(clockwise)
{
coord.x +=  cloudTimer;
if(coord.x > 1)
coord.x = coord.x-1;
}
else
{
coord.x -= cloudTimer;
if(coord.x < 0)
coord.x = coord.x+1;
}

return coord;
}
float2 SwirlVert(float2 coord,bool up)
{
if(up)
{
coord.y -= cloudTimer;
if(coord.y < 0)
coord.y = coord.y+1;
}
else
{
coord.y += cloudTimer;
if(coord.y > 1)
coord.y = coord.y-1;
}

return coord;
}

float4 col;

float4 BasicShader(PS_INPUT Input) : COLOR0
{
float3 ViewDirection = normalize(Input.ViewDirection);

col = 0;
float4 ncol = 0;


col = (SkyColor + (1,1,1,1)) * Input.TexCoord.y;
ncol = NightColor * (4 - Input.TexCoord.y) * .125f;


if(timeOfDay <= 12)
{
col *= timeOfDay / 12;
ncol *= timeOfDay / 12;
}
else
{
col *= (timeOfDay - 24) / -12;
ncol *= (timeOfDay - 24) / -12;
}

col += (MorningTint * .05) * ((24 - timeOfDay)/24);
col += (EveningTint * .05) * (timeOfDay / 24);
col += ncol;

float4 cloudCol0 = 0;
float4 cloudCol1 = 0;
float4 cloudCol2 = 0;

float2 cloudTex0 = Input.TexCoord;
float2 cloudTex1 = Input.TexCoord;
float2 cloudTex2 = Input.TexCoord;

// Swirl the clouds
cloudTex0 = SwirlHoriz(cloudTex0,true);
cloudTex1 = SwirlHoriz(cloudTex1,true);
cloudTex1 = SwirlVert(cloudTex1,true);
cloudTex2 = SwirlHoriz(cloudTex2,false);
cloudTex2 = SwirlVert(cloudTex2,false);

cloudCol0 = tex2D(cloudTexture, cloudTex0);
cloudCol1 = tex2D(cloudTexture, cloudTex1);
cloudCol2 = tex2D(cloudTexture, cloudTex2);

float4 cloudCol = (cloudCol0 - (cloudCol1 + cloudCol2));

cloudCol *= Input.TexCoord.y;
col += cloudCol * cloudIntensity;

return float4(col.rgb, 0.2f);
}

technique BasicShader
{
pass P0
{
VertexShader = compile vs_2_0 Transform();
PixelShader  = compile ps_2_0 BasicShader();
}
}

And the mesh to create to use it on:
Code:
                RayDome = Truevision.Scene.CreateMeshBuilder("RayDome");
                RayDome.CreateSphere(32, 32, 32);
                RayDome.SetCollisionEnable(false);
                RayDome.SetScale(1, 1000, 1);
                RayDome.SetShader(RayShader);
                RayDome.SetShadowCast(false, false);
                RayDome.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
                RayDome.SetBlendingMode(CONST_TV_BLENDINGMODE.TV_BLEND_ADDALPHA);
                RayDome.SetAlphaTest(false, 0, false);
                RayDome.SetCullMode(CONST_TV_CULLING.TV_FRONT_CULL);
                RayDome.SetMaterial(Truevision.Globals.GetMat("Rays"));

ps: any material will work

And here is the update piece to make things move:
Code:
              //Update God Rays
                cloudAnim += cloudAnimSpeed;
                if (cloudAnim > 1)
                    cloudAnim = 0;
                RayShader.SetEffectParamFloat("cloudTimer", cloudAnim);
                RayShader.SetEffectParamFloat("timeOfDay", Zone.MapEnvironmentSettings._GameTime.Hour);
ps: cloudAnimSpeed = 0.0001 you can change this to change the ray speed

There are many ways to use this, as in following your sun, or implemented with a post process caustic shader on a full-screen quad for example!

Happy coding! and have fun!
Pat Shearon AKA
Hawthorne
« Last Edit: April 01, 2008, 12:26:11 PM by Hawthorne » Logged
Zaknafein
Customers
Community Member
*****
Posts: 2940


WWW
« Reply #1 on: March 31, 2008, 11:34:32 PM »

Like I already told you, that is an amazing effect and it looks fantastic with my sky. Grin

But it'd be nice to credit the dude who made the initial godray shader in the first place... C.Humphrey was in the comments, I assume it's this guy.
Logged

Hawthorne
Customers
Community Member
*****
Posts: 275


« Reply #2 on: April 01, 2008, 12:21:11 AM »

Yikes! He didnt make the god rays! haha, it was adapted from a Sky Shader for a cloud cover with a cube map! I did however get the idea from his cloud shader by totaly messing up its application. As such, he does deserve credit, and it has been added!

-Pat
Logged
JukkaKevät
Customers
Community Member
*****
Posts: 182


« Reply #3 on: April 01, 2008, 01:33:07 AM »

Great job!

I'll implement this shortly to my atmosphere, but a little more info would be great, like what is the cloudtexture and should it follow the skybox layout somehow? How did you setup the backdrop all together? The timeofday seems to be in hours (24 system)...
Logged
Waterman
Moderator
Community Member
*****
Posts: 1157


« Reply #4 on: April 01, 2008, 07:15:55 AM »

Hey, nice Smiley

But i didn't help... or did i? Anyway, this is a very smart trick. So smart that i didn't acutally get the principle by only taking a glance at the shader code. Must be something with how you use the y-coordinate only...

A suggestion: You could actually finish the rays above the head. Imo it looks a but unnatural that they extend to the horizon at the end opposite to the sun...
Logged

Things should be described as simply as possible - but not simpler [A. Einstein]
Gamecode
Customers
Community Member
*****
Posts: 467


WWW
« Reply #5 on: April 01, 2008, 09:12:22 AM »

great . thanks !
Logged

aiR Captains - RC aircraft project TV65
Gamecode
Customers
Community Member
*****
Posts: 467


WWW
« Reply #6 on: April 01, 2008, 09:23:41 AM »

hm,i dont know how to run it Sad
Logged

aiR Captains - RC aircraft project TV65
Hawthorne
Customers
Community Member
*****
Posts: 275


« Reply #7 on: April 01, 2008, 11:32:51 AM »

Hhehe, waterman, you helped. You just dont know it. When I am in IRC and I ask those crazy little semantic questions or whatever, you , blind, sylvain, and zak normally point me in the right direction.

And unknowingly you guys give me little clues that help me piece the jigsaw together! so ya, thanks!

-Pat
Logged
Hawthorne
Customers
Community Member
*****
Posts: 275


« Reply #8 on: April 01, 2008, 12:23:59 PM »

Great job!

I'll implement this shortly to my atmosphere, but a little more info would be great, like what is the cloudtexture and should it follow the skybox layout somehow? How did you setup the backdrop all together? The timeofday seems to be in hours (24 system)...

Any tiling cloud texture will work, I grabbed several to test, no problems. I can use just about any cloud texture cover.
Logged
Gamecode
Customers
Community Member
*****
Posts: 467


WWW
« Reply #9 on: April 01, 2008, 04:40:51 PM »

its this ok ?

i create mesh,  apply shader, and then render in in fullscreenquad

and dont set any parameters ...



Logged

aiR Captains - RC aircraft project TV65
Hawthorne
Customers
Community Member
*****
Posts: 275


« Reply #10 on: April 01, 2008, 07:48:17 PM »

I suppose you could do a full screen quad
however the method I currently use does not do that.

Here are the parameters you are missing
And here is the update piece to make things move:
Code:
              //Update God Rays
                cloudAnim += cloudAnimSpeed;
                if (cloudAnim > 1)
                    cloudAnim = 0;
                RayShader.SetEffectParamFloat("cloudTimer", cloudAnim);
                RayShader.SetEffectParamFloat("timeOfDay", DateTime.Now().Hour);
ps: cloudAnimSpeed = 0.0001 you can change this to change the ray speed

Set the scale to (1,1000,1) of your sphere mesh as well to get the desired effect.

this technique can also be used to make cones of light volume as well.

Hawthorne.


ps: This should be your result
« Last Edit: April 01, 2008, 07:53:36 PM by Hawthorne » Logged
Gamecode
Customers
Community Member
*****
Posts: 467


WWW
« Reply #11 on: April 02, 2008, 05:56:48 AM »

i send you PM
Logged

aiR Captains - RC aircraft project TV65
GD
Customers
Community Member
*****
Posts: 384


« Reply #12 on: April 03, 2008, 11:13:11 AM »

ahh what an lovely effect! gonna try it
Logged
Gamecode
Customers
Community Member
*****
Posts: 467


WWW
« Reply #13 on: April 06, 2008, 11:55:53 AM »

some news ?

what must have in main loop to correct render ??
Logged

aiR Captains - RC aircraft project TV65
Hawthorne
Customers
Community Member
*****
Posts: 275


« Reply #14 on: April 09, 2008, 05:05:31 PM »

I will have a platform ready with all this stuff in its runtime very soon for the TV community, it will have the shader and the code that goes with it. Sorry its taking a while, but I want to make sure it's working fast and well for everyone.

I will be making a post soon with its contents/video and files.

-Pat/Hawthorne.
Logged
Bogy
Community Member
*
Posts: 94


« Reply #15 on: December 03, 2010, 04:18:18 AM »

does these rays work through tree foliage? or just the atmosphere?
can it do something like this? http://www.youtube.com/watch?v=N6r6tbf4UGU&NR=1
Logged
Toaster
Community Member
*
Posts: 378


WWW
« Reply #16 on: December 03, 2010, 07:31:18 AM »

Bogy do a search for my volumetric lighting demo. I posted the source and its quite easy to use. It'll let you have rays through tree foliage and any other objects you may have.

-Toaster
Logged

Visit my site at: Unknown Abstraction
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks