BsSpriteMaterial.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "BsMaterialParam.h"
  6. #include "BsVector2I.h"
  7. #include "BsColor.h"
  8. namespace BansheeEngine
  9. {
  10. /** @addtogroup 2D-Internal
  11. * @{
  12. */
  13. /** Extension structure that can be used by SpriteMaterial%s to access specialized data. */
  14. struct SpriteMaterialExtraInfo
  15. {
  16. virtual ~SpriteMaterialExtraInfo() { }
  17. /** Creates a new deep copy of the object. */
  18. virtual SPtr<SpriteMaterialExtraInfo> clone() const
  19. {
  20. return bs_shared_ptr_new<SpriteMaterialExtraInfo>();
  21. }
  22. };
  23. /** Contains information for initializing a sprite material. */
  24. struct SpriteMaterialInfo
  25. {
  26. SpriteMaterialInfo()
  27. :groupId(0)
  28. { }
  29. /**
  30. * Creates a new deep copy of the object. This is different from standard copy constructor which will just reference
  31. * the original "additionalData" field, while this will copy it.
  32. */
  33. SpriteMaterialInfo clone() const
  34. {
  35. SpriteMaterialInfo info;
  36. info.groupId = groupId;
  37. info.texture = texture;
  38. info.tint = tint;
  39. if(additionalData != nullptr)
  40. info.additionalData = additionalData->clone();
  41. return info;
  42. }
  43. UINT64 groupId;
  44. HTexture texture;
  45. Color tint;
  46. SPtr<SpriteMaterialExtraInfo> additionalData;
  47. };
  48. /** Interfaced implemented by materials used for rendering sprites. This is expected to be used as a singleton. */
  49. class BS_EXPORT SpriteMaterial
  50. {
  51. public:
  52. SpriteMaterial(UINT32 id, const HMaterial& material);
  53. virtual ~SpriteMaterial();
  54. /** Returns the unique ID of the sprite material. */
  55. UINT32 getId() const { return mId; };
  56. /**
  57. * Generates a hash value that describes the contents of the sprite material info structure. Returned hash doesn't
  58. * guarantee that the two objects with the same hash are idential, but rather that the objects are mergeable via
  59. * merge().
  60. */
  61. virtual UINT64 getMergeHash(const SpriteMaterialInfo& info) const;
  62. /**
  63. * Merges two SpriteMaterialInfo%s into one structure. User must guarantee that the two objects are mergeable
  64. * by ensuring their merge hashes match (by calling getMergeHash()).
  65. *
  66. * @param[in, out] mergeInto Object that contains the first part of the data, and will contain the result of the
  67. * merge.
  68. * @param[in] mergeFrom Object that contains the second part of the data to merge, which will be merged into
  69. * the first object.
  70. */
  71. virtual void merge(SpriteMaterialInfo& mergeInto, const SpriteMaterialInfo& mergeFrom) const { }
  72. /**
  73. * Renders the provided mesh using the current material.
  74. *
  75. * @param[in] mesh Mesh to render, containing vertices in screen space.
  76. * @param[in] texture Optional texture to render the mesh with.
  77. * @param[in] sampler Optional sampler to render the texture with.
  78. * @param[in] tint Color tint to apply to the rendered mesh.
  79. * @param[in] worldTransform World transform to apply to the rendered mesh.
  80. * @param[in] invViewportSize Inverse size of the viewport the mesh will be rendered to.
  81. * @param[in] additionalData Optional additional data that might be required by the renderer.
  82. */
  83. virtual void render(const SPtr<MeshCoreBase>& mesh, const SPtr<TextureCore>& texture,
  84. const SPtr<SamplerStateCore>& sampler, const Color& tint, const Matrix4& worldTransform,
  85. const Vector2& invViewportSize, const SPtr<SpriteMaterialExtraInfo>& additionalData) const;
  86. protected:
  87. /** Perform initialization of core-thread specific objects. */
  88. virtual void initialize();
  89. /** Destroys the core thread material. */
  90. static void destroy(const SPtr<MaterialCore>& material, const SPtr<GpuParamsSetCore>& params);
  91. UINT32 mId;
  92. // Core thread only (everything below)
  93. SPtr<MaterialCore> mMaterial;
  94. SPtr<GpuParamsSetCore> mParams;
  95. mutable MaterialParamMat4Core mWorldTransformParam;
  96. mutable MaterialParamFloatCore mInvViewportWidthParam;
  97. mutable MaterialParamFloatCore mInvViewportHeightParam;
  98. mutable MaterialParamColorCore mTintParam;
  99. mutable MaterialParamTextureCore mTextureParam;
  100. mutable MaterialParamSampStateCore mSamplerParam;
  101. };
  102. /** @} */
  103. }