Download

Post Processing/Global Distance Field effect fine in viewport/simulate but Black during play.

Hi

I’m experimenting with using the global distance field to do volumetric light shafts.

UE4Editor_2016-05-01_23-09-43.jpg

I know its expensive but wanted to give it a go. Turned out its not as bad as I though performance wise but there is one issue. Here’s my function going into a custom node. Looks okay in the viewport but goes black on play.


float3 curpos = CamPos;
float currentDistance = 0;
float currentLightDistance = 0;
float minDistance = minDist;
float3 lightVec = normalize(float3(1,1,0.12));
float3 lightPos;
float distanceToTravel = 0;
float distanceToTravelLight = 0;
float shadowMinDistance;
int i = 0;
float Accum = 0;
float T = 1.0;

while ( i < numSteps){
	
	
	currentDistance  = GetDistanceToNearestSurfaceGlobal(curpos);
	distanceToTravel = max(minDistance , currentDistance );


	if( (currentDistance < 0) ){

		return float(1-T);
	}
	
	float unOccluded = 1;
	int j = 0;
	
	lightPos = curpos;
	shadowMinDistance = minDistance; 
	while ( j < numSteps/4){

		currentLightDistance = GetDistanceToNearestSurfaceGlobal(lightPos);
		distanceToTravelLight = max(shadowMinDistance  , currentLightDistance );
		if ( currentLightDistance < 0){
			unOccluded = 0;
			j += 5000;
		}
		
		lightPos += lightVec * distanceToTravelLight ;
		shadowMinDistance += minDistMult;
		j++;
	}
	T *= 1.0 - density*distanceToTravel*unOccluded  ;

	//Accum += distanceToTravel *density * unOccluded ;
	//if (Accum > 1){
	//	return min(float(1),float(Accum));
	//}
	
	curpos -= CamVec * distanceToTravel;
	minDistance += minDistMult;
	i++;
}

return 1;

I figured out the issue. It was that the camera position wasn’t the same in the game vs editor. The ‘clipping plane’ or something like that must be different. If I subtract the camera vector from the position before sending it into the custom node then it all comes out fine.

UE4Editor_2016-05-02_21-23-40.jpg