2
0
Эх сурвалжийг харах

GLTF: Add more accessor component types

Aaron Franke 1 жил өмнө
parent
commit
c7391d2ee3

+ 15 - 0
modules/gltf/doc_classes/GLTFAccessor.xml

@@ -95,11 +95,26 @@
 		<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_SIGNED_INT" value="5124" enum="GLTFComponentType">
+			Component type "INT". The value is [code]0x1404[/code] which comes from OpenGL. This indicates data is stored in 4-byte or 32-bit signed integers. This is NOT a core part of the glTF specification, and may not be supported by all glTF importers. May be used by some extensions including [code]KHR_interactivity[/code].
+		</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>
+		<constant name="COMPONENT_TYPE_DOUBLE_FLOAT" value="5130" enum="GLTFComponentType">
+			Component type "DOUBLE". The value is [code]0x140A[/code] which comes from OpenGL. This indicates data is stored in 8-byte or 64-bit floating point numbers. This is NOT a core part of the glTF specification, and may not be supported by all glTF importers. May be used by some extensions including [code]KHR_interactivity[/code].
+		</constant>
+		<constant name="COMPONENT_TYPE_HALF_FLOAT" value="5131" enum="GLTFComponentType">
+			Component type "HALF_FLOAT". The value is [code]0x140B[/code] which comes from OpenGL. This indicates data is stored in 2-byte or 16-bit floating point numbers. This is NOT a core part of the glTF specification, and may not be supported by all glTF importers. May be used by some extensions including [code]KHR_interactivity[/code].
+		</constant>
+		<constant name="COMPONENT_TYPE_SIGNED_LONG" value="5134" enum="GLTFComponentType">
+			Component type "LONG". The value is [code]0x140E[/code] which comes from OpenGL. This indicates data is stored in 8-byte or 64-bit signed integers. This is NOT a core part of the glTF specification, and may not be supported by all glTF importers. May be used by some extensions including [code]KHR_interactivity[/code].
+		</constant>
+		<constant name="COMPONENT_TYPE_UNSIGNED_LONG" value="5135" enum="GLTFComponentType">
+			Component type "UNSIGNED_LONG". The value is [code]0x140F[/code] which comes from OpenGL. This indicates data is stored in 8-byte or 64-bit unsigned integers. This is NOT a core part of the glTF specification, and may not be supported by all glTF importers. May be used by some extensions including [code]KHR_interactivity[/code].
+		</constant>
 	</constants>
 </class>

+ 116 - 0
modules/gltf/gltf_document.cpp

@@ -1098,10 +1098,20 @@ String GLTFDocument::_get_component_type_name(const GLTFAccessor::GLTFComponentT
 			return "Short";
 		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT:
 			return "UShort";
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_INT:
+			return "Int";
 		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT:
 			return "UInt";
 		case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT:
 			return "Float";
+		case GLTFAccessor::COMPONENT_TYPE_DOUBLE_FLOAT:
+			return "Double";
+		case GLTFAccessor::COMPONENT_TYPE_HALF_FLOAT:
+			return "Half";
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_LONG:
+			return "Long";
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG:
+			return "ULong";
 	}
 
 	return "<Error>";
@@ -1260,6 +1270,26 @@ 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));
 			bv->byte_length = buffer.size() * sizeof(uint16_t);
 		} break;
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_INT: {
+			Vector<int32_t> buffer;
+			buffer.resize(p_count * component_count);
+			int32_t dst_i = 0;
+			for (int i = 0; i < p_count; i++) {
+				for (int j = 0; j < component_count; j++) {
+					if (skip_every && j > 0 && (j % skip_every) == 0) {
+						dst_i += skip_bytes;
+					}
+					double d = *p_src;
+					buffer.write[dst_i] = d;
+					p_src++;
+					dst_i++;
+				}
+			}
+			int64_t old_size = gltf_buffer.size();
+			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;
 		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT: {
 			Vector<uint32_t> buffer;
 			buffer.resize(p_count * component_count);
@@ -1300,6 +1330,71 @@ Error GLTFDocument::_encode_buffer_view(Ref<GLTFState> p_state, const double *p_
 			memcpy(gltf_buffer.ptrw() + old_size, buffer.ptrw(), buffer.size() * sizeof(float));
 			bv->byte_length = buffer.size() * sizeof(float);
 		} break;
+		case GLTFAccessor::COMPONENT_TYPE_DOUBLE_FLOAT: {
+			Vector<double> buffer;
+			buffer.resize(p_count * component_count);
+			int32_t dst_i = 0;
+			for (int i = 0; i < p_count; i++) {
+				for (int j = 0; j < component_count; j++) {
+					if (skip_every && j > 0 && (j % skip_every) == 0) {
+						dst_i += skip_bytes;
+					}
+					double d = *p_src;
+					buffer.write[dst_i] = d;
+					p_src++;
+					dst_i++;
+				}
+			}
+			int64_t old_size = gltf_buffer.size();
+			gltf_buffer.resize(old_size + (buffer.size() * sizeof(double)));
+			memcpy(gltf_buffer.ptrw() + old_size, buffer.ptrw(), buffer.size() * sizeof(double));
+			bv->byte_length = buffer.size() * sizeof(double);
+		} break;
+		case GLTFAccessor::COMPONENT_TYPE_HALF_FLOAT: {
+			ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "glTF: Half float not supported yet.");
+		} break;
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_LONG: {
+			Vector<int64_t> buffer;
+			buffer.resize(p_count * component_count);
+			int32_t dst_i = 0;
+			for (int i = 0; i < p_count; i++) {
+				for (int j = 0; j < component_count; j++) {
+					if (skip_every && j > 0 && (j % skip_every) == 0) {
+						dst_i += skip_bytes;
+					}
+					// FIXME: This can result in precision loss because int64_t can store some values that double can't.
+					double d = *p_src;
+					buffer.write[dst_i] = d;
+					p_src++;
+					dst_i++;
+				}
+			}
+			int64_t old_size = gltf_buffer.size();
+			gltf_buffer.resize(old_size + (buffer.size() * sizeof(int64_t)));
+			memcpy(gltf_buffer.ptrw() + old_size, buffer.ptrw(), buffer.size() * sizeof(int64_t));
+			bv->byte_length = buffer.size() * sizeof(int64_t);
+		} break;
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG: {
+			Vector<uint64_t> buffer;
+			buffer.resize(p_count * component_count);
+			int32_t dst_i = 0;
+			for (int i = 0; i < p_count; i++) {
+				for (int j = 0; j < component_count; j++) {
+					if (skip_every && j > 0 && (j % skip_every) == 0) {
+						dst_i += skip_bytes;
+					}
+					// FIXME: This can result in precision loss because int64_t can store some values that double can't.
+					double d = *p_src;
+					buffer.write[dst_i] = d;
+					p_src++;
+					dst_i++;
+				}
+			}
+			int64_t old_size = gltf_buffer.size();
+			gltf_buffer.resize(old_size + (buffer.size() * sizeof(uint64_t)));
+			memcpy(gltf_buffer.ptrw() + old_size, buffer.ptrw(), buffer.size() * sizeof(uint64_t));
+			bv->byte_length = buffer.size() * sizeof(uint64_t);
+		} break;
 	}
 	ERR_FAIL_COND_V(buffer_end > bv->byte_length, ERR_INVALID_DATA);
 
@@ -1388,12 +1483,27 @@ Error GLTFDocument::_decode_buffer_view(Ref<GLTFState> p_state, double *p_dst, c
 						d = double(s);
 					}
 				} break;
+				case GLTFAccessor::COMPONENT_TYPE_SIGNED_INT: {
+					d = *(int32_t *)src;
+				} break;
 				case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT: {
 					d = *(uint32_t *)src;
 				} break;
 				case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT: {
 					d = *(float *)src;
 				} break;
+				case GLTFAccessor::COMPONENT_TYPE_DOUBLE_FLOAT: {
+					d = *(double *)src;
+				} break;
+				case GLTFAccessor::COMPONENT_TYPE_HALF_FLOAT: {
+					ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "glTF: Half float not supported yet.");
+				} break;
+				case GLTFAccessor::COMPONENT_TYPE_SIGNED_LONG: {
+					d = *(int64_t *)src;
+				} break;
+				case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG: {
+					d = *(uint64_t *)src;
+				} break;
 			}
 
 			*p_dst++ = d;
@@ -1413,10 +1523,16 @@ int GLTFDocument::_get_component_type_size(const GLTFAccessor::GLTFComponentType
 			return 1;
 		case GLTFAccessor::COMPONENT_TYPE_SIGNED_SHORT:
 		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_SHORT:
+		case GLTFAccessor::COMPONENT_TYPE_HALF_FLOAT:
 			return 2;
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_INT:
 		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_INT:
 		case GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT:
 			return 4;
+		case GLTFAccessor::COMPONENT_TYPE_DOUBLE_FLOAT:
+		case GLTFAccessor::COMPONENT_TYPE_SIGNED_LONG:
+		case GLTFAccessor::COMPONENT_TYPE_UNSIGNED_LONG:
+			return 8;
 	}
 	ERR_FAIL_V(0);
 }

+ 5 - 0
modules/gltf/structures/gltf_accessor.cpp

@@ -44,8 +44,13 @@ void GLTFAccessor::_bind_methods() {
 	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_SIGNED_INT);
 	BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_INT);
 	BIND_ENUM_CONSTANT(COMPONENT_TYPE_SINGLE_FLOAT);
+	BIND_ENUM_CONSTANT(COMPONENT_TYPE_DOUBLE_FLOAT);
+	BIND_ENUM_CONSTANT(COMPONENT_TYPE_HALF_FLOAT);
+	BIND_ENUM_CONSTANT(COMPONENT_TYPE_SIGNED_LONG);
+	BIND_ENUM_CONSTANT(COMPONENT_TYPE_UNSIGNED_LONG);
 
 	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);

+ 5 - 0
modules/gltf/structures/gltf_accessor.h

@@ -56,8 +56,13 @@ public:
 		COMPONENT_TYPE_UNSIGNED_BYTE = 5121,
 		COMPONENT_TYPE_SIGNED_SHORT = 5122,
 		COMPONENT_TYPE_UNSIGNED_SHORT = 5123,
+		COMPONENT_TYPE_SIGNED_INT = 5124,
 		COMPONENT_TYPE_UNSIGNED_INT = 5125,
 		COMPONENT_TYPE_SINGLE_FLOAT = 5126,
+		COMPONENT_TYPE_DOUBLE_FLOAT = 5130,
+		COMPONENT_TYPE_HALF_FLOAT = 5131,
+		COMPONENT_TYPE_SIGNED_LONG = 5134,
+		COMPONENT_TYPE_UNSIGNED_LONG = 5135,
 	};
 
 private: