Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

unity fast sin

// Low percision sine approximation
public class SineLP
{
	private static float sin = 0;

	public static float Sin( float x )
	{
		if (x < -3.14159265f)
			x += 6.28318531f;
		else
			if (x >  3.14159265f)
				x -= 6.28318531f;

		if ( x < 0 )
			return x * ( 1.27323954f + 0.405284735f * x );
		else
			return x * ( 1.27323954f - 0.405284735f * x );
	}
}

// High percision sine approximation
public class SineHP
{
	private static float sin = 0;

	public static float Sin( float x )
	{
		if (x < -3.14159265f)
			x += 6.28318531f;
		else
			if (x >  3.14159265f)
				x -= 6.28318531f;	

		if ( x < 0 )
		{
			sin = x * ( 1.27323954f + 0.405284735f * x );

			if ( sin < 0 )
				sin = sin * ( -0.255f * ( sin + 1 ) + 1 );
			else
				sin = sin * ( 0.255f * ( sin - 1 ) + 1 );
		}
		else
		{
			sin = x * ( 1.27323954f - 0.405284735f * x );

			if ( sin < 0 )
				sin = sin * ( -0.255f * ( sin + 1 ) + 1 );
			else
				sin = sin * ( 0.255f * ( sin - 1 ) + 1 );
		}

		return sin;
	}
}
Source by www.mclimatiano.com #
 
PREVIOUS NEXT
Tagged: #unity #fast #sin
ADD COMMENT
Topic
Name
7+5 =