Browse Source

GLTF: Move the component type enum into GLTFAccessor

Aaron Franke 1 year ago
parent
commit
4b0085ac85

+ 22 - 1
modules/gltf/doc_classes/GLTFAccessor.xml

@@ -22,7 +22,7 @@
 			The offset relative to the start of the buffer view in bytes.
 			The offset relative to the start of the buffer view in bytes.
 		</member>
 		</member>
 		<member name="component_type" type="int" setter="set_component_type" getter="get_component_type" default="0">
 		<member name="component_type" type="int" setter="set_component_type" getter="get_component_type" default="0">
-			The glTF component type as an enum. Possible values are 5120 for "BYTE", 5121 for "UNSIGNED_BYTE", 5122 for "SHORT", 5123 for "UNSIGNED_SHORT", 5125 for "UNSIGNED_INT", and 5126 for "FLOAT". A value of 5125 or "UNSIGNED_INT" must not be used for any accessor that is not referenced by mesh.primitive.indices.
+			The glTF component type as an enum. See [enum GLTFComponentType] for possible values. Within the core glTF specification, a value of 5125 or "UNSIGNED_INT" must not be used for any accessor that is not referenced by mesh.primitive.indices.
 		</member>
 		</member>
 		<member name="count" type="int" setter="set_count" getter="get_count" default="0">
 		<member name="count" type="int" setter="set_count" getter="get_count" default="0">
 			The number of elements referenced by this accessor.
 			The number of elements referenced by this accessor.
@@ -80,5 +80,26 @@
 		<constant name="TYPE_MAT4" value="6" enum="GLTFAccessorType">
 		<constant name="TYPE_MAT4" value="6" enum="GLTFAccessorType">
 			Accessor type "MAT4". For the glTF object model, this maps to "float4x4", represented in the glTF JSON as an array of sixteen floats.
 			Accessor type "MAT4". For the glTF object model, this maps to "float4x4", represented in the glTF JSON as an array of sixteen floats.
 		</constant>
 		</constant>
+		<constant name="COMPONENT_TYPE_NONE" value="0" enum="GLTFComponentType">
+			Component type "NONE". This is not a valid component type, and is used to indicate that the component type is not set.
+		</constant>
+		<constant name="COMPONENT_TYPE_SIGNED_BYTE" value="5120" enum="GLTFComponentType">
+			Component type "BYTE". The value is [code]0x1400[/code] which comes from OpenGL. This indicates data is stored in 1-byte or 8-bit signed integers. This is a core part of the glTF specification.
+		</constant>
+		<constant name="COMPONENT_TYPE_UNSIGNED_BYTE" value="5121" enum="GLTFComponentType">
+			Component type "UNSIGNED_BYTE". The value is [code]0x1401[/code] which comes from OpenGL. This indicates data is stored in 1-byte or 8-bit unsigned integers. This is a core part of the glTF specification.
+		</constant>
+		<constant name="COMPONENT_TYPE_SIGNED_SHORT" value="5122" enum="GLTFComponentType">
+			Component type "SHORT". The value is [code]0x1402[/code] which comes from OpenGL. This indicates data is stored in 2-byte or 16-bit signed integers. This is a core part of the glTF specification.
+		</constant>
+		<constant name="COMPONENT_TYPE_UNSIGNED_SHORT" value="5123" enum="GLTFComponentType">
+			Component type "UNSIGNED_SHORT". The value is [code]0x1403[/code] which comes from OpenGL. This indicates data is stored in 2-byte or 16-bit unsigned integers. This is a core part of the glTF specification.
+		</constant>
+		<constant name="COMPONENT_TYPE_UNSIGNED_INT" value="5125" enum="GLTFComponentType">
+			Component type "UNSIGNED_INT". The value is [code]0x1405[/code] which comes from OpenGL. This indicates data is stored in 4-byte or 32-bit unsigned integers. This is a core part of the glTF specification.
+		</constant>
+		<constant name="COMPONENT_TYPE_SINGLE_FLOAT" value="5126" enum="GLTFComponentType">
+			Component type "FLOAT". The value is [code]0x1406[/code] which comes from OpenGL. This indicates data is stored in 4-byte or 32-bit floating point numbers. This is a core part of the glTF specification.
+		</constant>
 	</constants>
 	</constants>
 </class>
 </class>

+ 75 - 75
modules/gltf/gltf_document.cpp

@@ -69,6 +69,10 @@
 #include <stdlib.h>
 #include <stdlib.h>
 #include <cstdint>
 #include <cstdint>
 
 
+constexpr int COMPONENT_COUNT_FOR_ACCESSOR_TYPE[7] = {
+	1, 2, 3, 4, 4, 9, 16
+};
+
 static void _attach_extras_to_meta(const Dictionary &p_extras, Ref<Resource> p_node) {
 static void _attach_extras_to_meta(const Dictionary &p_extras, Ref<Resource> p_node) {
 	if (!p_extras.is_empty()) {
 	if (!p_extras.is_empty()) {
 		p_node->set_meta("extras", p_extras);
 		p_node->set_meta("extras", p_extras);
@@ -1013,7 +1017,7 @@ Error GLTFDocument::_parse_accessors(Ref<GLTFState> p_state) {
 		accessor.instantiate();
 		accessor.instantiate();
 
 
 		ERR_FAIL_COND_V(!d.has("componentType"), ERR_PARSE_ERROR);
 		ERR_FAIL_COND_V(!d.has("componentType"), ERR_PARSE_ERROR);
-		accessor->component_type = d["componentType"];
+		accessor->component_type = (GLTFAccessor::GLTFComponentType)(int32_t)d["componentType"];
 		ERR_FAIL_COND_V(!d.has("count"), ERR_PARSE_ERROR);
 		ERR_FAIL_COND_V(!d.has("count"), ERR_PARSE_ERROR);
 		accessor->count = d["count"];
 		accessor->count = d["count"];
 		ERR_FAIL_COND_V(!d.has("type"), ERR_PARSE_ERROR);
 		ERR_FAIL_COND_V(!d.has("type"), ERR_PARSE_ERROR);
@@ -1050,7 +1054,7 @@ Error GLTFDocument::_parse_accessors(Ref<GLTFState> p_state) {
 			ERR_FAIL_COND_V(!si.has("bufferView"), ERR_PARSE_ERROR);
 			ERR_FAIL_COND_V(!si.has("bufferView"), ERR_PARSE_ERROR);
 			accessor->sparse_indices_buffer_view = si["bufferView"];
 			accessor->sparse_indices_buffer_view = si["bufferView"];
 			ERR_FAIL_COND_V(!si.has("componentType"), ERR_PARSE_ERROR);
 			ERR_FAIL_COND_V(!si.has("componentType"), ERR_PARSE_ERROR);
-			accessor->sparse_indices_component_type = si["componentType"];
+			accessor->sparse_indices_component_type = (GLTFAccessor::GLTFComponentType)(int32_t)si["componentType"];
 
 
 			if (si.has("byteOffset")) {
 			if (si.has("byteOffset")) {
 				accessor->sparse_indices_byte_offset = si["byteOffset"];
 				accessor->sparse_indices_byte_offset = si["byteOffset"];
@@ -1082,31 +1086,29 @@ double GLTFDocument::_filter_number(double p_float) {
 	return (double)(float)p_float;
 	return (double)(float)p_float;
 }
 }
 
 
-String GLTFDocument::_get_component_type_name(const uint32_t p_component) {
+String GLTFDocument::_get_component_type_name(const GLTFAccessor::GLTFComponentType p_component) {
 	switch (p_component) {
 	switch (p_component) {
-		case GLTFDocument::COMPONENT_TYPE_BYTE:
+		case GLTFAccessor::COMPONENT_TYPE_NONE:
+			return "None";
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE:
 			return "Byte";
 			return "Byte";
-		case GLTFDocument::COMPONENT_TYPE_UNSIGNED_BYTE:
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE:
 			return "UByte";
 			return "UByte";
-		case GLTFDocument::COMPONENT_TYPE_SHORT:
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:
 			return "Short";
 			return "Short";
-		case GLTFDocument::COMPONENT_TYPE_UNSIGNED_SHORT:
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT:
 			return "UShort";
 			return "UShort";
-		case GLTFDocument::COMPONENT_TYPE_INT:
-			return "Int";
-		case GLTFDocument::COMPONENT_TYPE_FLOAT:
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT:
+			return "UInt";
+		case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT:
 			return "Float";
 			return "Float";
 	}
 	}
 
 
 	return "<Error>";
 	return "<Error>";
 }
 }
 
 
-Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_src, const int p_count, const GLTFAccessor::GLTFAccessorType p_accessor_type, const int p_component_type, const bool p_normalized, const int p_byte_offset, const bool p_for_vertex, GLTFBufferViewIndex &r_accessor, const bool p_for_vertex_indices) {
-	const int component_count_for_type[7] = {
-		1, 2, 3, 4, 4, 9, 16
-	};
-
-	const int component_count = component_count_for_type[p_accessor_type];
+Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_src, const int p_count, const GLTFAccessor::GLTFAccessorType p_accessor_type, const GLTFAccessor::GLTFComponentType p_component_type, const bool p_normalized, const int p_byte_offset, const bool p_for_vertex, GLTFBufferViewIndex &r_accessor, const bool p_for_vertex_indices) {
+	const int component_count = COMPONENT_COUNT_FOR_ACCESSOR_TYPE[p_accessor_type];
 	const int component_size = _get_component_type_size(p_component_type);
 	const int component_size = _get_component_type_size(p_component_type);
 	ERR_FAIL_COND_V(component_size == 0, FAILED);
 	ERR_FAIL_COND_V(component_size == 0, FAILED);
 
 
@@ -1114,8 +1116,8 @@ Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_
 	int skip_bytes = 0;
 	int skip_bytes = 0;
 	//special case of alignments, as described in spec
 	//special case of alignments, as described in spec
 	switch (p_component_type) {
 	switch (p_component_type) {
-		case COMPONENT_TYPE_BYTE:
-		case COMPONENT_TYPE_UNSIGNED_BYTE: {
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE:
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {
 			if (p_accessor_type == GLTFAccessor::TYPE_MAT2) {
 			if (p_accessor_type == GLTFAccessor::TYPE_MAT2) {
 				skip_every = 2;
 				skip_every = 2;
 				skip_bytes = 2;
 				skip_bytes = 2;
@@ -1125,8 +1127,8 @@ Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_
 				skip_bytes = 1;
 				skip_bytes = 1;
 			}
 			}
 		} break;
 		} break;
-		case COMPONENT_TYPE_SHORT:
-		case COMPONENT_TYPE_UNSIGNED_SHORT: {
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {
 			if (p_accessor_type == GLTFAccessor::TYPE_MAT3) {
 			if (p_accessor_type == GLTFAccessor::TYPE_MAT3) {
 				skip_every = 6;
 				skip_every = 6;
 				skip_bytes = 4;
 				skip_bytes = 4;
@@ -1161,7 +1163,10 @@ Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_
 	}
 	}
 
 
 	switch (p_component_type) {
 	switch (p_component_type) {
-		case COMPONENT_TYPE_BYTE: {
+		case GLTFAccessor::COMPONENT_TYPE_NONE: {
+			ERR_FAIL_V_MSG(ERR_INVALID_DATA, "glTF: Failed to encode buffer view, component type not set.");
+		}
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE: {
 			Vector<int8_t> buffer;
 			Vector<int8_t> buffer;
 			buffer.resize(p_count * component_count);
 			buffer.resize(p_count * component_count);
 			int32_t dst_i = 0;
 			int32_t dst_i = 0;
@@ -1185,7 +1190,7 @@ Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_
 			memcpy(gltf_buffer.ptrw() + old_size, buffer.ptrw(), buffer.size() * sizeof(int8_t));
 			memcpy(gltf_buffer.ptrw() + old_size, buffer.ptrw(), buffer.size() * sizeof(int8_t));
 			bv->byte_length = buffer.size() * sizeof(int8_t);
 			bv->byte_length = buffer.size() * sizeof(int8_t);
 		} break;
 		} break;
-		case COMPONENT_TYPE_UNSIGNED_BYTE: {
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {
 			Vector<uint8_t> buffer;
 			Vector<uint8_t> buffer;
 			buffer.resize(p_count * component_count);
 			buffer.resize(p_count * component_count);
 			int32_t dst_i = 0;
 			int32_t dst_i = 0;
@@ -1207,7 +1212,7 @@ Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_
 			gltf_buffer.append_array(buffer);
 			gltf_buffer.append_array(buffer);
 			bv->byte_length = buffer.size() * sizeof(uint8_t);
 			bv->byte_length = buffer.size() * sizeof(uint8_t);
 		} break;
 		} break;
-		case COMPONENT_TYPE_SHORT: {
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT: {
 			Vector<int16_t> buffer;
 			Vector<int16_t> buffer;
 			buffer.resize(p_count * component_count);
 			buffer.resize(p_count * component_count);
 			int32_t dst_i = 0;
 			int32_t dst_i = 0;
@@ -1231,7 +1236,7 @@ Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_
 			memcpy(gltf_buffer.ptrw() + old_size, buffer.ptrw(), buffer.size() * sizeof(int16_t));
 			memcpy(gltf_buffer.ptrw() + old_size, buffer.ptrw(), buffer.size() * sizeof(int16_t));
 			bv->byte_length = buffer.size() * sizeof(int16_t);
 			bv->byte_length = buffer.size() * sizeof(int16_t);
 		} break;
 		} break;
-		case COMPONENT_TYPE_UNSIGNED_SHORT: {
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {
 			Vector<uint16_t> buffer;
 			Vector<uint16_t> buffer;
 			buffer.resize(p_count * component_count);
 			buffer.resize(p_count * component_count);
 			int32_t dst_i = 0;
 			int32_t dst_i = 0;
@@ -1255,8 +1260,8 @@ Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_
 			memcpy(gltf_buffer.ptrw() + old_size, buffer.ptrw(), buffer.size() * sizeof(uint16_t));
 			memcpy(gltf_buffer.ptrw() + old_size, buffer.ptrw(), buffer.size() * sizeof(uint16_t));
 			bv->byte_length = buffer.size() * sizeof(uint16_t);
 			bv->byte_length = buffer.size() * sizeof(uint16_t);
 		} break;
 		} break;
-		case COMPONENT_TYPE_INT: {
-			Vector<int> buffer;
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT: {
+			Vector<uint32_t> buffer;
 			buffer.resize(p_count * component_count);
 			buffer.resize(p_count * component_count);
 			int32_t dst_i = 0;
 			int32_t dst_i = 0;
 			for (int i = 0; i < p_count; i++) {
 			for (int i = 0; i < p_count; i++) {
@@ -1271,11 +1276,11 @@ Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_
 				}
 				}
 			}
 			}
 			int64_t old_size = gltf_buffer.size();
 			int64_t old_size = gltf_buffer.size();
-			gltf_buffer.resize(old_size + (buffer.size() * sizeof(int32_t)));
-			memcpy(gltf_buffer.ptrw() + old_size, buffer.ptrw(), buffer.size() * sizeof(int32_t));
-			bv->byte_length = buffer.size() * sizeof(int32_t);
+			gltf_buffer.resize(old_size + (buffer.size() * sizeof(uint32_t)));
+			memcpy(gltf_buffer.ptrw() + old_size, buffer.ptrw(), buffer.size() * sizeof(uint32_t));
+			bv->byte_length = buffer.size() * sizeof(uint32_t);
 		} break;
 		} break;
-		case COMPONENT_TYPE_FLOAT: {
+		case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT: {
 			Vector<float> buffer;
 			Vector<float> buffer;
 			buffer.resize(p_count * component_count);
 			buffer.resize(p_count * component_count);
 			int32_t dst_i = 0;
 			int32_t dst_i = 0;
@@ -1309,7 +1314,7 @@ Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_
 	return OK;
 	return OK;
 }
 }
 
 
-Error GLTFDocument::_decode_buffer_view(Ref<GLTFState> p_state, double *p_dst, const GLTFBufferViewIndex p_buffer_view, const int p_skip_every, const int p_skip_bytes, const int p_element_size, const int p_count, const GLTFAccessor::GLTFAccessorType p_accessor_type, const int p_component_count, const int p_component_type, const int p_component_size, const bool p_normalized, const int p_byte_offset, const bool p_for_vertex) {
+Error GLTFDocument::_decode_buffer_view(Ref<GLTFState> p_state, double *p_dst, const GLTFBufferViewIndex p_buffer_view, const int p_skip_every, const int p_skip_bytes, const int p_element_size, const int p_count, const GLTFAccessor::GLTFAccessorType p_accessor_type, const int p_component_count, const GLTFAccessor::GLTFComponentType p_component_type, const int p_component_size, const bool p_normalized, const int p_byte_offset, const bool p_for_vertex) {
 	const Ref<GLTFBufferView> bv = p_state->buffer_views[p_buffer_view];
 	const Ref<GLTFBufferView> bv = p_state->buffer_views[p_buffer_view];
 
 
 	int stride = p_element_size;
 	int stride = p_element_size;
@@ -1348,7 +1353,10 @@ Error GLTFDocument::_decode_buffer_view(Ref<GLTFState> p_state, double *p_dst, c
 			double d = 0;
 			double d = 0;
 
 
 			switch (p_component_type) {
 			switch (p_component_type) {
-				case COMPONENT_TYPE_BYTE: {
+				case GLTFAccessor::COMPONENT_TYPE_NONE: {
+					ERR_FAIL_V_MSG(ERR_INVALID_DATA, "glTF: Failed to decode buffer view, component type not set.");
+				} break;
+				case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE: {
 					int8_t b = int8_t(*src);
 					int8_t b = int8_t(*src);
 					if (p_normalized) {
 					if (p_normalized) {
 						d = (double(b) / 128.0);
 						d = (double(b) / 128.0);
@@ -1356,7 +1364,7 @@ Error GLTFDocument::_decode_buffer_view(Ref<GLTFState> p_state, double *p_dst, c
 						d = double(b);
 						d = double(b);
 					}
 					}
 				} break;
 				} break;
-				case COMPONENT_TYPE_UNSIGNED_BYTE: {
+				case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {
 					uint8_t b = *src;
 					uint8_t b = *src;
 					if (p_normalized) {
 					if (p_normalized) {
 						d = (double(b) / 255.0);
 						d = (double(b) / 255.0);
@@ -1364,7 +1372,7 @@ Error GLTFDocument::_decode_buffer_view(Ref<GLTFState> p_state, double *p_dst, c
 						d = double(b);
 						d = double(b);
 					}
 					}
 				} break;
 				} break;
-				case COMPONENT_TYPE_SHORT: {
+				case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT: {
 					int16_t s = *(int16_t *)src;
 					int16_t s = *(int16_t *)src;
 					if (p_normalized) {
 					if (p_normalized) {
 						d = (double(s) / 32768.0);
 						d = (double(s) / 32768.0);
@@ -1372,7 +1380,7 @@ Error GLTFDocument::_decode_buffer_view(Ref<GLTFState> p_state, double *p_dst, c
 						d = double(s);
 						d = double(s);
 					}
 					}
 				} break;
 				} break;
-				case COMPONENT_TYPE_UNSIGNED_SHORT: {
+				case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {
 					uint16_t s = *(uint16_t *)src;
 					uint16_t s = *(uint16_t *)src;
 					if (p_normalized) {
 					if (p_normalized) {
 						d = (double(s) / 65535.0);
 						d = (double(s) / 65535.0);
@@ -1380,10 +1388,10 @@ Error GLTFDocument::_decode_buffer_view(Ref<GLTFState> p_state, double *p_dst, c
 						d = double(s);
 						d = double(s);
 					}
 					}
 				} break;
 				} break;
-				case COMPONENT_TYPE_INT: {
-					d = *(int *)src;
+				case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT: {
+					d = *(uint32_t *)src;
 				} break;
 				} break;
-				case COMPONENT_TYPE_FLOAT: {
+				case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT: {
 					d = *(float *)src;
 					d = *(float *)src;
 				} break;
 				} break;
 			}
 			}
@@ -1396,25 +1404,21 @@ Error GLTFDocument::_decode_buffer_view(Ref<GLTFState> p_state, double *p_dst, c
 	return OK;
 	return OK;
 }
 }
 
 
-int GLTFDocument::_get_component_type_size(const int p_component_type) {
+int GLTFDocument::_get_component_type_size(const GLTFAccessor::GLTFComponentType p_component_type) {
 	switch (p_component_type) {
 	switch (p_component_type) {
-		case COMPONENT_TYPE_BYTE:
-		case COMPONENT_TYPE_UNSIGNED_BYTE:
+		case GLTFAccessor::COMPONENT_TYPE_NONE:
+			ERR_FAIL_V(0);
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE:
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE:
 			return 1;
 			return 1;
-			break;
-		case COMPONENT_TYPE_SHORT:
-		case COMPONENT_TYPE_UNSIGNED_SHORT:
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT:
 			return 2;
 			return 2;
-			break;
-		case COMPONENT_TYPE_INT:
-		case COMPONENT_TYPE_FLOAT:
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT:
+		case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT:
 			return 4;
 			return 4;
-			break;
-		default: {
-			ERR_FAIL_V(0);
-		}
 	}
 	}
-	return 0;
+	ERR_FAIL_V(0);
 }
 }
 
 
 Vector<double> GLTFDocument::_decode_accessor(Ref<GLTFState> p_state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex) {
 Vector<double> GLTFDocument::_decode_accessor(Ref<GLTFState> p_state, const GLTFAccessorIndex p_accessor, const bool p_for_vertex) {
@@ -1425,11 +1429,7 @@ Vector<double> GLTFDocument::_decode_accessor(Ref<GLTFState> p_state, const GLTF
 
 
 	const Ref<GLTFAccessor> a = p_state->accessors[p_accessor];
 	const Ref<GLTFAccessor> a = p_state->accessors[p_accessor];
 
 
-	const int component_count_for_type[7] = {
-		1, 2, 3, 4, 4, 9, 16
-	};
-
-	const int component_count = component_count_for_type[a->accessor_type];
+	const int component_count = COMPONENT_COUNT_FOR_ACCESSOR_TYPE[a->accessor_type];
 	const int component_size = _get_component_type_size(a->component_type);
 	const int component_size = _get_component_type_size(a->component_type);
 	ERR_FAIL_COND_V(component_size == 0, Vector<double>());
 	ERR_FAIL_COND_V(component_size == 0, Vector<double>());
 	int element_size = component_count * component_size;
 	int element_size = component_count * component_size;
@@ -1438,8 +1438,8 @@ Vector<double> GLTFDocument::_decode_accessor(Ref<GLTFState> p_state, const GLTF
 	int skip_bytes = 0;
 	int skip_bytes = 0;
 	//special case of alignments, as described in spec
 	//special case of alignments, as described in spec
 	switch (a->component_type) {
 	switch (a->component_type) {
-		case COMPONENT_TYPE_BYTE:
-		case COMPONENT_TYPE_UNSIGNED_BYTE: {
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_BYTE:
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_BYTE: {
 			if (a->accessor_type == GLTFAccessor::TYPE_MAT2) {
 			if (a->accessor_type == GLTFAccessor::TYPE_MAT2) {
 				skip_every = 2;
 				skip_every = 2;
 				skip_bytes = 2;
 				skip_bytes = 2;
@@ -1451,8 +1451,8 @@ Vector<double> GLTFDocument::_decode_accessor(Ref<GLTFState> p_state, const GLTF
 				element_size = 12; //override for this case
 				element_size = 12; //override for this case
 			}
 			}
 		} break;
 		} break;
-		case COMPONENT_TYPE_SHORT:
-		case COMPONENT_TYPE_UNSIGNED_SHORT: {
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT: {
 			if (a->accessor_type == GLTFAccessor::TYPE_MAT3) {
 			if (a->accessor_type == GLTFAccessor::TYPE_MAT3) {
 				skip_every = 6;
 				skip_every = 6;
 				skip_bytes = 4;
 				skip_bytes = 4;
@@ -1550,11 +1550,11 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_ints(Ref<GLTFState> p_state,
 	}
 	}
 	int64_t size = p_state->buffers[0].size();
 	int64_t size = p_state->buffers[0].size();
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_SCALAR;
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_SCALAR;
-	int component_type;
+	GLTFAccessor::GLTFComponentType component_type;
 	if (max_index > 65535 || p_for_vertex) {
 	if (max_index > 65535 || p_for_vertex) {
-		component_type = GLTFDocument::COMPONENT_TYPE_INT;
+		component_type = GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT;
 	} else {
 	} else {
-		component_type = GLTFDocument::COMPONENT_TYPE_UNSIGNED_SHORT;
+		component_type = GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT;
 	}
 	}
 
 
 	accessor->max = type_max;
 	accessor->max = type_max;
@@ -1664,7 +1664,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_vec2(Ref<GLTFState> p_state,
 	}
 	}
 	int64_t size = p_state->buffers[0].size();
 	int64_t size = p_state->buffers[0].size();
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC2;
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC2;
-	const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;
+	const GLTFAccessor::GLTFComponentType component_type = GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT;
 
 
 	accessor->max = type_max;
 	accessor->max = type_max;
 	accessor->min = type_min;
 	accessor->min = type_min;
@@ -1717,7 +1717,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_color(Ref<GLTFState> p_state
 	}
 	}
 	int64_t size = p_state->buffers[0].size();
 	int64_t size = p_state->buffers[0].size();
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC4;
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC4;
-	const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;
+	const GLTFAccessor::GLTFComponentType component_type = GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT;
 
 
 	accessor->max = type_max;
 	accessor->max = type_max;
 	accessor->min = type_min;
 	accessor->min = type_min;
@@ -1784,7 +1784,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_weights(Ref<GLTFState> p_sta
 	}
 	}
 	int64_t size = p_state->buffers[0].size();
 	int64_t size = p_state->buffers[0].size();
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC4;
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC4;
-	const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;
+	const GLTFAccessor::GLTFComponentType component_type = GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT;
 
 
 	accessor->max = type_max;
 	accessor->max = type_max;
 	accessor->min = type_min;
 	accessor->min = type_min;
@@ -1835,7 +1835,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_joints(Ref<GLTFState> p_stat
 	}
 	}
 	int64_t size = p_state->buffers[0].size();
 	int64_t size = p_state->buffers[0].size();
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC4;
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC4;
-	const int component_type = GLTFDocument::COMPONENT_TYPE_UNSIGNED_SHORT;
+	const GLTFAccessor::GLTFComponentType component_type = GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT;
 
 
 	accessor->max = type_max;
 	accessor->max = type_max;
 	accessor->min = type_min;
 	accessor->min = type_min;
@@ -1888,7 +1888,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_quaternions(Ref<GLTFState> p
 	}
 	}
 	int64_t size = p_state->buffers[0].size();
 	int64_t size = p_state->buffers[0].size();
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC4;
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC4;
-	const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;
+	const GLTFAccessor::GLTFComponentType component_type = GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT;
 
 
 	accessor->max = type_max;
 	accessor->max = type_max;
 	accessor->min = type_min;
 	accessor->min = type_min;
@@ -1963,7 +1963,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_floats(Ref<GLTFState> p_stat
 	}
 	}
 	int64_t size = p_state->buffers[0].size();
 	int64_t size = p_state->buffers[0].size();
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_SCALAR;
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_SCALAR;
-	const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;
+	const GLTFAccessor::GLTFComponentType component_type = GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT;
 
 
 	accessor->max = type_max;
 	accessor->max = type_max;
 	accessor->min = type_min;
 	accessor->min = type_min;
@@ -2013,7 +2013,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_vec3(Ref<GLTFState> p_state,
 	}
 	}
 	int64_t size = p_state->buffers[0].size();
 	int64_t size = p_state->buffers[0].size();
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC3;
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC3;
-	const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;
+	const GLTFAccessor::GLTFComponentType component_type = GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT;
 
 
 	accessor->max = type_max;
 	accessor->max = type_max;
 	accessor->min = type_min;
 	accessor->min = type_min;
@@ -2089,7 +2089,7 @@ GLTFAccessorIndex GLTFDocument::_encode_sparse_accessor_as_vec3(Ref<GLTFState> p
 	}
 	}
 	int64_t size = p_state->buffers[0].size();
 	int64_t size = p_state->buffers[0].size();
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC3;
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_VEC3;
-	const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;
+	const GLTFAccessor::GLTFComponentType component_type = GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT;
 
 
 	sparse_accessor->normalized = false;
 	sparse_accessor->normalized = false;
 	sparse_accessor->count = p_attribs.size();
 	sparse_accessor->count = p_attribs.size();
@@ -2112,9 +2112,9 @@ GLTFAccessorIndex GLTFDocument::_encode_sparse_accessor_as_vec3(Ref<GLTFState> p
 		GLTFBufferIndex buffer_view_i_indices = -1;
 		GLTFBufferIndex buffer_view_i_indices = -1;
 		GLTFBufferIndex buffer_view_i_values = -1;
 		GLTFBufferIndex buffer_view_i_values = -1;
 		if (sparse_accessor_index_stride == 4) {
 		if (sparse_accessor_index_stride == 4) {
-			sparse_accessor->sparse_indices_component_type = GLTFDocument::COMPONENT_TYPE_INT;
+			sparse_accessor->sparse_indices_component_type = GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT;
 		} else {
 		} else {
-			sparse_accessor->sparse_indices_component_type = GLTFDocument::COMPONENT_TYPE_UNSIGNED_SHORT;
+			sparse_accessor->sparse_indices_component_type = GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT;
 		}
 		}
 		if (_encode_buffer_view(p_state, changed_indices.ptr(), changed_indices.size(), GLTFAccessor::TYPE_SCALAR, sparse_accessor->sparse_indices_component_type, sparse_accessor->normalized, sparse_accessor->sparse_indices_byte_offset, false, buffer_view_i_indices) != OK) {
 		if (_encode_buffer_view(p_state, changed_indices.ptr(), changed_indices.size(), GLTFAccessor::TYPE_SCALAR, sparse_accessor->sparse_indices_component_type, sparse_accessor->normalized, sparse_accessor->sparse_indices_byte_offset, false, buffer_view_i_indices) != OK) {
 			return -1;
 			return -1;
@@ -2194,7 +2194,7 @@ GLTFAccessorIndex GLTFDocument::_encode_accessor_as_xform(Ref<GLTFState> p_state
 	}
 	}
 	int64_t size = p_state->buffers[0].size();
 	int64_t size = p_state->buffers[0].size();
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_MAT4;
 	const GLTFAccessor::GLTFAccessorType accessor_type = GLTFAccessor::TYPE_MAT4;
-	const int component_type = GLTFDocument::COMPONENT_TYPE_FLOAT;
+	const GLTFAccessor::GLTFComponentType component_type = GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT;
 
 
 	accessor->max = type_max;
 	accessor->max = type_max;
 	accessor->min = type_min;
 	accessor->min = type_min;

+ 4 - 11
modules/gltf/gltf_document.h

@@ -56,13 +56,6 @@ public:
 	enum {
 	enum {
 		ARRAY_BUFFER = 34962,
 		ARRAY_BUFFER = 34962,
 		ELEMENT_ARRAY_BUFFER = 34963,
 		ELEMENT_ARRAY_BUFFER = 34963,
-
-		COMPONENT_TYPE_BYTE = 5120,
-		COMPONENT_TYPE_UNSIGNED_BYTE = 5121,
-		COMPONENT_TYPE_SHORT = 5122,
-		COMPONENT_TYPE_UNSIGNED_SHORT = 5123,
-		COMPONENT_TYPE_INT = 5125,
-		COMPONENT_TYPE_FLOAT = 5126,
 	};
 	};
 	enum {
 	enum {
 		TEXTURE_TYPE_GENERIC = 0,
 		TEXTURE_TYPE_GENERIC = 0,
@@ -109,8 +102,8 @@ private:
 	void _build_parent_hierachy(Ref<GLTFState> p_state);
 	void _build_parent_hierachy(Ref<GLTFState> p_state);
 	double _filter_number(double p_float);
 	double _filter_number(double p_float);
 	void _round_min_max_components(Vector<double> &r_type_min, Vector<double> &r_type_max);
 	void _round_min_max_components(Vector<double> &r_type_min, Vector<double> &r_type_max);
-	String _get_component_type_name(const uint32_t p_component);
-	int _get_component_type_size(const int p_component_type);
+	String _get_component_type_name(const GLTFAccessor::GLTFComponentType p_component_type);
+	int _get_component_type_size(const GLTFAccessor::GLTFComponentType p_component_type);
 	Error _parse_scenes(Ref<GLTFState> p_state);
 	Error _parse_scenes(Ref<GLTFState> p_state);
 	Error _parse_nodes(Ref<GLTFState> p_state);
 	Error _parse_nodes(Ref<GLTFState> p_state);
 	String _get_accessor_type_name(const GLTFAccessor::GLTFAccessorType p_accessor_type);
 	String _get_accessor_type_name(const GLTFAccessor::GLTFAccessorType p_accessor_type);
@@ -140,7 +133,7 @@ private:
 			const int p_skip_every, const int p_skip_bytes,
 			const int p_skip_every, const int p_skip_bytes,
 			const int p_element_size, const int p_count,
 			const int p_element_size, const int p_count,
 			const GLTFAccessor::GLTFAccessorType p_accessor_type, const int p_component_count,
 			const GLTFAccessor::GLTFAccessorType p_accessor_type, const int p_component_count,
-			const int p_component_type, const int p_component_size,
+			const GLTFAccessor::GLTFComponentType p_component_type, const int p_component_size,
 			const bool p_normalized, const int p_byte_offset,
 			const bool p_normalized, const int p_byte_offset,
 			const bool p_for_vertex);
 			const bool p_for_vertex);
 	Vector<double> _decode_accessor(Ref<GLTFState> p_state,
 	Vector<double> _decode_accessor(Ref<GLTFState> p_state,
@@ -269,7 +262,7 @@ private:
 			const bool p_for_vertex);
 			const bool p_for_vertex);
 	Error _encode_buffer_view(Ref<GLTFState> p_state, const double *p_src,
 	Error _encode_buffer_view(Ref<GLTFState> p_state, const double *p_src,
 			const int p_count, const GLTFAccessor::GLTFAccessorType p_accessor_type,
 			const int p_count, const GLTFAccessor::GLTFAccessorType p_accessor_type,
-			const int p_component_type, const bool p_normalized,
+			const GLTFAccessor::GLTFComponentType p_component_type, const bool p_normalized,
 			const int p_byte_offset, const bool p_for_vertex,
 			const int p_byte_offset, const bool p_for_vertex,
 			GLTFBufferViewIndex &r_accessor, const bool p_for_indices = false);
 			GLTFBufferViewIndex &r_accessor, const bool p_for_indices = false);
 
 

+ 10 - 2
modules/gltf/structures/gltf_accessor.cpp

@@ -39,6 +39,14 @@ void GLTFAccessor::_bind_methods() {
 	BIND_ENUM_CONSTANT(TYPE_MAT3);
 	BIND_ENUM_CONSTANT(TYPE_MAT3);
 	BIND_ENUM_CONSTANT(TYPE_MAT4);
 	BIND_ENUM_CONSTANT(TYPE_MAT4);
 
 
+	BIND_ENUM_CONSTANT(COMPONENT_TYPE_NONE);
+	BIND_ENUM_CONSTANT(COMPONENT_TYPE_SIGNED_BYTE);
+	BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_BYTE);
+	BIND_ENUM_CONSTANT(COMPONENT_TYPE_SIGNED_SHORT);
+	BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_SHORT);
+	BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_INT);
+	BIND_ENUM_CONSTANT(COMPONENT_TYPE_SINGLE_FLOAT);
+
 	ClassDB::bind_method(D_METHOD("get_buffer_view"), &GLTFAccessor::get_buffer_view);
 	ClassDB::bind_method(D_METHOD("get_buffer_view"), &GLTFAccessor::get_buffer_view);
 	ClassDB::bind_method(D_METHOD("set_buffer_view", "buffer_view"), &GLTFAccessor::set_buffer_view);
 	ClassDB::bind_method(D_METHOD("set_buffer_view", "buffer_view"), &GLTFAccessor::set_buffer_view);
 	ClassDB::bind_method(D_METHOD("get_byte_offset"), &GLTFAccessor::get_byte_offset);
 	ClassDB::bind_method(D_METHOD("get_byte_offset"), &GLTFAccessor::get_byte_offset);
@@ -108,7 +116,7 @@ int GLTFAccessor::get_component_type() {
 }
 }
 
 
 void GLTFAccessor::set_component_type(int p_component_type) {
 void GLTFAccessor::set_component_type(int p_component_type) {
-	component_type = p_component_type;
+	component_type = (GLTFComponentType)p_component_type;
 }
 }
 
 
 bool GLTFAccessor::get_normalized() {
 bool GLTFAccessor::get_normalized() {
@@ -188,7 +196,7 @@ int GLTFAccessor::get_sparse_indices_component_type() {
 }
 }
 
 
 void GLTFAccessor::set_sparse_indices_component_type(int p_sparse_indices_component_type) {
 void GLTFAccessor::set_sparse_indices_component_type(int p_sparse_indices_component_type) {
-	sparse_indices_component_type = p_sparse_indices_component_type;
+	sparse_indices_component_type = (GLTFComponentType)p_sparse_indices_component_type;
 }
 }
 
 
 int GLTFAccessor::get_sparse_values_buffer_view() {
 int GLTFAccessor::get_sparse_values_buffer_view() {

+ 13 - 2
modules/gltf/structures/gltf_accessor.h

@@ -50,10 +50,20 @@ public:
 		TYPE_MAT4,
 		TYPE_MAT4,
 	};
 	};
 
 
+	enum GLTFComponentType {
+		COMPONENT_TYPE_NONE = 0,
+		COMPONENT_TYPE_SIGNED_BYTE = 5120,
+		COMPONENT_TYPE_UNSIGNED_BYTE = 5121,
+		COMPONENT_TYPE_SIGNED_SHORT = 5122,
+		COMPONENT_TYPE_UNSIGNED_SHORT = 5123,
+		COMPONENT_TYPE_UNSIGNED_INT = 5125,
+		COMPONENT_TYPE_SINGLE_FLOAT = 5126,
+	};
+
 private:
 private:
 	GLTFBufferViewIndex buffer_view = -1;
 	GLTFBufferViewIndex buffer_view = -1;
 	int byte_offset = 0;
 	int byte_offset = 0;
-	int component_type = 0;
+	GLTFComponentType component_type = COMPONENT_TYPE_NONE;
 	bool normalized = false;
 	bool normalized = false;
 	int count = 0;
 	int count = 0;
 	GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_SCALAR;
 	GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_SCALAR;
@@ -62,7 +72,7 @@ private:
 	int sparse_count = 0;
 	int sparse_count = 0;
 	int sparse_indices_buffer_view = 0;
 	int sparse_indices_buffer_view = 0;
 	int sparse_indices_byte_offset = 0;
 	int sparse_indices_byte_offset = 0;
-	int sparse_indices_component_type = 0;
+	GLTFComponentType sparse_indices_component_type = COMPONENT_TYPE_NONE;
 	int sparse_values_buffer_view = 0;
 	int sparse_values_buffer_view = 0;
 	int sparse_values_byte_offset = 0;
 	int sparse_values_byte_offset = 0;
 
 
@@ -117,5 +127,6 @@ public:
 };
 };
 
 
 VARIANT_ENUM_CAST(GLTFAccessor::GLTFAccessorType);
 VARIANT_ENUM_CAST(GLTFAccessor::GLTFAccessorType);
+VARIANT_ENUM_CAST(GLTFAccessor::GLTFComponentType);
 
 
 #endif // GLTF_ACCESSOR_H
 #endif // GLTF_ACCESSOR_H