Your thoughts on and comments to Volume Rendering in Unreal Engine 4.


//bring vectors into local space to support object transforms
float3 localcampos = mul(float4( ResolvedView.WorldCameraOrigin,1.00000000), (Primitive.WorldToLocal)).xyz;
float3 localcamvec = -normalize( mul(Parameters.CameraVector, Primitive.WorldToLocal) );

//make camera position 0-1
localcampos = (localcampos / (Primitive.LocalObjectBoundsMax.x * 2)) + 0.5;

float3 invraydir = 1 / localcamvec;

float3 firstintersections = (0 - localcampos) * invraydir;
float3 secondintersections = (1 - localcampos) * invraydir;
float3 closest = min(firstintersections, secondintersections);
float3 furthest = max(firstintersections, secondintersections);

float t0 = max(closest.x, max(closest.y, closest.z));
float t1 = min(furthest.x, min(furthest.y, furthest.z));

float planeoffset = 1-frac( ( t0 - length(localcampos-0.5) ) * MaxSteps );

t0 += (planeoffset / MaxSteps) * PlaneAlignment;
 float scale = length( TransformLocalVectorToWorld(Parameters, float3(1.00000000,0.00000000,0.00000000)).xyz);
float localscenedepth = CalcSceneDepth(ScreenAlignedPosition(GetScreenPosition(Parameters)));

float3 camerafwd = mul(float3(0.00000000,0.00000000,1.00000000),ResolvedView.ViewToTranslatedWorld);
localscenedepth /= (Primitive.LocalObjectBoundsMax.x * 2 * scale);
localscenedepth /= abs( dot( camerafwd, Parameters.CameraVector ) );

//this line goes just before the line: t0 = max(0, t0);
t1 = min(t1, localscenedepth);
t0 = max(0, t0);

float boxthickness = max(0, t1 - t0);
float3 entrypos = localcampos + (max(0,t0) * localcamvec);
float3 exitpos = localcampos + (max(0,t1) * localcamvec);
return float4( exitpos, boxthickness );

///////////////////////////////////////////////////////////////////////////////////////////

float numFrames = XYFrames * XYFrames;
float4 accumdist = float4(0, 0, 0, 0);
float3 localcamvec = normalize( mul(Parameters.CameraVector, Primitive.WorldToLocal) );

for (int i = 0; i < MaxSteps; i++)
{
    float4 cursample = PseudoVolumeTextureColour(Tex, TexSampler, saturate(CurPos), XYFrames, numFrames);
    accumdist = lerp(accumdist, cursample, cursample.a);
	//accumdist += cursample * StepSize;
    CurPos += localcamvec * StepSize;
}


return accumdist*Opacity;

I am not using CLAMP(), but when I calculate the exitpos at the end I use MAX(0, t1) so I make sure it is greater or equal to 0. I added the RayMarcher as well for reference.

EDIT:
I found out what it was. Since the size of the volume can change arbitrarily I can not assume that the scale is x=y=z. When I scale the volume uniformly it works fine.