DecalComponent.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Scene/Components/DecalComponent.h>
  6. #include <AnKi/Scene/SceneGraph.h>
  7. #include <AnKi/Resource/ResourceManager.h>
  8. #include <AnKi/Shaders/Include/ClusteredShadingTypes.h>
  9. #include <AnKi/Core/GpuMemory/GpuSceneBuffer.h>
  10. namespace anki {
  11. DecalComponent::DecalComponent(SceneNode* node)
  12. : SceneComponent(node, kClassType)
  13. {
  14. m_gpuSceneDecal.allocate();
  15. }
  16. DecalComponent::~DecalComponent()
  17. {
  18. }
  19. void DecalComponent::setLayer(CString fname, F32 blendFactor, LayerType type)
  20. {
  21. Layer& l = m_layers[type];
  22. ImageResourcePtr rsrc;
  23. const Error err = ResourceManager::getSingleton().loadResource(fname, rsrc);
  24. if(err)
  25. {
  26. ANKI_SCENE_LOGE("Failed to load image");
  27. return;
  28. }
  29. m_dirty = true;
  30. l.m_image = std::move(rsrc);
  31. l.m_bindlessTextureIndex = l.m_image->getTexture().getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  32. l.m_blendFactor = blendFactor;
  33. }
  34. Error DecalComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  35. {
  36. updated = m_dirty || info.m_node->movedThisFrame();
  37. if(updated)
  38. {
  39. m_dirty = false;
  40. const Vec3 halfBoxSize = m_boxSize / 2.0f;
  41. // Calculate the texture matrix
  42. const Mat4 worldTransform(info.m_node->getWorldTransform());
  43. const Mat4 viewMat = worldTransform.getInverse();
  44. const Mat4 projMat = Mat4::calculateOrthographicProjectionMatrix(halfBoxSize.x(), -halfBoxSize.x(), halfBoxSize.y(), -halfBoxSize.y(),
  45. kClusterObjectFrustumNearPlane, m_boxSize.z());
  46. const Mat4 biasMat4(0.5f, 0.0f, 0.0f, 0.5f, 0.0f, -0.5f, 0.0f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
  47. m_biasProjViewMat = biasMat4 * projMat * viewMat;
  48. // Update the spatial
  49. const Vec4 center(0.0f, 0.0f, -halfBoxSize.z(), 0.0f);
  50. const Vec4 extend(halfBoxSize.x(), halfBoxSize.y(), halfBoxSize.z(), 0.0f);
  51. const Obb obbL(center, Mat3x4::getIdentity(), extend);
  52. const Obb obbW = obbL.getTransformed(info.m_node->getWorldTransform());
  53. // Upload to the GPU scene
  54. GpuSceneDecal gpuDecal;
  55. gpuDecal.m_diffuseTexture = m_layers[LayerType::kDiffuse].m_bindlessTextureIndex;
  56. gpuDecal.m_roughnessMetalnessTexture = m_layers[LayerType::kRoughnessMetalness].m_bindlessTextureIndex;
  57. gpuDecal.m_diffuseBlendFactor = m_layers[LayerType::kDiffuse].m_blendFactor;
  58. gpuDecal.m_roughnessMetalnessFactor = m_layers[LayerType::kRoughnessMetalness].m_blendFactor;
  59. gpuDecal.m_textureMatrix = m_biasProjViewMat;
  60. gpuDecal.m_sphereCenter = obbW.getCenter().xyz();
  61. gpuDecal.m_sphereRadius = obbW.getExtend().getLength();
  62. m_gpuSceneDecal.uploadToGpuScene(gpuDecal);
  63. }
  64. return Error::kNone;
  65. }
  66. } // end namespace anki