Sign in to follow this  

raycasting unit cube

This topic is 3853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

lets say i have a unit cube, i have a point inside the unit cube. From this point, given a direction, i want to cast a ray to find where it intersects with the cube. currently i've got a list of 6 planes and im doing plane-ray intersection tests
float GetBoxIntersection(float3 rayDir, float3 rayPos, inout float3 intersectPoint)
{
	float closestTime = 1.0;
	
	[unroll]
	for (int i = 0; i < 6; ++i)
	{
		float rayDirDotNormal = dot(gBoxPlanes[i].xyz, rayDir);
		
		// skip parallel planes
		// why break and not continue you ask? well if parallel time will always be 1.0 ;)
		if (rayDirDotNormal == 0.0)
			break;
						
		float3 pointOnPlane = -gBoxPlanes[i].xyz * gBoxPlanes[i].w;
		float time = dot(gBoxPlanes[i].xyz, pointOnPlane - rayPos) / rayDirDotNormal;
		
		// hrmm can we fix the shimmering somehow?
		if (time <= 0.0001 || time >= closestTime)
			continue;
			
		closestTime = time;
	}

	intersectPoint = rayPos + (rayDir * closestTime);
	return closestTime;
}
but this is very expensive, so im guessing there has got to be a cheaper way to do this considering its a unit cube

Share this post


Link to post
Share on other sites

This topic is 3853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this