2
0

DecalComponent.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/GpuMemory/GpuSceneBuffer.h>
  10. namespace anki {
  11. DecalComponent::DecalComponent(SceneNode* node)
  12. : SceneComponent(node, kClassType)
  13. {
  14. m_gpuSceneDecal.allocate();
  15. loadDiffuseImageResource("EngineAssets/DefaultDecal.png", 0.9f);
  16. }
  17. DecalComponent::~DecalComponent()
  18. {
  19. }
  20. void DecalComponent::setLayer(CString fname, F32 blendFactor, LayerType type)
  21. {
  22. Layer& l = m_layers[type];
  23. ImageResourcePtr rsrc;
  24. if(ResourceManager::getSingleton().loadResource(fname, rsrc))
  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 = clamp(blendFactor, 0.0f, 1.0f);
  33. }
  34. Error DecalComponent::update(SceneComponentUpdateInfo& info, Bool& updated)
  35. {
  36. updated = m_dirty || info.m_node->movedThisFrame();
  37. if(!updated) [[likely]]
  38. {
  39. return Error::kNone;
  40. }
  41. m_dirty = false;
  42. const Vec3 halfBoxSize = info.m_node->getWorldTransform().getScale().xyz();
  43. // Calculate the texture matrix
  44. Transform trf = info.m_node->getWorldTransform();
  45. trf.setScale(Vec3(1.0f));
  46. const Mat4 viewMat = Mat4(trf).invert();
  47. const Mat4 projMat = Mat4::calculateOrthographicProjectionMatrix(halfBoxSize.x(), -halfBoxSize.x(), halfBoxSize.y(), -halfBoxSize.y(),
  48. -halfBoxSize.z(), halfBoxSize.z());
  49. 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);
  50. const Mat4 biasedProjViewMat = biasMat4 * projMat * viewMat;
  51. // Upload to the GPU scene
  52. GpuSceneDecal gpuDecal;
  53. gpuDecal.m_diffuseTexture = m_layers[LayerType::kDiffuse].m_bindlessTextureIndex;
  54. gpuDecal.m_roughnessMetalnessTexture = m_layers[LayerType::kRoughnessMetalness].m_bindlessTextureIndex;
  55. gpuDecal.m_diffuseBlendFactor = m_layers[LayerType::kDiffuse].m_blendFactor;
  56. gpuDecal.m_roughnessMetalnessFactor = m_layers[LayerType::kRoughnessMetalness].m_blendFactor;
  57. gpuDecal.m_textureMatrix = biasedProjViewMat;
  58. gpuDecal.m_sphereCenter = info.m_node->getWorldTransform().getOrigin().xyz();
  59. gpuDecal.m_sphereRadius = halfBoxSize.length();
  60. m_gpuSceneDecal.uploadToGpuScene(gpuDecal);
  61. return Error::kNone;
  62. }
  63. } // end namespace anki