| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
|---|---|
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #include "Components/BsCRenderable.h" |
| 4 | #include "Private/RTTI/BsCRenderableRTTI.h" |
| 5 | #include "Scene/BsSceneObject.h" |
| 6 | #include "Mesh/BsMesh.h" |
| 7 | #include "Material/BsMaterial.h" |
| 8 | #include "Components/BsCAnimation.h" |
| 9 | #include "Math/BsBounds.h" |
| 10 | #include "Scene/BsSceneManager.h" |
| 11 | |
| 12 | namespace bs |
| 13 | { |
| 14 | CRenderable::CRenderable() |
| 15 | { |
| 16 | setName("Renderable"); |
| 17 | setFlag(ComponentFlag::AlwaysRun, true); |
| 18 | } |
| 19 | |
| 20 | CRenderable::CRenderable(const HSceneObject& parent) |
| 21 | :Component(parent) |
| 22 | { |
| 23 | setName("Renderable"); |
| 24 | setFlag(ComponentFlag::AlwaysRun, true); |
| 25 | } |
| 26 | |
| 27 | void CRenderable::setMesh(HMesh mesh) |
| 28 | { |
| 29 | mInternal->setMesh(mesh); |
| 30 | |
| 31 | if (mAnimation != nullptr) |
| 32 | mAnimation->_updateBounds(false); |
| 33 | } |
| 34 | |
| 35 | void CRenderable::onInitialized() |
| 36 | { |
| 37 | // If mInternal already exists this means this object was deserialized, |
| 38 | // so all we need to do is initialize it. |
| 39 | if (mInternal != nullptr) |
| 40 | mInternal->initialize(); |
| 41 | else |
| 42 | mInternal = Renderable::create(); |
| 43 | |
| 44 | gSceneManager()._bindActor(mInternal, sceneObject()); |
| 45 | |
| 46 | mAnimation = SO()->getComponent<CAnimation>(); |
| 47 | if (mAnimation != nullptr) |
| 48 | { |
| 49 | _registerAnimation(mAnimation); |
| 50 | mAnimation->_registerRenderable(static_object_cast<CRenderable>(mThisHandle)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | Bounds CRenderable::getBounds() const |
| 55 | { |
| 56 | mInternal->_updateState(*SO()); |
| 57 | return mInternal->getBounds(); |
| 58 | } |
| 59 | |
| 60 | bool CRenderable::calculateBounds(Bounds& bounds) |
| 61 | { |
| 62 | bounds = getBounds(); |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | void CRenderable::_registerAnimation(const HAnimation& animation) |
| 68 | { |
| 69 | mAnimation = animation; |
| 70 | |
| 71 | if (mInternal != nullptr) |
| 72 | { |
| 73 | mInternal->setAnimation(animation->_getInternal()); |
| 74 | |
| 75 | // Need to update transform because animated renderables handle local transforms through bones, so it |
| 76 | // shouldn't be included in the renderable's transform. |
| 77 | mInternal->_updateState(*SO(), true); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void CRenderable::_unregisterAnimation() |
| 82 | { |
| 83 | mAnimation = nullptr; |
| 84 | |
| 85 | if(mInternal != nullptr) |
| 86 | { |
| 87 | mInternal->setAnimation(nullptr); |
| 88 | |
| 89 | // Need to update transform because animated renderables handle local transforms through bones, so it |
| 90 | // shouldn't be included in the renderable's transform. |
| 91 | mInternal->_updateState(*SO(), true); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void CRenderable::update() |
| 96 | { |
| 97 | |
| 98 | } |
| 99 | |
| 100 | void CRenderable::onDestroyed() |
| 101 | { |
| 102 | if (mAnimation != nullptr) |
| 103 | mAnimation->_unregisterRenderable(); |
| 104 | |
| 105 | gSceneManager()._unbindActor(mInternal); |
| 106 | mInternal->destroy(); |
| 107 | } |
| 108 | |
| 109 | RTTITypeBase* CRenderable::getRTTIStatic() |
| 110 | { |
| 111 | return CRenderableRTTI::instance(); |
| 112 | } |
| 113 | |
| 114 | RTTITypeBase* CRenderable::getRTTI() const |
| 115 | { |
| 116 | return CRenderable::getRTTIStatic(); |
| 117 | } |
| 118 | } |