2
0

gltf_texture_sampler.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "scene/resources/material.h"
  33. class GLTFTextureSampler : public Resource {
  34. GDCLASS(GLTFTextureSampler, Resource);
  35. public:
  36. enum FilterMode {
  37. NEAREST = 9728,
  38. LINEAR = 9729,
  39. NEAREST_MIPMAP_NEAREST = 9984,
  40. LINEAR_MIPMAP_NEAREST = 9985,
  41. NEAREST_MIPMAP_LINEAR = 9986,
  42. LINEAR_MIPMAP_LINEAR = 9987
  43. };
  44. enum WrapMode {
  45. CLAMP_TO_EDGE = 33071,
  46. MIRRORED_REPEAT = 33648,
  47. REPEAT = 10497,
  48. DEFAULT = REPEAT
  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. StandardMaterial3D::TextureFilter get_filter_mode() const {
  75. using TextureFilter = StandardMaterial3D::TextureFilter;
  76. switch (min_filter) {
  77. case NEAREST:
  78. return TextureFilter::TEXTURE_FILTER_NEAREST;
  79. case LINEAR:
  80. return TextureFilter::TEXTURE_FILTER_LINEAR;
  81. case NEAREST_MIPMAP_NEAREST:
  82. case NEAREST_MIPMAP_LINEAR:
  83. return TextureFilter::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
  84. case LINEAR_MIPMAP_NEAREST:
  85. case LINEAR_MIPMAP_LINEAR:
  86. default:
  87. return TextureFilter::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
  88. }
  89. }
  90. void set_filter_mode(StandardMaterial3D::TextureFilter mode) {
  91. using TextureFilter = StandardMaterial3D::TextureFilter;
  92. switch (mode) {
  93. case TextureFilter::TEXTURE_FILTER_NEAREST:
  94. min_filter = FilterMode::NEAREST;
  95. mag_filter = FilterMode::NEAREST;
  96. break;
  97. case TextureFilter::TEXTURE_FILTER_LINEAR:
  98. min_filter = FilterMode::LINEAR;
  99. mag_filter = FilterMode::LINEAR;
  100. break;
  101. case TextureFilter::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
  102. case TextureFilter::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC:
  103. min_filter = FilterMode::NEAREST_MIPMAP_LINEAR;
  104. mag_filter = FilterMode::NEAREST;
  105. break;
  106. case TextureFilter::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
  107. case TextureFilter::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC:
  108. default:
  109. min_filter = FilterMode::LINEAR_MIPMAP_LINEAR;
  110. mag_filter = FilterMode::LINEAR;
  111. break;
  112. }
  113. }
  114. bool get_wrap_mode() const {
  115. // BaseMaterial3D presents wrapping as a boolean property. Either the texture is repeated
  116. // in both dimensions, non-mirrored, or it isn't repeated at all. This will cause oddities
  117. // when people import models having other wrapping mode combinations.
  118. return (wrap_s == WrapMode::REPEAT) && (wrap_t == WrapMode::REPEAT);
  119. }
  120. void set_wrap_mode(bool mat_repeats) {
  121. if (mat_repeats) {
  122. wrap_s = WrapMode::REPEAT;
  123. wrap_t = WrapMode::REPEAT;
  124. } else {
  125. wrap_s = WrapMode::CLAMP_TO_EDGE;
  126. wrap_t = WrapMode::CLAMP_TO_EDGE;
  127. }
  128. }
  129. protected:
  130. static void _bind_methods();
  131. private:
  132. FilterMode mag_filter = FilterMode::LINEAR;
  133. FilterMode min_filter = FilterMode::LINEAR_MIPMAP_LINEAR;
  134. WrapMode wrap_s = WrapMode::REPEAT;
  135. WrapMode wrap_t = WrapMode::REPEAT;
  136. };
  137. VARIANT_ENUM_CAST(GLTFTextureSampler::FilterMode);
  138. VARIANT_ENUM_CAST(GLTFTextureSampler::WrapMode);
  139. #endif // GLTF_TEXTURE_SAMPLER_H