Advertisement

Map worldspace Position into UVW space

Started by November 07, 2020 06:53 AM
4 comments, last by isu diss 4 years ago

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?

anybody home?

Advertisement

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.

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

cout<<Remap(512,0,1023,-1,1)<<endl;

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 ?

thanks @ddlox . @joej This is related to this, https://www.gamedev.net/forums/topic/708412-cloud-rendering-problem/​ .Im trying to read a 3d texture from world space position by converting world space pos into uvw. What is the best way to do that?

This topic is closed to new replies.

Advertisement