gltf_texture_sampler.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**************************************************************************/
  2. /* gltf_texture_sampler.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef GLTF_TEXTURE_SAMPLER_H
  31. #define GLTF_TEXTURE_SAMPLER_H
  32. #include "core/resource.h"
  33. #include "scene/resources/texture.h"
  34. class GLTFTextureSampler : public Resource {
  35. GDCLASS(GLTFTextureSampler, Resource);
  36. public:
  37. enum FilterMode {
  38. NEAREST = 9728,
  39. LINEAR = 9729,
  40. NEAREST_MIPMAP_NEAREST = 9984,
  41. LINEAR_MIPMAP_NEAREST = 9985,
  42. NEAREST_MIPMAP_LINEAR = 9986,
  43. LINEAR_MIPMAP_LINEAR = 9987
  44. };
  45. enum WrapMode {
  46. CLAMP_TO_EDGE = 33071,
  47. MIRRORED_REPEAT = 33648,
  48. REPEAT = 10497
  49. };
  50. int get_mag_filter() const {
  51. return mag_filter;
  52. }
  53. void set_mag_filter(const int filter_mode) {
  54. mag_filter = (FilterMode)filter_mode;
  55. }
  56. int get_min_filter() const {
  57. return min_filter;
  58. }
  59. void set_min_filter(const int filter_mode) {
  60. min_filter = (FilterMode)filter_mode;
  61. }
  62. int get_wrap_s() const {
  63. return wrap_s;
  64. }
  65. void set_wrap_s(const int wrap_mode) {
  66. wrap_s = (WrapMode)wrap_mode;
  67. }
  68. int get_wrap_t() const {
  69. return wrap_t;
  70. }
  71. void set_wrap_t(const int wrap_mode) {
  72. wrap_s = (WrapMode)wrap_mode;
  73. }
  74. Texture::Flags get_filter_mode() const {
  75. switch (min_filter) {
  76. case NEAREST:
  77. return (Texture::Flags)0;
  78. case LINEAR:
  79. return Texture::Flags::FLAG_FILTER;
  80. case NEAREST_MIPMAP_NEAREST:
  81. case NEAREST_MIPMAP_LINEAR:
  82. return Texture::Flags::FLAG_MIPMAPS;
  83. case LINEAR_MIPMAP_NEAREST:
  84. case LINEAR_MIPMAP_LINEAR:
  85. default:
  86. return (Texture::Flags)(Texture::Flags::FLAG_FILTER | Texture::Flags::FLAG_MIPMAPS);
  87. }
  88. }
  89. void set_filter_mode(uint32_t flags) {
  90. const bool filter = (flags & Texture::Flags::FLAG_FILTER);
  91. const bool mipmaps = (flags & Texture::Flags::FLAG_MIPMAPS);
  92. if (filter && mipmaps) {
  93. min_filter = FilterMode::LINEAR_MIPMAP_LINEAR;
  94. mag_filter = FilterMode::LINEAR;
  95. } else if (filter) {
  96. min_filter = FilterMode::LINEAR;
  97. mag_filter = FilterMode::LINEAR;
  98. } else if (mipmaps) {
  99. min_filter = FilterMode::NEAREST_MIPMAP_LINEAR;
  100. mag_filter = FilterMode::NEAREST;
  101. } else {
  102. min_filter = FilterMode::NEAREST;
  103. mag_filter = FilterMode::NEAREST;
  104. }
  105. }
  106. Texture::Flags get_wrap_mode() const {
  107. if ((wrap_s == WrapMode::MIRRORED_REPEAT) && (wrap_t == WrapMode::MIRRORED_REPEAT)) {
  108. return Texture::Flags::FLAG_MIRRORED_REPEAT;
  109. } else if ((wrap_s == WrapMode::REPEAT) && (wrap_t == WrapMode::REPEAT)) {
  110. return Texture::Flags::FLAG_REPEAT;
  111. } else {
  112. return (Texture::Flags)0;
  113. }
  114. }
  115. void set_wrap_mode(uint32_t flags) {
  116. if (flags & Texture::Flags::FLAG_MIRRORED_REPEAT) {
  117. wrap_s = WrapMode::MIRRORED_REPEAT;
  118. wrap_t = WrapMode::MIRRORED_REPEAT;
  119. } else if (flags & Texture::Flags::FLAG_REPEAT) {
  120. wrap_s = WrapMode::REPEAT;
  121. wrap_t = WrapMode::REPEAT;
  122. } else {
  123. wrap_s = WrapMode::CLAMP_TO_EDGE;
  124. wrap_t = WrapMode::CLAMP_TO_EDGE;
  125. }
  126. }
  127. Texture::Flags get_texture_flags() const {
  128. return (Texture::Flags)(get_filter_mode() | get_wrap_mode());
  129. }
  130. void set_texture_flags(uint32_t flags) {
  131. set_filter_mode(flags);
  132. set_wrap_mode(flags);
  133. }
  134. protected:
  135. static void _bind_methods();
  136. private:
  137. FilterMode mag_filter = FilterMode::LINEAR;
  138. FilterMode min_filter = FilterMode::LINEAR_MIPMAP_LINEAR;
  139. WrapMode wrap_s = WrapMode::REPEAT;
  140. WrapMode wrap_t = WrapMode::REPEAT;
  141. };
  142. #endif // GLTF_TEXTURE_SAMPLER_H