Skip to main content
GameDev.net gamedev.net
🔒 Locked

Map worldspace Position into UVW space

Started by isu diss Nov 7, 2020 at 6:53 AM 4 replies 7.1k views
Original Post
isu diss
isu diss

What I came up with is this. Any thoughts?

float Remap( float value, float inMin, float inMax, float outMin, float outMax )
{
	float mappedValue = value;

	if ( ( inMax - inMin ) == 0.0f )
	{
		mappedValue = ( outMax + outMin ) * 0.5f;
	}
	else
	{
		mappedValue = outMin + ( ( ( mappedValue - inMin ) / ( inMax - inMin ) ) * ( outMax - outMin ) );
	}

	return mappedValue;
}

float2 RangeMapFloat2( float2 value, float2 inMin, float2 inMax, float2 outMin, float2 outMax )
{
	float2 mappedValue = value;

	if ( ( inMax - inMin ).x == 0 &&  ( inMax - inMin ).y == 0)
	{
		mappedValue = ( outMax + outMin ) * 0.5f;
	}
	else
	{
		mappedValue = outMin + ( ( ( mappedValue - inMin ) / ( inMax - inMin ) ) * ( outMax - outMin ) );
	}

	return mappedValue;
}

//inMinMaxCloudLayer = (InnerRadius+1500, InnerRadius+4000)
float3 GetSampleUVW(float3 pos)
{
	float3 uvw;
	float radius = length(pos);
	float halfArcLength = 2*(inMinMaxCloudLayer.y-inMinMaxCloudLayer.x);
	float xRadius = length(float2(pos.x, pos.y));
	float xArcThetaRadians = halfArcLength / xRadius;
	float xMax = xRadius * sin(xArcThetaRadians);
	float zRadius = length(float2(pos.z, pos.y));
	float zArcThetaRadians = halfArcLength / zRadius;
	float zMax = zRadius * sin(zArcThetaRadians);

	float2 uvMappedXZ = RangeMapFloat2(pos.xz, float2(-xMax, -zMax), float2(xMax, zMax), float2(0, 0), float2(1, 1));
	uvw.x = uvMappedXZ.x;
	uvw.y = Remap(radius, (radius-inMinMaxCloudLayer.x), (inMinMaxCloudLayer.y-radius), 0.0f, 1.0f);
	uvw.z = uvMappedXZ.y;
	return uvw;
}

Is above code ok?

isu diss
isu diss

anybody home?

JoeJ
JoeJ

isu diss said:
anybody home?

Sure, it's Lockup time : )

But seriously, what do you try to do and what's the problem? We can just guess.

ddlox
ddlox

your Remap( ) and RangeMapFloat2( ) maths are fine as long as you're aware that for example:

cout<

remaps to 512 to 0.000977516 and not to 0 ( it is 511.5 which remaps to 0 in this case), I explained this in a recent post to a similar query;

about your GetSampleUVW( ) , this looks like you're trying to reconstruct texture coords from a spherical world position (which in your case appears to be an inverse spherical projection) then remapping to x ∈ [0, 1], y ∈ [0, 1], z ∈ [0,1];

if this is the case then my purely semi-educated guess is yes it seems to be ok (without plugging in any numbers) ?

maybe use this to verify a bit more: https://en.wikipedia.org/wiki/Spherical_coordinate_system

hope this helps ?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.