| 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 |  | 
|---|
| 31 | #ifndef GLTF_TEXTURE_SAMPLER_H | 
|---|
| 32 | #define GLTF_TEXTURE_SAMPLER_H | 
|---|
| 33 |  | 
|---|
| 34 | #include "scene/resources/material.h" | 
|---|
| 35 |  | 
|---|
| 36 | class GLTFTextureSampler : public Resource { | 
|---|
| 37 | GDCLASS(GLTFTextureSampler, Resource); | 
|---|
| 38 |  | 
|---|
| 39 | public: | 
|---|
| 40 | enum FilterMode { | 
|---|
| 41 | NEAREST = 9728, | 
|---|
| 42 | LINEAR = 9729, | 
|---|
| 43 | NEAREST_MIPMAP_NEAREST = 9984, | 
|---|
| 44 | LINEAR_MIPMAP_NEAREST = 9985, | 
|---|
| 45 | NEAREST_MIPMAP_LINEAR = 9986, | 
|---|
| 46 | LINEAR_MIPMAP_LINEAR = 9987 | 
|---|
| 47 | }; | 
|---|
| 48 |  | 
|---|
| 49 | enum WrapMode { | 
|---|
| 50 | CLAMP_TO_EDGE = 33071, | 
|---|
| 51 | MIRRORED_REPEAT = 33648, | 
|---|
| 52 | REPEAT = 10497, | 
|---|
| 53 | DEFAULT = REPEAT | 
|---|
| 54 | }; | 
|---|
| 55 |  | 
|---|
| 56 | int get_mag_filter() const { | 
|---|
| 57 | return mag_filter; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | void set_mag_filter(const int filter_mode) { | 
|---|
| 61 | mag_filter = (FilterMode)filter_mode; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | int get_min_filter() const { | 
|---|
| 65 | return min_filter; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | void set_min_filter(const int filter_mode) { | 
|---|
| 69 | min_filter = (FilterMode)filter_mode; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | int get_wrap_s() const { | 
|---|
| 73 | return wrap_s; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | void set_wrap_s(const int wrap_mode) { | 
|---|
| 77 | wrap_s = (WrapMode)wrap_mode; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | int get_wrap_t() const { | 
|---|
| 81 | return wrap_t; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | void set_wrap_t(const int wrap_mode) { | 
|---|
| 85 | wrap_s = (WrapMode)wrap_mode; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | StandardMaterial3D::TextureFilter get_filter_mode() const { | 
|---|
| 89 | using TextureFilter = StandardMaterial3D::TextureFilter; | 
|---|
| 90 |  | 
|---|
| 91 | switch (min_filter) { | 
|---|
| 92 | case NEAREST: | 
|---|
| 93 | return TextureFilter::TEXTURE_FILTER_NEAREST; | 
|---|
| 94 | case LINEAR: | 
|---|
| 95 | return TextureFilter::TEXTURE_FILTER_LINEAR; | 
|---|
| 96 | case NEAREST_MIPMAP_NEAREST: | 
|---|
| 97 | case NEAREST_MIPMAP_LINEAR: | 
|---|
| 98 | return TextureFilter::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS; | 
|---|
| 99 | case LINEAR_MIPMAP_NEAREST: | 
|---|
| 100 | case LINEAR_MIPMAP_LINEAR: | 
|---|
| 101 | default: | 
|---|
| 102 | return TextureFilter::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS; | 
|---|
| 103 | } | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void set_filter_mode(StandardMaterial3D::TextureFilter mode) { | 
|---|
| 107 | using TextureFilter = StandardMaterial3D::TextureFilter; | 
|---|
| 108 |  | 
|---|
| 109 | switch (mode) { | 
|---|
| 110 | case TextureFilter::TEXTURE_FILTER_NEAREST: | 
|---|
| 111 | min_filter = FilterMode::NEAREST; | 
|---|
| 112 | mag_filter = FilterMode::NEAREST; | 
|---|
| 113 | break; | 
|---|
| 114 | case TextureFilter::TEXTURE_FILTER_LINEAR: | 
|---|
| 115 | min_filter = FilterMode::LINEAR; | 
|---|
| 116 | mag_filter = FilterMode::LINEAR; | 
|---|
| 117 | break; | 
|---|
| 118 | case TextureFilter::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: | 
|---|
| 119 | case TextureFilter::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: | 
|---|
| 120 | min_filter = FilterMode::NEAREST_MIPMAP_LINEAR; | 
|---|
| 121 | mag_filter = FilterMode::NEAREST; | 
|---|
| 122 | break; | 
|---|
| 123 | case TextureFilter::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: | 
|---|
| 124 | case TextureFilter::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: | 
|---|
| 125 | default: | 
|---|
| 126 | min_filter = FilterMode::LINEAR_MIPMAP_LINEAR; | 
|---|
| 127 | mag_filter = FilterMode::LINEAR; | 
|---|
| 128 | break; | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | bool get_wrap_mode() const { | 
|---|
| 133 | // BaseMaterial3D presents wrapping as a boolean property. Either the texture is repeated | 
|---|
| 134 | // in both dimensions, non-mirrored, or it isn't repeated at all. This will cause oddities | 
|---|
| 135 | // when people import models having other wrapping mode combinations. | 
|---|
| 136 | return (wrap_s == WrapMode::REPEAT) && (wrap_t == WrapMode::REPEAT); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | void set_wrap_mode(bool mat_repeats) { | 
|---|
| 140 | if (mat_repeats) { | 
|---|
| 141 | wrap_s = WrapMode::REPEAT; | 
|---|
| 142 | wrap_t = WrapMode::REPEAT; | 
|---|
| 143 | } else { | 
|---|
| 144 | wrap_s = WrapMode::CLAMP_TO_EDGE; | 
|---|
| 145 | wrap_t = WrapMode::CLAMP_TO_EDGE; | 
|---|
| 146 | } | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | protected: | 
|---|
| 150 | static void _bind_methods(); | 
|---|
| 151 |  | 
|---|
| 152 | private: | 
|---|
| 153 | FilterMode mag_filter = FilterMode::LINEAR; | 
|---|
| 154 | FilterMode min_filter = FilterMode::LINEAR_MIPMAP_LINEAR; | 
|---|
| 155 | WrapMode wrap_s = WrapMode::REPEAT; | 
|---|
| 156 | WrapMode wrap_t = WrapMode::REPEAT; | 
|---|
| 157 | }; | 
|---|
| 158 |  | 
|---|
| 159 | VARIANT_ENUM_CAST(GLTFTextureSampler::FilterMode); | 
|---|
| 160 | VARIANT_ENUM_CAST(GLTFTextureSampler::WrapMode); | 
|---|
| 161 |  | 
|---|
| 162 | #endif // GLTF_TEXTURE_SAMPLER_H | 
|---|
| 163 |  | 
|---|