//------------------------------------------------------------------------------------------------------- void LightNode::Update( uint milisecon ) { if( isChange() || isFatherChange() || m_isLightChange ) { if ( isChange() || isFatherChange() ) { LocatableObject::Update( milisecon ); vector3f ws = GetWorldScale(); m_MaxScale = Math::GetMin( ws.m_x, Math::GetMin( ws.m_y, ws.m_z ) ); if ( m_eLightType != LT_POINT ) { m_WorldDirection = LightDirectionRaw * GetWorldRotation(); m_WorldUp = LightUpRaw * GetWorldRotation(); } CastChangedMessage();//通知子节点 _SetChangeFlag( false ); } m_isLightChange = false; _OnLightChangeRecaculater();//告诉自己的继承类,灯光变了 } }
//----------------------------------------------------------------------------------------------------------------------------------- void GameMouse::Draw(SpriteBatch* spriteBatch, SpriteFont* spriteFont) { if (IsVisible()) { spriteBatch->Draw( GetTexture()->GetTexture(), GetWorldPosition(), nullptr, GetColour() * GetOpacity(), GetWorldRotation(), Vector2::Zero, XMVectorDivide(GetSize(), GetTexture()->GetDimensions())); } }
//----------------------------------------------------------------------------------------------------------------------------------- void Label::Draw(SpriteBatch* spriteBatch, SpriteFont* spriteFont) { // Catches the label size being zero if (GetSize() == Vector2::Zero) { SetSize(spriteFont->MeasureString(m_text.c_str())); } // Do not need to do any base drawing - will just be doing a custom draw using the spritefont spriteFont->DrawString( spriteBatch, m_text.c_str(), GetWorldPosition(), GetColour(), GetWorldRotation(), spriteFont->MeasureString(m_text.c_str()) * 0.5f, XMVectorDivide(GetSize(), spriteFont->MeasureString(m_text.c_str()))); }
void Camera::_updateTM() { if (mTmChangeFlags == 0) return ; Node::_updateTM(); const Float3 & pos = GetWorldPosition(); const Quat & ort = GetWorldRotation(); mMatView.MakeViewLH(pos, ort); if (GetOrthoEnable()) { mMatProj.MakeOrthoLH(mOrthoWidth, mOrthoHeight, mNear, mFar); } else { mMatProj.MakePerspectiveLH(mFovy, mAspect, mNear, mFar); } mMatViewProj = mMatView * mMatProj; mFrustum.FromMatrix(mMatViewProj); // update corner Float3 corner[8] = { Float3(-1, 1, 0), Float3(1, 1, 0), Float3(-1, -1, 0), Float3(1, -1, 0), Float3(-1, 1, 1), Float3(1, 1, 1), Float3(-1, -1, 1), Float3(1, -1, 1) }; Mat4 matInvProj = mMatProj, matInvView = mMatView; matInvProj.Inverse(); matInvView.Inverse(); for (int i = 0; i < 8; ++i) mViewCorner[i] = corner[i] * matInvProj; for (int i = 0; i < 8; ++i) mWorldCorner[i] = mViewCorner[i] * matInvView; }
//----------------------------------------------------------------------------------------------------------------------------------- void BaseObject::Draw(SpriteBatch* spriteBatch, SpriteFont* spriteFont) { CheckVisible(); if (!m_visible) { return; } // Put draw code here assert(GetTexture()->GetTexture()); spriteBatch->Draw( GetTexture()->GetTexture(), GetWorldPosition(), nullptr, m_colour * m_opacity, GetWorldRotation(), m_textureHandler->GetCentre(), XMVectorDivide(m_size, m_textureHandler->GetDimensions())); }
//-------------------------------------------------------------------------------------------------------------------------------------- void Emitter::Update(uint millisecond) { LocatableObject::Update( millisecond ); m_isInview = false; if( !m_isForceStop ) { if( isEmit() || m_uLifedTime <= m_EmitterLife + m_LifeSpan.m_y )//如果粒子还活着 { aabbox3df worldbox = m_BindBox; //初始的绑定和不能变 worldbox.SetCenter( GetWorldPosition() ); if ( Engine::Instance().GetCamera()->GetFrustum().Intersect( worldbox ) )//判断是否可视 { m_isInview = true; } //更新粒子 m_uLifedTime += millisecond; if( m_isInview ) { m_ActiveTime += millisecond; if( m_isEmit ) { m_isEmit = m_uLifedTime < m_EmitterLife || m_EmitterLife < 0;//计算是否继续发射 vector2d view = Engine::Instance().GetDevice()->GetResolutionSize(); float particlesize = ( view.m_x + view.m_y ) * 0.5; float worldscale = Math::GetMax( GetWorldScale().m_x, Math::GetMax( GetWorldScale().m_y, GetWorldScale().m_z ) ); m_uTimeSpan += millisecond; int randomNum = Math::Random(m_CountSec.m_x, m_CountSec.m_y); float fnum = randomNum * TO_SEC( m_uTimeSpan ); int relaseNum = fnum; if( 0 != relaseNum) { m_uTimeSpan = ( fnum - relaseNum ) / randomNum * m_uTimeSpan; //计算一下小数位置的粒子个数是对应多长时间差内的粒子个数,这个是降低误差用的 relaseNum = Math::GetMin( relaseNum , m_Index.Max() ); int beginIndex = m_Index; for (int i = 0 ; i < relaseNum ; i++) { Quaternionf rotation; rotation.YawPitchRoll(Math::fRandom(-m_Angle.m_y,m_Angle.m_y), Math::fRandom(-m_Angle.m_x,m_Angle.m_x) , Math::fRandom(-m_Angle.m_z,m_Angle.m_z)); //Matrix33f rotation; //rotation.YawPitchRoll(fRandom(-m_Angle.m_y,m_Angle.m_y), fRandom(-m_Angle.m_x,m_Angle.m_x) , fRandom(-m_Angle.m_z,m_Angle.m_z)); vector3f Velocity = m_Direction * rotation * GetWorldRotation(); Velocity.NormalizeSelf(); Velocity *= ( Math::fRandom( m_Speed.m_x , m_Speed.m_y ) ); Color Beginclr, Endclr, clrInc; if( m_isBeginChangeColor ) { Beginclr = Color(Math::fRandom(m_BeginColorA.m_r, m_BeginColorB.m_r),Math::fRandom(m_BeginColorA.m_g, m_BeginColorB.m_g),Math::fRandom(m_BeginColorA.m_b, m_BeginColorB.m_b),Math::fRandom(m_BeginColorA.m_a, m_BeginColorB.m_a)); } else { Beginclr = m_BeginColorA; } if( m_isEndChangeColor ) { Endclr = Color(Math::fRandom(m_EndColorA.m_r, m_EndColorB.m_r),Math::fRandom(m_EndColorA.m_g, m_EndColorB.m_g),Math::fRandom(m_EndColorA.m_b, m_EndColorB.m_b),Math::fRandom(m_EndColorA.m_a, m_EndColorB.m_a)); } else { Endclr = m_EndColorA; } if( m_PositionNoise == vector3f::Zero() ) { m_pParticle[m_Index].Create( GetWorldPosition(), Beginclr, Endclr, Velocity * worldscale, m_ActiveTime, Math::Random(m_LifeSpan.m_x, m_LifeSpan.m_y), Math::fRandom(m_BeginSize.m_x, m_BeginSize.m_y) * particlesize * worldscale, Math::fRandom(m_EndSize.m_x, m_EndSize.m_y) * particlesize * worldscale, Math::fRandom(m_ColorPow.m_x, m_ColorPow.m_y), Math::fRandom(m_AlphaPow.m_x, m_AlphaPow.m_y), Math::fRandom(m_SizePow.m_x, m_SizePow.m_y) ); m_Index++; } else { m_pParticle[m_Index].Create( GetWorldPosition() + vector3f( Math::fRandom( -m_PositionNoise.m_x, m_PositionNoise.m_x ), Math::fRandom( -m_PositionNoise.m_y, m_PositionNoise.m_y ), Math::fRandom( -m_PositionNoise.m_z, m_PositionNoise.m_z ) ) * worldscale, Beginclr, Endclr, Velocity * worldscale, m_ActiveTime, Math::Random(m_LifeSpan.m_x, m_LifeSpan.m_y), Math::fRandom(m_BeginSize.m_x, m_BeginSize.m_y) * particlesize * worldscale, Math::fRandom(m_EndSize.m_x, m_EndSize.m_y) * particlesize * worldscale, Math::fRandom(m_ColorPow.m_x, m_ColorPow.m_y), Math::fRandom(m_AlphaPow.m_x, m_AlphaPow.m_y), Math::fRandom(m_SizePow.m_x, m_SizePow.m_y) ); m_Index++; } } if( beginIndex + relaseNum > m_ArraySize ) //头尾事件,从新flush { int toend = m_ArraySize - beginIndex; m_pVertexBuffer->FlushVertexBuffer( beginIndex, toend, &m_pParticle[beginIndex] ); m_pVertexBuffer->FlushVertexBuffer( 0, relaseNum - toend, &m_pParticle[0] ); } else { m_pVertexBuffer->FlushVertexBuffer( beginIndex, relaseNum, &m_pParticle[beginIndex] ); } } } } } } }
//----------------------------------------------------------------------------------------------------------------------------------- const Vector2 BaseObject::GetWorldPosition(const Vector2& localPosition) const { if (!m_parent) { return localPosition; } // Horrible, but like this for optimization purposes return m_parent->GetWorldPosition() + Vector2::Transform(localPosition, Matrix::CreateRotationZ(GetWorldRotation())); }