gltf_accessor.cpp 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. /**************************************************************************/
  2. /* gltf_accessor.cpp */
  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. #include "gltf_accessor.h"
  31. #include "gltf_accessor.compat.inc"
  32. #include "../gltf_state.h"
  33. void GLTFAccessor::_bind_methods() {
  34. BIND_ENUM_CONSTANT(TYPE_SCALAR);
  35. BIND_ENUM_CONSTANT(TYPE_VEC2);
  36. BIND_ENUM_CONSTANT(TYPE_VEC3);
  37. BIND_ENUM_CONSTANT(TYPE_VEC4);
  38. BIND_ENUM_CONSTANT(TYPE_MAT2);
  39. BIND_ENUM_CONSTANT(TYPE_MAT3);
  40. BIND_ENUM_CONSTANT(TYPE_MAT4);
  41. BIND_ENUM_CONSTANT(COMPONENT_TYPE_NONE);
  42. BIND_ENUM_CONSTANT(COMPONENT_TYPE_SIGNED_BYTE);
  43. BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_BYTE);
  44. BIND_ENUM_CONSTANT(COMPONENT_TYPE_SIGNED_SHORT);
  45. BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_SHORT);
  46. BIND_ENUM_CONSTANT(COMPONENT_TYPE_SIGNED_INT);
  47. BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_INT);
  48. BIND_ENUM_CONSTANT(COMPONENT_TYPE_SINGLE_FLOAT);
  49. BIND_ENUM_CONSTANT(COMPONENT_TYPE_DOUBLE_FLOAT);
  50. BIND_ENUM_CONSTANT(COMPONENT_TYPE_HALF_FLOAT);
  51. BIND_ENUM_CONSTANT(COMPONENT_TYPE_SIGNED_LONG);
  52. BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_LONG);
  53. ClassDB::bind_static_method("GLTFAccessor", D_METHOD("from_dictionary", "dictionary"), &GLTFAccessor::from_dictionary);
  54. ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFAccessor::to_dictionary);
  55. ClassDB::bind_method(D_METHOD("get_buffer_view"), &GLTFAccessor::get_buffer_view);
  56. ClassDB::bind_method(D_METHOD("set_buffer_view", "buffer_view"), &GLTFAccessor::set_buffer_view);
  57. ClassDB::bind_method(D_METHOD("get_byte_offset"), &GLTFAccessor::get_byte_offset);
  58. ClassDB::bind_method(D_METHOD("set_byte_offset", "byte_offset"), &GLTFAccessor::set_byte_offset);
  59. ClassDB::bind_method(D_METHOD("get_component_type"), &GLTFAccessor::get_component_type);
  60. ClassDB::bind_method(D_METHOD("set_component_type", "component_type"), &GLTFAccessor::set_component_type);
  61. ClassDB::bind_method(D_METHOD("get_normalized"), &GLTFAccessor::get_normalized);
  62. ClassDB::bind_method(D_METHOD("set_normalized", "normalized"), &GLTFAccessor::set_normalized);
  63. ClassDB::bind_method(D_METHOD("get_count"), &GLTFAccessor::get_count);
  64. ClassDB::bind_method(D_METHOD("set_count", "count"), &GLTFAccessor::set_count);
  65. ClassDB::bind_method(D_METHOD("get_accessor_type"), &GLTFAccessor::get_accessor_type);
  66. ClassDB::bind_method(D_METHOD("set_accessor_type", "accessor_type"), &GLTFAccessor::set_accessor_type);
  67. ClassDB::bind_method(D_METHOD("get_type"), &GLTFAccessor::get_type);
  68. ClassDB::bind_method(D_METHOD("set_type", "type"), &GLTFAccessor::set_type);
  69. ClassDB::bind_method(D_METHOD("get_min"), &GLTFAccessor::get_min);
  70. ClassDB::bind_method(D_METHOD("set_min", "min"), &GLTFAccessor::set_min);
  71. ClassDB::bind_method(D_METHOD("get_max"), &GLTFAccessor::get_max);
  72. ClassDB::bind_method(D_METHOD("set_max", "max"), &GLTFAccessor::set_max);
  73. ClassDB::bind_method(D_METHOD("get_sparse_count"), &GLTFAccessor::get_sparse_count);
  74. ClassDB::bind_method(D_METHOD("set_sparse_count", "sparse_count"), &GLTFAccessor::set_sparse_count);
  75. ClassDB::bind_method(D_METHOD("get_sparse_indices_buffer_view"), &GLTFAccessor::get_sparse_indices_buffer_view);
  76. ClassDB::bind_method(D_METHOD("set_sparse_indices_buffer_view", "sparse_indices_buffer_view"), &GLTFAccessor::set_sparse_indices_buffer_view);
  77. ClassDB::bind_method(D_METHOD("get_sparse_indices_byte_offset"), &GLTFAccessor::get_sparse_indices_byte_offset);
  78. ClassDB::bind_method(D_METHOD("set_sparse_indices_byte_offset", "sparse_indices_byte_offset"), &GLTFAccessor::set_sparse_indices_byte_offset);
  79. ClassDB::bind_method(D_METHOD("get_sparse_indices_component_type"), &GLTFAccessor::get_sparse_indices_component_type);
  80. ClassDB::bind_method(D_METHOD("set_sparse_indices_component_type", "sparse_indices_component_type"), &GLTFAccessor::set_sparse_indices_component_type);
  81. ClassDB::bind_method(D_METHOD("get_sparse_values_buffer_view"), &GLTFAccessor::get_sparse_values_buffer_view);
  82. ClassDB::bind_method(D_METHOD("set_sparse_values_buffer_view", "sparse_values_buffer_view"), &GLTFAccessor::set_sparse_values_buffer_view);
  83. ClassDB::bind_method(D_METHOD("get_sparse_values_byte_offset"), &GLTFAccessor::get_sparse_values_byte_offset);
  84. ClassDB::bind_method(D_METHOD("set_sparse_values_byte_offset", "sparse_values_byte_offset"), &GLTFAccessor::set_sparse_values_byte_offset);
  85. ADD_PROPERTY(PropertyInfo(Variant::INT, "buffer_view"), "set_buffer_view", "get_buffer_view"); // GLTFBufferViewIndex
  86. ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_offset"), "set_byte_offset", "get_byte_offset"); // int
  87. ADD_PROPERTY(PropertyInfo(Variant::INT, "component_type"), "set_component_type", "get_component_type"); // int
  88. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalized"), "set_normalized", "get_normalized"); // bool
  89. ADD_PROPERTY(PropertyInfo(Variant::INT, "count"), "set_count", "get_count"); // int
  90. ADD_PROPERTY(PropertyInfo(Variant::INT, "accessor_type"), "set_accessor_type", "get_accessor_type"); // GLTFAccessor::GLTFAccessorType
  91. ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_type", "get_type"); // Deprecated, int for GLTFAccessor::GLTFAccessorType
  92. ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT64_ARRAY, "min"), "set_min", "get_min"); // Vector<real_t>
  93. ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT64_ARRAY, "max"), "set_max", "get_max"); // Vector<real_t>
  94. ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_count"), "set_sparse_count", "get_sparse_count"); // int
  95. ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_indices_buffer_view"), "set_sparse_indices_buffer_view", "get_sparse_indices_buffer_view"); // int
  96. ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_indices_byte_offset"), "set_sparse_indices_byte_offset", "get_sparse_indices_byte_offset"); // int
  97. ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_indices_component_type"), "set_sparse_indices_component_type", "get_sparse_indices_component_type"); // int
  98. ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_values_buffer_view"), "set_sparse_values_buffer_view", "get_sparse_values_buffer_view"); // int
  99. ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_values_byte_offset"), "set_sparse_values_byte_offset", "get_sparse_values_byte_offset"); // int
  100. }
  101. // Property getters and setters.
  102. GLTFBufferViewIndex GLTFAccessor::get_buffer_view() const {
  103. return buffer_view;
  104. }
  105. void GLTFAccessor::set_buffer_view(GLTFBufferViewIndex p_buffer_view) {
  106. buffer_view = p_buffer_view;
  107. }
  108. int64_t GLTFAccessor::get_byte_offset() const {
  109. return byte_offset;
  110. }
  111. void GLTFAccessor::set_byte_offset(int64_t p_byte_offset) {
  112. byte_offset = p_byte_offset;
  113. }
  114. GLTFAccessor::GLTFComponentType GLTFAccessor::get_component_type() const {
  115. return component_type;
  116. }
  117. void GLTFAccessor::set_component_type(GLTFComponentType p_component_type) {
  118. component_type = (GLTFComponentType)p_component_type;
  119. }
  120. bool GLTFAccessor::get_normalized() const {
  121. return normalized;
  122. }
  123. void GLTFAccessor::set_normalized(bool p_normalized) {
  124. normalized = p_normalized;
  125. }
  126. int64_t GLTFAccessor::get_count() const {
  127. return count;
  128. }
  129. void GLTFAccessor::set_count(int64_t p_count) {
  130. count = p_count;
  131. }
  132. GLTFAccessor::GLTFAccessorType GLTFAccessor::get_accessor_type() const {
  133. return accessor_type;
  134. }
  135. void GLTFAccessor::set_accessor_type(GLTFAccessorType p_accessor_type) {
  136. accessor_type = p_accessor_type;
  137. }
  138. int GLTFAccessor::get_type() const {
  139. return (int)accessor_type;
  140. }
  141. void GLTFAccessor::set_type(int p_accessor_type) {
  142. accessor_type = (GLTFAccessorType)p_accessor_type; // TODO: Register enum
  143. }
  144. Vector<double> GLTFAccessor::get_min() const {
  145. return Vector<double>(min);
  146. }
  147. void GLTFAccessor::set_min(const Vector<double> &p_min) {
  148. min = Vector<double>(p_min);
  149. }
  150. Vector<double> GLTFAccessor::get_max() const {
  151. return Vector<double>(max);
  152. }
  153. void GLTFAccessor::set_max(const Vector<double> &p_max) {
  154. max = Vector<double>(p_max);
  155. }
  156. int64_t GLTFAccessor::get_sparse_count() const {
  157. return sparse_count;
  158. }
  159. void GLTFAccessor::set_sparse_count(int64_t p_sparse_count) {
  160. sparse_count = p_sparse_count;
  161. }
  162. GLTFBufferViewIndex GLTFAccessor::get_sparse_indices_buffer_view() const {
  163. return sparse_indices_buffer_view;
  164. }
  165. void GLTFAccessor::set_sparse_indices_buffer_view(GLTFBufferViewIndex p_sparse_indices_buffer_view) {
  166. sparse_indices_buffer_view = p_sparse_indices_buffer_view;
  167. }
  168. int64_t GLTFAccessor::get_sparse_indices_byte_offset() const {
  169. return sparse_indices_byte_offset;
  170. }
  171. void GLTFAccessor::set_sparse_indices_byte_offset(int64_t p_sparse_indices_byte_offset) {
  172. sparse_indices_byte_offset = p_sparse_indices_byte_offset;
  173. }
  174. GLTFAccessor::GLTFComponentType GLTFAccessor::get_sparse_indices_component_type() const {
  175. return sparse_indices_component_type;
  176. }
  177. void GLTFAccessor::set_sparse_indices_component_type(GLTFComponentType p_sparse_indices_component_type) {
  178. sparse_indices_component_type = (GLTFComponentType)p_sparse_indices_component_type;
  179. }
  180. GLTFBufferViewIndex GLTFAccessor::get_sparse_values_buffer_view() const {
  181. return sparse_values_buffer_view;
  182. }
  183. void GLTFAccessor::set_sparse_values_buffer_view(GLTFBufferViewIndex p_sparse_values_buffer_view) {
  184. sparse_values_buffer_view = p_sparse_values_buffer_view;
  185. }
  186. int64_t GLTFAccessor::get_sparse_values_byte_offset() const {
  187. return sparse_values_byte_offset;
  188. }
  189. void GLTFAccessor::set_sparse_values_byte_offset(int64_t p_sparse_values_byte_offset) {
  190. sparse_values_byte_offset = p_sparse_values_byte_offset;
  191. }
  192. // Trivial helper functions.
  193. void GLTFAccessor::_calculate_min_and_max(const PackedFloat64Array &p_numbers) {
  194. const int64_t vector_size = _get_vector_size();
  195. ERR_FAIL_COND(vector_size <= 0 || p_numbers.size() % vector_size != 0);
  196. min.resize(vector_size);
  197. max.resize(vector_size);
  198. // Initialize min and max with the first vector element values.
  199. for (int64_t in_vec = 0; in_vec < vector_size; in_vec++) {
  200. min.write[in_vec] = p_numbers[in_vec];
  201. max.write[in_vec] = p_numbers[in_vec];
  202. }
  203. // Iterate over the rest of the vectors.
  204. for (int64_t which_vec = vector_size; which_vec < p_numbers.size(); which_vec += vector_size) {
  205. for (int64_t in_vec = 0; in_vec < vector_size; in_vec++) {
  206. min.write[in_vec] = MIN(p_numbers[which_vec + in_vec], min[in_vec]);
  207. max.write[in_vec] = MAX(p_numbers[which_vec + in_vec], max[in_vec]);
  208. }
  209. }
  210. // 3.6.2.5: For floating-point components, JSON-stored minimum and maximum values represent single precision
  211. // floats and SHOULD be rounded to single precision before usage to avoid any potential boundary mismatches.
  212. // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#accessors-bounds
  213. if (component_type == GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT) {
  214. for (int64_t i = 0; i < min.size(); i++) {
  215. min.write[i] = (double)(float)min[i];
  216. max.write[i] = (double)(float)max[i];
  217. }
  218. }
  219. }
  220. void GLTFAccessor::_determine_pad_skip(int64_t &r_skip_every, int64_t &r_skip_bytes) const {
  221. // 3.6.2.4. Accessors of matrix type have data stored in column-major order. The start of each column MUST be aligned to 4-byte boundaries.
  222. // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#data-alignment
  223. switch (component_type) {
  224. case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE:
  225. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {
  226. if (accessor_type == GLTFAccessor::TYPE_MAT2) {
  227. r_skip_every = 2;
  228. r_skip_bytes = 2;
  229. }
  230. if (accessor_type == GLTFAccessor::TYPE_MAT3) {
  231. r_skip_every = 3;
  232. r_skip_bytes = 1;
  233. }
  234. } break;
  235. case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:
  236. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {
  237. if (accessor_type == GLTFAccessor::TYPE_MAT3) {
  238. r_skip_every = 6;
  239. r_skip_bytes = 2;
  240. }
  241. } break;
  242. default: {
  243. } break;
  244. }
  245. }
  246. int64_t GLTFAccessor::_determine_padded_byte_count(int64_t p_raw_byte_size) const {
  247. // 3.6.2.4. Accessors of matrix type have data stored in column-major order. The start of each column MUST be aligned to 4-byte boundaries.
  248. // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#data-alignment
  249. switch (component_type) {
  250. case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE:
  251. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {
  252. if (accessor_type == GLTFAccessor::TYPE_MAT2) {
  253. return p_raw_byte_size * 2;
  254. }
  255. if (accessor_type == GLTFAccessor::TYPE_MAT3) {
  256. return p_raw_byte_size * 4 / 3;
  257. }
  258. } break;
  259. case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:
  260. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {
  261. if (accessor_type == GLTFAccessor::TYPE_MAT3) {
  262. return p_raw_byte_size * 4 / 3;
  263. }
  264. } break;
  265. default: {
  266. } break;
  267. }
  268. return p_raw_byte_size;
  269. }
  270. PackedFloat64Array GLTFAccessor::_filter_numbers(const PackedFloat64Array &p_numbers) const {
  271. PackedFloat64Array filtered_numbers = p_numbers;
  272. for (int64_t i = 0; i < p_numbers.size(); i++) {
  273. const double num = p_numbers[i];
  274. if (!Math::is_finite(num)) {
  275. // 3.6.2.2. "Values of NaN, +Infinity, and -Infinity MUST NOT be present."
  276. // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#accessor-data-types
  277. filtered_numbers.set(i, 0.0);
  278. } else if (component_type == GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT) {
  279. filtered_numbers.set(i, (double)(float)num);
  280. }
  281. }
  282. return filtered_numbers;
  283. }
  284. String GLTFAccessor::_get_component_type_name(const GLTFComponentType p_component) {
  285. // These names are only for debugging and printing error messages, glTF uses the numeric values.
  286. switch (p_component) {
  287. case GLTFAccessor::COMPONENT_TYPE_NONE:
  288. return "None";
  289. case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE:
  290. return "Byte";
  291. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE:
  292. return "UByte";
  293. case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:
  294. return "Short";
  295. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT:
  296. return "UShort";
  297. case GLTFAccessor::COMPONENT_TYPE_SIGNED_INT:
  298. return "Int";
  299. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT:
  300. return "UInt";
  301. case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT:
  302. return "Float";
  303. case GLTFAccessor::COMPONENT_TYPE_DOUBLE_FLOAT:
  304. return "Double";
  305. case GLTFAccessor::COMPONENT_TYPE_HALF_FLOAT:
  306. return "Half";
  307. case GLTFAccessor::COMPONENT_TYPE_SIGNED_LONG:
  308. return "Long";
  309. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG:
  310. return "ULong";
  311. }
  312. return "<Error>";
  313. }
  314. GLTFAccessor::GLTFComponentType GLTFAccessor::_get_indices_component_type_for_size(const int64_t p_size) {
  315. ERR_FAIL_COND_V(p_size < 0, GLTFAccessor::COMPONENT_TYPE_NONE);
  316. // 3.7.2.1. indices accessor MUST NOT contain the maximum possible value for the component type used
  317. // (i.e., 255 for unsigned bytes, 65535 for unsigned shorts, 4294967295 for unsigned ints).
  318. // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview
  319. if (unlikely(p_size > 4294967294LL)) {
  320. return GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG;
  321. }
  322. if (p_size > 65534LL) {
  323. return GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT;
  324. }
  325. if (p_size > 254LL) {
  326. return GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT;
  327. }
  328. return GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE;
  329. }
  330. GLTFAccessor::GLTFAccessorType GLTFAccessor::_get_accessor_type_from_str(const String &p_string) {
  331. if (p_string == "SCALAR") {
  332. return GLTFAccessor::TYPE_SCALAR;
  333. }
  334. if (p_string == "VEC2") {
  335. return GLTFAccessor::TYPE_VEC2;
  336. }
  337. if (p_string == "VEC3") {
  338. return GLTFAccessor::TYPE_VEC3;
  339. }
  340. if (p_string == "VEC4") {
  341. return GLTFAccessor::TYPE_VEC4;
  342. }
  343. if (p_string == "MAT2") {
  344. return GLTFAccessor::TYPE_MAT2;
  345. }
  346. if (p_string == "MAT3") {
  347. return GLTFAccessor::TYPE_MAT3;
  348. }
  349. if (p_string == "MAT4") {
  350. return GLTFAccessor::TYPE_MAT4;
  351. }
  352. ERR_FAIL_V(GLTFAccessor::TYPE_SCALAR);
  353. }
  354. String GLTFAccessor::_get_accessor_type_name() const {
  355. switch (accessor_type) {
  356. case GLTFAccessor::TYPE_SCALAR:
  357. return "SCALAR";
  358. case GLTFAccessor::TYPE_VEC2:
  359. return "VEC2";
  360. case GLTFAccessor::TYPE_VEC3:
  361. return "VEC3";
  362. case GLTFAccessor::TYPE_VEC4:
  363. return "VEC4";
  364. case GLTFAccessor::TYPE_MAT2:
  365. return "MAT2";
  366. case GLTFAccessor::TYPE_MAT3:
  367. return "MAT3";
  368. case GLTFAccessor::TYPE_MAT4:
  369. return "MAT4";
  370. default:
  371. break;
  372. }
  373. ERR_FAIL_V("SCALAR");
  374. }
  375. int64_t GLTFAccessor::_get_vector_size() const {
  376. switch (accessor_type) {
  377. case GLTFAccessor::TYPE_SCALAR:
  378. return 1;
  379. case GLTFAccessor::TYPE_VEC2:
  380. return 2;
  381. case GLTFAccessor::TYPE_VEC3:
  382. return 3;
  383. case GLTFAccessor::TYPE_VEC4:
  384. return 4;
  385. case GLTFAccessor::TYPE_MAT2:
  386. return 4;
  387. case GLTFAccessor::TYPE_MAT3:
  388. return 9;
  389. case GLTFAccessor::TYPE_MAT4:
  390. return 16;
  391. default:
  392. break;
  393. }
  394. ERR_FAIL_V(0);
  395. }
  396. int64_t GLTFAccessor::_get_numbers_per_variant_for_gltf(Variant::Type p_variant_type) {
  397. // Note that these numbers are used to determine the size of the glTF accessor appropriate for the type (see `_get_vector_size`).
  398. // Therefore, the only valid values this can return are 1 (SCALAR), 2 (VEC2), 3 (VEC3), 4 (VEC4/MAT2), 9 (MAT3), and 16 (MAT4).
  399. // The value 0 indicates the Variant type can't map to glTF accessors, and INT64_MAX indicates it needs special handling.
  400. switch (p_variant_type) {
  401. case Variant::NIL:
  402. case Variant::STRING:
  403. case Variant::STRING_NAME:
  404. case Variant::NODE_PATH:
  405. case Variant::RID:
  406. case Variant::OBJECT:
  407. case Variant::CALLABLE:
  408. case Variant::SIGNAL:
  409. case Variant::DICTIONARY:
  410. case Variant::ARRAY:
  411. case Variant::PACKED_STRING_ARRAY:
  412. case Variant::PACKED_VECTOR2_ARRAY:
  413. case Variant::PACKED_VECTOR3_ARRAY:
  414. case Variant::PACKED_COLOR_ARRAY:
  415. case Variant::PACKED_VECTOR4_ARRAY:
  416. case Variant::VARIANT_MAX:
  417. return 0; // Not supported.
  418. case Variant::BOOL:
  419. case Variant::INT:
  420. case Variant::FLOAT:
  421. return 1;
  422. case Variant::VECTOR2:
  423. case Variant::VECTOR2I:
  424. return 2;
  425. case Variant::VECTOR3:
  426. case Variant::VECTOR3I:
  427. return 3;
  428. case Variant::RECT2:
  429. case Variant::RECT2I:
  430. case Variant::VECTOR4:
  431. case Variant::VECTOR4I:
  432. case Variant::PLANE:
  433. case Variant::QUATERNION:
  434. case Variant::COLOR:
  435. return 4;
  436. case Variant::TRANSFORM2D:
  437. case Variant::AABB:
  438. case Variant::BASIS:
  439. return 9;
  440. case Variant::TRANSFORM3D:
  441. case Variant::PROJECTION:
  442. return 16;
  443. case Variant::PACKED_BYTE_ARRAY:
  444. case Variant::PACKED_INT32_ARRAY:
  445. case Variant::PACKED_INT64_ARRAY:
  446. case Variant::PACKED_FLOAT32_ARRAY:
  447. case Variant::PACKED_FLOAT64_ARRAY:
  448. return INT64_MAX; // Special, use `_get_vector_size()` only to determine size.
  449. }
  450. return 0;
  451. }
  452. int64_t GLTFAccessor::_get_bytes_per_component(const GLTFComponentType p_component_type) {
  453. switch (p_component_type) {
  454. case GLTFAccessor::COMPONENT_TYPE_NONE:
  455. ERR_FAIL_V(0);
  456. case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE:
  457. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE:
  458. return 1;
  459. case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:
  460. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT:
  461. case GLTFAccessor::COMPONENT_TYPE_HALF_FLOAT:
  462. return 2;
  463. case GLTFAccessor::COMPONENT_TYPE_SIGNED_INT:
  464. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT:
  465. case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT:
  466. return 4;
  467. case GLTFAccessor::COMPONENT_TYPE_DOUBLE_FLOAT:
  468. case GLTFAccessor::COMPONENT_TYPE_SIGNED_LONG:
  469. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG:
  470. return 8;
  471. }
  472. ERR_FAIL_V(0);
  473. }
  474. int64_t GLTFAccessor::_get_bytes_per_vector() const {
  475. const int64_t raw_byte_size = _get_bytes_per_component(component_type) * _get_vector_size();
  476. return _determine_padded_byte_count(raw_byte_size);
  477. }
  478. bool GLTFAccessor::is_equal_exact(const Ref<GLTFAccessor> &p_other) const {
  479. if (p_other.is_null()) {
  480. return false;
  481. }
  482. return (buffer_view == p_other->buffer_view &&
  483. byte_offset == p_other->byte_offset &&
  484. component_type == p_other->component_type &&
  485. normalized == p_other->normalized &&
  486. count == p_other->count &&
  487. accessor_type == p_other->accessor_type &&
  488. min == p_other->min &&
  489. max == p_other->max &&
  490. sparse_count == p_other->sparse_count &&
  491. sparse_indices_buffer_view == p_other->sparse_indices_buffer_view &&
  492. sparse_indices_byte_offset == p_other->sparse_indices_byte_offset &&
  493. sparse_indices_component_type == p_other->sparse_indices_component_type &&
  494. sparse_values_buffer_view == p_other->sparse_values_buffer_view &&
  495. sparse_values_byte_offset == p_other->sparse_values_byte_offset);
  496. }
  497. // Private decode functions.
  498. PackedInt64Array GLTFAccessor::_decode_sparse_indices(const Ref<GLTFState> &p_gltf_state, const Vector<Ref<GLTFBufferView>> &p_buffer_views) const {
  499. const int64_t bytes_per_component = _get_bytes_per_component(sparse_indices_component_type);
  500. PackedInt64Array numbers;
  501. ERR_FAIL_INDEX_V(sparse_indices_buffer_view, p_buffer_views.size(), numbers);
  502. const Ref<GLTFBufferView> actual_buffer_view = p_buffer_views[sparse_indices_buffer_view];
  503. const PackedByteArray raw_bytes = actual_buffer_view->load_buffer_view_data(p_gltf_state);
  504. const int64_t min_raw_byte_size = bytes_per_component * sparse_count + sparse_indices_byte_offset;
  505. ERR_FAIL_COND_V_MSG(raw_bytes.size() < min_raw_byte_size, numbers, "glTF import: Sparse indices buffer view did not have enough bytes to read the expected number of indices. Returning an empty array.");
  506. numbers.resize(sparse_count);
  507. const uint8_t *raw_pointer = raw_bytes.ptr();
  508. int64_t raw_read_offset = sparse_indices_byte_offset;
  509. for (int64_t i = 0; i < sparse_count; i++) {
  510. const uint8_t *raw_source = &raw_pointer[raw_read_offset];
  511. int64_t number = 0;
  512. switch (sparse_indices_component_type) {
  513. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {
  514. number = *(uint8_t *)raw_source;
  515. } break;
  516. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {
  517. number = *(uint16_t *)raw_source;
  518. } break;
  519. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT: {
  520. number = *(uint32_t *)raw_source;
  521. } break;
  522. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG: {
  523. number = *(uint64_t *)raw_source;
  524. } break;
  525. default: {
  526. ERR_FAIL_V_MSG(PackedInt64Array(), "glTF import: Sparse indices must have an unsigned integer component type. Failed to decode, returning an empty array.");
  527. }
  528. }
  529. numbers.set(i, number);
  530. raw_read_offset += bytes_per_component;
  531. }
  532. ERR_FAIL_COND_V_MSG(raw_read_offset != raw_bytes.size(), numbers, "glTF import: Sparse indices buffer view size did not exactly match the expected size.");
  533. return numbers;
  534. }
  535. template <typename T>
  536. Vector<T> GLTFAccessor::_decode_raw_numbers(const Ref<GLTFState> &p_gltf_state, const Vector<Ref<GLTFBufferView>> &p_buffer_views, bool p_sparse_values) const {
  537. const int64_t bytes_per_component = _get_bytes_per_component(component_type);
  538. const int64_t bytes_per_vector = _get_bytes_per_vector();
  539. const int64_t vector_size = _get_vector_size();
  540. int64_t pad_skip_every = 0;
  541. int64_t pad_skip_bytes = 0;
  542. _determine_pad_skip(pad_skip_every, pad_skip_bytes);
  543. int64_t raw_vector_count;
  544. int64_t raw_buffer_view_index;
  545. int64_t raw_read_offset_start;
  546. if (p_sparse_values) {
  547. raw_vector_count = sparse_count;
  548. raw_buffer_view_index = sparse_values_buffer_view;
  549. raw_read_offset_start = sparse_values_byte_offset;
  550. } else {
  551. raw_vector_count = count;
  552. raw_buffer_view_index = buffer_view;
  553. raw_read_offset_start = byte_offset;
  554. }
  555. const int64_t raw_number_count = raw_vector_count * vector_size;
  556. Vector<T> ret_numbers;
  557. if (raw_buffer_view_index == -1) {
  558. ret_numbers.resize(raw_number_count);
  559. // No buffer view, so fill with zeros.
  560. for (int64_t i = 0; i < raw_number_count; i++) {
  561. ret_numbers.set(i, T(0));
  562. }
  563. return ret_numbers;
  564. }
  565. ERR_FAIL_INDEX_V(raw_buffer_view_index, p_buffer_views.size(), ret_numbers);
  566. const Ref<GLTFBufferView> raw_buffer_view = p_buffer_views[raw_buffer_view_index];
  567. if (raw_buffer_view->get_byte_offset() % bytes_per_component != 0) {
  568. WARN_PRINT("glTF import: Buffer view byte offset is not a multiple of accessor component size. This file is invalid per the glTF specification and will not load correctly in some glTF viewers, but Godot will try to load it anyway.");
  569. }
  570. if (byte_offset % bytes_per_component != 0) {
  571. WARN_PRINT("glTF import: Accessor byte offset is not a multiple of accessor component size. This file is invalid per the glTF specification and will not load correctly in some glTF viewers, but Godot will try to load it anyway.");
  572. }
  573. int64_t declared_byte_stride = raw_buffer_view->get_byte_stride();
  574. int64_t actual_byte_stride = bytes_per_vector;
  575. int64_t stride_skip_every = 0;
  576. int64_t stride_skip_bytes = 0;
  577. if (declared_byte_stride != -1) {
  578. ERR_FAIL_COND_V_MSG(declared_byte_stride % 4 != 0, ret_numbers, "glTF import: The declared buffer view byte stride " + itos(declared_byte_stride) + " was not a multiple of 4 as required by glTF. Returning an empty array.");
  579. if (declared_byte_stride > bytes_per_vector) {
  580. actual_byte_stride = declared_byte_stride;
  581. stride_skip_every = vector_size;
  582. stride_skip_bytes = declared_byte_stride - bytes_per_vector;
  583. }
  584. } else if (raw_buffer_view->get_vertex_attributes()) {
  585. print_verbose("WARNING: glTF import: Buffer view byte stride should be declared for vertex attributes. Assuming packed data and reading anyway.");
  586. }
  587. const int64_t min_raw_byte_size = actual_byte_stride * (raw_vector_count - 1) + bytes_per_vector + raw_read_offset_start;
  588. const PackedByteArray raw_bytes = raw_buffer_view->load_buffer_view_data(p_gltf_state);
  589. ERR_FAIL_COND_V_MSG(raw_bytes.size() < min_raw_byte_size, ret_numbers, "glTF import: The buffer view size was smaller than the minimum required size for the accessor. Returning an empty array.");
  590. ret_numbers.resize(raw_number_count);
  591. const uint8_t *raw_pointer = raw_bytes.ptr();
  592. int64_t raw_read_offset = raw_read_offset_start;
  593. for (int64_t i = 0; i < raw_number_count; i++) {
  594. const uint8_t *raw_source = &raw_pointer[raw_read_offset];
  595. T number = 0;
  596. // 3.11. Implementations MUST use following equations to decode real floating-point value f from a normalized integer c and vice-versa.
  597. // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#animations
  598. switch (component_type) {
  599. case GLTFAccessor::COMPONENT_TYPE_NONE: {
  600. ERR_FAIL_V_MSG(Vector<T>(), "glTF import: Failed to decode buffer view, component type not set. Returning an empty array.");
  601. } break;
  602. case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE: {
  603. int8_t prim = *(int8_t *)raw_source;
  604. if (normalized) {
  605. number = T(MAX(double(prim) / 127.0, -1.0));
  606. } else {
  607. number = T(prim);
  608. }
  609. } break;
  610. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {
  611. uint8_t prim = *(uint8_t *)raw_source;
  612. if (normalized) {
  613. number = T((double(prim) / 255.0));
  614. } else {
  615. number = T(prim);
  616. }
  617. } break;
  618. case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT: {
  619. int16_t prim = *(int16_t *)raw_source;
  620. if (normalized) {
  621. number = T(MAX(double(prim) / 32767.0, -1.0));
  622. } else {
  623. number = T(prim);
  624. }
  625. } break;
  626. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {
  627. uint16_t prim = *(uint16_t *)raw_source;
  628. if (normalized) {
  629. number = T(double(prim) / 65535.0);
  630. } else {
  631. number = T(prim);
  632. }
  633. } break;
  634. case GLTFAccessor::COMPONENT_TYPE_SIGNED_INT: {
  635. number = T(*(int32_t *)raw_source);
  636. } break;
  637. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT: {
  638. number = T(*(uint32_t *)raw_source);
  639. } break;
  640. case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT: {
  641. number = T(*(float *)raw_source);
  642. } break;
  643. case GLTFAccessor::COMPONENT_TYPE_DOUBLE_FLOAT: {
  644. number = T(*(double *)raw_source);
  645. } break;
  646. case GLTFAccessor::COMPONENT_TYPE_HALF_FLOAT: {
  647. number = Math::half_to_float(*(uint16_t *)raw_source);
  648. } break;
  649. case GLTFAccessor::COMPONENT_TYPE_SIGNED_LONG: {
  650. number = T(*(int64_t *)raw_source);
  651. } break;
  652. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG: {
  653. number = T(*(uint64_t *)raw_source);
  654. } break;
  655. }
  656. ret_numbers.set(i, number);
  657. raw_read_offset += bytes_per_component;
  658. // Padding and stride skipping are distinct concepts that both need to be handled.
  659. // For example, a 2-in-1 interleaved MAT3 bytes accessor has both, and would look like:
  660. // AAA0 AAA0 AAA0 BBB0 BBB0 BBB0 AAA0 AAA0 AAA0 BBB0 BBB0 BBB0
  661. // The "0" is skipped by the padding, and the "BBB0" is skipped by the stride.
  662. // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#data-alignment
  663. if (unlikely(pad_skip_every > 0)) {
  664. if ((i + 1) % pad_skip_every == 0) {
  665. raw_read_offset += pad_skip_bytes;
  666. }
  667. }
  668. if (unlikely(stride_skip_every > 0)) {
  669. if ((i + 1) % stride_skip_every == 0) {
  670. raw_read_offset += stride_skip_bytes;
  671. }
  672. }
  673. }
  674. return ret_numbers;
  675. }
  676. template <typename T>
  677. Vector<T> GLTFAccessor::_decode_as_numbers(const Ref<GLTFState> &p_gltf_state) const {
  678. const Vector<Ref<GLTFBufferView>> &p_buffer_views = p_gltf_state->get_buffer_views();
  679. Vector<T> ret_numbers = _decode_raw_numbers<T>(p_gltf_state, p_buffer_views, false);
  680. if (sparse_count == 0) {
  681. return ret_numbers;
  682. }
  683. // Handle sparse accessors.
  684. PackedInt64Array sparse_indices = _decode_sparse_indices(p_gltf_state, p_buffer_views);
  685. ERR_FAIL_COND_V_MSG(sparse_indices.size() != sparse_count, ret_numbers, "glTF import: Sparse indices size does not match the sparse count.");
  686. const int64_t vector_size = _get_vector_size();
  687. Vector<T> sparse_values = _decode_raw_numbers<T>(p_gltf_state, p_buffer_views, true);
  688. ERR_FAIL_COND_V_MSG(sparse_values.size() != sparse_count * vector_size, ret_numbers, "glTF import: Sparse values size does not match the sparse count.");
  689. for (int64_t in_sparse = 0; in_sparse < sparse_count; in_sparse++) {
  690. const int64_t sparse_index = sparse_indices[in_sparse];
  691. const int64_t array_offset = sparse_index * vector_size;
  692. ERR_FAIL_INDEX_V_MSG(array_offset, ret_numbers.size(), ret_numbers, "glTF import: Sparse indices were out of bounds for the accessor.");
  693. for (int64_t in_vec = 0; in_vec < vector_size; in_vec++) {
  694. ret_numbers.set(array_offset + in_vec, sparse_values[in_sparse * vector_size + in_vec]);
  695. }
  696. }
  697. return ret_numbers;
  698. }
  699. // High-level decode functions.
  700. PackedColorArray GLTFAccessor::decode_as_colors(const Ref<GLTFState> &p_gltf_state) const {
  701. PackedColorArray ret;
  702. PackedFloat32Array numbers = _decode_as_numbers<float>(p_gltf_state);
  703. if (accessor_type == TYPE_VEC3) {
  704. ERR_FAIL_COND_V_MSG(numbers.size() != count * 3, ret, "glTF import: The accessor does not have the expected amount of numbers for the given count and vector size.");
  705. ret.resize(count);
  706. for (int64_t i = 0; i < count; i++) {
  707. const int64_t number_index = i * 3;
  708. ret.set(i, Color(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], 1.0f));
  709. }
  710. } else if (accessor_type == TYPE_VEC4) {
  711. ERR_FAIL_COND_V_MSG(numbers.size() != count * 4, ret, "glTF import: The accessor does not have the expected amount of numbers for the given count and vector size.");
  712. ret.resize(count);
  713. for (int64_t i = 0; i < count; i++) {
  714. const int64_t number_index = i * 4;
  715. ret.set(i, Color(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], numbers[number_index + 3]));
  716. }
  717. } else {
  718. ERR_FAIL_V_MSG(ret, "glTF import: The `decode_as_colors` function is designed to be fast and can only be used with accessors of type \"VEC3\" or \"VEC4\", but was called with type \"" + _get_accessor_type_name() + "\". Consider using `decode_as_variants` if you need more flexible behavior with support for any accessor type.");
  719. }
  720. return ret;
  721. }
  722. PackedFloat32Array GLTFAccessor::decode_as_float32s(const Ref<GLTFState> &p_gltf_state) const {
  723. return _decode_as_numbers<float>(p_gltf_state);
  724. }
  725. PackedFloat64Array GLTFAccessor::decode_as_float64s(const Ref<GLTFState> &p_gltf_state) const {
  726. return _decode_as_numbers<double>(p_gltf_state);
  727. }
  728. PackedInt32Array GLTFAccessor::decode_as_int32s(const Ref<GLTFState> &p_gltf_state) const {
  729. return _decode_as_numbers<int32_t>(p_gltf_state);
  730. }
  731. PackedInt64Array GLTFAccessor::decode_as_int64s(const Ref<GLTFState> &p_gltf_state) const {
  732. return _decode_as_numbers<int64_t>(p_gltf_state);
  733. }
  734. Vector<Quaternion> GLTFAccessor::decode_as_quaternions(const Ref<GLTFState> &p_gltf_state) const {
  735. Vector<Quaternion> ret;
  736. ERR_FAIL_COND_V_MSG(accessor_type != TYPE_VEC4, ret, "glTF import: The `decode_as_quaternions` function is designed to be fast and can only be used with accessors of type \"VEC4\", but was called with type \"" + _get_accessor_type_name() + "\". Consider using `decode_as_variants` if you need more flexible behavior with support for any accessor type.");
  737. PackedRealArray numbers = _decode_as_numbers<real_t>(p_gltf_state);
  738. ERR_FAIL_COND_V_MSG(numbers.size() != count * 4, ret, "glTF import: The accessor does not have the expected amount of numbers for the given count and vector size.");
  739. ret.resize(count);
  740. for (int64_t i = 0; i < count; i++) {
  741. const int64_t number_index = i * 4;
  742. ret.set(i, Quaternion(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], numbers[number_index + 3]).normalized());
  743. }
  744. return ret;
  745. }
  746. Array GLTFAccessor::decode_as_variants(const Ref<GLTFState> &p_gltf_state, Variant::Type p_variant_type) const {
  747. const int64_t numbers_per_variant = _get_numbers_per_variant_for_gltf(p_variant_type);
  748. Array ret;
  749. ERR_FAIL_COND_V_MSG(numbers_per_variant < 1, ret, "glTF import: The Variant type '" + Variant::get_type_name(p_variant_type) + "' is not supported. Returning an empty array.");
  750. const PackedFloat64Array numbers = _decode_as_numbers<double>(p_gltf_state);
  751. const int64_t vector_size = _get_vector_size();
  752. ERR_FAIL_COND_V_MSG(vector_size < 1, ret, "glTF import: The accessor type '" + _get_accessor_type_name() + "' is not supported. Returning an empty array.");
  753. const int64_t numbers_to_read = MIN(vector_size, numbers_per_variant);
  754. ERR_FAIL_COND_V_MSG(numbers.size() != count * vector_size, ret, "glTF import: The accessor does not have the expected amount of numbers for the given count and vector size.");
  755. ret.resize(count);
  756. for (int64_t value_index = 0; value_index < count; value_index++) {
  757. const int64_t number_index = value_index * vector_size;
  758. switch (p_variant_type) {
  759. case Variant::BOOL: {
  760. ret[value_index] = numbers[number_index] != 0.0;
  761. } break;
  762. case Variant::INT: {
  763. ret[value_index] = (int64_t)numbers[number_index];
  764. } break;
  765. case Variant::FLOAT: {
  766. ret[value_index] = numbers[number_index];
  767. } break;
  768. case Variant::VECTOR2:
  769. case Variant::RECT2:
  770. case Variant::VECTOR3:
  771. case Variant::VECTOR4:
  772. case Variant::PLANE:
  773. case Variant::QUATERNION: {
  774. // General-purpose code for importing glTF accessor data with any component count into structs up to 4 `real_t`s in size.
  775. Vector4 vec;
  776. switch (numbers_to_read) {
  777. case 1: {
  778. vec = Vector4(numbers[number_index], 0.0f, 0.0f, 0.0f);
  779. } break;
  780. case 2: {
  781. vec = Vector4(numbers[number_index], numbers[number_index + 1], 0.0f, 0.0f);
  782. } break;
  783. case 3: {
  784. vec = Vector4(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], 0.0f);
  785. } break;
  786. default: {
  787. vec = Vector4(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], numbers[number_index + 3]);
  788. } break;
  789. }
  790. if (p_variant_type == Variant::QUATERNION) {
  791. vec.normalize();
  792. }
  793. // Evil hack that relies on the structure of Variant, but it's the
  794. // only way to accomplish this without a ton of code duplication.
  795. Variant variant = vec;
  796. *(Variant::Type *)&variant = p_variant_type;
  797. ret[value_index] = variant;
  798. } break;
  799. case Variant::VECTOR2I:
  800. case Variant::RECT2I:
  801. case Variant::VECTOR3I:
  802. case Variant::VECTOR4I: {
  803. // General-purpose code for importing glTF accessor data with any component count into structs up to 4 `int32_t`s in size.
  804. Vector4i vec;
  805. switch (numbers_to_read) {
  806. case 1: {
  807. vec = Vector4i((int32_t)numbers[number_index], 0, 0, 0);
  808. } break;
  809. case 2: {
  810. vec = Vector4i((int32_t)numbers[number_index], (int32_t)numbers[number_index + 1], 0, 0);
  811. } break;
  812. case 3: {
  813. vec = Vector4i((int32_t)numbers[number_index], (int32_t)numbers[number_index + 1], (int32_t)numbers[number_index + 2], 0);
  814. } break;
  815. default: {
  816. vec = Vector4i((int32_t)numbers[number_index], (int32_t)numbers[number_index + 1], (int32_t)numbers[number_index + 2], (int32_t)numbers[number_index + 3]);
  817. } break;
  818. }
  819. // Evil hack that relies on the structure of Variant, but it's the
  820. // only way to accomplish this without a ton of code duplication.
  821. Variant variant = vec;
  822. *(Variant::Type *)&variant = p_variant_type;
  823. ret[value_index] = variant;
  824. } break;
  825. // No more generalized hacks, each of the below types needs a lot of repetitive code.
  826. case Variant::COLOR: {
  827. Color color;
  828. switch (numbers_to_read) {
  829. case 1: {
  830. color = Color(numbers[number_index], 0.0f, 0.0f, 1.0f);
  831. } break;
  832. case 2: {
  833. color = Color(numbers[number_index], numbers[number_index + 1], 0.0f, 1.0f);
  834. } break;
  835. case 3: {
  836. color = Color(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], 1.0f);
  837. } break;
  838. default: {
  839. color = Color(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], numbers[number_index + 3]);
  840. } break;
  841. }
  842. ret[value_index] = color;
  843. } break;
  844. case Variant::TRANSFORM2D: {
  845. Transform2D t;
  846. switch (numbers_to_read) {
  847. case 4: {
  848. t.columns[0] = Vector2(numbers[number_index + 0], numbers[number_index + 1]);
  849. t.columns[1] = Vector2(numbers[number_index + 2], numbers[number_index + 3]);
  850. } break;
  851. case 9: {
  852. t.columns[0] = Vector2(numbers[number_index + 0], numbers[number_index + 1]);
  853. t.columns[1] = Vector2(numbers[number_index + 3], numbers[number_index + 4]);
  854. t.columns[2] = Vector2(numbers[number_index + 6], numbers[number_index + 7]);
  855. } break;
  856. case 16: {
  857. t.columns[0] = Vector2(numbers[number_index + 0], numbers[number_index + 1]);
  858. t.columns[1] = Vector2(numbers[number_index + 4], numbers[number_index + 5]);
  859. t.columns[2] = Vector2(numbers[number_index + 12], numbers[number_index + 13]);
  860. } break;
  861. }
  862. ret[value_index] = t;
  863. } break;
  864. case Variant::AABB: {
  865. AABB aabb;
  866. switch (numbers_to_read) {
  867. case 4: {
  868. aabb.position = Vector3(numbers[number_index + 0], numbers[number_index + 1], 0.0f);
  869. aabb.size = Vector3(numbers[number_index + 2], numbers[number_index + 3], 0.0f);
  870. } break;
  871. case 9: {
  872. aabb.position = Vector3(numbers[number_index + 0], numbers[number_index + 1], numbers[number_index + 2]);
  873. aabb.size = Vector3(numbers[number_index + 3], numbers[number_index + 4], numbers[number_index + 5]);
  874. } break;
  875. case 16: {
  876. aabb.position = Vector3(numbers[number_index + 0], numbers[number_index + 1], numbers[number_index + 2]);
  877. aabb.size = Vector3(numbers[number_index + 4], numbers[number_index + 5], numbers[number_index + 6]);
  878. } break;
  879. }
  880. ret[value_index] = aabb;
  881. } break;
  882. case Variant::BASIS: {
  883. Basis b;
  884. switch (numbers_to_read) {
  885. case 4: {
  886. b.rows[0] = Vector3(numbers[number_index + 0], numbers[number_index + 2], 0.0f);
  887. b.rows[1] = Vector3(numbers[number_index + 1], numbers[number_index + 3], 0.0f);
  888. } break;
  889. case 9: {
  890. b.rows[0] = Vector3(numbers[number_index + 0], numbers[number_index + 3], numbers[number_index + 6]);
  891. b.rows[1] = Vector3(numbers[number_index + 1], numbers[number_index + 4], numbers[number_index + 7]);
  892. b.rows[2] = Vector3(numbers[number_index + 2], numbers[number_index + 5], numbers[number_index + 8]);
  893. } break;
  894. case 16: {
  895. b.rows[0] = Vector3(numbers[number_index + 0], numbers[number_index + 4], numbers[number_index + 8]);
  896. b.rows[1] = Vector3(numbers[number_index + 1], numbers[number_index + 5], numbers[number_index + 9]);
  897. b.rows[2] = Vector3(numbers[number_index + 2], numbers[number_index + 6], numbers[number_index + 10]);
  898. } break;
  899. }
  900. ret[value_index] = b;
  901. } break;
  902. case Variant::TRANSFORM3D: {
  903. Transform3D t;
  904. switch (numbers_to_read) {
  905. case 4: {
  906. t.basis.rows[0] = Vector3(numbers[number_index + 0], numbers[number_index + 2], 0.0f);
  907. t.basis.rows[1] = Vector3(numbers[number_index + 1], numbers[number_index + 3], 0.0f);
  908. } break;
  909. case 9: {
  910. t.basis.rows[0] = Vector3(numbers[number_index + 0], numbers[number_index + 3], numbers[number_index + 6]);
  911. t.basis.rows[1] = Vector3(numbers[number_index + 1], numbers[number_index + 4], numbers[number_index + 7]);
  912. t.basis.rows[2] = Vector3(numbers[number_index + 2], numbers[number_index + 5], numbers[number_index + 8]);
  913. } break;
  914. case 16: {
  915. t.basis.rows[0] = Vector3(numbers[number_index + 0], numbers[number_index + 4], numbers[number_index + 8]);
  916. t.basis.rows[1] = Vector3(numbers[number_index + 1], numbers[number_index + 5], numbers[number_index + 9]);
  917. t.basis.rows[2] = Vector3(numbers[number_index + 2], numbers[number_index + 6], numbers[number_index + 10]);
  918. t.origin = Vector3(numbers[number_index + 12], numbers[number_index + 13], numbers[number_index + 14]);
  919. } break;
  920. }
  921. ret[value_index] = t;
  922. } break;
  923. case Variant::PROJECTION: {
  924. Projection p;
  925. switch (numbers_to_read) {
  926. case 4: {
  927. p.columns[0] = Vector4(numbers[number_index + 0], numbers[number_index + 1], 0.0f, 0.0f);
  928. p.columns[1] = Vector4(numbers[number_index + 4], numbers[number_index + 5], 0.0f, 0.0f);
  929. } break;
  930. case 9: {
  931. p.columns[0] = Vector4(numbers[number_index + 0], numbers[number_index + 1], numbers[number_index + 2], 0.0f);
  932. p.columns[1] = Vector4(numbers[number_index + 4], numbers[number_index + 5], numbers[number_index + 6], 0.0f);
  933. p.columns[2] = Vector4(numbers[number_index + 8], numbers[number_index + 9], numbers[number_index + 10], 0.0f);
  934. } break;
  935. case 16: {
  936. p.columns[0] = Vector4(numbers[number_index + 0], numbers[number_index + 1], numbers[number_index + 2], numbers[number_index + 3]);
  937. p.columns[1] = Vector4(numbers[number_index + 4], numbers[number_index + 5], numbers[number_index + 6], numbers[number_index + 7]);
  938. p.columns[2] = Vector4(numbers[number_index + 8], numbers[number_index + 9], numbers[number_index + 10], numbers[number_index + 11]);
  939. p.columns[3] = Vector4(numbers[number_index + 12], numbers[number_index + 13], numbers[number_index + 14], numbers[number_index + 15]);
  940. } break;
  941. }
  942. ret[value_index] = p;
  943. } break;
  944. case Variant::PACKED_BYTE_ARRAY: {
  945. PackedByteArray packed_array;
  946. packed_array.resize(numbers_to_read);
  947. for (int64_t j = 0; j < numbers_to_read; j++) {
  948. packed_array.set(value_index, numbers[number_index + j]);
  949. }
  950. } break;
  951. case Variant::PACKED_INT32_ARRAY: {
  952. PackedInt32Array packed_array;
  953. packed_array.resize(numbers_to_read);
  954. for (int64_t j = 0; j < numbers_to_read; j++) {
  955. packed_array.set(value_index, numbers[number_index + j]);
  956. }
  957. } break;
  958. case Variant::PACKED_INT64_ARRAY: {
  959. PackedInt64Array packed_array;
  960. packed_array.resize(numbers_to_read);
  961. for (int64_t j = 0; j < numbers_to_read; j++) {
  962. packed_array.set(value_index, numbers[number_index + j]);
  963. }
  964. } break;
  965. case Variant::PACKED_FLOAT32_ARRAY: {
  966. PackedFloat32Array packed_array;
  967. packed_array.resize(numbers_to_read);
  968. for (int64_t j = 0; j < numbers_to_read; j++) {
  969. packed_array.set(value_index, numbers[number_index + j]);
  970. }
  971. } break;
  972. case Variant::PACKED_FLOAT64_ARRAY: {
  973. PackedFloat64Array packed_array;
  974. packed_array.resize(numbers_to_read);
  975. for (int64_t j = 0; j < numbers_to_read; j++) {
  976. packed_array.set(value_index, numbers[number_index + j]);
  977. }
  978. } break;
  979. default: {
  980. ERR_FAIL_V_MSG(ret, "glTF: Cannot decode accessor as Variant of type " + Variant::get_type_name(p_variant_type) + ".");
  981. }
  982. }
  983. }
  984. return ret;
  985. }
  986. PackedVector2Array GLTFAccessor::decode_as_vector2s(const Ref<GLTFState> &p_gltf_state) const {
  987. PackedVector2Array ret;
  988. ERR_FAIL_COND_V_MSG(accessor_type != TYPE_VEC2, ret, "glTF import: The `decode_as_vector2s` function is designed to be fast and can only be used with accessors of type \"VEC2\", but was called with type \"" + _get_accessor_type_name() + "\". Consider using `decode_as_variants` if you need more flexible behavior with support for any accessor type.");
  989. PackedRealArray numbers = _decode_as_numbers<real_t>(p_gltf_state);
  990. ERR_FAIL_COND_V_MSG(numbers.size() != count * 2, ret, "glTF import: The accessor does not have the expected amount of numbers for the given count and vector size.");
  991. ret.resize(count);
  992. for (int64_t i = 0; i < count; i++) {
  993. const int64_t number_index = i * 2;
  994. ret.set(i, Vector2(numbers[number_index], numbers[number_index + 1]));
  995. }
  996. return ret;
  997. }
  998. PackedVector3Array GLTFAccessor::decode_as_vector3s(const Ref<GLTFState> &p_gltf_state) const {
  999. PackedVector3Array ret;
  1000. ERR_FAIL_COND_V_MSG(accessor_type != TYPE_VEC3, ret, "glTF import: The `decode_as_vector3s` function is designed to be fast and can only be used with accessors of type \"VEC3\", but was called with type \"" + _get_accessor_type_name() + "\". Consider using `decode_as_variants` if you need more flexible behavior with support for any accessor type.");
  1001. PackedRealArray numbers = _decode_as_numbers<real_t>(p_gltf_state);
  1002. ERR_FAIL_COND_V_MSG(numbers.size() != count * 3, ret, "glTF import: The accessor does not have the expected amount of numbers for the given count and vector size.");
  1003. ret.resize(count);
  1004. for (int64_t i = 0; i < count; i++) {
  1005. const int64_t number_index = i * 3;
  1006. ret.set(i, Vector3(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2]));
  1007. }
  1008. return ret;
  1009. }
  1010. PackedVector4Array GLTFAccessor::decode_as_vector4s(const Ref<GLTFState> &p_gltf_state) const {
  1011. PackedVector4Array ret;
  1012. ERR_FAIL_COND_V_MSG(accessor_type != TYPE_VEC4, ret, "glTF import: The `decode_as_vector4s` function is designed to be fast and can only be used with accessors of type \"VEC4\", but was called with type \"" + _get_accessor_type_name() + "\". Consider using `decode_as_variants` if you need more flexible behavior with support for any accessor type.");
  1013. PackedRealArray numbers = _decode_as_numbers<real_t>(p_gltf_state);
  1014. ERR_FAIL_COND_V_MSG(numbers.size() != count * 4, ret, "glTF import: The accessor does not have the expected amount of numbers for the given count and vector size.");
  1015. ret.resize(count);
  1016. for (int64_t i = 0; i < count; i++) {
  1017. const int64_t number_index = i * 4;
  1018. ret.set(i, Vector4(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], numbers[number_index + 3]));
  1019. }
  1020. return ret;
  1021. }
  1022. // Private encode functions.
  1023. PackedFloat64Array GLTFAccessor::_encode_variants_as_floats(const Array &p_input_data, Variant::Type p_variant_type) const {
  1024. const int64_t vector_size = _get_vector_size();
  1025. const int64_t input_size = p_input_data.size();
  1026. PackedFloat64Array numbers;
  1027. numbers.resize(input_size * vector_size);
  1028. for (int64_t input_index = 0; input_index < input_size; input_index++) {
  1029. Variant variant = p_input_data[input_index];
  1030. const int64_t vector_offset = input_index * vector_size;
  1031. switch (p_variant_type) {
  1032. case Variant::NIL:
  1033. case Variant::BOOL:
  1034. case Variant::INT:
  1035. case Variant::FLOAT: {
  1036. // For scalar values, just append them. Variant can convert all of these to double. Some padding may also be needed.
  1037. numbers.set(vector_offset, variant);
  1038. if (unlikely(vector_size > 1)) {
  1039. for (int64_t i = 1; i < vector_size; i++) {
  1040. numbers.set(vector_offset + i, 0.0);
  1041. }
  1042. }
  1043. } break;
  1044. case Variant::PLANE:
  1045. case Variant::QUATERNION:
  1046. case Variant::RECT2: {
  1047. // Evil hack that relies on the structure of Variant, but it's the
  1048. // only way to accomplish this without a ton of code duplication.
  1049. *(Variant::Type *)&variant = Variant::VECTOR4;
  1050. }
  1051. [[fallthrough]];
  1052. case Variant::VECTOR2:
  1053. case Variant::VECTOR3:
  1054. case Variant::VECTOR4: {
  1055. // Variant can handle converting Vector2/3/4 to Vector4 for us.
  1056. Vector4 vec = variant;
  1057. for (int64_t i = 0; i < vector_size; i++) {
  1058. numbers.set(vector_offset + i, vec[i]);
  1059. }
  1060. if (unlikely(vector_size > 4)) {
  1061. for (int64_t i = 4; i < vector_size; i++) {
  1062. numbers.set(vector_offset + i, 0.0);
  1063. }
  1064. }
  1065. } break;
  1066. case Variant::RECT2I: {
  1067. *(Variant::Type *)&variant = Variant::VECTOR4I;
  1068. }
  1069. [[fallthrough]];
  1070. case Variant::VECTOR2I:
  1071. case Variant::VECTOR3I:
  1072. case Variant::VECTOR4I: {
  1073. // Variant can handle converting Vector2i/3i/4i to Vector4i for us.
  1074. Vector4i vec = variant;
  1075. for (int64_t i = 0; i < vector_size; i++) {
  1076. numbers.set(vector_offset + i, vec[i]);
  1077. }
  1078. if (unlikely(vector_size > 4)) {
  1079. for (int64_t i = 4; i < vector_size; i++) {
  1080. numbers.set(vector_offset + i, 0.0);
  1081. }
  1082. }
  1083. } break;
  1084. case Variant::COLOR: {
  1085. Color c = variant;
  1086. for (int64_t i = 0; i < vector_size; i++) {
  1087. numbers.set(vector_offset + i, c[i]);
  1088. }
  1089. if (unlikely(vector_size > 4)) {
  1090. for (int64_t i = 4; i < vector_size; i++) {
  1091. numbers.set(vector_offset + i, 0.0);
  1092. }
  1093. }
  1094. } break;
  1095. case Variant::TRANSFORM2D:
  1096. case Variant::BASIS:
  1097. case Variant::TRANSFORM3D:
  1098. case Variant::PROJECTION: {
  1099. // Variant can handle converting Transform2D/Transform3D/Basis to Projection for us.
  1100. Projection p = variant;
  1101. if (vector_size == 16) {
  1102. for (int64_t i = 0; i < 4; i++) {
  1103. numbers.set(vector_offset + 4 * i, p.columns[i][0]);
  1104. numbers.set(vector_offset + 4 * i + 1, p.columns[i][1]);
  1105. numbers.set(vector_offset + 4 * i + 2, p.columns[i][2]);
  1106. numbers.set(vector_offset + 4 * i + 3, p.columns[i][3]);
  1107. }
  1108. } else if (vector_size == 9) {
  1109. for (int64_t i = 0; i < 3; i++) {
  1110. numbers.set(vector_offset + 3 * i, p.columns[i][0]);
  1111. numbers.set(vector_offset + 3 * i + 1, p.columns[i][1]);
  1112. numbers.set(vector_offset + 3 * i + 2, p.columns[i][2]);
  1113. }
  1114. } else if (vector_size == 4) {
  1115. numbers.set(vector_offset, p.columns[0][0]);
  1116. numbers.set(vector_offset + 1, p.columns[0][1]);
  1117. numbers.set(vector_offset + 2, p.columns[1][0]);
  1118. numbers.set(vector_offset + 3, p.columns[1][1]);
  1119. }
  1120. } break;
  1121. default: {
  1122. ERR_FAIL_V_MSG(PackedFloat64Array(), "glTF export: Cannot encode accessor from Variant of type " + Variant::get_type_name(p_variant_type) + ".");
  1123. }
  1124. }
  1125. }
  1126. return numbers;
  1127. }
  1128. void GLTFAccessor::_store_sparse_indices_into_state(const Ref<GLTFState> &p_gltf_state, const PackedInt64Array &p_sparse_indices, const bool p_deduplicate) {
  1129. // The byte offset of a sparse accessor's indices buffer view MUST be a multiple of the indices primitive componentType.
  1130. // https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/accessor.sparse.indices.schema.json
  1131. const int64_t bytes_per_index = _get_bytes_per_component(sparse_indices_component_type);
  1132. PackedByteArray indices_bytes;
  1133. indices_bytes.resize(bytes_per_index * p_sparse_indices.size());
  1134. uint8_t *ret_write = indices_bytes.ptrw();
  1135. int64_t ret_byte_offset = 0;
  1136. for (int64_t i = 0; i < p_sparse_indices.size(); i++) {
  1137. switch (sparse_indices_component_type) {
  1138. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {
  1139. *(uint8_t *)&ret_write[ret_byte_offset] = p_sparse_indices[i];
  1140. } break;
  1141. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {
  1142. *(uint16_t *)&ret_write[ret_byte_offset] = p_sparse_indices[i];
  1143. } break;
  1144. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT: {
  1145. *(uint32_t *)&ret_write[ret_byte_offset] = p_sparse_indices[i];
  1146. } break;
  1147. case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG: {
  1148. *(uint64_t *)&ret_write[ret_byte_offset] = p_sparse_indices[i];
  1149. } break;
  1150. default: {
  1151. ERR_FAIL_MSG("glTF export: Invalid sparse indices component type '" + _get_component_type_name(sparse_indices_component_type) + "' for sparse accessor indices.");
  1152. } break;
  1153. }
  1154. ret_byte_offset += bytes_per_index;
  1155. }
  1156. const GLTFBufferViewIndex buffer_view_index = GLTFBufferView::write_new_buffer_view_into_state(p_gltf_state, indices_bytes, bytes_per_index, GLTFBufferView::TARGET_NONE, -1, 0, p_deduplicate);
  1157. ERR_FAIL_COND_MSG(buffer_view_index == -1, "glTF export: Failed to write sparse indices into glTF state.");
  1158. set_sparse_indices_buffer_view(buffer_view_index);
  1159. }
  1160. // Low-level encode functions.
  1161. GLTFAccessor::GLTFComponentType GLTFAccessor::get_minimal_integer_component_type_from_ints(const PackedInt64Array &p_numbers) {
  1162. bool has_negative = false;
  1163. for (int64_t i = 0; i < p_numbers.size(); i++) {
  1164. if (p_numbers[i] < 0) {
  1165. has_negative = true;
  1166. break;
  1167. }
  1168. }
  1169. if (has_negative) {
  1170. GLTFComponentType ret = GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE;
  1171. for (int64_t i = 0; i < p_numbers.size(); i++) {
  1172. const int64_t num = p_numbers[i];
  1173. if (ret == GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE && (num < -128LL || num > 127LL)) {
  1174. ret = GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT;
  1175. }
  1176. if (ret == GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT && (num < -32768LL || num > 32767LL)) {
  1177. ret = GLTFAccessor::COMPONENT_TYPE_SIGNED_INT;
  1178. }
  1179. if (ret == GLTFAccessor::COMPONENT_TYPE_SIGNED_INT && (num < -2147483648LL || num > 2147483647LL)) {
  1180. return GLTFAccessor::COMPONENT_TYPE_SIGNED_LONG;
  1181. }
  1182. }
  1183. return ret;
  1184. }
  1185. GLTFComponentType ret = GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE;
  1186. for (int64_t i = 0; i < p_numbers.size(); i++) {
  1187. const int64_t num = p_numbers[i];
  1188. // 3.7.2.1. indices accessor MUST NOT contain the maximum possible value for the component type used
  1189. // (i.e., 255 for unsigned bytes, 65535 for unsigned shorts, 4294967295 for unsigned ints).
  1190. // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview
  1191. if (ret == GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE && num > 254LL) {
  1192. ret = GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT;
  1193. }
  1194. if (ret == GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT && num > 65534LL) {
  1195. ret = GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT;
  1196. }
  1197. if (ret == GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT && num > 4294967294LL) {
  1198. return GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG;
  1199. }
  1200. }
  1201. return ret;
  1202. }
  1203. PackedByteArray GLTFAccessor::encode_floats_as_bytes(const PackedFloat64Array &p_input_numbers) {
  1204. // Filter and update `count`, `min`, and `max` based on the given data.
  1205. PackedFloat64Array filtered_numbers = _filter_numbers(p_input_numbers);
  1206. count = filtered_numbers.size() / _get_vector_size();
  1207. _calculate_min_and_max(filtered_numbers);
  1208. // Actually encode the data.
  1209. const int64_t input_size = filtered_numbers.size();
  1210. const int64_t bytes_per_component = _get_bytes_per_component(component_type);
  1211. int64_t raw_byte_size = _determine_padded_byte_count(bytes_per_component * input_size);
  1212. int64_t skip_every = 0;
  1213. int64_t skip_bytes = 0;
  1214. _determine_pad_skip(skip_every, skip_bytes);
  1215. PackedByteArray ret;
  1216. ret.resize(raw_byte_size);
  1217. uint8_t *ret_write = ret.ptrw();
  1218. int64_t ret_byte_offset = 0;
  1219. for (int64_t i = 0; i < input_size; i++) {
  1220. switch (component_type) {
  1221. case COMPONENT_TYPE_NONE: {
  1222. ERR_FAIL_V_MSG(ret, "glTF export: Invalid component type 'NONE' for glTF accessor.");
  1223. } break;
  1224. case COMPONENT_TYPE_SIGNED_BYTE: {
  1225. *(int8_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];
  1226. } break;
  1227. case COMPONENT_TYPE_UNSIGNED_BYTE: {
  1228. *(uint8_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];
  1229. } break;
  1230. case COMPONENT_TYPE_SIGNED_SHORT: {
  1231. *(int16_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];
  1232. } break;
  1233. case COMPONENT_TYPE_UNSIGNED_SHORT: {
  1234. *(uint16_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];
  1235. } break;
  1236. case COMPONENT_TYPE_SIGNED_INT: {
  1237. *(int32_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];
  1238. } break;
  1239. case COMPONENT_TYPE_UNSIGNED_INT: {
  1240. *(uint32_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];
  1241. } break;
  1242. case COMPONENT_TYPE_SINGLE_FLOAT: {
  1243. *(float *)&ret_write[ret_byte_offset] = filtered_numbers[i];
  1244. } break;
  1245. case COMPONENT_TYPE_DOUBLE_FLOAT: {
  1246. *(double *)&ret_write[ret_byte_offset] = filtered_numbers[i];
  1247. } break;
  1248. case COMPONENT_TYPE_HALF_FLOAT: {
  1249. *(uint16_t *)&ret_write[ret_byte_offset] = Math::make_half_float(filtered_numbers[i]);
  1250. } break;
  1251. case COMPONENT_TYPE_SIGNED_LONG: {
  1252. // Note: This can potentially result in precision loss because int64_t can store some values that double can't.
  1253. *(int64_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];
  1254. } break;
  1255. case COMPONENT_TYPE_UNSIGNED_LONG: {
  1256. // Note: This can potentially result in precision loss because uint64_t can store some values that double can't.
  1257. *(uint64_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];
  1258. } break;
  1259. default: {
  1260. ERR_FAIL_V_MSG(ret, "glTF export: Godot does not support writing glTF accessor components of type '" + itos(component_type) + "'.");
  1261. } break;
  1262. }
  1263. ret_byte_offset += bytes_per_component;
  1264. if (unlikely(skip_every > 0)) {
  1265. if ((i + 1) % skip_every == 0) {
  1266. ret_byte_offset += skip_bytes;
  1267. }
  1268. }
  1269. }
  1270. ERR_FAIL_COND_V_MSG(ret_byte_offset != raw_byte_size, ret, "glTF export: Accessor encoded data did not write exactly the expected number of bytes.");
  1271. return ret;
  1272. }
  1273. PackedByteArray GLTFAccessor::encode_ints_as_bytes(const PackedInt64Array &p_input_numbers) {
  1274. // Filter and update `count`, `min`, and `max` based on the given data.
  1275. count = p_input_numbers.size() / _get_vector_size();
  1276. _calculate_min_and_max(Variant(p_input_numbers));
  1277. // Actually encode the data.
  1278. const int64_t input_size = p_input_numbers.size();
  1279. const int64_t bytes_per_component = _get_bytes_per_component(component_type);
  1280. int64_t raw_byte_size = _determine_padded_byte_count(bytes_per_component * input_size);
  1281. int64_t skip_every = 0;
  1282. int64_t skip_bytes = 0;
  1283. _determine_pad_skip(skip_every, skip_bytes);
  1284. PackedByteArray ret;
  1285. ret.resize(raw_byte_size);
  1286. uint8_t *ret_write = ret.ptrw();
  1287. int64_t ret_byte_offset = 0;
  1288. for (int64_t i = 0; i < input_size; i++) {
  1289. switch (component_type) {
  1290. case COMPONENT_TYPE_NONE: {
  1291. ERR_FAIL_V_MSG(ret, "glTF export: Invalid component type 'NONE' for glTF accessor.");
  1292. } break;
  1293. case COMPONENT_TYPE_SIGNED_BYTE: {
  1294. *(int8_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];
  1295. } break;
  1296. case COMPONENT_TYPE_UNSIGNED_BYTE: {
  1297. *(uint8_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];
  1298. } break;
  1299. case COMPONENT_TYPE_SIGNED_SHORT: {
  1300. *(int16_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];
  1301. } break;
  1302. case COMPONENT_TYPE_UNSIGNED_SHORT: {
  1303. *(uint16_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];
  1304. } break;
  1305. case COMPONENT_TYPE_SIGNED_INT: {
  1306. *(int32_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];
  1307. } break;
  1308. case COMPONENT_TYPE_UNSIGNED_INT: {
  1309. *(uint32_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];
  1310. } break;
  1311. case COMPONENT_TYPE_SINGLE_FLOAT: {
  1312. *(float *)&ret_write[ret_byte_offset] = p_input_numbers[i];
  1313. } break;
  1314. case COMPONENT_TYPE_DOUBLE_FLOAT: {
  1315. *(double *)&ret_write[ret_byte_offset] = p_input_numbers[i];
  1316. } break;
  1317. case COMPONENT_TYPE_HALF_FLOAT: {
  1318. *(uint16_t *)&ret_write[ret_byte_offset] = Math::make_half_float(p_input_numbers[i]);
  1319. } break;
  1320. case COMPONENT_TYPE_SIGNED_LONG: {
  1321. *(int64_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];
  1322. } break;
  1323. case COMPONENT_TYPE_UNSIGNED_LONG: {
  1324. *(uint64_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];
  1325. } break;
  1326. default: {
  1327. ERR_FAIL_V_MSG(ret, "glTF export: Godot does not support writing glTF accessor components of type '" + itos(component_type) + "'.");
  1328. } break;
  1329. }
  1330. ret_byte_offset += bytes_per_component;
  1331. if (unlikely(skip_every > 0)) {
  1332. if ((i + 1) % skip_every == 0) {
  1333. ret_byte_offset += skip_bytes;
  1334. }
  1335. }
  1336. }
  1337. ERR_FAIL_COND_V_MSG(ret_byte_offset != raw_byte_size, ret, "glTF export: Accessor encoded data did not write exactly the expected number of bytes.");
  1338. return ret;
  1339. }
  1340. PackedByteArray GLTFAccessor::encode_variants_as_bytes(const Array &p_input_data, Variant::Type p_variant_type) {
  1341. const int64_t bytes_per_vec = _get_bytes_per_vector();
  1342. ERR_FAIL_COND_V_MSG(bytes_per_vec == 0, PackedByteArray(), "glTF export: Cannot encode an accessor of this type.");
  1343. PackedFloat64Array numbers = _encode_variants_as_floats(p_input_data, p_variant_type);
  1344. return encode_floats_as_bytes(numbers);
  1345. }
  1346. GLTFAccessorIndex GLTFAccessor::store_accessor_data_into_state(const Ref<GLTFState> &p_gltf_state, const PackedByteArray &p_data_bytes, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target, const GLTFBufferIndex p_buffer_index, const bool p_deduplicate) {
  1347. ERR_FAIL_COND_V_MSG(p_data_bytes.is_empty(), -1, "glTF export: Cannot store nothing.");
  1348. // Update `count` based on the size of the data. It's possible that `count` may already be correct, but this function is public, so this prevents footguns.
  1349. const int64_t bytes_per_vec = _get_bytes_per_vector();
  1350. ERR_FAIL_COND_V_MSG(bytes_per_vec == 0 || p_data_bytes.size() % bytes_per_vec != 0, -1, "glTF export: Tried to store an accessor with data that is not a multiple of the accessor's bytes per vector.");
  1351. count = p_data_bytes.size() / bytes_per_vec;
  1352. // 3.6.2.4. The byte offset of an accessor's buffer view MUST be a multiple of the accessor's primitive size.
  1353. // https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#data-alignment
  1354. const int64_t alignment = _get_bytes_per_component(component_type);
  1355. // 3.6.2.4. Each element of a vertex attribute MUST be aligned to 4-byte boundaries inside a bufferView.
  1356. int64_t byte_stride = -1;
  1357. if (p_buffer_view_target == GLTFBufferView::TARGET_ARRAY_BUFFER) {
  1358. byte_stride = bytes_per_vec;
  1359. ERR_FAIL_COND_V_MSG(byte_stride < 4 || byte_stride % 4 != 0, -1, "glTF export: Vertex attributes using TARGET_ARRAY_BUFFER must have a byte stride that is a multiple of 4 as required by section 3.6.2.4 of the glTF specification.");
  1360. }
  1361. // Write the data into a new buffer view.
  1362. const GLTFBufferViewIndex buffer_view_index = GLTFBufferView::write_new_buffer_view_into_state(p_gltf_state, p_data_bytes, alignment, p_buffer_view_target, byte_stride, 0, p_deduplicate);
  1363. ERR_FAIL_COND_V_MSG(buffer_view_index == -1, -1, "glTF export: Accessor failed to write new buffer view into glTF state.");
  1364. set_buffer_view(buffer_view_index);
  1365. // Add the new accessor to the state, but check for duplicates first.
  1366. Vector<Ref<GLTFAccessor>> state_accessors = p_gltf_state->get_accessors();
  1367. const GLTFAccessorIndex accessor_count = state_accessors.size();
  1368. for (GLTFAccessorIndex i = 0; i < accessor_count; i++) {
  1369. const Ref<GLTFAccessor> &existing_accessor = state_accessors[i];
  1370. if (is_equal_exact(existing_accessor)) {
  1371. // An identical accessor already exists in the state, so just return the index.
  1372. return i;
  1373. }
  1374. }
  1375. Ref<GLTFAccessor> self = this;
  1376. state_accessors.append(self);
  1377. p_gltf_state->set_accessors(state_accessors);
  1378. return accessor_count;
  1379. }
  1380. Ref<GLTFAccessor> GLTFAccessor::make_new_accessor_without_data(GLTFAccessorType p_accessor_type, GLTFComponentType p_component_type) {
  1381. Ref<GLTFAccessor> accessor;
  1382. accessor.instantiate();
  1383. accessor->set_accessor_type(p_accessor_type);
  1384. accessor->set_component_type(p_component_type);
  1385. return accessor;
  1386. }
  1387. // High-level encode functions.
  1388. GLTFAccessorIndex GLTFAccessor::encode_new_accessor_from_colors(const Ref<GLTFState> &p_gltf_state, const PackedColorArray &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target, const bool p_deduplicate) {
  1389. ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");
  1390. PackedFloat64Array numbers;
  1391. numbers.resize(p_input_data.size() * 4);
  1392. for (int64_t i = 0; i < p_input_data.size(); i++) {
  1393. const Color &color = p_input_data[i];
  1394. numbers.set(i * 4, color.r);
  1395. numbers.set(i * 4 + 1, color.g);
  1396. numbers.set(i * 4 + 2, color.b);
  1397. numbers.set(i * 4 + 3, color.a);
  1398. }
  1399. Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC4, COMPONENT_TYPE_SINGLE_FLOAT);
  1400. PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(numbers);
  1401. ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");
  1402. return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);
  1403. }
  1404. GLTFAccessorIndex GLTFAccessor::encode_new_accessor_from_float64s(const Ref<GLTFState> &p_gltf_state, const PackedFloat64Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target, const bool p_deduplicate) {
  1405. ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");
  1406. Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_SCALAR, COMPONENT_TYPE_SINGLE_FLOAT);
  1407. PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(p_input_data);
  1408. ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");
  1409. return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);
  1410. }
  1411. GLTFAccessorIndex GLTFAccessor::encode_new_accessor_from_int32s(const Ref<GLTFState> &p_gltf_state, const PackedInt32Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target, const bool p_deduplicate) {
  1412. ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");
  1413. PackedInt64Array numbers;
  1414. numbers.resize(p_input_data.size());
  1415. for (int64_t i = 0; i < p_input_data.size(); i++) {
  1416. numbers.set(i, p_input_data[i]);
  1417. }
  1418. const GLTFComponentType component_type = get_minimal_integer_component_type_from_ints(numbers);
  1419. Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_SCALAR, component_type);
  1420. PackedByteArray encoded_bytes = accessor->encode_ints_as_bytes(numbers);
  1421. ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");
  1422. return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);
  1423. }
  1424. GLTFAccessorIndex GLTFAccessor::encode_new_accessor_from_int64s(const Ref<GLTFState> &p_gltf_state, const PackedInt64Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target, const bool p_deduplicate) {
  1425. ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");
  1426. const GLTFComponentType component_type = get_minimal_integer_component_type_from_ints(p_input_data);
  1427. Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_SCALAR, component_type);
  1428. PackedByteArray encoded_bytes = accessor->encode_ints_as_bytes(p_input_data);
  1429. ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");
  1430. return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);
  1431. }
  1432. GLTFAccessorIndex GLTFAccessor::encode_new_accessor_from_quaternions(const Ref<GLTFState> &p_gltf_state, const Vector<Quaternion> &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target, const bool p_deduplicate) {
  1433. ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");
  1434. PackedFloat64Array numbers;
  1435. numbers.resize(p_input_data.size() * 4);
  1436. for (int64_t i = 0; i < p_input_data.size(); i++) {
  1437. const Quaternion &quat = p_input_data[i];
  1438. numbers.set(i * 4, quat.x);
  1439. numbers.set(i * 4 + 1, quat.y);
  1440. numbers.set(i * 4 + 2, quat.z);
  1441. numbers.set(i * 4 + 3, quat.w);
  1442. }
  1443. Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC4, COMPONENT_TYPE_SINGLE_FLOAT);
  1444. PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(numbers);
  1445. ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");
  1446. return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);
  1447. }
  1448. GLTFAccessorIndex GLTFAccessor::encode_new_accessor_from_variants(const Ref<GLTFState> &p_gltf_state, const Array &p_input_data, Variant::Type p_variant_type, GLTFAccessorType p_accessor_type, GLTFComponentType p_component_type, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target, const bool p_deduplicate) {
  1449. ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");
  1450. Ref<GLTFAccessor> accessor = make_new_accessor_without_data(p_accessor_type, p_component_type);
  1451. // Write the data into a new buffer view.
  1452. PackedByteArray encoded_bytes = accessor->encode_variants_as_bytes(p_input_data, p_variant_type);
  1453. ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");
  1454. return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);
  1455. }
  1456. GLTFAccessorIndex GLTFAccessor::encode_new_accessor_from_vector2s(const Ref<GLTFState> &p_gltf_state, const PackedVector2Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target, const bool p_deduplicate) {
  1457. ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");
  1458. PackedFloat64Array numbers;
  1459. numbers.resize(p_input_data.size() * 2);
  1460. for (int64_t i = 0; i < p_input_data.size(); i++) {
  1461. const Vector2 &vec = p_input_data[i];
  1462. numbers.set(i * 2, vec.x);
  1463. numbers.set(i * 2 + 1, vec.y);
  1464. }
  1465. Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC2, COMPONENT_TYPE_SINGLE_FLOAT);
  1466. PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(numbers);
  1467. ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");
  1468. return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);
  1469. }
  1470. GLTFAccessorIndex GLTFAccessor::encode_new_accessor_from_vector3s(const Ref<GLTFState> &p_gltf_state, const PackedVector3Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target, const bool p_deduplicate) {
  1471. ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");
  1472. PackedFloat64Array numbers;
  1473. numbers.resize(p_input_data.size() * 3);
  1474. for (int64_t i = 0; i < p_input_data.size(); i++) {
  1475. const Vector3 &vec = p_input_data[i];
  1476. numbers.set(i * 3, vec.x);
  1477. numbers.set(i * 3 + 1, vec.y);
  1478. numbers.set(i * 3 + 2, vec.z);
  1479. }
  1480. Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC3, COMPONENT_TYPE_SINGLE_FLOAT);
  1481. PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(numbers);
  1482. ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");
  1483. return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);
  1484. }
  1485. GLTFAccessorIndex GLTFAccessor::encode_new_accessor_from_vector4s(const Ref<GLTFState> &p_gltf_state, const PackedVector4Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target, const bool p_deduplicate) {
  1486. ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");
  1487. PackedFloat64Array numbers;
  1488. numbers.resize(p_input_data.size() * 4);
  1489. for (int64_t i = 0; i < p_input_data.size(); i++) {
  1490. const Vector4 &vec = p_input_data[i];
  1491. numbers.set(i * 4, vec.x);
  1492. numbers.set(i * 4 + 1, vec.y);
  1493. numbers.set(i * 4 + 2, vec.z);
  1494. numbers.set(i * 4 + 3, vec.w);
  1495. }
  1496. Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC4, COMPONENT_TYPE_SINGLE_FLOAT);
  1497. PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(numbers);
  1498. ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");
  1499. return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);
  1500. }
  1501. GLTFAccessorIndex GLTFAccessor::encode_new_accessor_from_vector4is(const Ref<GLTFState> &p_gltf_state, const Vector<Vector4i> &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target, const bool p_deduplicate) {
  1502. ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");
  1503. PackedInt64Array numbers;
  1504. numbers.resize(p_input_data.size() * 4);
  1505. for (int64_t i = 0; i < p_input_data.size(); i++) {
  1506. const Vector4i &vec = p_input_data[i];
  1507. numbers.set(i * 4, vec.x);
  1508. numbers.set(i * 4 + 1, vec.y);
  1509. numbers.set(i * 4 + 2, vec.z);
  1510. numbers.set(i * 4 + 3, vec.w);
  1511. }
  1512. const GLTFComponentType component_type = get_minimal_integer_component_type_from_ints(numbers);
  1513. Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC4, component_type);
  1514. PackedByteArray encoded_bytes = accessor->encode_ints_as_bytes(numbers);
  1515. ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");
  1516. return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);
  1517. }
  1518. GLTFAccessorIndex GLTFAccessor::encode_new_sparse_accessor_from_vec3s(const Ref<GLTFState> &p_gltf_state, const PackedVector3Array &p_input_data, const PackedVector3Array &p_base_reference_data, const double p_tolerance_multiplier, const GLTFBufferView::ArrayBufferTarget p_main_buffer_view_target, const bool p_deduplicate) {
  1519. const int64_t input_size = p_input_data.size();
  1520. ERR_FAIL_COND_V_MSG(input_size == 0, -1, "glTF export: Cannot encode an accessor from an empty array.");
  1521. const bool is_base_empty = p_base_reference_data.is_empty();
  1522. ERR_FAIL_COND_V_MSG(!is_base_empty && p_base_reference_data.size() != input_size, -1, "glTF export: Base reference data must either be empty, or have the same size as the main input data.");
  1523. PackedInt64Array sparse_indices;
  1524. PackedFloat64Array sparse_values;
  1525. PackedFloat64Array dense_values;
  1526. int64_t highest_index = 0;
  1527. dense_values.resize(input_size * 3);
  1528. for (int64_t i = 0; i < input_size; i++) {
  1529. Vector3 vec = p_input_data[i];
  1530. Vector3 base_ref_vec;
  1531. Vector3 displacement;
  1532. if (is_base_empty) {
  1533. base_ref_vec = Vector3();
  1534. displacement = vec;
  1535. } else {
  1536. base_ref_vec = p_base_reference_data[i];
  1537. displacement = vec - base_ref_vec;
  1538. }
  1539. if ((displacement * p_tolerance_multiplier).is_zero_approx()) {
  1540. vec = base_ref_vec;
  1541. } else {
  1542. highest_index = i;
  1543. sparse_indices.append(i);
  1544. sparse_values.append(vec.x);
  1545. sparse_values.append(vec.y);
  1546. sparse_values.append(vec.z);
  1547. }
  1548. dense_values.set(i * 3, vec.x);
  1549. dense_values.set(i * 3 + 1, vec.y);
  1550. dense_values.set(i * 3 + 2, vec.z);
  1551. }
  1552. // Check if the sparse accessor actually saves space, or if it's better to just use a normal accessor.
  1553. const int64_t sparse_count = sparse_indices.size();
  1554. const int64_t bytes_per_value_component = _get_bytes_per_component(COMPONENT_TYPE_SINGLE_FLOAT);
  1555. const GLTFComponentType indices_component_type = _get_indices_component_type_for_size(highest_index);
  1556. const int64_t sparse_data_bytes = _get_bytes_per_component(indices_component_type) * sparse_count + bytes_per_value_component * sparse_values.size();
  1557. const int64_t dense_data_bytes = bytes_per_value_component * 3 * input_size;
  1558. // Sparse accessors require more JSON, a bit under 200 characters when minified, so factor that in.
  1559. constexpr int64_t sparse_json_fluff = 200;
  1560. Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC3, COMPONENT_TYPE_SINGLE_FLOAT);
  1561. if (sparse_data_bytes + sparse_json_fluff >= dense_data_bytes) {
  1562. // Sparse accessor is not worth it, just use a normal accessor instead.
  1563. // However, note that we use the calculated dense values instead of the original input data.
  1564. // This way, regardless of the underlying storage layout, the data is the same in both cases.
  1565. PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(dense_values);
  1566. ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");
  1567. return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_main_buffer_view_target, 0, p_deduplicate);
  1568. }
  1569. // Encode as a sparse accessor.
  1570. if (sparse_count > 0) {
  1571. accessor->set_sparse_count(sparse_count);
  1572. accessor->set_sparse_indices_component_type(indices_component_type);
  1573. accessor->_store_sparse_indices_into_state(p_gltf_state, sparse_indices, p_deduplicate);
  1574. const PackedByteArray sparse_values_encoded_bytes = accessor->encode_floats_as_bytes(sparse_values);
  1575. ERR_FAIL_COND_V_MSG(sparse_values_encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode sparse values as bytes.");
  1576. // Note: Sparse values always use TARGET_NONE, it does NOT match the target of the main buffer view.
  1577. const GLTFBufferViewIndex sparse_values_buffer_view_index = GLTFBufferView::write_new_buffer_view_into_state(p_gltf_state, sparse_values_encoded_bytes, bytes_per_value_component, GLTFBufferView::TARGET_NONE, -1, 0, p_deduplicate);
  1578. accessor->set_sparse_values_buffer_view(sparse_values_buffer_view_index);
  1579. }
  1580. // If the base reference data is empty, just directly add the accessor with only sparse data.
  1581. if (is_base_empty) {
  1582. // This is similar to `encode_floats_as_bytes` + `store_accessor_data_into_state` but we don't write a buffer view.
  1583. // Filter and update `count`, `min`, and `max` based on the given data.
  1584. accessor->set_count(input_size);
  1585. const PackedFloat64Array filtered_numbers = accessor->_filter_numbers(dense_values);
  1586. accessor->_calculate_min_and_max(filtered_numbers);
  1587. // Add the new accessor to the state, but check for duplicates first.
  1588. Vector<Ref<GLTFAccessor>> state_accessors = p_gltf_state->get_accessors();
  1589. const GLTFAccessorIndex accessor_count = state_accessors.size();
  1590. for (GLTFAccessorIndex i = 0; i < accessor_count; i++) {
  1591. const Ref<GLTFAccessor> &existing_accessor = state_accessors[i];
  1592. if (accessor->is_equal_exact(existing_accessor)) {
  1593. // An identical accessor already exists in the state, so just return the index.
  1594. return i;
  1595. }
  1596. }
  1597. state_accessors.append(accessor);
  1598. p_gltf_state->set_accessors(state_accessors);
  1599. return accessor_count;
  1600. }
  1601. // Encode the base reference alongside the sparse data.
  1602. PackedFloat64Array base_reference_values;
  1603. base_reference_values.resize(input_size * 3);
  1604. for (int64_t i = 0; i < input_size; i++) {
  1605. const Vector3 &base_ref_vec = p_base_reference_data[i];
  1606. base_reference_values.set(i * 3, base_ref_vec.x);
  1607. base_reference_values.set(i * 3 + 1, base_ref_vec.y);
  1608. base_reference_values.set(i * 3 + 2, base_ref_vec.z);
  1609. }
  1610. const PackedByteArray base_reference_encoded_bytes = accessor->encode_floats_as_bytes(base_reference_values);
  1611. ERR_FAIL_COND_V_MSG(base_reference_encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");
  1612. return accessor->store_accessor_data_into_state(p_gltf_state, base_reference_encoded_bytes, p_main_buffer_view_target, 0, p_deduplicate);
  1613. }
  1614. // Dictionary conversion.
  1615. Ref<GLTFAccessor> GLTFAccessor::from_dictionary(const Dictionary &p_dict) {
  1616. // See https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/accessor.schema.json
  1617. Ref<GLTFAccessor> accessor;
  1618. accessor.instantiate();
  1619. if (p_dict.has("bufferView")) {
  1620. // bufferView is optional. If not present, the accessor is considered to be zero-initialized.
  1621. accessor->buffer_view = p_dict["bufferView"];
  1622. }
  1623. if (p_dict.has("byteOffset")) {
  1624. accessor->byte_offset = p_dict["byteOffset"];
  1625. }
  1626. if (p_dict.has("componentType")) {
  1627. accessor->component_type = (GLTFAccessor::GLTFComponentType)(int32_t)p_dict["componentType"];
  1628. }
  1629. if (p_dict.has("count")) {
  1630. accessor->count = p_dict["count"];
  1631. }
  1632. if (accessor->count <= 0) {
  1633. ERR_PRINT("glTF import: Invalid accessor count " + itos(accessor->count) + " for accessor. Accessor count must be greater than 0.");
  1634. }
  1635. if (p_dict.has("max")) {
  1636. accessor->max = p_dict["max"];
  1637. }
  1638. if (p_dict.has("min")) {
  1639. accessor->min = p_dict["min"];
  1640. }
  1641. if (p_dict.has("normalized")) {
  1642. accessor->normalized = p_dict["normalized"];
  1643. }
  1644. if (p_dict.has("sparse")) {
  1645. // See https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/accessor.sparse.schema.json
  1646. const Dictionary &sparse_dict = p_dict["sparse"];
  1647. ERR_FAIL_COND_V(!sparse_dict.has("count"), accessor);
  1648. accessor->sparse_count = sparse_dict["count"];
  1649. ERR_FAIL_COND_V(!sparse_dict.has("indices"), accessor);
  1650. const Dictionary &sparse_indices_dict = sparse_dict["indices"];
  1651. ERR_FAIL_COND_V(!sparse_indices_dict.has("bufferView"), accessor);
  1652. accessor->sparse_indices_buffer_view = sparse_indices_dict["bufferView"];
  1653. ERR_FAIL_COND_V(!sparse_indices_dict.has("componentType"), accessor);
  1654. accessor->sparse_indices_component_type = (GLTFAccessor::GLTFComponentType)(int32_t)sparse_indices_dict["componentType"];
  1655. if (sparse_indices_dict.has("byteOffset")) {
  1656. accessor->sparse_indices_byte_offset = sparse_indices_dict["byteOffset"];
  1657. }
  1658. ERR_FAIL_COND_V(!sparse_dict.has("values"), accessor);
  1659. const Dictionary &sparse_values_dict = sparse_dict["values"];
  1660. ERR_FAIL_COND_V(!sparse_values_dict.has("bufferView"), accessor);
  1661. accessor->sparse_values_buffer_view = sparse_values_dict["bufferView"];
  1662. if (sparse_values_dict.has("byteOffset")) {
  1663. accessor->sparse_values_byte_offset = sparse_values_dict["byteOffset"];
  1664. }
  1665. }
  1666. accessor->accessor_type = _get_accessor_type_from_str(p_dict["type"]);
  1667. return accessor;
  1668. }
  1669. Dictionary GLTFAccessor::to_dictionary() const {
  1670. Dictionary dict;
  1671. if (buffer_view != -1) {
  1672. // bufferView may be omitted to zero-initialize the buffer. When this happens, byteOffset MUST also be omitted.
  1673. if (byte_offset > 0) {
  1674. dict["byteOffset"] = byte_offset;
  1675. }
  1676. dict["bufferView"] = buffer_view;
  1677. }
  1678. dict["componentType"] = component_type;
  1679. dict["count"] = count;
  1680. switch (component_type) {
  1681. case COMPONENT_TYPE_NONE: {
  1682. ERR_PRINT("glTF export: Invalid component type 'NONE' for glTF accessor.");
  1683. } break;
  1684. case COMPONENT_TYPE_SIGNED_BYTE:
  1685. case COMPONENT_TYPE_UNSIGNED_BYTE:
  1686. case COMPONENT_TYPE_SIGNED_SHORT:
  1687. case COMPONENT_TYPE_UNSIGNED_SHORT:
  1688. case COMPONENT_TYPE_SIGNED_INT:
  1689. case COMPONENT_TYPE_UNSIGNED_INT:
  1690. case COMPONENT_TYPE_SIGNED_LONG:
  1691. case COMPONENT_TYPE_UNSIGNED_LONG: {
  1692. dict["max"] = PackedInt64Array(Variant(max));
  1693. dict["min"] = PackedInt64Array(Variant(min));
  1694. } break;
  1695. case COMPONENT_TYPE_SINGLE_FLOAT:
  1696. case COMPONENT_TYPE_DOUBLE_FLOAT:
  1697. case COMPONENT_TYPE_HALF_FLOAT: {
  1698. dict["max"] = max;
  1699. dict["min"] = min;
  1700. } break;
  1701. }
  1702. dict["normalized"] = normalized;
  1703. dict["type"] = _get_accessor_type_name();
  1704. if (sparse_count > 0) {
  1705. Dictionary sparse_indices_dict;
  1706. sparse_indices_dict["bufferView"] = sparse_indices_buffer_view;
  1707. sparse_indices_dict["componentType"] = sparse_indices_component_type;
  1708. if (sparse_indices_byte_offset > 0) {
  1709. sparse_indices_dict["byteOffset"] = sparse_indices_byte_offset;
  1710. }
  1711. Dictionary sparse_values_dict;
  1712. sparse_values_dict["bufferView"] = sparse_values_buffer_view;
  1713. if (sparse_values_byte_offset > 0) {
  1714. sparse_values_dict["byteOffset"] = sparse_values_byte_offset;
  1715. }
  1716. Dictionary sparse_dict;
  1717. sparse_dict["count"] = sparse_count;
  1718. sparse_dict["indices"] = sparse_indices_dict;
  1719. sparse_dict["values"] = sparse_values_dict;
  1720. dict["sparse"] = sparse_dict;
  1721. }
  1722. return dict;
  1723. }