CatDogEngine
CatDogEngine copied to clipboard
[shaderc] variable in fragment shader seems optimized but actually used in bgfx shader language
trafficstars
fs_SingleScattering_RayMarching.sc
void main()
{
vec3 rayStart = u_cameraPos;
vec3 rayDir = normalize(v_worldPos.xyz);
// lightDir was optimized here
vec3 lightDir = normalize(-u_LightDir);
// We assume that camera always in atmosphere.
vec2 atmIntersection = RaySphereIntersection(rayStart, rayDir, _PlanetCenter, _PlanetRadius + _AtmosphereHeight);
float rayLength = atmIntersection.y;
// It is ok to put here
// vec3 lightDir = normalize(-u_LightDir);
// If ray intersect whith the earth.
vec2 earthIntersection = RaySphereIntersection(rayStart, rayDir, _PlanetCenter, _PlanetRadius);
if (earthIntersection.x > 0.0) {
rayLength = earthIntersection.x;
}
vec3 inscattering = IntegrateInscattering(rayStart, rayDir, rayLength, lightDir);
vec3 color = PostProcssing(inscattering, 0.0);
gl_FragColor = vec4(color.x, color.y, color.z, 1.0);
}
Not sure if bgfx_FragData0 will affect the compiler to judge if uniform variable contributes to gl_Color , the hlsl is:
void main( float4 gl_FragCoord : SV_POSITION , float3 v_worldPos : TEXCOORD1 , out float4 bgfx_FragData0 : SV_TARGET0 )
{
float4 bgfx_VoidFrag = vec4_splat(0.0);
float3 rayStart = u_params[0].xyz;
float3 rayDir = normalize(v_worldPos.xyz);
float3 lightDir = normalize(-u_params[1].xyz);
float2 atmIntersection = RaySphereIntersection(rayStart, rayDir, float3(0.0, -6371, 0.0), 6371 + 80);
float rayLength = atmIntersection.y;
float2 earthIntersection = RaySphereIntersection(rayStart, rayDir, float3(0.0, -6371, 0.0), 6371);
if (earthIntersection.x > 0.0) {
rayLength = earthIntersection.x;
}
float3 inscattering = IntegrateInscattering(rayStart, rayDir, rayLength, lightDir);
float3 color = PostProcssing(inscattering, 0.0);
bgfx_FragData0 = float4(color.x, color.y, color.z, 1.0);
}