Path: blob/master/modules/gltf/structures/gltf_accessor.cpp
20897 views
/**************************************************************************/1/* gltf_accessor.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "gltf_accessor.h"31#include "gltf_accessor.compat.inc"3233#include "../gltf_state.h"3435void GLTFAccessor::_bind_methods() {36BIND_ENUM_CONSTANT(TYPE_SCALAR);37BIND_ENUM_CONSTANT(TYPE_VEC2);38BIND_ENUM_CONSTANT(TYPE_VEC3);39BIND_ENUM_CONSTANT(TYPE_VEC4);40BIND_ENUM_CONSTANT(TYPE_MAT2);41BIND_ENUM_CONSTANT(TYPE_MAT3);42BIND_ENUM_CONSTANT(TYPE_MAT4);4344BIND_ENUM_CONSTANT(COMPONENT_TYPE_NONE);45BIND_ENUM_CONSTANT(COMPONENT_TYPE_SIGNED_BYTE);46BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_BYTE);47BIND_ENUM_CONSTANT(COMPONENT_TYPE_SIGNED_SHORT);48BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_SHORT);49BIND_ENUM_CONSTANT(COMPONENT_TYPE_SIGNED_INT);50BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_INT);51BIND_ENUM_CONSTANT(COMPONENT_TYPE_SINGLE_FLOAT);52BIND_ENUM_CONSTANT(COMPONENT_TYPE_DOUBLE_FLOAT);53BIND_ENUM_CONSTANT(COMPONENT_TYPE_HALF_FLOAT);54BIND_ENUM_CONSTANT(COMPONENT_TYPE_SIGNED_LONG);55BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_LONG);5657ClassDB::bind_static_method("GLTFAccessor", D_METHOD("from_dictionary", "dictionary"), &GLTFAccessor::from_dictionary);58ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFAccessor::to_dictionary);5960ClassDB::bind_method(D_METHOD("get_buffer_view"), &GLTFAccessor::get_buffer_view);61ClassDB::bind_method(D_METHOD("set_buffer_view", "buffer_view"), &GLTFAccessor::set_buffer_view);62ClassDB::bind_method(D_METHOD("get_byte_offset"), &GLTFAccessor::get_byte_offset);63ClassDB::bind_method(D_METHOD("set_byte_offset", "byte_offset"), &GLTFAccessor::set_byte_offset);64ClassDB::bind_method(D_METHOD("get_component_type"), &GLTFAccessor::get_component_type);65ClassDB::bind_method(D_METHOD("set_component_type", "component_type"), &GLTFAccessor::set_component_type);66ClassDB::bind_method(D_METHOD("get_normalized"), &GLTFAccessor::get_normalized);67ClassDB::bind_method(D_METHOD("set_normalized", "normalized"), &GLTFAccessor::set_normalized);68ClassDB::bind_method(D_METHOD("get_count"), &GLTFAccessor::get_count);69ClassDB::bind_method(D_METHOD("set_count", "count"), &GLTFAccessor::set_count);70ClassDB::bind_method(D_METHOD("get_accessor_type"), &GLTFAccessor::get_accessor_type);71ClassDB::bind_method(D_METHOD("set_accessor_type", "accessor_type"), &GLTFAccessor::set_accessor_type);72ClassDB::bind_method(D_METHOD("get_type"), &GLTFAccessor::get_type);73ClassDB::bind_method(D_METHOD("set_type", "type"), &GLTFAccessor::set_type);74ClassDB::bind_method(D_METHOD("get_min"), &GLTFAccessor::get_min);75ClassDB::bind_method(D_METHOD("set_min", "min"), &GLTFAccessor::set_min);76ClassDB::bind_method(D_METHOD("get_max"), &GLTFAccessor::get_max);77ClassDB::bind_method(D_METHOD("set_max", "max"), &GLTFAccessor::set_max);78ClassDB::bind_method(D_METHOD("get_sparse_count"), &GLTFAccessor::get_sparse_count);79ClassDB::bind_method(D_METHOD("set_sparse_count", "sparse_count"), &GLTFAccessor::set_sparse_count);80ClassDB::bind_method(D_METHOD("get_sparse_indices_buffer_view"), &GLTFAccessor::get_sparse_indices_buffer_view);81ClassDB::bind_method(D_METHOD("set_sparse_indices_buffer_view", "sparse_indices_buffer_view"), &GLTFAccessor::set_sparse_indices_buffer_view);82ClassDB::bind_method(D_METHOD("get_sparse_indices_byte_offset"), &GLTFAccessor::get_sparse_indices_byte_offset);83ClassDB::bind_method(D_METHOD("set_sparse_indices_byte_offset", "sparse_indices_byte_offset"), &GLTFAccessor::set_sparse_indices_byte_offset);84ClassDB::bind_method(D_METHOD("get_sparse_indices_component_type"), &GLTFAccessor::get_sparse_indices_component_type);85ClassDB::bind_method(D_METHOD("set_sparse_indices_component_type", "sparse_indices_component_type"), &GLTFAccessor::set_sparse_indices_component_type);86ClassDB::bind_method(D_METHOD("get_sparse_values_buffer_view"), &GLTFAccessor::get_sparse_values_buffer_view);87ClassDB::bind_method(D_METHOD("set_sparse_values_buffer_view", "sparse_values_buffer_view"), &GLTFAccessor::set_sparse_values_buffer_view);88ClassDB::bind_method(D_METHOD("get_sparse_values_byte_offset"), &GLTFAccessor::get_sparse_values_byte_offset);89ClassDB::bind_method(D_METHOD("set_sparse_values_byte_offset", "sparse_values_byte_offset"), &GLTFAccessor::set_sparse_values_byte_offset);9091ADD_PROPERTY(PropertyInfo(Variant::INT, "buffer_view"), "set_buffer_view", "get_buffer_view"); // GLTFBufferViewIndex92ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_offset"), "set_byte_offset", "get_byte_offset"); // int93ADD_PROPERTY(PropertyInfo(Variant::INT, "component_type"), "set_component_type", "get_component_type"); // int94ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalized"), "set_normalized", "get_normalized"); // bool95ADD_PROPERTY(PropertyInfo(Variant::INT, "count"), "set_count", "get_count"); // int96ADD_PROPERTY(PropertyInfo(Variant::INT, "accessor_type"), "set_accessor_type", "get_accessor_type"); // GLTFAccessor::GLTFAccessorType97ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_type", "get_type"); // Deprecated, int for GLTFAccessor::GLTFAccessorType98ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT64_ARRAY, "min"), "set_min", "get_min"); // Vector<real_t>99ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT64_ARRAY, "max"), "set_max", "get_max"); // Vector<real_t>100ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_count"), "set_sparse_count", "get_sparse_count"); // int101ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_indices_buffer_view"), "set_sparse_indices_buffer_view", "get_sparse_indices_buffer_view"); // int102ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_indices_byte_offset"), "set_sparse_indices_byte_offset", "get_sparse_indices_byte_offset"); // int103ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_indices_component_type"), "set_sparse_indices_component_type", "get_sparse_indices_component_type"); // int104ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_values_buffer_view"), "set_sparse_values_buffer_view", "get_sparse_values_buffer_view"); // int105ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_values_byte_offset"), "set_sparse_values_byte_offset", "get_sparse_values_byte_offset"); // int106}107108// Property getters and setters.109110GLTFBufferViewIndex GLTFAccessor::get_buffer_view() const {111return buffer_view;112}113114void GLTFAccessor::set_buffer_view(GLTFBufferViewIndex p_buffer_view) {115buffer_view = p_buffer_view;116}117118int64_t GLTFAccessor::get_byte_offset() const {119return byte_offset;120}121122void GLTFAccessor::set_byte_offset(int64_t p_byte_offset) {123byte_offset = p_byte_offset;124}125126GLTFAccessor::GLTFComponentType GLTFAccessor::get_component_type() const {127return component_type;128}129130void GLTFAccessor::set_component_type(GLTFComponentType p_component_type) {131component_type = (GLTFComponentType)p_component_type;132}133134bool GLTFAccessor::get_normalized() const {135return normalized;136}137138void GLTFAccessor::set_normalized(bool p_normalized) {139normalized = p_normalized;140}141142int64_t GLTFAccessor::get_count() const {143return count;144}145146void GLTFAccessor::set_count(int64_t p_count) {147count = p_count;148}149150GLTFAccessor::GLTFAccessorType GLTFAccessor::get_accessor_type() const {151return accessor_type;152}153154void GLTFAccessor::set_accessor_type(GLTFAccessorType p_accessor_type) {155accessor_type = p_accessor_type;156}157158int GLTFAccessor::get_type() const {159return (int)accessor_type;160}161162void GLTFAccessor::set_type(int p_accessor_type) {163accessor_type = (GLTFAccessorType)p_accessor_type; // TODO: Register enum164}165166Vector<double> GLTFAccessor::get_min() const {167return Vector<double>(min);168}169170void GLTFAccessor::set_min(const Vector<double> &p_min) {171min = Vector<double>(p_min);172}173174Vector<double> GLTFAccessor::get_max() const {175return Vector<double>(max);176}177178void GLTFAccessor::set_max(const Vector<double> &p_max) {179max = Vector<double>(p_max);180}181182int64_t GLTFAccessor::get_sparse_count() const {183return sparse_count;184}185186void GLTFAccessor::set_sparse_count(int64_t p_sparse_count) {187sparse_count = p_sparse_count;188}189190GLTFBufferViewIndex GLTFAccessor::get_sparse_indices_buffer_view() const {191return sparse_indices_buffer_view;192}193194void GLTFAccessor::set_sparse_indices_buffer_view(GLTFBufferViewIndex p_sparse_indices_buffer_view) {195sparse_indices_buffer_view = p_sparse_indices_buffer_view;196}197198int64_t GLTFAccessor::get_sparse_indices_byte_offset() const {199return sparse_indices_byte_offset;200}201202void GLTFAccessor::set_sparse_indices_byte_offset(int64_t p_sparse_indices_byte_offset) {203sparse_indices_byte_offset = p_sparse_indices_byte_offset;204}205206GLTFAccessor::GLTFComponentType GLTFAccessor::get_sparse_indices_component_type() const {207return sparse_indices_component_type;208}209210void GLTFAccessor::set_sparse_indices_component_type(GLTFComponentType p_sparse_indices_component_type) {211sparse_indices_component_type = (GLTFComponentType)p_sparse_indices_component_type;212}213214GLTFBufferViewIndex GLTFAccessor::get_sparse_values_buffer_view() const {215return sparse_values_buffer_view;216}217218void GLTFAccessor::set_sparse_values_buffer_view(GLTFBufferViewIndex p_sparse_values_buffer_view) {219sparse_values_buffer_view = p_sparse_values_buffer_view;220}221222int64_t GLTFAccessor::get_sparse_values_byte_offset() const {223return sparse_values_byte_offset;224}225226void GLTFAccessor::set_sparse_values_byte_offset(int64_t p_sparse_values_byte_offset) {227sparse_values_byte_offset = p_sparse_values_byte_offset;228}229230// Trivial helper functions.231232void GLTFAccessor::_calculate_min_and_max(const PackedFloat64Array &p_numbers) {233const int64_t vector_size = _get_vector_size();234ERR_FAIL_COND(vector_size <= 0 || p_numbers.size() % vector_size != 0);235min.resize(vector_size);236max.resize(vector_size);237// Initialize min and max with the first vector element values.238for (int64_t in_vec = 0; in_vec < vector_size; in_vec++) {239min.write[in_vec] = p_numbers[in_vec];240max.write[in_vec] = p_numbers[in_vec];241}242// Iterate over the rest of the vectors.243for (int64_t which_vec = vector_size; which_vec < p_numbers.size(); which_vec += vector_size) {244for (int64_t in_vec = 0; in_vec < vector_size; in_vec++) {245min.write[in_vec] = MIN(p_numbers[which_vec + in_vec], min[in_vec]);246max.write[in_vec] = MAX(p_numbers[which_vec + in_vec], max[in_vec]);247}248}249// 3.6.2.5: For floating-point components, JSON-stored minimum and maximum values represent single precision250// floats and SHOULD be rounded to single precision before usage to avoid any potential boundary mismatches.251// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#accessors-bounds252if (component_type == GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT) {253for (int64_t i = 0; i < min.size(); i++) {254min.write[i] = (double)(float)min[i];255max.write[i] = (double)(float)max[i];256}257}258}259260void GLTFAccessor::_determine_pad_skip(int64_t &r_skip_every, int64_t &r_skip_bytes) const {261// 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.262// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#data-alignment263switch (component_type) {264case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE:265case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {266if (accessor_type == GLTFAccessor::TYPE_MAT2) {267r_skip_every = 2;268r_skip_bytes = 2;269}270if (accessor_type == GLTFAccessor::TYPE_MAT3) {271r_skip_every = 3;272r_skip_bytes = 1;273}274} break;275case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:276case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {277if (accessor_type == GLTFAccessor::TYPE_MAT3) {278r_skip_every = 6;279r_skip_bytes = 2;280}281} break;282default: {283} break;284}285}286287int64_t GLTFAccessor::_determine_padded_byte_count(int64_t p_raw_byte_size) const {288// 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.289// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#data-alignment290switch (component_type) {291case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE:292case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {293if (accessor_type == GLTFAccessor::TYPE_MAT2) {294return p_raw_byte_size * 2;295}296if (accessor_type == GLTFAccessor::TYPE_MAT3) {297return p_raw_byte_size * 4 / 3;298}299} break;300case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:301case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {302if (accessor_type == GLTFAccessor::TYPE_MAT3) {303return p_raw_byte_size * 4 / 3;304}305} break;306default: {307} break;308}309return p_raw_byte_size;310}311312PackedFloat64Array GLTFAccessor::_filter_numbers(const PackedFloat64Array &p_numbers) const {313PackedFloat64Array filtered_numbers = p_numbers;314for (int64_t i = 0; i < p_numbers.size(); i++) {315const double num = p_numbers[i];316if (!Math::is_finite(num)) {317// 3.6.2.2. "Values of NaN, +Infinity, and -Infinity MUST NOT be present."318// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#accessor-data-types319filtered_numbers.set(i, 0.0);320} else if (component_type == GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT) {321filtered_numbers.set(i, (double)(float)num);322}323}324return filtered_numbers;325}326327String GLTFAccessor::_get_component_type_name(const GLTFComponentType p_component) {328// These names are only for debugging and printing error messages, glTF uses the numeric values.329switch (p_component) {330case GLTFAccessor::COMPONENT_TYPE_NONE:331return "None";332case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE:333return "Byte";334case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE:335return "UByte";336case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:337return "Short";338case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT:339return "UShort";340case GLTFAccessor::COMPONENT_TYPE_SIGNED_INT:341return "Int";342case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT:343return "UInt";344case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT:345return "Float";346case GLTFAccessor::COMPONENT_TYPE_DOUBLE_FLOAT:347return "Double";348case GLTFAccessor::COMPONENT_TYPE_HALF_FLOAT:349return "Half";350case GLTFAccessor::COMPONENT_TYPE_SIGNED_LONG:351return "Long";352case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG:353return "ULong";354}355356return "<Error>";357}358359GLTFAccessor::GLTFComponentType GLTFAccessor::_get_indices_component_type_for_size(const int64_t p_size) {360ERR_FAIL_COND_V(p_size < 0, GLTFAccessor::COMPONENT_TYPE_NONE);361// 3.7.2.1. indices accessor MUST NOT contain the maximum possible value for the component type used362// (i.e., 255 for unsigned bytes, 65535 for unsigned shorts, 4294967295 for unsigned ints).363// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview364if (unlikely(p_size > 4294967294LL)) {365return GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG;366}367if (p_size > 65534LL) {368return GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT;369}370if (p_size > 254LL) {371return GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT;372}373return GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE;374}375376GLTFAccessor::GLTFAccessorType GLTFAccessor::_get_accessor_type_from_str(const String &p_string) {377if (p_string == "SCALAR") {378return GLTFAccessor::TYPE_SCALAR;379}380if (p_string == "VEC2") {381return GLTFAccessor::TYPE_VEC2;382}383if (p_string == "VEC3") {384return GLTFAccessor::TYPE_VEC3;385}386if (p_string == "VEC4") {387return GLTFAccessor::TYPE_VEC4;388}389if (p_string == "MAT2") {390return GLTFAccessor::TYPE_MAT2;391}392if (p_string == "MAT3") {393return GLTFAccessor::TYPE_MAT3;394}395if (p_string == "MAT4") {396return GLTFAccessor::TYPE_MAT4;397}398ERR_FAIL_V(GLTFAccessor::TYPE_SCALAR);399}400401String GLTFAccessor::_get_accessor_type_name() const {402switch (accessor_type) {403case GLTFAccessor::TYPE_SCALAR:404return "SCALAR";405case GLTFAccessor::TYPE_VEC2:406return "VEC2";407case GLTFAccessor::TYPE_VEC3:408return "VEC3";409case GLTFAccessor::TYPE_VEC4:410return "VEC4";411case GLTFAccessor::TYPE_MAT2:412return "MAT2";413case GLTFAccessor::TYPE_MAT3:414return "MAT3";415case GLTFAccessor::TYPE_MAT4:416return "MAT4";417default:418break;419}420ERR_FAIL_V("SCALAR");421}422423int64_t GLTFAccessor::_get_vector_size() const {424switch (accessor_type) {425case GLTFAccessor::TYPE_SCALAR:426return 1;427case GLTFAccessor::TYPE_VEC2:428return 2;429case GLTFAccessor::TYPE_VEC3:430return 3;431case GLTFAccessor::TYPE_VEC4:432return 4;433case GLTFAccessor::TYPE_MAT2:434return 4;435case GLTFAccessor::TYPE_MAT3:436return 9;437case GLTFAccessor::TYPE_MAT4:438return 16;439default:440break;441}442ERR_FAIL_V(0);443}444445int64_t GLTFAccessor::_get_numbers_per_variant_for_gltf(Variant::Type p_variant_type) {446// Note that these numbers are used to determine the size of the glTF accessor appropriate for the type (see `_get_vector_size`).447// Therefore, the only valid values this can return are 1 (SCALAR), 2 (VEC2), 3 (VEC3), 4 (VEC4/MAT2), 9 (MAT3), and 16 (MAT4).448// The value 0 indicates the Variant type can't map to glTF accessors, and INT64_MAX indicates it needs special handling.449switch (p_variant_type) {450case Variant::NIL:451case Variant::STRING:452case Variant::STRING_NAME:453case Variant::NODE_PATH:454case Variant::RID:455case Variant::OBJECT:456case Variant::CALLABLE:457case Variant::SIGNAL:458case Variant::DICTIONARY:459case Variant::ARRAY:460case Variant::PACKED_STRING_ARRAY:461case Variant::PACKED_VECTOR2_ARRAY:462case Variant::PACKED_VECTOR3_ARRAY:463case Variant::PACKED_COLOR_ARRAY:464case Variant::PACKED_VECTOR4_ARRAY:465case Variant::VARIANT_MAX:466return 0; // Not supported.467case Variant::BOOL:468case Variant::INT:469case Variant::FLOAT:470return 1;471case Variant::VECTOR2:472case Variant::VECTOR2I:473return 2;474case Variant::VECTOR3:475case Variant::VECTOR3I:476return 3;477case Variant::RECT2:478case Variant::RECT2I:479case Variant::VECTOR4:480case Variant::VECTOR4I:481case Variant::PLANE:482case Variant::QUATERNION:483case Variant::COLOR:484return 4;485case Variant::TRANSFORM2D:486case Variant::AABB:487case Variant::BASIS:488return 9;489case Variant::TRANSFORM3D:490case Variant::PROJECTION:491return 16;492case Variant::PACKED_BYTE_ARRAY:493case Variant::PACKED_INT32_ARRAY:494case Variant::PACKED_INT64_ARRAY:495case Variant::PACKED_FLOAT32_ARRAY:496case Variant::PACKED_FLOAT64_ARRAY:497return INT64_MAX; // Special, use `_get_vector_size()` only to determine size.498}499return 0;500}501502int64_t GLTFAccessor::_get_bytes_per_component(const GLTFComponentType p_component_type) {503switch (p_component_type) {504case GLTFAccessor::COMPONENT_TYPE_NONE:505ERR_FAIL_V(0);506case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE:507case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE:508return 1;509case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:510case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT:511case GLTFAccessor::COMPONENT_TYPE_HALF_FLOAT:512return 2;513case GLTFAccessor::COMPONENT_TYPE_SIGNED_INT:514case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT:515case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT:516return 4;517case GLTFAccessor::COMPONENT_TYPE_DOUBLE_FLOAT:518case GLTFAccessor::COMPONENT_TYPE_SIGNED_LONG:519case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG:520return 8;521}522ERR_FAIL_V(0);523}524525int64_t GLTFAccessor::_get_bytes_per_vector() const {526const int64_t raw_byte_size = _get_bytes_per_component(component_type) * _get_vector_size();527return _determine_padded_byte_count(raw_byte_size);528}529530bool GLTFAccessor::is_equal_exact(const Ref<GLTFAccessor> &p_other) const {531if (p_other.is_null()) {532return false;533}534return (buffer_view == p_other->buffer_view &&535byte_offset == p_other->byte_offset &&536component_type == p_other->component_type &&537normalized == p_other->normalized &&538count == p_other->count &&539accessor_type == p_other->accessor_type &&540min == p_other->min &&541max == p_other->max &&542sparse_count == p_other->sparse_count &&543sparse_indices_buffer_view == p_other->sparse_indices_buffer_view &&544sparse_indices_byte_offset == p_other->sparse_indices_byte_offset &&545sparse_indices_component_type == p_other->sparse_indices_component_type &&546sparse_values_buffer_view == p_other->sparse_values_buffer_view &&547sparse_values_byte_offset == p_other->sparse_values_byte_offset);548}549550// Private decode functions.551552PackedInt64Array GLTFAccessor::_decode_sparse_indices(const Ref<GLTFState> &p_gltf_state, const Vector<Ref<GLTFBufferView>> &p_buffer_views) const {553const int64_t bytes_per_component = _get_bytes_per_component(sparse_indices_component_type);554PackedInt64Array numbers;555ERR_FAIL_INDEX_V(sparse_indices_buffer_view, p_buffer_views.size(), numbers);556const Ref<GLTFBufferView> actual_buffer_view = p_buffer_views[sparse_indices_buffer_view];557const PackedByteArray raw_bytes = actual_buffer_view->load_buffer_view_data(p_gltf_state);558const int64_t min_raw_byte_size = bytes_per_component * sparse_count + sparse_indices_byte_offset;559ERR_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.");560numbers.resize(sparse_count);561const uint8_t *raw_pointer = raw_bytes.ptr();562int64_t raw_read_offset = sparse_indices_byte_offset;563for (int64_t i = 0; i < sparse_count; i++) {564const uint8_t *raw_source = &raw_pointer[raw_read_offset];565int64_t number = 0;566switch (sparse_indices_component_type) {567case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {568number = *(uint8_t *)raw_source;569} break;570case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {571number = *(uint16_t *)raw_source;572} break;573case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT: {574number = *(uint32_t *)raw_source;575} break;576case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG: {577number = *(uint64_t *)raw_source;578} break;579default: {580ERR_FAIL_V_MSG(PackedInt64Array(), "glTF import: Sparse indices must have an unsigned integer component type. Failed to decode, returning an empty array.");581}582}583numbers.set(i, number);584raw_read_offset += bytes_per_component;585}586ERR_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.");587return numbers;588}589590template <typename T>591Vector<T> GLTFAccessor::_decode_raw_numbers(const Ref<GLTFState> &p_gltf_state, const Vector<Ref<GLTFBufferView>> &p_buffer_views, bool p_sparse_values) const {592const int64_t bytes_per_component = _get_bytes_per_component(component_type);593const int64_t bytes_per_vector = _get_bytes_per_vector();594const int64_t vector_size = _get_vector_size();595int64_t pad_skip_every = 0;596int64_t pad_skip_bytes = 0;597_determine_pad_skip(pad_skip_every, pad_skip_bytes);598int64_t raw_vector_count;599int64_t raw_buffer_view_index;600int64_t raw_read_offset_start;601if (p_sparse_values) {602raw_vector_count = sparse_count;603raw_buffer_view_index = sparse_values_buffer_view;604raw_read_offset_start = sparse_values_byte_offset;605} else {606raw_vector_count = count;607raw_buffer_view_index = buffer_view;608raw_read_offset_start = byte_offset;609}610const int64_t raw_number_count = raw_vector_count * vector_size;611Vector<T> ret_numbers;612if (raw_buffer_view_index == -1) {613ret_numbers.resize(raw_number_count);614// No buffer view, so fill with zeros.615for (int64_t i = 0; i < raw_number_count; i++) {616ret_numbers.set(i, T(0));617}618return ret_numbers;619}620ERR_FAIL_INDEX_V(raw_buffer_view_index, p_buffer_views.size(), ret_numbers);621const Ref<GLTFBufferView> raw_buffer_view = p_buffer_views[raw_buffer_view_index];622if (raw_buffer_view->get_byte_offset() % bytes_per_component != 0) {623WARN_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.");624}625if (byte_offset % bytes_per_component != 0) {626WARN_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.");627}628int64_t declared_byte_stride = raw_buffer_view->get_byte_stride();629int64_t actual_byte_stride = bytes_per_vector;630int64_t stride_skip_every = 0;631int64_t stride_skip_bytes = 0;632if (declared_byte_stride != -1) {633ERR_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.");634if (declared_byte_stride > bytes_per_vector) {635actual_byte_stride = declared_byte_stride;636stride_skip_every = vector_size;637stride_skip_bytes = declared_byte_stride - bytes_per_vector;638}639} else if (raw_buffer_view->get_vertex_attributes()) {640print_verbose("WARNING: glTF import: Buffer view byte stride should be declared for vertex attributes. Assuming packed data and reading anyway.");641}642const int64_t min_raw_byte_size = actual_byte_stride * (raw_vector_count - 1) + bytes_per_vector + raw_read_offset_start;643const PackedByteArray raw_bytes = raw_buffer_view->load_buffer_view_data(p_gltf_state);644ERR_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.");645ret_numbers.resize(raw_number_count);646const uint8_t *raw_pointer = raw_bytes.ptr();647int64_t raw_read_offset = raw_read_offset_start;648for (int64_t i = 0; i < raw_number_count; i++) {649const uint8_t *raw_source = &raw_pointer[raw_read_offset];650T number = 0;651// 3.11. Implementations MUST use following equations to decode real floating-point value f from a normalized integer c and vice-versa.652// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#animations653switch (component_type) {654case GLTFAccessor::COMPONENT_TYPE_NONE: {655ERR_FAIL_V_MSG(Vector<T>(), "glTF import: Failed to decode buffer view, component type not set. Returning an empty array.");656} break;657case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE: {658int8_t prim = *(int8_t *)raw_source;659if (normalized) {660number = T(MAX(double(prim) / 127.0, -1.0));661} else {662number = T(prim);663}664} break;665case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {666uint8_t prim = *(uint8_t *)raw_source;667if (normalized) {668number = T((double(prim) / 255.0));669} else {670number = T(prim);671}672} break;673case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT: {674int16_t prim = *(int16_t *)raw_source;675if (normalized) {676number = T(MAX(double(prim) / 32767.0, -1.0));677} else {678number = T(prim);679}680} break;681case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {682uint16_t prim = *(uint16_t *)raw_source;683if (normalized) {684number = T(double(prim) / 65535.0);685} else {686number = T(prim);687}688} break;689case GLTFAccessor::COMPONENT_TYPE_SIGNED_INT: {690number = T(*(int32_t *)raw_source);691} break;692case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT: {693number = T(*(uint32_t *)raw_source);694} break;695case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT: {696number = T(*(float *)raw_source);697} break;698case GLTFAccessor::COMPONENT_TYPE_DOUBLE_FLOAT: {699number = T(*(double *)raw_source);700} break;701case GLTFAccessor::COMPONENT_TYPE_HALF_FLOAT: {702number = Math::half_to_float(*(uint16_t *)raw_source);703} break;704case GLTFAccessor::COMPONENT_TYPE_SIGNED_LONG: {705number = T(*(int64_t *)raw_source);706} break;707case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG: {708number = T(*(uint64_t *)raw_source);709} break;710}711ret_numbers.set(i, number);712raw_read_offset += bytes_per_component;713// Padding and stride skipping are distinct concepts that both need to be handled.714// For example, a 2-in-1 interleaved MAT3 bytes accessor has both, and would look like:715// AAA0 AAA0 AAA0 BBB0 BBB0 BBB0 AAA0 AAA0 AAA0 BBB0 BBB0 BBB0716// The "0" is skipped by the padding, and the "BBB0" is skipped by the stride.717// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#data-alignment718if (unlikely(pad_skip_every > 0)) {719if ((i + 1) % pad_skip_every == 0) {720raw_read_offset += pad_skip_bytes;721}722}723if (unlikely(stride_skip_every > 0)) {724if ((i + 1) % stride_skip_every == 0) {725raw_read_offset += stride_skip_bytes;726}727}728}729return ret_numbers;730}731732template <typename T>733Vector<T> GLTFAccessor::_decode_as_numbers(const Ref<GLTFState> &p_gltf_state) const {734const Vector<Ref<GLTFBufferView>> &p_buffer_views = p_gltf_state->get_buffer_views();735Vector<T> ret_numbers = _decode_raw_numbers<T>(p_gltf_state, p_buffer_views, false);736if (sparse_count == 0) {737return ret_numbers;738}739// Handle sparse accessors.740PackedInt64Array sparse_indices = _decode_sparse_indices(p_gltf_state, p_buffer_views);741ERR_FAIL_COND_V_MSG(sparse_indices.size() != sparse_count, ret_numbers, "glTF import: Sparse indices size does not match the sparse count.");742const int64_t vector_size = _get_vector_size();743Vector<T> sparse_values = _decode_raw_numbers<T>(p_gltf_state, p_buffer_views, true);744ERR_FAIL_COND_V_MSG(sparse_values.size() != sparse_count * vector_size, ret_numbers, "glTF import: Sparse values size does not match the sparse count.");745for (int64_t in_sparse = 0; in_sparse < sparse_count; in_sparse++) {746const int64_t sparse_index = sparse_indices[in_sparse];747const int64_t array_offset = sparse_index * vector_size;748ERR_FAIL_INDEX_V_MSG(array_offset, ret_numbers.size(), ret_numbers, "glTF import: Sparse indices were out of bounds for the accessor.");749for (int64_t in_vec = 0; in_vec < vector_size; in_vec++) {750ret_numbers.set(array_offset + in_vec, sparse_values[in_sparse * vector_size + in_vec]);751}752}753return ret_numbers;754}755756// High-level decode functions.757758PackedColorArray GLTFAccessor::decode_as_colors(const Ref<GLTFState> &p_gltf_state) const {759PackedColorArray ret;760PackedFloat32Array numbers = _decode_as_numbers<float>(p_gltf_state);761if (accessor_type == TYPE_VEC3) {762ERR_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.");763ret.resize(count);764for (int64_t i = 0; i < count; i++) {765const int64_t number_index = i * 3;766ret.set(i, Color(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], 1.0f));767}768} else if (accessor_type == TYPE_VEC4) {769ERR_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.");770ret.resize(count);771for (int64_t i = 0; i < count; i++) {772const int64_t number_index = i * 4;773ret.set(i, Color(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], numbers[number_index + 3]));774}775} else {776ERR_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.");777}778return ret;779}780781PackedFloat32Array GLTFAccessor::decode_as_float32s(const Ref<GLTFState> &p_gltf_state) const {782return _decode_as_numbers<float>(p_gltf_state);783}784785PackedFloat64Array GLTFAccessor::decode_as_float64s(const Ref<GLTFState> &p_gltf_state) const {786return _decode_as_numbers<double>(p_gltf_state);787}788789PackedInt32Array GLTFAccessor::decode_as_int32s(const Ref<GLTFState> &p_gltf_state) const {790return _decode_as_numbers<int32_t>(p_gltf_state);791}792793PackedInt64Array GLTFAccessor::decode_as_int64s(const Ref<GLTFState> &p_gltf_state) const {794return _decode_as_numbers<int64_t>(p_gltf_state);795}796797Vector<Quaternion> GLTFAccessor::decode_as_quaternions(const Ref<GLTFState> &p_gltf_state) const {798Vector<Quaternion> ret;799ERR_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.");800PackedRealArray numbers = _decode_as_numbers<real_t>(p_gltf_state);801ERR_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.");802ret.resize(count);803for (int64_t i = 0; i < count; i++) {804const int64_t number_index = i * 4;805ret.set(i, Quaternion(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], numbers[number_index + 3]).normalized());806}807return ret;808}809810Array GLTFAccessor::decode_as_variants(const Ref<GLTFState> &p_gltf_state, Variant::Type p_variant_type) const {811const int64_t numbers_per_variant = _get_numbers_per_variant_for_gltf(p_variant_type);812Array ret;813ERR_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.");814const PackedFloat64Array numbers = _decode_as_numbers<double>(p_gltf_state);815const int64_t vector_size = _get_vector_size();816ERR_FAIL_COND_V_MSG(vector_size < 1, ret, "glTF import: The accessor type '" + _get_accessor_type_name() + "' is not supported. Returning an empty array.");817const int64_t numbers_to_read = MIN(vector_size, numbers_per_variant);818ERR_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.");819ret.resize(count);820for (int64_t value_index = 0; value_index < count; value_index++) {821const int64_t number_index = value_index * vector_size;822switch (p_variant_type) {823case Variant::BOOL: {824ret[value_index] = numbers[number_index] != 0.0;825} break;826case Variant::INT: {827ret[value_index] = (int64_t)numbers[number_index];828} break;829case Variant::FLOAT: {830ret[value_index] = numbers[number_index];831} break;832case Variant::VECTOR2:833case Variant::RECT2:834case Variant::VECTOR3:835case Variant::VECTOR4:836case Variant::PLANE:837case Variant::QUATERNION: {838// General-purpose code for importing glTF accessor data with any component count into structs up to 4 `real_t`s in size.839Vector4 vec;840switch (numbers_to_read) {841case 1: {842vec = Vector4(numbers[number_index], 0.0f, 0.0f, 0.0f);843} break;844case 2: {845vec = Vector4(numbers[number_index], numbers[number_index + 1], 0.0f, 0.0f);846} break;847case 3: {848vec = Vector4(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], 0.0f);849} break;850default: {851vec = Vector4(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], numbers[number_index + 3]);852} break;853}854if (p_variant_type == Variant::QUATERNION) {855vec.normalize();856}857// Evil hack that relies on the structure of Variant, but it's the858// only way to accomplish this without a ton of code duplication.859Variant variant = vec;860*(Variant::Type *)&variant = p_variant_type;861ret[value_index] = variant;862} break;863case Variant::VECTOR2I:864case Variant::RECT2I:865case Variant::VECTOR3I:866case Variant::VECTOR4I: {867// General-purpose code for importing glTF accessor data with any component count into structs up to 4 `int32_t`s in size.868Vector4i vec;869switch (numbers_to_read) {870case 1: {871vec = Vector4i((int32_t)numbers[number_index], 0, 0, 0);872} break;873case 2: {874vec = Vector4i((int32_t)numbers[number_index], (int32_t)numbers[number_index + 1], 0, 0);875} break;876case 3: {877vec = Vector4i((int32_t)numbers[number_index], (int32_t)numbers[number_index + 1], (int32_t)numbers[number_index + 2], 0);878} break;879default: {880vec = Vector4i((int32_t)numbers[number_index], (int32_t)numbers[number_index + 1], (int32_t)numbers[number_index + 2], (int32_t)numbers[number_index + 3]);881} break;882}883// Evil hack that relies on the structure of Variant, but it's the884// only way to accomplish this without a ton of code duplication.885Variant variant = vec;886*(Variant::Type *)&variant = p_variant_type;887ret[value_index] = variant;888} break;889// No more generalized hacks, each of the below types needs a lot of repetitive code.890case Variant::COLOR: {891Color color;892switch (numbers_to_read) {893case 1: {894color = Color(numbers[number_index], 0.0f, 0.0f, 1.0f);895} break;896case 2: {897color = Color(numbers[number_index], numbers[number_index + 1], 0.0f, 1.0f);898} break;899case 3: {900color = Color(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], 1.0f);901} break;902default: {903color = Color(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], numbers[number_index + 3]);904} break;905}906ret[value_index] = color;907} break;908case Variant::TRANSFORM2D: {909Transform2D t;910switch (numbers_to_read) {911case 4: {912t.columns[0] = Vector2(numbers[number_index + 0], numbers[number_index + 1]);913t.columns[1] = Vector2(numbers[number_index + 2], numbers[number_index + 3]);914} break;915case 9: {916t.columns[0] = Vector2(numbers[number_index + 0], numbers[number_index + 1]);917t.columns[1] = Vector2(numbers[number_index + 3], numbers[number_index + 4]);918t.columns[2] = Vector2(numbers[number_index + 6], numbers[number_index + 7]);919} break;920case 16: {921t.columns[0] = Vector2(numbers[number_index + 0], numbers[number_index + 1]);922t.columns[1] = Vector2(numbers[number_index + 4], numbers[number_index + 5]);923t.columns[2] = Vector2(numbers[number_index + 12], numbers[number_index + 13]);924} break;925}926ret[value_index] = t;927} break;928case Variant::AABB: {929AABB aabb;930switch (numbers_to_read) {931case 4: {932aabb.position = Vector3(numbers[number_index + 0], numbers[number_index + 1], 0.0f);933aabb.size = Vector3(numbers[number_index + 2], numbers[number_index + 3], 0.0f);934} break;935case 9: {936aabb.position = Vector3(numbers[number_index + 0], numbers[number_index + 1], numbers[number_index + 2]);937aabb.size = Vector3(numbers[number_index + 3], numbers[number_index + 4], numbers[number_index + 5]);938} break;939case 16: {940aabb.position = Vector3(numbers[number_index + 0], numbers[number_index + 1], numbers[number_index + 2]);941aabb.size = Vector3(numbers[number_index + 4], numbers[number_index + 5], numbers[number_index + 6]);942} break;943}944ret[value_index] = aabb;945} break;946case Variant::BASIS: {947Basis b;948switch (numbers_to_read) {949case 4: {950b.rows[0] = Vector3(numbers[number_index + 0], numbers[number_index + 2], 0.0f);951b.rows[1] = Vector3(numbers[number_index + 1], numbers[number_index + 3], 0.0f);952} break;953case 9: {954b.rows[0] = Vector3(numbers[number_index + 0], numbers[number_index + 3], numbers[number_index + 6]);955b.rows[1] = Vector3(numbers[number_index + 1], numbers[number_index + 4], numbers[number_index + 7]);956b.rows[2] = Vector3(numbers[number_index + 2], numbers[number_index + 5], numbers[number_index + 8]);957} break;958case 16: {959b.rows[0] = Vector3(numbers[number_index + 0], numbers[number_index + 4], numbers[number_index + 8]);960b.rows[1] = Vector3(numbers[number_index + 1], numbers[number_index + 5], numbers[number_index + 9]);961b.rows[2] = Vector3(numbers[number_index + 2], numbers[number_index + 6], numbers[number_index + 10]);962} break;963}964ret[value_index] = b;965} break;966case Variant::TRANSFORM3D: {967Transform3D t;968switch (numbers_to_read) {969case 4: {970t.basis.rows[0] = Vector3(numbers[number_index + 0], numbers[number_index + 2], 0.0f);971t.basis.rows[1] = Vector3(numbers[number_index + 1], numbers[number_index + 3], 0.0f);972} break;973case 9: {974t.basis.rows[0] = Vector3(numbers[number_index + 0], numbers[number_index + 3], numbers[number_index + 6]);975t.basis.rows[1] = Vector3(numbers[number_index + 1], numbers[number_index + 4], numbers[number_index + 7]);976t.basis.rows[2] = Vector3(numbers[number_index + 2], numbers[number_index + 5], numbers[number_index + 8]);977} break;978case 16: {979t.basis.rows[0] = Vector3(numbers[number_index + 0], numbers[number_index + 4], numbers[number_index + 8]);980t.basis.rows[1] = Vector3(numbers[number_index + 1], numbers[number_index + 5], numbers[number_index + 9]);981t.basis.rows[2] = Vector3(numbers[number_index + 2], numbers[number_index + 6], numbers[number_index + 10]);982t.origin = Vector3(numbers[number_index + 12], numbers[number_index + 13], numbers[number_index + 14]);983} break;984}985ret[value_index] = t;986} break;987case Variant::PROJECTION: {988Projection p;989switch (numbers_to_read) {990case 4: {991p.columns[0] = Vector4(numbers[number_index + 0], numbers[number_index + 1], 0.0f, 0.0f);992p.columns[1] = Vector4(numbers[number_index + 4], numbers[number_index + 5], 0.0f, 0.0f);993} break;994case 9: {995p.columns[0] = Vector4(numbers[number_index + 0], numbers[number_index + 1], numbers[number_index + 2], 0.0f);996p.columns[1] = Vector4(numbers[number_index + 4], numbers[number_index + 5], numbers[number_index + 6], 0.0f);997p.columns[2] = Vector4(numbers[number_index + 8], numbers[number_index + 9], numbers[number_index + 10], 0.0f);998} break;999case 16: {1000p.columns[0] = Vector4(numbers[number_index + 0], numbers[number_index + 1], numbers[number_index + 2], numbers[number_index + 3]);1001p.columns[1] = Vector4(numbers[number_index + 4], numbers[number_index + 5], numbers[number_index + 6], numbers[number_index + 7]);1002p.columns[2] = Vector4(numbers[number_index + 8], numbers[number_index + 9], numbers[number_index + 10], numbers[number_index + 11]);1003p.columns[3] = Vector4(numbers[number_index + 12], numbers[number_index + 13], numbers[number_index + 14], numbers[number_index + 15]);1004} break;1005}1006ret[value_index] = p;1007} break;1008case Variant::PACKED_BYTE_ARRAY: {1009PackedByteArray packed_array;1010packed_array.resize(numbers_to_read);1011for (int64_t j = 0; j < numbers_to_read; j++) {1012packed_array.set(value_index, numbers[number_index + j]);1013}1014} break;1015case Variant::PACKED_INT32_ARRAY: {1016PackedInt32Array packed_array;1017packed_array.resize(numbers_to_read);1018for (int64_t j = 0; j < numbers_to_read; j++) {1019packed_array.set(value_index, numbers[number_index + j]);1020}1021} break;1022case Variant::PACKED_INT64_ARRAY: {1023PackedInt64Array packed_array;1024packed_array.resize(numbers_to_read);1025for (int64_t j = 0; j < numbers_to_read; j++) {1026packed_array.set(value_index, numbers[number_index + j]);1027}1028} break;1029case Variant::PACKED_FLOAT32_ARRAY: {1030PackedFloat32Array packed_array;1031packed_array.resize(numbers_to_read);1032for (int64_t j = 0; j < numbers_to_read; j++) {1033packed_array.set(value_index, numbers[number_index + j]);1034}1035} break;1036case Variant::PACKED_FLOAT64_ARRAY: {1037PackedFloat64Array packed_array;1038packed_array.resize(numbers_to_read);1039for (int64_t j = 0; j < numbers_to_read; j++) {1040packed_array.set(value_index, numbers[number_index + j]);1041}1042} break;1043default: {1044ERR_FAIL_V_MSG(ret, "glTF: Cannot decode accessor as Variant of type " + Variant::get_type_name(p_variant_type) + ".");1045}1046}1047}1048return ret;1049}10501051PackedVector2Array GLTFAccessor::decode_as_vector2s(const Ref<GLTFState> &p_gltf_state) const {1052PackedVector2Array ret;1053ERR_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.");1054PackedRealArray numbers = _decode_as_numbers<real_t>(p_gltf_state);1055ERR_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.");1056ret.resize(count);1057for (int64_t i = 0; i < count; i++) {1058const int64_t number_index = i * 2;1059ret.set(i, Vector2(numbers[number_index], numbers[number_index + 1]));1060}1061return ret;1062}10631064PackedVector3Array GLTFAccessor::decode_as_vector3s(const Ref<GLTFState> &p_gltf_state) const {1065PackedVector3Array ret;1066ERR_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.");1067PackedRealArray numbers = _decode_as_numbers<real_t>(p_gltf_state);1068ERR_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.");1069ret.resize(count);1070for (int64_t i = 0; i < count; i++) {1071const int64_t number_index = i * 3;1072ret.set(i, Vector3(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2]));1073}1074return ret;1075}10761077PackedVector4Array GLTFAccessor::decode_as_vector4s(const Ref<GLTFState> &p_gltf_state) const {1078PackedVector4Array ret;1079ERR_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.");1080PackedRealArray numbers = _decode_as_numbers<real_t>(p_gltf_state);1081ERR_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.");1082ret.resize(count);1083for (int64_t i = 0; i < count; i++) {1084const int64_t number_index = i * 4;1085ret.set(i, Vector4(numbers[number_index], numbers[number_index + 1], numbers[number_index + 2], numbers[number_index + 3]));1086}1087return ret;1088}10891090// Private encode functions.10911092PackedFloat64Array GLTFAccessor::_encode_variants_as_floats(const Array &p_input_data, Variant::Type p_variant_type) const {1093const int64_t vector_size = _get_vector_size();1094const int64_t input_size = p_input_data.size();1095PackedFloat64Array numbers;1096numbers.resize(input_size * vector_size);1097for (int64_t input_index = 0; input_index < input_size; input_index++) {1098Variant variant = p_input_data[input_index];1099const int64_t vector_offset = input_index * vector_size;1100switch (p_variant_type) {1101case Variant::NIL:1102case Variant::BOOL:1103case Variant::INT:1104case Variant::FLOAT: {1105// For scalar values, just append them. Variant can convert all of these to double. Some padding may also be needed.1106numbers.set(vector_offset, variant);1107if (unlikely(vector_size > 1)) {1108for (int64_t i = 1; i < vector_size; i++) {1109numbers.set(vector_offset + i, 0.0);1110}1111}1112} break;1113case Variant::PLANE:1114case Variant::QUATERNION:1115case Variant::RECT2: {1116// Evil hack that relies on the structure of Variant, but it's the1117// only way to accomplish this without a ton of code duplication.1118*(Variant::Type *)&variant = Variant::VECTOR4;1119}1120[[fallthrough]];1121case Variant::VECTOR2:1122case Variant::VECTOR3:1123case Variant::VECTOR4: {1124// Variant can handle converting Vector2/3/4 to Vector4 for us.1125Vector4 vec = variant;1126for (int64_t i = 0; i < vector_size; i++) {1127numbers.set(vector_offset + i, vec[i]);1128}1129if (unlikely(vector_size > 4)) {1130for (int64_t i = 4; i < vector_size; i++) {1131numbers.set(vector_offset + i, 0.0);1132}1133}1134} break;1135case Variant::RECT2I: {1136*(Variant::Type *)&variant = Variant::VECTOR4I;1137}1138[[fallthrough]];1139case Variant::VECTOR2I:1140case Variant::VECTOR3I:1141case Variant::VECTOR4I: {1142// Variant can handle converting Vector2i/3i/4i to Vector4i for us.1143Vector4i vec = variant;1144for (int64_t i = 0; i < vector_size; i++) {1145numbers.set(vector_offset + i, vec[i]);1146}1147if (unlikely(vector_size > 4)) {1148for (int64_t i = 4; i < vector_size; i++) {1149numbers.set(vector_offset + i, 0.0);1150}1151}1152} break;1153case Variant::COLOR: {1154Color c = variant;1155for (int64_t i = 0; i < vector_size; i++) {1156numbers.set(vector_offset + i, c[i]);1157}1158if (unlikely(vector_size > 4)) {1159for (int64_t i = 4; i < vector_size; i++) {1160numbers.set(vector_offset + i, 0.0);1161}1162}1163} break;1164case Variant::TRANSFORM2D:1165case Variant::BASIS:1166case Variant::TRANSFORM3D:1167case Variant::PROJECTION: {1168// Variant can handle converting Transform2D/Transform3D/Basis to Projection for us.1169Projection p = variant;1170if (vector_size == 16) {1171for (int64_t i = 0; i < 4; i++) {1172numbers.set(vector_offset + 4 * i, p.columns[i][0]);1173numbers.set(vector_offset + 4 * i + 1, p.columns[i][1]);1174numbers.set(vector_offset + 4 * i + 2, p.columns[i][2]);1175numbers.set(vector_offset + 4 * i + 3, p.columns[i][3]);1176}1177} else if (vector_size == 9) {1178for (int64_t i = 0; i < 3; i++) {1179numbers.set(vector_offset + 3 * i, p.columns[i][0]);1180numbers.set(vector_offset + 3 * i + 1, p.columns[i][1]);1181numbers.set(vector_offset + 3 * i + 2, p.columns[i][2]);1182}1183} else if (vector_size == 4) {1184numbers.set(vector_offset, p.columns[0][0]);1185numbers.set(vector_offset + 1, p.columns[0][1]);1186numbers.set(vector_offset + 2, p.columns[1][0]);1187numbers.set(vector_offset + 3, p.columns[1][1]);1188}1189} break;1190default: {1191ERR_FAIL_V_MSG(PackedFloat64Array(), "glTF export: Cannot encode accessor from Variant of type " + Variant::get_type_name(p_variant_type) + ".");1192}1193}1194}1195return numbers;1196}11971198void GLTFAccessor::_store_sparse_indices_into_state(const Ref<GLTFState> &p_gltf_state, const PackedInt64Array &p_sparse_indices, const bool p_deduplicate) {1199// The byte offset of a sparse accessor's indices buffer view MUST be a multiple of the indices primitive componentType.1200// https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/accessor.sparse.indices.schema.json1201const int64_t bytes_per_index = _get_bytes_per_component(sparse_indices_component_type);1202PackedByteArray indices_bytes;1203indices_bytes.resize(bytes_per_index * p_sparse_indices.size());1204uint8_t *ret_write = indices_bytes.ptrw();1205int64_t ret_byte_offset = 0;1206for (int64_t i = 0; i < p_sparse_indices.size(); i++) {1207switch (sparse_indices_component_type) {1208case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {1209*(uint8_t *)&ret_write[ret_byte_offset] = p_sparse_indices[i];1210} break;1211case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {1212*(uint16_t *)&ret_write[ret_byte_offset] = p_sparse_indices[i];1213} break;1214case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT: {1215*(uint32_t *)&ret_write[ret_byte_offset] = p_sparse_indices[i];1216} break;1217case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG: {1218*(uint64_t *)&ret_write[ret_byte_offset] = p_sparse_indices[i];1219} break;1220default: {1221ERR_FAIL_MSG("glTF export: Invalid sparse indices component type '" + _get_component_type_name(sparse_indices_component_type) + "' for sparse accessor indices.");1222} break;1223}1224ret_byte_offset += bytes_per_index;1225}1226const 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);1227ERR_FAIL_COND_MSG(buffer_view_index == -1, "glTF export: Failed to write sparse indices into glTF state.");1228set_sparse_indices_buffer_view(buffer_view_index);1229}12301231// Low-level encode functions.12321233GLTFAccessor::GLTFComponentType GLTFAccessor::get_minimal_integer_component_type_from_ints(const PackedInt64Array &p_numbers) {1234bool has_negative = false;1235for (int64_t i = 0; i < p_numbers.size(); i++) {1236if (p_numbers[i] < 0) {1237has_negative = true;1238break;1239}1240}1241if (has_negative) {1242GLTFComponentType ret = GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE;1243for (int64_t i = 0; i < p_numbers.size(); i++) {1244const int64_t num = p_numbers[i];1245if (ret == GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE && (num < -128LL || num > 127LL)) {1246ret = GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT;1247}1248if (ret == GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT && (num < -32768LL || num > 32767LL)) {1249ret = GLTFAccessor::COMPONENT_TYPE_SIGNED_INT;1250}1251if (ret == GLTFAccessor::COMPONENT_TYPE_SIGNED_INT && (num < -2147483648LL || num > 2147483647LL)) {1252return GLTFAccessor::COMPONENT_TYPE_SIGNED_LONG;1253}1254}1255return ret;1256}1257GLTFComponentType ret = GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE;1258for (int64_t i = 0; i < p_numbers.size(); i++) {1259const int64_t num = p_numbers[i];1260// 3.7.2.1. indices accessor MUST NOT contain the maximum possible value for the component type used1261// (i.e., 255 for unsigned bytes, 65535 for unsigned shorts, 4294967295 for unsigned ints).1262// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview1263if (ret == GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE && num > 254LL) {1264ret = GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT;1265}1266if (ret == GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT && num > 65534LL) {1267ret = GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT;1268}1269if (ret == GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT && num > 4294967294LL) {1270return GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG;1271}1272}1273return ret;1274}12751276PackedByteArray GLTFAccessor::encode_floats_as_bytes(const PackedFloat64Array &p_input_numbers) {1277// Filter and update `count`, `min`, and `max` based on the given data.1278PackedFloat64Array filtered_numbers = _filter_numbers(p_input_numbers);1279count = filtered_numbers.size() / _get_vector_size();1280_calculate_min_and_max(filtered_numbers);1281// Actually encode the data.1282const int64_t input_size = filtered_numbers.size();1283const int64_t bytes_per_component = _get_bytes_per_component(component_type);1284int64_t raw_byte_size = _determine_padded_byte_count(bytes_per_component * input_size);1285int64_t skip_every = 0;1286int64_t skip_bytes = 0;1287_determine_pad_skip(skip_every, skip_bytes);1288PackedByteArray ret;1289ret.resize(raw_byte_size);1290uint8_t *ret_write = ret.ptrw();1291int64_t ret_byte_offset = 0;1292for (int64_t i = 0; i < input_size; i++) {1293switch (component_type) {1294case COMPONENT_TYPE_NONE: {1295ERR_FAIL_V_MSG(ret, "glTF export: Invalid component type 'NONE' for glTF accessor.");1296} break;1297case COMPONENT_TYPE_SIGNED_BYTE: {1298*(int8_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];1299} break;1300case COMPONENT_TYPE_UNSIGNED_BYTE: {1301*(uint8_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];1302} break;1303case COMPONENT_TYPE_SIGNED_SHORT: {1304*(int16_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];1305} break;1306case COMPONENT_TYPE_UNSIGNED_SHORT: {1307*(uint16_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];1308} break;1309case COMPONENT_TYPE_SIGNED_INT: {1310*(int32_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];1311} break;1312case COMPONENT_TYPE_UNSIGNED_INT: {1313*(uint32_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];1314} break;1315case COMPONENT_TYPE_SINGLE_FLOAT: {1316*(float *)&ret_write[ret_byte_offset] = filtered_numbers[i];1317} break;1318case COMPONENT_TYPE_DOUBLE_FLOAT: {1319*(double *)&ret_write[ret_byte_offset] = filtered_numbers[i];1320} break;1321case COMPONENT_TYPE_HALF_FLOAT: {1322*(uint16_t *)&ret_write[ret_byte_offset] = Math::make_half_float(filtered_numbers[i]);1323} break;1324case COMPONENT_TYPE_SIGNED_LONG: {1325// Note: This can potentially result in precision loss because int64_t can store some values that double can't.1326*(int64_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];1327} break;1328case COMPONENT_TYPE_UNSIGNED_LONG: {1329// Note: This can potentially result in precision loss because uint64_t can store some values that double can't.1330*(uint64_t *)&ret_write[ret_byte_offset] = filtered_numbers[i];1331} break;1332default: {1333ERR_FAIL_V_MSG(ret, "glTF export: Godot does not support writing glTF accessor components of type '" + itos(component_type) + "'.");1334} break;1335}1336ret_byte_offset += bytes_per_component;1337if (unlikely(skip_every > 0)) {1338if ((i + 1) % skip_every == 0) {1339ret_byte_offset += skip_bytes;1340}1341}1342}1343ERR_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.");1344return ret;1345}13461347PackedByteArray GLTFAccessor::encode_ints_as_bytes(const PackedInt64Array &p_input_numbers) {1348// Filter and update `count`, `min`, and `max` based on the given data.1349count = p_input_numbers.size() / _get_vector_size();1350_calculate_min_and_max(Variant(p_input_numbers));1351// Actually encode the data.1352const int64_t input_size = p_input_numbers.size();1353const int64_t bytes_per_component = _get_bytes_per_component(component_type);1354int64_t raw_byte_size = _determine_padded_byte_count(bytes_per_component * input_size);1355int64_t skip_every = 0;1356int64_t skip_bytes = 0;1357_determine_pad_skip(skip_every, skip_bytes);1358PackedByteArray ret;1359ret.resize(raw_byte_size);1360uint8_t *ret_write = ret.ptrw();1361int64_t ret_byte_offset = 0;1362for (int64_t i = 0; i < input_size; i++) {1363switch (component_type) {1364case COMPONENT_TYPE_NONE: {1365ERR_FAIL_V_MSG(ret, "glTF export: Invalid component type 'NONE' for glTF accessor.");1366} break;1367case COMPONENT_TYPE_SIGNED_BYTE: {1368*(int8_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];1369} break;1370case COMPONENT_TYPE_UNSIGNED_BYTE: {1371*(uint8_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];1372} break;1373case COMPONENT_TYPE_SIGNED_SHORT: {1374*(int16_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];1375} break;1376case COMPONENT_TYPE_UNSIGNED_SHORT: {1377*(uint16_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];1378} break;1379case COMPONENT_TYPE_SIGNED_INT: {1380*(int32_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];1381} break;1382case COMPONENT_TYPE_UNSIGNED_INT: {1383*(uint32_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];1384} break;1385case COMPONENT_TYPE_SINGLE_FLOAT: {1386*(float *)&ret_write[ret_byte_offset] = p_input_numbers[i];1387} break;1388case COMPONENT_TYPE_DOUBLE_FLOAT: {1389*(double *)&ret_write[ret_byte_offset] = p_input_numbers[i];1390} break;1391case COMPONENT_TYPE_HALF_FLOAT: {1392*(uint16_t *)&ret_write[ret_byte_offset] = Math::make_half_float(p_input_numbers[i]);1393} break;1394case COMPONENT_TYPE_SIGNED_LONG: {1395*(int64_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];1396} break;1397case COMPONENT_TYPE_UNSIGNED_LONG: {1398*(uint64_t *)&ret_write[ret_byte_offset] = p_input_numbers[i];1399} break;1400default: {1401ERR_FAIL_V_MSG(ret, "glTF export: Godot does not support writing glTF accessor components of type '" + itos(component_type) + "'.");1402} break;1403}1404ret_byte_offset += bytes_per_component;1405if (unlikely(skip_every > 0)) {1406if ((i + 1) % skip_every == 0) {1407ret_byte_offset += skip_bytes;1408}1409}1410}1411ERR_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.");1412return ret;1413}14141415PackedByteArray GLTFAccessor::encode_variants_as_bytes(const Array &p_input_data, Variant::Type p_variant_type) {1416const int64_t bytes_per_vec = _get_bytes_per_vector();1417ERR_FAIL_COND_V_MSG(bytes_per_vec == 0, PackedByteArray(), "glTF export: Cannot encode an accessor of this type.");1418PackedFloat64Array numbers = _encode_variants_as_floats(p_input_data, p_variant_type);1419return encode_floats_as_bytes(numbers);1420}14211422GLTFAccessorIndex 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) {1423ERR_FAIL_COND_V_MSG(p_data_bytes.is_empty(), -1, "glTF export: Cannot store nothing.");1424// 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.1425const int64_t bytes_per_vec = _get_bytes_per_vector();1426ERR_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.");1427count = p_data_bytes.size() / bytes_per_vec;1428// 3.6.2.4. The byte offset of an accessor's buffer view MUST be a multiple of the accessor's primitive size.1429// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#data-alignment1430const int64_t alignment = _get_bytes_per_component(component_type);1431// 3.6.2.4. Each element of a vertex attribute MUST be aligned to 4-byte boundaries inside a bufferView.1432int64_t byte_stride = -1;1433if (p_buffer_view_target == GLTFBufferView::TARGET_ARRAY_BUFFER) {1434byte_stride = bytes_per_vec;1435ERR_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.");1436}1437// Write the data into a new buffer view.1438const 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);1439ERR_FAIL_COND_V_MSG(buffer_view_index == -1, -1, "glTF export: Accessor failed to write new buffer view into glTF state.");1440set_buffer_view(buffer_view_index);1441// Add the new accessor to the state, but check for duplicates first.1442Vector<Ref<GLTFAccessor>> state_accessors = p_gltf_state->get_accessors();1443const GLTFAccessorIndex accessor_count = state_accessors.size();1444for (GLTFAccessorIndex i = 0; i < accessor_count; i++) {1445const Ref<GLTFAccessor> &existing_accessor = state_accessors[i];1446if (is_equal_exact(existing_accessor)) {1447// An identical accessor already exists in the state, so just return the index.1448return i;1449}1450}1451Ref<GLTFAccessor> self = this;1452state_accessors.append(self);1453p_gltf_state->set_accessors(state_accessors);1454return accessor_count;1455}14561457Ref<GLTFAccessor> GLTFAccessor::make_new_accessor_without_data(GLTFAccessorType p_accessor_type, GLTFComponentType p_component_type) {1458Ref<GLTFAccessor> accessor;1459accessor.instantiate();1460accessor->set_accessor_type(p_accessor_type);1461accessor->set_component_type(p_component_type);1462return accessor;1463}14641465// High-level encode functions.14661467GLTFAccessorIndex 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) {1468ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");1469PackedFloat64Array numbers;1470numbers.resize(p_input_data.size() * 4);1471for (int64_t i = 0; i < p_input_data.size(); i++) {1472const Color &color = p_input_data[i];1473numbers.set(i * 4, color.r);1474numbers.set(i * 4 + 1, color.g);1475numbers.set(i * 4 + 2, color.b);1476numbers.set(i * 4 + 3, color.a);1477}1478Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC4, COMPONENT_TYPE_SINGLE_FLOAT);1479PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(numbers);1480ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");1481return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);1482}14831484GLTFAccessorIndex 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) {1485ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");1486Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_SCALAR, COMPONENT_TYPE_SINGLE_FLOAT);1487PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(p_input_data);1488ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");1489return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);1490}14911492GLTFAccessorIndex 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) {1493ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");1494PackedInt64Array numbers;1495numbers.resize(p_input_data.size());1496for (int64_t i = 0; i < p_input_data.size(); i++) {1497numbers.set(i, p_input_data[i]);1498}1499const GLTFComponentType component_type = get_minimal_integer_component_type_from_ints(numbers);1500Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_SCALAR, component_type);1501PackedByteArray encoded_bytes = accessor->encode_ints_as_bytes(numbers);1502ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");1503return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);1504}15051506GLTFAccessorIndex 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) {1507ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");1508const GLTFComponentType component_type = get_minimal_integer_component_type_from_ints(p_input_data);1509Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_SCALAR, component_type);1510PackedByteArray encoded_bytes = accessor->encode_ints_as_bytes(p_input_data);1511ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");1512return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);1513}15141515GLTFAccessorIndex 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) {1516ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");1517PackedFloat64Array numbers;1518numbers.resize(p_input_data.size() * 4);1519for (int64_t i = 0; i < p_input_data.size(); i++) {1520const Quaternion &quat = p_input_data[i];1521numbers.set(i * 4, quat.x);1522numbers.set(i * 4 + 1, quat.y);1523numbers.set(i * 4 + 2, quat.z);1524numbers.set(i * 4 + 3, quat.w);1525}1526Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC4, COMPONENT_TYPE_SINGLE_FLOAT);1527PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(numbers);1528ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");1529return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);1530}15311532GLTFAccessorIndex 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) {1533ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");1534Ref<GLTFAccessor> accessor = make_new_accessor_without_data(p_accessor_type, p_component_type);1535// Write the data into a new buffer view.1536PackedByteArray encoded_bytes = accessor->encode_variants_as_bytes(p_input_data, p_variant_type);1537ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");1538return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);1539}15401541GLTFAccessorIndex 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) {1542ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");1543PackedFloat64Array numbers;1544numbers.resize(p_input_data.size() * 2);1545for (int64_t i = 0; i < p_input_data.size(); i++) {1546const Vector2 &vec = p_input_data[i];1547numbers.set(i * 2, vec.x);1548numbers.set(i * 2 + 1, vec.y);1549}1550Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC2, COMPONENT_TYPE_SINGLE_FLOAT);1551PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(numbers);1552ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");1553return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);1554}15551556GLTFAccessorIndex 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) {1557ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");1558PackedFloat64Array numbers;1559numbers.resize(p_input_data.size() * 3);1560for (int64_t i = 0; i < p_input_data.size(); i++) {1561const Vector3 &vec = p_input_data[i];1562numbers.set(i * 3, vec.x);1563numbers.set(i * 3 + 1, vec.y);1564numbers.set(i * 3 + 2, vec.z);1565}1566Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC3, COMPONENT_TYPE_SINGLE_FLOAT);1567PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(numbers);1568ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");1569return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);1570}15711572GLTFAccessorIndex 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) {1573ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");1574PackedFloat64Array numbers;1575numbers.resize(p_input_data.size() * 4);1576for (int64_t i = 0; i < p_input_data.size(); i++) {1577const Vector4 &vec = p_input_data[i];1578numbers.set(i * 4, vec.x);1579numbers.set(i * 4 + 1, vec.y);1580numbers.set(i * 4 + 2, vec.z);1581numbers.set(i * 4 + 3, vec.w);1582}1583Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC4, COMPONENT_TYPE_SINGLE_FLOAT);1584PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(numbers);1585ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");1586return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);1587}15881589GLTFAccessorIndex 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) {1590ERR_FAIL_COND_V_MSG(p_input_data.is_empty(), -1, "glTF export: Cannot encode an accessor from an empty array.");1591PackedInt64Array numbers;1592numbers.resize(p_input_data.size() * 4);1593for (int64_t i = 0; i < p_input_data.size(); i++) {1594const Vector4i &vec = p_input_data[i];1595numbers.set(i * 4, vec.x);1596numbers.set(i * 4 + 1, vec.y);1597numbers.set(i * 4 + 2, vec.z);1598numbers.set(i * 4 + 3, vec.w);1599}1600const GLTFComponentType component_type = get_minimal_integer_component_type_from_ints(numbers);1601Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC4, component_type);1602PackedByteArray encoded_bytes = accessor->encode_ints_as_bytes(numbers);1603ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");1604return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_buffer_view_target, 0, p_deduplicate);1605}16061607GLTFAccessorIndex 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) {1608const int64_t input_size = p_input_data.size();1609ERR_FAIL_COND_V_MSG(input_size == 0, -1, "glTF export: Cannot encode an accessor from an empty array.");1610const bool is_base_empty = p_base_reference_data.is_empty();1611ERR_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.");1612PackedInt64Array sparse_indices;1613PackedFloat64Array sparse_values;1614PackedFloat64Array dense_values;1615int64_t highest_index = 0;1616dense_values.resize(input_size * 3);1617for (int64_t i = 0; i < input_size; i++) {1618Vector3 vec = p_input_data[i];1619Vector3 base_ref_vec;1620Vector3 displacement;1621if (is_base_empty) {1622base_ref_vec = Vector3();1623displacement = vec;1624} else {1625base_ref_vec = p_base_reference_data[i];1626displacement = vec - base_ref_vec;1627}1628if ((displacement * p_tolerance_multiplier).is_zero_approx()) {1629vec = base_ref_vec;1630} else {1631highest_index = i;1632sparse_indices.append(i);1633sparse_values.append(vec.x);1634sparse_values.append(vec.y);1635sparse_values.append(vec.z);1636}1637dense_values.set(i * 3, vec.x);1638dense_values.set(i * 3 + 1, vec.y);1639dense_values.set(i * 3 + 2, vec.z);1640}1641// Check if the sparse accessor actually saves space, or if it's better to just use a normal accessor.1642const int64_t sparse_count = sparse_indices.size();1643const int64_t bytes_per_value_component = _get_bytes_per_component(COMPONENT_TYPE_SINGLE_FLOAT);1644const GLTFComponentType indices_component_type = _get_indices_component_type_for_size(highest_index);1645const int64_t sparse_data_bytes = _get_bytes_per_component(indices_component_type) * sparse_count + bytes_per_value_component * sparse_values.size();1646const int64_t dense_data_bytes = bytes_per_value_component * 3 * input_size;1647// Sparse accessors require more JSON, a bit under 200 characters when minified, so factor that in.1648constexpr int64_t sparse_json_fluff = 200;1649Ref<GLTFAccessor> accessor = make_new_accessor_without_data(TYPE_VEC3, COMPONENT_TYPE_SINGLE_FLOAT);1650if (sparse_data_bytes + sparse_json_fluff >= dense_data_bytes) {1651// Sparse accessor is not worth it, just use a normal accessor instead.1652// However, note that we use the calculated dense values instead of the original input data.1653// This way, regardless of the underlying storage layout, the data is the same in both cases.1654PackedByteArray encoded_bytes = accessor->encode_floats_as_bytes(dense_values);1655ERR_FAIL_COND_V_MSG(encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode data as bytes (was the input data empty?).");1656return accessor->store_accessor_data_into_state(p_gltf_state, encoded_bytes, p_main_buffer_view_target, 0, p_deduplicate);1657}1658// Encode as a sparse accessor.1659if (sparse_count > 0) {1660accessor->set_sparse_count(sparse_count);1661accessor->set_sparse_indices_component_type(indices_component_type);1662accessor->_store_sparse_indices_into_state(p_gltf_state, sparse_indices, p_deduplicate);1663const PackedByteArray sparse_values_encoded_bytes = accessor->encode_floats_as_bytes(sparse_values);1664ERR_FAIL_COND_V_MSG(sparse_values_encoded_bytes.is_empty(), -1, "glTF export: Accessor failed to encode sparse values as bytes.");1665// Note: Sparse values always use TARGET_NONE, it does NOT match the target of the main buffer view.1666const 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);1667accessor->set_sparse_values_buffer_view(sparse_values_buffer_view_index);1668}1669// If the base reference data is empty, just directly add the accessor with only sparse data.1670if (is_base_empty) {1671// This is similar to `encode_floats_as_bytes` + `store_accessor_data_into_state` but we don't write a buffer view.1672// Filter and update `count`, `min`, and `max` based on the given data.1673accessor->set_count(input_size);1674const PackedFloat64Array filtered_numbers = accessor->_filter_numbers(dense_values);1675accessor->_calculate_min_and_max(filtered_numbers);1676// Add the new accessor to the state, but check for duplicates first.1677Vector<Ref<GLTFAccessor>> state_accessors = p_gltf_state->get_accessors();1678const GLTFAccessorIndex accessor_count = state_accessors.size();1679for (GLTFAccessorIndex i = 0; i < accessor_count; i++) {1680const Ref<GLTFAccessor> &existing_accessor = state_accessors[i];1681if (accessor->is_equal_exact(existing_accessor)) {1682// An identical accessor already exists in the state, so just return the index.1683return i;1684}1685}1686state_accessors.append(accessor);1687p_gltf_state->set_accessors(state_accessors);1688return accessor_count;1689}1690// Encode the base reference alongside the sparse data.1691PackedFloat64Array base_reference_values;1692base_reference_values.resize(input_size * 3);1693for (int64_t i = 0; i < input_size; i++) {1694const Vector3 &base_ref_vec = p_base_reference_data[i];1695base_reference_values.set(i * 3, base_ref_vec.x);1696base_reference_values.set(i * 3 + 1, base_ref_vec.y);1697base_reference_values.set(i * 3 + 2, base_ref_vec.z);1698}1699const PackedByteArray base_reference_encoded_bytes = accessor->encode_floats_as_bytes(base_reference_values);1700ERR_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?).");1701return accessor->store_accessor_data_into_state(p_gltf_state, base_reference_encoded_bytes, p_main_buffer_view_target, 0, p_deduplicate);1702}17031704// Dictionary conversion.17051706Ref<GLTFAccessor> GLTFAccessor::from_dictionary(const Dictionary &p_dict) {1707// See https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/accessor.schema.json1708Ref<GLTFAccessor> accessor;1709accessor.instantiate();1710if (p_dict.has("bufferView")) {1711// bufferView is optional. If not present, the accessor is considered to be zero-initialized.1712accessor->buffer_view = p_dict["bufferView"];1713}1714if (p_dict.has("byteOffset")) {1715accessor->byte_offset = p_dict["byteOffset"];1716}1717if (p_dict.has("componentType")) {1718accessor->component_type = (GLTFAccessor::GLTFComponentType)(int32_t)p_dict["componentType"];1719}1720if (p_dict.has("count")) {1721accessor->count = p_dict["count"];1722}1723if (accessor->count <= 0) {1724ERR_PRINT("glTF import: Invalid accessor count " + itos(accessor->count) + " for accessor. Accessor count must be greater than 0.");1725}1726if (p_dict.has("max")) {1727accessor->max = p_dict["max"];1728}1729if (p_dict.has("min")) {1730accessor->min = p_dict["min"];1731}1732if (p_dict.has("normalized")) {1733accessor->normalized = p_dict["normalized"];1734}1735if (p_dict.has("sparse")) {1736// See https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/accessor.sparse.schema.json1737const Dictionary &sparse_dict = p_dict["sparse"];1738ERR_FAIL_COND_V(!sparse_dict.has("count"), accessor);1739accessor->sparse_count = sparse_dict["count"];1740ERR_FAIL_COND_V(!sparse_dict.has("indices"), accessor);1741const Dictionary &sparse_indices_dict = sparse_dict["indices"];1742ERR_FAIL_COND_V(!sparse_indices_dict.has("bufferView"), accessor);1743accessor->sparse_indices_buffer_view = sparse_indices_dict["bufferView"];1744ERR_FAIL_COND_V(!sparse_indices_dict.has("componentType"), accessor);1745accessor->sparse_indices_component_type = (GLTFAccessor::GLTFComponentType)(int32_t)sparse_indices_dict["componentType"];1746if (sparse_indices_dict.has("byteOffset")) {1747accessor->sparse_indices_byte_offset = sparse_indices_dict["byteOffset"];1748}1749ERR_FAIL_COND_V(!sparse_dict.has("values"), accessor);1750const Dictionary &sparse_values_dict = sparse_dict["values"];1751ERR_FAIL_COND_V(!sparse_values_dict.has("bufferView"), accessor);1752accessor->sparse_values_buffer_view = sparse_values_dict["bufferView"];1753if (sparse_values_dict.has("byteOffset")) {1754accessor->sparse_values_byte_offset = sparse_values_dict["byteOffset"];1755}1756}1757accessor->accessor_type = _get_accessor_type_from_str(p_dict["type"]);1758return accessor;1759}17601761Dictionary GLTFAccessor::to_dictionary() const {1762Dictionary dict;1763if (buffer_view != -1) {1764// bufferView may be omitted to zero-initialize the buffer. When this happens, byteOffset MUST also be omitted.1765if (byte_offset > 0) {1766dict["byteOffset"] = byte_offset;1767}1768dict["bufferView"] = buffer_view;1769}1770dict["componentType"] = component_type;1771dict["count"] = count;1772switch (component_type) {1773case COMPONENT_TYPE_NONE: {1774ERR_PRINT("glTF export: Invalid component type 'NONE' for glTF accessor.");1775} break;1776case COMPONENT_TYPE_SIGNED_BYTE:1777case COMPONENT_TYPE_UNSIGNED_BYTE:1778case COMPONENT_TYPE_SIGNED_SHORT:1779case COMPONENT_TYPE_UNSIGNED_SHORT:1780case COMPONENT_TYPE_SIGNED_INT:1781case COMPONENT_TYPE_UNSIGNED_INT:1782case COMPONENT_TYPE_SIGNED_LONG:1783case COMPONENT_TYPE_UNSIGNED_LONG: {1784dict["max"] = PackedInt64Array(Variant(max));1785dict["min"] = PackedInt64Array(Variant(min));1786} break;1787case COMPONENT_TYPE_SINGLE_FLOAT:1788case COMPONENT_TYPE_DOUBLE_FLOAT:1789case COMPONENT_TYPE_HALF_FLOAT: {1790dict["max"] = max;1791dict["min"] = min;1792} break;1793}1794dict["normalized"] = normalized;1795dict["type"] = _get_accessor_type_name();17961797if (sparse_count > 0) {1798Dictionary sparse_indices_dict;1799sparse_indices_dict["bufferView"] = sparse_indices_buffer_view;1800sparse_indices_dict["componentType"] = sparse_indices_component_type;1801if (sparse_indices_byte_offset > 0) {1802sparse_indices_dict["byteOffset"] = sparse_indices_byte_offset;1803}1804Dictionary sparse_values_dict;1805sparse_values_dict["bufferView"] = sparse_values_buffer_view;1806if (sparse_values_byte_offset > 0) {1807sparse_values_dict["byteOffset"] = sparse_values_byte_offset;1808}1809Dictionary sparse_dict;1810sparse_dict["count"] = sparse_count;1811sparse_dict["indices"] = sparse_indices_dict;1812sparse_dict["values"] = sparse_values_dict;1813dict["sparse"] = sparse_dict;1814}1815return dict;1816}181718181819