//----------------------------------------------------------- //----------------------------------------------------------- void SpriteComponent::Render(RenderSystem* inpRenderSystem, CameraComponent* inpCam, ShaderPass ineShaderPass) { if (ineShaderPass == ShaderPass::k_ambient) { if(IsTextureSizeCacheValid() == false) { OnTransformChanged(); SetTextureSizeCacheValid(); } if(m_vertexPositionsValid == false) { //We have been transformed so we need to recalculate our vertices UpdateVertexPositions(); m_vertexPositionsValid = true; } m_spriteData.pMaterial = mpMaterial; //Add us to the render systems dynamic batch //If we force a batch flush here then the previous sprites //will be rendered. inpRenderSystem->GetDynamicSpriteBatchPtr()->Render(m_spriteData); } }
void SpatialNode::SetTransform(const Vector3& newPosition, const Quaternion& newRotation, const Vector3& newScale) { position = newPosition; rotation = newRotation; scale = newScale; OnTransformChanged(); }
void SpatialNode::Rotate(const Quaternion& delta, TransformSpace space) { SpatialNode* parentNode = SpatialParent(); switch (space) { case TS_LOCAL: rotation = (rotation * delta).Normalized(); break; case TS_PARENT: rotation = (delta * rotation).Normalized(); break; case TS_WORLD: if (!parentNode) rotation = (delta * rotation).Normalized(); else { Quaternion worldRotation = WorldRotation(); rotation = rotation * worldRotation.Inverse() * delta * worldRotation; } break; } OnTransformChanged(); }
//---------------------------------------------------------------- /// Move By /// /// @param X Component /// @param Y Component /// @param Z Component //---------------------------------------------------------------- void Transform::MoveBy(f32 infX, f32 infY, f32 infZ) { mvPosition.x += infX; mvPosition.y += infY; mvPosition.z += infZ; OnTransformChanged(); }
//---------------------------------------------------------------- /// Scale By /// /// Scale along each axis by the given amount /// /// @param Axis vector //---------------------------------------------------------------- void Transform::ScaleBy(const Vector3 &Vec) { mvScale.x *= Vec.x; mvScale.y *= Vec.y; mvScale.z *= Vec.z; OnTransformChanged(); }
//---------------------------------------------------------------- /// Scale By /// /// Scale along each axis by the given amount /// /// @param X Component /// @param Y Component /// @param Z Component //---------------------------------------------------------------- void Transform::ScaleBy(f32 inX, f32 inY, f32 inZ) { mvScale.x *= inX; mvScale.y *= inY; mvScale.z *= inZ; OnTransformChanged(); }
//---------------------------------------------------------------- /// Scale By /// /// Scale uniformly by the given amount /// /// @param Scale factor //---------------------------------------------------------------- void Transform::ScaleBy(f32 inScale) { mvScale.x *= inScale; mvScale.y *= inScale; mvScale.z *= inScale; OnTransformChanged(); }
//---------------------------------------------------------------- /// Set Position Scale Orientation /// /// @param Position vector /// @param Scale vector /// @param Orientation quaternion //---------------------------------------------------------------- void Transform::SetPositionScaleOrientation(const Vector3& invPos, const Vector3& invScale, const Quaternion& invOrientation) { mvPosition = invPos; mvScale = invScale; mqOrientation = invOrientation; OnTransformChanged(); }
//---------------------------------------------------------------- /// Scale To /// /// Scale each axis to the given amount /// /// @param Axis vector //---------------------------------------------------------------- void Transform::ScaleTo(const Vector3 &Vec) { if(mvScale == Vec) return; mvScale = Vec; OnTransformChanged(); }
//---------------------------------------------------------------- /// Set Orientation /// /// @param Orientation quaternion //---------------------------------------------------------------- void Transform::SetOrientation(const Quaternion & inqOrientation) { if(mqOrientation == inqOrientation) return; mqOrientation = inqOrientation; OnTransformChanged(); }
//---------------------------------------------------------------- /// Set Position /// /// @param Position vector //---------------------------------------------------------------- void Transform::SetPosition(const Vector3 &invPos) { if(mvPosition == invPos) return; mvPosition = invPos; OnTransformChanged(); }
//----------------------------------------------------------- //----------------------------------------------------------- void SpriteComponent::SetTextureAtlasId(const std::string& in_atlasId) { CS_ASSERT(m_textureAtlas != nullptr, "SpriteComponent: Cannot set texture atlas id until atlas has been set"); OnTransformChanged(); m_hashedTextureAtlasId = HashCRC32::GenerateHashCode(in_atlasId); m_uvs = m_textureAtlas->GetFrameUVs(m_hashedTextureAtlasId); }
//---------------------------------------------------------------- /// Set Local Transform /// /// This will overwrite any local previous transformations /// /// @param Objects transformation matrix //---------------------------------------------------------------- void Transform::SetLocalTransform(const Matrix4& inmatTransform) { inmatTransform.Decompose(mvPosition, mvScale, mqOrientation); mmatTransform = inmatTransform; OnTransformChanged(); mbIsTransformCacheValid = false; }
//---------------------------------------------------------------- /// Scale To /// /// Scale each axis to the given amount /// /// @param X Component /// @param Y Component /// @param Z Component //---------------------------------------------------------------- void Transform::ScaleTo(f32 inX, f32 inY, f32 inZ) { if(mvScale.x == inX && mvScale.y == inY && mvScale.z == inZ) return; mvScale.x = inX; mvScale.y = inY; mvScale.z = inZ; OnTransformChanged(); }
//---------------------------------------------------------------- /// Scale To /// /// Scale uniformly to the given amount /// /// @param Scale factor //---------------------------------------------------------------- void Transform::ScaleTo(f32 inScale) { if(mvScale.x == inScale && mvScale.y == inScale && mvScale.z == inScale) return; mvScale.x = inScale; mvScale.y = inScale; mvScale.z = inScale; OnTransformChanged(); }
//---------------------------------------------------------------- /// Set World Transform /// /// This will overwrite any parent or previous transformations /// /// @param Objects transformation matrix //---------------------------------------------------------------- void Transform::SetWorldTransform(const Matrix4& inmatTransform) { inmatTransform.Decompose(mvWorldPosition, mvWorldScale, mqWorldOrientation); mmatWorldTransform = inmatTransform; OnTransformChanged(); mbIsTransformCacheValid = true; mbIsParentTransformCacheValid = true; }
//---------------------------------------------------------------- /// Set Position /// /// @param X Component /// @param Y Component /// @param Z Component //---------------------------------------------------------------- void Transform::SetPosition(f32 infX, f32 infY, f32 infZ) { if(mvPosition.x == infX && mvPosition.y == infY && mvPosition.z == infZ) return; mvPosition.x = infX; mvPosition.y = infY; mvPosition.z = infZ; OnTransformChanged(); }
void SpatialNode::SetScale(const Vector3& newScale) { scale = newScale; // Make sure scale components never go to exactly zero, to prevent problems with decomposing the world matrix if (scale.x == 0.0f) scale.x = M_EPSILON; if (scale.y == 0.0f) scale.y = M_EPSILON; if (scale.z == 0.0f) scale.z = M_EPSILON; OnTransformChanged(); }
//---------------------------------------------------- //---------------------------------------------------- const Core::Sphere& SpriteComponent::GetBoundingSphere() { if(IsTextureSizeCacheValid() == false) { OnTransformChanged(); SetTextureSizeCacheValid(); } if(GetEntity() && m_isBSValid == false) { m_isBSValid = true; Core::Vector2 transformedSize = GetSize(); // Realign the origin Core::Vector2 anchorPoint = GetAnchorPoint(m_originAlignment, transformedSize * 0.5f); mBoundingSphere.vOrigin = GetEntity()->GetTransform().GetWorldPosition() - Core::Vector3(anchorPoint, 0.0f); mBoundingSphere.fRadius = std::sqrt((transformedSize.x * transformedSize.x) + (transformedSize.y * transformedSize.y)) * 0.5f; } return mBoundingSphere; }
void SpatialNode::Translate(const Vector3& delta, TransformSpace space) { SpatialNode* parentNode = SpatialParent(); switch (space) { case TS_LOCAL: // Note: local space translation disregards local scale for scale-independent movement speed position += rotation * delta; break; case TS_PARENT: position += delta; break; case TS_WORLD: position += !parentNode ? delta : parentNode->WorldTransform().Inverse() * Vector4(delta, 0.0f); break; } OnTransformChanged(); }
void SpatialNode::RotateAround(const Vector3& point, const Quaternion& delta, TransformSpace space) { SpatialNode* parentNode = SpatialParent(); Vector3 parentSpacePoint; Quaternion oldRotation = rotation; switch (space) { case TS_LOCAL: parentSpacePoint = Transform() * point; rotation = (rotation * delta).Normalized(); break; case TS_PARENT: parentSpacePoint = point; rotation = (delta * rotation).Normalized(); break; case TS_WORLD: if (!parentNode) { parentSpacePoint = point; rotation = (delta * rotation).Normalized(); } else { parentSpacePoint = parentNode->WorldTransform().Inverse() * point; Quaternion worldRotation = WorldRotation(); rotation = rotation * worldRotation.Inverse() * delta * worldRotation; } break; } Vector3 oldRelativePos = oldRotation.Inverse() * (position - parentSpacePoint); position = rotation * oldRelativePos + parentSpacePoint; OnTransformChanged(); }
//---------------------------------------------------- //---------------------------------------------------- const Core::AABB& SpriteComponent::GetAABB() { if(IsTextureSizeCacheValid() == false) { OnTransformChanged(); SetTextureSizeCacheValid(); } if(GetEntity() && m_isAABBValid == false) { m_isAABBValid = true; Core::Vector2 transformedSize = GetSize(); // Realign the origin Core::Vector2 anchorPoint = GetAnchorPoint(m_originAlignment, transformedSize * 0.5f); // Rebuild the box mBoundingBox.SetSize(Core::Vector3(transformedSize, 0.0f)); mBoundingBox.SetOrigin(GetEntity()->GetTransform().GetWorldPosition() - Core::Vector3(anchorPoint, 0.0f)); } return mBoundingBox; }
//---------------------------------------------------- //---------------------------------------------------- const OOBB& SpriteComponent::GetOOBB() { if(IsTextureSizeCacheValid() == false) { OnTransformChanged(); SetTextureSizeCacheValid(); } if(GetEntity() && m_isOOBBValid == false) { m_isOOBBValid = true; Vector2 transformedSize = GetSize(); // Realign the origin Vector2 anchorPoint = GetAnchorPoint(m_originAlignment, transformedSize * 0.5f); // Rebuild the box mOBBoundingBox.SetOrigin(Vector3(-anchorPoint, 0.0f)); mOBBoundingBox.SetSize(Vector3(transformedSize, 0.0f)); mOBBoundingBox.SetTransform(GetEntity()->GetTransform().GetWorldTransform()); } return mOBBoundingBox; }
void SpatialNode::SetDirection(const Vector3& newDirection) { rotation = Quaternion(Vector3::FORWARD, newDirection); OnTransformChanged(); }
void SpatialNode::SetRotation(const Quaternion& newRotation) { rotation = newRotation; OnTransformChanged(); }
void SpatialNode::OnParentSet(Node* newParent, Node*) { SetFlag(NF_SPATIAL_PARENT, dynamic_cast<SpatialNode*>(newParent) != 0); OnTransformChanged(); }
void SpatialNode::ApplyScale(const Vector3& delta) { scale *= delta; OnTransformChanged(); }
void SpatialNode::SetPosition(const Vector3& newPosition) { position = newPosition; OnTransformChanged(); }
//----------------------------------------------------------- //----------------------------------------------------------- void SpriteComponent::SetTextureAtlas(const TextureAtlasCSPtr& in_atlas) { OnTransformChanged(); m_textureAtlas = in_atlas; }
//---------------------------------------------------- //---------------------------------------------------- void SpriteComponent::OnAddedToScene() { m_transformChangedConnection = GetEntity()->GetTransform().GetTransformChangedEvent().OpenConnection(Core::MakeDelegate(this, &SpriteComponent::OnTransformChanged)); OnTransformChanged(); }