Dimev/Realistic-Atmosphere-Godot-and-UE4

UE4 Atmosphere shader breaks with Ray Tracing Enabled

arraybercov opened this issue · 7 comments

Heya,

I have a project that requires ray tracing enabled even though my space scene does not use it. When I enable ray tracing in the project settings the shader stops working. Any ideas how to fix?

Dimev commented

How exactly does it break? does it just disappear?

Hey Dimev,

Yes. I can tell you exactly how.

I get a SM5 Ray tracing closest hit shaders cannot read from the SceneTexturesStruct error in the Stats tab in the material graph (I'm looking at the non-post version.

The issue is in your Get Max Distance custom expression node. Any ideas how we could re-write that Texture2DSampleLevel function to not need this struct? Maybe we can add an input to Get Max Distance to replace that or just try and do it all with nodes?

float3 Forward = mul(float3(0.00000000,0.00000000,1.00000000), ResolvedView.ViewToTranslatedWorld);
float DeviceZ = Texture2DSampleLevel(SceneTexturesStruct.SceneDepthTexture, SceneTexturesStruct.SceneDepthTextureSampler, ScreenAlignedPosition(GetScreenPosition(Parameters)), 0).r;

float Depth = DeviceZ * View.InvDeviceZToWorldZTransform[0] + View.InvDeviceZToWorldZTransform[1] + 1.0f / (DeviceZ * View.InvDeviceZToWorldZTransform[2] - (View.InvDeviceZToWorldZTransform[3] + 0.00000001));

return Depth / abs(dot(Forward, Parameters.CameraVector));
Dimev commented

Someone else also had this issue in the 4.25 preview

The code I'm using here essentially makes sure the distance returned by the depth buffer is correct, because using the scene depth node divided by the dot product of the forward and camera vector directly results in the depth not being 100% accurate at large distances.

I assume the issue has something to do with the material being involved in raytracing. maybe it can be fixed by somehow making the material not a closest hit shader, but I'll have to look at it, and maybe a different workaround of the max distance thing.

If you have any ideas I may be able to implement, happy to try. If there is any way to do it without HLSL that'd be safer of course.

Dimev commented

The weird depth thing is because the ue4 devs added a very small value to DeviceZToWorldZ to avoid dividing by 0 in one shader in the engine (which is a stupid hack, just do proper divide by zero checking), causing the depth to be inaccurate, and needing this workaround (written by Dexyfex, not me)

There's probably a way to get around this with only nodes, but that needs investigation. Probably dividing the resulting depth by some values using nodes may work, but I haven't had success with that so far

Dimev commented

I found a fix for the issue, I'll soon upload a 4.25 version of the project and close this.
The new code for the Get Max Distance node is this:

float3 Forward = mul(float3(0.00000000,0.00000000,1.00000000), ResolvedView.ViewToTranslatedWorld);
float DeviceZ = LookupDeviceZ(ScreenAlignedPosition(GetScreenPosition(Parameters))).r;

float Depth = DeviceZ * View.InvDeviceZToWorldZTransform[0] + View.InvDeviceZToWorldZTransform[1] + 1.0f / (DeviceZ * View.InvDeviceZToWorldZTransform[2] - (View.InvDeviceZToWorldZTransform[3] + 0.00000001));

return Depth / abs(dot(Forward, Parameters.CameraVector));
Dimev commented

I uploaded the version with the fix, should work now