
float4x4 m44world; //Transforms object space to world space.float4x4 m44view; //Transforms world space to view space.float4x4 m44proj; //Transforms view space to screen space.texture CloudTexture < string filename="sky\\cloudscloudy.dds"; >;float3 greyscale = float3(0.3,0.59,0.11);static const float nearZ = m44proj._43 / m44proj._33;static const float farZ = (m44proj._33 * nearZ) / (m44proj._33 - 1.0f);static const float CoordOffset = sin(f4Time.x/3600)*5;static const float4x4 m44worldviewproj = m44world * (m44view * m44proj);static const float4x4 m44invworldviewproj = pow(m44worldviewproj,-1); //Depth calculations. float depth = tex2D(DepthSampler, IN.UVCoord).x; depth = pow(abs(depth), 0.05f); depth = (2.0f * nearZ) / (nearZ + farZ - depth * (farZ - nearZ)); //Converting a point in screen space to world space. // Coord is the viewport position at this pixel in the range -1 to 1. float4 Coord = float4(IN.UVCoord.x * 2 - 1, (1 - IN.UVCoord.y) * 2 - 1,depth,1); Coord = mul(Coord,m44invworldviewproj); Coord = Coord/Coord.w; float4 tex = tex2Dproj(TextureSampler, Coord); tex.rgb = dot(greyscale,tex.rgb) / 3; if (tex.r < LumCutoff || depth == 1) tex.rgb = 0; return tex;
static const float3 sd = -f4SunDir.xyz;static const float d=dot(f3EyeForward,sd);static const float3 sunview_v=mul(sd/d,m44view);
static const float2 invtexproj = 2 * float2( 1 , - rcpres.x / rcpres.y ) * tan(radians(fov * 0.5));
float3 eyevec = float3(mview[0][2],mview[1][2],mview[2][2]);eyevec += invtexproj.x * (Tex.x-0.5) * float3(mview[0][0],mview[1][0],mview[2][0]);eyevec += invtexproj.y * (Tex.y-0.5) * float3(mview[0][1],mview[1][1],mview[2][1]);float3 xpos = eyepos + depth * eyevec;float sunraypath = (xpos.z/sunvec.z);float gdist = length(xpos.xyz - eyepos.xyz);float shadows = tex2D(s3, (xpos - sunvec * sunraypath ).xy / 100000).b * gdist * saturate(exp(gdist*400)) * cauststr / 50;
float4 source = tex2D(s1, tex);float4 blend = tex2D(s2, tex);blend = 1- blend;float4 final = source * blend;final = (1-blend) * source + final * 0.3;return final;
//This works, but is relative to player (view space), but the origin is not at screen centre.float4 RotatingCloudShadow(VSOUT IN) : COLOR0{ //Depth calculations. float depth = tex2D(DepthSampler, IN.UVCoord).x; depth = pow(abs(depth), 0.05f); depth = (2.0f * nearZ) / (nearZ + farZ - depth * (farZ - nearZ)); // Coord is the viewport position at this pixel in the range -1 to 1. float3 Coord = float3(IN.UVCoord.x * 2 - 1, (1 - IN.UVCoord.y) * 2 - 1,depth); Coord = Coord*Coord.z; float4 tex = tex2D(TextureSampler, Coord.xy + CoordOffset); tex.rgb = dot(greyscale,tex.rgb) / 3; if (tex.r < LumCutoff || depth == 1) tex.rgb = 0; return tex;}//In view space.float4 TestShadow(VSOUT IN) : COLOR0{ //Depth calculations. float depth = tex2D(DepthSampler, IN.UVCoord).x; depth = pow(abs(depth), 0.05f); depth = (2.0f * nearZ) / (nearZ + farZ - depth * (farZ - nearZ)); //SSCoord is the position vector of the pixel in screen space. float3 SSCoord = float3(IN.UVCoord.x * 2 - 1, (1 - IN.UVCoord.y) * 2 - 1,0); //DepthCoord is the position vector of the pixel's depth from the pixel. float3 DepthCoord = float3(0,0,depth); //r is the position vector of the point in view space. Using trig: float3 r = sqrt( pow(SSCoord,2) + pow(DepthCoord,2) ); r = r*r.z; float4 tex = tex2D(TextureSampler, r.xy + CoordOffset); tex.rgb = dot(greyscale,tex.rgb) / 3; if (tex.r < LumCutoff || depth == 1) tex.rgb = 0; return tex;}
//Cloud Shadow Shader by WrinklyNinja - trying to learn how to do stuff.//TWEAKABLE VARIABLES.float ShadowIntensity = 0.25;//Controls how dark the cloud shadows are.float LumCutoff = 0.27;//Controls how bright a cloud has to be to cast a shadow.float CloudSpeed = 7200;//Controls the speed of cloud movement. Higher is slower.float FOV = 75;//Your FOV, obviously.//END OF TWEAKABLE VARIABLES.float4x4 m44world; //Transforms object space to world space.float4x4 m44view; //Transforms world space to view space.float4x4 m44proj; //Transforms view space to screen space.float3 f3EyeForward; //Forward unit vector of camera.float2 rcpres;float4 f4Time;texture Depth;texture thisframe;texture lastpass;float t = tan(radians(FOV * 0.5));static const float aspect = rcpres.x/rcpres.y;static const float CoordOffset = sin(f4Time.x/3600)*3;texture CloudTexture < string filename="sky\\cloudscloudy.dds"; >;float3 greyscale = float3(0.3,0.59,0.11);static const float nearZ = m44proj._43 / m44proj._33;static const float farZ = (m44proj._33 * nearZ) / (m44proj._33 - 1.0f);sampler FrameSampler = sampler_state{ texture =; AddressU = CLAMP; AddressV = CLAMP; MINFILTER = POINT; MAGFILTER = POINT;};sampler PassSampler = sampler_state{ texture = ; AddressU = CLAMP; AddressV = CLAMP; MINFILTER = POINT; MAGFILTER = POINT;};sampler DepthSampler = sampler_state{ texture = ; AddressU = CLAMP; AddressV = CLAMP; MINFILTER = POINT; MAGFILTER = POINT;};sampler TextureSampler = sampler_state{ texture = ; AddressU = WRAP; AddressV = WRAP; MINFILTER = POINT; MAGFILTER = POINT;};struct VSOUT{ float4 vertPos : POSITION; float4 UVCoord : TEXCOORD0;};float4 ProjectShadow(VSOUT IN) : COLOR0{ //Depth calculations. float depth = tex2D(DepthSampler, IN.UVCoord).x; depth = pow(abs(depth), 0.05f); depth = (2.0f * nearZ) / (nearZ + farZ - depth * (farZ - nearZ)); float4x4 m44invworld = pow(m44world,-1); float4x4 m44invview = pow(m44view,-1); float4x4 m44invproj = pow(m44proj,-1); //Normalise screen space coordinates, and add depth. float3 NormCoord = float3(IN.UVCoord.x * 2 - 1,1 - IN.UVCoord.y * 2,1/t); //Transform to view space. NormCoord.x *= aspect; //Transform to world space. NormCoord = mul(m44invview,NormCoord); //Transform to model space. NormCoord = mul(m44invworld,NormCoord); //Transform camera pos. Starts off in view space. float3 EyeForward = f3EyeForward; //Transform to world space. EyeForward = mul(m44invview,EyeForward); //Into model space. EyeForward = mul(m44invworld,f3EyeForward); NormCoord = EyeForward + depth*NormCoord; NormCoord *= NormCoord.z; float4 tex = tex2D(TextureSampler, NormCoord.xy - f4Time.x/CloudSpeed); tex.rgb = dot(greyscale,tex.rgb) / 3; if (tex.r < LumCutoff || depth == 1) tex.rgb = 0; return tex;}float4 Combine(VSOUT IN) : COLOR0{ float4 frame = tex2D(FrameSampler, IN.UVCoord); float4 shadowpass = tex2D(PassSampler, IN.UVCoord); frame.rgb -= shadowpass.rgb * ShadowIntensity; return frame;}technique t0 { pass p0 { PixelShader = compile ps_2_0 ProjectShadow(); } pass p1 { PixelShader = compile ps_2_0 Combine(); }}