Skip to content

Commit

Permalink
Fix sun jump on the sky at 12:00
Browse files Browse the repository at this point in the history
  • Loading branch information
SaiyansKing committed Nov 9, 2022
1 parent 4a99dfb commit fc362df
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
7 changes: 7 additions & 0 deletions D3D11Engine/Toolbox.cpp
Expand Up @@ -250,7 +250,14 @@ namespace Toolbox {

/** Linear interpolation */
float lerp( float a, float b, float w ) {
#if defined(_XM_FMA3_INTRINSICS_)
__m128 v0 = _mm_set_ss( a );
__m128 v1 = _mm_set_ss( b );
__m128 t = _mm_set_ss( w );
return _mm_cvtss_f32( _mm_fmadd_ss( t, v1, _mm_fnmadd_ss( t, v0, v0 ) ) );
#else
return (1.0f - w) * a + w * b;
#endif
}

/** Converts an errorcode into a string */
Expand Down
27 changes: 23 additions & 4 deletions D3D11Engine/zCSkyController_Outdoor.h
Expand Up @@ -204,23 +204,42 @@ class zCSkyController_Outdoor : public zCSkyController {
#endif
}

__forceinline void __vectorcall MatrixVector3Multiply( XMFLOAT3& p, FXMVECTOR V, FXMMATRIX M ) noexcept
{
__m128 R0 = _mm_add_ss( XMVector3Dot( M.r[0], V ), _mm_shuffle_ps( M.r[0], M.r[0], _MM_SHUFFLE( 3, 3, 3, 3 ) ) );
__m128 R1 = _mm_add_ss( XMVector3Dot( M.r[1], V ), _mm_shuffle_ps( M.r[1], M.r[1], _MM_SHUFFLE( 3, 3, 3, 3 ) ) );
__m128 R2 = _mm_add_ss( XMVector3Dot( M.r[2], V ), _mm_shuffle_ps( M.r[2], M.r[2], _MM_SHUFFLE( 3, 3, 3, 3 ) ) );
p.x = _mm_cvtss_f32( R0 );
p.y = _mm_cvtss_f32( R1 );
p.z = _mm_cvtss_f32( R2 );
}

/** Returns the sun position in world coords */
XMFLOAT3 GetSunWorldPosition( float timeScale = 1.0f ) {
//float angle = GetMasterTime() * XM_2PI; // Get mastertime into rad, 0 and 12 are now at the horizon, 18 is in the sky
//angle += XM_PIDIV2; // 12 is now in the sky, 18 horizon
float skyTime = GetMasterTime();
if ( skyTime >= 0.708f ) {
skyTime = Toolbox::lerp(0.75f, 1.0f, (skyTime - 0.708f) / 0.292f );
} else if ( skyTime <= 0.292f ) {
skyTime = Toolbox::lerp( 0.0f, 0.25f, skyTime / 0.292f );
} else if ( skyTime >= 0.5f ) {
skyTime = Toolbox::lerp( 0.5f, 0.75f, (skyTime - 0.5f) / 0.208f );
} else {
skyTime = Toolbox::lerp( 0.25f, 0.5f, (skyTime - 0.292f) / 0.208f );
}

float angle;
if ( timeScale <= -1 ) {
angle = 4.71375f;
} else {
angle = ((GetMasterTime() * timeScale - 0.3f) * 1.25f + 0.5f) * XM_2PI;
angle = skyTime * timeScale * XM_2PI + XM_PIDIV2;
}

constexpr XMVECTORF32 sunPos = { -60, 0, 100, 0 };
XMFLOAT3 rotAxis = XMFLOAT3( 1, 0, 0 );

XMFLOAT3 pos;
//XMVector3NormalizeEst leads to jumping shadows dueto reduced accuracy in combination with XMStoreFloat3( &LightDir, XMVector3NormalizeEst( XMLoadFloat3( &LightDir ) ) ); but setting this mentioned code line in this comment to non Est does not influence if this active code line before the comment is Est or not
XMStoreFloat3( &pos, XMVector3TransformNormal( XMVector3Normalize( sunPos ), XMMatrixTranspose( XMLoadFloat4x4( &(HookedFunctions::OriginalFunctions.original_Alg_Rotation3DNRad( rotAxis, -angle )) ) ) ) );
MatrixVector3Multiply( pos, XMVector3Normalize( sunPos ), XMLoadFloat4x4( &(HookedFunctions::OriginalFunctions.original_Alg_Rotation3DNRad( rotAxis, -angle )) ) );

return pos;
}
Expand Down

0 comments on commit fc362df

Please sign in to comment.