Path: blob/master/modules/gltf/structures/gltf_accessor.h
21409 views
/**************************************************************************/1/* gltf_accessor.h */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#pragma once3132#include "../gltf_defines.h"3334#include "gltf_buffer_view.h"3536class GLTFAccessor : public Resource {37GDCLASS(GLTFAccessor, Resource);38friend class GLTFDocument;3940public:41enum GLTFAccessorType {42TYPE_SCALAR,43TYPE_VEC2,44TYPE_VEC3,45TYPE_VEC4,46TYPE_MAT2,47TYPE_MAT3,48TYPE_MAT4,49};5051enum GLTFComponentType {52COMPONENT_TYPE_NONE = 0,53COMPONENT_TYPE_SIGNED_BYTE = 5120,54COMPONENT_TYPE_UNSIGNED_BYTE = 5121,55COMPONENT_TYPE_SIGNED_SHORT = 5122,56COMPONENT_TYPE_UNSIGNED_SHORT = 5123,57COMPONENT_TYPE_SIGNED_INT = 5124,58COMPONENT_TYPE_UNSIGNED_INT = 5125,59COMPONENT_TYPE_SINGLE_FLOAT = 5126,60COMPONENT_TYPE_DOUBLE_FLOAT = 5130,61COMPONENT_TYPE_HALF_FLOAT = 5131,62COMPONENT_TYPE_SIGNED_LONG = 5134,63COMPONENT_TYPE_UNSIGNED_LONG = 5135,64};6566private:67GLTFBufferViewIndex buffer_view = -1;68int64_t byte_offset = 0;69GLTFComponentType component_type = COMPONENT_TYPE_NONE;70bool normalized = false;71int64_t count = 0;72GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_SCALAR;73Vector<double> min;74Vector<double> max;75int64_t sparse_count = 0;76GLTFBufferViewIndex sparse_indices_buffer_view = 0;77int64_t sparse_indices_byte_offset = 0;78GLTFComponentType sparse_indices_component_type = COMPONENT_TYPE_NONE;79GLTFBufferViewIndex sparse_values_buffer_view = 0;80int64_t sparse_values_byte_offset = 0;8182// Trivial helper functions.83void _calculate_min_and_max(const PackedFloat64Array &p_numbers);84void _determine_pad_skip(int64_t &r_skip_every, int64_t &r_skip_bytes) const;85int64_t _determine_padded_byte_count(int64_t p_raw_byte_size) const;86PackedFloat64Array _filter_numbers(const PackedFloat64Array &p_numbers) const;87static String _get_component_type_name(const GLTFComponentType p_component);88static GLTFComponentType _get_indices_component_type_for_size(const int64_t p_size);89static GLTFAccessorType _get_accessor_type_from_str(const String &p_string);90String _get_accessor_type_name() const;91int64_t _get_vector_size() const;92static int64_t _get_numbers_per_variant_for_gltf(Variant::Type p_variant_type);93static int64_t _get_bytes_per_component(const GLTFComponentType p_component_type);94int64_t _get_bytes_per_vector() const;9596// Private decode functions.97PackedInt64Array _decode_sparse_indices(const Ref<GLTFState> &p_gltf_state, const Vector<Ref<GLTFBufferView>> &p_buffer_views) const;98template <typename T>99Vector<T> _decode_raw_numbers(const Ref<GLTFState> &p_gltf_state, const Vector<Ref<GLTFBufferView>> &p_buffer_views, bool p_sparse_values) const;100template <typename T>101Vector<T> _decode_as_numbers(const Ref<GLTFState> &p_gltf_state) const;102103// Private encode functions.104PackedFloat64Array _encode_variants_as_floats(const Array &p_input_data, Variant::Type p_variant_type) const;105void _store_sparse_indices_into_state(const Ref<GLTFState> &p_gltf_state, const PackedInt64Array &p_sparse_indices, const bool p_deduplicate = true);106107protected:108static void _bind_methods();109110#ifndef DISABLE_DEPRECATED111// 32-bit and non-const versions for compatibility.112GLTFBufferViewIndex _get_buffer_view_bind_compat_106220();113int _get_byte_offset_bind_compat_106220();114int _get_component_type_bind_compat_106220();115void _set_component_type_bind_compat_106220(int p_component_type);116bool _get_normalized_bind_compat_106220();117int _get_count_bind_compat_106220();118GLTFAccessorType _get_accessor_type_bind_compat_106220();119int _get_type_bind_compat_106220();120Vector<double> _get_min_bind_compat_106220();121Vector<double> _get_max_bind_compat_106220();122int _get_sparse_count_bind_compat_106220();123int _get_sparse_indices_buffer_view_bind_compat_106220();124int _get_sparse_indices_byte_offset_bind_compat_106220();125int _get_sparse_indices_component_type_bind_compat_106220();126void _set_sparse_indices_component_type_bind_compat_106220(int p_sparse_indices_component_type);127int _get_sparse_values_buffer_view_bind_compat_106220();128int _get_sparse_values_byte_offset_bind_compat_106220();129static void _bind_compatibility_methods();130#endif // DISABLE_DEPRECATED131132public:133// Property getters and setters.134GLTFBufferViewIndex get_buffer_view() const;135void set_buffer_view(GLTFBufferViewIndex p_buffer_view);136137int64_t get_byte_offset() const;138void set_byte_offset(int64_t p_byte_offset);139140GLTFComponentType get_component_type() const;141void set_component_type(GLTFComponentType p_component_type);142143bool get_normalized() const;144void set_normalized(bool p_normalized);145146int64_t get_count() const;147void set_count(int64_t p_count);148149GLTFAccessorType get_accessor_type() const;150void set_accessor_type(GLTFAccessorType p_accessor_type);151152int get_type() const;153void set_type(int p_accessor_type);154155Vector<double> get_min() const;156void set_min(const Vector<double> &p_min);157158Vector<double> get_max() const;159void set_max(const Vector<double> &p_max);160161int64_t get_sparse_count() const;162void set_sparse_count(int64_t p_sparse_count);163164GLTFBufferViewIndex get_sparse_indices_buffer_view() const;165void set_sparse_indices_buffer_view(GLTFBufferViewIndex p_sparse_indices_buffer_view);166167int64_t get_sparse_indices_byte_offset() const;168void set_sparse_indices_byte_offset(int64_t p_sparse_indices_byte_offset);169170GLTFComponentType get_sparse_indices_component_type() const;171void set_sparse_indices_component_type(GLTFComponentType p_sparse_indices_component_type);172173GLTFBufferViewIndex get_sparse_values_buffer_view() const;174void set_sparse_values_buffer_view(GLTFBufferViewIndex p_sparse_values_buffer_view);175176int64_t get_sparse_values_byte_offset() const;177void set_sparse_values_byte_offset(int64_t p_sparse_values_byte_offset);178179bool is_equal_exact(const Ref<GLTFAccessor> &p_other) const;180181// High-level decode functions.182PackedColorArray decode_as_colors(const Ref<GLTFState> &p_gltf_state) const;183PackedFloat32Array decode_as_float32s(const Ref<GLTFState> &p_gltf_state) const;184PackedFloat64Array decode_as_float64s(const Ref<GLTFState> &p_gltf_state) const;185PackedInt32Array decode_as_int32s(const Ref<GLTFState> &p_gltf_state) const;186PackedInt64Array decode_as_int64s(const Ref<GLTFState> &p_gltf_state) const;187Vector<Quaternion> decode_as_quaternions(const Ref<GLTFState> &p_gltf_state) const;188PackedVector2Array decode_as_vector2s(const Ref<GLTFState> &p_gltf_state) const;189PackedVector3Array decode_as_vector3s(const Ref<GLTFState> &p_gltf_state) const;190PackedVector4Array decode_as_vector4s(const Ref<GLTFState> &p_gltf_state) const;191Array decode_as_variants(const Ref<GLTFState> &p_gltf_state, Variant::Type p_variant_type) const;192193// Low-level encode functions.194static GLTFComponentType get_minimal_integer_component_type_from_ints(const PackedInt64Array &p_numbers);195PackedByteArray encode_floats_as_bytes(const PackedFloat64Array &p_input_numbers);196PackedByteArray encode_ints_as_bytes(const PackedInt64Array &p_input_numbers);197PackedByteArray encode_variants_as_bytes(const Array &p_input_data, Variant::Type p_variant_type);198199GLTFAccessorIndex store_accessor_data_into_state(const Ref<GLTFState> &p_gltf_state, const PackedByteArray &p_data_bytes, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const GLTFBufferIndex p_buffer_index = 0, const bool p_deduplicate = true);200static Ref<GLTFAccessor> make_new_accessor_without_data(GLTFAccessorType p_accessor_type = TYPE_SCALAR, GLTFComponentType p_component_type = COMPONENT_TYPE_SINGLE_FLOAT);201202// High-level encode functions.203static GLTFAccessorIndex encode_new_accessor_from_colors(const Ref<GLTFState> &p_gltf_state, const PackedColorArray &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);204static GLTFAccessorIndex encode_new_accessor_from_float64s(const Ref<GLTFState> &p_gltf_state, const PackedFloat64Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);205static GLTFAccessorIndex encode_new_accessor_from_int32s(const Ref<GLTFState> &p_gltf_state, const PackedInt32Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);206static GLTFAccessorIndex encode_new_accessor_from_int64s(const Ref<GLTFState> &p_gltf_state, const PackedInt64Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);207static GLTFAccessorIndex encode_new_accessor_from_quaternions(const Ref<GLTFState> &p_gltf_state, const Vector<Quaternion> &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);208static GLTFAccessorIndex 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 = TYPE_SCALAR, GLTFComponentType p_component_type = COMPONENT_TYPE_SINGLE_FLOAT, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);209static GLTFAccessorIndex encode_new_accessor_from_vector2s(const Ref<GLTFState> &p_gltf_state, const PackedVector2Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);210static GLTFAccessorIndex encode_new_accessor_from_vector3s(const Ref<GLTFState> &p_gltf_state, const PackedVector3Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);211static GLTFAccessorIndex encode_new_accessor_from_vector4s(const Ref<GLTFState> &p_gltf_state, const PackedVector4Array &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);212static GLTFAccessorIndex encode_new_accessor_from_vector4is(const Ref<GLTFState> &p_gltf_state, const Vector<Vector4i> &p_input_data, const GLTFBufferView::ArrayBufferTarget p_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);213static GLTFAccessorIndex 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 = 1.0, const GLTFBufferView::ArrayBufferTarget p_main_buffer_view_target = GLTFBufferView::TARGET_NONE, const bool p_deduplicate = true);214215// Dictionary conversion.216static Ref<GLTFAccessor> from_dictionary(const Dictionary &p_dict);217Dictionary to_dictionary() const;218};219220VARIANT_ENUM_CAST(GLTFAccessor::GLTFAccessorType);221VARIANT_ENUM_CAST(GLTFAccessor::GLTFComponentType);222223224