//------------------------------------------------------------ //------------------------------------------------------------ void SpriteComponent::OnRenderSnapshot(RenderSnapshot& in_renderSnapshot) noexcept { Vector2 frameCenter; Vector2 frameSize; if(m_textureAtlas != nullptr && m_hashedTextureAtlasId > 0) { CalcFrameCentreAndSize(frameCenter, frameSize); } else if(mpMaterial != nullptr && mpMaterial->GetTexture() != nullptr) { auto texture = mpMaterial->GetTexture().get(); frameSize = m_sizePolicyDelegate(m_originalSize, Vector2((f32)texture->GetDimensions().x, (f32)texture->GetDimensions().y)); } UVs transformedUVs = m_uvs; if(m_flippedHorizontally == true && m_flippedVertically == true) { transformedUVs = UVs::FlipDiagonally(transformedUVs); } else if(m_flippedHorizontally == true) { transformedUVs = UVs::FlipHorizontally(transformedUVs); } else if(m_flippedVertically == true) { transformedUVs = UVs::FlipVertically(transformedUVs); } const auto& transform = GetEntity()->GetTransform(); auto renderDynamicMesh = SpriteMeshBuilder::Build(in_renderSnapshot.GetFrameAllocator(), Vector3(frameCenter, 0.0f), frameSize, transformedUVs, m_colour, m_originAlignment); auto boundingSphere = Sphere::Transform(renderDynamicMesh->GetBoundingSphere(), transform.GetWorldPosition(), transform.GetWorldScale()); in_renderSnapshot.AddRenderObject(RenderObject(GetMaterial()->GetRenderMaterialGroup(), renderDynamicMesh.get(), transform.GetWorldTransform(), boundingSphere, false, RenderLayer::k_standard)); in_renderSnapshot.AddRenderDynamicMesh(std::move(renderDynamicMesh)); }
//----------------------------------------------------------- /// The image from the texture atlas will have potentially /// been cropped by the tool. This will affect the sprites /// position within the uncropped image and we need to /// account for that when positioning the corners //----------------------------------------------------------- void SpriteComponent::UpdateVertexPositions() { Core::Vector2 frameCenter; Core::Vector2 frameSize; if(m_textureAtlas != nullptr && m_hashedTextureAtlasId > 0) { CalcFrameCentreAndSize(frameCenter, frameSize); } else if(mpMaterial != nullptr && mpMaterial->GetTexture() != nullptr) { auto texture = mpMaterial->GetTexture().get(); frameSize = m_sizePolicyDelegate(m_originalSize, Core::Vector2((f32)texture->GetWidth(), (f32)texture->GetHeight())); } const Core::Matrix4& worldTransform = GetEntity()->GetTransform().GetWorldTransform(); Core::Vector2 halfFrameSize(frameSize.x * 0.5f, frameSize.y * 0.5f); Core::Vector2 alignedPosition = -GetAnchorPoint(m_originAlignment, halfFrameSize); Core::Vector4 vertexCentre(alignedPosition.x + frameCenter.x, alignedPosition.y + frameCenter.y, 0.0f, 1.0f); //TL Core::Vector4 vertexOffset(-halfFrameSize.x, halfFrameSize.y, 0.0f, 0.0f); m_spriteData.sVerts[(u32)SpriteBatch::Verts::k_topLeft].vPos = (vertexCentre + vertexOffset) * worldTransform; //TR vertexOffset.x = halfFrameSize.x; vertexOffset.y = halfFrameSize.y; m_spriteData.sVerts[(u32)SpriteBatch::Verts::k_topRight].vPos = (vertexCentre + vertexOffset) * worldTransform; //BL vertexOffset.x = -halfFrameSize.x; vertexOffset.y = -halfFrameSize.y; m_spriteData.sVerts[(u32)SpriteBatch::Verts::k_bottomLeft].vPos = (vertexCentre + vertexOffset) * worldTransform; //BR vertexOffset.x = halfFrameSize.x; vertexOffset.y = -halfFrameSize.y; m_spriteData.sVerts[(u32)SpriteBatch::Verts::k_bottomRight].vPos = (vertexCentre + vertexOffset) * worldTransform; }