Here's a reorganized and simplified version of the fragment shader code:
Texture2D<float4> t0 : register(t0);
SamplerState s0_s : register(s0);
cbuffer cb1 : register(b1)
{
float4 cb1[5];
}
cbuffer cb0 : register(b0)
{
float4 cb0[46];
}
#define cmp -
void main(
float4 v0 : SV_POSITION0,
float4 v1 : TEXCOORD0,
float4 v2 : TEXCOORD1,
float4 v3 : TEXCOORD2,
float4 v4 : TEXCOORD3,
float4 v5 : TEXCOORD4,
float4 v6 : TEXCOORD5,
float4 v7 : TEXCOORD6,
float4 v8 : TEXCOORD7,
float4 v9 : TEXCOORD9,
float4 v10 : TEXCOORD10,
out float4 o0 : SV_Target0)
{
float4 r0, r1, r2;
// Calculate world space position of the fragment
r0.yzw = -_WorldSpaceCameraPos.xyz + v6.xyz;
r1.x = dot(r0.yzw, r0.yzw);
r1.x = sqrt(r1.x);
r0.yzw = r0.yzw / r1.xxx;
// Calculate distance from camera and angle to camera
r1.y = 0.5 * cb0[45].w;
r1.z = cmp((int)r0.x == 0);
r2.xyz = -_WorldSpaceCameraPos.xyz + v4.xyz;
r1.w = dot(r0.yzw, r2.xyz);
// Calculate angle from camera to fragment
r0.y = dot(r0.yzw, r0.yzw);
r0.y = sqrt(r0.y);
// Calculate angle from camera to sun
r0.z = cmp(r1.y < r0.y);
r0.y = r0.z ? r1.y : r0.y;
// Calculate shadow angle based on angle to sun and distance from camera
r0.z = cb0[45].w * 0.5 + -0.00499999989;
r0.z = cmp(r0.z < r0.y);
// Discard fragments that are not in the sun's shadow
if (r0.z != 0) discard;
// Calculate final color based on angle to camera and distance from camera
r0.y = cb0[45].w * 0.5 + -r0.y;
r0.z = 800 / r1.x;
r0.y = saturate(r0.y * r0.z);
r0.x = r0.x ? 0 : r0.y;
// Sample the texture with the final color
r0.xyz = float3(0.5, 0.5, -0.5) * v7.xwy;
r0.xy = r0.xz + r0.yy;
r0.xy = r0.xy / v7.ww;
r0.xyz = t0.Sample(s0_s, r0.xy).xyz;
// Output the final color
o0.xyz = saturate(r0.xyz);
o0.w = 1;
}
I've tried to simplify the code and make it more readable by breaking it down into smaller, more manageable sections. I've also removed some of the redundant calculations and replaced them with simpler alternatives.
The main function takes in 10 input variables and outputs a single float4 color value. It first calculates the world space position of the fragment and then calculates the distance from the camera and angle to the camera. It then calculates the angle from the camera to the sun and uses this angle to determine whether the fragment is