initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled

This commit is contained in:
2025-09-16 20:46:46 -04:00
commit 9d30169a8d
13378 changed files with 7050105 additions and 0 deletions

11
modules/gltf/README.md Normal file
View File

@@ -0,0 +1,11 @@
# Godot glTF import and export module
In a nutshell, the glTF module works like this:
* The [`structures/`](structures/) folder contains glTF structures, the
small pieces that make up a glTF file, represented as C++ classes.
* The [`extensions/`](extensions/) folder contains glTF extensions, which
are optional features that build on top of the base glTF spec.
* [`GLTFState`](gltf_state.h) holds collections of structures and extensions.
* [`GLTFDocument`](gltf_document.h) operates on GLTFState and its elements.
* The [`editor/`](editor/) folder uses GLTFDocument to import and export 3D models.

17
modules/gltf/SCsub Normal file
View File

@@ -0,0 +1,17 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *
Import("env")
Import("env_modules")
env_gltf = env_modules.Clone()
# Godot source files
env_gltf.add_source_files(env.modules_sources, "*.cpp")
env_gltf.add_source_files(env.modules_sources, "structures/*.cpp")
SConscript("extensions/SCsub")
if env.editor_build:
env_gltf.add_source_files(env.modules_sources, "editor/*.cpp")

37
modules/gltf/config.py Normal file
View File

@@ -0,0 +1,37 @@
def can_build(env, platform):
env.module_add_dependencies("gltf", ["csg", "gridmap"], True)
return not env["disable_3d"]
def configure(env):
pass
def get_doc_classes():
return [
"EditorSceneFormatImporterBlend",
"EditorSceneFormatImporterGLTF",
"GLTFAccessor",
"GLTFAnimation",
"GLTFBufferView",
"GLTFCamera",
"GLTFDocument",
"GLTFDocumentExtension",
"GLTFDocumentExtensionConvertImporterMesh",
"GLTFLight",
"GLTFMesh",
"GLTFNode",
"GLTFObjectModelProperty",
"GLTFPhysicsBody",
"GLTFPhysicsShape",
"GLTFSkeleton",
"GLTFSkin",
"GLTFSpecGloss",
"GLTFState",
"GLTFTexture",
"GLTFTextureSampler",
]
def get_doc_path():
return "doc_classes"

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="EditorSceneFormatImporterBlend" inherits="EditorSceneFormatImporter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Importer for Blender's [code].blend[/code] scene file format.
</brief_description>
<description>
Imports Blender scenes in the [code].blend[/code] file format through the glTF 2.0 3D import pipeline. This importer requires Blender to be installed by the user, so that it can be used to export the scene as glTF 2.0.
The location of the Blender binary is set via the [member EditorSettings.filesystem/import/blender/blender_path] setting.
This importer is only used if [member ProjectSettings.filesystem/import/blender/enabled] is enabled, otherwise [code].blend[/code] files present in the project folder are not imported.
Blend import requires Blender 3.0.
Internally, the EditorSceneFormatImporterBlend uses the Blender glTF "Use Original" mode to reference external textures.
</description>
<tutorials>
</tutorials>
</class>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="EditorSceneFormatImporterGLTF" inherits="EditorSceneFormatImporter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
</class>

View File

@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFAccessor" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Represents a glTF accessor.
</brief_description>
<description>
GLTFAccessor is a data structure representing a glTF [code]accessor[/code] that would be found in the [code]"accessors"[/code] array. A buffer is a blob of binary data. A buffer view is a slice of a buffer. An accessor is a typed interpretation of the data in a buffer view.
Most custom data stored in glTF does not need accessors, only buffer views (see [GLTFBufferView]). Accessors are for more advanced use cases such as interleaved mesh data encoded for the GPU.
</description>
<tutorials>
<link title="Buffers, BufferViews, and Accessors in Khronos glTF specification">https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_005_BuffersBufferViewsAccessors.md</link>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
</tutorials>
<members>
<member name="accessor_type" type="int" setter="set_accessor_type" getter="get_accessor_type" enum="GLTFAccessor.GLTFAccessorType" default="0">
The glTF accessor type, as an enum.
</member>
<member name="buffer_view" type="int" setter="set_buffer_view" getter="get_buffer_view" default="-1">
The index of the buffer view this accessor is referencing. If [code]-1[/code], this accessor is not referencing any buffer view.
</member>
<member name="byte_offset" type="int" setter="set_byte_offset" getter="get_byte_offset" default="0">
The offset relative to the start of the buffer view in bytes.
</member>
<member name="component_type" type="int" setter="set_component_type" getter="get_component_type" enum="GLTFAccessor.GLTFComponentType" default="0">
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 name="count" type="int" setter="set_count" getter="get_count" default="0">
The number of elements referenced by this accessor.
</member>
<member name="max" type="PackedFloat64Array" setter="set_max" getter="get_max" default="PackedFloat64Array()">
Maximum value of each component in this accessor.
</member>
<member name="min" type="PackedFloat64Array" setter="set_min" getter="get_min" default="PackedFloat64Array()">
Minimum value of each component in this accessor.
</member>
<member name="normalized" type="bool" setter="set_normalized" getter="get_normalized" default="false">
Specifies whether integer data values are normalized before usage.
</member>
<member name="sparse_count" type="int" setter="set_sparse_count" getter="get_sparse_count" default="0">
Number of deviating accessor values stored in the sparse array.
</member>
<member name="sparse_indices_buffer_view" type="int" setter="set_sparse_indices_buffer_view" getter="get_sparse_indices_buffer_view" default="0">
The index of the buffer view with sparse indices. The referenced buffer view MUST NOT have its target or byteStride properties defined. The buffer view and the optional byteOffset MUST be aligned to the componentType byte length.
</member>
<member name="sparse_indices_byte_offset" type="int" setter="set_sparse_indices_byte_offset" getter="get_sparse_indices_byte_offset" default="0">
The offset relative to the start of the buffer view in bytes.
</member>
<member name="sparse_indices_component_type" type="int" setter="set_sparse_indices_component_type" getter="get_sparse_indices_component_type" enum="GLTFAccessor.GLTFComponentType" default="0">
The indices component data type as an enum. Possible values are 5121 for "UNSIGNED_BYTE", 5123 for "UNSIGNED_SHORT", and 5125 for "UNSIGNED_INT".
</member>
<member name="sparse_values_buffer_view" type="int" setter="set_sparse_values_buffer_view" getter="get_sparse_values_buffer_view" default="0">
The index of the bufferView with sparse values. The referenced buffer view MUST NOT have its target or byteStride properties defined.
</member>
<member name="sparse_values_byte_offset" type="int" setter="set_sparse_values_byte_offset" getter="get_sparse_values_byte_offset" default="0">
The offset relative to the start of the bufferView in bytes.
</member>
<member name="type" type="int" setter="set_type" getter="get_type" deprecated="Use [member accessor_type] instead.">
The glTF accessor type, as an [int]. Possible values are [code]0[/code] for "SCALAR", [code]1[/code] for "VEC2", [code]2[/code] for "VEC3", [code]3[/code] for "VEC4", [code]4[/code] for "MAT2", [code]5[/code] for "MAT3", and [code]6[/code] for "MAT4".
</member>
</members>
<constants>
<constant name="TYPE_SCALAR" value="0" enum="GLTFAccessorType">
Accessor type "SCALAR". For the glTF object model, this can be used to map to a single float, int, or bool value, or a float array.
</constant>
<constant name="TYPE_VEC2" value="1" enum="GLTFAccessorType">
Accessor type "VEC2". For the glTF object model, this maps to "float2", represented in the glTF JSON as an array of two floats.
</constant>
<constant name="TYPE_VEC3" value="2" enum="GLTFAccessorType">
Accessor type "VEC3". For the glTF object model, this maps to "float3", represented in the glTF JSON as an array of three floats.
</constant>
<constant name="TYPE_VEC4" value="3" enum="GLTFAccessorType">
Accessor type "VEC4". For the glTF object model, this maps to "float4", represented in the glTF JSON as an array of four floats.
</constant>
<constant name="TYPE_MAT2" value="4" enum="GLTFAccessorType">
Accessor type "MAT2". For the glTF object model, this maps to "float2x2", represented in the glTF JSON as an array of four floats.
</constant>
<constant name="TYPE_MAT3" value="5" enum="GLTFAccessorType">
Accessor type "MAT3". For the glTF object model, this maps to "float3x3", represented in the glTF JSON as an array of nine floats.
</constant>
<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.
</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_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>

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFAnimation" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
</tutorials>
<methods>
<method name="get_additional_data">
<return type="Variant" />
<param index="0" name="extension_name" type="StringName" />
<description>
Gets additional arbitrary data in this [GLTFAnimation] instance. This can be used to keep per-node state data in [GLTFDocumentExtension] classes, which is important because they are stateless.
The argument should be the [GLTFDocumentExtension] name (does not have to match the extension name in the glTF file), and the return value can be anything you set. If nothing was set, the return value is [code]null[/code].
</description>
</method>
<method name="set_additional_data">
<return type="void" />
<param index="0" name="extension_name" type="StringName" />
<param index="1" name="additional_data" type="Variant" />
<description>
Sets additional arbitrary data in this [GLTFAnimation] instance. This can be used to keep per-node state data in [GLTFDocumentExtension] classes, which is important because they are stateless.
The first argument should be the [GLTFDocumentExtension] name (does not have to match the extension name in the glTF file), and the second argument can be anything you want.
</description>
</method>
</methods>
<members>
<member name="loop" type="bool" setter="set_loop" getter="get_loop" default="false">
</member>
<member name="original_name" type="String" setter="set_original_name" getter="get_original_name" default="&quot;&quot;">
The original name of the animation.
</member>
</members>
</class>

View File

@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFBufferView" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Represents a glTF buffer view.
</brief_description>
<description>
GLTFBufferView is a data structure representing a glTF [code]bufferView[/code] that would be found in the [code]"bufferViews"[/code] array. A buffer is a blob of binary data. A buffer view is a slice of a buffer that can be used to identify and extract data from the buffer.
Most custom uses of buffers only need to use the [member buffer], [member byte_length], and [member byte_offset]. The [member byte_stride] and [member indices] properties are for more advanced use cases such as interleaved mesh data encoded for the GPU.
</description>
<tutorials>
<link title="Buffers, BufferViews, and Accessors in Khronos glTF specification">https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_005_BuffersBufferViewsAccessors.md</link>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
</tutorials>
<methods>
<method name="load_buffer_view_data" qualifiers="const">
<return type="PackedByteArray" />
<param index="0" name="state" type="GLTFState" />
<description>
Loads the buffer view data from the buffer referenced by this buffer view in the given [GLTFState]. Interleaved data with a byte stride is not yet supported by this method. The data is returned as a [PackedByteArray].
</description>
</method>
</methods>
<members>
<member name="buffer" type="int" setter="set_buffer" getter="get_buffer" default="-1">
The index of the buffer this buffer view is referencing. If [code]-1[/code], this buffer view is not referencing any buffer.
</member>
<member name="byte_length" type="int" setter="set_byte_length" getter="get_byte_length" default="0">
The length, in bytes, of this buffer view. If [code]0[/code], this buffer view is empty.
</member>
<member name="byte_offset" type="int" setter="set_byte_offset" getter="get_byte_offset" default="0">
The offset, in bytes, from the start of the buffer to the start of this buffer view.
</member>
<member name="byte_stride" type="int" setter="set_byte_stride" getter="get_byte_stride" default="-1">
The stride, in bytes, between interleaved data. If [code]-1[/code], this buffer view is not interleaved.
</member>
<member name="indices" type="bool" setter="set_indices" getter="get_indices" default="false">
[code]true[/code] if the GLTFBufferView's OpenGL GPU buffer type is an [code]ELEMENT_ARRAY_BUFFER[/code] used for vertex indices (integer constant [code]34963[/code]). [code]false[/code] if the buffer type is any other value. See [url=https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_005_BuffersBufferViewsAccessors.md]Buffers, BufferViews, and Accessors[/url] for possible values. This property is set on import and used on export.
</member>
<member name="vertex_attributes" type="bool" setter="set_vertex_attributes" getter="get_vertex_attributes" default="false">
[code]true[/code] if the GLTFBufferView's OpenGL GPU buffer type is an [code]ARRAY_BUFFER[/code] used for vertex attributes (integer constant [code]34962[/code]). [code]false[/code] if the buffer type is any other value. See [url=https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_005_BuffersBufferViewsAccessors.md]Buffers, BufferViews, and Accessors[/url] for possible values. This property is set on import and used on export.
</member>
</members>
</class>

View File

@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFCamera" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Represents a glTF camera.
</brief_description>
<description>
Represents a camera as defined by the base glTF spec.
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
<link title="glTF camera detailed specification">https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-camera</link>
<link title="glTF camera spec and example file">https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_015_SimpleCameras.md</link>
</tutorials>
<methods>
<method name="from_dictionary" qualifiers="static">
<return type="GLTFCamera" />
<param index="0" name="dictionary" type="Dictionary" />
<description>
Creates a new GLTFCamera instance by parsing the given [Dictionary].
</description>
</method>
<method name="from_node" qualifiers="static">
<return type="GLTFCamera" />
<param index="0" name="camera_node" type="Camera3D" />
<description>
Create a new GLTFCamera instance from the given Godot [Camera3D] node.
</description>
</method>
<method name="to_dictionary" qualifiers="const">
<return type="Dictionary" />
<description>
Serializes this GLTFCamera instance into a [Dictionary].
</description>
</method>
<method name="to_node" qualifiers="const">
<return type="Camera3D" />
<description>
Converts this GLTFCamera instance into a Godot [Camera3D] node.
</description>
</method>
</methods>
<members>
<member name="depth_far" type="float" setter="set_depth_far" getter="get_depth_far" default="4000.0">
The distance to the far culling boundary for this camera relative to its local Z axis, in meters. This maps to glTF's [code]zfar[/code] property.
</member>
<member name="depth_near" type="float" setter="set_depth_near" getter="get_depth_near" default="0.05">
The distance to the near culling boundary for this camera relative to its local Z axis, in meters. This maps to glTF's [code]znear[/code] property.
</member>
<member name="fov" type="float" setter="set_fov" getter="get_fov" default="1.3089969">
The FOV of the camera. This class and glTF define the camera FOV in radians, while Godot uses degrees. This maps to glTF's [code]yfov[/code] property. This value is only used for perspective cameras, when [member perspective] is [code]true[/code].
</member>
<member name="perspective" type="bool" setter="set_perspective" getter="get_perspective" default="true">
If [code]true[/code], the camera is in perspective mode. Otherwise, the camera is in orthographic/orthogonal mode. This maps to glTF's camera [code]type[/code] property. See [member Camera3D.projection] and the glTF spec for more information.
</member>
<member name="size_mag" type="float" setter="set_size_mag" getter="get_size_mag" default="0.5">
The size of the camera. This class and glTF define the camera size magnitude as a radius in meters, while Godot defines it as a diameter in meters. This maps to glTF's [code]ymag[/code] property. This value is only used for orthographic/orthogonal cameras, when [member perspective] is [code]false[/code].
</member>
</members>
</class>

View File

@@ -0,0 +1,160 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFDocument" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Class for importing and exporting glTF files in and out of Godot.
</brief_description>
<description>
GLTFDocument supports reading data from a glTF file, buffer, or Godot scene. This data can then be written to the filesystem, buffer, or used to create a Godot scene.
All of the data in a glTF scene is stored in the [GLTFState] class. GLTFDocument processes state objects, but does not contain any scene data itself. GLTFDocument has member variables to store export configuration settings such as the image format, but is otherwise stateless. Multiple scenes can be processed with the same settings using the same GLTFDocument object and different [GLTFState] objects.
GLTFDocument can be extended with arbitrary functionality by extending the [GLTFDocumentExtension] class and registering it with GLTFDocument via [method register_gltf_document_extension]. This allows for custom data to be imported and exported.
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
<link title="glTF &apos;What the duck?&apos; guide">https://www.khronos.org/files/gltf20-reference-guide.pdf</link>
<link title="Khronos glTF specification">https://registry.khronos.org/glTF/</link>
</tutorials>
<methods>
<method name="append_from_buffer">
<return type="int" enum="Error" />
<param index="0" name="bytes" type="PackedByteArray" />
<param index="1" name="base_path" type="String" />
<param index="2" name="state" type="GLTFState" />
<param index="3" name="flags" type="int" default="0" />
<description>
Takes a [PackedByteArray] defining a glTF and imports the data to the given [GLTFState] object through the [param state] parameter.
[b]Note:[/b] The [param base_path] tells [method append_from_buffer] where to find dependencies and can be empty.
</description>
</method>
<method name="append_from_file">
<return type="int" enum="Error" />
<param index="0" name="path" type="String" />
<param index="1" name="state" type="GLTFState" />
<param index="2" name="flags" type="int" default="0" />
<param index="3" name="base_path" type="String" default="&quot;&quot;" />
<description>
Takes a path to a glTF file and imports the data at that file path to the given [GLTFState] object through the [param state] parameter.
[b]Note:[/b] The [param base_path] tells [method append_from_file] where to find dependencies and can be empty.
</description>
</method>
<method name="append_from_scene">
<return type="int" enum="Error" />
<param index="0" name="node" type="Node" />
<param index="1" name="state" type="GLTFState" />
<param index="2" name="flags" type="int" default="0" />
<description>
Takes a Godot Engine scene node and exports it and its descendants to the given [GLTFState] object through the [param state] parameter.
</description>
</method>
<method name="export_object_model_property" qualifiers="static">
<return type="GLTFObjectModelProperty" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="node_path" type="NodePath" />
<param index="2" name="godot_node" type="Node" />
<param index="3" name="gltf_node_index" type="int" />
<description>
Determines a mapping between the given Godot [param node_path] and the corresponding glTF Object Model JSON pointer(s) in the generated glTF file. The details of this mapping are returned in a [GLTFObjectModelProperty] object. Additional mappings can be supplied via the [method GLTFDocumentExtension._import_object_model_property] callback method.
</description>
</method>
<method name="generate_buffer">
<return type="PackedByteArray" />
<param index="0" name="state" type="GLTFState" />
<description>
Takes a [GLTFState] object through the [param state] parameter and returns a glTF [PackedByteArray].
</description>
</method>
<method name="generate_scene">
<return type="Node" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="bake_fps" type="float" default="30" />
<param index="2" name="trimming" type="bool" default="false" />
<param index="3" name="remove_immutable_tracks" type="bool" default="true" />
<description>
Takes a [GLTFState] object through the [param state] parameter and returns a Godot Engine scene node.
The [param bake_fps] parameter overrides the bake_fps in [param state].
</description>
</method>
<method name="get_supported_gltf_extensions" qualifiers="static">
<return type="PackedStringArray" />
<description>
Returns a list of all support glTF extensions, including extensions supported directly by the engine, and extensions supported by user plugins registering [GLTFDocumentExtension] classes.
[b]Note:[/b] If this method is run before a GLTFDocumentExtension is registered, its extensions won't be included in the list. Be sure to only run this method after all extensions are registered. If you run this when the engine starts, consider waiting a frame before calling this method to ensure all extensions are registered.
</description>
</method>
<method name="import_object_model_property" qualifiers="static">
<return type="GLTFObjectModelProperty" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="json_pointer" type="String" />
<description>
Determines a mapping between the given glTF Object Model [param json_pointer] and the corresponding Godot node path(s) in the generated Godot scene. The details of this mapping are returned in a [GLTFObjectModelProperty] object. Additional mappings can be supplied via the [method GLTFDocumentExtension._export_object_model_property] callback method.
</description>
</method>
<method name="register_gltf_document_extension" qualifiers="static">
<return type="void" />
<param index="0" name="extension" type="GLTFDocumentExtension" />
<param index="1" name="first_priority" type="bool" default="false" />
<description>
Registers the given [GLTFDocumentExtension] instance with GLTFDocument. If [param first_priority] is [code]true[/code], this extension will be run first. Otherwise, it will be run last.
[b]Note:[/b] Like GLTFDocument itself, all GLTFDocumentExtension classes must be stateless in order to function properly. If you need to store data, use the [code]set_additional_data[/code] and [code]get_additional_data[/code] methods in [GLTFState] or [GLTFNode].
</description>
</method>
<method name="unregister_gltf_document_extension" qualifiers="static">
<return type="void" />
<param index="0" name="extension" type="GLTFDocumentExtension" />
<description>
Unregisters the given [GLTFDocumentExtension] instance.
</description>
</method>
<method name="write_to_filesystem">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="path" type="String" />
<description>
Takes a [GLTFState] object through the [param state] parameter and writes a glTF file to the filesystem.
[b]Note:[/b] The extension of the glTF file determines if it is a .glb binary file or a .gltf text file.
</description>
</method>
</methods>
<members>
<member name="fallback_image_format" type="String" setter="set_fallback_image_format" getter="get_fallback_image_format" default="&quot;None&quot;">
The user-friendly name of the fallback image format. This is used when exporting the glTF file, including writing to a file and writing to a byte array.
This property may only be one of "None", "PNG", or "JPEG", and is only used when the [member image_format] is not one of "None", "PNG", or "JPEG". If having multiple extension image formats is desired, that can be done using a [GLTFDocumentExtension] class - this property only covers the use case of providing a base glTF fallback image when using a custom image format.
</member>
<member name="fallback_image_quality" type="float" setter="set_fallback_image_quality" getter="get_fallback_image_quality" default="0.25">
The quality of the fallback image, if any. For PNG files, this downscales the image on both dimensions by this factor. For JPEG files, this is the lossy quality of the image. A low value is recommended, since including multiple high quality images in a glTF file defeats the file size gains of using a more efficient image format.
</member>
<member name="image_format" type="String" setter="set_image_format" getter="get_image_format" default="&quot;PNG&quot;">
The user-friendly name of the export image format. This is used when exporting the glTF file, including writing to a file and writing to a byte array.
By default, Godot allows the following options: "None", "PNG", "JPEG", "Lossless WebP", and "Lossy WebP". Support for more image formats can be added in [GLTFDocumentExtension] classes. A single extension class can provide multiple options for the specific format to use, or even an option that uses multiple formats at once.
</member>
<member name="lossy_quality" type="float" setter="set_lossy_quality" getter="get_lossy_quality" default="0.75">
If [member image_format] is a lossy image format, this determines the lossy quality of the image. On a range of [code]0.0[/code] to [code]1.0[/code], where [code]0.0[/code] is the lowest quality and [code]1.0[/code] is the highest quality. A lossy quality of [code]1.0[/code] is not the same as lossless.
</member>
<member name="root_node_mode" type="int" setter="set_root_node_mode" getter="get_root_node_mode" enum="GLTFDocument.RootNodeMode" default="0">
How to process the root node during export. The default and recommended value is [constant ROOT_NODE_MODE_SINGLE_ROOT].
[b]Note:[/b] Regardless of how the glTF file is exported, when importing, the root node type and name can be overridden in the scene import settings tab.
</member>
<member name="visibility_mode" type="int" setter="set_visibility_mode" getter="get_visibility_mode" enum="GLTFDocument.VisibilityMode" default="0">
How to deal with node visibility during export. This setting does nothing if all nodes are visible. The default and recommended value is [constant VISIBILITY_MODE_INCLUDE_REQUIRED], which uses the [code]KHR_node_visibility[/code] extension.
</member>
</members>
<constants>
<constant name="ROOT_NODE_MODE_SINGLE_ROOT" value="0" enum="RootNodeMode">
Treat the Godot scene's root node as the root node of the glTF file, and mark it as the single root node via the [code]GODOT_single_root[/code] glTF extension. This will be parsed the same as [constant ROOT_NODE_MODE_KEEP_ROOT] if the implementation does not support [code]GODOT_single_root[/code].
</constant>
<constant name="ROOT_NODE_MODE_KEEP_ROOT" value="1" enum="RootNodeMode">
Treat the Godot scene's root node as the root node of the glTF file, but do not mark it as anything special. An extra root node will be generated when importing into Godot. This uses only vanilla glTF features. This is equivalent to the behavior in Godot 4.1 and earlier.
</constant>
<constant name="ROOT_NODE_MODE_MULTI_ROOT" value="2" enum="RootNodeMode">
Treat the Godot scene's root node as the name of the glTF scene, and add all of its children as root nodes of the glTF file. This uses only vanilla glTF features. This avoids an extra root node, but only the name of the Godot scene's root node will be preserved, as it will not be saved as a node.
</constant>
<constant name="VISIBILITY_MODE_INCLUDE_REQUIRED" value="0" enum="VisibilityMode">
If the scene contains any non-visible nodes, include them, mark them as non-visible with [code]KHR_node_visibility[/code], and require that importers respect their non-visibility. Downside: If the importer does not support [code]KHR_node_visibility[/code], the file cannot be imported.
</constant>
<constant name="VISIBILITY_MODE_INCLUDE_OPTIONAL" value="1" enum="VisibilityMode">
If the scene contains any non-visible nodes, include them, mark them as non-visible with [code]KHR_node_visibility[/code], and do not impose any requirements on importers. Downside: If the importer does not support [code]KHR_node_visibility[/code], invisible objects will be visible.
</constant>
<constant name="VISIBILITY_MODE_EXCLUDE" value="2" enum="VisibilityMode">
If the scene contains any non-visible nodes, do not include them in the export. This is the same as the behavior in Godot 4.4 and earlier. Downside: Invisible nodes will not exist in the exported file.
</constant>
</constants>
</class>

View File

@@ -0,0 +1,239 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFDocumentExtension" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
[GLTFDocument] extension class.
</brief_description>
<description>
Extends the functionality of the [GLTFDocument] class by allowing you to run arbitrary code at various stages of glTF import or export.
To use, make a new class extending GLTFDocumentExtension, override any methods you need, make an instance of your class, and register it using [method GLTFDocument.register_gltf_document_extension].
[b]Note:[/b] Like GLTFDocument itself, all GLTFDocumentExtension classes must be stateless in order to function properly. If you need to store data, use the [code]set_additional_data[/code] and [code]get_additional_data[/code] methods in [GLTFState] or [GLTFNode].
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
</tutorials>
<methods>
<method name="_convert_scene_node" qualifiers="virtual">
<return type="void" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="gltf_node" type="GLTFNode" />
<param index="2" name="scene_node" type="Node" />
<description>
Part of the export process. This method is run after [method _export_preflight] and before [method _export_post_convert].
Runs when converting the data from a Godot scene node. This method can be used to process the Godot scene node data into a format that can be used by [method _export_node].
</description>
</method>
<method name="_export_node" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="gltf_node" type="GLTFNode" />
<param index="2" name="json" type="Dictionary" />
<param index="3" name="node" type="Node" />
<description>
Part of the export process. This method is run after [method _get_saveable_image_formats] and before [method _export_post]. If this [GLTFDocumentExtension] is used for exporting images, this runs after [method _serialize_texture_json].
This method can be used to modify the final JSON of each node. Data should be primarily stored in [param gltf_node] prior to serializing the JSON, but the original Godot [Node] is also provided if available. [param node] may be [code]null[/code] if not available, such as when exporting glTF data not generated from a Godot scene.
</description>
</method>
<method name="_export_object_model_property" qualifiers="virtual">
<return type="GLTFObjectModelProperty" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="node_path" type="NodePath" />
<param index="2" name="godot_node" type="Node" />
<param index="3" name="gltf_node_index" type="int" />
<param index="4" name="target_object" type="Object" />
<param index="5" name="target_depth" type="int" />
<description>
Part of the export process. Allows GLTFDocumentExtension classes to provide mappings for properties of nodes in the Godot scene tree, to JSON pointers to glTF properties, as defined by the glTF object model.
Returns a [GLTFObjectModelProperty] instance that defines how the property should be mapped. If your extension can't handle the property, return [code]null[/code] or an instance without any JSON pointers (see [method GLTFObjectModelProperty.has_json_pointers]). You should use [method GLTFObjectModelProperty.set_types] to set the types, and set the JSON pointer(s) using the [member GLTFObjectModelProperty.json_pointers] property.
The parameters provide context for the property, including the NodePath, the Godot node, the GLTF node index, and the target object. The [param target_object] will be equal to [param godot_node] if no sub-object can be found, otherwise it will point to a sub-object. For example, if the path is [code]^"A/B/C/MeshInstance3D:mesh:surface_0/material:emission_intensity"[/code], it will get the node, then the mesh, and then the material, so [param target_object] will be the [Material] resource, and [param target_depth] will be 2 because 2 levels were traversed to get to the target.
</description>
</method>
<method name="_export_post" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<description>
Part of the export process. This method is run last, after all other parts of the export process.
This method can be used to modify the final JSON of the generated glTF file.
</description>
</method>
<method name="_export_post_convert" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="root" type="Node" />
<description>
Part of the export process. This method is run after [method _convert_scene_node] and before [method _export_preserialize].
This method can be used to modify the converted node data structures before serialization with any additional data from the scene tree.
</description>
</method>
<method name="_export_preflight" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="root" type="Node" />
<description>
Part of the export process. This method is run first, before all other parts of the export process.
The return value is used to determine if this [GLTFDocumentExtension] instance should be used for exporting a given glTF file. If [constant OK], the export will use this [GLTFDocumentExtension] instance. If not overridden, [constant OK] is returned.
</description>
</method>
<method name="_export_preserialize" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<description>
Part of the export process. This method is run after [method _export_post_convert] and before [method _get_saveable_image_formats].
This method can be used to alter the state before performing serialization. It runs every time when generating a buffer with [method GLTFDocument.generate_buffer] or writing to the file system with [method GLTFDocument.write_to_filesystem].
</description>
</method>
<method name="_generate_scene_node" qualifiers="virtual">
<return type="Node3D" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="gltf_node" type="GLTFNode" />
<param index="2" name="scene_parent" type="Node" />
<description>
Part of the import process. This method is run after [method _import_pre_generate] and before [method _import_node].
Runs when generating a Godot scene node from a GLTFNode. The returned node will be added to the scene tree. Multiple nodes can be generated in this step if they are added as a child of the returned node.
[b]Note:[/b] The [param scene_parent] parameter may be [code]null[/code] if this is the single root node.
</description>
</method>
<method name="_get_image_file_extension" qualifiers="virtual">
<return type="String" />
<description>
Returns the file extension to use for saving image data into, for example, [code]".png"[/code]. If defined, when this extension is used to handle images, and the images are saved to a separate file, the image bytes will be copied to a file with this extension. If this is set, there should be a [ResourceImporter] class able to import the file. If not defined or empty, Godot will save the image into a PNG file.
</description>
</method>
<method name="_get_saveable_image_formats" qualifiers="virtual">
<return type="PackedStringArray" />
<description>
Part of the export process. This method is run after [method _convert_scene_node] and before [method _export_node].
Returns an array of the image formats that can be saved/exported by this extension. This extension will only be selected as the image exporter if the [GLTFDocument]'s [member GLTFDocument.image_format] is in this array. If this [GLTFDocumentExtension] is selected as the image exporter, one of the [method _save_image_at_path] or [method _serialize_image_to_bytes] methods will run next, otherwise [method _export_node] will run next. If the format name contains [code]"Lossy"[/code], the lossy quality slider will be displayed.
</description>
</method>
<method name="_get_supported_extensions" qualifiers="virtual">
<return type="PackedStringArray" />
<description>
Part of the import process. This method is run after [method _import_preflight] and before [method _parse_node_extensions].
Returns an array of the glTF extensions supported by this GLTFDocumentExtension class. This is used to validate if a glTF file with required extensions can be loaded.
</description>
</method>
<method name="_import_node" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="gltf_node" type="GLTFNode" />
<param index="2" name="json" type="Dictionary" />
<param index="3" name="node" type="Node" />
<description>
Part of the import process. This method is run after [method _generate_scene_node] and before [method _import_post].
This method can be used to make modifications to each of the generated Godot scene nodes.
</description>
</method>
<method name="_import_object_model_property" qualifiers="virtual">
<return type="GLTFObjectModelProperty" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="split_json_pointer" type="PackedStringArray" />
<param index="2" name="partial_paths" type="NodePath[]" />
<description>
Part of the import process. Allows GLTFDocumentExtension classes to provide mappings for JSON pointers to glTF properties, as defined by the glTF object model, to properties of nodes in the Godot scene tree.
Returns a [GLTFObjectModelProperty] instance that defines how the property should be mapped. If your extension can't handle the property, return [code]null[/code] or an instance without any NodePaths (see [method GLTFObjectModelProperty.has_node_paths]). You should use [method GLTFObjectModelProperty.set_types] to set the types, and [method GLTFObjectModelProperty.append_path_to_property] function is useful for most simple cases.
In many cases, [param partial_paths] will contain the start of a path, allowing the extension to complete the path. For example, for [code]/nodes/3/extensions/MY_ext/prop[/code], Godot will pass you a NodePath that leads to node 3, so the GLTFDocumentExtension class only needs to resolve the last [code]MY_ext/prop[/code] part of the path. In this example, the extension should check [code]split.size() &gt; 4 and split[0] == "nodes" and split[2] == "extensions" and split[3] == "MY_ext"[/code] at the start of the function to check if this JSON pointer applies to it, then it can use [param partial_paths] and handle [code]split[4][/code].
</description>
</method>
<method name="_import_post" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="root" type="Node" />
<description>
Part of the import process. This method is run last, after all other parts of the import process.
This method can be used to modify the final Godot scene generated by the import process.
</description>
</method>
<method name="_import_post_parse" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<description>
Part of the import process. This method is run after [method _parse_node_extensions] and before [method _import_pre_generate].
This method can be used to modify any of the data imported so far after parsing each node, but before generating the scene or any of its nodes.
</description>
</method>
<method name="_import_pre_generate" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<description>
Part of the import process. This method is run after [method _import_post_parse] and before [method _generate_scene_node].
This method can be used to modify or read from any of the processed data structures, before generating the nodes and then running the final per-node import step.
</description>
</method>
<method name="_import_preflight" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="extensions" type="PackedStringArray" />
<description>
Part of the import process. This method is run first, before all other parts of the import process.
The return value is used to determine if this [GLTFDocumentExtension] instance should be used for importing a given glTF file. If [constant OK], the import will use this [GLTFDocumentExtension] instance. If not overridden, [constant OK] is returned.
</description>
</method>
<method name="_parse_image_data" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="image_data" type="PackedByteArray" />
<param index="2" name="mime_type" type="String" />
<param index="3" name="ret_image" type="Image" />
<description>
Part of the import process. This method is run after [method _parse_node_extensions] and before [method _parse_texture_json].
Runs when parsing image data from a glTF file. The data could be sourced from a separate file, a URI, or a buffer, and then is passed as a byte array.
</description>
</method>
<method name="_parse_node_extensions" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="gltf_node" type="GLTFNode" />
<param index="2" name="extensions" type="Dictionary" />
<description>
Part of the import process. This method is run after [method _get_supported_extensions] and before [method _import_post_parse].
Runs when parsing the node extensions of a GLTFNode. This method can be used to process the extension JSON data into a format that can be used by [method _generate_scene_node]. The return value should be a member of the [enum Error] enum.
</description>
</method>
<method name="_parse_texture_json" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="texture_json" type="Dictionary" />
<param index="2" name="ret_gltf_texture" type="GLTFTexture" />
<description>
Part of the import process. This method is run after [method _parse_image_data] and before [method _generate_scene_node].
Runs when parsing the texture JSON from the glTF textures array. This can be used to set the source image index to use as the texture.
</description>
</method>
<method name="_save_image_at_path" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="image" type="Image" />
<param index="2" name="file_path" type="String" />
<param index="3" name="image_format" type="String" />
<param index="4" name="lossy_quality" type="float" />
<description>
Part of the export process. This method is run after [method _get_saveable_image_formats] and before [method _serialize_texture_json].
This method is run when saving images separately from the glTF file. When images are embedded, [method _serialize_image_to_bytes] runs instead. Note that these methods only run when this [GLTFDocumentExtension] is selected as the image exporter.
</description>
</method>
<method name="_serialize_image_to_bytes" qualifiers="virtual">
<return type="PackedByteArray" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="image" type="Image" />
<param index="2" name="image_dict" type="Dictionary" />
<param index="3" name="image_format" type="String" />
<param index="4" name="lossy_quality" type="float" />
<description>
Part of the export process. This method is run after [method _get_saveable_image_formats] and before [method _serialize_texture_json].
This method is run when embedding images in the glTF file. When images are saved separately, [method _save_image_at_path] runs instead. Note that these methods only run when this [GLTFDocumentExtension] is selected as the image exporter.
This method must set the image MIME type in the [param image_dict] with the [code]"mimeType"[/code] key. For example, for a PNG image, it would be set to [code]"image/png"[/code]. The return value must be a [PackedByteArray] containing the image data.
</description>
</method>
<method name="_serialize_texture_json" qualifiers="virtual">
<return type="int" enum="Error" />
<param index="0" name="state" type="GLTFState" />
<param index="1" name="texture_json" type="Dictionary" />
<param index="2" name="gltf_texture" type="GLTFTexture" />
<param index="3" name="image_format" type="String" />
<description>
Part of the export process. This method is run after [method _save_image_at_path] or [method _serialize_image_to_bytes], and before [method _export_node]. Note that this method only runs when this [GLTFDocumentExtension] is selected as the image exporter.
This method can be used to set up the extensions for the texture JSON by editing [param texture_json]. The extension must also be added as used extension with [method GLTFState.add_used_extension], be sure to set [code]required[/code] to [code]true[/code] if you are not providing a fallback.
</description>
</method>
</methods>
</class>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFDocumentExtensionConvertImporterMesh" inherits="GLTFDocumentExtension" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
</tutorials>
</class>

View File

@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFLight" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Represents a glTF light.
</brief_description>
<description>
Represents a light as defined by the [code]KHR_lights_punctual[/code] glTF extension.
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
<link title="KHR_lights_punctual glTF extension spec">https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual</link>
</tutorials>
<methods>
<method name="from_dictionary" qualifiers="static">
<return type="GLTFLight" />
<param index="0" name="dictionary" type="Dictionary" />
<description>
Creates a new GLTFLight instance by parsing the given [Dictionary].
</description>
</method>
<method name="from_node" qualifiers="static">
<return type="GLTFLight" />
<param index="0" name="light_node" type="Light3D" />
<description>
Create a new GLTFLight instance from the given Godot [Light3D] node.
</description>
</method>
<method name="get_additional_data">
<return type="Variant" />
<param index="0" name="extension_name" type="StringName" />
<description>
</description>
</method>
<method name="set_additional_data">
<return type="void" />
<param index="0" name="extension_name" type="StringName" />
<param index="1" name="additional_data" type="Variant" />
<description>
</description>
</method>
<method name="to_dictionary" qualifiers="const">
<return type="Dictionary" />
<description>
Serializes this GLTFLight instance into a [Dictionary].
</description>
</method>
<method name="to_node" qualifiers="const">
<return type="Light3D" />
<description>
Converts this GLTFLight instance into a Godot [Light3D] node.
</description>
</method>
</methods>
<members>
<member name="color" type="Color" setter="set_color" getter="get_color" default="Color(1, 1, 1, 1)" keywords="colour">
The [Color] of the light in linear space. Defaults to white. A black color causes the light to have no effect.
This value is linear to match glTF, but will be converted to nonlinear sRGB when creating a Godot [Light3D] node upon import, or converted to linear when exporting a Godot [Light3D] to glTF.
</member>
<member name="inner_cone_angle" type="float" setter="set_inner_cone_angle" getter="get_inner_cone_angle" default="0.0">
The inner angle of the cone in a spotlight. Must be less than or equal to the outer cone angle.
Within this angle, the light is at full brightness. Between the inner and outer cone angles, there is a transition from full brightness to zero brightness. When creating a Godot [SpotLight3D], the ratio between the inner and outer cone angles is used to calculate the attenuation of the light.
</member>
<member name="intensity" type="float" setter="set_intensity" getter="get_intensity" default="1.0">
The intensity of the light. This is expressed in candelas (lumens per steradian) for point and spot lights, and lux (lumens per m²) for directional lights. When creating a Godot light, this value is converted to a unitless multiplier.
</member>
<member name="light_type" type="String" setter="set_light_type" getter="get_light_type" default="&quot;&quot;">
The type of the light. The values accepted by Godot are "point", "spot", and "directional", which correspond to Godot's [OmniLight3D], [SpotLight3D], and [DirectionalLight3D] respectively.
</member>
<member name="outer_cone_angle" type="float" setter="set_outer_cone_angle" getter="get_outer_cone_angle" default="0.7853982">
The outer angle of the cone in a spotlight. Must be greater than or equal to the inner angle.
At this angle, the light drops off to zero brightness. Between the inner and outer cone angles, there is a transition from full brightness to zero brightness. If this angle is a half turn, then the spotlight emits in all directions. When creating a Godot [SpotLight3D], the outer cone angle is used as the angle of the spotlight.
</member>
<member name="range" type="float" setter="set_range" getter="get_range" default="inf">
The range of the light, beyond which the light has no effect. glTF lights with no range defined behave like physical lights (which have infinite range). When creating a Godot light, the range is clamped to [code]4096.0[/code].
</member>
</members>
</class>

View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFMesh" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
GLTFMesh represents a glTF mesh.
</brief_description>
<description>
GLTFMesh handles 3D mesh data imported from glTF files. It includes properties for blend channels, blend weights, instance materials, and the mesh itself.
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
</tutorials>
<methods>
<method name="get_additional_data">
<return type="Variant" />
<param index="0" name="extension_name" type="StringName" />
<description>
Gets additional arbitrary data in this [GLTFMesh] instance. This can be used to keep per-node state data in [GLTFDocumentExtension] classes, which is important because they are stateless.
The argument should be the [GLTFDocumentExtension] name (does not have to match the extension name in the glTF file), and the return value can be anything you set. If nothing was set, the return value is [code]null[/code].
</description>
</method>
<method name="set_additional_data">
<return type="void" />
<param index="0" name="extension_name" type="StringName" />
<param index="1" name="additional_data" type="Variant" />
<description>
Sets additional arbitrary data in this [GLTFMesh] instance. This can be used to keep per-node state data in [GLTFDocumentExtension] classes, which is important because they are stateless.
The first argument should be the [GLTFDocumentExtension] name (does not have to match the extension name in the glTF file), and the second argument can be anything you want.
</description>
</method>
</methods>
<members>
<member name="blend_weights" type="PackedFloat32Array" setter="set_blend_weights" getter="get_blend_weights" default="PackedFloat32Array()">
An array of floats representing the blend weights of the mesh.
</member>
<member name="instance_materials" type="Material[]" setter="set_instance_materials" getter="get_instance_materials" default="[]">
An array of Material objects representing the materials used in the mesh.
</member>
<member name="mesh" type="ImporterMesh" setter="set_mesh" getter="get_mesh">
The [ImporterMesh] object representing the mesh itself.
</member>
<member name="original_name" type="String" setter="set_original_name" getter="get_original_name" default="&quot;&quot;">
The original name of the mesh.
</member>
</members>
</class>

View File

@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFNode" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
glTF node class.
</brief_description>
<description>
Represents a glTF node. glTF nodes may have names, transforms, children (other glTF nodes), and more specialized properties (represented by their own classes).
glTF nodes generally exist inside of [GLTFState] which represents all data of a glTF file. Most of GLTFNode's properties are indices of other data in the glTF file. You can extend a glTF node with additional properties by using [method get_additional_data] and [method set_additional_data].
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
<link title="glTF scene and node spec">https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_004_ScenesNodes.md"</link>
</tutorials>
<methods>
<method name="append_child_index">
<return type="void" />
<param index="0" name="child_index" type="int" />
<description>
Appends the given child node index to the [member children] array.
</description>
</method>
<method name="get_additional_data">
<return type="Variant" />
<param index="0" name="extension_name" type="StringName" />
<description>
Gets additional arbitrary data in this [GLTFNode] instance. This can be used to keep per-node state data in [GLTFDocumentExtension] classes, which is important because they are stateless.
The argument should be the [GLTFDocumentExtension] name (does not have to match the extension name in the glTF file), and the return value can be anything you set. If nothing was set, the return value is [code]null[/code].
</description>
</method>
<method name="get_scene_node_path">
<return type="NodePath" />
<param index="0" name="gltf_state" type="GLTFState" />
<param index="1" name="handle_skeletons" type="bool" default="true" />
<description>
Returns the [NodePath] that this GLTF node will have in the Godot scene tree after being imported. This is useful when importing glTF object model pointers with [GLTFObjectModelProperty], for handling extensions such as [code]KHR_animation_pointer[/code] or [code]KHR_interactivity[/code].
If [param handle_skeletons] is [code]true[/code], paths to skeleton bone glTF nodes will be resolved properly. For example, a path that would be [code]^"A/B/C/Bone1/Bone2/Bone3"[/code] if [code]false[/code] will become [code]^"A/B/C/Skeleton3D:Bone3"[/code].
</description>
</method>
<method name="set_additional_data">
<return type="void" />
<param index="0" name="extension_name" type="StringName" />
<param index="1" name="additional_data" type="Variant" />
<description>
Sets additional arbitrary data in this [GLTFNode] instance. This can be used to keep per-node state data in [GLTFDocumentExtension] classes, which is important because they are stateless.
The first argument should be the [GLTFDocumentExtension] name (does not have to match the extension name in the glTF file), and the second argument can be anything you want.
</description>
</method>
</methods>
<members>
<member name="camera" type="int" setter="set_camera" getter="get_camera" default="-1">
If this glTF node is a camera, the index of the [GLTFCamera] in the [GLTFState] that describes the camera's properties. If [code]-1[/code], this node is not a camera.
</member>
<member name="children" type="PackedInt32Array" setter="set_children" getter="get_children" default="PackedInt32Array()">
The indices of the child nodes in the [GLTFState]. If this glTF node has no children, this will be an empty array.
</member>
<member name="height" type="int" setter="set_height" getter="get_height" default="-1">
How deep into the node hierarchy this node is. A root node will have a height of 0, its children will have a height of 1, and so on. If -1, the height has not been calculated.
</member>
<member name="light" type="int" setter="set_light" getter="get_light" default="-1">
If this glTF node is a light, the index of the [GLTFLight] in the [GLTFState] that describes the light's properties. If -1, this node is not a light.
</member>
<member name="mesh" type="int" setter="set_mesh" getter="get_mesh" default="-1">
If this glTF node is a mesh, the index of the [GLTFMesh] in the [GLTFState] that describes the mesh's properties. If -1, this node is not a mesh.
</member>
<member name="original_name" type="String" setter="set_original_name" getter="get_original_name" default="&quot;&quot;">
The original name of the node.
</member>
<member name="parent" type="int" setter="set_parent" getter="get_parent" default="-1">
The index of the parent node in the [GLTFState]. If -1, this node is a root node.
</member>
<member name="position" type="Vector3" setter="set_position" getter="get_position" default="Vector3(0, 0, 0)">
The position of the glTF node relative to its parent.
</member>
<member name="rotation" type="Quaternion" setter="set_rotation" getter="get_rotation" default="Quaternion(0, 0, 0, 1)">
The rotation of the glTF node relative to its parent.
</member>
<member name="scale" type="Vector3" setter="set_scale" getter="get_scale" default="Vector3(1, 1, 1)">
The scale of the glTF node relative to its parent.
</member>
<member name="skeleton" type="int" setter="set_skeleton" getter="get_skeleton" default="-1">
If this glTF node has a skeleton, the index of the [GLTFSkeleton] in the [GLTFState] that describes the skeleton's properties. If -1, this node does not have a skeleton.
</member>
<member name="skin" type="int" setter="set_skin" getter="get_skin" default="-1">
If this glTF node has a skin, the index of the [GLTFSkin] in the [GLTFState] that describes the skin's properties. If -1, this node does not have a skin.
</member>
<member name="visible" type="bool" setter="set_visible" getter="get_visible" default="true">
If [code]true[/code], the GLTF node is visible. If [code]false[/code], the GLTF node is not visible. This is translated to the [member Node3D.visible] property in the Godot scene, and is exported to [code]KHR_node_visibility[/code] when [code]false[/code].
</member>
<member name="xform" type="Transform3D" setter="set_xform" getter="get_xform" default="Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)">
The transform of the glTF node relative to its parent. This property is usually unused since the position, rotation, and scale properties are preferred.
</member>
</members>
</class>

View File

@@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFObjectModelProperty" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Describes how to access a property as defined in the glTF object model.
</brief_description>
<description>
GLTFObjectModelProperty defines a mapping between a property in the glTF object model and a NodePath in the Godot scene tree. This can be used to animate properties in a glTF file using the [code]KHR_animation_pointer[/code] extension, or to access them through an engine-agnostic script such as a behavior graph as defined by the [code]KHR_interactivity[/code] extension.
The glTF property is identified by JSON pointer(s) stored in [member json_pointers], while the Godot property it maps to is defined by [member node_paths]. In most cases [member json_pointers] and [member node_paths] will each only have one item, but in some cases a single glTF JSON pointer will map to multiple Godot properties, or a single Godot property will be mapped to multiple glTF JSON pointers, or it might be a many-to-many relationship.
[Expression] objects can be used to define conversions between the data, such as when glTF defines an angle in radians and Godot uses degrees. The [member object_model_type] property defines the type of data stored in the glTF file as defined by the object model, see [enum GLTFObjectModelType] for possible values.
</description>
<tutorials>
<link title="GLTF Object Model">https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/ObjectModel.adoc</link>
<link title="KHR_animation_pointer GLTF extension">https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_animation_pointer</link>
</tutorials>
<methods>
<method name="append_node_path">
<return type="void" />
<param index="0" name="node_path" type="NodePath" />
<description>
Appends a [NodePath] to [member node_paths]. This can be used by [GLTFDocumentExtension] classes to define how a glTF object model property maps to a Godot property, or multiple Godot properties. Prefer using [method append_path_to_property] for simple cases. Be sure to also call [method set_types] once (the order does not matter).
</description>
</method>
<method name="append_path_to_property">
<return type="void" />
<param index="0" name="node_path" type="NodePath" />
<param index="1" name="prop_name" type="StringName" />
<description>
High-level wrapper over [method append_node_path] that handles the most common cases. It constructs a new [NodePath] using [param node_path] as a base and appends [param prop_name] to the subpath. Be sure to also call [method set_types] once (the order does not matter).
</description>
</method>
<method name="get_accessor_type" qualifiers="const">
<return type="int" enum="GLTFAccessor.GLTFAccessorType" />
<description>
The GLTF accessor type associated with this property's [member object_model_type]. See [member GLTFAccessor.accessor_type] for possible values, and see [enum GLTFObjectModelType] for how the object model type maps to accessor types.
</description>
</method>
<method name="has_json_pointers" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if [member json_pointers] is not empty. This is used during export to determine if a [GLTFObjectModelProperty] can handle converting a Godot property to a glTF object model property.
</description>
</method>
<method name="has_node_paths" qualifiers="const">
<return type="bool" />
<description>
Returns [code]true[/code] if [member node_paths] is not empty. This is used during import to determine if a [GLTFObjectModelProperty] can handle converting a glTF object model property to a Godot property.
</description>
</method>
<method name="set_types">
<return type="void" />
<param index="0" name="variant_type" type="int" enum="Variant.Type" />
<param index="1" name="obj_model_type" type="int" enum="GLTFObjectModelProperty.GLTFObjectModelType" />
<description>
Sets the [member variant_type] and [member object_model_type] properties. This is a convenience method to set both properties at once, since they are almost always known at the same time. This method should be called once. Calling it again with the same values will have no effect.
</description>
</method>
</methods>
<members>
<member name="gltf_to_godot_expression" type="Expression" setter="set_gltf_to_godot_expression" getter="get_gltf_to_godot_expression">
If set, this [Expression] will be used to convert the property value from the glTF object model to the value expected by the Godot property. This is useful when the glTF object model uses a different unit system, or when the data needs to be transformed in some way. If [code]null[/code], the value will be copied as-is.
</member>
<member name="godot_to_gltf_expression" type="Expression" setter="set_godot_to_gltf_expression" getter="get_godot_to_gltf_expression">
If set, this [Expression] will be used to convert the property value from the Godot property to the value expected by the glTF object model. This is useful when the glTF object model uses a different unit system, or when the data needs to be transformed in some way. If [code]null[/code], the value will be copied as-is.
</member>
<member name="json_pointers" type="PackedStringArray[]" setter="set_json_pointers" getter="get_json_pointers" default="[]">
The glTF object model JSON pointers used to identify the property in the glTF object model. In most cases, there will be only one item in this array, but specific cases may require multiple pointers. The items are themselves arrays which represent the JSON pointer split into its components.
</member>
<member name="node_paths" type="NodePath[]" setter="set_node_paths" getter="get_node_paths" default="[]">
An array of [NodePath]s that point to a property, or multiple properties, in the Godot scene tree. On import, this will either be set by [GLTFDocument], or by a [GLTFDocumentExtension] class. For simple cases, use [method append_path_to_property] to add properties to this array.
In most cases [member node_paths] will only have one item, but in some cases a single glTF JSON pointer will map to multiple Godot properties. For example, a [GLTFCamera] or [GLTFLight] used on multiple glTF nodes will be represented by multiple Godot nodes.
</member>
<member name="object_model_type" type="int" setter="set_object_model_type" getter="get_object_model_type" enum="GLTFObjectModelProperty.GLTFObjectModelType" default="0">
The type of data stored in the glTF file as defined by the object model. This is a superset of the available accessor types, and determines the accessor type.
</member>
<member name="variant_type" type="int" setter="set_variant_type" getter="get_variant_type" enum="Variant.Type" default="0">
The type of data stored in the Godot property. This is the type of the property that the [member node_paths] point to.
</member>
</members>
<constants>
<constant name="GLTF_OBJECT_MODEL_TYPE_UNKNOWN" value="0" enum="GLTFObjectModelType">
Unknown or not set object model type. If the object model type is set to this value, the real type still needs to be determined.
</constant>
<constant name="GLTF_OBJECT_MODEL_TYPE_BOOL" value="1" enum="GLTFObjectModelType">
Object model type "bool". Represented in the glTF JSON as a boolean, and encoded in a [GLTFAccessor] as "SCALAR". When encoded in an accessor, a value of [code]0[/code] is [code]false[/code], and any other value is [code]true[/code].
</constant>
<constant name="GLTF_OBJECT_MODEL_TYPE_FLOAT" value="2" enum="GLTFObjectModelType">
Object model type "float". Represented in the glTF JSON as a number, and encoded in a [GLTFAccessor] as "SCALAR".
</constant>
<constant name="GLTF_OBJECT_MODEL_TYPE_FLOAT_ARRAY" value="3" enum="GLTFObjectModelType">
Object model type "float[lb][rb]". Represented in the glTF JSON as an array of numbers, and encoded in a [GLTFAccessor] as "SCALAR".
</constant>
<constant name="GLTF_OBJECT_MODEL_TYPE_FLOAT2" value="4" enum="GLTFObjectModelType">
Object model type "float2". Represented in the glTF JSON as an array of two numbers, and encoded in a [GLTFAccessor] as "VEC2".
</constant>
<constant name="GLTF_OBJECT_MODEL_TYPE_FLOAT3" value="5" enum="GLTFObjectModelType">
Object model type "float3". Represented in the glTF JSON as an array of three numbers, and encoded in a [GLTFAccessor] as "VEC3".
</constant>
<constant name="GLTF_OBJECT_MODEL_TYPE_FLOAT4" value="6" enum="GLTFObjectModelType">
Object model type "float4". Represented in the glTF JSON as an array of four numbers, and encoded in a [GLTFAccessor] as "VEC4".
</constant>
<constant name="GLTF_OBJECT_MODEL_TYPE_FLOAT2X2" value="7" enum="GLTFObjectModelType">
Object model type "float2x2". Represented in the glTF JSON as an array of four numbers, and encoded in a [GLTFAccessor] as "MAT2".
</constant>
<constant name="GLTF_OBJECT_MODEL_TYPE_FLOAT3X3" value="8" enum="GLTFObjectModelType">
Object model type "float3x3". Represented in the glTF JSON as an array of nine numbers, and encoded in a [GLTFAccessor] as "MAT3".
</constant>
<constant name="GLTF_OBJECT_MODEL_TYPE_FLOAT4X4" value="9" enum="GLTFObjectModelType">
Object model type "float4x4". Represented in the glTF JSON as an array of sixteen numbers, and encoded in a [GLTFAccessor] as "MAT4".
</constant>
<constant name="GLTF_OBJECT_MODEL_TYPE_INT" value="10" enum="GLTFObjectModelType">
Object model type "int". Represented in the glTF JSON as a number, and encoded in a [GLTFAccessor] as "SCALAR". The range of values is limited to signed integers. For [code]KHR_interactivity[/code], only 32-bit integers are supported.
</constant>
</constants>
</class>

View File

@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFPhysicsBody" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Represents a glTF physics body.
</brief_description>
<description>
Represents a physics body as an intermediary between the [code]OMI_physics_body[/code] glTF data and Godot's nodes, and it's abstracted in a way that allows adding support for different glTF physics extensions in the future.
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
<link title="OMI_physics_body glTF extension">https://github.com/omigroup/gltf-extensions/tree/main/extensions/2.0/OMI_physics_body</link>
</tutorials>
<methods>
<method name="from_dictionary" qualifiers="static">
<return type="GLTFPhysicsBody" />
<param index="0" name="dictionary" type="Dictionary" />
<description>
Creates a new GLTFPhysicsBody instance by parsing the given [Dictionary] in the [code]OMI_physics_body[/code] glTF extension format.
</description>
</method>
<method name="from_node" qualifiers="static">
<return type="GLTFPhysicsBody" />
<param index="0" name="body_node" type="CollisionObject3D" />
<description>
Creates a new GLTFPhysicsBody instance from the given Godot [CollisionObject3D] node.
</description>
</method>
<method name="to_dictionary" qualifiers="const">
<return type="Dictionary" />
<description>
Serializes this GLTFPhysicsBody instance into a [Dictionary]. It will be in the format expected by the [code]OMI_physics_body[/code] glTF extension.
</description>
</method>
<method name="to_node" qualifiers="const">
<return type="CollisionObject3D" />
<description>
Converts this GLTFPhysicsBody instance into a Godot [CollisionObject3D] node.
</description>
</method>
</methods>
<members>
<member name="angular_velocity" type="Vector3" setter="set_angular_velocity" getter="get_angular_velocity" default="Vector3(0, 0, 0)">
The angular velocity of the physics body, in radians per second. This is only used when the body type is "rigid" or "vehicle".
</member>
<member name="body_type" type="String" setter="set_body_type" getter="get_body_type" default="&quot;rigid&quot;">
The type of the body.
When importing, this controls what type of [CollisionObject3D] node Godot should generate. Valid values are [code]"static"[/code], [code]"animatable"[/code], [code]"character"[/code], [code]"rigid"[/code], [code]"vehicle"[/code], and [code]"trigger"[/code].
When exporting, this will be squashed down to one of [code]"static"[/code], [code]"kinematic"[/code], or [code]"dynamic"[/code] motion types, or the [code]"trigger"[/code] property.
</member>
<member name="center_of_mass" type="Vector3" setter="set_center_of_mass" getter="get_center_of_mass" default="Vector3(0, 0, 0)">
The center of mass of the body, in meters. This is in local space relative to the body. By default, the center of the mass is the body's origin.
</member>
<member name="inertia_diagonal" type="Vector3" setter="set_inertia_diagonal" getter="get_inertia_diagonal" default="Vector3(0, 0, 0)">
The inertia strength of the physics body, in kilogram meter squared (kg⋅m²). This represents the inertia around the principle axes, the diagonal of the inertia tensor matrix. This is only used when the body type is "rigid" or "vehicle".
When converted to a Godot [RigidBody3D] node, if this value is zero, then the inertia will be calculated automatically.
</member>
<member name="inertia_orientation" type="Quaternion" setter="set_inertia_orientation" getter="get_inertia_orientation" default="Quaternion(0, 0, 0, 1)">
The inertia orientation of the physics body. This defines the rotation of the inertia's principle axes relative to the object's local axes. This is only used when the body type is "rigid" or "vehicle" and [member inertia_diagonal] is set to a non-zero value.
</member>
<member name="inertia_tensor" type="Basis" setter="set_inertia_tensor" getter="get_inertia_tensor" default="Basis(0, 0, 0, 0, 0, 0, 0, 0, 0)" deprecated="">
The inertia tensor of the physics body, in kilogram meter squared (kg⋅m²). This is only used when the body type is "rigid" or "vehicle".
When converted to a Godot [RigidBody3D] node, if this value is zero, then the inertia will be calculated automatically.
</member>
<member name="linear_velocity" type="Vector3" setter="set_linear_velocity" getter="get_linear_velocity" default="Vector3(0, 0, 0)">
The linear velocity of the physics body, in meters per second. This is only used when the body type is "rigid" or "vehicle".
</member>
<member name="mass" type="float" setter="set_mass" getter="get_mass" default="1.0">
The mass of the physics body, in kilograms. This is only used when the body type is "rigid" or "vehicle".
</member>
</members>
</class>

View File

@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFPhysicsShape" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Represents a glTF physics shape.
</brief_description>
<description>
Represents a physics shape as defined by the [code]OMI_physics_shape[/code] or [code]OMI_collider[/code] glTF extensions. This class is an intermediary between the glTF data and Godot's nodes, and it's abstracted in a way that allows adding support for different glTF physics extensions in the future.
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
<link title="OMI_physics_shape glTF extension">https://github.com/omigroup/gltf-extensions/tree/main/extensions/2.0/OMI_physics_shape</link>
<link title="OMI_collider glTF extension">https://github.com/omigroup/gltf-extensions/tree/main/extensions/2.0/Archived/OMI_collider</link>
</tutorials>
<methods>
<method name="from_dictionary" qualifiers="static">
<return type="GLTFPhysicsShape" />
<param index="0" name="dictionary" type="Dictionary" />
<description>
Creates a new GLTFPhysicsShape instance by parsing the given [Dictionary].
</description>
</method>
<method name="from_node" qualifiers="static">
<return type="GLTFPhysicsShape" />
<param index="0" name="shape_node" type="CollisionShape3D" />
<description>
Creates a new GLTFPhysicsShape instance from the given Godot [CollisionShape3D] node.
</description>
</method>
<method name="from_resource" qualifiers="static">
<return type="GLTFPhysicsShape" />
<param index="0" name="shape_resource" type="Shape3D" />
<description>
Creates a new GLTFPhysicsShape instance from the given Godot [Shape3D] resource.
</description>
</method>
<method name="to_dictionary" qualifiers="const">
<return type="Dictionary" />
<description>
Serializes this GLTFPhysicsShape instance into a [Dictionary] in the format defined by [code]OMI_physics_shape[/code].
</description>
</method>
<method name="to_node">
<return type="CollisionShape3D" />
<param index="0" name="cache_shapes" type="bool" default="false" />
<description>
Converts this GLTFPhysicsShape instance into a Godot [CollisionShape3D] node.
</description>
</method>
<method name="to_resource">
<return type="Shape3D" />
<param index="0" name="cache_shapes" type="bool" default="false" />
<description>
Converts this GLTFPhysicsShape instance into a Godot [Shape3D] resource.
</description>
</method>
</methods>
<members>
<member name="height" type="float" setter="set_height" getter="get_height" default="2.0">
The height of the shape, in meters. This is only used when the shape type is [code]"capsule"[/code] or [code]"cylinder"[/code]. This value should not be negative, and for [code]"capsule"[/code] it should be at least twice the radius.
</member>
<member name="importer_mesh" type="ImporterMesh" setter="set_importer_mesh" getter="get_importer_mesh">
The [ImporterMesh] resource of the shape. This is only used when the shape type is [code]"hull"[/code] (convex hull) or [code]"trimesh"[/code] (concave trimesh).
</member>
<member name="is_trigger" type="bool" setter="set_is_trigger" getter="get_is_trigger" default="false">
If [code]true[/code], indicates that this shape is a trigger. For Godot, this means that the shape should be a child of an [Area3D] node.
This is the only variable not used in the [method to_node] method, it's intended to be used alongside when deciding where to add the generated node as a child.
</member>
<member name="mesh_index" type="int" setter="set_mesh_index" getter="get_mesh_index" default="-1">
The index of the shape's mesh in the glTF file. This is only used when the shape type is [code]"hull"[/code] (convex hull) or [code]"trimesh"[/code] (concave trimesh).
</member>
<member name="radius" type="float" setter="set_radius" getter="get_radius" default="0.5">
The radius of the shape, in meters. This is only used when the shape type is [code]"capsule"[/code], [code]"cylinder"[/code], or [code]"sphere"[/code]. This value should not be negative.
</member>
<member name="shape_type" type="String" setter="set_shape_type" getter="get_shape_type" default="&quot;&quot;">
The type of shape this shape represents. Valid values are [code]"box"[/code], [code]"capsule"[/code], [code]"cylinder"[/code], [code]"sphere"[/code], [code]"hull"[/code], and [code]"trimesh"[/code].
</member>
<member name="size" type="Vector3" setter="set_size" getter="get_size" default="Vector3(1, 1, 1)">
The size of the shape, in meters. This is only used when the shape type is [code]"box"[/code], and it represents the [code]"diameter"[/code] of the box. This value should not be negative.
</member>
</members>
</class>

View File

@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFSkeleton" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
</tutorials>
<methods>
<method name="get_bone_attachment">
<return type="BoneAttachment3D" />
<param index="0" name="idx" type="int" />
<description>
</description>
</method>
<method name="get_bone_attachment_count">
<return type="int" />
<description>
</description>
</method>
<method name="get_godot_bone_node">
<return type="Dictionary" />
<description>
Returns a [Dictionary] that maps skeleton bone indices to the indices of glTF nodes. This property is unused during import, and only set during export. In a glTF file, a bone is a node, so Godot converts skeleton bones to glTF nodes.
</description>
</method>
<method name="get_godot_skeleton">
<return type="Skeleton3D" />
<description>
</description>
</method>
<method name="get_unique_names">
<return type="String[]" />
<description>
</description>
</method>
<method name="set_godot_bone_node">
<return type="void" />
<param index="0" name="godot_bone_node" type="Dictionary" />
<description>
Sets a [Dictionary] that maps skeleton bone indices to the indices of glTF nodes. This property is unused during import, and only set during export. In a glTF file, a bone is a node, so Godot converts skeleton bones to glTF nodes.
</description>
</method>
<method name="set_unique_names">
<return type="void" />
<param index="0" name="unique_names" type="String[]" />
<description>
</description>
</method>
</methods>
<members>
<member name="joints" type="PackedInt32Array" setter="set_joints" getter="get_joints" default="PackedInt32Array()">
</member>
<member name="roots" type="PackedInt32Array" setter="set_roots" getter="get_roots" default="PackedInt32Array()">
</member>
</members>
</class>

View File

@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFSkin" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
</tutorials>
<methods>
<method name="get_inverse_binds">
<return type="Transform3D[]" />
<description>
</description>
</method>
<method name="get_joint_i_to_bone_i">
<return type="Dictionary" />
<description>
</description>
</method>
<method name="get_joint_i_to_name">
<return type="Dictionary" />
<description>
</description>
</method>
<method name="set_inverse_binds">
<return type="void" />
<param index="0" name="inverse_binds" type="Transform3D[]" />
<description>
</description>
</method>
<method name="set_joint_i_to_bone_i">
<return type="void" />
<param index="0" name="joint_i_to_bone_i" type="Dictionary" />
<description>
</description>
</method>
<method name="set_joint_i_to_name">
<return type="void" />
<param index="0" name="joint_i_to_name" type="Dictionary" />
<description>
</description>
</method>
</methods>
<members>
<member name="godot_skin" type="Skin" setter="set_godot_skin" getter="get_godot_skin">
</member>
<member name="joints" type="PackedInt32Array" setter="set_joints" getter="get_joints" default="PackedInt32Array()">
</member>
<member name="joints_original" type="PackedInt32Array" setter="set_joints_original" getter="get_joints_original" default="PackedInt32Array()">
</member>
<member name="non_joints" type="PackedInt32Array" setter="set_non_joints" getter="get_non_joints" default="PackedInt32Array()">
</member>
<member name="roots" type="PackedInt32Array" setter="set_roots" getter="get_roots" default="PackedInt32Array()">
</member>
<member name="skeleton" type="int" setter="set_skeleton" getter="get_skeleton" default="-1">
</member>
<member name="skin_root" type="int" setter="set_skin_root" getter="get_skin_root" default="-1">
</member>
</members>
</class>

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFSpecGloss" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Archived glTF extension for specular/glossy materials.
</brief_description>
<description>
KHR_materials_pbrSpecularGlossiness is an archived glTF extension. This means that it is deprecated and not recommended for new files. However, it is still supported for loading old files.
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
<link title="KHR_materials_pbrSpecularGlossiness glTF extension spec">https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Archived/KHR_materials_pbrSpecularGlossiness</link>
</tutorials>
<members>
<member name="diffuse_factor" type="Color" setter="set_diffuse_factor" getter="get_diffuse_factor" default="Color(1, 1, 1, 1)">
The reflected diffuse factor of the material.
</member>
<member name="diffuse_img" type="Image" setter="set_diffuse_img" getter="get_diffuse_img">
The diffuse texture.
</member>
<member name="gloss_factor" type="float" setter="set_gloss_factor" getter="get_gloss_factor" default="1.0">
The glossiness or smoothness of the material.
</member>
<member name="spec_gloss_img" type="Image" setter="set_spec_gloss_img" getter="get_spec_gloss_img">
The specular-glossiness texture.
</member>
<member name="specular_factor" type="Color" setter="set_specular_factor" getter="get_specular_factor" default="Color(1, 1, 1, 1)">
The specular RGB color of the material. The alpha channel is unused.
</member>
</members>
</class>

View File

@@ -0,0 +1,341 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFState" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Represents all data of a glTF file.
</brief_description>
<description>
Contains all nodes and resources of a glTF file. This is used by [GLTFDocument] as data storage, which allows [GLTFDocument] and all [GLTFDocumentExtension] classes to remain stateless.
GLTFState can be populated by [GLTFDocument] reading a file or by converting a Godot scene. Then the data can either be used to create a Godot scene or save to a glTF file. The code that converts to/from a Godot scene can be intercepted at arbitrary points by [GLTFDocumentExtension] classes. This allows for custom data to be stored in the glTF file or for custom data to be converted to/from Godot nodes.
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
<link title="glTF asset header schema">https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/asset.schema.json</link>
</tutorials>
<methods>
<method name="add_used_extension">
<return type="void" />
<param index="0" name="extension_name" type="String" />
<param index="1" name="required" type="bool" />
<description>
Appends an extension to the list of extensions used by this glTF file during serialization. If [param required] is [code]true[/code], the extension will also be added to the list of required extensions. Do not run this in [method GLTFDocumentExtension._export_post], as that stage is too late to add extensions. The final list is sorted alphabetically.
</description>
</method>
<method name="append_data_to_buffers">
<return type="int" />
<param index="0" name="data" type="PackedByteArray" />
<param index="1" name="deduplication" type="bool" />
<description>
Appends the given byte array [param data] to the buffers and creates a [GLTFBufferView] for it. The index of the destination [GLTFBufferView] is returned. If [param deduplication] is [code]true[/code], the buffers are first searched for duplicate data, otherwise new bytes are always appended.
</description>
</method>
<method name="append_gltf_node">
<return type="int" />
<param index="0" name="gltf_node" type="GLTFNode" />
<param index="1" name="godot_scene_node" type="Node" />
<param index="2" name="parent_node_index" type="int" />
<description>
Appends the given [GLTFNode] to the state, and returns its new index. This can be used to export one Godot node as multiple glTF nodes, or inject new glTF nodes at import time. On import, this must be called before [method GLTFDocumentExtension._generate_scene_node] finishes for the parent node. On export, this must be called before [method GLTFDocumentExtension._export_node] runs for the parent node.
The [param godot_scene_node] parameter is the Godot scene node that corresponds to this glTF node. This is highly recommended to be set to a valid node, but may be [code]null[/code] if there is no corresponding Godot scene node. One Godot scene node may be used for multiple glTF nodes, so if exporting multiple glTF nodes for one Godot scene node, use the same Godot scene node for each.
The [param parent_node_index] parameter is the index of the parent [GLTFNode] in the state. If [code]-1[/code], the node will be a root node, otherwise the new node will be added to the parent's list of children. The index will also be written to the [member GLTFNode.parent] property of the new node.
</description>
</method>
<method name="get_accessors">
<return type="GLTFAccessor[]" />
<description>
</description>
</method>
<method name="get_additional_data">
<return type="Variant" />
<param index="0" name="extension_name" type="StringName" />
<description>
Gets additional arbitrary data in this [GLTFState] instance. This can be used to keep per-file state data in [GLTFDocumentExtension] classes, which is important because they are stateless.
The argument should be the [GLTFDocumentExtension] name (does not have to match the extension name in the glTF file), and the return value can be anything you set. If nothing was set, the return value is [code]null[/code].
</description>
</method>
<method name="get_animation_player">
<return type="AnimationPlayer" />
<param index="0" name="idx" type="int" />
<description>
Returns the [AnimationPlayer] node with the given index. These nodes are only used during the export process when converting Godot [AnimationPlayer] nodes to glTF animations.
</description>
</method>
<method name="get_animation_players_count">
<return type="int" />
<param index="0" name="idx" type="int" />
<description>
Returns the number of [AnimationPlayer] nodes in this [GLTFState]. These nodes are only used during the export process when converting Godot [AnimationPlayer] nodes to glTF animations.
</description>
</method>
<method name="get_animations">
<return type="GLTFAnimation[]" />
<description>
Returns an array of all [GLTFAnimation]s in the glTF file. When importing, these will be generated as animations in an [AnimationPlayer] node. When exporting, these will be generated from Godot [AnimationPlayer] nodes.
</description>
</method>
<method name="get_buffer_views">
<return type="GLTFBufferView[]" />
<description>
</description>
</method>
<method name="get_cameras">
<return type="GLTFCamera[]" />
<description>
Returns an array of all [GLTFCamera]s in the glTF file. These are the cameras that the [member GLTFNode.camera] index refers to.
</description>
</method>
<method name="get_handle_binary_image">
<return type="int" />
<description>
</description>
</method>
<method name="get_images">
<return type="Texture2D[]" />
<description>
Gets the images of the glTF file as an array of [Texture2D]s. These are the images that the [member GLTFTexture.src_image] index refers to.
</description>
</method>
<method name="get_lights">
<return type="GLTFLight[]" />
<description>
Returns an array of all [GLTFLight]s in the glTF file. These are the lights that the [member GLTFNode.light] index refers to.
</description>
</method>
<method name="get_materials">
<return type="Material[]" />
<description>
</description>
</method>
<method name="get_meshes">
<return type="GLTFMesh[]" />
<description>
Returns an array of all [GLTFMesh]es in the glTF file. These are the meshes that the [member GLTFNode.mesh] index refers to.
</description>
</method>
<method name="get_node_index">
<return type="int" />
<param index="0" name="scene_node" type="Node" />
<description>
Returns the index of the [GLTFNode] corresponding to this Godot scene node. This is the inverse of [method get_scene_node]. Useful during the export process.
[b]Note:[/b] Not every Godot scene node will have a corresponding [GLTFNode], and not every [GLTFNode] will have a scene node generated. If there is no [GLTFNode] index for this scene node, [code]-1[/code] is returned.
</description>
</method>
<method name="get_nodes">
<return type="GLTFNode[]" />
<description>
Returns an array of all [GLTFNode]s in the glTF file. These are the nodes that [member GLTFNode.children] and [member root_nodes] refer to. This includes nodes that may not be generated in the Godot scene, or nodes that may generate multiple Godot scene nodes.
</description>
</method>
<method name="get_scene_node">
<return type="Node" />
<param index="0" name="idx" type="int" />
<description>
Returns the Godot scene node that corresponds to the same index as the [GLTFNode] it was generated from. This is the inverse of [method get_node_index]. Useful during the import process.
[b]Note:[/b] Not every [GLTFNode] will have a scene node generated, and not every generated scene node will have a corresponding [GLTFNode]. If there is no scene node for this [GLTFNode] index, [code]null[/code] is returned.
</description>
</method>
<method name="get_skeletons">
<return type="GLTFSkeleton[]" />
<description>
Returns an array of all [GLTFSkeleton]s in the glTF file. These are the skeletons that the [member GLTFNode.skeleton] index refers to.
</description>
</method>
<method name="get_skins">
<return type="GLTFSkin[]" />
<description>
Returns an array of all [GLTFSkin]s in the glTF file. These are the skins that the [member GLTFNode.skin] index refers to.
</description>
</method>
<method name="get_texture_samplers">
<return type="GLTFTextureSampler[]" />
<description>
Retrieves the array of texture samplers that are used by the textures contained in the glTF.
</description>
</method>
<method name="get_textures">
<return type="GLTFTexture[]" />
<description>
</description>
</method>
<method name="get_unique_animation_names">
<return type="String[]" />
<description>
Returns an array of unique animation names. This is only used during the import process.
</description>
</method>
<method name="get_unique_names">
<return type="String[]" />
<description>
Returns an array of unique node names. This is used in both the import process and export process.
</description>
</method>
<method name="set_accessors">
<return type="void" />
<param index="0" name="accessors" type="GLTFAccessor[]" />
<description>
</description>
</method>
<method name="set_additional_data">
<return type="void" />
<param index="0" name="extension_name" type="StringName" />
<param index="1" name="additional_data" type="Variant" />
<description>
Sets additional arbitrary data in this [GLTFState] instance. This can be used to keep per-file state data in [GLTFDocumentExtension] classes, which is important because they are stateless.
The first argument should be the [GLTFDocumentExtension] name (does not have to match the extension name in the glTF file), and the second argument can be anything you want.
</description>
</method>
<method name="set_animations">
<return type="void" />
<param index="0" name="animations" type="GLTFAnimation[]" />
<description>
Sets the [GLTFAnimation]s in the state. When importing, these will be generated as animations in an [AnimationPlayer] node. When exporting, these will be generated from Godot [AnimationPlayer] nodes.
</description>
</method>
<method name="set_buffer_views">
<return type="void" />
<param index="0" name="buffer_views" type="GLTFBufferView[]" />
<description>
</description>
</method>
<method name="set_cameras">
<return type="void" />
<param index="0" name="cameras" type="GLTFCamera[]" />
<description>
Sets the [GLTFCamera]s in the state. These are the cameras that the [member GLTFNode.camera] index refers to.
</description>
</method>
<method name="set_handle_binary_image">
<return type="void" />
<param index="0" name="method" type="int" />
<description>
</description>
</method>
<method name="set_images">
<return type="void" />
<param index="0" name="images" type="Texture2D[]" />
<description>
Sets the images in the state stored as an array of [Texture2D]s. This can be used during export. These are the images that the [member GLTFTexture.src_image] index refers to.
</description>
</method>
<method name="set_lights">
<return type="void" />
<param index="0" name="lights" type="GLTFLight[]" />
<description>
Sets the [GLTFLight]s in the state. These are the lights that the [member GLTFNode.light] index refers to.
</description>
</method>
<method name="set_materials">
<return type="void" />
<param index="0" name="materials" type="Material[]" />
<description>
</description>
</method>
<method name="set_meshes">
<return type="void" />
<param index="0" name="meshes" type="GLTFMesh[]" />
<description>
Sets the [GLTFMesh]es in the state. These are the meshes that the [member GLTFNode.mesh] index refers to.
</description>
</method>
<method name="set_nodes">
<return type="void" />
<param index="0" name="nodes" type="GLTFNode[]" />
<description>
Sets the [GLTFNode]s in the state. These are the nodes that [member GLTFNode.children] and [member root_nodes] refer to. Some of the nodes set here may not be generated in the Godot scene, or may generate multiple Godot scene nodes.
</description>
</method>
<method name="set_skeletons">
<return type="void" />
<param index="0" name="skeletons" type="GLTFSkeleton[]" />
<description>
Sets the [GLTFSkeleton]s in the state. These are the skeletons that the [member GLTFNode.skeleton] index refers to.
</description>
</method>
<method name="set_skins">
<return type="void" />
<param index="0" name="skins" type="GLTFSkin[]" />
<description>
Sets the [GLTFSkin]s in the state. These are the skins that the [member GLTFNode.skin] index refers to.
</description>
</method>
<method name="set_texture_samplers">
<return type="void" />
<param index="0" name="texture_samplers" type="GLTFTextureSampler[]" />
<description>
Sets the array of texture samplers that are used by the textures contained in the glTF.
</description>
</method>
<method name="set_textures">
<return type="void" />
<param index="0" name="textures" type="GLTFTexture[]" />
<description>
</description>
</method>
<method name="set_unique_animation_names">
<return type="void" />
<param index="0" name="unique_animation_names" type="String[]" />
<description>
Sets the unique animation names in the state. This is only used during the import process.
</description>
</method>
<method name="set_unique_names">
<return type="void" />
<param index="0" name="unique_names" type="String[]" />
<description>
Sets the unique node names in the state. This is used in both the import process and export process.
</description>
</method>
</methods>
<members>
<member name="bake_fps" type="float" setter="set_bake_fps" getter="get_bake_fps" default="30.0">
The baking fps of the animation for either import or export.
</member>
<member name="base_path" type="String" setter="set_base_path" getter="get_base_path" default="&quot;&quot;">
The folder path associated with this glTF data. This is used to find other files the glTF file references, like images or binary buffers. This will be set during import when appending from a file, and will be set during export when writing to a file.
</member>
<member name="buffers" type="PackedByteArray[]" setter="set_buffers" getter="get_buffers" default="[]">
</member>
<member name="copyright" type="String" setter="set_copyright" getter="get_copyright" default="&quot;&quot;">
The copyright string in the asset header of the glTF file. This is set during import if present and export if non-empty. See the glTF asset header documentation for more information.
</member>
<member name="create_animations" type="bool" setter="set_create_animations" getter="get_create_animations" default="true">
</member>
<member name="filename" type="String" setter="set_filename" getter="get_filename" default="&quot;&quot;">
The file name associated with this glTF data. If it ends with [code].gltf[/code], this is text-based glTF, otherwise this is binary GLB. This will be set during import when appending from a file, and will be set during export when writing to a file. If writing to a buffer, this will be an empty string.
</member>
<member name="glb_data" type="PackedByteArray" setter="set_glb_data" getter="get_glb_data" default="PackedByteArray()">
The binary buffer attached to a .glb file.
</member>
<member name="import_as_skeleton_bones" type="bool" setter="set_import_as_skeleton_bones" getter="get_import_as_skeleton_bones" default="false">
If [code]true[/code], forces all GLTFNodes in the document to be bones of a single [Skeleton3D] Godot node.
</member>
<member name="json" type="Dictionary" setter="set_json" getter="get_json" default="{}">
The original raw JSON document corresponding to this GLTFState.
</member>
<member name="major_version" type="int" setter="set_major_version" getter="get_major_version" default="0">
</member>
<member name="minor_version" type="int" setter="set_minor_version" getter="get_minor_version" default="0">
</member>
<member name="root_nodes" type="PackedInt32Array" setter="set_root_nodes" getter="get_root_nodes" default="PackedInt32Array()">
The root nodes of the glTF file. Typically, a glTF file will only have one scene, and therefore one root node. However, a glTF file may have multiple scenes and therefore multiple root nodes, which will be generated as siblings of each other and as children of the root node of the generated Godot scene.
</member>
<member name="scene_name" type="String" setter="set_scene_name" getter="get_scene_name" default="&quot;&quot;">
The name of the scene. When importing, if not specified, this will be the file name. When exporting, if specified, the scene name will be saved to the glTF file.
</member>
<member name="use_named_skin_binds" type="bool" setter="set_use_named_skin_binds" getter="get_use_named_skin_binds" default="false">
</member>
</members>
<constants>
<constant name="HANDLE_BINARY_DISCARD_TEXTURES" value="0">
Discards all embedded textures and uses untextured materials.
</constant>
<constant name="HANDLE_BINARY_EXTRACT_TEXTURES" value="1">
Extracts embedded textures to be reimported and compressed. Editor only. Acts as uncompressed at runtime.
</constant>
<constant name="HANDLE_BINARY_EMBED_AS_BASISU" value="2">
Embeds textures VRAM compressed with Basis Universal into the generated scene.
</constant>
<constant name="HANDLE_BINARY_EMBED_AS_UNCOMPRESSED" value="3">
Embeds textures compressed losslessly into the generated scene, matching old behavior.
</constant>
</constants>
</class>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFTexture" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
GLTFTexture represents a texture in a glTF file.
</brief_description>
<description>
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
</tutorials>
<members>
<member name="sampler" type="int" setter="set_sampler" getter="get_sampler" default="-1">
ID of the texture sampler to use when sampling the image. If -1, then the default texture sampler is used (linear filtering, and repeat wrapping in both axes).
</member>
<member name="src_image" type="int" setter="set_src_image" getter="get_src_image" default="-1">
The index of the image associated with this texture, see [method GLTFState.get_images]. If -1, then this texture does not have an image assigned.
</member>
</members>
</class>

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFTextureSampler" inherits="Resource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Represents a glTF texture sampler
</brief_description>
<description>
Represents a texture sampler as defined by the base glTF spec. Texture samplers in glTF specify how to sample data from the texture's base image, when rendering the texture on an object.
</description>
<tutorials>
<link title="Runtime file loading and saving">$DOCS_URL/tutorials/io/runtime_file_loading_and_saving.html</link>
</tutorials>
<members>
<member name="mag_filter" type="int" setter="set_mag_filter" getter="get_mag_filter" default="9729">
Texture's magnification filter, used when texture appears larger on screen than the source image.
</member>
<member name="min_filter" type="int" setter="set_min_filter" getter="get_min_filter" default="9987">
Texture's minification filter, used when the texture appears smaller on screen than the source image.
</member>
<member name="wrap_s" type="int" setter="set_wrap_s" getter="get_wrap_s" default="10497">
Wrapping mode to use for S-axis (horizontal) texture coordinates.
</member>
<member name="wrap_t" type="int" setter="set_wrap_t" getter="get_wrap_t" default="10497">
Wrapping mode to use for T-axis (vertical) texture coordinates.
</member>
</members>
</class>

View File

@@ -0,0 +1,390 @@
/**************************************************************************/
/* editor_import_blend_runner.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "editor_import_blend_runner.h"
#include "core/io/http_client.h"
#include "editor/editor_node.h"
#include "editor/file_system/editor_file_system.h"
#include "editor/settings/editor_settings.h"
static constexpr char PYTHON_SCRIPT_RPC[] = R"(
import bpy, sys, threading
from xmlrpc.server import SimpleXMLRPCServer
req = threading.Condition()
res = threading.Condition()
info = None
export_err = None
def xmlrpc_server():
server = SimpleXMLRPCServer(('127.0.0.1', %d))
server.register_function(export_gltf)
server.serve_forever()
def export_gltf(opts):
with req:
global info
info = ('export_gltf', opts)
req.notify()
with res:
res.wait()
if export_err:
raise export_err
# Important to return a value to prevent the error 'cannot marshal None unless allow_none is enabled'.
return 'BLENDER_GODOT_EXPORT_SUCCESSFUL'
if bpy.app.version < (3, 0, 0):
print('Blender 3.0 or higher is required.', file=sys.stderr)
threading.Thread(target=xmlrpc_server).start()
while True:
with req:
while info is None:
req.wait()
method, opts = info
if method == 'export_gltf':
try:
export_err = None
bpy.ops.wm.open_mainfile(filepath=opts['path'])
if opts['unpack_all']:
bpy.ops.file.unpack_all(method='USE_LOCAL')
bpy.ops.export_scene.gltf(**opts['gltf_options'])
except Exception as e:
export_err = e
info = None
with res:
res.notify()
)";
static constexpr char PYTHON_SCRIPT_DIRECT[] = R"(
import bpy, sys
opts = %s
if bpy.app.version < (3, 0, 0):
print('Blender 3.0 or higher is required.', file=sys.stderr)
bpy.ops.wm.open_mainfile(filepath=opts['path'])
if opts['unpack_all']:
bpy.ops.file.unpack_all(method='USE_LOCAL')
bpy.ops.export_scene.gltf(**opts['gltf_options'])
)";
String dict_to_python(const Dictionary &p_dict) {
String entries;
for (const KeyValue<Variant, Variant> &kv : p_dict) {
const String &key = kv.key;
String value;
const Variant &raw_value = kv.value;
switch (raw_value.get_type()) {
case Variant::Type::BOOL: {
value = raw_value ? "True" : "False";
break;
}
case Variant::Type::STRING:
case Variant::Type::STRING_NAME: {
value = raw_value;
value = vformat("'%s'", value.c_escape());
break;
}
case Variant::Type::DICTIONARY: {
value = dict_to_python(raw_value);
break;
}
default: {
ERR_FAIL_V_MSG("", vformat("Unhandled Variant type %s for python dictionary", Variant::get_type_name(raw_value.get_type())));
}
}
entries += vformat("'%s': %s,", key, value);
}
return vformat("{%s}", entries);
}
String dict_to_xmlrpc(const Dictionary &p_dict) {
String members;
for (const KeyValue<Variant, Variant> &kv : p_dict) {
const String &key = kv.key;
String value;
const Variant &raw_value = kv.value;
switch (raw_value.get_type()) {
case Variant::Type::BOOL: {
value = vformat("<boolean>%d</boolean>", raw_value ? 1 : 0);
break;
}
case Variant::Type::STRING:
case Variant::Type::STRING_NAME: {
value = raw_value;
value = vformat("<string>%s</string>", value.xml_escape());
break;
}
case Variant::Type::DICTIONARY: {
value = dict_to_xmlrpc(raw_value);
break;
}
default: {
ERR_FAIL_V_MSG("", vformat("Unhandled Variant type %s for XMLRPC", Variant::get_type_name(raw_value.get_type())));
}
}
members += vformat("<member><name>%s</name><value>%s</value></member>", key, value);
}
return vformat("<struct>%s</struct>", members);
}
Error EditorImportBlendRunner::start_blender(const String &p_python_script, bool p_blocking) {
String blender_path = EDITOR_GET("filesystem/import/blender/blender_path");
List<String> args;
args.push_back("--background");
args.push_back("--python-expr");
args.push_back(p_python_script);
Error err;
if (p_blocking) {
int exitcode = 0;
err = OS::get_singleton()->execute(blender_path, args, nullptr, &exitcode);
if (exitcode != 0) {
return FAILED;
}
} else {
err = OS::get_singleton()->create_process(blender_path, args, &blender_pid);
}
return err;
}
Error EditorImportBlendRunner::do_import(const Dictionary &p_options) {
if (is_using_rpc()) {
Error err = do_import_rpc(p_options);
if (err != OK) {
// Retry without using RPC (slow, but better than the import failing completely).
if (err == ERR_CONNECTION_ERROR) {
// Disable RPC if the connection could not be established.
print_error(vformat("Failed to connect to Blender via RPC, switching to direct imports of .blend files. Check your proxy and firewall settings, then RPC can be re-enabled by changing the editor setting `filesystem/import/blender/rpc_port` to %d.", rpc_port));
EditorSettings::get_singleton()->set_manually("filesystem/import/blender/rpc_port", 0);
rpc_port = 0;
}
if (err != ERR_QUERY_FAILED) {
err = do_import_direct(p_options);
}
}
return err;
} else {
return do_import_direct(p_options);
}
}
HTTPClient::Status EditorImportBlendRunner::connect_blender_rpc(const Ref<HTTPClient> &p_client, int p_timeout_usecs) {
p_client->connect_to_host("127.0.0.1", rpc_port);
HTTPClient::Status status = p_client->get_status();
int attempts = 1;
int wait_usecs = 1000;
bool done = false;
while (!done) {
OS::get_singleton()->delay_usec(wait_usecs);
status = p_client->get_status();
switch (status) {
case HTTPClient::STATUS_RESOLVING:
case HTTPClient::STATUS_CONNECTING: {
p_client->poll();
break;
}
case HTTPClient::STATUS_CONNECTED: {
done = true;
break;
}
default: {
if (attempts * wait_usecs < p_timeout_usecs) {
p_client->connect_to_host("127.0.0.1", rpc_port);
} else {
return status;
}
}
}
}
return status;
}
Error EditorImportBlendRunner::do_import_rpc(const Dictionary &p_options) {
kill_timer->stop();
// Start Blender if not already running.
if (!is_running()) {
// Start an XML RPC server on the given port.
String python = vformat(PYTHON_SCRIPT_RPC, rpc_port);
Error err = start_blender(python, false);
if (err != OK || blender_pid == 0) {
return FAILED;
}
}
// Convert options to XML body.
String xml_options = dict_to_xmlrpc(p_options);
String xml_body = vformat("<?xml version=\"1.0\"?><methodCall><methodName>export_gltf</methodName><params><param><value>%s</value></param></params></methodCall>", xml_options);
// Connect to RPC server.
Ref<HTTPClient> client = HTTPClient::create();
HTTPClient::Status status = connect_blender_rpc(client, 1000000);
if (status != HTTPClient::STATUS_CONNECTED) {
ERR_FAIL_V_MSG(ERR_CONNECTION_ERROR, vformat("Unexpected status during RPC connection: %d", status));
}
// Send XML request.
PackedByteArray xml_buffer = xml_body.to_utf8_buffer();
Error err = client->request(HTTPClient::METHOD_POST, "/", Vector<String>(), xml_buffer.ptr(), xml_buffer.size());
if (err != OK) {
ERR_FAIL_V_MSG(err, vformat("Unable to send RPC request: %d", err));
}
// Wait for response.
bool done = false;
PackedByteArray response;
while (!done) {
status = client->get_status();
switch (status) {
case HTTPClient::STATUS_REQUESTING: {
client->poll();
break;
}
case HTTPClient::STATUS_BODY: {
client->poll();
response.append_array(client->read_response_body_chunk());
break;
}
case HTTPClient::STATUS_CONNECTED: {
done = true;
break;
}
default: {
ERR_FAIL_V_MSG(ERR_CONNECTION_ERROR, vformat("Unexpected status during RPC response: %d", status));
}
}
}
String response_text = "No response from Blender.";
if (response.size() > 0) {
response_text = String::utf8((const char *)response.ptr(), response.size());
}
if (client->get_response_code() != HTTPClient::RESPONSE_OK) {
ERR_FAIL_V_MSG(ERR_QUERY_FAILED, vformat("Error received from Blender - status code: %s, error: %s", client->get_response_code(), response_text));
} else if (response_text.find("BLENDER_GODOT_EXPORT_SUCCESSFUL") < 0) {
// Previous versions of Godot used a Python script where the RPC function did not return
// a value, causing the error 'cannot marshal None unless allow_none is enabled'.
// If an older version of Godot is running and has started Blender with this script,
// we will receive the error, but there's a good chance that the import was successful.
// We are discarding this error to maintain backward compatibility and prevent situations
// where the user needs to close the older version of Godot or kill Blender.
if (response_text.find("cannot marshal None unless allow_none is enabled") < 0) {
String error_message;
if (_extract_error_message_xml(response, error_message)) {
ERR_FAIL_V_MSG(ERR_QUERY_FAILED, vformat("Blender exportation failed: %s", error_message));
} else {
ERR_FAIL_V_MSG(ERR_QUERY_FAILED, vformat("Blender exportation failed: %s", response_text));
}
}
}
return OK;
}
bool EditorImportBlendRunner::_extract_error_message_xml(const Vector<uint8_t> &p_response_data, String &r_error_message) {
// Based on RPC Xml spec from: https://xmlrpc.com/spec.md
Ref<XMLParser> parser = memnew(XMLParser);
Error err = parser->open_buffer(p_response_data);
if (err) {
return false;
}
r_error_message = String();
while (parser->read() == OK) {
if (parser->get_node_type() == XMLParser::NODE_TEXT) {
if (parser->get_node_data().size()) {
if (r_error_message.size()) {
r_error_message += " ";
}
r_error_message += parser->get_node_data().trim_suffix("\n");
}
}
}
return r_error_message.size();
}
Error EditorImportBlendRunner::do_import_direct(const Dictionary &p_options) {
// Export glTF directly.
String python = vformat(PYTHON_SCRIPT_DIRECT, dict_to_python(p_options));
Error err = start_blender(python, true);
if (err != OK) {
return err;
}
return OK;
}
void EditorImportBlendRunner::_resources_reimported(const PackedStringArray &p_files) {
if (is_running()) {
// After a batch of imports is done, wait a few seconds before trying to kill blender,
// in case of having multiple imports trigger in quick succession.
kill_timer->start();
}
}
void EditorImportBlendRunner::_kill_blender() {
kill_timer->stop();
if (is_running()) {
OS::get_singleton()->kill(blender_pid);
}
blender_pid = 0;
}
void EditorImportBlendRunner::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_PREDELETE: {
_kill_blender();
break;
}
}
}
EditorImportBlendRunner *EditorImportBlendRunner::singleton = nullptr;
EditorImportBlendRunner::EditorImportBlendRunner() {
ERR_FAIL_COND_MSG(singleton != nullptr, "EditorImportBlendRunner already created.");
singleton = this;
rpc_port = EDITOR_GET("filesystem/import/blender/rpc_port");
kill_timer = memnew(Timer);
add_child(kill_timer);
kill_timer->set_one_shot(true);
kill_timer->set_wait_time(EDITOR_GET("filesystem/import/blender/rpc_server_uptime"));
kill_timer->connect("timeout", callable_mp(this, &EditorImportBlendRunner::_kill_blender));
EditorFileSystem::get_singleton()->connect("resources_reimported", callable_mp(this, &EditorImportBlendRunner::_resources_reimported));
}

View File

@@ -0,0 +1,65 @@
/**************************************************************************/
/* editor_import_blend_runner.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "core/io/http_client.h"
#include "core/os/os.h"
#include "scene/main/node.h"
#include "scene/main/timer.h"
class EditorImportBlendRunner : public Node {
GDCLASS(EditorImportBlendRunner, Node);
static EditorImportBlendRunner *singleton;
Timer *kill_timer;
void _resources_reimported(const PackedStringArray &p_files);
void _kill_blender();
void _notification(int p_what);
bool _extract_error_message_xml(const Vector<uint8_t> &p_response_data, String &r_error_message);
protected:
int rpc_port = 0;
OS::ProcessID blender_pid = 0;
Error start_blender(const String &p_python_script, bool p_blocking);
Error do_import_direct(const Dictionary &p_options);
Error do_import_rpc(const Dictionary &p_options);
public:
static EditorImportBlendRunner *get_singleton() { return singleton; }
bool is_running() { return blender_pid != 0 && OS::get_singleton()->is_process_running(blender_pid); }
bool is_using_rpc() { return rpc_port != 0; }
Error do_import(const Dictionary &p_options);
HTTPClient::Status connect_blender_rpc(const Ref<HTTPClient> &p_client, int p_timeout_usecs);
EditorImportBlendRunner();
};

View File

@@ -0,0 +1,118 @@
/**************************************************************************/
/* editor_scene_exporter_gltf_plugin.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "editor_scene_exporter_gltf_plugin.h"
#include "editor_scene_exporter_gltf_settings.h"
#include "editor/editor_node.h"
#include "editor/file_system/editor_file_system.h"
#include "editor/gui/editor_file_dialog.h"
#include "editor/import/3d/scene_import_settings.h"
#include "editor/inspector/editor_inspector.h"
#include "editor/themes/editor_scale.h"
String SceneExporterGLTFPlugin::get_plugin_name() const {
return "ConvertGLTF2";
}
bool SceneExporterGLTFPlugin::has_main_screen() const {
return false;
}
SceneExporterGLTFPlugin::SceneExporterGLTFPlugin() {
_gltf_document.instantiate();
// Set up the file dialog.
_file_dialog = memnew(EditorFileDialog);
_file_dialog->connect("file_selected", callable_mp(this, &SceneExporterGLTFPlugin::_export_scene_as_gltf));
_file_dialog->set_title(TTR("Export Library"));
_file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
_file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
_file_dialog->clear_filters();
_file_dialog->add_filter("*.glb");
_file_dialog->add_filter("*.gltf");
_file_dialog->set_title(TTR("Export Scene to glTF 2.0 File"));
EditorNode::get_singleton()->get_gui_base()->add_child(_file_dialog);
// Set up the export settings menu.
_export_settings.instantiate();
_export_settings->generate_property_list(_gltf_document);
_settings_inspector = memnew(EditorInspector);
_settings_inspector->set_custom_minimum_size(Size2(350, 300) * EDSCALE);
_file_dialog->add_side_menu(_settings_inspector, TTR("Export Settings:"));
// Add a button to the Scene -> Export menu to pop up the settings dialog.
PopupMenu *menu = get_export_as_menu();
int idx = menu->get_item_count();
menu->add_item(TTRC("glTF 2.0 Scene..."));
menu->set_item_metadata(idx, callable_mp(this, &SceneExporterGLTFPlugin::_popup_gltf_export_dialog));
}
void SceneExporterGLTFPlugin::_popup_gltf_export_dialog() {
Node *root = EditorNode::get_singleton()->get_tree()->get_edited_scene_root();
if (!root) {
EditorNode::get_singleton()->show_accept(TTR("This operation can't be done without a scene."), TTR("OK"));
return;
}
// Set the file dialog's file name to the scene name.
String filename = String(root->get_scene_file_path().get_file().get_basename());
if (filename.is_empty()) {
filename = root->get_name();
}
_file_dialog->set_current_file(filename + String(".gltf"));
// Generate and refresh the export settings.
_export_settings->generate_property_list(_gltf_document, root);
_settings_inspector->edit(nullptr);
_settings_inspector->edit(_export_settings.ptr());
// Show the file dialog.
_file_dialog->popup_centered_ratio();
}
void SceneExporterGLTFPlugin::_export_scene_as_gltf(const String &p_file_path) {
Node *root = EditorNode::get_singleton()->get_tree()->get_edited_scene_root();
if (!root) {
EditorNode::get_singleton()->show_accept(TTR("This operation can't be done without a scene."), TTR("OK"));
return;
}
List<String> deps;
Ref<GLTFState> state;
state.instantiate();
state->set_copyright(_export_settings->get_copyright());
int32_t flags = 0;
flags |= EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS;
state->set_bake_fps(_export_settings->get_bake_fps());
Error err = _gltf_document->append_from_scene(root, state, flags);
if (err != OK) {
ERR_PRINT(vformat("glTF2 save scene error %s.", itos(err)));
}
err = _gltf_document->write_to_filesystem(state, p_file_path);
if (err != OK) {
ERR_PRINT(vformat("glTF2 save scene error %s.", itos(err)));
}
EditorFileSystem::get_singleton()->scan_changes();
}

View File

@@ -0,0 +1,55 @@
/**************************************************************************/
/* editor_scene_exporter_gltf_plugin.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "../gltf_document.h"
#include "editor_scene_exporter_gltf_settings.h"
#include "editor/plugins/editor_plugin.h"
class EditorFileDialog;
class EditorInspector;
class SceneExporterGLTFPlugin : public EditorPlugin {
GDCLASS(SceneExporterGLTFPlugin, EditorPlugin);
Ref<GLTFDocument> _gltf_document;
Ref<EditorSceneExporterGLTFSettings> _export_settings;
EditorInspector *_settings_inspector = nullptr;
EditorFileDialog *_file_dialog = nullptr;
void _popup_gltf_export_dialog();
void _export_scene_as_gltf(const String &p_file_path);
public:
virtual String get_plugin_name() const override;
bool has_main_screen() const override;
SceneExporterGLTFPlugin();
};

View File

@@ -0,0 +1,257 @@
/**************************************************************************/
/* editor_scene_exporter_gltf_settings.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "editor_scene_exporter_gltf_settings.h"
const uint32_t PROP_EDITOR_SCRIPT_VAR = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE;
bool EditorSceneExporterGLTFSettings::_set(const StringName &p_name, const Variant &p_value) {
String name_str = String(p_name);
if (name_str.contains_char('/')) {
return _set_extension_setting(name_str, p_value);
}
if (p_name == StringName("image_format")) {
_document->set_image_format(p_value);
emit_signal(CoreStringName(property_list_changed));
return true;
}
if (p_name == StringName("lossy_quality")) {
_document->set_lossy_quality(p_value);
return true;
}
if (p_name == StringName("fallback_image_format")) {
_document->set_fallback_image_format(p_value);
emit_signal(CoreStringName(property_list_changed));
return true;
}
if (p_name == StringName("fallback_image_quality")) {
_document->set_fallback_image_quality(p_value);
return true;
}
if (p_name == StringName("root_node_mode")) {
_document->set_root_node_mode((GLTFDocument::RootNodeMode)(int64_t)p_value);
return true;
}
if (p_name == StringName("visibility_mode")) {
_document->set_visibility_mode((GLTFDocument::VisibilityMode)(int64_t)p_value);
return true;
}
return false;
}
bool EditorSceneExporterGLTFSettings::_get(const StringName &p_name, Variant &r_ret) const {
String name_str = String(p_name);
if (name_str.contains_char('/')) {
return _get_extension_setting(name_str, r_ret);
}
if (p_name == StringName("image_format")) {
r_ret = _document->get_image_format();
return true;
}
if (p_name == StringName("lossy_quality")) {
r_ret = _document->get_lossy_quality();
return true;
}
if (p_name == StringName("fallback_image_format")) {
r_ret = _document->get_fallback_image_format();
return true;
}
if (p_name == StringName("fallback_image_quality")) {
r_ret = _document->get_fallback_image_quality();
return true;
}
if (p_name == StringName("root_node_mode")) {
r_ret = _document->get_root_node_mode();
return true;
}
if (p_name == StringName("visibility_mode")) {
r_ret = _document->get_visibility_mode();
return true;
}
return false;
}
void EditorSceneExporterGLTFSettings::_get_property_list(List<PropertyInfo> *p_list) const {
for (PropertyInfo prop : _property_list) {
if (prop.name == "lossy_quality") {
const String image_format = get("image_format");
const bool is_image_format_lossy = image_format == "JPEG" || image_format.containsn("Lossy");
prop.usage = is_image_format_lossy ? PROPERTY_USAGE_DEFAULT : PROPERTY_USAGE_STORAGE;
}
if (prop.name == "fallback_image_format") {
const String image_format = get("image_format");
const bool is_image_format_extension = image_format != "None" && image_format != "PNG" && image_format != "JPEG";
prop.usage = is_image_format_extension ? PROPERTY_USAGE_DEFAULT : PROPERTY_USAGE_STORAGE;
}
if (prop.name == "fallback_image_quality") {
const String image_format = get("image_format");
const bool is_image_format_extension = image_format != "None" && image_format != "PNG" && image_format != "JPEG";
const String fallback_format = get("fallback_image_format");
prop.usage = (is_image_format_extension && fallback_format != "None") ? PROPERTY_USAGE_DEFAULT : PROPERTY_USAGE_STORAGE;
}
p_list->push_back(prop);
}
}
void EditorSceneExporterGLTFSettings::_on_extension_property_list_changed() {
generate_property_list(_document);
emit_signal(CoreStringName(property_list_changed));
}
bool EditorSceneExporterGLTFSettings::_set_extension_setting(const String &p_name_str, const Variant &p_value) {
PackedStringArray split = String(p_name_str).split("/", true, 1);
if (!_config_name_to_extension_map.has(split[0])) {
return false;
}
Ref<GLTFDocumentExtension> extension = _config_name_to_extension_map[split[0]];
bool valid;
extension->set(split[1], p_value, &valid);
return valid;
}
bool EditorSceneExporterGLTFSettings::_get_extension_setting(const String &p_name_str, Variant &r_ret) const {
PackedStringArray split = String(p_name_str).split("/", true, 1);
if (!_config_name_to_extension_map.has(split[0])) {
return false;
}
Ref<GLTFDocumentExtension> extension = _config_name_to_extension_map[split[0]];
bool valid;
r_ret = extension->get(split[1], &valid);
return valid;
}
String get_friendly_config_prefix(Ref<GLTFDocumentExtension> p_extension) {
String config_prefix = p_extension->get_name();
if (!config_prefix.is_empty()) {
return config_prefix;
}
const String class_name = p_extension->get_class_name();
config_prefix = class_name.trim_prefix("GLTFDocumentExtension").trim_suffix("GLTFDocumentExtension").capitalize();
if (!config_prefix.is_empty()) {
return config_prefix;
}
PackedStringArray supported_extensions = p_extension->get_supported_extensions();
if (supported_extensions.size() > 0) {
return supported_extensions[0];
}
return "Unknown GLTFDocumentExtension";
}
bool is_any_node_invisible(Node *p_node) {
if (p_node->has_method("is_visible")) {
bool visible = p_node->call("is_visible");
if (!visible) {
return true;
}
}
for (int i = 0; i < p_node->get_child_count(); i++) {
if (is_any_node_invisible(p_node->get_child(i))) {
return true;
}
}
return false;
}
// Run this before popping up the export settings, because the extensions may have changed.
void EditorSceneExporterGLTFSettings::generate_property_list(Ref<GLTFDocument> p_document, Node *p_root) {
_property_list.clear();
_document = p_document;
String image_format_hint_string = "None,PNG,JPEG";
// Add properties from all document extensions.
for (Ref<GLTFDocumentExtension> &extension : GLTFDocument::get_all_gltf_document_extensions()) {
const Callable on_prop_changed = callable_mp(this, &EditorSceneExporterGLTFSettings::_on_extension_property_list_changed);
if (!extension->is_connected(CoreStringName(property_list_changed), on_prop_changed)) {
extension->connect(CoreStringName(property_list_changed), on_prop_changed);
}
const String config_prefix = get_friendly_config_prefix(extension);
_config_name_to_extension_map[config_prefix] = extension;
// If the extension allows saving in different image formats, add to the enum.
PackedStringArray saveable_image_formats = extension->get_saveable_image_formats();
for (int i = 0; i < saveable_image_formats.size(); i++) {
image_format_hint_string += "," + saveable_image_formats[i];
}
// Look through the extension's properties and find the relevant ones.
List<PropertyInfo> ext_prop_list;
extension->get_property_list(&ext_prop_list);
for (const PropertyInfo &prop : ext_prop_list) {
// We only want properties that will show up in the exporter
// settings list. Exclude Resource's properties, as they are
// not relevant to the exporter. Include any user-defined script
// variables exposed to the editor (PROP_EDITOR_SCRIPT_VAR).
if ((prop.usage & PROP_EDITOR_SCRIPT_VAR) == PROP_EDITOR_SCRIPT_VAR) {
PropertyInfo ext_prop = prop;
ext_prop.name = config_prefix + "/" + prop.name;
_property_list.push_back(ext_prop);
}
}
}
// Add top-level properties (in addition to what _bind_methods registers).
PropertyInfo image_format_prop = PropertyInfo(Variant::STRING, "image_format", PROPERTY_HINT_ENUM, image_format_hint_string);
_property_list.push_back(image_format_prop);
PropertyInfo lossy_quality_prop = PropertyInfo(Variant::FLOAT, "lossy_quality", PROPERTY_HINT_RANGE, "0,1,0.01");
_property_list.push_back(lossy_quality_prop);
PropertyInfo fallback_image_format_prop = PropertyInfo(Variant::STRING, "fallback_image_format", PROPERTY_HINT_ENUM, "None,PNG,JPEG");
_property_list.push_back(fallback_image_format_prop);
PropertyInfo fallback_image_quality_prop = PropertyInfo(Variant::FLOAT, "fallback_image_quality", PROPERTY_HINT_RANGE, "0,1,0.01");
_property_list.push_back(fallback_image_quality_prop);
PropertyInfo root_node_mode_prop = PropertyInfo(Variant::INT, "root_node_mode", PROPERTY_HINT_ENUM, "Single Root,Keep Root,Multi Root");
_property_list.push_back(root_node_mode_prop);
// If the scene contains any non-visible nodes, show the visibility mode setting.
if (p_root != nullptr && is_any_node_invisible(p_root)) {
PropertyInfo visibility_mode_prop = PropertyInfo(Variant::INT, "visibility_mode", PROPERTY_HINT_ENUM, "Include & Required,Include & Optional,Exclude");
_property_list.push_back(visibility_mode_prop);
}
}
String EditorSceneExporterGLTFSettings::get_copyright() const {
return _copyright;
}
void EditorSceneExporterGLTFSettings::set_copyright(const String &p_copyright) {
_copyright = p_copyright;
}
void EditorSceneExporterGLTFSettings::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_copyright"), &EditorSceneExporterGLTFSettings::get_copyright);
ClassDB::bind_method(D_METHOD("set_copyright", "copyright"), &EditorSceneExporterGLTFSettings::set_copyright);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "copyright", PROPERTY_HINT_PLACEHOLDER_TEXT, "Example: 2014 Godette"), "set_copyright", "get_copyright");
ClassDB::bind_method(D_METHOD("get_bake_fps"), &EditorSceneExporterGLTFSettings::get_bake_fps);
ClassDB::bind_method(D_METHOD("set_bake_fps", "bake_fps"), &EditorSceneExporterGLTFSettings::set_bake_fps);
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_fps"), "set_bake_fps", "get_bake_fps");
}
double EditorSceneExporterGLTFSettings::get_bake_fps() const {
return _bake_fps;
}
void EditorSceneExporterGLTFSettings::set_bake_fps(const double p_bake_fps) {
_bake_fps = p_bake_fps;
}

View File

@@ -0,0 +1,62 @@
/**************************************************************************/
/* editor_scene_exporter_gltf_settings.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "../gltf_document.h"
class EditorSceneExporterGLTFSettings : public RefCounted {
GDCLASS(EditorSceneExporterGLTFSettings, RefCounted);
List<PropertyInfo> _property_list;
Ref<GLTFDocument> _document;
HashMap<String, Ref<GLTFDocumentExtension>> _config_name_to_extension_map;
String _copyright;
double _bake_fps = 30.0;
protected:
static void _bind_methods();
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
void _on_extension_property_list_changed();
bool _set_extension_setting(const String &p_name_str, const Variant &p_value);
bool _get_extension_setting(const String &p_name_str, Variant &r_ret) const;
public:
void generate_property_list(Ref<GLTFDocument> p_document, Node *p_root = nullptr);
String get_copyright() const;
void set_copyright(const String &p_copyright);
double get_bake_fps() const;
void set_bake_fps(const double p_bake_fps);
};

View File

@@ -0,0 +1,615 @@
/**************************************************************************/
/* editor_scene_importer_blend.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "editor_scene_importer_blend.h"
#include "../gltf_defines.h"
#include "../gltf_document.h"
#include "editor_import_blend_runner.h"
#include "core/config/project_settings.h"
#include "editor/editor_node.h"
#include "editor/editor_string_names.h"
#include "editor/gui/editor_file_dialog.h"
#include "editor/settings/editor_settings.h"
#include "editor/themes/editor_scale.h"
#include "main/main.h"
#include "scene/gui/line_edit.h"
#ifdef WINDOWS_ENABLED
#include <shlwapi.h>
#endif
static bool _get_blender_version(const String &p_path, int &r_major, int &r_minor, String *r_err = nullptr) {
if (!FileAccess::exists(p_path)) {
if (r_err) {
*r_err = TTR("Path does not point to a valid executable.");
}
return false;
}
List<String> args;
args.push_back("--version");
String pipe;
Error err = OS::get_singleton()->execute(p_path, args, &pipe);
if (err != OK) {
if (r_err) {
*r_err = TTR("Couldn't run Blender executable.");
}
return false;
}
int bl = pipe.find("Blender ");
if (bl == -1) {
if (r_err) {
*r_err = vformat(TTR("Unexpected --version output from Blender executable at: %s."), p_path);
}
return false;
}
pipe = pipe.substr(bl);
pipe = pipe.replace_first("Blender ", "");
int pp = pipe.find_char('.');
if (pp == -1) {
if (r_err) {
*r_err = vformat(TTR("Couldn't extract version information from Blender executable at: %s."), p_path);
}
return false;
}
String v = pipe.substr(0, pp);
r_major = v.to_int();
if (r_major < 3) {
if (r_err) {
*r_err = vformat(TTR("Found Blender version %d.x, which is too old for this importer (3.0+ is required)."), r_major);
}
return false;
}
int pp2 = pipe.find_char('.', pp + 1);
r_minor = pp2 > pp ? pipe.substr(pp + 1, pp2 - pp - 1).to_int() : 0;
return true;
}
void EditorSceneFormatImporterBlend::get_extensions(List<String> *r_extensions) const {
r_extensions->push_back("blend");
}
Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_t p_flags,
const HashMap<StringName, Variant> &p_options,
List<String> *r_missing_deps, Error *r_err) {
String blender_path = EDITOR_GET("filesystem/import/blender/blender_path");
ERR_FAIL_COND_V_MSG(blender_path.is_empty(), nullptr, "Blender path is empty, check your Editor Settings.");
ERR_FAIL_COND_V_MSG(!FileAccess::exists(blender_path), nullptr, vformat("Invalid Blender path: %s, check your Editor Settings.", blender_path));
if (blender_major_version == -1 || blender_minor_version == -1 || last_tested_blender_path != blender_path) {
String error;
if (!_get_blender_version(blender_path, blender_major_version, blender_minor_version, &error)) {
ERR_FAIL_V_MSG(nullptr, error);
}
last_tested_blender_path = blender_path;
}
// Get global paths for source and sink.
// Escape paths to be valid Python strings to embed in the script.
String source_global = ProjectSettings::get_singleton()->globalize_path(p_path);
#ifdef WINDOWS_ENABLED
// On Windows, when using a network share path, the above will return a path starting with "//"
// which once handed to Blender will be treated like a relative path. So we need to replace the
// first two characters with "\\" to make it absolute again.
if (source_global.is_network_share_path()) {
source_global = "\\\\" + source_global.substr(2);
}
#endif
const String blend_basename = p_path.get_file().get_basename();
const String sink = ProjectSettings::get_singleton()->get_imported_files_path().path_join(
vformat("%s-%s.gltf", blend_basename, p_path.md5_text()));
const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink);
// If true, unpack the original images to the Godot file system and use them. Allows changing image import settings like VRAM compression.
// If false, allow Blender to convert the original images, such as re-packing roughness and metallic into one roughness+metallic texture.
// In most cases this is desired, but if the .blend file's images are not in the correct format, this must be disabled for correct behavior.
const bool unpack_original_images = p_options.has(SNAME("blender/materials/unpack_enabled")) && p_options[SNAME("blender/materials/unpack_enabled")];
// Handle configuration options.
Dictionary request_options;
Dictionary parameters_map;
parameters_map["filepath"] = sink_global;
parameters_map["export_keep_originals"] = unpack_original_images;
parameters_map["export_format"] = "GLTF_SEPARATE";
parameters_map["export_yup"] = true;
parameters_map["export_import_convert_lighting_mode"] = "COMPAT";
if (p_options.has(SNAME("blender/nodes/custom_properties")) && p_options[SNAME("blender/nodes/custom_properties")]) {
parameters_map["export_extras"] = true;
} else {
parameters_map["export_extras"] = false;
}
if (p_options.has(SNAME("blender/meshes/skins"))) {
int32_t skins = p_options["blender/meshes/skins"];
if (skins == BLEND_BONE_INFLUENCES_NONE) {
parameters_map["export_skins"] = false;
} else if (skins == BLEND_BONE_INFLUENCES_COMPATIBLE) {
parameters_map["export_skins"] = true;
parameters_map["export_all_influences"] = false;
} else if (skins == BLEND_BONE_INFLUENCES_ALL) {
parameters_map["export_skins"] = true;
parameters_map["export_all_influences"] = true;
}
} else {
parameters_map["export_skins"] = false;
}
if (p_options.has(SNAME("blender/materials/export_materials"))) {
int32_t exports = p_options["blender/materials/export_materials"];
switch (exports) {
case BLEND_MATERIAL_EXPORT_PLACEHOLDER: {
parameters_map["export_materials"] = "PLACEHOLDER";
} break;
case BLEND_MATERIAL_EXPORT_EXPORT: {
parameters_map["export_materials"] = "EXPORT";
} break;
case BLEND_MATERIAL_EXPORT_NAMED_PLACEHOLDER: {
parameters_map["export_materials"] = "EXPORT";
parameters_map["export_image_format"] = "NONE";
} break;
}
} else {
parameters_map["export_materials"] = "PLACEHOLDER";
}
if (p_options.has(SNAME("blender/nodes/cameras")) && p_options[SNAME("blender/nodes/cameras")]) {
parameters_map["export_cameras"] = true;
} else {
parameters_map["export_cameras"] = false;
}
if (p_options.has(SNAME("blender/nodes/punctual_lights")) && p_options[SNAME("blender/nodes/punctual_lights")]) {
parameters_map["export_lights"] = true;
} else {
parameters_map["export_lights"] = false;
}
if (blender_major_version > 4 || (blender_major_version == 4 && blender_minor_version >= 2)) {
if (p_options.has(SNAME("blender/meshes/colors")) && p_options[SNAME("blender/meshes/colors")]) {
parameters_map["export_vertex_color"] = "MATERIAL";
} else {
parameters_map["export_vertex_color"] = "NONE";
}
} else {
if (p_options.has(SNAME("blender/meshes/colors")) && p_options[SNAME("blender/meshes/colors")]) {
parameters_map["export_colors"] = true;
} else {
parameters_map["export_colors"] = false;
}
}
if (p_options.has(SNAME("blender/nodes/visible"))) {
int32_t visible = p_options["blender/nodes/visible"];
if (visible == BLEND_VISIBLE_VISIBLE_ONLY) {
parameters_map["use_visible"] = true;
} else if (visible == BLEND_VISIBLE_RENDERABLE) {
parameters_map["use_renderable"] = true;
} else if (visible == BLEND_VISIBLE_ALL) {
parameters_map["use_renderable"] = false;
parameters_map["use_visible"] = false;
}
} else {
parameters_map["use_renderable"] = false;
parameters_map["use_visible"] = false;
}
if (p_options.has(SNAME("blender/nodes/active_collection_only")) && p_options[SNAME("blender/nodes/active_collection_only")]) {
parameters_map["use_active_collection"] = true;
}
if (p_options.has(SNAME("blender/meshes/uvs")) && p_options[SNAME("blender/meshes/uvs")]) {
parameters_map["export_texcoords"] = true;
} else {
parameters_map["export_texcoords"] = false;
}
if (p_options.has(SNAME("blender/meshes/normals")) && p_options[SNAME("blender/meshes/normals")]) {
parameters_map["export_normals"] = true;
} else {
parameters_map["export_normals"] = false;
}
if (blender_major_version > 4 || (blender_major_version == 4 && blender_minor_version >= 1)) {
if (p_options.has(SNAME("blender/meshes/export_geometry_nodes_instances")) && p_options[SNAME("blender/meshes/export_geometry_nodes_instances")]) {
parameters_map["export_gn_mesh"] = true;
if (blender_major_version == 4 && blender_minor_version == 1) {
// There is a bug in Blender 4.1 where it can't export lights and geometry nodes at the same time, one must be disabled.
parameters_map["export_lights"] = false;
}
} else {
parameters_map["export_gn_mesh"] = false;
}
}
if (p_options.has(SNAME("blender/meshes/tangents")) && p_options[SNAME("blender/meshes/tangents")]) {
parameters_map["export_tangents"] = true;
} else {
parameters_map["export_tangents"] = false;
}
if (p_options.has(SNAME("blender/animation/group_tracks")) && p_options[SNAME("blender/animation/group_tracks")]) {
if (blender_major_version > 3 || (blender_major_version == 3 && blender_minor_version >= 6)) {
parameters_map["export_animation_mode"] = "ACTIONS";
} else {
parameters_map["export_nla_strips"] = true;
}
} else {
if (blender_major_version > 3 || (blender_major_version == 3 && blender_minor_version >= 6)) {
parameters_map["export_animation_mode"] = "ACTIVE_ACTIONS";
} else {
parameters_map["export_nla_strips"] = false;
}
}
if (p_options.has(SNAME("blender/animation/limit_playback")) && p_options[SNAME("blender/animation/limit_playback")]) {
parameters_map["export_frame_range"] = true;
} else {
parameters_map["export_frame_range"] = false;
}
if (p_options.has(SNAME("blender/animation/always_sample")) && p_options[SNAME("blender/animation/always_sample")]) {
parameters_map["export_force_sampling"] = true;
} else {
parameters_map["export_force_sampling"] = false;
}
if (p_options.has(SNAME("blender/meshes/export_bones_deforming_mesh_only")) && p_options[SNAME("blender/meshes/export_bones_deforming_mesh_only")]) {
parameters_map["export_def_bones"] = true;
} else {
parameters_map["export_def_bones"] = false;
}
if (p_options.has(SNAME("blender/nodes/modifiers")) && p_options[SNAME("blender/nodes/modifiers")]) {
parameters_map["export_apply"] = true;
} else {
parameters_map["export_apply"] = false;
}
request_options["unpack_all"] = unpack_original_images;
request_options["path"] = source_global;
request_options["gltf_options"] = parameters_map;
// Run Blender and export glTF.
Error err = EditorImportBlendRunner::get_singleton()->do_import(request_options);
if (err != OK) {
if (r_err) {
*r_err = ERR_SCRIPT_FAILED;
}
return nullptr;
}
// Import the generated glTF.
// Use GLTFDocument instead of glTF importer to keep image references.
Ref<GLTFDocument> gltf;
gltf.instantiate();
Ref<GLTFState> state;
state.instantiate();
if (p_options.has("gltf/naming_version")) {
int naming_version = p_options["gltf/naming_version"];
gltf->set_naming_version(naming_version);
}
if (p_options.has(SNAME("nodes/import_as_skeleton_bones")) ? (bool)p_options[SNAME("nodes/import_as_skeleton_bones")] : false) {
state->set_import_as_skeleton_bones(true);
}
state->set_scene_name(blend_basename);
state->set_extract_path(p_path.get_base_dir());
state->set_extract_prefix(blend_basename);
err = gltf->append_from_file(sink.get_basename() + ".gltf", state, p_flags, sink.get_base_dir());
if (err != OK) {
if (r_err) {
*r_err = FAILED;
}
return nullptr;
}
ERR_FAIL_COND_V(!p_options.has("animation/fps"), nullptr);
#ifndef DISABLE_DEPRECATED
bool trimming = p_options.has("animation/trimming") ? (bool)p_options["animation/trimming"] : false;
return gltf->generate_scene(state, (float)p_options["animation/fps"], trimming, false);
#else
return gltf->generate_scene(state, (float)p_options["animation/fps"], (bool)p_options["animation/trimming"], false);
#endif
}
Variant EditorSceneFormatImporterBlend::get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option,
const HashMap<StringName, Variant> &p_options) {
if (p_path.get_extension().to_lower() != "blend") {
return true;
}
if (p_option.begins_with("animation/")) {
if (p_option != "animation/import" && !bool(p_options["animation/import"])) {
return false;
}
}
return true;
}
void EditorSceneFormatImporterBlend::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options) {
// Returns all the options when path is empty because that means it's for the Project Settings.
if (!p_path.is_empty() && p_path.get_extension().to_lower() != "blend") {
return;
}
#define ADD_OPTION_BOOL(PATH, VALUE) \
r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, SNAME(PATH)), VALUE));
#define ADD_OPTION_ENUM(PATH, ENUM_HINT, VALUE) \
r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::INT, SNAME(PATH), PROPERTY_HINT_ENUM, ENUM_HINT), VALUE));
ADD_OPTION_ENUM("blender/nodes/visible", "All,Visible Only,Renderable", BLEND_VISIBLE_ALL);
ADD_OPTION_BOOL("blender/nodes/active_collection_only", false);
ADD_OPTION_BOOL("blender/nodes/punctual_lights", true);
ADD_OPTION_BOOL("blender/nodes/cameras", true);
ADD_OPTION_BOOL("blender/nodes/custom_properties", true);
ADD_OPTION_ENUM("blender/nodes/modifiers", "No Modifiers,All Modifiers", BLEND_MODIFIERS_ALL);
ADD_OPTION_BOOL("blender/meshes/colors", false);
ADD_OPTION_BOOL("blender/meshes/uvs", true);
ADD_OPTION_BOOL("blender/meshes/normals", true);
ADD_OPTION_BOOL("blender/meshes/export_geometry_nodes_instances", false);
ADD_OPTION_BOOL("blender/meshes/tangents", true);
ADD_OPTION_ENUM("blender/meshes/skins", "None,4 Influences (Compatible),All Influences", BLEND_BONE_INFLUENCES_ALL);
ADD_OPTION_BOOL("blender/meshes/export_bones_deforming_mesh_only", false);
ADD_OPTION_BOOL("blender/materials/unpack_enabled", true);
ADD_OPTION_ENUM("blender/materials/export_materials", "Placeholder,Export,Named Placeholder", BLEND_MATERIAL_EXPORT_EXPORT);
ADD_OPTION_BOOL("blender/animation/limit_playback", true);
ADD_OPTION_BOOL("blender/animation/always_sample", true);
ADD_OPTION_BOOL("blender/animation/group_tracks", true);
r_options->push_back(ResourceImporterScene::ImportOption(PropertyInfo(Variant::INT, "gltf/naming_version", PROPERTY_HINT_ENUM, "Godot 4.0 or 4.1,Godot 4.2 to 4.4,Godot 4.5 or later"), 2));
}
void EditorSceneFormatImporterBlend::handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {
if (!p_import_params.has("gltf/naming_version")) {
// If a .blend's existing import file is missing the glTF
// naming compatibility version, we need to use version 1.
// Version 1 is the behavior before this option was added.
p_import_params["gltf/naming_version"] = 1;
}
}
///////////////////////////
static bool _test_blender_path(const String &p_path, String *r_err = nullptr) {
int major, minor;
return _get_blender_version(p_path, major, minor, r_err);
}
bool EditorFileSystemImportFormatSupportQueryBlend::is_active() const {
bool blend_enabled = GLOBAL_GET_CACHED(bool, "filesystem/import/blender/enabled");
if (blend_enabled && !_test_blender_path(EDITOR_GET("filesystem/import/blender/blender_path").operator String())) {
// Intending to import Blender, but blend not configured.
return true;
}
return false;
}
Vector<String> EditorFileSystemImportFormatSupportQueryBlend::get_file_extensions() const {
Vector<String> ret;
ret.push_back("blend");
return ret;
}
void EditorFileSystemImportFormatSupportQueryBlend::_validate_path(String p_path) {
String error;
bool success = false;
if (p_path == "") {
error = TTR("Path is empty.");
} else {
if (_test_blender_path(p_path, &error)) {
success = true;
if (auto_detected_path == p_path) {
error = TTR("Path to Blender executable is valid (Autodetected).");
} else {
error = TTR("Path to Blender executable is valid.");
}
}
}
path_status->set_text(error);
if (success) {
path_status->add_theme_color_override(SceneStringName(font_color), path_status->get_theme_color(SNAME("success_color"), EditorStringName(Editor)));
configure_blender_dialog->get_ok_button()->set_disabled(false);
} else {
path_status->add_theme_color_override(SceneStringName(font_color), path_status->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
configure_blender_dialog->get_ok_button()->set_disabled(true);
}
}
bool EditorFileSystemImportFormatSupportQueryBlend::_autodetect_path() {
// Autodetect
auto_detected_path = "";
#if defined(MACOS_ENABLED)
Vector<String> find_paths = {
"/opt/homebrew/bin/blender",
"/opt/local/bin/blender",
"/usr/local/bin/blender",
"/usr/local/opt/blender",
"/Applications/Blender.app/Contents/MacOS/Blender",
};
{
List<String> mdfind_args;
mdfind_args.push_back("kMDItemCFBundleIdentifier=org.blenderfoundation.blender");
String output;
Error err = OS::get_singleton()->execute("mdfind", mdfind_args, &output);
if (err == OK) {
for (const String &find_path : output.split("\n")) {
find_paths.push_back(find_path.path_join("Contents/MacOS/Blender"));
}
}
}
#elif defined(WINDOWS_ENABLED)
Vector<String> find_paths = {
"C:\\Program Files\\Blender Foundation\\blender.exe",
"C:\\Program Files (x86)\\Blender Foundation\\blender.exe",
};
{
char blender_opener_path[MAX_PATH];
DWORD path_len = MAX_PATH;
HRESULT res = AssocQueryString(0, ASSOCSTR_EXECUTABLE, ".blend", "open", blender_opener_path, &path_len);
if (res == S_OK) {
find_paths.push_back(String(blender_opener_path).get_base_dir().path_join("blender.exe"));
}
}
#elif defined(UNIX_ENABLED)
Vector<String> find_paths = {
"/usr/bin/blender",
"/usr/local/bin/blender",
"/opt/blender/bin/blender",
};
#endif
for (const String &find_path : find_paths) {
if (_test_blender_path(find_path)) {
auto_detected_path = find_path;
return true;
}
}
return false;
}
void EditorFileSystemImportFormatSupportQueryBlend::_path_confirmed() {
confirmed = true;
}
void EditorFileSystemImportFormatSupportQueryBlend::_select_install(String p_path) {
blender_path->set_text(p_path);
_validate_path(p_path);
}
void EditorFileSystemImportFormatSupportQueryBlend::_browse_install() {
if (blender_path->get_text() != String()) {
browse_dialog->set_current_file(blender_path->get_text());
}
browse_dialog->popup_centered_ratio();
}
void EditorFileSystemImportFormatSupportQueryBlend::_update_icons() {
blender_path_browse->set_button_icon(blender_path_browse->get_editor_theme_icon(SNAME("FolderBrowse")));
}
bool EditorFileSystemImportFormatSupportQueryBlend::query() {
if (!configure_blender_dialog) {
configure_blender_dialog = memnew(ConfirmationDialog);
configure_blender_dialog->set_title(TTR("Configure Blender Importer"));
configure_blender_dialog->set_flag(Window::FLAG_BORDERLESS, true); // Avoid closing accidentally.
configure_blender_dialog->set_close_on_escape(false);
String select_exec_label = TTR("Blender 3.0+ is required to import '.blend' files.\nPlease provide a valid path to a Blender executable.");
#ifdef MACOS_ENABLED
select_exec_label += "\n" + TTR("On macOS, this should be the `Contents/MacOS/blender` file within the Blender `.app` folder.");
#endif
VBoxContainer *vb = memnew(VBoxContainer);
vb->add_child(memnew(Label(select_exec_label)));
HBoxContainer *hb = memnew(HBoxContainer);
blender_path = memnew(LineEdit);
blender_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
blender_path->set_accessibility_name(TTRC("Path"));
hb->add_child(blender_path);
blender_path_browse = memnew(Button);
blender_path_browse->set_text(TTR("Browse"));
blender_path_browse->connect(SceneStringName(pressed), callable_mp(this, &EditorFileSystemImportFormatSupportQueryBlend::_browse_install));
hb->add_child(blender_path_browse);
hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
hb->set_custom_minimum_size(Size2(400 * EDSCALE, 0));
vb->add_child(hb);
path_status = memnew(Label);
path_status->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
vb->add_child(path_status);
configure_blender_dialog->add_child(vb);
blender_path->connect(SceneStringName(text_changed), callable_mp(this, &EditorFileSystemImportFormatSupportQueryBlend::_validate_path));
EditorNode::get_singleton()->get_gui_base()->add_child(configure_blender_dialog);
configure_blender_dialog->set_ok_button_text(TTR("Confirm Path"));
configure_blender_dialog->set_cancel_button_text(TTR("Disable '.blend' Import"));
configure_blender_dialog->get_cancel_button()->set_tooltip_text(TTR("Disables Blender '.blend' files import for this project. Can be re-enabled in Project Settings."));
configure_blender_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorFileSystemImportFormatSupportQueryBlend::_path_confirmed));
browse_dialog = memnew(EditorFileDialog);
browse_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
browse_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
browse_dialog->connect("file_selected", callable_mp(this, &EditorFileSystemImportFormatSupportQueryBlend::_select_install));
EditorNode::get_singleton()->get_gui_base()->add_child(browse_dialog);
// Update icons.
// This is a hack because we can't rely on notifications here as we don't receive them.
// Usually, we only have to wait for `NOTIFICATION_THEME_CHANGED` to update the icons.
callable_mp(this, &EditorFileSystemImportFormatSupportQueryBlend::_update_icons).call_deferred();
}
String path = EDITOR_GET("filesystem/import/blender/blender_path");
if (path.is_empty() && _autodetect_path()) {
path = auto_detected_path;
}
blender_path->set_text(path);
_validate_path(path);
configure_blender_dialog->popup_centered();
confirmed = false;
while (true) {
DisplayServer::get_singleton()->process_events();
Main::iteration();
if (!configure_blender_dialog->is_visible() || confirmed) {
break;
}
}
if (confirmed) {
// Can only confirm a valid path.
EditorSettings::get_singleton()->set("filesystem/import/blender/blender_path", blender_path->get_text());
EditorSettings::get_singleton()->save();
} else {
// Disable Blender import
ProjectSettings::get_singleton()->set("filesystem/import/blender/enabled", false);
ProjectSettings::get_singleton()->save();
if (EditorNode::immediate_confirmation_dialog(TTR("Disabling '.blend' file import requires restarting the editor."), TTR("Save & Restart"), TTR("Restart"))) {
EditorNode::get_singleton()->save_all_scenes();
}
EditorNode::get_singleton()->restart_editor();
return true;
}
return false;
}

View File

@@ -0,0 +1,110 @@
/**************************************************************************/
/* editor_scene_importer_blend.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "editor/file_system/editor_file_system.h"
#include "editor/import/3d/resource_importer_scene.h"
class Animation;
class Node;
class ConfirmationDialog;
class EditorSceneFormatImporterBlend : public EditorSceneFormatImporter {
GDCLASS(EditorSceneFormatImporterBlend, EditorSceneFormatImporter);
int blender_major_version = -1;
int blender_minor_version = -1;
String last_tested_blender_path;
public:
enum {
BLEND_VISIBLE_ALL,
BLEND_VISIBLE_VISIBLE_ONLY,
BLEND_VISIBLE_RENDERABLE
};
enum {
BLEND_BONE_INFLUENCES_NONE,
BLEND_BONE_INFLUENCES_COMPATIBLE,
BLEND_BONE_INFLUENCES_ALL
};
enum {
BLEND_MATERIAL_EXPORT_PLACEHOLDER,
BLEND_MATERIAL_EXPORT_EXPORT,
BLEND_MATERIAL_EXPORT_NAMED_PLACEHOLDER,
};
enum {
BLEND_MODIFIERS_NONE,
BLEND_MODIFIERS_ALL
};
virtual void get_extensions(List<String> *r_extensions) const override;
virtual Node *import_scene(const String &p_path, uint32_t p_flags,
const HashMap<StringName, Variant> &p_options,
List<String> *r_missing_deps, Error *r_err = nullptr) override;
virtual void get_import_options(const String &p_path,
List<ResourceImporter::ImportOption> *r_options) override;
virtual Variant get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option,
const HashMap<StringName, Variant> &p_options) override;
virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const override;
};
class LineEdit;
class Button;
class EditorFileDialog;
class Label;
class EditorFileSystemImportFormatSupportQueryBlend : public EditorFileSystemImportFormatSupportQuery {
GDCLASS(EditorFileSystemImportFormatSupportQueryBlend, EditorFileSystemImportFormatSupportQuery);
ConfirmationDialog *configure_blender_dialog = nullptr;
LineEdit *blender_path = nullptr;
Button *blender_path_browse = nullptr;
EditorFileDialog *browse_dialog = nullptr;
Label *path_status = nullptr;
bool confirmed = false;
String auto_detected_path;
void _validate_path(String p_path);
bool _autodetect_path();
void _path_confirmed();
void _select_install(String p_path);
void _browse_install();
void _update_icons();
public:
virtual bool is_active() const override;
virtual Vector<String> get_file_extensions() const override;
virtual bool query() override;
};

View File

@@ -0,0 +1,103 @@
/**************************************************************************/
/* editor_scene_importer_gltf.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "editor_scene_importer_gltf.h"
#include "../gltf_defines.h"
#include "../gltf_document.h"
void EditorSceneFormatImporterGLTF::get_extensions(List<String> *r_extensions) const {
r_extensions->push_back("gltf");
r_extensions->push_back("glb");
}
Node *EditorSceneFormatImporterGLTF::import_scene(const String &p_path, uint32_t p_flags,
const HashMap<StringName, Variant> &p_options,
List<String> *r_missing_deps, Error *r_err) {
Ref<GLTFDocument> gltf;
gltf.instantiate();
Ref<GLTFState> state;
state.instantiate();
if (p_options.has("gltf/naming_version")) {
int naming_version = p_options["gltf/naming_version"];
gltf->set_naming_version(naming_version);
}
if (p_options.has("gltf/embedded_image_handling")) {
int32_t enum_option = p_options["gltf/embedded_image_handling"];
state->set_handle_binary_image(enum_option);
}
if (p_options.has(SNAME("nodes/import_as_skeleton_bones")) ? (bool)p_options[SNAME("nodes/import_as_skeleton_bones")] : false) {
state->set_import_as_skeleton_bones(true);
}
if (p_options.has(SNAME("extract_path"))) {
state->set_extract_path(p_options["extract_path"]);
}
state->set_bake_fps(p_options["animation/fps"]);
Error err = gltf->append_from_file(p_path, state, p_flags);
if (err != OK) {
if (r_err) {
*r_err = err;
}
return nullptr;
}
if (p_options.has("animation/import")) {
state->set_create_animations(bool(p_options["animation/import"]));
}
#ifndef DISABLE_DEPRECATED
bool trimming = p_options.has("animation/trimming") ? (bool)p_options["animation/trimming"] : false;
return gltf->generate_scene(state, state->get_bake_fps(), trimming, false);
#else
return gltf->generate_scene(state, state->get_bake_fps(), (bool)p_options["animation/trimming"], false);
#endif
}
void EditorSceneFormatImporterGLTF::get_import_options(const String &p_path,
List<ResourceImporter::ImportOption> *r_options) {
String file_extension = p_path.get_extension().to_lower();
// Returns all the options when path is empty because that means it's for the Project Settings.
if (p_path.is_empty() || file_extension == "gltf" || file_extension == "glb") {
r_options->push_back(ResourceImporterScene::ImportOption(PropertyInfo(Variant::INT, "gltf/naming_version", PROPERTY_HINT_ENUM, "Godot 4.0 or 4.1,Godot 4.2 to 4.4,Godot 4.5 or later"), 2));
r_options->push_back(ResourceImporterScene::ImportOption(PropertyInfo(Variant::INT, "gltf/embedded_image_handling", PROPERTY_HINT_ENUM, "Discard All Textures,Extract Textures,Embed as Basis Universal,Embed as Uncompressed", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), GLTFState::HANDLE_BINARY_EXTRACT_TEXTURES));
}
}
void EditorSceneFormatImporterGLTF::handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {
if (!p_import_params.has("gltf/naming_version")) {
// If an existing import file is missing the glTF
// compatibility version, we need to use version 0.
p_import_params["gltf/naming_version"] = 0;
}
}
Variant EditorSceneFormatImporterGLTF::get_option_visibility(const String &p_path, const String &p_scene_import_type,
const String &p_option, const HashMap<StringName, Variant> &p_options) {
return true;
}

View File

@@ -0,0 +1,51 @@
/**************************************************************************/
/* editor_scene_importer_gltf.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "editor/import/3d/resource_importer_scene.h"
class Animation;
class Node;
class EditorSceneFormatImporterGLTF : public EditorSceneFormatImporter {
GDCLASS(EditorSceneFormatImporterGLTF, EditorSceneFormatImporter);
public:
virtual void get_extensions(List<String> *r_extensions) const override;
virtual Node *import_scene(const String &p_path, uint32_t p_flags,
const HashMap<StringName, Variant> &p_options,
List<String> *r_missing_deps, Error *r_err = nullptr) override;
virtual void get_import_options(const String &p_path,
List<ResourceImporter::ImportOption> *r_options) override;
virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const override;
virtual Variant get_option_visibility(const String &p_path, const String &p_scene_import_type,
const String &p_option, const HashMap<StringName, Variant> &p_options) override;
};

View File

@@ -0,0 +1,13 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *
Import("env")
Import("env_modules")
env_gltf = env_modules.Clone()
# Godot source files
env_gltf.add_source_files(env.modules_sources, "*.cpp")
if not env["disable_physics_3d"]:
env_gltf.add_source_files(env.modules_sources, "physics/*.cpp")

View File

@@ -0,0 +1,233 @@
/**************************************************************************/
/* gltf_document_extension.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_document_extension.h"
void GLTFDocumentExtension::_bind_methods() {
// Import process.
GDVIRTUAL_BIND(_import_preflight, "state", "extensions");
GDVIRTUAL_BIND(_get_supported_extensions);
GDVIRTUAL_BIND(_parse_node_extensions, "state", "gltf_node", "extensions");
GDVIRTUAL_BIND(_parse_image_data, "state", "image_data", "mime_type", "ret_image");
GDVIRTUAL_BIND(_get_image_file_extension);
GDVIRTUAL_BIND(_parse_texture_json, "state", "texture_json", "ret_gltf_texture");
GDVIRTUAL_BIND(_import_object_model_property, "state", "split_json_pointer", "partial_paths");
GDVIRTUAL_BIND(_import_post_parse, "state");
GDVIRTUAL_BIND(_import_pre_generate, "state");
GDVIRTUAL_BIND(_generate_scene_node, "state", "gltf_node", "scene_parent");
GDVIRTUAL_BIND(_import_node, "state", "gltf_node", "json", "node");
GDVIRTUAL_BIND(_import_post, "state", "root");
// Export process.
GDVIRTUAL_BIND(_export_preflight, "state", "root");
GDVIRTUAL_BIND(_convert_scene_node, "state", "gltf_node", "scene_node");
GDVIRTUAL_BIND(_export_post_convert, "state", "root");
GDVIRTUAL_BIND(_export_preserialize, "state");
GDVIRTUAL_BIND(_export_object_model_property, "state", "node_path", "godot_node", "gltf_node_index", "target_object", "target_depth");
GDVIRTUAL_BIND(_get_saveable_image_formats);
GDVIRTUAL_BIND(_serialize_image_to_bytes, "state", "image", "image_dict", "image_format", "lossy_quality");
GDVIRTUAL_BIND(_save_image_at_path, "state", "image", "file_path", "image_format", "lossy_quality");
GDVIRTUAL_BIND(_serialize_texture_json, "state", "texture_json", "gltf_texture", "image_format");
GDVIRTUAL_BIND(_export_node, "state", "gltf_node", "json", "node");
GDVIRTUAL_BIND(_export_post, "state");
}
// Import process.
Error GLTFDocumentExtension::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_import_preflight, p_state, p_extensions, err);
return err;
}
Vector<String> GLTFDocumentExtension::get_supported_extensions() {
Vector<String> ret;
GDVIRTUAL_CALL(_get_supported_extensions, ret);
return ret;
}
Error GLTFDocumentExtension::parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_parse_node_extensions, p_state, p_gltf_node, p_extensions, err);
return err;
}
Error GLTFDocumentExtension::parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(r_image.is_null(), ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_parse_image_data, p_state, p_image_data, p_mime_type, r_image, err);
return err;
}
String GLTFDocumentExtension::get_image_file_extension() {
String ret;
GDVIRTUAL_CALL(_get_image_file_extension, ret);
return ret;
}
Error GLTFDocumentExtension::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(r_gltf_texture.is_null(), ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_parse_texture_json, p_state, p_texture_json, r_gltf_texture, err);
return err;
}
Ref<GLTFObjectModelProperty> GLTFDocumentExtension::import_object_model_property(Ref<GLTFState> p_state, const PackedStringArray &p_split_json_pointer, const TypedArray<NodePath> &p_partial_paths) {
Ref<GLTFObjectModelProperty> ret;
ERR_FAIL_COND_V(p_state.is_null(), ret);
GDVIRTUAL_CALL(_import_object_model_property, p_state, p_split_json_pointer, p_partial_paths, ret);
return ret;
}
Error GLTFDocumentExtension::import_post_parse(Ref<GLTFState> p_state) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_import_post_parse, p_state, err);
return err;
}
Error GLTFDocumentExtension::import_pre_generate(Ref<GLTFState> p_state) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_import_pre_generate, p_state, err);
return err;
}
Node3D *GLTFDocumentExtension::generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) {
ERR_FAIL_COND_V(p_state.is_null(), nullptr);
ERR_FAIL_COND_V(p_gltf_node.is_null(), nullptr);
Node3D *ret_node = nullptr;
GDVIRTUAL_CALL(_generate_scene_node, p_state, p_gltf_node, p_scene_parent, ret_node);
return ret_node;
}
Error GLTFDocumentExtension::import_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_dict, Node *p_node) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);
ERR_FAIL_NULL_V(p_node, ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_import_node, p_state, p_gltf_node, r_dict, p_node, err);
return err;
}
Error GLTFDocumentExtension::import_post(Ref<GLTFState> p_state, Node *p_root) {
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_import_post, p_state, p_root, err);
return err;
}
// Export process.
Error GLTFDocumentExtension::export_preflight(Ref<GLTFState> p_state, Node *p_root) {
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_export_preflight, p_state, p_root, err);
return err;
}
void GLTFDocumentExtension::convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) {
ERR_FAIL_COND(p_state.is_null());
ERR_FAIL_COND(p_gltf_node.is_null());
ERR_FAIL_NULL(p_scene_node);
GDVIRTUAL_CALL(_convert_scene_node, p_state, p_gltf_node, p_scene_node);
}
Error GLTFDocumentExtension::export_post_convert(Ref<GLTFState> p_state, Node *p_root) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_export_post_convert, p_state, p_root, err);
return err;
}
Error GLTFDocumentExtension::export_preserialize(Ref<GLTFState> p_state) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_export_preserialize, p_state, err);
return err;
}
Ref<GLTFObjectModelProperty> GLTFDocumentExtension::export_object_model_property(Ref<GLTFState> p_state, const NodePath &p_node_path, const Node *p_godot_node, GLTFNodeIndex p_gltf_node_index, const Object *p_target_object, int p_target_depth) {
Ref<GLTFObjectModelProperty> ret;
ERR_FAIL_COND_V(p_state.is_null(), ret);
ERR_FAIL_NULL_V(p_godot_node, ret);
ERR_FAIL_NULL_V(p_target_object, ret);
GDVIRTUAL_CALL(_export_object_model_property, p_state, p_node_path, p_godot_node, p_gltf_node_index, p_target_object, p_target_depth, ret);
return ret;
}
Vector<String> GLTFDocumentExtension::get_saveable_image_formats() {
Vector<String> ret;
GDVIRTUAL_CALL(_get_saveable_image_formats, ret);
return ret;
}
PackedByteArray GLTFDocumentExtension::serialize_image_to_bytes(Ref<GLTFState> p_state, Ref<Image> p_image, Dictionary p_image_dict, const String &p_image_format, float p_lossy_quality) {
PackedByteArray ret;
ERR_FAIL_COND_V(p_state.is_null(), ret);
ERR_FAIL_COND_V(p_image.is_null(), ret);
GDVIRTUAL_CALL(_serialize_image_to_bytes, p_state, p_image, p_image_dict, p_image_format, p_lossy_quality, ret);
return ret;
}
Error GLTFDocumentExtension::save_image_at_path(Ref<GLTFState> p_state, Ref<Image> p_image, const String &p_file_path, const String &p_image_format, float p_lossy_quality) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(p_image.is_null(), ERR_INVALID_PARAMETER);
Error ret = OK;
GDVIRTUAL_CALL(_save_image_at_path, p_state, p_image, p_file_path, p_image_format, p_lossy_quality, ret);
return ret;
}
Error GLTFDocumentExtension::serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(p_gltf_texture.is_null(), ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_serialize_texture_json, p_state, p_texture_json, p_gltf_texture, p_image_format, err);
return err;
}
Error GLTFDocumentExtension::export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_dict, Node *p_node) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_export_node, p_state, p_gltf_node, r_dict, p_node, err);
return err;
}
Error GLTFDocumentExtension::export_post(Ref<GLTFState> p_state) {
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
Error err = OK;
GDVIRTUAL_CALL(_export_post, p_state, err);
return err;
}

View File

@@ -0,0 +1,95 @@
/**************************************************************************/
/* gltf_document_extension.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "../gltf_state.h"
#include "scene/3d/node_3d.h"
class GLTFDocumentExtension : public Resource {
GDCLASS(GLTFDocumentExtension, Resource);
protected:
static void _bind_methods();
public:
// Import process.
virtual Error import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions);
virtual Vector<String> get_supported_extensions();
virtual Error parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions);
virtual Error parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image);
virtual String get_image_file_extension();
virtual Error parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture);
virtual Ref<GLTFObjectModelProperty> import_object_model_property(Ref<GLTFState> p_state, const PackedStringArray &p_split_json_pointer, const TypedArray<NodePath> &p_partial_paths);
virtual Error import_post_parse(Ref<GLTFState> p_state);
virtual Error import_pre_generate(Ref<GLTFState> p_state);
virtual Node3D *generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent);
virtual Error import_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_json, Node *p_node);
virtual Error import_post(Ref<GLTFState> p_state, Node *p_node);
// Export process.
virtual Error export_preflight(Ref<GLTFState> p_state, Node *p_root);
virtual void convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node);
virtual Error export_post_convert(Ref<GLTFState> p_state, Node *p_root);
virtual Error export_preserialize(Ref<GLTFState> p_state);
virtual Ref<GLTFObjectModelProperty> export_object_model_property(Ref<GLTFState> p_state, const NodePath &p_node_path, const Node *p_godot_node, GLTFNodeIndex p_gltf_node_index, const Object *p_target_object, int p_target_depth);
virtual Vector<String> get_saveable_image_formats();
virtual PackedByteArray serialize_image_to_bytes(Ref<GLTFState> p_state, Ref<Image> p_image, Dictionary p_image_dict, const String &p_image_format, float p_lossy_quality);
virtual Error save_image_at_path(Ref<GLTFState> p_state, Ref<Image> p_image, const String &p_file_path, const String &p_image_format, float p_lossy_quality);
virtual Error serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format);
virtual Error export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_json, Node *p_node);
virtual Error export_post(Ref<GLTFState> p_state);
// Import process.
GDVIRTUAL2R(Error, _import_preflight, Ref<GLTFState>, Vector<String>);
GDVIRTUAL0R(Vector<String>, _get_supported_extensions);
GDVIRTUAL3R(Error, _parse_node_extensions, Ref<GLTFState>, Ref<GLTFNode>, Dictionary);
GDVIRTUAL4R(Error, _parse_image_data, Ref<GLTFState>, PackedByteArray, String, Ref<Image>);
GDVIRTUAL0R(String, _get_image_file_extension);
GDVIRTUAL3R(Error, _parse_texture_json, Ref<GLTFState>, Dictionary, Ref<GLTFTexture>);
GDVIRTUAL3R(Ref<GLTFObjectModelProperty>, _import_object_model_property, Ref<GLTFState>, PackedStringArray, TypedArray<NodePath>);
GDVIRTUAL1R(Error, _import_post_parse, Ref<GLTFState>);
GDVIRTUAL1R(Error, _import_pre_generate, Ref<GLTFState>);
GDVIRTUAL3R(Node3D *, _generate_scene_node, Ref<GLTFState>, Ref<GLTFNode>, Node *);
GDVIRTUAL4R(Error, _import_node, Ref<GLTFState>, Ref<GLTFNode>, Dictionary, Node *);
GDVIRTUAL2R(Error, _import_post, Ref<GLTFState>, Node *);
// Export process.
GDVIRTUAL2R(Error, _export_preflight, Ref<GLTFState>, Node *);
GDVIRTUAL3(_convert_scene_node, Ref<GLTFState>, Ref<GLTFNode>, Node *);
GDVIRTUAL2R(Error, _export_post_convert, Ref<GLTFState>, Node *);
GDVIRTUAL1R(Error, _export_preserialize, Ref<GLTFState>);
GDVIRTUAL6R(Ref<GLTFObjectModelProperty>, _export_object_model_property, Ref<GLTFState>, NodePath, const Node *, GLTFNodeIndex, const Object *, int);
GDVIRTUAL0R(Vector<String>, _get_saveable_image_formats);
GDVIRTUAL5R(PackedByteArray, _serialize_image_to_bytes, Ref<GLTFState>, Ref<Image>, Dictionary, String, float);
GDVIRTUAL5R(Error, _save_image_at_path, Ref<GLTFState>, Ref<Image>, String, String, float);
GDVIRTUAL4R(Error, _serialize_texture_json, Ref<GLTFState>, Dictionary, Ref<GLTFTexture>, String);
GDVIRTUAL4R(Error, _export_node, Ref<GLTFState>, Ref<GLTFNode>, Dictionary, Node *);
GDVIRTUAL1R(Error, _export_post, Ref<GLTFState>);
};

View File

@@ -0,0 +1,96 @@
/**************************************************************************/
/* gltf_document_extension_convert_importer_mesh.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_document_extension_convert_importer_mesh.h"
#include "scene/3d/importer_mesh_instance_3d.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/resources/3d/importer_mesh.h"
void GLTFDocumentExtensionConvertImporterMesh::_copy_meta(Object *p_src_object, Object *p_dst_object) {
List<StringName> meta_list;
p_src_object->get_meta_list(&meta_list);
for (const StringName &meta_key : meta_list) {
Variant meta_value = p_src_object->get_meta(meta_key);
p_dst_object->set_meta(meta_key, meta_value);
}
}
MeshInstance3D *GLTFDocumentExtensionConvertImporterMesh::convert_importer_mesh_instance_3d(ImporterMeshInstance3D *p_importer_mesh_instance_3d) {
// Convert the node itself first.
MeshInstance3D *mesh_instance_node_3d = memnew(MeshInstance3D);
ERR_FAIL_NULL_V(p_importer_mesh_instance_3d, mesh_instance_node_3d);
mesh_instance_node_3d->set_name(p_importer_mesh_instance_3d->get_name());
mesh_instance_node_3d->set_transform(p_importer_mesh_instance_3d->get_transform());
mesh_instance_node_3d->set_skin(p_importer_mesh_instance_3d->get_skin());
mesh_instance_node_3d->set_skeleton_path(p_importer_mesh_instance_3d->get_skeleton_path());
mesh_instance_node_3d->set_visible(p_importer_mesh_instance_3d->is_visible());
p_importer_mesh_instance_3d->replace_by(mesh_instance_node_3d);
_copy_meta(p_importer_mesh_instance_3d, mesh_instance_node_3d);
// Convert the mesh data in the mesh resource.
Ref<ImporterMesh> importer_mesh = p_importer_mesh_instance_3d->get_mesh();
if (importer_mesh.is_valid()) {
Ref<ArrayMesh> array_mesh = importer_mesh->get_mesh();
mesh_instance_node_3d->set_mesh(array_mesh);
_copy_meta(importer_mesh.ptr(), array_mesh.ptr());
} else {
WARN_PRINT("glTF: ImporterMeshInstance3D does not have a valid mesh. This should not happen. Continuing anyway.");
}
return mesh_instance_node_3d;
}
Error GLTFDocumentExtensionConvertImporterMesh::import_post(Ref<GLTFState> p_state, Node *p_root) {
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
List<Node *> queue;
queue.push_back(p_root);
List<Node *> delete_queue;
while (!queue.is_empty()) {
List<Node *>::Element *E = queue.front();
Node *node = E->get();
ImporterMeshInstance3D *importer_mesh_3d = Object::cast_to<ImporterMeshInstance3D>(node);
if (importer_mesh_3d) {
delete_queue.push_back(importer_mesh_3d);
node = convert_importer_mesh_instance_3d(importer_mesh_3d);
}
int child_count = node->get_child_count();
for (int i = 0; i < child_count; i++) {
queue.push_back(node->get_child(i));
}
queue.pop_front();
}
while (!delete_queue.is_empty()) {
List<Node *>::Element *E = delete_queue.front();
Node *node = E->get();
memdelete(node);
delete_queue.pop_front();
}
return OK;
}

View File

@@ -0,0 +1,46 @@
/**************************************************************************/
/* gltf_document_extension_convert_importer_mesh.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "gltf_document_extension.h"
class MeshInstance3D;
class GLTFDocumentExtensionConvertImporterMesh : public GLTFDocumentExtension {
GDCLASS(GLTFDocumentExtensionConvertImporterMesh, GLTFDocumentExtension);
protected:
static void _copy_meta(Object *p_src_object, Object *p_dst_object);
public:
static MeshInstance3D *convert_importer_mesh_instance_3d(ImporterMeshInstance3D *p_importer_mesh_instance_3d);
Error import_post(Ref<GLTFState> p_state, Node *p_root) override;
};

View File

@@ -0,0 +1,66 @@
/**************************************************************************/
/* gltf_document_extension_texture_ktx.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_document_extension_texture_ktx.h"
// Import process.
Error GLTFDocumentExtensionTextureKTX::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
if (!p_extensions.has("KHR_texture_basisu")) {
return ERR_SKIP;
}
return OK;
}
Vector<String> GLTFDocumentExtensionTextureKTX::get_supported_extensions() {
Vector<String> ret;
ret.push_back("KHR_texture_basisu");
return ret;
}
Error GLTFDocumentExtensionTextureKTX::parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) {
if (p_mime_type == "image/ktx2") {
return r_image->load_ktx_from_buffer(p_image_data);
}
return OK;
}
Error GLTFDocumentExtensionTextureKTX::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) {
if (!p_texture_json.has("extensions")) {
return OK;
}
const Dictionary &extensions = p_texture_json["extensions"];
if (!extensions.has("KHR_texture_basisu")) {
return OK;
}
const Dictionary &texture_ktx = extensions["KHR_texture_basisu"];
ERR_FAIL_COND_V(!texture_ktx.has("source"), ERR_PARSE_ERROR);
r_gltf_texture->set_src_image(texture_ktx["source"]);
return OK;
}

View File

@@ -0,0 +1,44 @@
/**************************************************************************/
/* gltf_document_extension_texture_ktx.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "gltf_document_extension.h"
class GLTFDocumentExtensionTextureKTX : public GLTFDocumentExtension {
GDCLASS(GLTFDocumentExtensionTextureKTX, GLTFDocumentExtension);
public:
// Import process.
Error import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) override;
Vector<String> get_supported_extensions() override;
Error parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) override;
Error parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) override;
};

View File

@@ -0,0 +1,109 @@
/**************************************************************************/
/* gltf_document_extension_texture_webp.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_document_extension_texture_webp.h"
// Import process.
Error GLTFDocumentExtensionTextureWebP::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
if (!p_extensions.has("EXT_texture_webp")) {
return ERR_SKIP;
}
return OK;
}
Vector<String> GLTFDocumentExtensionTextureWebP::get_supported_extensions() {
Vector<String> ret;
ret.push_back("EXT_texture_webp");
return ret;
}
Error GLTFDocumentExtensionTextureWebP::parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) {
if (p_mime_type == "image/webp") {
return r_image->load_webp_from_buffer(p_image_data);
}
return OK;
}
String GLTFDocumentExtensionTextureWebP::get_image_file_extension() {
return ".webp";
}
Error GLTFDocumentExtensionTextureWebP::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) {
if (!p_texture_json.has("extensions")) {
return OK;
}
const Dictionary &extensions = p_texture_json["extensions"];
if (!extensions.has("EXT_texture_webp")) {
return OK;
}
const Dictionary &texture_webp = extensions["EXT_texture_webp"];
ERR_FAIL_COND_V(!texture_webp.has("source"), ERR_PARSE_ERROR);
r_gltf_texture->set_src_image(texture_webp["source"]);
return OK;
}
Vector<String> GLTFDocumentExtensionTextureWebP::get_saveable_image_formats() {
Vector<String> ret;
ret.push_back("Lossless WebP");
ret.push_back("Lossy WebP");
return ret;
}
PackedByteArray GLTFDocumentExtensionTextureWebP::serialize_image_to_bytes(Ref<GLTFState> p_state, Ref<Image> p_image, Dictionary p_image_dict, const String &p_image_format, float p_lossy_quality) {
if (p_image_format == "Lossless WebP") {
p_image_dict["mimeType"] = "image/webp";
return p_image->save_webp_to_buffer(false);
} else if (p_image_format == "Lossy WebP") {
p_image_dict["mimeType"] = "image/webp";
return p_image->save_webp_to_buffer(true, p_lossy_quality);
}
ERR_FAIL_V(PackedByteArray());
}
Error GLTFDocumentExtensionTextureWebP::save_image_at_path(Ref<GLTFState> p_state, Ref<Image> p_image, const String &p_file_path, const String &p_image_format, float p_lossy_quality) {
if (p_image_format == "Lossless WebP") {
p_image->save_webp(p_file_path, false);
return OK;
} else if (p_image_format == "Lossy WebP") {
p_image->save_webp(p_file_path, true, p_lossy_quality);
return OK;
}
return ERR_INVALID_PARAMETER;
}
Error GLTFDocumentExtensionTextureWebP::serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format) {
Dictionary ext_texture_webp;
ext_texture_webp["source"] = p_gltf_texture->get_src_image();
Dictionary texture_extensions;
texture_extensions["EXT_texture_webp"] = ext_texture_webp;
p_texture_json["extensions"] = texture_extensions;
p_state->add_used_extension("EXT_texture_webp", true);
return OK;
}

View File

@@ -0,0 +1,50 @@
/**************************************************************************/
/* gltf_document_extension_texture_webp.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "gltf_document_extension.h"
class GLTFDocumentExtensionTextureWebP : public GLTFDocumentExtension {
GDCLASS(GLTFDocumentExtensionTextureWebP, GLTFDocumentExtension);
public:
// Import process.
Error import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) override;
Vector<String> get_supported_extensions() override;
Error parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) override;
String get_image_file_extension() override;
Error parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) override;
// Export process.
Vector<String> get_saveable_image_formats() override;
PackedByteArray serialize_image_to_bytes(Ref<GLTFState> p_state, Ref<Image> p_image, Dictionary p_image_dict, const String &p_image_format, float p_lossy_quality) override;
Error save_image_at_path(Ref<GLTFState> p_state, Ref<Image> p_image, const String &p_full_path, const String &p_image_format, float p_lossy_quality) override;
Error serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format) override;
};

View File

@@ -0,0 +1,255 @@
/**************************************************************************/
/* gltf_light.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_light.h"
#include "../structures/gltf_object_model_property.h"
#include "scene/3d/light_3d.h"
void GLTFLight::_bind_methods() {
ClassDB::bind_static_method("GLTFLight", D_METHOD("from_node", "light_node"), &GLTFLight::from_node);
ClassDB::bind_method(D_METHOD("to_node"), &GLTFLight::to_node);
ClassDB::bind_static_method("GLTFLight", D_METHOD("from_dictionary", "dictionary"), &GLTFLight::from_dictionary);
ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFLight::to_dictionary);
ClassDB::bind_method(D_METHOD("get_color"), &GLTFLight::get_color);
ClassDB::bind_method(D_METHOD("set_color", "color"), &GLTFLight::set_color);
ClassDB::bind_method(D_METHOD("get_intensity"), &GLTFLight::get_intensity);
ClassDB::bind_method(D_METHOD("set_intensity", "intensity"), &GLTFLight::set_intensity);
ClassDB::bind_method(D_METHOD("get_light_type"), &GLTFLight::get_light_type);
ClassDB::bind_method(D_METHOD("set_light_type", "light_type"), &GLTFLight::set_light_type);
ClassDB::bind_method(D_METHOD("get_range"), &GLTFLight::get_range);
ClassDB::bind_method(D_METHOD("set_range", "range"), &GLTFLight::set_range);
ClassDB::bind_method(D_METHOD("get_inner_cone_angle"), &GLTFLight::get_inner_cone_angle);
ClassDB::bind_method(D_METHOD("set_inner_cone_angle", "inner_cone_angle"), &GLTFLight::set_inner_cone_angle);
ClassDB::bind_method(D_METHOD("get_outer_cone_angle"), &GLTFLight::get_outer_cone_angle);
ClassDB::bind_method(D_METHOD("set_outer_cone_angle", "outer_cone_angle"), &GLTFLight::set_outer_cone_angle);
ClassDB::bind_method(D_METHOD("get_additional_data", "extension_name"), &GLTFLight::get_additional_data);
ClassDB::bind_method(D_METHOD("set_additional_data", "extension_name", "additional_data"), &GLTFLight::set_additional_data);
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); // Color
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "intensity"), "set_intensity", "get_intensity"); // float
ADD_PROPERTY(PropertyInfo(Variant::STRING, "light_type"), "set_light_type", "get_light_type"); // String
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "range"), "set_range", "get_range"); // float
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inner_cone_angle"), "set_inner_cone_angle", "get_inner_cone_angle"); // float
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "outer_cone_angle"), "set_outer_cone_angle", "get_outer_cone_angle"); // float
}
void GLTFLight::set_cone_inner_attenuation_conversion_expressions(Ref<GLTFObjectModelProperty> &r_obj_model_prop) {
// Expression to convert glTF innerConeAngle to Godot spot_angle_attenuation.
Ref<Expression> gltf_to_godot_expr;
gltf_to_godot_expr.instantiate();
PackedStringArray gltf_to_godot_args = { "inner_cone_angle" };
gltf_to_godot_expr->parse("0.2 / (1.0 - inner_cone_angle / spot_angle) - 0.1", gltf_to_godot_args);
r_obj_model_prop->set_gltf_to_godot_expression(gltf_to_godot_expr);
// Expression to convert Godot spot_angle_attenuation to glTF innerConeAngle.
Ref<Expression> godot_to_gltf_expr;
godot_to_gltf_expr.instantiate();
PackedStringArray godot_to_gltf_args = { "godot_spot_angle_att" };
godot_to_gltf_expr->parse("spot_angle * maxf(0.0, 1.0 - (0.2 / (0.1 + godot_spot_angle_att)))", godot_to_gltf_args);
r_obj_model_prop->set_godot_to_gltf_expression(godot_to_gltf_expr);
}
Color GLTFLight::get_color() {
return color;
}
void GLTFLight::set_color(Color p_color) {
color = p_color;
}
float GLTFLight::get_intensity() {
return intensity;
}
void GLTFLight::set_intensity(float p_intensity) {
intensity = p_intensity;
}
String GLTFLight::get_light_type() {
return light_type;
}
void GLTFLight::set_light_type(String p_light_type) {
light_type = p_light_type;
}
float GLTFLight::get_range() {
return range;
}
void GLTFLight::set_range(float p_range) {
range = p_range;
}
float GLTFLight::get_inner_cone_angle() {
return inner_cone_angle;
}
void GLTFLight::set_inner_cone_angle(float p_inner_cone_angle) {
inner_cone_angle = p_inner_cone_angle;
}
float GLTFLight::get_outer_cone_angle() {
return outer_cone_angle;
}
void GLTFLight::set_outer_cone_angle(float p_outer_cone_angle) {
outer_cone_angle = p_outer_cone_angle;
}
Ref<GLTFLight> GLTFLight::from_node(const Light3D *p_light) {
Ref<GLTFLight> l;
l.instantiate();
ERR_FAIL_NULL_V_MSG(p_light, l, "Tried to create a GLTFLight from a Light3D node, but the given node was null.");
l->color = p_light->get_color().srgb_to_linear();
if (cast_to<DirectionalLight3D>(p_light)) {
l->light_type = "directional";
const DirectionalLight3D *light = cast_to<const DirectionalLight3D>(p_light);
l->intensity = light->get_param(DirectionalLight3D::PARAM_ENERGY);
l->range = FLT_MAX; // Range for directional lights is infinite in Godot.
} else if (cast_to<const OmniLight3D>(p_light)) {
l->light_type = "point";
const OmniLight3D *light = cast_to<const OmniLight3D>(p_light);
l->range = light->get_param(OmniLight3D::PARAM_RANGE);
l->intensity = light->get_param(OmniLight3D::PARAM_ENERGY);
} else if (cast_to<const SpotLight3D>(p_light)) {
l->light_type = "spot";
const SpotLight3D *light = cast_to<const SpotLight3D>(p_light);
l->range = light->get_param(SpotLight3D::PARAM_RANGE);
l->intensity = light->get_param(SpotLight3D::PARAM_ENERGY);
l->outer_cone_angle = Math::deg_to_rad(light->get_param(SpotLight3D::PARAM_SPOT_ANGLE));
// This equation is the inverse of the import equation (which has a desmos link).
float angle_ratio = 1 - (0.2 / (0.1 + light->get_param(SpotLight3D::PARAM_SPOT_ATTENUATION)));
angle_ratio = MAX(0, angle_ratio);
l->inner_cone_angle = l->outer_cone_angle * angle_ratio;
}
return l;
}
Light3D *GLTFLight::to_node() const {
Light3D *light = nullptr;
if (light_type == "directional") {
DirectionalLight3D *dir_light = memnew(DirectionalLight3D);
dir_light->set_param(Light3D::PARAM_ENERGY, intensity);
light = dir_light;
} else if (light_type == "point") {
OmniLight3D *omni_light = memnew(OmniLight3D);
omni_light->set_param(OmniLight3D::PARAM_ENERGY, intensity);
omni_light->set_param(OmniLight3D::PARAM_RANGE, CLAMP(range, 0, 4096));
light = omni_light;
} else if (light_type == "spot") {
SpotLight3D *spot_light = memnew(SpotLight3D);
spot_light->set_param(SpotLight3D::PARAM_ENERGY, intensity);
spot_light->set_param(SpotLight3D::PARAM_RANGE, CLAMP(range, 0, 4096));
spot_light->set_param(SpotLight3D::PARAM_SPOT_ANGLE, Math::rad_to_deg(outer_cone_angle));
// Line of best fit derived from guessing, see https://www.desmos.com/calculator/biiflubp8b
// The points in desmos are not exact, except for (1, infinity).
float angle_ratio = inner_cone_angle / outer_cone_angle;
float angle_attenuation = 0.2 / (1 - angle_ratio) - 0.1;
spot_light->set_param(SpotLight3D::PARAM_SPOT_ATTENUATION, angle_attenuation);
light = spot_light;
} else {
ERR_PRINT("Failed to create a Light3D node from GLTFLight, unknown light type '" + light_type + "'.");
return nullptr;
}
light->set_color(color.linear_to_srgb());
light->set_param(Light3D::PARAM_ATTENUATION, 2.0);
return light;
}
Ref<GLTFLight> GLTFLight::from_dictionary(const Dictionary p_dictionary) {
ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFLight>(), "Failed to parse glTF light, missing required field 'type'.");
Ref<GLTFLight> light;
light.instantiate();
const String &type = p_dictionary["type"];
light->light_type = type;
if (p_dictionary.has("color")) {
const Array &arr = p_dictionary["color"];
if (arr.size() == 3) {
light->color = Color(arr[0], arr[1], arr[2]);
} else {
ERR_PRINT("Error parsing glTF light: The color must have exactly 3 numbers.");
}
}
if (p_dictionary.has("intensity")) {
light->intensity = p_dictionary["intensity"];
}
if (p_dictionary.has("range")) {
light->range = p_dictionary["range"];
}
if (type == "spot") {
const Dictionary &spot = p_dictionary["spot"];
light->inner_cone_angle = spot["innerConeAngle"];
light->outer_cone_angle = spot["outerConeAngle"];
if (light->inner_cone_angle >= light->outer_cone_angle) {
ERR_PRINT("Error parsing glTF light: The inner angle must be smaller than the outer angle.");
}
} else if (type != "point" && type != "directional") {
ERR_PRINT("Error parsing glTF light: Light type '" + type + "' is unknown.");
}
return light;
}
Dictionary GLTFLight::to_dictionary() const {
Dictionary d;
if (color != Color(1.0f, 1.0f, 1.0f)) {
Array color_array;
color_array.resize(3);
color_array[0] = color.r;
color_array[1] = color.g;
color_array[2] = color.b;
d["color"] = color_array;
}
if (intensity != 1.0f) {
d["intensity"] = intensity;
}
if (light_type != "directional" && range != Math::INF) {
d["range"] = range;
}
if (light_type == "spot") {
Dictionary spot_dict;
spot_dict["innerConeAngle"] = inner_cone_angle;
spot_dict["outerConeAngle"] = outer_cone_angle;
d["spot"] = spot_dict;
}
d["type"] = light_type;
return d;
}
Variant GLTFLight::get_additional_data(const StringName &p_extension_name) {
return additional_data[p_extension_name];
}
void GLTFLight::set_additional_data(const StringName &p_extension_name, Variant p_additional_data) {
additional_data[p_extension_name] = p_additional_data;
}

View File

@@ -0,0 +1,85 @@
/**************************************************************************/
/* gltf_light.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "core/io/resource.h"
class GLTFObjectModelProperty;
class Light3D;
// https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual
class GLTFLight : public Resource {
GDCLASS(GLTFLight, Resource)
friend class GLTFDocument;
protected:
static void _bind_methods();
private:
Color color = Color(1.0f, 1.0f, 1.0f);
float intensity = 1.0f;
String light_type;
float range = Math::INF;
float inner_cone_angle = 0.0f;
float outer_cone_angle = Math::TAU / 8.0f;
Dictionary additional_data;
public:
static void set_cone_inner_attenuation_conversion_expressions(Ref<GLTFObjectModelProperty> &r_obj_model_prop);
Color get_color();
void set_color(Color p_color);
float get_intensity();
void set_intensity(float p_intensity);
String get_light_type();
void set_light_type(String p_light_type);
float get_range();
void set_range(float p_range);
float get_inner_cone_angle();
void set_inner_cone_angle(float p_inner_cone_angle);
float get_outer_cone_angle();
void set_outer_cone_angle(float p_outer_cone_angle);
static Ref<GLTFLight> from_node(const Light3D *p_light);
Light3D *to_node() const;
static Ref<GLTFLight> from_dictionary(const Dictionary p_dictionary);
Dictionary to_dictionary() const;
Variant get_additional_data(const StringName &p_extension_name);
void set_additional_data(const StringName &p_extension_name, Variant p_additional_data);
};

View File

@@ -0,0 +1,92 @@
/**************************************************************************/
/* gltf_spec_gloss.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_spec_gloss.h"
#include "core/io/image.h"
void GLTFSpecGloss::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_diffuse_img"), &GLTFSpecGloss::get_diffuse_img);
ClassDB::bind_method(D_METHOD("set_diffuse_img", "diffuse_img"), &GLTFSpecGloss::set_diffuse_img);
ClassDB::bind_method(D_METHOD("get_diffuse_factor"), &GLTFSpecGloss::get_diffuse_factor);
ClassDB::bind_method(D_METHOD("set_diffuse_factor", "diffuse_factor"), &GLTFSpecGloss::set_diffuse_factor);
ClassDB::bind_method(D_METHOD("get_gloss_factor"), &GLTFSpecGloss::get_gloss_factor);
ClassDB::bind_method(D_METHOD("set_gloss_factor", "gloss_factor"), &GLTFSpecGloss::set_gloss_factor);
ClassDB::bind_method(D_METHOD("get_specular_factor"), &GLTFSpecGloss::get_specular_factor);
ClassDB::bind_method(D_METHOD("set_specular_factor", "specular_factor"), &GLTFSpecGloss::set_specular_factor);
ClassDB::bind_method(D_METHOD("get_spec_gloss_img"), &GLTFSpecGloss::get_spec_gloss_img);
ClassDB::bind_method(D_METHOD("set_spec_gloss_img", "spec_gloss_img"), &GLTFSpecGloss::set_spec_gloss_img);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "diffuse_img"), "set_diffuse_img", "get_diffuse_img"); // Ref<Image>
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "diffuse_factor"), "set_diffuse_factor", "get_diffuse_factor"); // Color
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gloss_factor"), "set_gloss_factor", "get_gloss_factor"); // float
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "specular_factor"), "set_specular_factor", "get_specular_factor"); // Color
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "spec_gloss_img"), "set_spec_gloss_img", "get_spec_gloss_img"); // Ref<Image>
}
Ref<Image> GLTFSpecGloss::get_diffuse_img() {
return diffuse_img;
}
void GLTFSpecGloss::set_diffuse_img(Ref<Image> p_diffuse_img) {
diffuse_img = p_diffuse_img;
}
Color GLTFSpecGloss::get_diffuse_factor() {
return diffuse_factor;
}
void GLTFSpecGloss::set_diffuse_factor(Color p_diffuse_factor) {
diffuse_factor = p_diffuse_factor;
}
float GLTFSpecGloss::get_gloss_factor() {
return gloss_factor;
}
void GLTFSpecGloss::set_gloss_factor(float p_gloss_factor) {
gloss_factor = p_gloss_factor;
}
Color GLTFSpecGloss::get_specular_factor() {
return specular_factor;
}
void GLTFSpecGloss::set_specular_factor(Color p_specular_factor) {
specular_factor = p_specular_factor;
}
Ref<Image> GLTFSpecGloss::get_spec_gloss_img() {
return spec_gloss_img;
}
void GLTFSpecGloss::set_spec_gloss_img(Ref<Image> p_spec_gloss_img) {
spec_gloss_img = p_spec_gloss_img;
}

View File

@@ -0,0 +1,71 @@
/**************************************************************************/
/* gltf_spec_gloss.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "core/io/resource.h"
class Image;
// KHR_materials_pbrSpecularGlossiness is an archived GLTF extension.
// This means that it is deprecated and not recommended for new files.
// However, it is still supported for loading old files.
// https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Archived/KHR_materials_pbrSpecularGlossiness
class GLTFSpecGloss : public Resource {
GDCLASS(GLTFSpecGloss, Resource);
friend class GLTFDocument;
private:
Ref<Image> diffuse_img = nullptr;
Color diffuse_factor = Color(1.0f, 1.0f, 1.0f);
float gloss_factor = 1.0f;
Color specular_factor = Color(1.0f, 1.0f, 1.0f);
Ref<Image> spec_gloss_img = nullptr;
protected:
static void _bind_methods();
public:
Ref<Image> get_diffuse_img();
void set_diffuse_img(Ref<Image> p_diffuse_img);
Color get_diffuse_factor();
void set_diffuse_factor(Color p_diffuse_factor);
float get_gloss_factor();
void set_gloss_factor(float p_gloss_factor);
Color get_specular_factor();
void set_specular_factor(Color p_specular_factor);
Ref<Image> get_spec_gloss_img();
void set_spec_gloss_img(Ref<Image> p_spec_gloss_img);
};

View File

@@ -0,0 +1,730 @@
/**************************************************************************/
/* gltf_document_extension_physics.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_document_extension_physics.h"
#include "scene/3d/physics/area_3d.h"
#include "scene/3d/physics/rigid_body_3d.h"
#include "scene/3d/physics/static_body_3d.h"
using GLTFShapeIndex = int64_t;
// Import process.
Error GLTFDocumentExtensionPhysics::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
if (!p_extensions.has("OMI_collider") && !p_extensions.has("OMI_physics_body") && !p_extensions.has("OMI_physics_shape")) {
return ERR_SKIP;
}
Dictionary state_json = p_state->get_json();
if (state_json.has("extensions")) {
Dictionary state_extensions = state_json["extensions"];
if (state_extensions.has("OMI_physics_shape")) {
Dictionary omi_physics_shape_ext = state_extensions["OMI_physics_shape"];
if (omi_physics_shape_ext.has("shapes")) {
Array state_shape_dicts = omi_physics_shape_ext["shapes"];
if (state_shape_dicts.size() > 0) {
Array state_shapes;
for (int i = 0; i < state_shape_dicts.size(); i++) {
state_shapes.push_back(GLTFPhysicsShape::from_dictionary(state_shape_dicts[i]));
}
p_state->set_additional_data(StringName("GLTFPhysicsShapes"), state_shapes);
}
}
#ifndef DISABLE_DEPRECATED
} else if (state_extensions.has("OMI_collider")) {
Dictionary omi_collider_ext = state_extensions["OMI_collider"];
if (omi_collider_ext.has("colliders")) {
Array state_collider_dicts = omi_collider_ext["colliders"];
if (state_collider_dicts.size() > 0) {
Array state_colliders;
for (int i = 0; i < state_collider_dicts.size(); i++) {
state_colliders.push_back(GLTFPhysicsShape::from_dictionary(state_collider_dicts[i]));
}
p_state->set_additional_data(StringName("GLTFPhysicsShapes"), state_colliders);
}
}
#endif // DISABLE_DEPRECATED
}
}
return OK;
}
Vector<String> GLTFDocumentExtensionPhysics::get_supported_extensions() {
Vector<String> ret;
ret.push_back("OMI_collider");
ret.push_back("OMI_physics_body");
ret.push_back("OMI_physics_shape");
return ret;
}
Error GLTFDocumentExtensionPhysics::parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) {
#ifndef DISABLE_DEPRECATED
if (p_extensions.has("OMI_collider")) {
Dictionary node_collider_ext = p_extensions["OMI_collider"];
if (node_collider_ext.has("collider")) {
// "collider" is the index of the collider in the state colliders array.
int node_collider_index = node_collider_ext["collider"];
Array state_colliders = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
ERR_FAIL_INDEX_V_MSG(node_collider_index, state_colliders.size(), Error::ERR_FILE_CORRUPT, "glTF Physics: On node " + p_gltf_node->get_name() + ", the collider index " + itos(node_collider_index) + " is not in the state colliders (size: " + itos(state_colliders.size()) + ").");
p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), state_colliders[node_collider_index]);
} else {
p_gltf_node->set_additional_data(StringName("GLTFPhysicsShape"), GLTFPhysicsShape::from_dictionary(node_collider_ext));
}
}
#endif // DISABLE_DEPRECATED
if (p_extensions.has("OMI_physics_body")) {
Dictionary physics_body_ext = p_extensions["OMI_physics_body"];
if (physics_body_ext.has("collider")) {
Dictionary node_collider = physics_body_ext["collider"];
// "shape" is the index of the shape in the state shapes array.
int node_shape_index = node_collider.get("shape", -1);
if (node_shape_index != -1) {
Array state_shapes = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
ERR_FAIL_INDEX_V_MSG(node_shape_index, state_shapes.size(), Error::ERR_FILE_CORRUPT, "glTF Physics: On node " + p_gltf_node->get_name() + ", the shape index " + itos(node_shape_index) + " is not in the state shapes (size: " + itos(state_shapes.size()) + ").");
p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShape"), state_shapes[node_shape_index]);
p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShapeIndex"), node_shape_index);
} else {
// If this node is a collider but does not have a collider
// shape, then it only serves to combine together shapes.
p_gltf_node->set_additional_data(StringName("GLTFPhysicsCompoundCollider"), true);
}
}
if (physics_body_ext.has("trigger")) {
Dictionary node_trigger = physics_body_ext["trigger"];
// "shape" is the index of the shape in the state shapes array.
int node_shape_index = node_trigger.get("shape", -1);
if (node_shape_index != -1) {
Array state_shapes = p_state->get_additional_data(StringName("GLTFPhysicsShapes"));
ERR_FAIL_INDEX_V_MSG(node_shape_index, state_shapes.size(), Error::ERR_FILE_CORRUPT, "glTF Physics: On node " + p_gltf_node->get_name() + ", the shape index " + itos(node_shape_index) + " is not in the state shapes (size: " + itos(state_shapes.size()) + ").");
p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShape"), state_shapes[node_shape_index]);
p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"), node_shape_index);
} else {
// If this node is a trigger but does not have a trigger shape,
// then it's a trigger body, what Godot calls an Area3D node.
Ref<GLTFPhysicsBody> trigger_body;
trigger_body.instantiate();
trigger_body->set_body_type("trigger");
p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), trigger_body);
}
// If this node defines explicit member shape nodes, save this information.
if (node_trigger.has("nodes")) {
Array compound_trigger_nodes = node_trigger["nodes"];
p_gltf_node->set_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"), compound_trigger_nodes);
}
}
if (physics_body_ext.has("motion") || physics_body_ext.has("type")) {
p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_dictionary(physics_body_ext));
}
}
return OK;
}
bool _will_gltf_shape_become_subnode(Ref<GLTFState> p_state, const Ref<GLTFNode> p_gltf_node, GLTFNodeIndex p_gltf_node_index) {
if (p_gltf_node->has_additional_data(StringName("GLTFPhysicsBody"))) {
return true;
}
const TypedArray<GLTFNode> state_gltf_nodes = p_state->get_nodes();
const GLTFNodeIndex parent_index = p_gltf_node->get_parent();
if (parent_index == -1 || parent_index >= state_gltf_nodes.size()) {
return true;
}
const Ref<GLTFNode> parent_gltf_node = state_gltf_nodes[parent_index];
const Variant parent_body_maybe = parent_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
if (parent_body_maybe.get_type() != Variant::NIL) {
Ref<GLTFPhysicsBody> parent_body = parent_body_maybe;
// If the parent matches the triggerness, then this node will be generated as a shape (CollisionShape3D).
// Otherwise, if there is a mismatch, a body will be generated for this node, and a subnode will also be generated for the shape.
if (parent_body->get_body_type() == "trigger") {
return p_gltf_node->has_additional_data(StringName("GLTFPhysicsColliderShape"));
} else {
return p_gltf_node->has_additional_data(StringName("GLTFPhysicsTriggerShape"));
}
}
if (parent_gltf_node->has_additional_data(StringName("GLTFPhysicsColliderShape"))) {
return false;
}
if (parent_gltf_node->has_additional_data(StringName("GLTFPhysicsTriggerShape"))) {
return false;
}
Variant compound_trigger_maybe = parent_gltf_node->has_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"));
if (compound_trigger_maybe.get_type() != Variant::NIL) {
Array compound_trigger_nodes = compound_trigger_maybe;
// Remember, JSON only has numbers, not integers, so must cast to double.
return !compound_trigger_nodes.has((double)p_gltf_node_index);
}
return true;
}
NodePath _get_scene_node_path_for_shape_index(Ref<GLTFState> p_state, const GLTFNodeIndex p_shape_index) {
TypedArray<GLTFNode> state_gltf_nodes = p_state->get_nodes();
for (GLTFNodeIndex node_index = 0; node_index < state_gltf_nodes.size(); node_index++) {
const Ref<GLTFNode> gltf_node = state_gltf_nodes[node_index];
ERR_CONTINUE(gltf_node.is_null());
// Check if this node has a shape index and if it matches the one we are looking for.
Variant shape_index_maybe = gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShapeIndex"));
if (shape_index_maybe.get_type() != Variant::INT) {
shape_index_maybe = gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"));
if (shape_index_maybe.get_type() != Variant::INT) {
continue;
}
}
const GLTFShapeIndex shape_index = shape_index_maybe;
if (shape_index != p_shape_index) {
continue;
}
NodePath node_path = gltf_node->get_scene_node_path(p_state);
// At this point, we have found a node with the shape index we were looking for.
if (_will_gltf_shape_become_subnode(p_state, gltf_node, node_index)) {
Vector<StringName> sname_path = node_path.get_names();
sname_path.append(gltf_node->get_name() + "Shape");
node_path = NodePath(sname_path, false);
}
return node_path;
}
return NodePath();
}
Ref<GLTFObjectModelProperty> GLTFDocumentExtensionPhysics::import_object_model_property(Ref<GLTFState> p_state, const PackedStringArray &p_split_json_pointer, const TypedArray<NodePath> &p_partial_paths) {
Ref<GLTFObjectModelProperty> ret;
if (p_split_json_pointer.size() != 6) {
// The only properties this class cares about are exactly 6 levels deep.
return ret;
}
ret.instantiate();
const String &prop_name = p_split_json_pointer[5];
if (p_split_json_pointer[0] == "extensions" && p_split_json_pointer[2] == "shapes") {
if (p_split_json_pointer[1] == "OMI_physics_shape" || p_split_json_pointer[1] == "KHR_collision_shapes") {
const GLTFNodeIndex shape_index = p_split_json_pointer[3].to_int();
NodePath node_path = _get_scene_node_path_for_shape_index(p_state, shape_index);
if (node_path.is_empty()) {
return ret;
}
String godot_prop_name = prop_name;
if (prop_name == "size") {
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
} else if (prop_name == "height" || prop_name == "radius") {
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
} else if (prop_name == "radiusBottom" || prop_name == "radiusTop") {
godot_prop_name = "radius";
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
} else {
// Not something we handle, return without appending a NodePath.
return ret;
}
// Example: `A/B/C/CollisionShape3D:shape:radius`.
Vector<StringName> subnames;
subnames.append("shape");
subnames.append(godot_prop_name);
node_path = NodePath(node_path.get_names(), subnames, false);
ret->append_node_path(node_path);
}
} else if (p_split_json_pointer[0] == "nodes" && p_split_json_pointer[2] == "extensions" && p_split_json_pointer[4] == "motion") {
if (p_split_json_pointer[3] == "OMI_physics_body" || p_split_json_pointer[3] == "KHR_physics_rigid_bodies") {
const GLTFNodeIndex node_index = p_split_json_pointer[1].to_int();
const TypedArray<GLTFNode> all_gltf_nodes = p_state->get_nodes();
ERR_FAIL_INDEX_V_MSG(node_index, all_gltf_nodes.size(), ret, "GLTF Physics: The node index " + itos(node_index) + " is not in the state nodes (size: " + itos(all_gltf_nodes.size()) + ").");
const Ref<GLTFNode> gltf_node = all_gltf_nodes[node_index];
NodePath node_path;
if (p_partial_paths.is_empty()) {
node_path = gltf_node->get_scene_node_path(p_state);
} else {
// The path is already computed for us, just grab it.
node_path = p_partial_paths[0];
}
if (prop_name == "mass") {
ret->append_path_to_property(node_path, "mass");
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
} else if (prop_name == "linearVelocity") {
ret->append_path_to_property(node_path, "linear_velocity");
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
} else if (prop_name == "angularVelocity") {
ret->append_path_to_property(node_path, "angular_velocity");
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
} else if (prop_name == "centerOfMass") {
ret->append_path_to_property(node_path, "center_of_mass");
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
} else if (prop_name == "inertiaDiagonal") {
ret->append_path_to_property(node_path, "inertia");
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
} else if (prop_name == "inertiaOrientation") {
WARN_PRINT("GLTF Physics: The 'inertiaOrientation' property is not supported by Godot.");
} else {
// Not something we handle, return without appending a NodePath.
return ret;
}
}
}
return ret;
}
void _setup_shape_mesh_resource_from_index_if_needed(Ref<GLTFState> p_state, Ref<GLTFPhysicsShape> p_gltf_shape) {
GLTFMeshIndex shape_mesh_index = p_gltf_shape->get_mesh_index();
if (shape_mesh_index == -1) {
return; // No mesh for this shape.
}
Ref<ImporterMesh> importer_mesh = p_gltf_shape->get_importer_mesh();
if (importer_mesh.is_valid()) {
return; // The mesh resource is already set up.
}
TypedArray<GLTFMesh> state_meshes = p_state->get_meshes();
ERR_FAIL_INDEX_MSG(shape_mesh_index, state_meshes.size(), "glTF Physics: When importing '" + p_state->get_scene_name() + "', the shape mesh index " + itos(shape_mesh_index) + " is not in the state meshes (size: " + itos(state_meshes.size()) + ").");
Ref<GLTFMesh> gltf_mesh = state_meshes[shape_mesh_index];
ERR_FAIL_COND(gltf_mesh.is_null());
importer_mesh = gltf_mesh->get_mesh();
ERR_FAIL_COND(importer_mesh.is_null());
p_gltf_shape->set_importer_mesh(importer_mesh);
}
#ifndef DISABLE_DEPRECATED
CollisionObject3D *_generate_shape_with_body(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Ref<GLTFPhysicsShape> p_physics_shape, Ref<GLTFPhysicsBody> p_physics_body) {
print_verbose("glTF: Creating shape with body for: " + p_gltf_node->get_name());
bool is_trigger = p_physics_shape->get_is_trigger();
// This method is used for the case where we must generate a parent body.
// This is can happen for multiple reasons. One possibility is that this
// glTF file is using OMI_collider but not OMI_physics_body, or at least
// this particular node is not using it. Another possibility is that the
// physics body information is set up on the same glTF node, not a parent.
CollisionObject3D *body;
if (p_physics_body.is_valid()) {
// This code is run when the physics body is on the same glTF node.
body = p_physics_body->to_node();
if (is_trigger && (p_physics_body->get_body_type() != "trigger")) {
// Edge case: If the body's trigger and the collider's trigger
// are in disagreement, we need to create another new body.
CollisionObject3D *child = _generate_shape_with_body(p_state, p_gltf_node, p_physics_shape, nullptr);
child->set_name(p_gltf_node->get_name() + (is_trigger ? String("Trigger") : String("Solid")));
body->add_child(child);
return body;
}
} else if (is_trigger) {
body = memnew(Area3D);
} else {
body = memnew(StaticBody3D);
}
CollisionShape3D *shape = p_physics_shape->to_node();
shape->set_name(p_gltf_node->get_name() + "Shape");
body->add_child(shape);
return body;
}
#endif // DISABLE_DEPRECATED
CollisionObject3D *_get_ancestor_collision_object(Node *p_scene_parent) {
// Note: Despite the name of the method, at the moment this only checks
// the direct parent. Only check more later if Godot adds support for it.
if (p_scene_parent) {
CollisionObject3D *co = Object::cast_to<CollisionObject3D>(p_scene_parent);
if (likely(co)) {
return co;
}
}
return nullptr;
}
Node3D *_generate_shape_node_and_body_if_needed(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Ref<GLTFPhysicsShape> p_physics_shape, CollisionObject3D *p_col_object, bool p_is_trigger) {
// If we need to generate a body node, do so.
CollisionObject3D *body_node = nullptr;
if (p_is_trigger || p_physics_shape->get_is_trigger()) {
// If the shape wants to be a trigger but it doesn't
// have an Area3D parent, we need to make one.
if (!Object::cast_to<Area3D>(p_col_object)) {
body_node = memnew(Area3D);
}
} else {
if (!Object::cast_to<PhysicsBody3D>(p_col_object)) {
body_node = memnew(StaticBody3D);
}
}
// Generate the shape node.
_setup_shape_mesh_resource_from_index_if_needed(p_state, p_physics_shape);
CollisionShape3D *shape_node = p_physics_shape->to_node(true);
if (body_node) {
shape_node->set_name(p_gltf_node->get_name() + "Shape");
body_node->add_child(shape_node);
return body_node;
}
return shape_node;
}
// Either add the child to the parent, or return the child if there is no parent.
Node3D *_add_physics_node_to_given_node(Node3D *p_current_node, Node3D *p_child, Ref<GLTFNode> p_gltf_node) {
if (!p_current_node) {
return p_child;
}
String suffix;
if (Object::cast_to<CollisionShape3D>(p_child)) {
suffix = "Shape";
} else if (Object::cast_to<Area3D>(p_child)) {
suffix = "Trigger";
} else {
suffix = "Collider";
}
p_child->set_name(p_gltf_node->get_name() + suffix);
p_current_node->add_child(p_child);
return p_current_node;
}
Array _get_ancestor_compound_trigger_nodes(Ref<GLTFState> p_state, TypedArray<GLTFNode> p_state_nodes, CollisionObject3D *p_ancestor_col_obj) {
GLTFNodeIndex ancestor_index = p_state->get_node_index(p_ancestor_col_obj);
ERR_FAIL_INDEX_V(ancestor_index, p_state_nodes.size(), Array());
Ref<GLTFNode> ancestor_gltf_node = p_state_nodes[ancestor_index];
Variant compound_trigger_nodes = ancestor_gltf_node->get_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"));
if (compound_trigger_nodes.is_array()) {
return compound_trigger_nodes;
}
Array ret;
ancestor_gltf_node->set_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"), ret);
return ret;
}
Node3D *GLTFDocumentExtensionPhysics::generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) {
Ref<GLTFPhysicsBody> gltf_physics_body = p_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
#ifndef DISABLE_DEPRECATED
// This deprecated code handles OMI_collider (which we internally name "GLTFPhysicsShape").
Ref<GLTFPhysicsShape> gltf_physics_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsShape"));
if (gltf_physics_shape.is_valid()) {
_setup_shape_mesh_resource_from_index_if_needed(p_state, gltf_physics_shape);
// If this glTF node specifies both a shape and a body, generate both.
if (gltf_physics_body.is_valid()) {
return _generate_shape_with_body(p_state, p_gltf_node, gltf_physics_shape, gltf_physics_body);
}
CollisionObject3D *ancestor_col_obj = _get_ancestor_collision_object(p_scene_parent);
if (gltf_physics_shape->get_is_trigger()) {
// If the shape wants to be a trigger and it already has a
// trigger parent, we only need to make the shape node.
if (Object::cast_to<Area3D>(ancestor_col_obj)) {
return gltf_physics_shape->to_node(true);
}
} else if (ancestor_col_obj != nullptr) {
// If the shape has a valid parent, only make the shape node.
return gltf_physics_shape->to_node(true);
}
// Otherwise, we need to create a new body.
return _generate_shape_with_body(p_state, p_gltf_node, gltf_physics_shape, nullptr);
}
#endif // DISABLE_DEPRECATED
Node3D *ret = nullptr;
CollisionObject3D *ancestor_col_obj = nullptr;
Ref<GLTFPhysicsShape> gltf_physics_collider_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
Ref<GLTFPhysicsShape> gltf_physics_trigger_shape = p_gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
if (gltf_physics_body.is_valid()) {
ancestor_col_obj = gltf_physics_body->to_node();
ret = ancestor_col_obj;
} else {
ancestor_col_obj = _get_ancestor_collision_object(p_scene_parent);
if (Object::cast_to<Area3D>(ancestor_col_obj) && gltf_physics_trigger_shape.is_valid()) {
// At this point, we found an ancestor Area3D node. But do we want to use it for this trigger shape?
TypedArray<GLTFNode> state_nodes = p_state->get_nodes();
GLTFNodeIndex self_index = state_nodes.find(p_gltf_node);
Array compound_trigger_nodes = _get_ancestor_compound_trigger_nodes(p_state, state_nodes, ancestor_col_obj);
// Check if the ancestor specifies compound trigger nodes, and if this node is in there.
// Remember that JSON does not have integers, only "number", aka double-precision floats.
if (compound_trigger_nodes.size() > 0 && !compound_trigger_nodes.has(double(self_index))) {
// If the compound trigger we found is not the intended user of
// this shape node, then we need to create a new Area3D node.
ancestor_col_obj = memnew(Area3D);
ret = ancestor_col_obj;
}
} else if (!Object::cast_to<PhysicsBody3D>(ancestor_col_obj)) {
if (p_gltf_node->get_additional_data(StringName("GLTFPhysicsCompoundCollider"))) {
// If the glTF file wants this node to group solid shapes together,
// and there is no parent body, we need to create a static body.
ancestor_col_obj = memnew(StaticBody3D);
ret = ancestor_col_obj;
}
}
}
// Add the shapes to the tree. When an ancestor body is present, use it.
// If an explicit body was specified, it has already been generated and
// set above. If there is no ancestor body, we will either generate an
// Area3D or StaticBody3D implicitly, so prefer an Area3D as the base
// node for best compatibility with signal connections to this node.
bool is_ancestor_col_obj_solid = Object::cast_to<PhysicsBody3D>(ancestor_col_obj);
if (is_ancestor_col_obj_solid && gltf_physics_collider_shape.is_valid()) {
Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_collider_shape, ancestor_col_obj, false);
ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
}
if (gltf_physics_trigger_shape.is_valid()) {
Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_trigger_shape, ancestor_col_obj, true);
ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
}
if (!is_ancestor_col_obj_solid && gltf_physics_collider_shape.is_valid()) {
Node3D *child = _generate_shape_node_and_body_if_needed(p_state, p_gltf_node, gltf_physics_collider_shape, ancestor_col_obj, false);
ret = _add_physics_node_to_given_node(ret, child, p_gltf_node);
}
return ret;
}
// Export process.
bool _are_all_faces_equal(const Vector<Face3> &p_a, const Vector<Face3> &p_b) {
if (p_a.size() != p_b.size()) {
return false;
}
for (int i = 0; i < p_a.size(); i++) {
const Vector3 *a_vertices = p_a[i].vertex;
const Vector3 *b_vertices = p_b[i].vertex;
for (int j = 0; j < 3; j++) {
if (!a_vertices[j].is_equal_approx(b_vertices[j])) {
return false;
}
}
}
return true;
}
GLTFMeshIndex _get_or_insert_mesh_in_state(Ref<GLTFState> p_state, Ref<ImporterMesh> p_mesh) {
ERR_FAIL_COND_V(p_mesh.is_null(), -1);
TypedArray<GLTFMesh> state_meshes = p_state->get_meshes();
Vector<Face3> mesh_faces = p_mesh->get_faces();
// De-duplication: If the state already has the mesh we need, use that one.
for (GLTFMeshIndex i = 0; i < state_meshes.size(); i++) {
Ref<GLTFMesh> state_gltf_mesh = state_meshes[i];
ERR_CONTINUE(state_gltf_mesh.is_null());
Ref<ImporterMesh> state_importer_mesh = state_gltf_mesh->get_mesh();
ERR_CONTINUE(state_importer_mesh.is_null());
if (state_importer_mesh == p_mesh) {
return i;
}
if (_are_all_faces_equal(state_importer_mesh->get_faces(), mesh_faces)) {
return i;
}
}
// After the loop, we have checked that the mesh is not equal to any of the
// meshes in the state. So we insert a new mesh into the state mesh array.
Ref<GLTFMesh> gltf_mesh;
gltf_mesh.instantiate();
gltf_mesh->set_mesh(p_mesh);
GLTFMeshIndex mesh_index = state_meshes.size();
state_meshes.push_back(gltf_mesh);
p_state->set_meshes(state_meshes);
return mesh_index;
}
void GLTFDocumentExtensionPhysics::convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) {
if (cast_to<CollisionShape3D>(p_scene_node)) {
CollisionShape3D *godot_shape = Object::cast_to<CollisionShape3D>(p_scene_node);
Ref<GLTFPhysicsShape> gltf_shape = GLTFPhysicsShape::from_node(godot_shape);
ERR_FAIL_COND_MSG(gltf_shape.is_null(), "glTF Physics: Could not convert CollisionShape3D to GLTFPhysicsShape. Does it have a valid Shape3D?");
{
Ref<ImporterMesh> importer_mesh = gltf_shape->get_importer_mesh();
if (importer_mesh.is_valid()) {
gltf_shape->set_mesh_index(_get_or_insert_mesh_in_state(p_state, importer_mesh));
}
}
CollisionObject3D *ancestor_col_obj = _get_ancestor_collision_object(p_scene_node->get_parent());
if (cast_to<Area3D>(ancestor_col_obj)) {
p_gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShape"), gltf_shape);
// Write explicit member shape nodes to the ancestor compound trigger node.
TypedArray<GLTFNode> state_nodes = p_state->get_nodes();
GLTFNodeIndex self_index = state_nodes.size(); // The current p_gltf_node will be inserted next.
Array compound_trigger_nodes = _get_ancestor_compound_trigger_nodes(p_state, p_state->get_nodes(), ancestor_col_obj);
compound_trigger_nodes.push_back(double(self_index));
} else {
p_gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShape"), gltf_shape);
}
} else if (cast_to<CollisionObject3D>(p_scene_node)) {
CollisionObject3D *godot_body = Object::cast_to<CollisionObject3D>(p_scene_node);
p_gltf_node->set_additional_data(StringName("GLTFPhysicsBody"), GLTFPhysicsBody::from_node(godot_body));
}
}
Array _get_or_create_state_shapes_in_state(Ref<GLTFState> p_state) {
Dictionary state_json = p_state->get_json();
Dictionary state_extensions;
if (state_json.has("extensions")) {
state_extensions = state_json["extensions"];
} else {
state_json["extensions"] = state_extensions;
}
Dictionary omi_physics_shape_ext;
if (state_extensions.has("OMI_physics_shape")) {
omi_physics_shape_ext = state_extensions["OMI_physics_shape"];
} else {
state_extensions["OMI_physics_shape"] = omi_physics_shape_ext;
p_state->add_used_extension("OMI_physics_shape");
}
Array state_shapes;
if (omi_physics_shape_ext.has("shapes")) {
state_shapes = omi_physics_shape_ext["shapes"];
} else {
omi_physics_shape_ext["shapes"] = state_shapes;
}
return state_shapes;
}
GLTFShapeIndex _export_node_shape(Ref<GLTFState> p_state, Ref<GLTFPhysicsShape> p_physics_shape) {
Array state_shapes = _get_or_create_state_shapes_in_state(p_state);
GLTFShapeIndex size = state_shapes.size();
Dictionary shape_property;
Dictionary shape_dict = p_physics_shape->to_dictionary();
for (GLTFShapeIndex i = 0; i < size; i++) {
Dictionary other = state_shapes[i];
if (other == shape_dict) {
// De-duplication: If we already have an identical shape,
// set the shape index to the existing one and return.
return i;
}
}
// If we don't have an identical shape, add it to the array.
state_shapes.push_back(shape_dict);
return size;
}
Error GLTFDocumentExtensionPhysics::export_preserialize(Ref<GLTFState> p_state) {
// Note: Need to do _export_node_shape before exporting animations, so export_node is too late.
TypedArray<GLTFNode> state_gltf_nodes = p_state->get_nodes();
for (Ref<GLTFNode> gltf_node : state_gltf_nodes) {
Ref<GLTFPhysicsShape> collider_shape = gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
if (collider_shape.is_valid()) {
GLTFShapeIndex collider_shape_index = _export_node_shape(p_state, collider_shape);
gltf_node->set_additional_data(StringName("GLTFPhysicsColliderShapeIndex"), collider_shape_index);
}
Ref<GLTFPhysicsShape> trigger_shape = gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
if (trigger_shape.is_valid()) {
GLTFShapeIndex trigger_shape_index = _export_node_shape(p_state, trigger_shape);
gltf_node->set_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"), trigger_shape_index);
}
}
return OK;
}
Ref<GLTFObjectModelProperty> GLTFDocumentExtensionPhysics::export_object_model_property(Ref<GLTFState> p_state, const NodePath &p_node_path, const Node *p_godot_node, GLTFNodeIndex p_gltf_node_index, const Object *p_target_object, int p_target_depth) {
Ref<GLTFObjectModelProperty> ret;
const Vector<StringName> &path_subnames = p_node_path.get_subnames();
if (path_subnames.is_empty()) {
return ret;
}
ret.instantiate();
const StringName &node_prop = path_subnames[0];
if (Object::cast_to<RigidBody3D>(p_target_object)) {
if (path_subnames.size() != 1) {
return ret;
}
// Example: `/nodes/0/extensions/OMI_physics_body/motion/mass`
PackedStringArray split_json_pointer;
split_json_pointer.append("nodes");
split_json_pointer.append(itos(p_gltf_node_index));
split_json_pointer.append("extensions");
split_json_pointer.append("OMI_physics_body");
split_json_pointer.append("motion");
if (node_prop == StringName("mass")) {
split_json_pointer.append("mass");
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
} else if (node_prop == StringName("linear_velocity")) {
split_json_pointer.append("linearVelocity");
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
} else if (node_prop == StringName("angular_velocity")) {
split_json_pointer.append("angularVelocity");
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
} else if (node_prop == StringName("center_of_mass")) {
split_json_pointer.append("centerOfMass");
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
} else if (node_prop == StringName("inertia")) {
split_json_pointer.append("inertiaDiagonal");
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
} else {
// Not something we handle, return without setting the JSON pointer.
return ret;
}
ret->set_json_pointers({ split_json_pointer });
} else if (Object::cast_to<CollisionShape3D>(p_godot_node)) {
if (path_subnames.size() != 2) {
return ret;
}
// Example: `/extensions/OMI_physics_shape/shapes/0/box/size`
PackedStringArray split_json_pointer;
split_json_pointer.append("extensions");
split_json_pointer.append("OMI_physics_shape");
split_json_pointer.append("shapes");
TypedArray<GLTFNode> state_gltf_nodes = p_state->get_nodes();
ERR_FAIL_INDEX_V(p_gltf_node_index, state_gltf_nodes.size(), ret);
Ref<GLTFNode> gltf_node = state_gltf_nodes[p_gltf_node_index];
Variant shape_index_maybe = gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShapeIndex"));
String shape_type;
if (shape_index_maybe.get_type() == Variant::INT) {
Ref<GLTFPhysicsShape> collider_shape = gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShape"));
shape_type = collider_shape->get_shape_type();
} else {
shape_index_maybe = gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"));
if (shape_index_maybe.get_type() == Variant::INT) {
Ref<GLTFPhysicsShape> trigger_shape = gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShape"));
shape_type = trigger_shape->get_shape_type();
}
}
ERR_FAIL_COND_V(shape_index_maybe.get_type() != Variant::INT, ret);
GLTFShapeIndex shape_index = shape_index_maybe;
split_json_pointer.append(itos(shape_index));
split_json_pointer.append(shape_type);
const StringName &shape_prop = path_subnames[1];
if (shape_prop == StringName("size")) {
split_json_pointer.append("size");
ret->set_types(Variant::VECTOR3, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT3);
} else if (shape_prop == StringName("radius")) {
split_json_pointer.append("radius");
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
} else if (shape_prop == StringName("height")) {
split_json_pointer.append("height");
ret->set_types(Variant::FLOAT, GLTFObjectModelProperty::GLTF_OBJECT_MODEL_TYPE_FLOAT);
} else {
// Not something we handle, return without setting the JSON pointer.
return ret;
}
ret->set_json_pointers({ split_json_pointer });
}
return ret;
}
Error GLTFDocumentExtensionPhysics::export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_node_json, Node *p_node) {
Dictionary physics_body_ext;
Ref<GLTFPhysicsBody> physics_body = p_gltf_node->get_additional_data(StringName("GLTFPhysicsBody"));
if (physics_body.is_valid()) {
physics_body_ext = physics_body->to_dictionary();
Variant compound_trigger_nodes = p_gltf_node->get_additional_data(StringName("GLTFPhysicsCompoundTriggerNodes"));
if (compound_trigger_nodes.is_array()) {
Dictionary trigger_property = physics_body_ext.get_or_add("trigger", {});
trigger_property["nodes"] = compound_trigger_nodes;
}
}
Variant collider_shape_index = p_gltf_node->get_additional_data(StringName("GLTFPhysicsColliderShapeIndex"));
if (collider_shape_index.get_type() == Variant::INT) {
Dictionary collider_dict;
collider_dict["shape"] = collider_shape_index;
physics_body_ext["collider"] = collider_dict;
}
Variant trigger_shape_index = p_gltf_node->get_additional_data(StringName("GLTFPhysicsTriggerShapeIndex"));
if (trigger_shape_index.get_type() == Variant::INT) {
Dictionary trigger_dict = physics_body_ext.get_or_add("trigger", {});
trigger_dict["shape"] = trigger_shape_index;
}
if (!physics_body_ext.is_empty()) {
Dictionary node_extensions = r_node_json["extensions"];
node_extensions["OMI_physics_body"] = physics_body_ext;
p_state->add_used_extension("OMI_physics_body");
}
return OK;
}

View File

@@ -0,0 +1,52 @@
/**************************************************************************/
/* gltf_document_extension_physics.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "../gltf_document_extension.h"
#include "gltf_physics_body.h"
#include "gltf_physics_shape.h"
class GLTFDocumentExtensionPhysics : public GLTFDocumentExtension {
GDCLASS(GLTFDocumentExtensionPhysics, GLTFDocumentExtension);
public:
// Import process.
Error import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) override;
Vector<String> get_supported_extensions() override;
Error parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) override;
Ref<GLTFObjectModelProperty> import_object_model_property(Ref<GLTFState> p_state, const PackedStringArray &p_split_json_pointer, const TypedArray<NodePath> &p_partial_paths) override;
Node3D *generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) override;
// Export process.
void convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) override;
Error export_preserialize(Ref<GLTFState> p_state) override;
Ref<GLTFObjectModelProperty> export_object_model_property(Ref<GLTFState> p_state, const NodePath &p_node_path, const Node *p_godot_node, GLTFNodeIndex p_gltf_node_index, const Object *p_target_object, int p_target_depth) override;
Error export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_node_json, Node *p_scene_node) override;
};

View File

@@ -0,0 +1,405 @@
/**************************************************************************/
/* gltf_physics_body.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_physics_body.h"
#include "scene/3d/physics/animatable_body_3d.h"
#include "scene/3d/physics/area_3d.h"
#include "scene/3d/physics/character_body_3d.h"
#include "scene/3d/physics/static_body_3d.h"
#include "scene/3d/physics/vehicle_body_3d.h"
void GLTFPhysicsBody::_bind_methods() {
ClassDB::bind_static_method("GLTFPhysicsBody", D_METHOD("from_node", "body_node"), &GLTFPhysicsBody::from_node);
ClassDB::bind_method(D_METHOD("to_node"), &GLTFPhysicsBody::to_node);
ClassDB::bind_static_method("GLTFPhysicsBody", D_METHOD("from_dictionary", "dictionary"), &GLTFPhysicsBody::from_dictionary);
ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFPhysicsBody::to_dictionary);
ClassDB::bind_method(D_METHOD("get_body_type"), &GLTFPhysicsBody::get_body_type);
ClassDB::bind_method(D_METHOD("set_body_type", "body_type"), &GLTFPhysicsBody::set_body_type);
ClassDB::bind_method(D_METHOD("get_mass"), &GLTFPhysicsBody::get_mass);
ClassDB::bind_method(D_METHOD("set_mass", "mass"), &GLTFPhysicsBody::set_mass);
ClassDB::bind_method(D_METHOD("get_linear_velocity"), &GLTFPhysicsBody::get_linear_velocity);
ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &GLTFPhysicsBody::set_linear_velocity);
ClassDB::bind_method(D_METHOD("get_angular_velocity"), &GLTFPhysicsBody::get_angular_velocity);
ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &GLTFPhysicsBody::set_angular_velocity);
ClassDB::bind_method(D_METHOD("get_center_of_mass"), &GLTFPhysicsBody::get_center_of_mass);
ClassDB::bind_method(D_METHOD("set_center_of_mass", "center_of_mass"), &GLTFPhysicsBody::set_center_of_mass);
ClassDB::bind_method(D_METHOD("get_inertia_diagonal"), &GLTFPhysicsBody::get_inertia_diagonal);
ClassDB::bind_method(D_METHOD("set_inertia_diagonal", "inertia_diagonal"), &GLTFPhysicsBody::set_inertia_diagonal);
ClassDB::bind_method(D_METHOD("get_inertia_orientation"), &GLTFPhysicsBody::get_inertia_orientation);
ClassDB::bind_method(D_METHOD("set_inertia_orientation", "inertia_orientation"), &GLTFPhysicsBody::set_inertia_orientation);
#ifndef DISABLE_DEPRECATED
ClassDB::bind_method(D_METHOD("get_inertia_tensor"), &GLTFPhysicsBody::get_inertia_tensor);
ClassDB::bind_method(D_METHOD("set_inertia_tensor", "inertia_tensor"), &GLTFPhysicsBody::set_inertia_tensor);
#endif // DISABLE_DEPRECATED
ADD_PROPERTY(PropertyInfo(Variant::STRING, "body_type"), "set_body_type", "get_body_type");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass"), "set_mass", "get_mass");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "center_of_mass"), "set_center_of_mass", "get_center_of_mass");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "inertia_diagonal"), "set_inertia_diagonal", "get_inertia_diagonal");
ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "inertia_orientation"), "set_inertia_orientation", "get_inertia_orientation");
#ifndef DISABLE_DEPRECATED
ADD_PROPERTY(PropertyInfo(Variant::BASIS, "inertia_tensor"), "set_inertia_tensor", "get_inertia_tensor");
#endif // DISABLE_DEPRECATED
}
String GLTFPhysicsBody::get_body_type() const {
switch (body_type) {
case PhysicsBodyType::STATIC:
return "static";
case PhysicsBodyType::ANIMATABLE:
return "animatable";
case PhysicsBodyType::CHARACTER:
return "character";
case PhysicsBodyType::RIGID:
return "rigid";
case PhysicsBodyType::VEHICLE:
return "vehicle";
case PhysicsBodyType::TRIGGER:
return "trigger";
}
// Unreachable, the switch cases handle all values the enum can take.
// Omitting this works on Clang but not GCC or MSVC. If reached, it's UB.
return "rigid";
}
void GLTFPhysicsBody::set_body_type(String p_body_type) {
if (p_body_type == "static") {
body_type = PhysicsBodyType::STATIC;
} else if (p_body_type == "animatable") {
body_type = PhysicsBodyType::ANIMATABLE;
} else if (p_body_type == "character") {
body_type = PhysicsBodyType::CHARACTER;
} else if (p_body_type == "rigid") {
body_type = PhysicsBodyType::RIGID;
} else if (p_body_type == "vehicle") {
body_type = PhysicsBodyType::VEHICLE;
} else if (p_body_type == "trigger") {
body_type = PhysicsBodyType::TRIGGER;
} else {
ERR_PRINT("Error setting glTF physics body type: The body type must be one of \"static\", \"animatable\", \"character\", \"rigid\", \"vehicle\", or \"trigger\".");
}
}
GLTFPhysicsBody::PhysicsBodyType GLTFPhysicsBody::get_physics_body_type() const {
return body_type;
}
void GLTFPhysicsBody::set_physics_body_type(PhysicsBodyType p_body_type) {
body_type = p_body_type;
}
real_t GLTFPhysicsBody::get_mass() const {
return mass;
}
void GLTFPhysicsBody::set_mass(real_t p_mass) {
mass = p_mass;
}
Vector3 GLTFPhysicsBody::get_linear_velocity() const {
return linear_velocity;
}
void GLTFPhysicsBody::set_linear_velocity(Vector3 p_linear_velocity) {
linear_velocity = p_linear_velocity;
}
Vector3 GLTFPhysicsBody::get_angular_velocity() const {
return angular_velocity;
}
void GLTFPhysicsBody::set_angular_velocity(Vector3 p_angular_velocity) {
angular_velocity = p_angular_velocity;
}
Vector3 GLTFPhysicsBody::get_center_of_mass() const {
return center_of_mass;
}
void GLTFPhysicsBody::set_center_of_mass(const Vector3 &p_center_of_mass) {
center_of_mass = p_center_of_mass;
}
Vector3 GLTFPhysicsBody::get_inertia_diagonal() const {
return inertia_diagonal;
}
void GLTFPhysicsBody::set_inertia_diagonal(const Vector3 &p_inertia_diagonal) {
inertia_diagonal = p_inertia_diagonal;
}
Quaternion GLTFPhysicsBody::get_inertia_orientation() const {
return inertia_orientation;
}
void GLTFPhysicsBody::set_inertia_orientation(const Quaternion &p_inertia_orientation) {
inertia_orientation = p_inertia_orientation;
}
#ifndef DISABLE_DEPRECATED
Basis GLTFPhysicsBody::get_inertia_tensor() const {
return Basis::from_scale(inertia_diagonal);
}
void GLTFPhysicsBody::set_inertia_tensor(Basis p_inertia_tensor) {
inertia_diagonal = p_inertia_tensor.get_main_diagonal();
}
#endif // DISABLE_DEPRECATED
Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_node(const CollisionObject3D *p_body_node) {
Ref<GLTFPhysicsBody> physics_body;
physics_body.instantiate();
ERR_FAIL_NULL_V_MSG(p_body_node, physics_body, "Tried to create a GLTFPhysicsBody from a CollisionObject3D node, but the given node was null.");
if (cast_to<CharacterBody3D>(p_body_node)) {
physics_body->body_type = PhysicsBodyType::CHARACTER;
} else if (cast_to<AnimatableBody3D>(p_body_node)) {
physics_body->body_type = PhysicsBodyType::ANIMATABLE;
} else if (cast_to<RigidBody3D>(p_body_node)) {
const RigidBody3D *body = cast_to<const RigidBody3D>(p_body_node);
physics_body->mass = body->get_mass();
physics_body->linear_velocity = body->get_linear_velocity();
physics_body->angular_velocity = body->get_angular_velocity();
physics_body->center_of_mass = body->get_center_of_mass();
physics_body->inertia_diagonal = body->get_inertia();
if (cast_to<VehicleBody3D>(p_body_node)) {
physics_body->body_type = PhysicsBodyType::VEHICLE;
} else {
physics_body->body_type = PhysicsBodyType::RIGID;
}
} else if (cast_to<StaticBody3D>(p_body_node)) {
physics_body->body_type = PhysicsBodyType::STATIC;
} else if (cast_to<Area3D>(p_body_node)) {
physics_body->body_type = PhysicsBodyType::TRIGGER;
}
return physics_body;
}
CollisionObject3D *GLTFPhysicsBody::to_node() const {
switch (body_type) {
case PhysicsBodyType::CHARACTER: {
CharacterBody3D *body = memnew(CharacterBody3D);
return body;
}
case PhysicsBodyType::ANIMATABLE: {
AnimatableBody3D *body = memnew(AnimatableBody3D);
return body;
}
case PhysicsBodyType::VEHICLE: {
VehicleBody3D *body = memnew(VehicleBody3D);
body->set_mass(mass);
body->set_linear_velocity(linear_velocity);
body->set_angular_velocity(angular_velocity);
body->set_inertia(inertia_diagonal);
body->set_center_of_mass_mode(RigidBody3D::CENTER_OF_MASS_MODE_CUSTOM);
body->set_center_of_mass(center_of_mass);
return body;
}
case PhysicsBodyType::RIGID: {
RigidBody3D *body = memnew(RigidBody3D);
body->set_mass(mass);
body->set_linear_velocity(linear_velocity);
body->set_angular_velocity(angular_velocity);
body->set_inertia(inertia_diagonal);
body->set_center_of_mass_mode(RigidBody3D::CENTER_OF_MASS_MODE_CUSTOM);
body->set_center_of_mass(center_of_mass);
return body;
}
case PhysicsBodyType::STATIC: {
StaticBody3D *body = memnew(StaticBody3D);
return body;
}
case PhysicsBodyType::TRIGGER: {
Area3D *body = memnew(Area3D);
return body;
}
}
// Unreachable, the switch cases handle all values the enum can take.
// Omitting this works on Clang but not GCC or MSVC. If reached, it's UB.
return nullptr;
}
Ref<GLTFPhysicsBody> GLTFPhysicsBody::from_dictionary(const Dictionary p_dictionary) {
Ref<GLTFPhysicsBody> physics_body;
physics_body.instantiate();
Dictionary motion;
if (p_dictionary.has("motion")) {
motion = p_dictionary["motion"];
#ifndef DISABLE_DEPRECATED
} else {
motion = p_dictionary;
#endif // DISABLE_DEPRECATED
}
if (motion.has("type")) {
// Read the body type. This representation sits between glTF's and Godot's physics nodes.
// While we may only read "static", "kinematic", or "dynamic" from a valid glTF file, we
// want to allow another extension to override this to another Godot node type mid-import.
// For example, a vehicle extension may want to override the body type to "vehicle"
// so Godot generates a VehicleBody3D node. Therefore we distinguish by importing
// "dynamic" as "rigid", and "kinematic" as "animatable", in the GLTFPhysicsBody code.
String body_type_string = motion["type"];
if (body_type_string == "static") {
physics_body->body_type = PhysicsBodyType::STATIC;
} else if (body_type_string == "kinematic") {
physics_body->body_type = PhysicsBodyType::ANIMATABLE;
} else if (body_type_string == "dynamic") {
physics_body->body_type = PhysicsBodyType::RIGID;
#ifndef DISABLE_DEPRECATED
} else if (body_type_string == "character") {
physics_body->body_type = PhysicsBodyType::CHARACTER;
} else if (body_type_string == "rigid") {
physics_body->body_type = PhysicsBodyType::RIGID;
} else if (body_type_string == "vehicle") {
physics_body->body_type = PhysicsBodyType::VEHICLE;
} else if (body_type_string == "trigger") {
physics_body->body_type = PhysicsBodyType::TRIGGER;
#endif // DISABLE_DEPRECATED
} else {
ERR_PRINT("Error parsing glTF physics body: The body type in the glTF file \"" + body_type_string + "\" was not recognized.");
}
}
if (motion.has("mass")) {
physics_body->mass = motion["mass"];
}
if (motion.has("linearVelocity")) {
const Array &arr = motion["linearVelocity"];
if (arr.size() == 3) {
physics_body->set_linear_velocity(Vector3(arr[0], arr[1], arr[2]));
} else {
ERR_PRINT("Error parsing glTF physics body: The linear velocity vector must have exactly 3 numbers.");
}
}
if (motion.has("angularVelocity")) {
const Array &arr = motion["angularVelocity"];
if (arr.size() == 3) {
physics_body->set_angular_velocity(Vector3(arr[0], arr[1], arr[2]));
} else {
ERR_PRINT("Error parsing glTF physics body: The angular velocity vector must have exactly 3 numbers.");
}
}
if (motion.has("centerOfMass")) {
const Array &arr = motion["centerOfMass"];
if (arr.size() == 3) {
physics_body->set_center_of_mass(Vector3(arr[0], arr[1], arr[2]));
} else {
ERR_PRINT("Error parsing glTF physics body: The center of mass vector must have exactly 3 numbers.");
}
}
if (motion.has("inertiaDiagonal")) {
const Array &arr = motion["inertiaDiagonal"];
if (arr.size() == 3) {
physics_body->set_inertia_diagonal(Vector3(arr[0], arr[1], arr[2]));
} else {
ERR_PRINT("Error parsing glTF physics body: The inertia diagonal vector must have exactly 3 numbers.");
}
}
if (motion.has("inertiaOrientation")) {
const Array &arr = motion["inertiaOrientation"];
if (arr.size() == 4) {
physics_body->set_inertia_orientation(Quaternion(arr[0], arr[1], arr[2], arr[3]));
} else {
ERR_PRINT("Error parsing glTF physics body: The inertia orientation quaternion must have exactly 4 numbers.");
}
}
return physics_body;
}
Dictionary GLTFPhysicsBody::to_dictionary() const {
Dictionary ret;
if (body_type == PhysicsBodyType::TRIGGER) {
// The equivalent of a Godot Area3D node in glTF is a node that
// defines that it is a trigger, but does not have a shape.
Dictionary trigger;
ret["trigger"] = trigger;
return ret;
}
// All non-trigger body types are defined using the motion property.
Dictionary motion;
// When stored in memory, the body type can correspond to a Godot
// node type. However, when exporting to glTF, we need to squash
// this down to one of "static", "kinematic", or "dynamic".
if (body_type == PhysicsBodyType::STATIC) {
motion["type"] = "static";
} else if (body_type == PhysicsBodyType::ANIMATABLE || body_type == PhysicsBodyType::CHARACTER) {
motion["type"] = "kinematic";
} else {
motion["type"] = "dynamic";
}
if (mass != 1.0) {
motion["mass"] = mass;
}
if (linear_velocity != Vector3()) {
Array velocity_array;
velocity_array.resize(3);
velocity_array[0] = linear_velocity.x;
velocity_array[1] = linear_velocity.y;
velocity_array[2] = linear_velocity.z;
motion["linearVelocity"] = velocity_array;
}
if (angular_velocity != Vector3()) {
Array velocity_array;
velocity_array.resize(3);
velocity_array[0] = angular_velocity.x;
velocity_array[1] = angular_velocity.y;
velocity_array[2] = angular_velocity.z;
motion["angularVelocity"] = velocity_array;
}
if (center_of_mass != Vector3()) {
Array center_of_mass_array;
center_of_mass_array.resize(3);
center_of_mass_array[0] = center_of_mass.x;
center_of_mass_array[1] = center_of_mass.y;
center_of_mass_array[2] = center_of_mass.z;
motion["centerOfMass"] = center_of_mass_array;
}
if (inertia_diagonal != Vector3()) {
Array inertia_array;
inertia_array.resize(3);
inertia_array[0] = inertia_diagonal[0];
inertia_array[1] = inertia_diagonal[1];
inertia_array[2] = inertia_diagonal[2];
motion["inertiaDiagonal"] = inertia_array;
}
if (inertia_orientation != Quaternion()) {
Array inertia_array;
inertia_array.resize(4);
inertia_array[0] = inertia_orientation[0];
inertia_array[1] = inertia_orientation[1];
inertia_array[2] = inertia_orientation[2];
inertia_array[3] = inertia_orientation[3];
motion["inertiaDiagonal"] = inertia_array;
}
ret["motion"] = motion;
return ret;
}

View File

@@ -0,0 +1,104 @@
/**************************************************************************/
/* gltf_physics_body.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "scene/3d/physics/physics_body_3d.h"
// GLTFPhysicsBody is an intermediary between Godot's physics body nodes
// and the OMI_physics_body extension.
// https://github.com/omigroup/gltf-extensions/tree/main/extensions/2.0/OMI_physics_body
class GLTFPhysicsBody : public Resource {
GDCLASS(GLTFPhysicsBody, Resource)
public:
// These values map to Godot's physics body types.
// When importing, the body type will be set to the closest match, and
// user code can change this to make Godot generate a different node type.
// When exporting, this will be squashed down to one of "static",
// "kinematic", or "dynamic" motion types, or the "trigger" property.
enum class PhysicsBodyType {
STATIC,
ANIMATABLE,
CHARACTER,
RIGID,
VEHICLE,
TRIGGER,
};
protected:
static void _bind_methods();
private:
PhysicsBodyType body_type = PhysicsBodyType::RIGID;
real_t mass = 1.0;
Vector3 linear_velocity;
Vector3 angular_velocity;
Vector3 center_of_mass;
Vector3 inertia_diagonal;
Quaternion inertia_orientation;
public:
String get_body_type() const;
void set_body_type(String p_body_type);
PhysicsBodyType get_physics_body_type() const;
void set_physics_body_type(PhysicsBodyType p_body_type);
real_t get_mass() const;
void set_mass(real_t p_mass);
Vector3 get_linear_velocity() const;
void set_linear_velocity(Vector3 p_linear_velocity);
Vector3 get_angular_velocity() const;
void set_angular_velocity(Vector3 p_angular_velocity);
Vector3 get_center_of_mass() const;
void set_center_of_mass(const Vector3 &p_center_of_mass);
Vector3 get_inertia_diagonal() const;
void set_inertia_diagonal(const Vector3 &p_inertia_diagonal);
Quaternion get_inertia_orientation() const;
void set_inertia_orientation(const Quaternion &p_inertia_orientation);
#ifndef DISABLE_DEPRECATED
Basis get_inertia_tensor() const;
void set_inertia_tensor(Basis p_inertia_tensor);
#endif // DISABLE_DEPRECATED
static Ref<GLTFPhysicsBody> from_node(const CollisionObject3D *p_body_node);
CollisionObject3D *to_node() const;
static Ref<GLTFPhysicsBody> from_dictionary(const Dictionary p_dictionary);
Dictionary to_dictionary() const;
};

View File

@@ -0,0 +1,338 @@
/**************************************************************************/
/* gltf_physics_shape.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_physics_shape.h"
#include "../../gltf_state.h"
#include "core/math/convex_hull.h"
#include "scene/3d/physics/area_3d.h"
#include "scene/resources/3d/box_shape_3d.h"
#include "scene/resources/3d/capsule_shape_3d.h"
#include "scene/resources/3d/concave_polygon_shape_3d.h"
#include "scene/resources/3d/convex_polygon_shape_3d.h"
#include "scene/resources/3d/cylinder_shape_3d.h"
#include "scene/resources/3d/importer_mesh.h"
#include "scene/resources/3d/sphere_shape_3d.h"
void GLTFPhysicsShape::_bind_methods() {
ClassDB::bind_static_method("GLTFPhysicsShape", D_METHOD("from_node", "shape_node"), &GLTFPhysicsShape::from_node);
ClassDB::bind_method(D_METHOD("to_node", "cache_shapes"), &GLTFPhysicsShape::to_node, DEFVAL(false));
ClassDB::bind_static_method("GLTFPhysicsShape", D_METHOD("from_resource", "shape_resource"), &GLTFPhysicsShape::from_resource);
ClassDB::bind_method(D_METHOD("to_resource", "cache_shapes"), &GLTFPhysicsShape::to_resource, DEFVAL(false));
ClassDB::bind_static_method("GLTFPhysicsShape", D_METHOD("from_dictionary", "dictionary"), &GLTFPhysicsShape::from_dictionary);
ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFPhysicsShape::to_dictionary);
ClassDB::bind_method(D_METHOD("get_shape_type"), &GLTFPhysicsShape::get_shape_type);
ClassDB::bind_method(D_METHOD("set_shape_type", "shape_type"), &GLTFPhysicsShape::set_shape_type);
ClassDB::bind_method(D_METHOD("get_size"), &GLTFPhysicsShape::get_size);
ClassDB::bind_method(D_METHOD("set_size", "size"), &GLTFPhysicsShape::set_size);
ClassDB::bind_method(D_METHOD("get_radius"), &GLTFPhysicsShape::get_radius);
ClassDB::bind_method(D_METHOD("set_radius", "radius"), &GLTFPhysicsShape::set_radius);
ClassDB::bind_method(D_METHOD("get_height"), &GLTFPhysicsShape::get_height);
ClassDB::bind_method(D_METHOD("set_height", "height"), &GLTFPhysicsShape::set_height);
ClassDB::bind_method(D_METHOD("get_is_trigger"), &GLTFPhysicsShape::get_is_trigger);
ClassDB::bind_method(D_METHOD("set_is_trigger", "is_trigger"), &GLTFPhysicsShape::set_is_trigger);
ClassDB::bind_method(D_METHOD("get_mesh_index"), &GLTFPhysicsShape::get_mesh_index);
ClassDB::bind_method(D_METHOD("set_mesh_index", "mesh_index"), &GLTFPhysicsShape::set_mesh_index);
ClassDB::bind_method(D_METHOD("get_importer_mesh"), &GLTFPhysicsShape::get_importer_mesh);
ClassDB::bind_method(D_METHOD("set_importer_mesh", "importer_mesh"), &GLTFPhysicsShape::set_importer_mesh);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "shape_type"), "set_shape_type", "get_shape_type");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size"), "set_size", "get_size");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius"), "set_radius", "get_radius");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height"), "set_height", "get_height");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_trigger"), "set_is_trigger", "get_is_trigger");
ADD_PROPERTY(PropertyInfo(Variant::INT, "mesh_index"), "set_mesh_index", "get_mesh_index");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "importer_mesh", PROPERTY_HINT_RESOURCE_TYPE, "ImporterMesh"), "set_importer_mesh", "get_importer_mesh");
}
String GLTFPhysicsShape::get_shape_type() const {
return shape_type;
}
void GLTFPhysicsShape::set_shape_type(String p_shape_type) {
shape_type = p_shape_type;
}
Vector3 GLTFPhysicsShape::get_size() const {
return size;
}
void GLTFPhysicsShape::set_size(Vector3 p_size) {
size = p_size;
}
real_t GLTFPhysicsShape::get_radius() const {
return radius;
}
void GLTFPhysicsShape::set_radius(real_t p_radius) {
radius = p_radius;
}
real_t GLTFPhysicsShape::get_height() const {
return height;
}
void GLTFPhysicsShape::set_height(real_t p_height) {
height = p_height;
}
bool GLTFPhysicsShape::get_is_trigger() const {
return is_trigger;
}
void GLTFPhysicsShape::set_is_trigger(bool p_is_trigger) {
is_trigger = p_is_trigger;
}
GLTFMeshIndex GLTFPhysicsShape::get_mesh_index() const {
return mesh_index;
}
void GLTFPhysicsShape::set_mesh_index(GLTFMeshIndex p_mesh_index) {
mesh_index = p_mesh_index;
}
Ref<ImporterMesh> GLTFPhysicsShape::get_importer_mesh() const {
return importer_mesh;
}
void GLTFPhysicsShape::set_importer_mesh(Ref<ImporterMesh> p_importer_mesh) {
importer_mesh = p_importer_mesh;
}
Ref<ImporterMesh> _convert_hull_points_to_mesh(const Vector<Vector3> &p_hull_points) {
Ref<ImporterMesh> importer_mesh;
ERR_FAIL_COND_V_MSG(p_hull_points.size() < 3, importer_mesh, "GLTFPhysicsShape: Convex hull has fewer points (" + itos(p_hull_points.size()) + ") than the minimum of 3. At least 3 points are required in order to save to glTF, since it uses a mesh to represent convex hulls.");
if (p_hull_points.size() > 255) {
WARN_PRINT("GLTFPhysicsShape: Convex hull has more points (" + itos(p_hull_points.size()) + ") than the recommended maximum of 255. This may not load correctly in other engines.");
}
// Convert the convex hull points into an array of faces.
Geometry3D::MeshData md;
Error err = ConvexHullComputer::convex_hull(p_hull_points, md);
ERR_FAIL_COND_V_MSG(err != OK, importer_mesh, "GLTFPhysicsShape: Failed to compute convex hull.");
Vector<Vector3> face_vertices;
for (uint32_t i = 0; i < md.faces.size(); i++) {
uint32_t index_count = md.faces[i].indices.size();
for (uint32_t j = 1; j < index_count - 1; j++) {
face_vertices.append(p_hull_points[md.faces[i].indices[0]]);
face_vertices.append(p_hull_points[md.faces[i].indices[j]]);
face_vertices.append(p_hull_points[md.faces[i].indices[j + 1]]);
}
}
// Create an ImporterMesh from the faces.
importer_mesh.instantiate();
Array surface_array;
surface_array.resize(Mesh::ArrayType::ARRAY_MAX);
surface_array[Mesh::ArrayType::ARRAY_VERTEX] = face_vertices;
importer_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, surface_array);
return importer_mesh;
}
Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_node(const CollisionShape3D *p_godot_shape_node) {
Ref<GLTFPhysicsShape> gltf_shape;
ERR_FAIL_NULL_V_MSG(p_godot_shape_node, gltf_shape, "Tried to create a GLTFPhysicsShape from a CollisionShape3D node, but the given node was null.");
Ref<Shape3D> shape_resource = p_godot_shape_node->get_shape();
ERR_FAIL_COND_V_MSG(shape_resource.is_null(), gltf_shape, "Tried to create a GLTFPhysicsShape from a CollisionShape3D node, but the given node had a null shape.");
gltf_shape = from_resource(shape_resource);
// Check if the shape is part of a trigger.
Node *parent = p_godot_shape_node->get_parent();
if (cast_to<const Area3D>(parent)) {
gltf_shape->set_is_trigger(true);
}
return gltf_shape;
}
CollisionShape3D *GLTFPhysicsShape::to_node(bool p_cache_shapes) {
CollisionShape3D *godot_shape_node = memnew(CollisionShape3D);
to_resource(p_cache_shapes); // Sets `_shape_cache`.
godot_shape_node->set_shape(_shape_cache);
return godot_shape_node;
}
Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_resource(const Ref<Shape3D> &p_shape_resource) {
Ref<GLTFPhysicsShape> gltf_shape;
gltf_shape.instantiate();
ERR_FAIL_COND_V_MSG(p_shape_resource.is_null(), gltf_shape, "Tried to create a GLTFPhysicsShape from a Shape3D resource, but the given resource was null.");
if (cast_to<BoxShape3D>(p_shape_resource.ptr())) {
gltf_shape->shape_type = "box";
Ref<BoxShape3D> box = p_shape_resource;
gltf_shape->set_size(box->get_size());
} else if (cast_to<const CapsuleShape3D>(p_shape_resource.ptr())) {
gltf_shape->shape_type = "capsule";
Ref<CapsuleShape3D> capsule = p_shape_resource;
gltf_shape->set_radius(capsule->get_radius());
gltf_shape->set_height(capsule->get_height());
} else if (cast_to<const CylinderShape3D>(p_shape_resource.ptr())) {
gltf_shape->shape_type = "cylinder";
Ref<CylinderShape3D> cylinder = p_shape_resource;
gltf_shape->set_radius(cylinder->get_radius());
gltf_shape->set_height(cylinder->get_height());
} else if (cast_to<const SphereShape3D>(p_shape_resource.ptr())) {
gltf_shape->shape_type = "sphere";
Ref<SphereShape3D> sphere = p_shape_resource;
gltf_shape->set_radius(sphere->get_radius());
} else if (cast_to<const ConvexPolygonShape3D>(p_shape_resource.ptr())) {
gltf_shape->shape_type = "convex";
Ref<ConvexPolygonShape3D> convex = p_shape_resource;
Vector<Vector3> hull_points = convex->get_points();
Ref<ImporterMesh> importer_mesh = _convert_hull_points_to_mesh(hull_points);
ERR_FAIL_COND_V_MSG(importer_mesh.is_null(), gltf_shape, "GLTFPhysicsShape: Failed to convert convex hull points to a mesh.");
gltf_shape->set_importer_mesh(importer_mesh);
} else if (cast_to<const ConcavePolygonShape3D>(p_shape_resource.ptr())) {
gltf_shape->shape_type = "trimesh";
Ref<ConcavePolygonShape3D> concave = p_shape_resource;
Ref<ImporterMesh> importer_mesh;
importer_mesh.instantiate();
Array surface_array;
surface_array.resize(Mesh::ArrayType::ARRAY_MAX);
surface_array[Mesh::ArrayType::ARRAY_VERTEX] = concave->get_faces();
importer_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, surface_array);
gltf_shape->set_importer_mesh(importer_mesh);
} else {
ERR_PRINT("Tried to create a GLTFPhysicsShape from a Shape3D, but the given shape '" + String(Variant(p_shape_resource)) +
"' had an unsupported shape type. Only BoxShape3D, CapsuleShape3D, CylinderShape3D, SphereShape3D, ConcavePolygonShape3D, and ConvexPolygonShape3D are supported.");
}
gltf_shape->_shape_cache = p_shape_resource;
return gltf_shape;
}
Ref<Shape3D> GLTFPhysicsShape::to_resource(bool p_cache_shapes) {
if (!p_cache_shapes || _shape_cache.is_null()) {
if (shape_type == "box") {
Ref<BoxShape3D> box;
box.instantiate();
box->set_size(size);
_shape_cache = box;
} else if (shape_type == "capsule") {
Ref<CapsuleShape3D> capsule;
capsule.instantiate();
capsule->set_radius(radius);
capsule->set_height(height);
_shape_cache = capsule;
} else if (shape_type == "cylinder") {
Ref<CylinderShape3D> cylinder;
cylinder.instantiate();
cylinder->set_radius(radius);
cylinder->set_height(height);
_shape_cache = cylinder;
} else if (shape_type == "sphere") {
Ref<SphereShape3D> sphere;
sphere.instantiate();
sphere->set_radius(radius);
_shape_cache = sphere;
} else if (shape_type == "convex") {
ERR_FAIL_COND_V_MSG(importer_mesh.is_null(), _shape_cache, "GLTFPhysicsShape: Error converting convex hull shape to a shape resource: The mesh resource is null.");
Ref<ConvexPolygonShape3D> convex = importer_mesh->get_mesh()->create_convex_shape();
_shape_cache = convex;
} else if (shape_type == "trimesh") {
ERR_FAIL_COND_V_MSG(importer_mesh.is_null(), _shape_cache, "GLTFPhysicsShape: Error converting concave mesh shape to a shape resource: The mesh resource is null.");
Ref<ConcavePolygonShape3D> concave = importer_mesh->create_trimesh_shape();
_shape_cache = concave;
} else {
ERR_PRINT("GLTFPhysicsShape: Error converting to a shape resource: Shape type '" + shape_type + "' is unknown.");
}
}
return _shape_cache;
}
Ref<GLTFPhysicsShape> GLTFPhysicsShape::from_dictionary(const Dictionary p_dictionary) {
ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFPhysicsShape>(), "Failed to parse GLTFPhysicsShape, missing required field 'type'.");
Ref<GLTFPhysicsShape> gltf_shape;
gltf_shape.instantiate();
String shape_type = p_dictionary["type"];
if (shape_type == "hull") {
shape_type = "convex";
}
gltf_shape->shape_type = shape_type;
if (shape_type != "box" && shape_type != "capsule" && shape_type != "cylinder" && shape_type != "sphere" && shape_type != "convex" && shape_type != "trimesh") {
ERR_PRINT("GLTFPhysicsShape: Error parsing unknown shape type '" + shape_type + "'. Only box, capsule, cylinder, sphere, convex, and trimesh are supported.");
}
Dictionary properties;
if (p_dictionary.has(shape_type)) {
properties = p_dictionary[shape_type];
} else {
properties = p_dictionary;
}
if (properties.has("radius")) {
gltf_shape->set_radius(properties["radius"]);
}
if (properties.has("height")) {
gltf_shape->set_height(properties["height"]);
}
if (properties.has("size")) {
const Array &arr = properties["size"];
if (arr.size() == 3) {
gltf_shape->set_size(Vector3(arr[0], arr[1], arr[2]));
} else {
ERR_PRINT("GLTFPhysicsShape: Error parsing the size, it must have exactly 3 numbers.");
}
}
if (properties.has("isTrigger")) {
gltf_shape->set_is_trigger(properties["isTrigger"]);
}
if (properties.has("mesh")) {
gltf_shape->set_mesh_index(properties["mesh"]);
}
if (unlikely(gltf_shape->get_mesh_index() < 0 && (shape_type == "convex" || shape_type == "trimesh"))) {
ERR_PRINT("Error parsing GLTFPhysicsShape: The mesh-based shape type '" + shape_type + "' does not have a valid mesh index.");
}
return gltf_shape;
}
Dictionary GLTFPhysicsShape::to_dictionary() const {
Dictionary gltf_shape;
gltf_shape["type"] = shape_type;
Dictionary sub;
if (shape_type == "box") {
Array size_array;
size_array.resize(3);
size_array[0] = size.x;
size_array[1] = size.y;
size_array[2] = size.z;
sub["size"] = size_array;
} else if (shape_type == "capsule") {
sub["radius"] = get_radius();
sub["height"] = get_height();
} else if (shape_type == "cylinder") {
sub["radius"] = get_radius();
sub["height"] = get_height();
} else if (shape_type == "sphere") {
sub["radius"] = get_radius();
} else if (shape_type == "trimesh" || shape_type == "convex") {
sub["mesh"] = get_mesh_index();
}
gltf_shape[shape_type] = sub;
return gltf_shape;
}

View File

@@ -0,0 +1,90 @@
/**************************************************************************/
/* gltf_physics_shape.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "../../gltf_defines.h"
#include "scene/3d/physics/collision_shape_3d.h"
class ImporterMesh;
// GLTFPhysicsShape is an intermediary between Godot's collision shape nodes
// and the OMI_physics_shape extension.
// https://github.com/omigroup/gltf-extensions/tree/main/extensions/2.0/OMI_physics_shape
class GLTFPhysicsShape : public Resource {
GDCLASS(GLTFPhysicsShape, Resource)
protected:
static void _bind_methods();
private:
String shape_type;
Vector3 size = Vector3(1.0, 1.0, 1.0);
real_t radius = 0.5;
real_t height = 2.0;
bool is_trigger = false;
GLTFMeshIndex mesh_index = -1;
Ref<ImporterMesh> importer_mesh = nullptr;
// Internal only, for caching Godot shape resources. Used in `to_resource` and `to_node`.
Ref<Shape3D> _shape_cache = nullptr;
public:
String get_shape_type() const;
void set_shape_type(String p_shape_type);
Vector3 get_size() const;
void set_size(Vector3 p_size);
real_t get_radius() const;
void set_radius(real_t p_radius);
real_t get_height() const;
void set_height(real_t p_height);
bool get_is_trigger() const;
void set_is_trigger(bool p_is_trigger);
GLTFMeshIndex get_mesh_index() const;
void set_mesh_index(GLTFMeshIndex p_mesh_index);
Ref<ImporterMesh> get_importer_mesh() const;
void set_importer_mesh(Ref<ImporterMesh> p_importer_mesh);
static Ref<GLTFPhysicsShape> from_node(const CollisionShape3D *p_shape_node);
CollisionShape3D *to_node(bool p_cache_shapes = false);
static Ref<GLTFPhysicsShape> from_resource(const Ref<Shape3D> &p_shape_resource);
Ref<Shape3D> to_resource(bool p_cache_shapes = false);
static Ref<GLTFPhysicsShape> from_dictionary(const Dictionary p_dictionary);
Dictionary to_dictionary() const;
};

View File

@@ -0,0 +1,67 @@
/**************************************************************************/
/* gltf_defines.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
// This file should only be included by other headers.
// GLTF classes.
struct GLTFAccessor;
class GLTFAnimation;
class GLTFBufferView;
class GLTFCamera;
class GLTFDocument;
class GLTFDocumentExtension;
class GLTFLight;
class GLTFMesh;
class GLTFNode;
class GLTFObjectModelProperty;
class GLTFSkeleton;
class GLTFSkin;
class GLTFSpecGloss;
class GLTFState;
class GLTFTexture;
class GLTFTextureSampler;
// GLTF index aliases.
using GLTFAccessorIndex = int;
using GLTFAnimationIndex = int;
using GLTFBufferIndex = int;
using GLTFBufferViewIndex = int;
using GLTFCameraIndex = int;
using GLTFImageIndex = int;
using GLTFLightIndex = int;
using GLTFMaterialIndex = int;
using GLTFMeshIndex = int;
using GLTFNodeIndex = int;
using GLTFSkeletonIndex = int;
using GLTFSkinIndex = int;
using GLTFTextureIndex = int;
using GLTFTextureSamplerIndex = int;

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,398 @@
/**************************************************************************/
/* gltf_document.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "extensions/gltf_document_extension.h"
#include "extensions/gltf_spec_gloss.h"
#include "gltf_defines.h"
#include "gltf_state.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/multimesh_instance_3d.h"
class CSGShape3D;
class GridMap;
class GLTFDocument : public Resource {
GDCLASS(GLTFDocument, Resource);
public:
const int32_t JOINT_GROUP_SIZE = 4;
enum {
ARRAY_BUFFER = 34962,
ELEMENT_ARRAY_BUFFER = 34963,
};
enum {
TEXTURE_TYPE_GENERIC = 0,
TEXTURE_TYPE_NORMAL = 1,
};
enum RootNodeMode {
ROOT_NODE_MODE_SINGLE_ROOT,
ROOT_NODE_MODE_KEEP_ROOT,
ROOT_NODE_MODE_MULTI_ROOT,
};
enum VisibilityMode {
VISIBILITY_MODE_INCLUDE_REQUIRED,
VISIBILITY_MODE_INCLUDE_OPTIONAL,
VISIBILITY_MODE_EXCLUDE,
};
private:
int _naming_version = 2;
String _image_format = "PNG";
float _lossy_quality = 0.75f;
String _fallback_image_format = "None";
float _fallback_image_quality = 0.25f;
Ref<GLTFDocumentExtension> _image_save_extension;
RootNodeMode _root_node_mode = RootNodeMode::ROOT_NODE_MODE_SINGLE_ROOT;
VisibilityMode _visibility_mode = VisibilityMode::VISIBILITY_MODE_INCLUDE_REQUIRED;
protected:
static void _bind_methods();
String _gen_unique_name(Ref<GLTFState> p_state, const String &p_name);
static Vector<Ref<GLTFDocumentExtension>> all_document_extensions;
Vector<Ref<GLTFDocumentExtension>> document_extensions;
public:
static void register_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension, bool p_first_priority = false);
static void unregister_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension);
static void unregister_all_gltf_document_extensions();
static Vector<Ref<GLTFDocumentExtension>> get_all_gltf_document_extensions();
static Vector<String> get_supported_gltf_extensions();
static HashSet<String> get_supported_gltf_extensions_hashset();
static NodePath _find_material_node_path(Ref<GLTFState> p_state, Ref<Material> p_material);
static Ref<GLTFObjectModelProperty> import_object_model_property(Ref<GLTFState> p_state, const String &p_json_pointer);
static Ref<GLTFObjectModelProperty> export_object_model_property(Ref<GLTFState> p_state, const NodePath &p_node_path, const Node *p_godot_node, GLTFNodeIndex p_gltf_node_index);
void set_naming_version(int p_version);
int get_naming_version() const;
void set_image_format(const String &p_image_format);
String get_image_format() const;
void set_lossy_quality(float p_lossy_quality);
float get_lossy_quality() const;
void set_fallback_image_format(const String &p_fallback_image_format);
String get_fallback_image_format() const;
void set_fallback_image_quality(float p_fallback_image_quality);
float get_fallback_image_quality() const;
void set_root_node_mode(RootNodeMode p_root_node_mode);
RootNodeMode get_root_node_mode() const;
void set_visibility_mode(VisibilityMode p_visibility_mode);
VisibilityMode get_visibility_mode() const;
static String _gen_unique_name_static(HashSet<String> &r_unique_names, const String &p_name);
private:
void _build_parent_hierarchy(Ref<GLTFState> p_state);
double _filter_number(double p_float);
void _round_min_max_components(Vector<double> &r_type_min, Vector<double> &r_type_max);
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_nodes(Ref<GLTFState> p_state);
String _get_accessor_type_name(const GLTFAccessor::GLTFAccessorType p_accessor_type);
String _sanitize_animation_name(const String &p_name);
String _gen_unique_animation_name(Ref<GLTFState> p_state, const String &p_name);
String _sanitize_bone_name(const String &p_name);
String _gen_unique_bone_name(Ref<GLTFState> p_state,
const GLTFSkeletonIndex p_skel_i,
const String &p_name);
GLTFTextureIndex _set_texture(Ref<GLTFState> p_state, Ref<Texture2D> p_texture,
StandardMaterial3D::TextureFilter p_filter_mode, bool p_repeats);
Ref<Texture2D> _get_texture(Ref<GLTFState> p_state,
const GLTFTextureIndex p_texture, int p_texture_type);
GLTFTextureSamplerIndex _set_sampler_for_mode(Ref<GLTFState> p_state,
StandardMaterial3D::TextureFilter p_filter_mode, bool p_repeats);
Ref<GLTFTextureSampler> _get_sampler_for_texture(Ref<GLTFState> p_state,
const GLTFTextureIndex p_texture);
Error _parse_json(const String &p_path, Ref<GLTFState> p_state);
Error _parse_glb(Ref<FileAccess> p_file, Ref<GLTFState> p_state);
void _compute_node_heights(Ref<GLTFState> p_state);
Error _parse_buffers(Ref<GLTFState> p_state, const String &p_base_path);
Error _parse_buffer_views(Ref<GLTFState> p_state);
GLTFAccessor::GLTFAccessorType _get_accessor_type_from_str(const String &p_string);
Error _parse_accessors(Ref<GLTFState> p_state);
Error _decode_buffer_view(Ref<GLTFState> p_state, double *p_dst,
const GLTFBufferViewIndex p_buffer_view,
const int64_t p_skip_every, const int64_t p_skip_bytes,
const int64_t p_element_size, const int64_t p_count,
const GLTFAccessor::GLTFAccessorType p_accessor_type, const int64_t p_component_count,
const GLTFAccessor::GLTFComponentType p_component_type, const int64_t p_component_size,
const bool p_normalized, const int64_t p_byte_offset,
const bool p_for_vertex);
Vector<double> _decode_accessor(Ref<GLTFState> p_state,
const GLTFAccessorIndex p_accessor,
const bool p_for_vertex);
Vector<float> _decode_accessor_as_floats(Ref<GLTFState> p_state,
const GLTFAccessorIndex p_accessor,
const bool p_for_vertex,
const Vector<int> &p_packed_vertex_ids = Vector<int>());
Vector<int> _decode_accessor_as_ints(Ref<GLTFState> p_state,
const GLTFAccessorIndex p_accessor,
const bool p_for_vertex,
const Vector<int> &p_packed_vertex_ids = Vector<int>());
Vector<Vector2> _decode_accessor_as_vec2(Ref<GLTFState> p_state,
const GLTFAccessorIndex p_accessor,
const bool p_for_vertex,
const Vector<int> &p_packed_vertex_ids = Vector<int>());
Vector<Vector3> _decode_accessor_as_vec3(Ref<GLTFState> p_state,
const GLTFAccessorIndex p_accessor,
const bool p_for_vertex,
const Vector<int> &p_packed_vertex_ids = Vector<int>());
Vector<Color> _decode_accessor_as_color(Ref<GLTFState> p_state,
const GLTFAccessorIndex p_accessor,
const bool p_for_vertex,
const Vector<int> &p_packed_vertex_ids = Vector<int>());
Vector<Quaternion> _decode_accessor_as_quaternion(Ref<GLTFState> p_state,
const GLTFAccessorIndex p_accessor,
const bool p_for_vertex);
Vector<Transform2D> _decode_accessor_as_xform2d(Ref<GLTFState> p_state,
const GLTFAccessorIndex p_accessor,
const bool p_for_vertex);
Vector<Basis> _decode_accessor_as_basis(Ref<GLTFState> p_state,
const GLTFAccessorIndex p_accessor,
const bool p_for_vertex);
Vector<Transform3D> _decode_accessor_as_xform(Ref<GLTFState> p_state,
const GLTFAccessorIndex p_accessor,
const bool p_for_vertex);
Vector<Variant> _decode_accessor_as_variant(Ref<GLTFState> p_state,
const GLTFAccessorIndex p_accessor,
Variant::Type p_variant_type,
GLTFAccessor::GLTFAccessorType p_accessor_type);
GLTFAccessorIndex _encode_accessor_as_variant(Ref<GLTFState> p_state,
Vector<Variant> p_attribs,
Variant::Type p_variant_type,
GLTFAccessor::GLTFAccessorType p_accessor_type,
GLTFAccessor::GLTFComponentType p_component_type = GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT);
Error _parse_meshes(Ref<GLTFState> p_state);
Error _serialize_textures(Ref<GLTFState> p_state);
Error _serialize_texture_samplers(Ref<GLTFState> p_state);
Error _serialize_images(Ref<GLTFState> p_state);
Dictionary _serialize_image(Ref<GLTFState> p_state, Ref<Image> p_image, const String &p_image_format, float p_lossy_quality, Ref<GLTFDocumentExtension> p_image_save_extension);
Error _serialize_lights(Ref<GLTFState> p_state);
Ref<Image> _parse_image_bytes_into_image(Ref<GLTFState> p_state, const Vector<uint8_t> &p_bytes, const String &p_mime_type, int p_index, String &r_file_extension);
void _parse_image_save_image(Ref<GLTFState> p_state, const Vector<uint8_t> &p_bytes, const String &p_resource_uri, const String &p_file_extension, int p_index, Ref<Image> p_image);
Error _parse_images(Ref<GLTFState> p_state, const String &p_base_path);
Error _parse_textures(Ref<GLTFState> p_state);
Error _parse_texture_samplers(Ref<GLTFState> p_state);
Error _parse_materials(Ref<GLTFState> p_state);
void _set_texture_transform_uv1(const Dictionary &d, Ref<BaseMaterial3D> p_material);
void spec_gloss_to_rough_metal(Ref<GLTFSpecGloss> r_spec_gloss,
Ref<BaseMaterial3D> p_material);
static void spec_gloss_to_metal_base_color(const Color &p_specular_factor,
const Color &p_diffuse,
Color &r_base_color,
float &r_metallic);
Error _parse_skins(Ref<GLTFState> p_state);
Error _serialize_skins(Ref<GLTFState> p_state);
Error _create_skins(Ref<GLTFState> p_state);
bool _skins_are_same(const Ref<Skin> p_skin_a, const Ref<Skin> p_skin_b);
void _remove_duplicate_skins(Ref<GLTFState> p_state);
Error _serialize_cameras(Ref<GLTFState> p_state);
Error _parse_cameras(Ref<GLTFState> p_state);
Error _parse_lights(Ref<GLTFState> p_state);
Error _parse_animations(Ref<GLTFState> p_state);
void _parse_animation_pointer(Ref<GLTFState> p_state, const String &p_animation_json_pointer, const Ref<GLTFAnimation> p_gltf_animation, const GLTFAnimation::Interpolation p_interp, const Vector<double> &p_times, const int p_output_value_accessor_index);
Error _serialize_animations(Ref<GLTFState> p_state);
bool _does_skinned_mesh_require_placeholder_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node);
BoneAttachment3D *_generate_bone_attachment(Skeleton3D *p_godot_skeleton, const Ref<GLTFNode> &p_bone_node);
BoneAttachment3D *_generate_bone_attachment_compat_4pt4(Ref<GLTFState> p_state, Skeleton3D *p_skeleton, const GLTFNodeIndex p_node_index, const GLTFNodeIndex p_bone_index);
ImporterMeshInstance3D *_generate_mesh_instance(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
Camera3D *_generate_camera(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
Light3D *_generate_light(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
Node3D *_generate_spatial(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
void _assign_node_names(Ref<GLTFState> p_state);
template <typename T>
T _interpolate_track(const Vector<double> &p_times, const Vector<T> &p_values,
const float p_time,
const GLTFAnimation::Interpolation p_interp);
GLTFAccessorIndex _encode_accessor_as_quaternions(Ref<GLTFState> p_state,
const Vector<Quaternion> p_attribs,
const bool p_for_vertex);
GLTFAccessorIndex _encode_accessor_as_weights(Ref<GLTFState> p_state,
const Vector<Color> p_attribs,
const bool p_for_vertex);
GLTFAccessorIndex _encode_accessor_as_joints(Ref<GLTFState> p_state,
const Vector<Color> p_attribs,
const bool p_for_vertex);
GLTFAccessorIndex _encode_accessor_as_floats(Ref<GLTFState> p_state,
const Vector<double> p_attribs,
const bool p_for_vertex);
GLTFAccessorIndex _encode_accessor_as_vec2(Ref<GLTFState> p_state,
const Vector<Vector2> p_attribs,
const bool p_for_vertex);
void _calc_accessor_vec2_min_max(int p_i, const int64_t p_element_count, Vector<double> &p_type_max, Vector2 p_attribs, Vector<double> &p_type_min) {
if (p_i == 0) {
for (int64_t type_i = 0; type_i < p_element_count; type_i++) {
p_type_max.write[type_i] = p_attribs[(p_i * p_element_count) + type_i];
p_type_min.write[type_i] = p_attribs[(p_i * p_element_count) + type_i];
}
}
for (int64_t type_i = 0; type_i < p_element_count; type_i++) {
p_type_max.write[type_i] = MAX(p_attribs[(p_i * p_element_count) + type_i], p_type_max[type_i]);
p_type_min.write[type_i] = MIN(p_attribs[(p_i * p_element_count) + type_i], p_type_min[type_i]);
p_type_max.write[type_i] = _filter_number(p_type_max.write[type_i]);
p_type_min.write[type_i] = _filter_number(p_type_min.write[type_i]);
}
}
GLTFAccessorIndex _encode_accessor_as_vec3(Ref<GLTFState> p_state,
const Vector<Vector3> p_attribs,
const bool p_for_vertex);
GLTFAccessorIndex _encode_sparse_accessor_as_vec3(Ref<GLTFState> p_state, const Vector<Vector3> p_attribs, const Vector<Vector3> p_reference_attribs, const float p_reference_multiplier, const bool p_for_vertex, const GLTFAccessorIndex p_reference_accessor);
GLTFAccessorIndex _encode_accessor_as_color(Ref<GLTFState> p_state,
const Vector<Color> p_attribs,
const bool p_for_vertex);
void _calc_accessor_min_max(int p_i, const int64_t p_element_count, Vector<double> &p_type_max, Vector<double> p_attribs, Vector<double> &p_type_min);
GLTFAccessorIndex _encode_accessor_as_ints(Ref<GLTFState> p_state,
const Vector<int32_t> p_attribs,
const bool p_for_vertex,
const bool p_for_indices);
GLTFAccessorIndex _encode_accessor_as_xform(Ref<GLTFState> p_state,
const Vector<Transform3D> p_attribs,
const bool p_for_vertex);
Error _encode_accessor_into_buffer_view(Ref<GLTFState> p_state, const double *p_src,
const int64_t p_count, const GLTFAccessor::GLTFAccessorType p_accessor_type,
const GLTFAccessor::GLTFComponentType p_component_type, const bool p_normalized,
const int64_t p_byte_offset, const bool p_for_vertex,
GLTFBufferViewIndex &r_buffer_view, const bool p_for_indices = false);
Error _encode_accessors(Ref<GLTFState> p_state);
Error _encode_buffer_views(Ref<GLTFState> p_state);
Error _serialize_materials(Ref<GLTFState> p_state);
Error _serialize_meshes(Ref<GLTFState> p_state);
Error _serialize_nodes(Ref<GLTFState> p_state);
Error _serialize_scenes(Ref<GLTFState> p_state);
String interpolation_to_string(const GLTFAnimation::Interpolation p_interp);
Error _encode_buffer_bins(Ref<GLTFState> p_state, const String &p_path);
Error _encode_buffer_glb(Ref<GLTFState> p_state, const String &p_path);
PackedByteArray _serialize_glb_buffer(Ref<GLTFState> p_state, Error *r_err);
Dictionary _serialize_texture_transform_uv1(Ref<BaseMaterial3D> p_material);
Dictionary _serialize_texture_transform_uv2(Ref<BaseMaterial3D> p_material);
Error _serialize_asset_header(Ref<GLTFState> p_state);
Error _serialize_file(Ref<GLTFState> p_state, const String p_path);
Error _serialize_gltf_extensions(Ref<GLTFState> p_state) const;
public:
// https://www.itu.int/rec/R-REC-BT.601
// https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.601-7-201103-I!!PDF-E.pdf
static constexpr float R_BRIGHTNESS_COEFF = 0.299f;
static constexpr float G_BRIGHTNESS_COEFF = 0.587f;
static constexpr float B_BRIGHTNESS_COEFF = 0.114f;
private:
// https://github.com/microsoft/glTF-SDK/blob/master/GLTFSDK/Source/PBRUtils.cpp#L9
// https://bghgary.github.io/glTF/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
static float solve_metallic(float p_dielectric_specular, float p_diffuse,
float p_specular,
float p_one_minus_specular_strength);
static float get_perceived_brightness(const Color p_color);
static float get_max_component(const Color &p_color);
public:
virtual Error append_from_file(String p_path, Ref<GLTFState> p_state, uint32_t p_flags = 0, String p_base_path = String());
virtual Error append_from_buffer(PackedByteArray p_bytes, String p_base_path, Ref<GLTFState> p_state, uint32_t p_flags = 0);
virtual Error append_from_scene(Node *p_node, Ref<GLTFState> p_state, uint32_t p_flags = 0);
virtual Node *generate_scene(Ref<GLTFState> p_state, float p_bake_fps = 30.0f, bool p_trimming = false, bool p_remove_immutable_tracks = true);
virtual PackedByteArray generate_buffer(Ref<GLTFState> p_state);
virtual Error write_to_filesystem(Ref<GLTFState> p_state, const String &p_path);
public:
Error _parse_gltf_state(Ref<GLTFState> p_state, const String &p_search_path);
Error _parse_asset_header(Ref<GLTFState> p_state);
Error _parse_gltf_extensions(Ref<GLTFState> p_state);
void _process_mesh_instances(Ref<GLTFState> p_state, Node *p_scene_root);
Node *_generate_scene_node_tree(Ref<GLTFState> p_state);
void _generate_scene_node(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root);
void _generate_skeleton_bone_node(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node3D *p_current_node, Node *p_scene_parent, Node *p_scene_root);
void _attach_node_to_skeleton(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node3D *p_current_node, Skeleton3D *p_godot_skeleton, Node *p_scene_root, GLTFNodeIndex p_bone_node_index = -1);
void _generate_scene_node_compat_4pt4(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root);
void _generate_skeleton_bone_node_compat_4pt4(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root);
void _import_animation(Ref<GLTFState> p_state, AnimationPlayer *p_animation_player,
const GLTFAnimationIndex p_index, const bool p_trimming, const bool p_remove_immutable_tracks);
void _convert_mesh_instances(Ref<GLTFState> p_state);
GLTFCameraIndex _convert_camera(Ref<GLTFState> p_state, Camera3D *p_camera);
void _convert_light_to_gltf(Light3D *p_light, Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node);
GLTFLightIndex _convert_light(Ref<GLTFState> p_state, Light3D *p_light);
void _convert_spatial(Ref<GLTFState> p_state, Node3D *p_spatial, Ref<GLTFNode> p_node);
void _convert_scene_node(Ref<GLTFState> p_state, Node *p_current,
const GLTFNodeIndex p_gltf_current,
const GLTFNodeIndex p_gltf_root);
void _convert_csg_shape_to_gltf(CSGShape3D *p_current, GLTFNodeIndex p_gltf_parent, Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
void _check_visibility(Node *p_node, bool &r_retflag);
void _convert_camera_to_gltf(Camera3D *p_camera, Ref<GLTFState> p_state,
Ref<GLTFNode> p_gltf_node);
void _convert_grid_map_to_gltf(
GridMap *p_grid_map,
GLTFNodeIndex p_parent_node_index,
GLTFNodeIndex p_root_node_index,
Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
void _convert_multi_mesh_instance_to_gltf(
MultiMeshInstance3D *p_multi_mesh_instance,
GLTFNodeIndex p_parent_node_index,
GLTFNodeIndex p_root_node_index,
Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
void _convert_skeleton_to_gltf(
Skeleton3D *p_scene_parent, Ref<GLTFState> p_state,
GLTFNodeIndex p_parent_node_index,
GLTFNodeIndex p_root_node_index,
Ref<GLTFNode> p_gltf_node);
void _convert_bone_attachment_to_gltf(BoneAttachment3D *p_bone_attachment,
Ref<GLTFState> p_state,
GLTFNodeIndex p_parent_node_index,
GLTFNodeIndex p_root_node_index,
Ref<GLTFNode> p_gltf_node);
void _convert_mesh_instance_to_gltf(MeshInstance3D *p_mesh_instance,
Ref<GLTFState> p_state,
Ref<GLTFNode> p_gltf_node);
GLTFMeshIndex _convert_mesh_to_gltf(Ref<GLTFState> p_state,
MeshInstance3D *p_mesh_instance);
GLTFNodeIndex _node_and_or_bone_to_gltf_node_index(Ref<GLTFState> p_state, const Vector<StringName> &p_node_subpath, const Node *p_godot_node);
bool _convert_animation_node_track(Ref<GLTFState> p_state,
GLTFAnimation::NodeTrack &p_gltf_node_track,
const Ref<Animation> &p_godot_animation,
int32_t p_godot_anim_track_index,
Vector<double> &p_times);
void _convert_animation(Ref<GLTFState> p_state, AnimationPlayer *p_animation_player, const String &p_animation_track_name);
Error _serialize(Ref<GLTFState> p_state);
Error _parse(Ref<GLTFState> p_state, String p_path, Ref<FileAccess> p_file);
};
VARIANT_ENUM_CAST(GLTFDocument::RootNodeMode);
VARIANT_ENUM_CAST(GLTFDocument::VisibilityMode);

479
modules/gltf/gltf_state.cpp Normal file
View File

@@ -0,0 +1,479 @@
/**************************************************************************/
/* gltf_state.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_state.h"
#include "gltf_template_convert.h"
void GLTFState::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_used_extension", "extension_name", "required"), &GLTFState::add_used_extension);
ClassDB::bind_method(D_METHOD("append_data_to_buffers", "data", "deduplication"), &GLTFState::append_data_to_buffers);
ClassDB::bind_method(D_METHOD("append_gltf_node", "gltf_node", "godot_scene_node", "parent_node_index"), &GLTFState::append_gltf_node);
ClassDB::bind_method(D_METHOD("get_json"), &GLTFState::get_json);
ClassDB::bind_method(D_METHOD("set_json", "json"), &GLTFState::set_json);
ClassDB::bind_method(D_METHOD("get_major_version"), &GLTFState::get_major_version);
ClassDB::bind_method(D_METHOD("set_major_version", "major_version"), &GLTFState::set_major_version);
ClassDB::bind_method(D_METHOD("get_minor_version"), &GLTFState::get_minor_version);
ClassDB::bind_method(D_METHOD("set_minor_version", "minor_version"), &GLTFState::set_minor_version);
ClassDB::bind_method(D_METHOD("get_copyright"), &GLTFState::get_copyright);
ClassDB::bind_method(D_METHOD("set_copyright", "copyright"), &GLTFState::set_copyright);
ClassDB::bind_method(D_METHOD("get_glb_data"), &GLTFState::get_glb_data);
ClassDB::bind_method(D_METHOD("set_glb_data", "glb_data"), &GLTFState::set_glb_data);
ClassDB::bind_method(D_METHOD("get_use_named_skin_binds"), &GLTFState::get_use_named_skin_binds);
ClassDB::bind_method(D_METHOD("set_use_named_skin_binds", "use_named_skin_binds"), &GLTFState::set_use_named_skin_binds);
ClassDB::bind_method(D_METHOD("get_nodes"), &GLTFState::get_nodes);
ClassDB::bind_method(D_METHOD("set_nodes", "nodes"), &GLTFState::set_nodes);
ClassDB::bind_method(D_METHOD("get_buffers"), &GLTFState::get_buffers);
ClassDB::bind_method(D_METHOD("set_buffers", "buffers"), &GLTFState::set_buffers);
ClassDB::bind_method(D_METHOD("get_buffer_views"), &GLTFState::get_buffer_views);
ClassDB::bind_method(D_METHOD("set_buffer_views", "buffer_views"), &GLTFState::set_buffer_views);
ClassDB::bind_method(D_METHOD("get_accessors"), &GLTFState::get_accessors);
ClassDB::bind_method(D_METHOD("set_accessors", "accessors"), &GLTFState::set_accessors);
ClassDB::bind_method(D_METHOD("get_meshes"), &GLTFState::get_meshes);
ClassDB::bind_method(D_METHOD("set_meshes", "meshes"), &GLTFState::set_meshes);
ClassDB::bind_method(D_METHOD("get_animation_players_count", "idx"), &GLTFState::get_animation_players_count);
ClassDB::bind_method(D_METHOD("get_animation_player", "idx"), &GLTFState::get_animation_player);
ClassDB::bind_method(D_METHOD("get_materials"), &GLTFState::get_materials);
ClassDB::bind_method(D_METHOD("set_materials", "materials"), &GLTFState::set_materials);
ClassDB::bind_method(D_METHOD("get_scene_name"), &GLTFState::get_scene_name);
ClassDB::bind_method(D_METHOD("set_scene_name", "scene_name"), &GLTFState::set_scene_name);
ClassDB::bind_method(D_METHOD("get_base_path"), &GLTFState::get_base_path);
ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &GLTFState::set_base_path);
ClassDB::bind_method(D_METHOD("get_filename"), &GLTFState::get_filename);
ClassDB::bind_method(D_METHOD("set_filename", "filename"), &GLTFState::set_filename);
ClassDB::bind_method(D_METHOD("get_root_nodes"), &GLTFState::get_root_nodes);
ClassDB::bind_method(D_METHOD("set_root_nodes", "root_nodes"), &GLTFState::set_root_nodes);
ClassDB::bind_method(D_METHOD("get_textures"), &GLTFState::get_textures);
ClassDB::bind_method(D_METHOD("set_textures", "textures"), &GLTFState::set_textures);
ClassDB::bind_method(D_METHOD("get_texture_samplers"), &GLTFState::get_texture_samplers);
ClassDB::bind_method(D_METHOD("set_texture_samplers", "texture_samplers"), &GLTFState::set_texture_samplers);
ClassDB::bind_method(D_METHOD("get_images"), &GLTFState::get_images);
ClassDB::bind_method(D_METHOD("set_images", "images"), &GLTFState::set_images);
ClassDB::bind_method(D_METHOD("get_skins"), &GLTFState::get_skins);
ClassDB::bind_method(D_METHOD("set_skins", "skins"), &GLTFState::set_skins);
ClassDB::bind_method(D_METHOD("get_cameras"), &GLTFState::get_cameras);
ClassDB::bind_method(D_METHOD("set_cameras", "cameras"), &GLTFState::set_cameras);
ClassDB::bind_method(D_METHOD("get_lights"), &GLTFState::get_lights);
ClassDB::bind_method(D_METHOD("set_lights", "lights"), &GLTFState::set_lights);
ClassDB::bind_method(D_METHOD("get_unique_names"), &GLTFState::get_unique_names);
ClassDB::bind_method(D_METHOD("set_unique_names", "unique_names"), &GLTFState::set_unique_names);
ClassDB::bind_method(D_METHOD("get_unique_animation_names"), &GLTFState::get_unique_animation_names);
ClassDB::bind_method(D_METHOD("set_unique_animation_names", "unique_animation_names"), &GLTFState::set_unique_animation_names);
ClassDB::bind_method(D_METHOD("get_skeletons"), &GLTFState::get_skeletons);
ClassDB::bind_method(D_METHOD("set_skeletons", "skeletons"), &GLTFState::set_skeletons);
ClassDB::bind_method(D_METHOD("get_create_animations"), &GLTFState::get_create_animations);
ClassDB::bind_method(D_METHOD("set_create_animations", "create_animations"), &GLTFState::set_create_animations);
ClassDB::bind_method(D_METHOD("get_import_as_skeleton_bones"), &GLTFState::get_import_as_skeleton_bones);
ClassDB::bind_method(D_METHOD("set_import_as_skeleton_bones", "import_as_skeleton_bones"), &GLTFState::set_import_as_skeleton_bones);
ClassDB::bind_method(D_METHOD("get_animations"), &GLTFState::get_animations);
ClassDB::bind_method(D_METHOD("set_animations", "animations"), &GLTFState::set_animations);
ClassDB::bind_method(D_METHOD("get_scene_node", "idx"), &GLTFState::get_scene_node);
ClassDB::bind_method(D_METHOD("get_node_index", "scene_node"), &GLTFState::get_node_index);
ClassDB::bind_method(D_METHOD("get_additional_data", "extension_name"), &GLTFState::get_additional_data);
ClassDB::bind_method(D_METHOD("set_additional_data", "extension_name", "additional_data"), &GLTFState::set_additional_data);
ClassDB::bind_method(D_METHOD("get_handle_binary_image"), &GLTFState::get_handle_binary_image);
ClassDB::bind_method(D_METHOD("set_handle_binary_image", "method"), &GLTFState::set_handle_binary_image);
ClassDB::bind_method(D_METHOD("set_bake_fps", "value"), &GLTFState::set_bake_fps);
ClassDB::bind_method(D_METHOD("get_bake_fps"), &GLTFState::get_bake_fps);
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "json"), "set_json", "get_json"); // Dictionary
ADD_PROPERTY(PropertyInfo(Variant::INT, "major_version"), "set_major_version", "get_major_version"); // int
ADD_PROPERTY(PropertyInfo(Variant::INT, "minor_version"), "set_minor_version", "get_minor_version"); // int
ADD_PROPERTY(PropertyInfo(Variant::STRING, "copyright"), "set_copyright", "get_copyright"); // String
ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "glb_data"), "set_glb_data", "get_glb_data"); // Vector<uint8_t>
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_named_skin_binds"), "set_use_named_skin_binds", "get_use_named_skin_binds"); // bool
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "nodes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_nodes", "get_nodes"); // Vector<Ref<GLTFNode>>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "buffers"), "set_buffers", "get_buffers"); // Vector<Vector<uint8_t>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "buffer_views", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_buffer_views", "get_buffer_views"); // Vector<Ref<GLTFBufferView>>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "accessors", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_accessors", "get_accessors"); // Vector<Ref<GLTFAccessor>>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "meshes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_meshes", "get_meshes"); // Vector<Ref<GLTFMesh>>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "materials", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_materials", "get_materials"); // Vector<Ref<Material>
ADD_PROPERTY(PropertyInfo(Variant::STRING, "scene_name"), "set_scene_name", "get_scene_name"); // String
ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_path"), "set_base_path", "get_base_path"); // String
ADD_PROPERTY(PropertyInfo(Variant::STRING, "filename"), "set_filename", "get_filename"); // String
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "root_nodes"), "set_root_nodes", "get_root_nodes"); // Vector<int>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "textures", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_textures", "get_textures"); // Vector<Ref<GLTFTexture>>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "texture_samplers", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_texture_samplers", "get_texture_samplers"); //Vector<Ref<GLTFTextureSampler>>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "images", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_images", "get_images"); // Vector<Ref<Texture>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "skins", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_skins", "get_skins"); // Vector<Ref<GLTFSkin>>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "cameras", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_cameras", "get_cameras"); // Vector<Ref<GLTFCamera>>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "lights", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_lights", "get_lights"); // Vector<Ref<GLTFLight>>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "unique_names", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_unique_names", "get_unique_names"); // Set<String>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "unique_animation_names", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_unique_animation_names", "get_unique_animation_names"); // Set<String>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "skeletons", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_skeletons", "get_skeletons"); // Vector<Ref<GLTFSkeleton>>
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "create_animations"), "set_create_animations", "get_create_animations"); // bool
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "import_as_skeleton_bones"), "set_import_as_skeleton_bones", "get_import_as_skeleton_bones"); // bool
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "animations", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_animations", "get_animations"); // Vector<Ref<GLTFAnimation>>
ADD_PROPERTY(PropertyInfo(Variant::INT, "handle_binary_image", PROPERTY_HINT_ENUM, "Discard All Textures,Extract Textures,Embed as Basis Universal,Embed as Uncompressed", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_handle_binary_image", "get_handle_binary_image"); // enum
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bake_fps"), "set_bake_fps", "get_bake_fps");
BIND_CONSTANT(HANDLE_BINARY_DISCARD_TEXTURES);
BIND_CONSTANT(HANDLE_BINARY_EXTRACT_TEXTURES);
BIND_CONSTANT(HANDLE_BINARY_EMBED_AS_BASISU);
BIND_CONSTANT(HANDLE_BINARY_EMBED_AS_UNCOMPRESSED);
}
void GLTFState::add_used_extension(const String &p_extension_name, bool p_required) {
if (!extensions_used.has(p_extension_name)) {
extensions_used.push_back(p_extension_name);
}
if (p_required) {
if (!extensions_required.has(p_extension_name)) {
extensions_required.push_back(p_extension_name);
}
}
}
Dictionary GLTFState::get_json() {
return json;
}
void GLTFState::set_json(Dictionary p_json) {
json = p_json;
}
int GLTFState::get_major_version() {
return major_version;
}
void GLTFState::set_major_version(int p_major_version) {
major_version = p_major_version;
}
int GLTFState::get_minor_version() {
return minor_version;
}
void GLTFState::set_minor_version(int p_minor_version) {
minor_version = p_minor_version;
}
String GLTFState::get_copyright() const {
return copyright;
}
void GLTFState::set_copyright(const String &p_copyright) {
copyright = p_copyright;
}
Vector<uint8_t> GLTFState::get_glb_data() {
return glb_data;
}
void GLTFState::set_glb_data(Vector<uint8_t> p_glb_data) {
glb_data = p_glb_data;
}
bool GLTFState::get_use_named_skin_binds() {
return use_named_skin_binds;
}
void GLTFState::set_use_named_skin_binds(bool p_use_named_skin_binds) {
use_named_skin_binds = p_use_named_skin_binds;
}
TypedArray<GLTFNode> GLTFState::get_nodes() {
return GLTFTemplateConvert::to_array(nodes);
}
void GLTFState::set_nodes(TypedArray<GLTFNode> p_nodes) {
GLTFTemplateConvert::set_from_array(nodes, p_nodes);
}
TypedArray<PackedByteArray> GLTFState::get_buffers() {
return GLTFTemplateConvert::to_array(buffers);
}
void GLTFState::set_buffers(TypedArray<PackedByteArray> p_buffers) {
GLTFTemplateConvert::set_from_array(buffers, p_buffers);
}
TypedArray<GLTFBufferView> GLTFState::get_buffer_views() {
return GLTFTemplateConvert::to_array(buffer_views);
}
void GLTFState::set_buffer_views(TypedArray<GLTFBufferView> p_buffer_views) {
GLTFTemplateConvert::set_from_array(buffer_views, p_buffer_views);
}
TypedArray<GLTFAccessor> GLTFState::get_accessors() {
return GLTFTemplateConvert::to_array(accessors);
}
void GLTFState::set_accessors(TypedArray<GLTFAccessor> p_accessors) {
GLTFTemplateConvert::set_from_array(accessors, p_accessors);
}
TypedArray<GLTFMesh> GLTFState::get_meshes() {
return GLTFTemplateConvert::to_array(meshes);
}
void GLTFState::set_meshes(TypedArray<GLTFMesh> p_meshes) {
GLTFTemplateConvert::set_from_array(meshes, p_meshes);
}
TypedArray<Material> GLTFState::get_materials() {
return GLTFTemplateConvert::to_array(materials);
}
void GLTFState::set_materials(TypedArray<Material> p_materials) {
GLTFTemplateConvert::set_from_array(materials, p_materials);
}
String GLTFState::get_scene_name() {
return scene_name;
}
void GLTFState::set_scene_name(String p_scene_name) {
scene_name = p_scene_name;
}
PackedInt32Array GLTFState::get_root_nodes() {
return root_nodes;
}
void GLTFState::set_root_nodes(PackedInt32Array p_root_nodes) {
root_nodes = p_root_nodes;
}
TypedArray<GLTFTexture> GLTFState::get_textures() {
return GLTFTemplateConvert::to_array(textures);
}
void GLTFState::set_textures(TypedArray<GLTFTexture> p_textures) {
GLTFTemplateConvert::set_from_array(textures, p_textures);
}
TypedArray<GLTFTextureSampler> GLTFState::get_texture_samplers() {
return GLTFTemplateConvert::to_array(texture_samplers);
}
void GLTFState::set_texture_samplers(TypedArray<GLTFTextureSampler> p_texture_samplers) {
GLTFTemplateConvert::set_from_array(texture_samplers, p_texture_samplers);
}
TypedArray<Texture2D> GLTFState::get_images() {
return GLTFTemplateConvert::to_array(images);
}
void GLTFState::set_images(TypedArray<Texture2D> p_images) {
GLTFTemplateConvert::set_from_array(images, p_images);
}
TypedArray<GLTFSkin> GLTFState::get_skins() {
return GLTFTemplateConvert::to_array(skins);
}
void GLTFState::set_skins(TypedArray<GLTFSkin> p_skins) {
GLTFTemplateConvert::set_from_array(skins, p_skins);
}
TypedArray<GLTFCamera> GLTFState::get_cameras() {
return GLTFTemplateConvert::to_array(cameras);
}
void GLTFState::set_cameras(TypedArray<GLTFCamera> p_cameras) {
GLTFTemplateConvert::set_from_array(cameras, p_cameras);
}
TypedArray<GLTFLight> GLTFState::get_lights() {
return GLTFTemplateConvert::to_array(lights);
}
void GLTFState::set_lights(TypedArray<GLTFLight> p_lights) {
GLTFTemplateConvert::set_from_array(lights, p_lights);
}
TypedArray<String> GLTFState::get_unique_names() {
return GLTFTemplateConvert::to_array(unique_names);
}
void GLTFState::set_unique_names(TypedArray<String> p_unique_names) {
GLTFTemplateConvert::set_from_array(unique_names, p_unique_names);
}
TypedArray<String> GLTFState::get_unique_animation_names() {
return GLTFTemplateConvert::to_array(unique_animation_names);
}
void GLTFState::set_unique_animation_names(TypedArray<String> p_unique_animation_names) {
GLTFTemplateConvert::set_from_array(unique_animation_names, p_unique_animation_names);
}
TypedArray<GLTFSkeleton> GLTFState::get_skeletons() {
return GLTFTemplateConvert::to_array(skeletons);
}
void GLTFState::set_skeletons(TypedArray<GLTFSkeleton> p_skeletons) {
GLTFTemplateConvert::set_from_array(skeletons, p_skeletons);
}
bool GLTFState::get_create_animations() {
return create_animations;
}
void GLTFState::set_create_animations(bool p_create_animations) {
create_animations = p_create_animations;
}
bool GLTFState::get_import_as_skeleton_bones() {
return import_as_skeleton_bones;
}
void GLTFState::set_import_as_skeleton_bones(bool p_import_as_skeleton_bones) {
import_as_skeleton_bones = p_import_as_skeleton_bones;
}
TypedArray<GLTFAnimation> GLTFState::get_animations() {
return GLTFTemplateConvert::to_array(animations);
}
void GLTFState::set_animations(TypedArray<GLTFAnimation> p_animations) {
GLTFTemplateConvert::set_from_array(animations, p_animations);
}
Node *GLTFState::get_scene_node(GLTFNodeIndex idx) {
if (!scene_nodes.has(idx)) {
return nullptr;
}
return scene_nodes[idx];
}
GLTFNodeIndex GLTFState::get_node_index(Node *p_node) {
for (KeyValue<GLTFNodeIndex, Node *> x : scene_nodes) {
if (x.value == p_node) {
return x.key;
}
}
return -1;
}
int GLTFState::get_animation_players_count(int idx) {
return animation_players.size();
}
AnimationPlayer *GLTFState::get_animation_player(int idx) {
ERR_FAIL_INDEX_V(idx, animation_players.size(), nullptr);
return animation_players[idx];
}
void GLTFState::set_discard_meshes_and_materials(bool p_discard_meshes_and_materials) {
discard_meshes_and_materials = p_discard_meshes_and_materials;
}
bool GLTFState::get_discard_meshes_and_materials() {
return discard_meshes_and_materials;
}
String GLTFState::get_base_path() {
return base_path;
}
void GLTFState::set_base_path(const String &p_base_path) {
base_path = p_base_path;
if (extract_path.is_empty()) {
extract_path = p_base_path;
}
}
String GLTFState::get_extract_path() {
return extract_path;
}
void GLTFState::set_extract_path(const String &p_extract_path) {
extract_path = p_extract_path;
}
String GLTFState::get_extract_prefix() {
return extract_prefix;
}
void GLTFState::set_extract_prefix(const String &p_extract_prefix) {
extract_prefix = p_extract_prefix;
}
String GLTFState::get_filename() const {
return filename;
}
void GLTFState::set_filename(const String &p_filename) {
filename = p_filename;
if (extract_prefix.is_empty()) {
extract_prefix = p_filename.get_basename();
}
}
Variant GLTFState::get_additional_data(const StringName &p_extension_name) {
return additional_data[p_extension_name];
}
void GLTFState::set_additional_data(const StringName &p_extension_name, Variant p_additional_data) {
additional_data[p_extension_name] = p_additional_data;
}
GLTFBufferViewIndex GLTFState::append_data_to_buffers(const Vector<uint8_t> &p_data, const bool p_deduplication = false) {
if (p_deduplication) {
for (int i = 0; i < buffer_views.size(); i++) {
Ref<GLTFBufferView> buffer_view = buffer_views[i];
Vector<uint8_t> buffer_view_data = buffer_view->load_buffer_view_data(this);
if (buffer_view_data == p_data) {
return i;
}
}
}
// Append the given data to a buffer and create a buffer view for it.
if (unlikely(buffers.is_empty())) {
buffers.push_back(Vector<uint8_t>());
}
Vector<uint8_t> &destination_buffer = buffers.write[0];
Ref<GLTFBufferView> buffer_view;
buffer_view.instantiate();
buffer_view->set_buffer(0);
buffer_view->set_byte_offset(destination_buffer.size());
buffer_view->set_byte_length(p_data.size());
destination_buffer.append_array(p_data);
const int new_index = buffer_views.size();
buffer_views.push_back(buffer_view);
return new_index;
}
GLTFNodeIndex GLTFState::append_gltf_node(Ref<GLTFNode> p_gltf_node, Node *p_godot_scene_node, GLTFNodeIndex p_parent_node_index) {
p_gltf_node->set_parent(p_parent_node_index);
const GLTFNodeIndex new_index = nodes.size();
nodes.append(p_gltf_node);
scene_nodes.insert(new_index, p_godot_scene_node);
if (p_parent_node_index == -1) {
root_nodes.append(new_index);
} else if (p_parent_node_index < new_index) {
nodes.write[p_parent_node_index]->append_child_index(new_index);
}
return new_index;
}

253
modules/gltf/gltf_state.h Normal file
View File

@@ -0,0 +1,253 @@
/**************************************************************************/
/* gltf_state.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "extensions/gltf_light.h"
#include "structures/gltf_accessor.h"
#include "structures/gltf_animation.h"
#include "structures/gltf_buffer_view.h"
#include "structures/gltf_camera.h"
#include "structures/gltf_mesh.h"
#include "structures/gltf_node.h"
#include "structures/gltf_object_model_property.h"
#include "structures/gltf_skeleton.h"
#include "structures/gltf_skin.h"
#include "structures/gltf_texture.h"
#include "structures/gltf_texture_sampler.h"
#include "scene/3d/importer_mesh_instance_3d.h"
#include "scene/animation/animation_player.h"
class GLTFState : public Resource {
GDCLASS(GLTFState, Resource);
friend class GLTFDocument;
friend class GLTFNode;
protected:
String base_path;
String extract_path;
String extract_prefix;
String filename;
Dictionary json;
int major_version = 0;
int minor_version = 0;
String copyright;
Vector<uint8_t> glb_data;
double bake_fps = 30.0;
bool use_named_skin_binds = false;
bool use_khr_texture_transform = false;
bool discard_meshes_and_materials = false;
bool force_generate_tangents = false;
bool create_animations = true;
bool force_disable_compression = false;
bool import_as_skeleton_bones = false;
int handle_binary_image = HANDLE_BINARY_EXTRACT_TEXTURES;
Vector<Ref<GLTFNode>> nodes;
Vector<Vector<uint8_t>> buffers;
Vector<Ref<GLTFBufferView>> buffer_views;
Vector<Ref<GLTFAccessor>> accessors;
Vector<Ref<GLTFMesh>> meshes; // Meshes are loaded directly, no reason not to.
Vector<AnimationPlayer *> animation_players;
HashMap<Ref<Material>, GLTFMaterialIndex> material_cache;
Vector<Ref<Material>> materials;
String scene_name;
Vector<int> root_nodes;
Vector<Ref<GLTFTexture>> textures;
Vector<Ref<GLTFTextureSampler>> texture_samplers;
Ref<GLTFTextureSampler> default_texture_sampler;
Vector<Ref<Texture2D>> images;
Vector<String> extensions_used;
Vector<String> extensions_required;
Vector<Ref<Image>> source_images;
Vector<Ref<GLTFSkin>> skins;
Vector<Ref<GLTFCamera>> cameras;
Vector<Ref<GLTFLight>> lights;
HashSet<String> unique_names;
HashSet<String> unique_animation_names;
Vector<Ref<GLTFSkeleton>> skeletons;
Vector<Ref<GLTFAnimation>> animations;
HashMap<GLTFNodeIndex, Node *> scene_nodes;
HashMap<GLTFNodeIndex, ImporterMeshInstance3D *> scene_mesh_instances;
HashMap<String, Ref<GLTFObjectModelProperty>> object_model_properties;
HashMap<ObjectID, GLTFSkeletonIndex> skeleton3d_to_gltf_skeleton;
HashMap<ObjectID, HashMap<ObjectID, GLTFSkinIndex>> skin_and_skeleton3d_to_gltf_skin;
Dictionary additional_data;
protected:
static void _bind_methods();
public:
double get_bake_fps() const {
return bake_fps;
}
void set_bake_fps(double value) {
bake_fps = value;
}
void add_used_extension(const String &p_extension, bool p_required = false);
GLTFBufferViewIndex append_data_to_buffers(const Vector<uint8_t> &p_data, const bool p_deduplication);
GLTFNodeIndex append_gltf_node(Ref<GLTFNode> p_gltf_node, Node *p_godot_scene_node, GLTFNodeIndex p_parent_node_index);
enum GLTFHandleBinary {
HANDLE_BINARY_DISCARD_TEXTURES = 0,
HANDLE_BINARY_EXTRACT_TEXTURES,
HANDLE_BINARY_EMBED_AS_BASISU,
HANDLE_BINARY_EMBED_AS_UNCOMPRESSED, // If this value changes from 3, ResourceImporterScene::pre_import must be changed as well.
};
int32_t get_handle_binary_image() {
return handle_binary_image;
}
void set_handle_binary_image(int32_t p_handle_binary_image) {
handle_binary_image = p_handle_binary_image;
}
Dictionary get_json();
void set_json(Dictionary p_json);
int get_major_version();
void set_major_version(int p_major_version);
int get_minor_version();
void set_minor_version(int p_minor_version);
String get_copyright() const;
void set_copyright(const String &p_copyright);
Vector<uint8_t> get_glb_data();
void set_glb_data(Vector<uint8_t> p_glb_data);
bool get_use_named_skin_binds();
void set_use_named_skin_binds(bool p_use_named_skin_binds);
bool get_discard_textures();
void set_discard_textures(bool p_discard_textures);
bool get_embed_as_basisu();
void set_embed_as_basisu(bool p_embed_as_basisu);
bool get_extract_textures();
void set_extract_textures(bool p_extract_textures);
bool get_discard_meshes_and_materials();
void set_discard_meshes_and_materials(bool p_discard_meshes_and_materials);
TypedArray<GLTFNode> get_nodes();
void set_nodes(TypedArray<GLTFNode> p_nodes);
TypedArray<PackedByteArray> get_buffers();
void set_buffers(TypedArray<PackedByteArray> p_buffers);
TypedArray<GLTFBufferView> get_buffer_views();
void set_buffer_views(TypedArray<GLTFBufferView> p_buffer_views);
TypedArray<GLTFAccessor> get_accessors();
void set_accessors(TypedArray<GLTFAccessor> p_accessors);
TypedArray<GLTFMesh> get_meshes();
void set_meshes(TypedArray<GLTFMesh> p_meshes);
TypedArray<Material> get_materials();
void set_materials(TypedArray<Material> p_materials);
String get_scene_name();
void set_scene_name(String p_scene_name);
String get_base_path();
void set_base_path(const String &p_base_path);
String get_extract_path();
void set_extract_path(const String &p_extract_path);
String get_extract_prefix();
void set_extract_prefix(const String &p_extract_prefix);
String get_filename() const;
void set_filename(const String &p_filename);
PackedInt32Array get_root_nodes();
void set_root_nodes(PackedInt32Array p_root_nodes);
TypedArray<GLTFTexture> get_textures();
void set_textures(TypedArray<GLTFTexture> p_textures);
TypedArray<GLTFTextureSampler> get_texture_samplers();
void set_texture_samplers(TypedArray<GLTFTextureSampler> p_texture_samplers);
TypedArray<Texture2D> get_images();
void set_images(TypedArray<Texture2D> p_images);
TypedArray<GLTFSkin> get_skins();
void set_skins(TypedArray<GLTFSkin> p_skins);
TypedArray<GLTFCamera> get_cameras();
void set_cameras(TypedArray<GLTFCamera> p_cameras);
TypedArray<GLTFLight> get_lights();
void set_lights(TypedArray<GLTFLight> p_lights);
TypedArray<String> get_unique_names();
void set_unique_names(TypedArray<String> p_unique_names);
TypedArray<String> get_unique_animation_names();
void set_unique_animation_names(TypedArray<String> p_unique_names);
TypedArray<GLTFSkeleton> get_skeletons();
void set_skeletons(TypedArray<GLTFSkeleton> p_skeletons);
bool get_create_animations();
void set_create_animations(bool p_create_animations);
bool get_import_as_skeleton_bones();
void set_import_as_skeleton_bones(bool p_import_as_skeleton_bones);
TypedArray<GLTFAnimation> get_animations();
void set_animations(TypedArray<GLTFAnimation> p_animations);
Node *get_scene_node(GLTFNodeIndex idx);
GLTFNodeIndex get_node_index(Node *p_node);
int get_animation_players_count(int idx);
AnimationPlayer *get_animation_player(int idx);
Variant get_additional_data(const StringName &p_extension_name);
void set_additional_data(const StringName &p_extension_name, Variant p_additional_data);
};

View File

@@ -0,0 +1,91 @@
/**************************************************************************/
/* gltf_template_convert.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "core/templates/hash_set.h"
#include "core/variant/array.h"
#include "core/variant/dictionary.h"
#include "core/variant/typed_array.h"
namespace GLTFTemplateConvert {
template <typename T>
static Array to_array(const Vector<T> &p_inp) {
Array ret;
for (int i = 0; i < p_inp.size(); i++) {
ret.push_back(p_inp[i]);
}
return ret;
}
template <typename T>
static TypedArray<T> to_array(const HashSet<T> &p_inp) {
TypedArray<T> ret;
typename HashSet<T>::Iterator elem = p_inp.begin();
while (elem) {
ret.push_back(*elem);
++elem;
}
return ret;
}
template <typename T>
static void set_from_array(Vector<T> &r_out, const Array &p_inp) {
r_out.clear();
for (int i = 0; i < p_inp.size(); i++) {
r_out.push_back(p_inp[i]);
}
}
template <typename T>
static void set_from_array(HashSet<T> &r_out, const TypedArray<T> &p_inp) {
r_out.clear();
for (int i = 0; i < p_inp.size(); i++) {
r_out.insert(p_inp[i]);
}
}
template <typename K, typename V>
static Dictionary to_dictionary(const HashMap<K, V> &p_inp) {
Dictionary ret;
for (const KeyValue<K, V> &E : p_inp) {
ret[E.key] = E.value;
}
return ret;
}
template <typename K, typename V>
static void set_from_dictionary(HashMap<K, V> &r_out, const Dictionary &p_inp) {
r_out.clear();
for (const KeyValue<Variant, Variant> &kv : p_inp) {
r_out[kv.key] = kv.value;
}
}
} //namespace GLTFTemplateConvert

View File

@@ -0,0 +1,165 @@
/**************************************************************************/
/* register_types.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "register_types.h"
#include "extensions/gltf_document_extension_convert_importer_mesh.h"
#include "extensions/gltf_document_extension_texture_ktx.h"
#include "extensions/gltf_document_extension_texture_webp.h"
#include "extensions/gltf_spec_gloss.h"
#include "gltf_document.h"
#include "gltf_state.h"
#include "structures/gltf_object_model_property.h"
#ifndef PHYSICS_3D_DISABLED
#include "extensions/physics/gltf_document_extension_physics.h"
#endif // PHYSICS_3D_DISABLED
#ifdef TOOLS_ENABLED
#include "editor/editor_import_blend_runner.h"
#include "editor/editor_scene_exporter_gltf_plugin.h"
#include "editor/editor_scene_importer_blend.h"
#include "editor/editor_scene_importer_gltf.h"
#include "core/config/project_settings.h"
#include "editor/editor_node.h"
#include "editor/settings/editor_settings.h"
static void _editor_init() {
Ref<EditorSceneFormatImporterGLTF> import_gltf;
import_gltf.instantiate();
ResourceImporterScene::add_scene_importer(import_gltf);
// Blend to glTF importer.
String blender_path = EDITOR_GET("filesystem/import/blender/blender_path");
if (blender_path.is_empty() && EditorSettings::get_singleton()->has_setting("filesystem/import/blender/blender3_path")) {
blender_path = EDITOR_GET("filesystem/import/blender/blender3_path");
if (!blender_path.is_empty()) {
#if defined(MACOS_ENABLED)
if (blender_path.contains(".app")) {
blender_path += "/Contents/MacOS/Blender";
} else {
blender_path += "/blender";
}
#elif defined(WINDOWS_ENABLED)
blender_path += "\\blender.exe";
#elif defined(UNIX_ENABLED)
blender_path += "/blender";
#endif
EditorSettings::get_singleton()->set("filesystem/import/blender/blender_path", blender_path);
}
EditorSettings::get_singleton()->erase("filesystem/import/blender/blender3_path");
EditorSettings::get_singleton()->save();
}
bool blend_enabled = GLOBAL_GET("filesystem/import/blender/enabled");
if (blend_enabled) {
Ref<EditorSceneFormatImporterBlend> importer;
importer.instantiate();
ResourceImporterScene::add_scene_importer(importer);
Ref<EditorFileSystemImportFormatSupportQueryBlend> blend_import_query;
blend_import_query.instantiate();
EditorFileSystem::get_singleton()->add_import_format_support_query(blend_import_query);
}
memnew(EditorImportBlendRunner);
EditorNode::get_singleton()->add_child(EditorImportBlendRunner::get_singleton());
}
#endif // TOOLS_ENABLED
#define GLTF_REGISTER_DOCUMENT_EXTENSION(m_doc_ext_class) \
Ref<m_doc_ext_class> extension_##m_doc_ext_class; \
extension_##m_doc_ext_class.instantiate(); \
GLTFDocument::register_gltf_document_extension(extension_##m_doc_ext_class);
void initialize_gltf_module(ModuleInitializationLevel p_level) {
if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
// glTF API available at runtime.
GDREGISTER_CLASS(GLTFAccessor);
GDREGISTER_CLASS(GLTFAnimation);
GDREGISTER_CLASS(GLTFBufferView);
GDREGISTER_CLASS(GLTFCamera);
GDREGISTER_CLASS(GLTFDocument);
GDREGISTER_CLASS(GLTFDocumentExtension);
GDREGISTER_CLASS(GLTFDocumentExtensionConvertImporterMesh);
GDREGISTER_CLASS(GLTFLight);
GDREGISTER_CLASS(GLTFMesh);
GDREGISTER_CLASS(GLTFNode);
GDREGISTER_CLASS(GLTFObjectModelProperty);
#ifndef PHYSICS_3D_DISABLED
GDREGISTER_CLASS(GLTFPhysicsBody);
GDREGISTER_CLASS(GLTFPhysicsShape);
#endif // PHYSICS_3D_DISABLED
GDREGISTER_CLASS(GLTFSkeleton);
GDREGISTER_CLASS(GLTFSkin);
GDREGISTER_CLASS(GLTFSpecGloss);
GDREGISTER_CLASS(GLTFState);
GDREGISTER_CLASS(GLTFTexture);
GDREGISTER_CLASS(GLTFTextureSampler);
// Register GLTFDocumentExtension classes with GLTFDocument.
#ifndef PHYSICS_3D_DISABLED
// Ensure physics is first in this list so that physics nodes are created before other nodes.
GLTF_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionPhysics);
#endif // PHYSICS_3D_DISABLED
GLTF_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionTextureKTX);
GLTF_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionTextureWebP);
bool is_editor = Engine::get_singleton()->is_editor_hint();
if (!is_editor) {
GLTF_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionConvertImporterMesh);
}
}
#ifdef TOOLS_ENABLED
if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
GDREGISTER_CLASS(EditorSceneFormatImporterGLTF);
EditorPlugins::add_by_type<SceneExporterGLTFPlugin>();
// Project settings defined here so doctool finds them.
GLOBAL_DEF_RST_BASIC("filesystem/import/blender/enabled", true);
GDREGISTER_CLASS(EditorSceneFormatImporterBlend);
// Can't (a priori) run external app on these platforms.
GLOBAL_DEF_RST("filesystem/import/blender/enabled.android", false);
GLOBAL_DEF_RST("filesystem/import/blender/enabled.web", false);
EditorNode::add_init_callback(_editor_init);
}
#endif // TOOLS_ENABLED
}
void uninitialize_gltf_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
GLTFDocument::unregister_all_gltf_document_extensions();
}

View File

@@ -0,0 +1,36 @@
/**************************************************************************/
/* register_types.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "modules/register_module_types.h"
void initialize_gltf_module(ModuleInitializationLevel p_level);
void uninitialize_gltf_module(ModuleInitializationLevel p_level);

848
modules/gltf/skin_tool.cpp Normal file
View File

@@ -0,0 +1,848 @@
/**************************************************************************/
/* skin_tool.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "skin_tool.h"
SkinNodeIndex SkinTool::_find_highest_node(Vector<Ref<GLTFNode>> &r_nodes, const Vector<GLTFNodeIndex> &p_subset) {
int highest = -1;
SkinNodeIndex best_node = -1;
for (int i = 0; i < p_subset.size(); ++i) {
const SkinNodeIndex node_i = p_subset[i];
const Ref<GLTFNode> node = r_nodes[node_i];
if (highest == -1 || node->height < highest) {
highest = node->height;
best_node = node_i;
}
}
return best_node;
}
bool SkinTool::_capture_nodes_in_skin(const Vector<Ref<GLTFNode>> &nodes, Ref<GLTFSkin> p_skin, const SkinNodeIndex p_node_index) {
bool found_joint = false;
Ref<GLTFNode> current_node = nodes[p_node_index];
for (int i = 0; i < current_node->children.size(); ++i) {
found_joint |= _capture_nodes_in_skin(nodes, p_skin, current_node->children[i]);
}
if (found_joint) {
// Mark it if we happen to find another skins joint...
if (current_node->joint && !p_skin->joints.has(p_node_index)) {
p_skin->joints.push_back(p_node_index);
} else if (!p_skin->non_joints.has(p_node_index)) {
p_skin->non_joints.push_back(p_node_index);
}
}
if (p_skin->joints.find(p_node_index) > 0) {
return true;
}
return false;
}
void SkinTool::_capture_nodes_for_multirooted_skin(Vector<Ref<GLTFNode>> &r_nodes, Ref<GLTFSkin> p_skin) {
DisjointSet<SkinNodeIndex> disjoint_set;
for (int i = 0; i < p_skin->joints.size(); ++i) {
const SkinNodeIndex node_index = p_skin->joints[i];
const SkinNodeIndex parent = r_nodes[node_index]->parent;
disjoint_set.insert(node_index);
if (p_skin->joints.has(parent)) {
disjoint_set.create_union(parent, node_index);
}
}
Vector<SkinNodeIndex> roots;
disjoint_set.get_representatives(roots);
if (roots.size() <= 1) {
return;
}
int maxHeight = -1;
// Determine the max height rooted tree
for (int i = 0; i < roots.size(); ++i) {
const SkinNodeIndex root = roots[i];
if (maxHeight == -1 || r_nodes[root]->height < maxHeight) {
maxHeight = r_nodes[root]->height;
}
}
// Go up the tree till all of the multiple roots of the skin are at the same hierarchy level.
// This sucks, but 99% of all game engines (not just Godot) would have this same issue.
for (int i = 0; i < roots.size(); ++i) {
SkinNodeIndex current_node = roots[i];
while (r_nodes[current_node]->height > maxHeight) {
SkinNodeIndex parent = r_nodes[current_node]->parent;
if (r_nodes[parent]->joint && !p_skin->joints.has(parent)) {
p_skin->joints.push_back(parent);
} else if (!p_skin->non_joints.has(parent)) {
p_skin->non_joints.push_back(parent);
}
current_node = parent;
}
// replace the roots
roots.write[i] = current_node;
}
// Climb up the tree until they all have the same parent
bool all_same;
do {
all_same = true;
const SkinNodeIndex first_parent = r_nodes[roots[0]]->parent;
for (int i = 1; i < roots.size(); ++i) {
all_same &= (first_parent == r_nodes[roots[i]]->parent);
}
if (!all_same) {
for (int i = 0; i < roots.size(); ++i) {
const SkinNodeIndex current_node = roots[i];
const SkinNodeIndex parent = r_nodes[current_node]->parent;
if (r_nodes[parent]->joint && !p_skin->joints.has(parent)) {
p_skin->joints.push_back(parent);
} else if (!p_skin->non_joints.has(parent)) {
p_skin->non_joints.push_back(parent);
}
roots.write[i] = parent;
}
}
} while (!all_same);
}
Error SkinTool::_expand_skin(Vector<Ref<GLTFNode>> &r_nodes, Ref<GLTFSkin> p_skin) {
_capture_nodes_for_multirooted_skin(r_nodes, p_skin);
// Grab all nodes that lay in between skin joints/nodes
DisjointSet<GLTFNodeIndex> disjoint_set;
Vector<SkinNodeIndex> all_skin_nodes;
all_skin_nodes.append_array(p_skin->joints);
all_skin_nodes.append_array(p_skin->non_joints);
for (int i = 0; i < all_skin_nodes.size(); ++i) {
const SkinNodeIndex node_index = all_skin_nodes[i];
const SkinNodeIndex parent = r_nodes[node_index]->parent;
disjoint_set.insert(node_index);
if (all_skin_nodes.has(parent)) {
disjoint_set.create_union(parent, node_index);
}
}
Vector<SkinNodeIndex> out_owners;
disjoint_set.get_representatives(out_owners);
Vector<SkinNodeIndex> out_roots;
for (int i = 0; i < out_owners.size(); ++i) {
Vector<SkinNodeIndex> set;
disjoint_set.get_members(set, out_owners[i]);
const SkinNodeIndex root = _find_highest_node(r_nodes, set);
ERR_FAIL_COND_V(root < 0, FAILED);
out_roots.push_back(root);
}
out_roots.sort();
for (int i = 0; i < out_roots.size(); ++i) {
_capture_nodes_in_skin(r_nodes, p_skin, out_roots[i]);
}
p_skin->roots = out_roots;
return OK;
}
Error SkinTool::_verify_skin(Vector<Ref<GLTFNode>> &r_nodes, Ref<GLTFSkin> p_skin) {
// This may seem duplicated from expand_skins, but this is really a sanity check! (so it kinda is)
// In case additional interpolating logic is added to the skins, this will help ensure that you
// do not cause it to self implode into a fiery blaze
// We are going to re-calculate the root nodes and compare them to the ones saved in the skin,
// then ensure the multiple trees (if they exist) are on the same sublevel
// Grab all nodes that lay in between skin joints/nodes
DisjointSet<GLTFNodeIndex> disjoint_set;
Vector<SkinNodeIndex> all_skin_nodes;
all_skin_nodes.append_array(p_skin->joints);
all_skin_nodes.append_array(p_skin->non_joints);
for (int i = 0; i < all_skin_nodes.size(); ++i) {
const SkinNodeIndex node_index = all_skin_nodes[i];
const SkinNodeIndex parent = r_nodes[node_index]->parent;
disjoint_set.insert(node_index);
if (all_skin_nodes.has(parent)) {
disjoint_set.create_union(parent, node_index);
}
}
Vector<SkinNodeIndex> out_owners;
disjoint_set.get_representatives(out_owners);
Vector<SkinNodeIndex> out_roots;
for (int i = 0; i < out_owners.size(); ++i) {
Vector<SkinNodeIndex> set;
disjoint_set.get_members(set, out_owners[i]);
const SkinNodeIndex root = _find_highest_node(r_nodes, set);
ERR_FAIL_COND_V(root < 0, FAILED);
out_roots.push_back(root);
}
out_roots.sort();
ERR_FAIL_COND_V(out_roots.is_empty(), FAILED);
// Make sure the roots are the exact same (they better be)
ERR_FAIL_COND_V(out_roots.size() != p_skin->roots.size(), FAILED);
for (int i = 0; i < out_roots.size(); ++i) {
ERR_FAIL_COND_V(out_roots[i] != p_skin->roots[i], FAILED);
}
// Single rooted skin? Perfectly ok!
if (out_roots.size() == 1) {
return OK;
}
// Make sure all parents of a multi-rooted skin are the SAME
const SkinNodeIndex parent = r_nodes[out_roots[0]]->parent;
for (int i = 1; i < out_roots.size(); ++i) {
if (r_nodes[out_roots[i]]->parent != parent) {
return FAILED;
}
}
return OK;
}
void SkinTool::_recurse_children(
Vector<Ref<GLTFNode>> &nodes,
const SkinNodeIndex p_node_index,
RBSet<GLTFNodeIndex> &p_all_skin_nodes,
HashSet<GLTFNodeIndex> &p_child_visited_set) {
if (p_child_visited_set.has(p_node_index)) {
return;
}
p_child_visited_set.insert(p_node_index);
Ref<GLTFNode> current_node = nodes[p_node_index];
for (int i = 0; i < current_node->children.size(); ++i) {
_recurse_children(nodes, current_node->children[i], p_all_skin_nodes, p_child_visited_set);
}
// Continue to use 'current_node' for clarity and direct access.
if (current_node->skin < 0 || current_node->mesh < 0 || !current_node->children.is_empty()) {
p_all_skin_nodes.insert(p_node_index);
}
}
void SkinTool::_check_if_parent_needs_to_become_joint(const Vector<Ref<GLTFNode>> &p_all_nodes, const Vector<GLTFNodeIndex> &p_skeleton_node_indices, const Ref<GLTFNode> &p_gltf_node, Vector<GLTFNodeIndex> &r_non_joint_indices) {
const GLTFNodeIndex parent_index = p_gltf_node->parent;
if (parent_index >= 0) {
const Ref<GLTFNode> &parent = p_all_nodes[parent_index];
if (!parent->joint && p_skeleton_node_indices.has(parent_index) && !r_non_joint_indices.has(parent_index)) {
_check_if_parent_needs_to_become_joint(p_all_nodes, p_skeleton_node_indices, parent, r_non_joint_indices);
r_non_joint_indices.push_back(parent_index);
}
}
}
Error SkinTool::_determine_skeletons(
Vector<Ref<GLTFSkin>> &skins,
Vector<Ref<GLTFNode>> &nodes,
Vector<Ref<GLTFSkeleton>> &skeletons,
const Vector<GLTFNodeIndex> &p_single_skeleton_roots,
bool p_turn_non_joint_descendants_into_bones) {
if (!p_single_skeleton_roots.is_empty()) {
Ref<GLTFSkin> skin;
skin.instantiate();
skin->set_name("godot_single_skeleton_root");
for (GLTFNodeIndex i = 0; i < p_single_skeleton_roots.size(); i++) {
skin->joints.push_back(p_single_skeleton_roots[i]);
}
skins.push_back(skin);
}
// Using a disjoint set, we are going to potentially combine all skins that are actually branches
// of a main skeleton, or treat skins defining the same set of nodes as ONE skeleton.
// This is another unclear issue caused by the current glTF specification.
DisjointSet<GLTFNodeIndex> skeleton_sets;
for (GLTFSkinIndex skin_i = 0; skin_i < skins.size(); ++skin_i) {
const Ref<GLTFSkin> skin = skins[skin_i];
ERR_CONTINUE(skin.is_null());
HashSet<GLTFNodeIndex> child_visited_set;
RBSet<GLTFNodeIndex> all_skin_nodes;
for (int i = 0; i < skin->joints.size(); ++i) {
all_skin_nodes.insert(skin->joints[i]);
SkinTool::_recurse_children(nodes, skin->joints[i], all_skin_nodes, child_visited_set);
}
for (int i = 0; i < skin->non_joints.size(); ++i) {
all_skin_nodes.insert(skin->non_joints[i]);
SkinTool::_recurse_children(nodes, skin->non_joints[i], all_skin_nodes, child_visited_set);
}
for (GLTFNodeIndex node_index : all_skin_nodes) {
const GLTFNodeIndex parent = nodes[node_index]->parent;
skeleton_sets.insert(node_index);
if (all_skin_nodes.has(parent)) {
skeleton_sets.create_union(parent, node_index);
}
}
// We are going to connect the separate skin subtrees in each skin together
// so that the final roots are entire sets of valid skin trees
for (int i = 1; i < skin->roots.size(); ++i) {
skeleton_sets.create_union(skin->roots[0], skin->roots[i]);
}
}
{ // attempt to joint all touching subsets (siblings/parent are part of another skin)
Vector<SkinNodeIndex> groups_representatives;
skeleton_sets.get_representatives(groups_representatives);
Vector<SkinNodeIndex> highest_group_members;
Vector<Vector<SkinNodeIndex>> groups;
for (int i = 0; i < groups_representatives.size(); ++i) {
Vector<SkinNodeIndex> group;
skeleton_sets.get_members(group, groups_representatives[i]);
highest_group_members.push_back(SkinTool::_find_highest_node(nodes, group));
groups.push_back(group);
}
for (int i = 0; i < highest_group_members.size(); ++i) {
const SkinNodeIndex node_i = highest_group_members[i];
// Attach any siblings together (this needs to be done n^2/2 times)
for (int j = i + 1; j < highest_group_members.size(); ++j) {
const SkinNodeIndex node_j = highest_group_members[j];
// Even if they are siblings under the root! :)
if (nodes[node_i]->parent == nodes[node_j]->parent) {
skeleton_sets.create_union(node_i, node_j);
}
}
// Attach any parenting going on together (we need to do this n^2 times)
const SkinNodeIndex node_i_parent = nodes[node_i]->parent;
if (node_i_parent >= 0) {
for (int j = 0; j < groups.size() && i != j; ++j) {
const Vector<SkinNodeIndex> &group = groups[j];
if (group.has(node_i_parent)) {
const SkinNodeIndex node_j = highest_group_members[j];
skeleton_sets.create_union(node_i, node_j);
}
}
}
}
}
// At this point, the skeleton groups should be finalized
Vector<SkinNodeIndex> skeleton_owners;
skeleton_sets.get_representatives(skeleton_owners);
// Mark all the skins actual skeletons, after we have merged them
for (SkinSkeletonIndex skel_i = 0; skel_i < skeleton_owners.size(); ++skel_i) {
const SkinNodeIndex skeleton_owner = skeleton_owners[skel_i];
Ref<GLTFSkeleton> skeleton;
skeleton.instantiate();
Vector<SkinNodeIndex> skeleton_nodes;
skeleton_sets.get_members(skeleton_nodes, skeleton_owner);
for (GLTFSkinIndex skin_i = 0; skin_i < skins.size(); ++skin_i) {
Ref<GLTFSkin> skin = skins.write[skin_i];
// If any of the skeletons nodes exist in a skin, that skin now maps to the skeleton
for (int i = 0; i < skeleton_nodes.size(); ++i) {
SkinNodeIndex skel_node_i = skeleton_nodes[i];
if (skin->joints.has(skel_node_i) || skin->non_joints.has(skel_node_i)) {
skin->skeleton = skel_i;
continue;
}
}
}
// The nodes placed into `non_joints` will be passed to `_reparent_non_joint_skeleton_subtrees`
// which will add them to the skeleton and set `node->joint` to true.
Vector<SkinNodeIndex> non_joints;
for (int i = 0; i < skeleton_nodes.size(); ++i) {
const SkinNodeIndex node_i = skeleton_nodes[i];
Ref<GLTFNode> node = nodes[node_i];
if (node->joint) {
if (!p_turn_non_joint_descendants_into_bones) {
// If a joint node has non-joint parents, we need to make them joints as well.
// For example, if A/B/C/D, and A/B and D are joints, then we need to make C a joint as well.
// This is required to handle the "skinD" example in `Animation_Skin_09.gltf` from the glTF-Asset-Generator:
// https://github.com/KhronosGroup/glTF-Asset-Generator/blob/master/Output/Positive/Animation_Skin
_check_if_parent_needs_to_become_joint(nodes, skeleton_nodes, node, non_joints);
}
skeleton->joints.push_back(node_i);
} else if (p_turn_non_joint_descendants_into_bones) {
non_joints.push_back(node_i);
}
}
skeletons.push_back(skeleton);
SkinTool::_reparent_non_joint_skeleton_subtrees(nodes, skeletons.write[skel_i], non_joints);
}
for (SkinSkeletonIndex skel_i = 0; skel_i < skeletons.size(); ++skel_i) {
Ref<GLTFSkeleton> skeleton = skeletons.write[skel_i];
for (int i = 0; i < skeleton->joints.size(); ++i) {
const SkinNodeIndex node_i = skeleton->joints[i];
Ref<GLTFNode> node = nodes[node_i];
ERR_FAIL_COND_V(!node->joint, ERR_PARSE_ERROR);
ERR_FAIL_COND_V(node->skeleton >= 0, ERR_PARSE_ERROR);
node->skeleton = skel_i;
}
ERR_FAIL_COND_V(SkinTool::_determine_skeleton_roots(nodes, skeletons, skel_i), ERR_PARSE_ERROR);
}
return OK;
}
Error SkinTool::_reparent_non_joint_skeleton_subtrees(
Vector<Ref<GLTFNode>> &nodes,
Ref<GLTFSkeleton> p_skeleton,
const Vector<SkinNodeIndex> &p_non_joints) {
DisjointSet<GLTFNodeIndex> subtree_set;
// Populate the disjoint set with ONLY non joints that are in the skeleton hierarchy (non_joints vector)
// This way we can find any joints that lie in between joints, as the current glTF specification
// mentions nothing about non-joints being in between joints of the same skin. Hopefully one day we
// can remove this code.
// skinD depicted here explains this issue:
// https://github.com/KhronosGroup/glTF-Asset-Generator/blob/master/Output/Positive/Animation_Skin
for (int i = 0; i < p_non_joints.size(); ++i) {
const SkinNodeIndex node_i = p_non_joints[i];
subtree_set.insert(node_i);
const SkinNodeIndex parent_i = nodes[node_i]->parent;
if (parent_i >= 0 && p_non_joints.has(parent_i) && !nodes[parent_i]->joint) {
subtree_set.create_union(parent_i, node_i);
}
}
// Find all the non joint subtrees and re-parent them to a new "fake" joint
Vector<SkinNodeIndex> non_joint_subtree_roots;
subtree_set.get_representatives(non_joint_subtree_roots);
for (int root_i = 0; root_i < non_joint_subtree_roots.size(); ++root_i) {
const SkinNodeIndex subtree_root = non_joint_subtree_roots[root_i];
Vector<SkinNodeIndex> subtree_nodes;
subtree_set.get_members(subtree_nodes, subtree_root);
for (int subtree_i = 0; subtree_i < subtree_nodes.size(); ++subtree_i) {
Ref<GLTFNode> node = nodes[subtree_nodes[subtree_i]];
node->joint = true;
// Add the joint to the skeletons joints
p_skeleton->joints.push_back(subtree_nodes[subtree_i]);
}
}
return OK;
}
Error SkinTool::_determine_skeleton_roots(
Vector<Ref<GLTFNode>> &nodes,
Vector<Ref<GLTFSkeleton>> &skeletons,
const SkinSkeletonIndex p_skel_i) {
DisjointSet<GLTFNodeIndex> disjoint_set;
for (SkinNodeIndex i = 0; i < nodes.size(); ++i) {
const Ref<GLTFNode> node = nodes[i];
if (node->skeleton != p_skel_i) {
continue;
}
disjoint_set.insert(i);
if (node->parent >= 0 && nodes[node->parent]->skeleton == p_skel_i) {
disjoint_set.create_union(node->parent, i);
}
}
Ref<GLTFSkeleton> skeleton = skeletons.write[p_skel_i];
Vector<SkinNodeIndex> representatives;
disjoint_set.get_representatives(representatives);
Vector<SkinNodeIndex> roots;
for (int i = 0; i < representatives.size(); ++i) {
Vector<SkinNodeIndex> set;
disjoint_set.get_members(set, representatives[i]);
const SkinNodeIndex root = _find_highest_node(nodes, set);
ERR_FAIL_COND_V(root < 0, FAILED);
roots.push_back(root);
}
roots.sort();
skeleton->roots = roots;
if (roots.is_empty()) {
return FAILED;
} else if (roots.size() == 1) {
return OK;
}
// Check that the subtrees have the same parent root
const SkinNodeIndex parent = nodes[roots[0]]->parent;
for (int i = 1; i < roots.size(); ++i) {
if (nodes[roots[i]]->parent != parent) {
return FAILED;
}
}
return OK;
}
Error SkinTool::_create_skeletons(
HashSet<String> &unique_names,
Vector<Ref<GLTFSkin>> &skins,
Vector<Ref<GLTFNode>> &nodes,
HashMap<ObjectID, GLTFSkeletonIndex> &skeleton3d_to_gltf_skeleton,
Vector<Ref<GLTFSkeleton>> &skeletons,
HashMap<GLTFNodeIndex, Node *> &scene_nodes,
int p_naming_version) {
// This is the syntax to duplicate a Godot HashSet.
HashSet<String> unique_node_names(unique_names);
for (SkinSkeletonIndex skel_i = 0; skel_i < skeletons.size(); ++skel_i) {
Ref<GLTFSkeleton> gltf_skeleton = skeletons.write[skel_i];
HashSet<String> skel_unique_names(unique_node_names);
Skeleton3D *skeleton = memnew(Skeleton3D);
gltf_skeleton->godot_skeleton = skeleton;
skeleton3d_to_gltf_skeleton[skeleton->get_instance_id()] = skel_i;
// Make a unique name, no gltf node represents this skeleton
skeleton->set_name("Skeleton3D");
List<GLTFNodeIndex> bones;
for (int i = 0; i < gltf_skeleton->roots.size(); ++i) {
bones.push_back(gltf_skeleton->roots[i]);
}
// Make the skeleton creation deterministic by going through the roots in
// a sorted order, and DEPTH FIRST
bones.sort();
while (!bones.is_empty()) {
const SkinNodeIndex node_i = bones.front()->get();
bones.pop_front();
Ref<GLTFNode> node = nodes[node_i];
ERR_FAIL_COND_V(node->skeleton != skel_i, FAILED);
{ // Add all child nodes to the stack (deterministically)
Vector<SkinNodeIndex> child_nodes;
for (int i = 0; i < node->children.size(); ++i) {
const SkinNodeIndex child_i = node->children[i];
if (nodes[child_i]->skeleton == skel_i) {
child_nodes.push_back(child_i);
}
}
// Depth first insertion
child_nodes.sort();
for (int i = child_nodes.size() - 1; i >= 0; --i) {
bones.push_front(child_nodes[i]);
}
}
const int bone_index = skeleton->get_bone_count();
if (node->get_name().is_empty()) {
node->set_name("bone");
}
if (p_naming_version < 2) {
node->set_name(_gen_unique_bone_name(unique_names, node->get_name()));
} else {
// Make sure the bone name is unique in the skeleton and unique compared
// to scene nodes, but bone names may be duplicated between skeletons.
// Example: Two skeletons with a "Head" bone should not have one become "Head_2".
const String unique_bone_name = _gen_unique_bone_name(skel_unique_names, node->get_name());
unique_names.insert(unique_bone_name);
node->set_name(unique_bone_name);
}
skeleton->add_bone(node->get_name());
Transform3D rest_transform = node->get_additional_data("GODOT_rest_transform");
skeleton->set_bone_rest(bone_index, rest_transform);
skeleton->set_bone_pose_position(bone_index, node->transform.origin);
skeleton->set_bone_pose_rotation(bone_index, node->transform.basis.get_rotation_quaternion());
skeleton->set_bone_pose_scale(bone_index, node->transform.basis.get_scale());
// Store bone-level GLTF extras in skeleton per bone meta.
if (node->has_meta("extras")) {
skeleton->set_bone_meta(bone_index, "extras", node->get_meta("extras"));
}
if (node->parent >= 0 && nodes[node->parent]->skeleton == skel_i) {
const int bone_parent = skeleton->find_bone(nodes[node->parent]->get_name());
ERR_FAIL_COND_V(bone_parent < 0, FAILED);
skeleton->set_bone_parent(bone_index, skeleton->find_bone(nodes[node->parent]->get_name()));
}
scene_nodes.insert(node_i, skeleton);
}
}
ERR_FAIL_COND_V(_map_skin_joints_indices_to_skeleton_bone_indices(skins, skeletons, nodes), ERR_PARSE_ERROR);
return OK;
}
Error SkinTool::_map_skin_joints_indices_to_skeleton_bone_indices(
Vector<Ref<GLTFSkin>> &skins,
Vector<Ref<GLTFSkeleton>> &skeletons,
Vector<Ref<GLTFNode>> &nodes) {
for (GLTFSkinIndex skin_i = 0; skin_i < skins.size(); ++skin_i) {
Ref<GLTFSkin> skin = skins.write[skin_i];
ERR_CONTINUE(skin.is_null());
Ref<GLTFSkeleton> skeleton = skeletons[skin->skeleton];
for (int joint_index = 0; joint_index < skin->joints_original.size(); ++joint_index) {
const SkinNodeIndex node_i = skin->joints_original[joint_index];
const Ref<GLTFNode> node = nodes[node_i];
const int bone_index = skeleton->godot_skeleton->find_bone(node->get_name());
ERR_FAIL_COND_V(bone_index < 0, FAILED);
skin->joint_i_to_bone_i.insert(joint_index, bone_index);
}
}
return OK;
}
Error SkinTool::_create_skins(Vector<Ref<GLTFSkin>> &skins, Vector<Ref<GLTFNode>> &nodes, bool use_named_skin_binds, HashSet<String> &unique_names) {
for (GLTFSkinIndex skin_i = 0; skin_i < skins.size(); ++skin_i) {
Ref<GLTFSkin> gltf_skin = skins.write[skin_i];
ERR_CONTINUE(gltf_skin.is_null());
Ref<Skin> skin;
skin.instantiate();
// Some skins don't have IBM's! What absolute monsters!
const bool has_ibms = !gltf_skin->inverse_binds.is_empty();
for (int joint_i = 0; joint_i < gltf_skin->joints_original.size(); ++joint_i) {
SkinNodeIndex node = gltf_skin->joints_original[joint_i];
String bone_name = nodes[node]->get_name();
Transform3D xform;
if (has_ibms) {
xform = gltf_skin->inverse_binds[joint_i];
}
if (use_named_skin_binds) {
skin->add_named_bind(bone_name, xform);
} else {
int32_t bone_i = gltf_skin->joint_i_to_bone_i[joint_i];
skin->add_bind(bone_i, xform);
}
}
gltf_skin->godot_skin = skin;
}
// Purge the duplicates!
_remove_duplicate_skins(skins);
// Create unique names now, after removing duplicates
for (GLTFSkinIndex skin_i = 0; skin_i < skins.size(); ++skin_i) {
ERR_CONTINUE(skins.get(skin_i).is_null());
Ref<Skin> skin = skins.write[skin_i]->godot_skin;
ERR_CONTINUE(skin.is_null());
if (skin->get_name().is_empty()) {
// Make a unique name, no node represents this skin
skin->set_name(_gen_unique_name(unique_names, "Skin"));
}
}
return OK;
}
// FIXME: Duplicated from FBXDocument, very similar code in GLTFDocument too,
// and even below in this class for bone names.
String SkinTool::_gen_unique_name(HashSet<String> &unique_names, const String &p_name) {
const String s_name = p_name.validate_node_name();
String u_name;
int index = 1;
while (true) {
u_name = s_name;
if (index > 1) {
u_name += itos(index);
}
if (!unique_names.has(u_name)) {
break;
}
index++;
}
unique_names.insert(u_name);
return u_name;
}
bool SkinTool::_skins_are_same(const Ref<Skin> p_skin_a, const Ref<Skin> p_skin_b) {
if (p_skin_a->get_bind_count() != p_skin_b->get_bind_count()) {
return false;
}
for (int i = 0; i < p_skin_a->get_bind_count(); ++i) {
if (p_skin_a->get_bind_bone(i) != p_skin_b->get_bind_bone(i)) {
return false;
}
if (p_skin_a->get_bind_name(i) != p_skin_b->get_bind_name(i)) {
return false;
}
Transform3D a_xform = p_skin_a->get_bind_pose(i);
Transform3D b_xform = p_skin_b->get_bind_pose(i);
if (a_xform != b_xform) {
return false;
}
}
return true;
}
void SkinTool::_remove_duplicate_skins(Vector<Ref<GLTFSkin>> &r_skins) {
for (int i = 0; i < r_skins.size(); ++i) {
for (int j = i + 1; j < r_skins.size(); ++j) {
const Ref<Skin> skin_i = r_skins[i]->godot_skin;
const Ref<Skin> skin_j = r_skins[j]->godot_skin;
if (_skins_are_same(skin_i, skin_j)) {
// replace it and delete the old
r_skins.write[j]->godot_skin = skin_i;
}
}
}
}
String SkinTool::_gen_unique_bone_name(HashSet<String> &r_unique_names, const String &p_name) {
String s_name = _sanitize_bone_name(p_name);
if (s_name.is_empty()) {
s_name = "bone";
}
String u_name;
int index = 1;
while (true) {
u_name = s_name;
if (index > 1) {
u_name += "_" + itos(index);
}
if (!r_unique_names.has(u_name)) {
break;
}
index++;
}
r_unique_names.insert(u_name);
return u_name;
}
Error SkinTool::_asset_parse_skins(
const Vector<SkinNodeIndex> &input_skin_indices,
const Vector<Ref<GLTFSkin>> &input_skins,
const Vector<Ref<GLTFNode>> &input_nodes,
Vector<SkinNodeIndex> &output_skin_indices,
Vector<Ref<GLTFSkin>> &output_skins,
HashMap<GLTFNodeIndex, bool> &joint_mapping) {
output_skin_indices.clear();
output_skins.clear();
joint_mapping.clear();
for (int i = 0; i < input_skin_indices.size(); ++i) {
SkinNodeIndex skin_index = input_skin_indices[i];
if (skin_index >= 0 && skin_index < input_skins.size()) {
output_skin_indices.push_back(skin_index);
output_skins.push_back(input_skins[skin_index]);
Ref<GLTFSkin> skin = input_skins[skin_index];
Vector<SkinNodeIndex> skin_joints = skin->get_joints();
for (int j = 0; j < skin_joints.size(); ++j) {
SkinNodeIndex joint_index = skin_joints[j];
joint_mapping[joint_index] = true;
}
}
}
return OK;
}
String SkinTool::_sanitize_bone_name(const String &p_name) {
String bone_name = p_name;
bone_name = bone_name.replace_chars(":/", '_');
return bone_name;
}

103
modules/gltf/skin_tool.h Normal file
View File

@@ -0,0 +1,103 @@
/**************************************************************************/
/* skin_tool.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "gltf_defines.h"
#include "structures/gltf_node.h"
#include "structures/gltf_skeleton.h"
#include "structures/gltf_skin.h"
#include "core/math/disjoint_set.h"
#include "core/templates/rb_set.h"
using SkinNodeIndex = int;
using SkinSkeletonIndex = int;
class SkinTool {
public:
static String _sanitize_bone_name(const String &p_name);
static String _gen_unique_bone_name(HashSet<String> &r_unique_names, const String &p_name);
static SkinNodeIndex _find_highest_node(Vector<Ref<GLTFNode>> &r_nodes, const Vector<SkinNodeIndex> &p_subset);
static bool _capture_nodes_in_skin(const Vector<Ref<GLTFNode>> &p_nodes, Ref<GLTFSkin> p_skin, const SkinNodeIndex p_node_index);
static void _capture_nodes_for_multirooted_skin(Vector<Ref<GLTFNode>> &r_nodes, Ref<GLTFSkin> p_skin);
static void _recurse_children(
Vector<Ref<GLTFNode>> &r_nodes,
const SkinNodeIndex p_node_index,
RBSet<SkinNodeIndex> &r_all_skin_nodes,
HashSet<SkinNodeIndex> &r_child_visited_set);
static Error _reparent_non_joint_skeleton_subtrees(
Vector<Ref<GLTFNode>> &r_nodes,
Ref<GLTFSkeleton> p_skeleton,
const Vector<SkinNodeIndex> &p_non_joints);
static Error _determine_skeleton_roots(
Vector<Ref<GLTFNode>> &r_nodes,
Vector<Ref<GLTFSkeleton>> &r_skeletons,
const SkinSkeletonIndex p_skel_i);
static Error _map_skin_joints_indices_to_skeleton_bone_indices(
Vector<Ref<GLTFSkin>> &r_skins,
Vector<Ref<GLTFSkeleton>> &r_skeletons,
Vector<Ref<GLTFNode>> &r_nodes);
static String _gen_unique_name(HashSet<String> &unique_names, const String &p_name);
static bool _skins_are_same(const Ref<Skin> p_skin_a, const Ref<Skin> p_skin_b);
static void _remove_duplicate_skins(Vector<Ref<GLTFSkin>> &r_skins);
static void _check_if_parent_needs_to_become_joint(
const Vector<Ref<GLTFNode>> &p_all_nodes,
const Vector<GLTFNodeIndex> &p_skeleton_node_indices,
const Ref<GLTFNode> &p_gltf_node,
Vector<GLTFNodeIndex> &r_non_joint_indices);
public:
static Error _expand_skin(Vector<Ref<GLTFNode>> &r_nodes, Ref<GLTFSkin> p_skin);
static Error _verify_skin(Vector<Ref<GLTFNode>> &r_nodes, Ref<GLTFSkin> p_skin);
static Error _asset_parse_skins(
const Vector<SkinNodeIndex> &p_input_skin_indices,
const Vector<Ref<GLTFSkin>> &p_input_skins,
const Vector<Ref<GLTFNode>> &p_input_nodes,
Vector<SkinNodeIndex> &r_output_skin_indices,
Vector<Ref<GLTFSkin>> &r_output_skins,
HashMap<GLTFNodeIndex, bool> &r_joint_mapping);
static Error _determine_skeletons(
Vector<Ref<GLTFSkin>> &r_skins,
Vector<Ref<GLTFNode>> &r_nodes,
Vector<Ref<GLTFSkeleton>> &r_skeletons,
const Vector<GLTFNodeIndex> &p_single_skeleton_root,
bool p_turn_non_joint_descendants_into_bones);
static Error _create_skeletons(
HashSet<String> &r_unique_names,
Vector<Ref<GLTFSkin>> &r_skins,
Vector<Ref<GLTFNode>> &r_nodes,
HashMap<ObjectID, GLTFSkeletonIndex> &r_skeleton3d_to_fbx_skeleton,
Vector<Ref<GLTFSkeleton>> &r_skeletons,
HashMap<GLTFNodeIndex, Node *> &r_scene_nodes,
int p_naming_version);
static Error _create_skins(Vector<Ref<GLTFSkin>> &skins, Vector<Ref<GLTFNode>> &nodes, bool use_named_skin_binds, HashSet<String> &unique_names);
};

View File

@@ -0,0 +1,124 @@
/**************************************************************************/
/* gltf_accessor.compat.inc */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef DISABLE_DEPRECATED
// 32-bit versions for compatibility.
GLTFBufferViewIndex GLTFAccessor::_get_buffer_view_bind_compat_106220() {
return get_buffer_view();
}
int GLTFAccessor::_get_byte_offset_bind_compat_106220() {
return get_byte_offset();
}
int GLTFAccessor::_get_component_type_bind_compat_106220() {
return (int)get_component_type();
}
void GLTFAccessor::_set_component_type_bind_compat_106220(int p_component_type) {
set_component_type((GLTFComponentType)p_component_type);
}
bool GLTFAccessor::_get_normalized_bind_compat_106220() {
return get_normalized();
}
int GLTFAccessor::_get_count_bind_compat_106220() {
return get_count();
}
GLTFAccessor::GLTFAccessorType GLTFAccessor::_get_accessor_type_bind_compat_106220() {
return get_accessor_type();
}
int GLTFAccessor::_get_type_bind_compat_106220() {
return (int)get_accessor_type();
}
Vector<double> GLTFAccessor::_get_min_bind_compat_106220() {
return get_min();
}
Vector<double> GLTFAccessor::_get_max_bind_compat_106220() {
return get_max();
}
int GLTFAccessor::_get_sparse_count_bind_compat_106220() {
return get_sparse_count();
}
int GLTFAccessor::_get_sparse_indices_buffer_view_bind_compat_106220() {
return get_sparse_indices_buffer_view();
}
int GLTFAccessor::_get_sparse_indices_byte_offset_bind_compat_106220() {
return get_sparse_indices_byte_offset();
}
int GLTFAccessor::_get_sparse_indices_component_type_bind_compat_106220() {
return (int)get_sparse_indices_component_type();
}
void GLTFAccessor::_set_sparse_indices_component_type_bind_compat_106220(int p_sparse_indices_component_type) {
set_sparse_indices_component_type((GLTFComponentType)p_sparse_indices_component_type);
}
int GLTFAccessor::_get_sparse_values_buffer_view_bind_compat_106220() {
return get_sparse_values_buffer_view();
}
int GLTFAccessor::_get_sparse_values_byte_offset_bind_compat_106220() {
return get_sparse_values_byte_offset();
}
void GLTFAccessor::_bind_compatibility_methods() {
// 32-bit versions for compatibility.
ClassDB::bind_compatibility_method(D_METHOD("get_buffer_view"), &GLTFAccessor::_get_buffer_view_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_byte_offset"), &GLTFAccessor::_get_byte_offset_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_component_type"), &GLTFAccessor::_get_component_type_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("set_component_type", "component_type"), &GLTFAccessor::_set_component_type_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_normalized"), &GLTFAccessor::_get_normalized_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_count"), &GLTFAccessor::_get_count_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_accessor_type"), &GLTFAccessor::_get_accessor_type_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_type"), &GLTFAccessor::_get_type_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_min"), &GLTFAccessor::_get_min_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_max"), &GLTFAccessor::_get_max_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_sparse_count"), &GLTFAccessor::_get_sparse_count_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_sparse_indices_buffer_view"), &GLTFAccessor::_get_sparse_indices_buffer_view_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_sparse_indices_byte_offset"), &GLTFAccessor::_get_sparse_indices_byte_offset_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_sparse_indices_component_type"), &GLTFAccessor::_get_sparse_indices_component_type_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("set_sparse_indices_component_type", "sparse_indices_component_type"), &GLTFAccessor::_set_sparse_indices_component_type_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_sparse_values_buffer_view"), &GLTFAccessor::_get_sparse_values_buffer_view_bind_compat_106220);
ClassDB::bind_compatibility_method(D_METHOD("get_sparse_values_byte_offset"), &GLTFAccessor::_get_sparse_values_byte_offset_bind_compat_106220);
}
#endif // DISABLE_DEPRECATED

View File

@@ -0,0 +1,222 @@
/**************************************************************************/
/* gltf_accessor.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_accessor.h"
#include "gltf_accessor.compat.inc"
void GLTFAccessor::_bind_methods() {
BIND_ENUM_CONSTANT(TYPE_SCALAR);
BIND_ENUM_CONSTANT(TYPE_VEC2);
BIND_ENUM_CONSTANT(TYPE_VEC3);
BIND_ENUM_CONSTANT(TYPE_VEC4);
BIND_ENUM_CONSTANT(TYPE_MAT2);
BIND_ENUM_CONSTANT(TYPE_MAT3);
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_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);
ClassDB::bind_method(D_METHOD("get_byte_offset"), &GLTFAccessor::get_byte_offset);
ClassDB::bind_method(D_METHOD("set_byte_offset", "byte_offset"), &GLTFAccessor::set_byte_offset);
ClassDB::bind_method(D_METHOD("get_component_type"), &GLTFAccessor::get_component_type);
ClassDB::bind_method(D_METHOD("set_component_type", "component_type"), &GLTFAccessor::set_component_type);
ClassDB::bind_method(D_METHOD("get_normalized"), &GLTFAccessor::get_normalized);
ClassDB::bind_method(D_METHOD("set_normalized", "normalized"), &GLTFAccessor::set_normalized);
ClassDB::bind_method(D_METHOD("get_count"), &GLTFAccessor::get_count);
ClassDB::bind_method(D_METHOD("set_count", "count"), &GLTFAccessor::set_count);
ClassDB::bind_method(D_METHOD("get_accessor_type"), &GLTFAccessor::get_accessor_type);
ClassDB::bind_method(D_METHOD("set_accessor_type", "accessor_type"), &GLTFAccessor::set_accessor_type);
ClassDB::bind_method(D_METHOD("get_type"), &GLTFAccessor::get_type);
ClassDB::bind_method(D_METHOD("set_type", "type"), &GLTFAccessor::set_type);
ClassDB::bind_method(D_METHOD("get_min"), &GLTFAccessor::get_min);
ClassDB::bind_method(D_METHOD("set_min", "min"), &GLTFAccessor::set_min);
ClassDB::bind_method(D_METHOD("get_max"), &GLTFAccessor::get_max);
ClassDB::bind_method(D_METHOD("set_max", "max"), &GLTFAccessor::set_max);
ClassDB::bind_method(D_METHOD("get_sparse_count"), &GLTFAccessor::get_sparse_count);
ClassDB::bind_method(D_METHOD("set_sparse_count", "sparse_count"), &GLTFAccessor::set_sparse_count);
ClassDB::bind_method(D_METHOD("get_sparse_indices_buffer_view"), &GLTFAccessor::get_sparse_indices_buffer_view);
ClassDB::bind_method(D_METHOD("set_sparse_indices_buffer_view", "sparse_indices_buffer_view"), &GLTFAccessor::set_sparse_indices_buffer_view);
ClassDB::bind_method(D_METHOD("get_sparse_indices_byte_offset"), &GLTFAccessor::get_sparse_indices_byte_offset);
ClassDB::bind_method(D_METHOD("set_sparse_indices_byte_offset", "sparse_indices_byte_offset"), &GLTFAccessor::set_sparse_indices_byte_offset);
ClassDB::bind_method(D_METHOD("get_sparse_indices_component_type"), &GLTFAccessor::get_sparse_indices_component_type);
ClassDB::bind_method(D_METHOD("set_sparse_indices_component_type", "sparse_indices_component_type"), &GLTFAccessor::set_sparse_indices_component_type);
ClassDB::bind_method(D_METHOD("get_sparse_values_buffer_view"), &GLTFAccessor::get_sparse_values_buffer_view);
ClassDB::bind_method(D_METHOD("set_sparse_values_buffer_view", "sparse_values_buffer_view"), &GLTFAccessor::set_sparse_values_buffer_view);
ClassDB::bind_method(D_METHOD("get_sparse_values_byte_offset"), &GLTFAccessor::get_sparse_values_byte_offset);
ClassDB::bind_method(D_METHOD("set_sparse_values_byte_offset", "sparse_values_byte_offset"), &GLTFAccessor::set_sparse_values_byte_offset);
ADD_PROPERTY(PropertyInfo(Variant::INT, "buffer_view"), "set_buffer_view", "get_buffer_view"); // GLTFBufferViewIndex
ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_offset"), "set_byte_offset", "get_byte_offset"); // int
ADD_PROPERTY(PropertyInfo(Variant::INT, "component_type"), "set_component_type", "get_component_type"); // int
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "normalized"), "set_normalized", "get_normalized"); // bool
ADD_PROPERTY(PropertyInfo(Variant::INT, "count"), "set_count", "get_count"); // int
ADD_PROPERTY(PropertyInfo(Variant::INT, "accessor_type"), "set_accessor_type", "get_accessor_type"); // GLTFAccessor::GLTFAccessorType
ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_type", "get_type"); // Deprecated, int for GLTFAccessor::GLTFAccessorType
ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT64_ARRAY, "min"), "set_min", "get_min"); // Vector<real_t>
ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT64_ARRAY, "max"), "set_max", "get_max"); // Vector<real_t>
ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_count"), "set_sparse_count", "get_sparse_count"); // int
ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_indices_buffer_view"), "set_sparse_indices_buffer_view", "get_sparse_indices_buffer_view"); // int
ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_indices_byte_offset"), "set_sparse_indices_byte_offset", "get_sparse_indices_byte_offset"); // int
ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_indices_component_type"), "set_sparse_indices_component_type", "get_sparse_indices_component_type"); // int
ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_values_buffer_view"), "set_sparse_values_buffer_view", "get_sparse_values_buffer_view"); // int
ADD_PROPERTY(PropertyInfo(Variant::INT, "sparse_values_byte_offset"), "set_sparse_values_byte_offset", "get_sparse_values_byte_offset"); // int
}
GLTFBufferViewIndex GLTFAccessor::get_buffer_view() const {
return buffer_view;
}
void GLTFAccessor::set_buffer_view(GLTFBufferViewIndex p_buffer_view) {
buffer_view = p_buffer_view;
}
int64_t GLTFAccessor::get_byte_offset() const {
return byte_offset;
}
void GLTFAccessor::set_byte_offset(int64_t p_byte_offset) {
byte_offset = p_byte_offset;
}
GLTFAccessor::GLTFComponentType GLTFAccessor::get_component_type() const {
return component_type;
}
void GLTFAccessor::set_component_type(GLTFComponentType p_component_type) {
component_type = (GLTFComponentType)p_component_type;
}
bool GLTFAccessor::get_normalized() const {
return normalized;
}
void GLTFAccessor::set_normalized(bool p_normalized) {
normalized = p_normalized;
}
int64_t GLTFAccessor::get_count() const {
return count;
}
void GLTFAccessor::set_count(int64_t p_count) {
count = p_count;
}
GLTFAccessor::GLTFAccessorType GLTFAccessor::get_accessor_type() const {
return accessor_type;
}
void GLTFAccessor::set_accessor_type(GLTFAccessorType p_accessor_type) {
accessor_type = p_accessor_type;
}
int GLTFAccessor::get_type() const {
return (int)accessor_type;
}
void GLTFAccessor::set_type(int p_accessor_type) {
accessor_type = (GLTFAccessorType)p_accessor_type; // TODO: Register enum
}
Vector<double> GLTFAccessor::get_min() const {
return min;
}
void GLTFAccessor::set_min(Vector<double> p_min) {
min = p_min;
}
Vector<double> GLTFAccessor::get_max() const {
return max;
}
void GLTFAccessor::set_max(Vector<double> p_max) {
max = p_max;
}
int64_t GLTFAccessor::get_sparse_count() const {
return sparse_count;
}
void GLTFAccessor::set_sparse_count(int64_t p_sparse_count) {
sparse_count = p_sparse_count;
}
GLTFBufferViewIndex GLTFAccessor::get_sparse_indices_buffer_view() const {
return sparse_indices_buffer_view;
}
void GLTFAccessor::set_sparse_indices_buffer_view(GLTFBufferViewIndex p_sparse_indices_buffer_view) {
sparse_indices_buffer_view = p_sparse_indices_buffer_view;
}
int64_t GLTFAccessor::get_sparse_indices_byte_offset() const {
return sparse_indices_byte_offset;
}
void GLTFAccessor::set_sparse_indices_byte_offset(int64_t p_sparse_indices_byte_offset) {
sparse_indices_byte_offset = p_sparse_indices_byte_offset;
}
GLTFAccessor::GLTFComponentType GLTFAccessor::get_sparse_indices_component_type() const {
return sparse_indices_component_type;
}
void GLTFAccessor::set_sparse_indices_component_type(GLTFComponentType p_sparse_indices_component_type) {
sparse_indices_component_type = (GLTFComponentType)p_sparse_indices_component_type;
}
GLTFBufferViewIndex GLTFAccessor::get_sparse_values_buffer_view() const {
return sparse_values_buffer_view;
}
void GLTFAccessor::set_sparse_values_buffer_view(GLTFBufferViewIndex p_sparse_values_buffer_view) {
sparse_values_buffer_view = p_sparse_values_buffer_view;
}
int64_t GLTFAccessor::get_sparse_values_byte_offset() const {
return sparse_values_byte_offset;
}
void GLTFAccessor::set_sparse_values_byte_offset(int64_t p_sparse_values_byte_offset) {
sparse_values_byte_offset = p_sparse_values_byte_offset;
}

View File

@@ -0,0 +1,156 @@
/**************************************************************************/
/* gltf_accessor.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "../gltf_defines.h"
#include "core/io/resource.h"
struct GLTFAccessor : public Resource {
GDCLASS(GLTFAccessor, Resource);
friend class GLTFDocument;
public:
enum GLTFAccessorType {
TYPE_SCALAR,
TYPE_VEC2,
TYPE_VEC3,
TYPE_VEC4,
TYPE_MAT2,
TYPE_MAT3,
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_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:
GLTFBufferViewIndex buffer_view = -1;
int64_t byte_offset = 0;
GLTFComponentType component_type = COMPONENT_TYPE_NONE;
bool normalized = false;
int64_t count = 0;
GLTFAccessorType accessor_type = GLTFAccessorType::TYPE_SCALAR;
Vector<double> min;
Vector<double> max;
int64_t sparse_count = 0;
GLTFBufferViewIndex sparse_indices_buffer_view = 0;
int64_t sparse_indices_byte_offset = 0;
GLTFComponentType sparse_indices_component_type = COMPONENT_TYPE_NONE;
GLTFBufferViewIndex sparse_values_buffer_view = 0;
int64_t sparse_values_byte_offset = 0;
protected:
static void _bind_methods();
#ifndef DISABLE_DEPRECATED
// 32-bit and non-const versions for compatibility.
GLTFBufferViewIndex _get_buffer_view_bind_compat_106220();
int _get_byte_offset_bind_compat_106220();
int _get_component_type_bind_compat_106220();
void _set_component_type_bind_compat_106220(int p_component_type);
bool _get_normalized_bind_compat_106220();
int _get_count_bind_compat_106220();
GLTFAccessorType _get_accessor_type_bind_compat_106220();
int _get_type_bind_compat_106220();
Vector<double> _get_min_bind_compat_106220();
Vector<double> _get_max_bind_compat_106220();
int _get_sparse_count_bind_compat_106220();
int _get_sparse_indices_buffer_view_bind_compat_106220();
int _get_sparse_indices_byte_offset_bind_compat_106220();
int _get_sparse_indices_component_type_bind_compat_106220();
void _set_sparse_indices_component_type_bind_compat_106220(int p_sparse_indices_component_type);
int _get_sparse_values_buffer_view_bind_compat_106220();
int _get_sparse_values_byte_offset_bind_compat_106220();
static void _bind_compatibility_methods();
#endif // DISABLE_DEPRECATED
public:
GLTFBufferViewIndex get_buffer_view() const;
void set_buffer_view(GLTFBufferViewIndex p_buffer_view);
int64_t get_byte_offset() const;
void set_byte_offset(int64_t p_byte_offset);
GLTFComponentType get_component_type() const;
void set_component_type(GLTFComponentType p_component_type);
bool get_normalized() const;
void set_normalized(bool p_normalized);
int64_t get_count() const;
void set_count(int64_t p_count);
GLTFAccessorType get_accessor_type() const;
void set_accessor_type(GLTFAccessorType p_accessor_type);
int get_type() const;
void set_type(int p_accessor_type);
Vector<double> get_min() const;
void set_min(Vector<double> p_min);
Vector<double> get_max() const;
void set_max(Vector<double> p_max);
int64_t get_sparse_count() const;
void set_sparse_count(int64_t p_sparse_count);
GLTFBufferViewIndex get_sparse_indices_buffer_view() const;
void set_sparse_indices_buffer_view(GLTFBufferViewIndex p_sparse_indices_buffer_view);
int64_t get_sparse_indices_byte_offset() const;
void set_sparse_indices_byte_offset(int64_t p_sparse_indices_byte_offset);
GLTFComponentType get_sparse_indices_component_type() const;
void set_sparse_indices_component_type(GLTFComponentType p_sparse_indices_component_type);
GLTFBufferViewIndex get_sparse_values_buffer_view() const;
void set_sparse_values_buffer_view(GLTFBufferViewIndex p_sparse_values_buffer_view);
int64_t get_sparse_values_byte_offset() const;
void set_sparse_values_byte_offset(int64_t p_sparse_values_byte_offset);
};
VARIANT_ENUM_CAST(GLTFAccessor::GLTFAccessorType);
VARIANT_ENUM_CAST(GLTFAccessor::GLTFComponentType);

View File

@@ -0,0 +1,110 @@
/**************************************************************************/
/* gltf_animation.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_animation.h"
void GLTFAnimation::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_original_name"), &GLTFAnimation::get_original_name);
ClassDB::bind_method(D_METHOD("set_original_name", "original_name"), &GLTFAnimation::set_original_name);
ClassDB::bind_method(D_METHOD("get_loop"), &GLTFAnimation::get_loop);
ClassDB::bind_method(D_METHOD("set_loop", "loop"), &GLTFAnimation::set_loop);
ClassDB::bind_method(D_METHOD("get_additional_data", "extension_name"), &GLTFAnimation::get_additional_data);
ClassDB::bind_method(D_METHOD("set_additional_data", "extension_name", "additional_data"), &GLTFAnimation::set_additional_data);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "original_name"), "set_original_name", "get_original_name"); // String
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "get_loop"); // bool
}
GLTFAnimation::Interpolation GLTFAnimation::godot_to_gltf_interpolation(const Ref<Animation> &p_godot_animation, int32_t p_godot_anim_track_index) {
Animation::InterpolationType interpolation = p_godot_animation->track_get_interpolation_type(p_godot_anim_track_index);
switch (interpolation) {
case Animation::INTERPOLATION_LINEAR:
case Animation::INTERPOLATION_LINEAR_ANGLE:
return INTERP_LINEAR;
case Animation::INTERPOLATION_NEAREST:
return INTERP_STEP;
case Animation::INTERPOLATION_CUBIC:
case Animation::INTERPOLATION_CUBIC_ANGLE:
return INTERP_CUBIC_SPLINE;
}
return INTERP_LINEAR;
}
Animation::InterpolationType GLTFAnimation::gltf_to_godot_interpolation(Interpolation p_gltf_interpolation) {
switch (p_gltf_interpolation) {
case INTERP_LINEAR:
return Animation::INTERPOLATION_LINEAR;
case INTERP_STEP:
return Animation::INTERPOLATION_NEAREST;
case INTERP_CATMULLROMSPLINE:
case INTERP_CUBIC_SPLINE:
return Animation::INTERPOLATION_CUBIC;
}
return Animation::INTERPOLATION_LINEAR;
}
String GLTFAnimation::get_original_name() {
return original_name;
}
void GLTFAnimation::set_original_name(String p_name) {
original_name = p_name;
}
bool GLTFAnimation::get_loop() const {
return loop;
}
void GLTFAnimation::set_loop(bool p_val) {
loop = p_val;
}
HashMap<int, GLTFAnimation::NodeTrack> &GLTFAnimation::get_node_tracks() {
return node_tracks;
}
HashMap<String, GLTFAnimation::Channel<Variant>> &GLTFAnimation::get_pointer_tracks() {
return pointer_tracks;
}
bool GLTFAnimation::is_empty_of_tracks() const {
return node_tracks.is_empty() && pointer_tracks.is_empty();
}
GLTFAnimation::GLTFAnimation() {
}
Variant GLTFAnimation::get_additional_data(const StringName &p_extension_name) {
return additional_data[p_extension_name];
}
void GLTFAnimation::set_additional_data(const StringName &p_extension_name, Variant p_additional_data) {
additional_data[p_extension_name] = p_additional_data;
}

View File

@@ -0,0 +1,87 @@
/**************************************************************************/
/* gltf_animation.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "scene/resources/animation.h"
class GLTFAnimation : public Resource {
GDCLASS(GLTFAnimation, Resource);
protected:
static void _bind_methods();
public:
enum Interpolation {
INTERP_LINEAR,
INTERP_STEP,
INTERP_CATMULLROMSPLINE,
INTERP_CUBIC_SPLINE,
};
template <typename T>
struct Channel {
Interpolation interpolation = INTERP_LINEAR;
Vector<double> times;
Vector<T> values;
};
struct NodeTrack {
Channel<Vector3> position_track;
Channel<Quaternion> rotation_track;
Channel<Vector3> scale_track;
Vector<Channel<real_t>> weight_tracks;
};
String original_name;
bool loop = false;
HashMap<int, NodeTrack> node_tracks;
HashMap<String, Channel<Variant>> pointer_tracks;
Dictionary additional_data;
public:
static Interpolation godot_to_gltf_interpolation(const Ref<Animation> &p_godot_animation, int32_t p_godot_anim_track_index);
static Animation::InterpolationType gltf_to_godot_interpolation(Interpolation p_gltf_interpolation);
String get_original_name();
void set_original_name(String p_name);
bool get_loop() const;
void set_loop(bool p_val);
HashMap<int, GLTFAnimation::NodeTrack> &get_node_tracks();
HashMap<String, GLTFAnimation::Channel<Variant>> &get_pointer_tracks();
bool is_empty_of_tracks() const;
Variant get_additional_data(const StringName &p_extension_name);
void set_additional_data(const StringName &p_extension_name, Variant p_additional_data);
GLTFAnimation();
};

View File

@@ -0,0 +1,62 @@
/**************************************************************************/
/* gltf_buffer_view.compat.inc */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#ifndef DISABLE_DEPRECATED
GLTFBufferIndex GLTFBufferView::_get_buffer_bind_compat_86907() {
return get_buffer();
}
int GLTFBufferView::_get_byte_offset_bind_compat_86907() {
return get_byte_offset();
}
int GLTFBufferView::_get_byte_length_bind_compat_86907() {
return get_byte_length();
}
int GLTFBufferView::_get_byte_stride_bind_compat_86907() {
return get_byte_stride();
}
bool GLTFBufferView::_get_indices_bind_compat_86907() {
return get_indices();
}
void GLTFBufferView::_bind_compatibility_methods() {
// Non-const versions for compatibility.
ClassDB::bind_compatibility_method(D_METHOD("get_buffer"), &GLTFBufferView::_get_buffer_bind_compat_86907);
ClassDB::bind_compatibility_method(D_METHOD("get_byte_offset"), &GLTFBufferView::_get_byte_offset_bind_compat_86907);
ClassDB::bind_compatibility_method(D_METHOD("get_byte_length"), &GLTFBufferView::_get_byte_length_bind_compat_86907);
ClassDB::bind_compatibility_method(D_METHOD("get_byte_stride"), &GLTFBufferView::_get_byte_stride_bind_compat_86907);
ClassDB::bind_compatibility_method(D_METHOD("get_indices"), &GLTFBufferView::_get_indices_bind_compat_86907);
}
#endif // DISABLE_DEPRECATED

View File

@@ -0,0 +1,116 @@
/**************************************************************************/
/* gltf_buffer_view.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_buffer_view.h"
#include "gltf_buffer_view.compat.inc"
#include "../gltf_state.h"
void GLTFBufferView::_bind_methods() {
ClassDB::bind_method(D_METHOD("load_buffer_view_data", "state"), &GLTFBufferView::load_buffer_view_data);
ClassDB::bind_method(D_METHOD("get_buffer"), &GLTFBufferView::get_buffer);
ClassDB::bind_method(D_METHOD("set_buffer", "buffer"), &GLTFBufferView::set_buffer);
ClassDB::bind_method(D_METHOD("get_byte_offset"), &GLTFBufferView::get_byte_offset);
ClassDB::bind_method(D_METHOD("set_byte_offset", "byte_offset"), &GLTFBufferView::set_byte_offset);
ClassDB::bind_method(D_METHOD("get_byte_length"), &GLTFBufferView::get_byte_length);
ClassDB::bind_method(D_METHOD("set_byte_length", "byte_length"), &GLTFBufferView::set_byte_length);
ClassDB::bind_method(D_METHOD("get_byte_stride"), &GLTFBufferView::get_byte_stride);
ClassDB::bind_method(D_METHOD("set_byte_stride", "byte_stride"), &GLTFBufferView::set_byte_stride);
ClassDB::bind_method(D_METHOD("get_indices"), &GLTFBufferView::get_indices);
ClassDB::bind_method(D_METHOD("set_indices", "indices"), &GLTFBufferView::set_indices);
ClassDB::bind_method(D_METHOD("get_vertex_attributes"), &GLTFBufferView::get_vertex_attributes);
ClassDB::bind_method(D_METHOD("set_vertex_attributes", "is_attributes"), &GLTFBufferView::set_vertex_attributes);
ADD_PROPERTY(PropertyInfo(Variant::INT, "buffer"), "set_buffer", "get_buffer"); // GLTFBufferIndex
ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_offset"), "set_byte_offset", "get_byte_offset"); // int
ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_length"), "set_byte_length", "get_byte_length"); // int
ADD_PROPERTY(PropertyInfo(Variant::INT, "byte_stride"), "set_byte_stride", "get_byte_stride"); // int
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indices"), "set_indices", "get_indices"); // bool
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertex_attributes"), "set_vertex_attributes", "get_vertex_attributes"); // bool
}
GLTFBufferIndex GLTFBufferView::get_buffer() const {
return buffer;
}
void GLTFBufferView::set_buffer(GLTFBufferIndex p_buffer) {
buffer = p_buffer;
}
int64_t GLTFBufferView::get_byte_offset() const {
return byte_offset;
}
void GLTFBufferView::set_byte_offset(int64_t p_byte_offset) {
byte_offset = p_byte_offset;
}
int64_t GLTFBufferView::get_byte_length() const {
return byte_length;
}
void GLTFBufferView::set_byte_length(int64_t p_byte_length) {
byte_length = p_byte_length;
}
int64_t GLTFBufferView::get_byte_stride() const {
return byte_stride;
}
void GLTFBufferView::set_byte_stride(int64_t p_byte_stride) {
byte_stride = p_byte_stride;
}
bool GLTFBufferView::get_indices() const {
return indices;
}
void GLTFBufferView::set_indices(bool p_indices) {
indices = p_indices;
}
bool GLTFBufferView::get_vertex_attributes() const {
return vertex_attributes;
}
void GLTFBufferView::set_vertex_attributes(bool p_attributes) {
vertex_attributes = p_attributes;
}
Vector<uint8_t> GLTFBufferView::load_buffer_view_data(const Ref<GLTFState> p_state) const {
ERR_FAIL_COND_V(p_state.is_null(), Vector<uint8_t>());
ERR_FAIL_COND_V_MSG(byte_stride > 0, Vector<uint8_t>(), "Buffer views with byte stride are not yet supported by this method.");
const TypedArray<Vector<uint8_t>> &buffers = p_state->get_buffers();
ERR_FAIL_INDEX_V(buffer, buffers.size(), Vector<uint8_t>());
const PackedByteArray &buffer_data = buffers[buffer];
const int64_t byte_end = byte_offset + byte_length;
return buffer_data.slice(byte_offset, byte_end);
}

View File

@@ -0,0 +1,82 @@
/**************************************************************************/
/* gltf_buffer_view.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "../gltf_defines.h"
#include "core/io/resource.h"
class GLTFBufferView : public Resource {
GDCLASS(GLTFBufferView, Resource);
friend class GLTFDocument;
private:
GLTFBufferIndex buffer = -1;
int64_t byte_offset = 0;
int64_t byte_length = 0;
int64_t byte_stride = -1;
bool indices = false;
bool vertex_attributes = false;
protected:
static void _bind_methods();
#ifndef DISABLE_DEPRECATED
// Non-const versions for compatibility.
GLTFBufferIndex _get_buffer_bind_compat_86907();
int _get_byte_offset_bind_compat_86907();
int _get_byte_length_bind_compat_86907();
int _get_byte_stride_bind_compat_86907();
bool _get_indices_bind_compat_86907();
static void _bind_compatibility_methods();
#endif // DISABLE_DEPRECATED
public:
GLTFBufferIndex get_buffer() const;
void set_buffer(GLTFBufferIndex p_buffer);
int64_t get_byte_offset() const;
void set_byte_offset(int64_t p_byte_offset);
int64_t get_byte_length() const;
void set_byte_length(int64_t p_byte_length);
int64_t get_byte_stride() const;
void set_byte_stride(int64_t p_byte_stride);
bool get_indices() const;
void set_indices(bool p_indices);
bool get_vertex_attributes() const;
void set_vertex_attributes(bool p_attributes);
Vector<uint8_t> load_buffer_view_data(const Ref<GLTFState> p_state) const;
};

View File

@@ -0,0 +1,150 @@
/**************************************************************************/
/* gltf_camera.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_camera.h"
#include "gltf_object_model_property.h"
#include "scene/3d/camera_3d.h"
void GLTFCamera::_bind_methods() {
ClassDB::bind_static_method("GLTFCamera", D_METHOD("from_node", "camera_node"), &GLTFCamera::from_node);
ClassDB::bind_method(D_METHOD("to_node"), &GLTFCamera::to_node);
ClassDB::bind_static_method("GLTFCamera", D_METHOD("from_dictionary", "dictionary"), &GLTFCamera::from_dictionary);
ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFCamera::to_dictionary);
ClassDB::bind_method(D_METHOD("get_perspective"), &GLTFCamera::get_perspective);
ClassDB::bind_method(D_METHOD("set_perspective", "perspective"), &GLTFCamera::set_perspective);
ClassDB::bind_method(D_METHOD("get_fov"), &GLTFCamera::get_fov);
ClassDB::bind_method(D_METHOD("set_fov", "fov"), &GLTFCamera::set_fov);
ClassDB::bind_method(D_METHOD("get_size_mag"), &GLTFCamera::get_size_mag);
ClassDB::bind_method(D_METHOD("set_size_mag", "size_mag"), &GLTFCamera::set_size_mag);
ClassDB::bind_method(D_METHOD("get_depth_far"), &GLTFCamera::get_depth_far);
ClassDB::bind_method(D_METHOD("set_depth_far", "zdepth_far"), &GLTFCamera::set_depth_far);
ClassDB::bind_method(D_METHOD("get_depth_near"), &GLTFCamera::get_depth_near);
ClassDB::bind_method(D_METHOD("set_depth_near", "zdepth_near"), &GLTFCamera::set_depth_near);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "perspective"), "set_perspective", "get_perspective");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fov"), "set_fov", "get_fov");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size_mag"), "set_size_mag", "get_size_mag");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_far"), "set_depth_far", "get_depth_far");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_near"), "set_depth_near", "get_depth_near");
}
void GLTFCamera::set_fov_conversion_expressions(Ref<GLTFObjectModelProperty> &r_obj_model_prop) {
// Expression to convert glTF yfov in radians to Godot fov in degrees.
Ref<Expression> gltf_to_godot_expr;
gltf_to_godot_expr.instantiate();
PackedStringArray gltf_to_godot_args = { "yfov_rad" };
gltf_to_godot_expr->parse("rad_to_deg(yfov_rad)", gltf_to_godot_args);
r_obj_model_prop->set_gltf_to_godot_expression(gltf_to_godot_expr);
// Expression to convert Godot fov in degrees to glTF yfov in radians.
Ref<Expression> godot_to_gltf_expr;
godot_to_gltf_expr.instantiate();
PackedStringArray godot_to_gltf_args = { "fov_deg" };
godot_to_gltf_expr->parse("deg_to_rad(fov_deg)", godot_to_gltf_args);
r_obj_model_prop->set_godot_to_gltf_expression(godot_to_gltf_expr);
}
Ref<GLTFCamera> GLTFCamera::from_node(const Camera3D *p_camera) {
Ref<GLTFCamera> c;
c.instantiate();
ERR_FAIL_NULL_V_MSG(p_camera, c, "Tried to create a GLTFCamera from a Camera3D node, but the given node was null.");
c->set_perspective(p_camera->get_projection() == Camera3D::ProjectionType::PROJECTION_PERSPECTIVE);
// glTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.
c->set_fov(Math::deg_to_rad(p_camera->get_fov()));
// glTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters.
c->set_size_mag(p_camera->get_size() * 0.5f);
c->set_depth_far(p_camera->get_far());
c->set_depth_near(p_camera->get_near());
return c;
}
Camera3D *GLTFCamera::to_node() const {
Camera3D *camera = memnew(Camera3D);
camera->set_projection(perspective ? Camera3D::PROJECTION_PERSPECTIVE : Camera3D::PROJECTION_ORTHOGONAL);
// glTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.
camera->set_fov(Math::rad_to_deg(fov));
// glTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters.
camera->set_size(size_mag * 2.0f);
camera->set_near(depth_near);
camera->set_far(depth_far);
return camera;
}
Ref<GLTFCamera> GLTFCamera::from_dictionary(const Dictionary p_dictionary) {
ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFCamera>(), "Failed to parse glTF camera, missing required field 'type'.");
Ref<GLTFCamera> camera;
camera.instantiate();
const String &type = p_dictionary["type"];
if (type == "perspective") {
camera->set_perspective(true);
if (p_dictionary.has("perspective")) {
const Dictionary &persp = p_dictionary["perspective"];
camera->set_fov(persp["yfov"]);
if (persp.has("zfar")) {
camera->set_depth_far(persp["zfar"]);
}
camera->set_depth_near(persp["znear"]);
}
} else if (type == "orthographic") {
camera->set_perspective(false);
if (p_dictionary.has("orthographic")) {
const Dictionary &ortho = p_dictionary["orthographic"];
camera->set_size_mag(ortho["ymag"]);
camera->set_depth_far(ortho["zfar"]);
camera->set_depth_near(ortho["znear"]);
}
} else {
ERR_PRINT("Error parsing glTF camera: Camera type '" + type + "' is unknown, should be perspective or orthographic.");
}
return camera;
}
Dictionary GLTFCamera::to_dictionary() const {
Dictionary d;
if (perspective) {
Dictionary persp;
persp["yfov"] = fov;
persp["zfar"] = depth_far;
persp["znear"] = depth_near;
d["perspective"] = persp;
d["type"] = "perspective";
} else {
Dictionary ortho;
ortho["ymag"] = size_mag;
ortho["xmag"] = size_mag;
ortho["zfar"] = depth_far;
ortho["znear"] = depth_near;
d["orthographic"] = ortho;
d["type"] = "orthographic";
}
return d;
}

View File

@@ -0,0 +1,75 @@
/**************************************************************************/
/* gltf_camera.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "core/io/resource.h"
class Camera3D;
class GLTFObjectModelProperty;
// Reference and test file:
// https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_015_SimpleCameras.md
class GLTFCamera : public Resource {
GDCLASS(GLTFCamera, Resource);
private:
// glTF has no default camera values, they should always be specified in
// the glTF file. Here we default to Godot's default camera settings.
bool perspective = true;
real_t fov = Math::deg_to_rad(75.0);
real_t size_mag = 0.5;
real_t depth_far = 4000.0;
real_t depth_near = 0.05;
protected:
static void _bind_methods();
public:
static void set_fov_conversion_expressions(Ref<GLTFObjectModelProperty> &r_obj_model_prop);
bool get_perspective() const { return perspective; }
void set_perspective(bool p_val) { perspective = p_val; }
real_t get_fov() const { return fov; }
void set_fov(real_t p_val) { fov = p_val; }
real_t get_size_mag() const { return size_mag; }
void set_size_mag(real_t p_val) { size_mag = p_val; }
real_t get_depth_far() const { return depth_far; }
void set_depth_far(real_t p_val) { depth_far = p_val; }
real_t get_depth_near() const { return depth_near; }
void set_depth_near(real_t p_val) { depth_near = p_val; }
static Ref<GLTFCamera> from_node(const Camera3D *p_camera);
Camera3D *to_node() const;
static Ref<GLTFCamera> from_dictionary(const Dictionary p_dictionary);
virtual Dictionary to_dictionary() const;
};

View File

@@ -0,0 +1,91 @@
/**************************************************************************/
/* gltf_mesh.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_mesh.h"
#include "scene/resources/3d/importer_mesh.h"
void GLTFMesh::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_original_name"), &GLTFMesh::get_original_name);
ClassDB::bind_method(D_METHOD("set_original_name", "original_name"), &GLTFMesh::set_original_name);
ClassDB::bind_method(D_METHOD("get_mesh"), &GLTFMesh::get_mesh);
ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &GLTFMesh::set_mesh);
ClassDB::bind_method(D_METHOD("get_blend_weights"), &GLTFMesh::get_blend_weights);
ClassDB::bind_method(D_METHOD("set_blend_weights", "blend_weights"), &GLTFMesh::set_blend_weights);
ClassDB::bind_method(D_METHOD("get_instance_materials"), &GLTFMesh::get_instance_materials);
ClassDB::bind_method(D_METHOD("set_instance_materials", "instance_materials"), &GLTFMesh::set_instance_materials);
ClassDB::bind_method(D_METHOD("get_additional_data", "extension_name"), &GLTFMesh::get_additional_data);
ClassDB::bind_method(D_METHOD("set_additional_data", "extension_name", "additional_data"), &GLTFMesh::set_additional_data);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "original_name"), "set_original_name", "get_original_name");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh"), "set_mesh", "get_mesh");
ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "blend_weights"), "set_blend_weights", "get_blend_weights"); // Vector<float>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "instance_materials"), "set_instance_materials", "get_instance_materials");
}
String GLTFMesh::get_original_name() {
return original_name;
}
void GLTFMesh::set_original_name(String p_name) {
original_name = p_name;
}
Ref<ImporterMesh> GLTFMesh::get_mesh() {
return mesh;
}
void GLTFMesh::set_mesh(Ref<ImporterMesh> p_mesh) {
mesh = p_mesh;
}
TypedArray<Material> GLTFMesh::get_instance_materials() {
return instance_materials;
}
void GLTFMesh::set_instance_materials(TypedArray<Material> p_instance_materials) {
instance_materials = p_instance_materials;
}
Vector<float> GLTFMesh::get_blend_weights() {
return blend_weights;
}
void GLTFMesh::set_blend_weights(Vector<float> p_blend_weights) {
blend_weights = p_blend_weights;
}
Variant GLTFMesh::get_additional_data(const StringName &p_extension_name) {
return additional_data[p_extension_name];
}
void GLTFMesh::set_additional_data(const StringName &p_extension_name, Variant p_additional_data) {
additional_data[p_extension_name] = p_additional_data;
}

View File

@@ -0,0 +1,61 @@
/**************************************************************************/
/* gltf_mesh.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "../gltf_defines.h"
#include "scene/resources/3d/importer_mesh.h"
class GLTFMesh : public Resource {
GDCLASS(GLTFMesh, Resource);
private:
String original_name;
Ref<ImporterMesh> mesh;
Vector<float> blend_weights;
TypedArray<Material> instance_materials;
Dictionary additional_data;
protected:
static void _bind_methods();
public:
String get_original_name();
void set_original_name(String p_name);
Ref<ImporterMesh> get_mesh();
void set_mesh(Ref<ImporterMesh> p_mesh);
Vector<float> get_blend_weights();
void set_blend_weights(Vector<float> p_blend_weights);
TypedArray<Material> get_instance_materials();
void set_instance_materials(TypedArray<Material> p_instance_materials);
Variant get_additional_data(const StringName &p_extension_name);
void set_additional_data(const StringName &p_extension_name, Variant p_additional_data);
};

View File

@@ -0,0 +1,248 @@
/**************************************************************************/
/* gltf_node.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_node.h"
#include "../gltf_state.h"
void GLTFNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_original_name"), &GLTFNode::get_original_name);
ClassDB::bind_method(D_METHOD("set_original_name", "original_name"), &GLTFNode::set_original_name);
ClassDB::bind_method(D_METHOD("get_parent"), &GLTFNode::get_parent);
ClassDB::bind_method(D_METHOD("set_parent", "parent"), &GLTFNode::set_parent);
ClassDB::bind_method(D_METHOD("get_height"), &GLTFNode::get_height);
ClassDB::bind_method(D_METHOD("set_height", "height"), &GLTFNode::set_height);
ClassDB::bind_method(D_METHOD("get_xform"), &GLTFNode::get_xform);
ClassDB::bind_method(D_METHOD("set_xform", "xform"), &GLTFNode::set_xform);
ClassDB::bind_method(D_METHOD("get_mesh"), &GLTFNode::get_mesh);
ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &GLTFNode::set_mesh);
ClassDB::bind_method(D_METHOD("get_camera"), &GLTFNode::get_camera);
ClassDB::bind_method(D_METHOD("set_camera", "camera"), &GLTFNode::set_camera);
ClassDB::bind_method(D_METHOD("get_skin"), &GLTFNode::get_skin);
ClassDB::bind_method(D_METHOD("set_skin", "skin"), &GLTFNode::set_skin);
ClassDB::bind_method(D_METHOD("get_skeleton"), &GLTFNode::get_skeleton);
ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &GLTFNode::set_skeleton);
ClassDB::bind_method(D_METHOD("get_position"), &GLTFNode::get_position);
ClassDB::bind_method(D_METHOD("set_position", "position"), &GLTFNode::set_position);
ClassDB::bind_method(D_METHOD("get_rotation"), &GLTFNode::get_rotation);
ClassDB::bind_method(D_METHOD("set_rotation", "rotation"), &GLTFNode::set_rotation);
ClassDB::bind_method(D_METHOD("get_scale"), &GLTFNode::get_scale);
ClassDB::bind_method(D_METHOD("set_scale", "scale"), &GLTFNode::set_scale);
ClassDB::bind_method(D_METHOD("get_children"), &GLTFNode::get_children);
ClassDB::bind_method(D_METHOD("set_children", "children"), &GLTFNode::set_children);
ClassDB::bind_method(D_METHOD("append_child_index", "child_index"), &GLTFNode::append_child_index);
ClassDB::bind_method(D_METHOD("get_light"), &GLTFNode::get_light);
ClassDB::bind_method(D_METHOD("set_light", "light"), &GLTFNode::set_light);
ClassDB::bind_method(D_METHOD("get_visible"), &GLTFNode::get_visible);
ClassDB::bind_method(D_METHOD("set_visible", "visible"), &GLTFNode::set_visible);
ClassDB::bind_method(D_METHOD("get_additional_data", "extension_name"), &GLTFNode::get_additional_data);
ClassDB::bind_method(D_METHOD("set_additional_data", "extension_name", "additional_data"), &GLTFNode::set_additional_data);
ClassDB::bind_method(D_METHOD("get_scene_node_path", "gltf_state", "handle_skeletons"), &GLTFNode::get_scene_node_path, DEFVAL(true));
ADD_PROPERTY(PropertyInfo(Variant::STRING, "original_name"), "set_original_name", "get_original_name"); // String
ADD_PROPERTY(PropertyInfo(Variant::INT, "parent"), "set_parent", "get_parent"); // GLTFNodeIndex
ADD_PROPERTY(PropertyInfo(Variant::INT, "height"), "set_height", "get_height"); // int
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "xform"), "set_xform", "get_xform"); // Transform3D
ADD_PROPERTY(PropertyInfo(Variant::INT, "mesh"), "set_mesh", "get_mesh"); // GLTFMeshIndex
ADD_PROPERTY(PropertyInfo(Variant::INT, "camera"), "set_camera", "get_camera"); // GLTFCameraIndex
ADD_PROPERTY(PropertyInfo(Variant::INT, "skin"), "set_skin", "get_skin"); // GLTFSkinIndex
ADD_PROPERTY(PropertyInfo(Variant::INT, "skeleton"), "set_skeleton", "get_skeleton"); // GLTFSkeletonIndex
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "position"), "set_position", "get_position"); // Vector3
ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "rotation"), "set_rotation", "get_rotation"); // Quaternion
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "scale"), "set_scale", "get_scale"); // Vector3
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "children"), "set_children", "get_children"); // Vector<int>
ADD_PROPERTY(PropertyInfo(Variant::INT, "light"), "set_light", "get_light"); // GLTFLightIndex
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "get_visible"); // bool
}
String GLTFNode::get_original_name() {
return original_name;
}
void GLTFNode::set_original_name(String p_name) {
original_name = p_name;
}
GLTFNodeIndex GLTFNode::get_parent() {
return parent;
}
void GLTFNode::set_parent(GLTFNodeIndex p_parent) {
parent = p_parent;
}
int GLTFNode::get_height() {
return height;
}
void GLTFNode::set_height(int p_height) {
height = p_height;
}
Transform3D GLTFNode::get_xform() {
return transform;
}
void GLTFNode::set_xform(Transform3D p_xform) {
transform = p_xform;
}
GLTFMeshIndex GLTFNode::get_mesh() {
return mesh;
}
void GLTFNode::set_mesh(GLTFMeshIndex p_mesh) {
mesh = p_mesh;
}
GLTFCameraIndex GLTFNode::get_camera() {
return camera;
}
void GLTFNode::set_camera(GLTFCameraIndex p_camera) {
camera = p_camera;
}
GLTFSkinIndex GLTFNode::get_skin() {
return skin;
}
void GLTFNode::set_skin(GLTFSkinIndex p_skin) {
skin = p_skin;
}
GLTFSkeletonIndex GLTFNode::get_skeleton() {
return skeleton;
}
void GLTFNode::set_skeleton(GLTFSkeletonIndex p_skeleton) {
skeleton = p_skeleton;
}
Vector3 GLTFNode::get_position() {
return transform.origin;
}
void GLTFNode::set_position(Vector3 p_position) {
transform.origin = p_position;
}
Quaternion GLTFNode::get_rotation() {
return transform.basis.get_rotation_quaternion();
}
void GLTFNode::set_rotation(Quaternion p_rotation) {
transform.basis.set_quaternion_scale(p_rotation, transform.basis.get_scale());
}
Vector3 GLTFNode::get_scale() {
return transform.basis.get_scale();
}
void GLTFNode::set_scale(Vector3 p_scale) {
transform.basis = transform.basis.orthonormalized() * Basis::from_scale(p_scale);
}
Vector<int> GLTFNode::get_children() {
return children;
}
void GLTFNode::set_children(Vector<int> p_children) {
children = p_children;
}
void GLTFNode::append_child_index(int p_child_index) {
children.append(p_child_index);
}
GLTFLightIndex GLTFNode::get_light() {
return light;
}
void GLTFNode::set_light(GLTFLightIndex p_light) {
light = p_light;
}
bool GLTFNode::get_visible() {
return visible;
}
void GLTFNode::set_visible(bool p_visible) {
visible = p_visible;
}
Variant GLTFNode::get_additional_data(const StringName &p_extension_name) {
return additional_data[p_extension_name];
}
bool GLTFNode::has_additional_data(const StringName &p_extension_name) {
return additional_data.has(p_extension_name);
}
void GLTFNode::set_additional_data(const StringName &p_extension_name, Variant p_additional_data) {
additional_data[p_extension_name] = p_additional_data;
}
NodePath GLTFNode::get_scene_node_path(Ref<GLTFState> p_state, bool p_handle_skeletons) {
Vector<StringName> path;
Vector<StringName> subpath;
Ref<GLTFNode> current_gltf_node = this;
const int gltf_node_count = p_state->nodes.size();
if (p_handle_skeletons && skeleton != -1) {
// Special case for skeleton nodes, skip all bones so that the path is to the Skeleton3D node.
// A path that would otherwise be `A/B/C/Bone1/Bone2/Bone3` becomes `A/B/C/Skeleton3D:Bone3`.
subpath.append(get_name());
// The generated Skeleton3D node will be named Skeleton3D, so add it to the path.
path.append("Skeleton3D");
do {
const int parent_index = current_gltf_node->get_parent();
ERR_FAIL_INDEX_V(parent_index, gltf_node_count, NodePath());
current_gltf_node = p_state->nodes[parent_index];
} while (current_gltf_node->skeleton != -1);
}
const bool is_godot_single_root = p_state->extensions_used.has("GODOT_single_root");
while (true) {
const int parent_index = current_gltf_node->get_parent();
if (is_godot_single_root && parent_index == -1) {
// For GODOT_single_root scenes, the root glTF node becomes the Godot scene root, so it
// should not be included in the path. Ex: A/B/C, A is single root, we want B/C only.
break;
}
path.insert(0, current_gltf_node->get_name());
if (!is_godot_single_root && parent_index == -1) {
break;
}
ERR_FAIL_INDEX_V(parent_index, gltf_node_count, NodePath());
current_gltf_node = p_state->nodes[parent_index];
}
if (unlikely(path.is_empty())) {
path.append(".");
}
return NodePath(path, subpath, false);
}

View File

@@ -0,0 +1,113 @@
/**************************************************************************/
/* gltf_node.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "../gltf_defines.h"
#include "core/io/resource.h"
class GLTFNode : public Resource {
GDCLASS(GLTFNode, Resource);
friend class GLTFDocument;
friend class SkinTool;
friend class FBXDocument;
private:
String original_name;
GLTFNodeIndex parent = -1;
int height = -1;
Transform3D transform;
GLTFMeshIndex mesh = -1;
GLTFCameraIndex camera = -1;
GLTFSkinIndex skin = -1;
GLTFSkeletonIndex skeleton = -1;
bool joint = false;
bool visible = true;
Vector<int> children;
GLTFLightIndex light = -1;
Dictionary additional_data;
protected:
static void _bind_methods();
public:
String get_original_name();
void set_original_name(String p_name);
GLTFNodeIndex get_parent();
void set_parent(GLTFNodeIndex p_parent);
int get_height();
void set_height(int p_height);
Transform3D get_xform();
void set_xform(Transform3D p_xform);
Transform3D get_rest_xform();
void set_rest_xform(Transform3D p_rest_xform);
GLTFMeshIndex get_mesh();
void set_mesh(GLTFMeshIndex p_mesh);
GLTFCameraIndex get_camera();
void set_camera(GLTFCameraIndex p_camera);
GLTFSkinIndex get_skin();
void set_skin(GLTFSkinIndex p_skin);
GLTFSkeletonIndex get_skeleton();
void set_skeleton(GLTFSkeletonIndex p_skeleton);
Vector3 get_position();
void set_position(Vector3 p_position);
Quaternion get_rotation();
void set_rotation(Quaternion p_rotation);
Vector3 get_scale();
void set_scale(Vector3 p_scale);
Vector<int> get_children();
void set_children(Vector<int> p_children);
void append_child_index(int p_child_index);
GLTFLightIndex get_light();
void set_light(GLTFLightIndex p_light);
bool get_visible();
void set_visible(bool p_visible);
Variant get_additional_data(const StringName &p_extension_name);
bool has_additional_data(const StringName &p_extension_name);
void set_additional_data(const StringName &p_extension_name, Variant p_additional_data);
NodePath get_scene_node_path(Ref<GLTFState> p_state, bool p_handle_skeletons = true);
};

View File

@@ -0,0 +1,173 @@
/**************************************************************************/
/* gltf_object_model_property.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_object_model_property.h"
#include "../gltf_template_convert.h"
void GLTFObjectModelProperty::_bind_methods() {
BIND_ENUM_CONSTANT(GLTF_OBJECT_MODEL_TYPE_UNKNOWN);
BIND_ENUM_CONSTANT(GLTF_OBJECT_MODEL_TYPE_BOOL);
BIND_ENUM_CONSTANT(GLTF_OBJECT_MODEL_TYPE_FLOAT);
BIND_ENUM_CONSTANT(GLTF_OBJECT_MODEL_TYPE_FLOAT_ARRAY);
BIND_ENUM_CONSTANT(GLTF_OBJECT_MODEL_TYPE_FLOAT2);
BIND_ENUM_CONSTANT(GLTF_OBJECT_MODEL_TYPE_FLOAT3);
BIND_ENUM_CONSTANT(GLTF_OBJECT_MODEL_TYPE_FLOAT4);
BIND_ENUM_CONSTANT(GLTF_OBJECT_MODEL_TYPE_FLOAT2X2);
BIND_ENUM_CONSTANT(GLTF_OBJECT_MODEL_TYPE_FLOAT3X3);
BIND_ENUM_CONSTANT(GLTF_OBJECT_MODEL_TYPE_FLOAT4X4);
BIND_ENUM_CONSTANT(GLTF_OBJECT_MODEL_TYPE_INT);
ClassDB::bind_method(D_METHOD("append_node_path", "node_path"), &GLTFObjectModelProperty::append_node_path);
ClassDB::bind_method(D_METHOD("append_path_to_property", "node_path", "prop_name"), &GLTFObjectModelProperty::append_path_to_property);
ClassDB::bind_method(D_METHOD("get_accessor_type"), &GLTFObjectModelProperty::get_accessor_type);
ClassDB::bind_method(D_METHOD("get_gltf_to_godot_expression"), &GLTFObjectModelProperty::get_gltf_to_godot_expression);
ClassDB::bind_method(D_METHOD("set_gltf_to_godot_expression", "gltf_to_godot_expr"), &GLTFObjectModelProperty::set_gltf_to_godot_expression);
ClassDB::bind_method(D_METHOD("get_godot_to_gltf_expression"), &GLTFObjectModelProperty::get_godot_to_gltf_expression);
ClassDB::bind_method(D_METHOD("set_godot_to_gltf_expression", "godot_to_gltf_expr"), &GLTFObjectModelProperty::set_godot_to_gltf_expression);
ClassDB::bind_method(D_METHOD("get_node_paths"), &GLTFObjectModelProperty::get_node_paths);
ClassDB::bind_method(D_METHOD("has_node_paths"), &GLTFObjectModelProperty::has_node_paths);
ClassDB::bind_method(D_METHOD("set_node_paths", "node_paths"), &GLTFObjectModelProperty::set_node_paths);
ClassDB::bind_method(D_METHOD("get_object_model_type"), &GLTFObjectModelProperty::get_object_model_type);
ClassDB::bind_method(D_METHOD("set_object_model_type", "type"), &GLTFObjectModelProperty::set_object_model_type);
ClassDB::bind_method(D_METHOD("get_json_pointers"), &GLTFObjectModelProperty::get_json_pointers_bind);
ClassDB::bind_method(D_METHOD("has_json_pointers"), &GLTFObjectModelProperty::has_json_pointers);
ClassDB::bind_method(D_METHOD("set_json_pointers", "json_pointers"), &GLTFObjectModelProperty::set_json_pointers_bind);
ClassDB::bind_method(D_METHOD("get_variant_type"), &GLTFObjectModelProperty::get_variant_type);
ClassDB::bind_method(D_METHOD("set_variant_type", "variant_type"), &GLTFObjectModelProperty::set_variant_type);
ClassDB::bind_method(D_METHOD("set_types", "variant_type", "obj_model_type"), &GLTFObjectModelProperty::set_types);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gltf_to_godot_expression", PROPERTY_HINT_RESOURCE_TYPE, "Expression"), "set_gltf_to_godot_expression", "get_gltf_to_godot_expression"); // Ref<Expression>
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "godot_to_gltf_expression", PROPERTY_HINT_RESOURCE_TYPE, "Expression"), "set_godot_to_gltf_expression", "get_godot_to_gltf_expression"); // Ref<Expression>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "node_paths", PROPERTY_HINT_TYPE_STRING, "NodePath"), "set_node_paths", "get_node_paths"); // TypedArray<NodePath>
ADD_PROPERTY(PropertyInfo(Variant::INT, "object_model_type"), "set_object_model_type", "get_object_model_type"); // GLTFObjectModelType
ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "json_pointers"), "set_json_pointers", "get_json_pointers"); // TypedArray<PackedStringArray>
ADD_PROPERTY(PropertyInfo(Variant::INT, "variant_type"), "set_variant_type", "get_variant_type"); // Variant::Type
}
void GLTFObjectModelProperty::append_node_path(const NodePath &p_node_path) {
node_paths.push_back(p_node_path);
}
void GLTFObjectModelProperty::append_path_to_property(const NodePath &p_node_path, const StringName &p_prop_name) {
Vector<StringName> node_names = p_node_path.get_names();
Vector<StringName> subpath = p_node_path.get_subnames();
subpath.append(p_prop_name);
node_paths.push_back(NodePath(node_names, subpath, false));
}
GLTFAccessor::GLTFAccessorType GLTFObjectModelProperty::get_accessor_type() const {
switch (object_model_type) {
case GLTF_OBJECT_MODEL_TYPE_FLOAT2:
return GLTFAccessor::TYPE_VEC2;
case GLTF_OBJECT_MODEL_TYPE_FLOAT3:
return GLTFAccessor::TYPE_VEC3;
case GLTF_OBJECT_MODEL_TYPE_FLOAT4:
return GLTFAccessor::TYPE_VEC4;
case GLTF_OBJECT_MODEL_TYPE_FLOAT2X2:
return GLTFAccessor::TYPE_MAT2;
case GLTF_OBJECT_MODEL_TYPE_FLOAT3X3:
return GLTFAccessor::TYPE_MAT3;
case GLTF_OBJECT_MODEL_TYPE_FLOAT4X4:
return GLTFAccessor::TYPE_MAT4;
default:
return GLTFAccessor::TYPE_SCALAR;
}
}
Ref<Expression> GLTFObjectModelProperty::get_gltf_to_godot_expression() const {
return gltf_to_godot_expr;
}
void GLTFObjectModelProperty::set_gltf_to_godot_expression(Ref<Expression> p_gltf_to_godot_expr) {
gltf_to_godot_expr = p_gltf_to_godot_expr;
}
Ref<Expression> GLTFObjectModelProperty::get_godot_to_gltf_expression() const {
return godot_to_gltf_expr;
}
void GLTFObjectModelProperty::set_godot_to_gltf_expression(Ref<Expression> p_godot_to_gltf_expr) {
godot_to_gltf_expr = p_godot_to_gltf_expr;
}
TypedArray<NodePath> GLTFObjectModelProperty::get_node_paths() const {
return node_paths;
}
bool GLTFObjectModelProperty::has_node_paths() const {
return !node_paths.is_empty();
}
void GLTFObjectModelProperty::set_node_paths(TypedArray<NodePath> p_node_paths) {
node_paths = p_node_paths;
}
GLTFObjectModelProperty::GLTFObjectModelType GLTFObjectModelProperty::get_object_model_type() const {
return object_model_type;
}
void GLTFObjectModelProperty::set_object_model_type(GLTFObjectModelType p_type) {
object_model_type = p_type;
}
Vector<PackedStringArray> GLTFObjectModelProperty::get_json_pointers() const {
return json_pointers;
}
bool GLTFObjectModelProperty::has_json_pointers() const {
return !json_pointers.is_empty();
}
void GLTFObjectModelProperty::set_json_pointers(const Vector<PackedStringArray> &p_json_pointers) {
json_pointers = p_json_pointers;
}
TypedArray<PackedStringArray> GLTFObjectModelProperty::get_json_pointers_bind() const {
return GLTFTemplateConvert::to_array(json_pointers);
}
void GLTFObjectModelProperty::set_json_pointers_bind(const TypedArray<PackedStringArray> &p_json_pointers) {
GLTFTemplateConvert::set_from_array(json_pointers, p_json_pointers);
}
Variant::Type GLTFObjectModelProperty::get_variant_type() const {
return variant_type;
}
void GLTFObjectModelProperty::set_variant_type(Variant::Type p_variant_type) {
variant_type = p_variant_type;
}
void GLTFObjectModelProperty::set_types(Variant::Type p_variant_type, GLTFObjectModelType p_obj_model_type) {
variant_type = p_variant_type;
object_model_type = p_obj_model_type;
}

View File

@@ -0,0 +1,101 @@
/**************************************************************************/
/* gltf_object_model_property.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "core/math/expression.h"
#include "core/variant/typed_array.h"
#include "gltf_accessor.h"
// Object model: https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/ObjectModel.adoc
// KHR_animation_pointer: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_animation_pointer
class GLTFObjectModelProperty : public RefCounted {
GDCLASS(GLTFObjectModelProperty, RefCounted);
public:
enum GLTFObjectModelType {
GLTF_OBJECT_MODEL_TYPE_UNKNOWN,
GLTF_OBJECT_MODEL_TYPE_BOOL,
GLTF_OBJECT_MODEL_TYPE_FLOAT,
GLTF_OBJECT_MODEL_TYPE_FLOAT_ARRAY,
GLTF_OBJECT_MODEL_TYPE_FLOAT2,
GLTF_OBJECT_MODEL_TYPE_FLOAT3,
GLTF_OBJECT_MODEL_TYPE_FLOAT4,
GLTF_OBJECT_MODEL_TYPE_FLOAT2X2,
GLTF_OBJECT_MODEL_TYPE_FLOAT3X3,
GLTF_OBJECT_MODEL_TYPE_FLOAT4X4,
GLTF_OBJECT_MODEL_TYPE_INT,
};
private:
Ref<Expression> gltf_to_godot_expr;
Ref<Expression> godot_to_gltf_expr;
TypedArray<NodePath> node_paths;
GLTFObjectModelType object_model_type = GLTF_OBJECT_MODEL_TYPE_UNKNOWN;
Vector<PackedStringArray> json_pointers;
Variant::Type variant_type = Variant::NIL;
protected:
static void _bind_methods();
public:
void append_node_path(const NodePath &p_node_path);
void append_path_to_property(const NodePath &p_node_path, const StringName &p_prop_name);
GLTFAccessor::GLTFAccessorType get_accessor_type() const;
Ref<Expression> get_gltf_to_godot_expression() const;
void set_gltf_to_godot_expression(Ref<Expression> p_gltf_to_godot_expr);
Ref<Expression> get_godot_to_gltf_expression() const;
void set_godot_to_gltf_expression(Ref<Expression> p_godot_to_gltf_expr);
TypedArray<NodePath> get_node_paths() const;
bool has_node_paths() const;
void set_node_paths(TypedArray<NodePath> p_node_paths);
GLTFObjectModelType get_object_model_type() const;
void set_object_model_type(GLTFObjectModelType p_type);
Vector<PackedStringArray> get_json_pointers() const;
bool has_json_pointers() const;
void set_json_pointers(const Vector<PackedStringArray> &p_json_pointers);
TypedArray<PackedStringArray> get_json_pointers_bind() const;
void set_json_pointers_bind(const TypedArray<PackedStringArray> &p_json_pointers);
Variant::Type get_variant_type() const;
void set_variant_type(Variant::Type p_variant_type);
void set_types(Variant::Type p_variant_type, GLTFObjectModelType p_obj_model_type);
};
VARIANT_ENUM_CAST(GLTFObjectModelProperty::GLTFObjectModelType);

View File

@@ -0,0 +1,99 @@
/**************************************************************************/
/* gltf_skeleton.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_skeleton.h"
#include "../gltf_template_convert.h"
#include "scene/3d/bone_attachment_3d.h"
void GLTFSkeleton::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_joints"), &GLTFSkeleton::get_joints);
ClassDB::bind_method(D_METHOD("set_joints", "joints"), &GLTFSkeleton::set_joints);
ClassDB::bind_method(D_METHOD("get_roots"), &GLTFSkeleton::get_roots);
ClassDB::bind_method(D_METHOD("set_roots", "roots"), &GLTFSkeleton::set_roots);
ClassDB::bind_method(D_METHOD("get_godot_skeleton"), &GLTFSkeleton::get_godot_skeleton);
ClassDB::bind_method(D_METHOD("get_unique_names"), &GLTFSkeleton::get_unique_names);
ClassDB::bind_method(D_METHOD("set_unique_names", "unique_names"), &GLTFSkeleton::set_unique_names);
ClassDB::bind_method(D_METHOD("get_godot_bone_node"), &GLTFSkeleton::get_godot_bone_node);
ClassDB::bind_method(D_METHOD("set_godot_bone_node", "godot_bone_node"), &GLTFSkeleton::set_godot_bone_node);
ClassDB::bind_method(D_METHOD("get_bone_attachment_count"), &GLTFSkeleton::get_bone_attachment_count);
ClassDB::bind_method(D_METHOD("get_bone_attachment", "idx"), &GLTFSkeleton::get_bone_attachment);
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "joints"), "set_joints", "get_joints"); // Vector<GLTFNodeIndex>
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "roots"), "set_roots", "get_roots"); // Vector<GLTFNodeIndex>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "unique_names", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_unique_names", "get_unique_names"); // Set<String>
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "godot_bone_node", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR), "set_godot_bone_node", "get_godot_bone_node"); // RBMap<int32_t,
}
Vector<GLTFNodeIndex> GLTFSkeleton::get_joints() {
return joints;
}
void GLTFSkeleton::set_joints(Vector<GLTFNodeIndex> p_joints) {
joints = p_joints;
}
Vector<GLTFNodeIndex> GLTFSkeleton::get_roots() {
return roots;
}
void GLTFSkeleton::set_roots(Vector<GLTFNodeIndex> p_roots) {
roots = p_roots;
}
Skeleton3D *GLTFSkeleton::get_godot_skeleton() {
return godot_skeleton;
}
TypedArray<String> GLTFSkeleton::get_unique_names() {
return GLTFTemplateConvert::to_array(unique_names);
}
void GLTFSkeleton::set_unique_names(TypedArray<String> p_unique_names) {
GLTFTemplateConvert::set_from_array(unique_names, p_unique_names);
}
Dictionary GLTFSkeleton::get_godot_bone_node() {
return GLTFTemplateConvert::to_dictionary(godot_bone_node);
}
void GLTFSkeleton::set_godot_bone_node(Dictionary p_indict) {
GLTFTemplateConvert::set_from_dictionary(godot_bone_node, p_indict);
}
BoneAttachment3D *GLTFSkeleton::get_bone_attachment(int idx) {
ERR_FAIL_INDEX_V(idx, bone_attachments.size(), nullptr);
return bone_attachments[idx];
}
int32_t GLTFSkeleton::get_bone_attachment_count() {
return bone_attachments.size();
}

View File

@@ -0,0 +1,104 @@
/**************************************************************************/
/* gltf_skeleton.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "../gltf_defines.h"
#include "core/io/resource.h"
#include "scene/3d/bone_attachment_3d.h"
#include "scene/3d/skeleton_3d.h"
class GLTFSkeleton : public Resource {
GDCLASS(GLTFSkeleton, Resource);
friend class GLTFDocument;
friend class SkinTool;
friend class FBXDocument;
private:
// The *synthesized* skeletons joints
Vector<GLTFNodeIndex> joints;
// The roots of the skeleton. If there are multiple, each root must have the
// same parent (ie roots are siblings)
Vector<GLTFNodeIndex> roots;
// The created Skeleton3D for the scene
Skeleton3D *godot_skeleton = nullptr;
// Set of unique bone names for the skeleton
HashSet<String> unique_names;
HashMap<int32_t, GLTFNodeIndex> godot_bone_node;
Vector<BoneAttachment3D *> bone_attachments;
protected:
static void _bind_methods();
public:
Vector<GLTFNodeIndex> get_joints();
void set_joints(Vector<GLTFNodeIndex> p_joints);
Vector<GLTFNodeIndex> get_roots();
void set_roots(Vector<GLTFNodeIndex> p_roots);
Skeleton3D *get_godot_skeleton();
// Skeleton *get_godot_skeleton() {
// return godot_skeleton;
// }
// void set_godot_skeleton(Skeleton p_*godot_skeleton) {
// godot_skeleton = p_godot_skeleton;
// }
TypedArray<String> get_unique_names();
void set_unique_names(TypedArray<String> p_unique_names);
//RBMap<int32_t, GLTFNodeIndex> get_godot_bone_node() {
// return godot_bone_node;
//}
//void set_godot_bone_node(const RBMap<int32_t, GLTFNodeIndex> &p_godot_bone_node) {
// godot_bone_node = p_godot_bone_node;
//}
Dictionary get_godot_bone_node();
void set_godot_bone_node(Dictionary p_indict);
//Dictionary get_godot_bone_node() {
// return VariantConversion::to_dict(godot_bone_node);
//}
//void set_godot_bone_node(Dictionary p_indict) {
// VariantConversion::set_from_dict(godot_bone_node, p_indict);
//}
BoneAttachment3D *get_bone_attachment(int idx);
int32_t get_bone_attachment_count();
};

View File

@@ -0,0 +1,277 @@
/**************************************************************************/
/* gltf_skin.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_skin.h"
#include "../gltf_template_convert.h"
#include "core/variant/typed_array.h"
#include "scene/resources/3d/skin.h"
void GLTFSkin::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_skin_root"), &GLTFSkin::get_skin_root);
ClassDB::bind_method(D_METHOD("set_skin_root", "skin_root"), &GLTFSkin::set_skin_root);
ClassDB::bind_method(D_METHOD("get_joints_original"), &GLTFSkin::get_joints_original);
ClassDB::bind_method(D_METHOD("set_joints_original", "joints_original"), &GLTFSkin::set_joints_original);
ClassDB::bind_method(D_METHOD("get_inverse_binds"), &GLTFSkin::get_inverse_binds);
ClassDB::bind_method(D_METHOD("set_inverse_binds", "inverse_binds"), &GLTFSkin::set_inverse_binds);
ClassDB::bind_method(D_METHOD("get_joints"), &GLTFSkin::get_joints);
ClassDB::bind_method(D_METHOD("set_joints", "joints"), &GLTFSkin::set_joints);
ClassDB::bind_method(D_METHOD("get_non_joints"), &GLTFSkin::get_non_joints);
ClassDB::bind_method(D_METHOD("set_non_joints", "non_joints"), &GLTFSkin::set_non_joints);
ClassDB::bind_method(D_METHOD("get_roots"), &GLTFSkin::get_roots);
ClassDB::bind_method(D_METHOD("set_roots", "roots"), &GLTFSkin::set_roots);
ClassDB::bind_method(D_METHOD("get_skeleton"), &GLTFSkin::get_skeleton);
ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &GLTFSkin::set_skeleton);
ClassDB::bind_method(D_METHOD("get_joint_i_to_bone_i"), &GLTFSkin::get_joint_i_to_bone_i);
ClassDB::bind_method(D_METHOD("set_joint_i_to_bone_i", "joint_i_to_bone_i"), &GLTFSkin::set_joint_i_to_bone_i);
ClassDB::bind_method(D_METHOD("get_joint_i_to_name"), &GLTFSkin::get_joint_i_to_name);
ClassDB::bind_method(D_METHOD("set_joint_i_to_name", "joint_i_to_name"), &GLTFSkin::set_joint_i_to_name);
ClassDB::bind_method(D_METHOD("get_godot_skin"), &GLTFSkin::get_godot_skin);
ClassDB::bind_method(D_METHOD("set_godot_skin", "godot_skin"), &GLTFSkin::set_godot_skin);
ADD_PROPERTY(PropertyInfo(Variant::INT, "skin_root"), "set_skin_root", "get_skin_root"); // GLTFNodeIndex
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "joints_original"), "set_joints_original", "get_joints_original"); // Vector<GLTFNodeIndex>
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "inverse_binds", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "set_inverse_binds", "get_inverse_binds"); // Vector<Transform3D>
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "joints"), "set_joints", "get_joints"); // Vector<GLTFNodeIndex>
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "non_joints"), "set_non_joints", "get_non_joints"); // Vector<GLTFNodeIndex>
ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "roots"), "set_roots", "get_roots"); // Vector<GLTFNodeIndex>
ADD_PROPERTY(PropertyInfo(Variant::INT, "skeleton"), "set_skeleton", "get_skeleton"); // int
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "joint_i_to_bone_i", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "set_joint_i_to_bone_i", "get_joint_i_to_bone_i"); // RBMap<int,
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "joint_i_to_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL), "set_joint_i_to_name", "get_joint_i_to_name"); // RBMap<int,
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "godot_skin", PROPERTY_HINT_RESOURCE_TYPE, "Skin"), "set_godot_skin", "get_godot_skin"); // Ref<Skin>
}
GLTFNodeIndex GLTFSkin::get_skin_root() {
return skin_root;
}
void GLTFSkin::set_skin_root(GLTFNodeIndex p_skin_root) {
skin_root = p_skin_root;
}
Vector<GLTFNodeIndex> GLTFSkin::get_joints_original() {
return joints_original;
}
void GLTFSkin::set_joints_original(Vector<GLTFNodeIndex> p_joints_original) {
joints_original = p_joints_original;
}
TypedArray<Transform3D> GLTFSkin::get_inverse_binds() {
return GLTFTemplateConvert::to_array(inverse_binds);
}
void GLTFSkin::set_inverse_binds(TypedArray<Transform3D> p_inverse_binds) {
GLTFTemplateConvert::set_from_array(inverse_binds, p_inverse_binds);
}
Vector<GLTFNodeIndex> GLTFSkin::get_joints() {
return joints;
}
void GLTFSkin::set_joints(Vector<GLTFNodeIndex> p_joints) {
joints = p_joints;
}
Vector<GLTFNodeIndex> GLTFSkin::get_non_joints() {
return non_joints;
}
void GLTFSkin::set_non_joints(Vector<GLTFNodeIndex> p_non_joints) {
non_joints = p_non_joints;
}
Vector<GLTFNodeIndex> GLTFSkin::get_roots() {
return roots;
}
void GLTFSkin::set_roots(Vector<GLTFNodeIndex> p_roots) {
roots = p_roots;
}
int GLTFSkin::get_skeleton() {
return skeleton;
}
void GLTFSkin::set_skeleton(int p_skeleton) {
skeleton = p_skeleton;
}
Dictionary GLTFSkin::get_joint_i_to_bone_i() {
return GLTFTemplateConvert::to_dictionary(joint_i_to_bone_i);
}
void GLTFSkin::set_joint_i_to_bone_i(Dictionary p_joint_i_to_bone_i) {
GLTFTemplateConvert::set_from_dictionary(joint_i_to_bone_i, p_joint_i_to_bone_i);
}
Dictionary GLTFSkin::get_joint_i_to_name() {
Dictionary ret;
HashMap<int, StringName>::Iterator elem = joint_i_to_name.begin();
while (elem) {
ret[elem->key] = String(elem->value);
++elem;
}
return ret;
}
void GLTFSkin::set_joint_i_to_name(Dictionary p_joint_i_to_name) {
joint_i_to_name = HashMap<int, StringName>();
for (const KeyValue<Variant, Variant> &kv : p_joint_i_to_name) {
joint_i_to_name[kv.key] = kv.value;
}
}
Ref<Skin> GLTFSkin::get_godot_skin() {
return godot_skin;
}
void GLTFSkin::set_godot_skin(Ref<Skin> p_godot_skin) {
godot_skin = p_godot_skin;
}
Error GLTFSkin::from_dictionary(const Dictionary &dict) {
ERR_FAIL_COND_V(!dict.has("skin_root"), ERR_INVALID_DATA);
skin_root = dict["skin_root"];
ERR_FAIL_COND_V(!dict.has("joints_original"), ERR_INVALID_DATA);
Array joints_original_array = dict["joints_original"];
joints_original.clear();
for (int i = 0; i < joints_original_array.size(); ++i) {
joints_original.push_back(joints_original_array[i]);
}
ERR_FAIL_COND_V(!dict.has("inverse_binds"), ERR_INVALID_DATA);
Array inverse_binds_array = dict["inverse_binds"];
inverse_binds.clear();
for (int i = 0; i < inverse_binds_array.size(); ++i) {
ERR_FAIL_COND_V(inverse_binds_array[i].get_type() != Variant::TRANSFORM3D, ERR_INVALID_DATA);
inverse_binds.push_back(inverse_binds_array[i]);
}
ERR_FAIL_COND_V(!dict.has("joints"), ERR_INVALID_DATA);
Array joints_array = dict["joints"];
joints.clear();
for (int i = 0; i < joints_array.size(); ++i) {
joints.push_back(joints_array[i]);
}
ERR_FAIL_COND_V(!dict.has("non_joints"), ERR_INVALID_DATA);
Array non_joints_array = dict["non_joints"];
non_joints.clear();
for (int i = 0; i < non_joints_array.size(); ++i) {
non_joints.push_back(non_joints_array[i]);
}
ERR_FAIL_COND_V(!dict.has("roots"), ERR_INVALID_DATA);
Array roots_array = dict["roots"];
roots.clear();
for (int i = 0; i < roots_array.size(); ++i) {
roots.push_back(roots_array[i]);
}
ERR_FAIL_COND_V(!dict.has("skeleton"), ERR_INVALID_DATA);
skeleton = dict["skeleton"];
ERR_FAIL_COND_V(!dict.has("joint_i_to_bone_i"), ERR_INVALID_DATA);
Dictionary joint_i_to_bone_i_dict = dict["joint_i_to_bone_i"];
joint_i_to_bone_i.clear();
for (const KeyValue<Variant, Variant> &kv : joint_i_to_bone_i_dict) {
int key = kv.key;
int value = kv.value;
joint_i_to_bone_i[key] = value;
}
ERR_FAIL_COND_V(!dict.has("joint_i_to_name"), ERR_INVALID_DATA);
Dictionary joint_i_to_name_dict = dict["joint_i_to_name"];
joint_i_to_name.clear();
for (const KeyValue<Variant, Variant> &kv : joint_i_to_name_dict) {
int key = kv.key;
StringName value = kv.value;
joint_i_to_name[key] = value;
}
if (dict.has("godot_skin")) {
godot_skin = dict["godot_skin"];
}
return OK;
}
Dictionary GLTFSkin::to_dictionary() {
Dictionary dict;
dict["skin_root"] = skin_root;
Array joints_original_array;
for (int i = 0; i < joints_original.size(); ++i) {
joints_original_array.push_back(joints_original[i]);
}
dict["joints_original"] = joints_original_array;
Array inverse_binds_array;
for (int i = 0; i < inverse_binds.size(); ++i) {
inverse_binds_array.push_back(inverse_binds[i]);
}
dict["inverse_binds"] = inverse_binds_array;
Array joints_array;
for (int i = 0; i < joints.size(); ++i) {
joints_array.push_back(joints[i]);
}
dict["joints"] = joints_array;
Array non_joints_array;
for (int i = 0; i < non_joints.size(); ++i) {
non_joints_array.push_back(non_joints[i]);
}
dict["non_joints"] = non_joints_array;
Array roots_array;
for (int i = 0; i < roots.size(); ++i) {
roots_array.push_back(roots[i]);
}
dict["roots"] = roots_array;
dict["skeleton"] = skeleton;
Dictionary joint_i_to_bone_i_dict;
for (HashMap<int, int>::Iterator E = joint_i_to_bone_i.begin(); E; ++E) {
joint_i_to_bone_i_dict[E->key] = E->value;
}
dict["joint_i_to_bone_i"] = joint_i_to_bone_i_dict;
Dictionary joint_i_to_name_dict;
for (HashMap<int, StringName>::Iterator E = joint_i_to_name.begin(); E; ++E) {
joint_i_to_name_dict[E->key] = E->value;
}
dict["joint_i_to_name"] = joint_i_to_name_dict;
dict["godot_skin"] = godot_skin;
return dict;
}

View File

@@ -0,0 +1,117 @@
/**************************************************************************/
/* gltf_skin.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "../gltf_defines.h"
#include "core/io/resource.h"
#include "scene/resources/3d/skin.h"
template <typename T>
class TypedArray;
class GLTFSkin : public Resource {
GDCLASS(GLTFSkin, Resource);
friend class GLTFDocument;
friend class SkinTool;
friend class FBXDocument;
private:
// The "skeleton" property defined in the gltf spec. -1 = Scene Root
GLTFNodeIndex skin_root = -1;
Vector<GLTFNodeIndex> joints_original;
Vector<Transform3D> inverse_binds;
// Note: joints + non_joints should form a complete subtree, or subtrees
// with a common parent
// All nodes that are skins that are caught in-between the original joints
// (inclusive of joints_original)
Vector<GLTFNodeIndex> joints;
// All Nodes that are caught in-between skin joint nodes, and are not
// defined as joints by any skin
Vector<GLTFNodeIndex> non_joints;
// The roots of the skin. In the case of multiple roots, their parent *must*
// be the same (the roots must be siblings)
Vector<GLTFNodeIndex> roots;
// The GLTF Skeleton this Skin points to (after we determine skeletons)
GLTFSkeletonIndex skeleton = -1;
// A mapping from the joint indices (in the order of joints_original) to the
// Godot Skeleton's bone_indices
HashMap<int, int> joint_i_to_bone_i;
HashMap<int, StringName> joint_i_to_name;
// The Actual Skin that will be created as a mapping between the IBM's of
// this skin to the generated skeleton for the mesh instances.
Ref<Skin> godot_skin;
protected:
static void _bind_methods();
public:
GLTFNodeIndex get_skin_root();
void set_skin_root(GLTFNodeIndex p_skin_root);
Vector<GLTFNodeIndex> get_joints_original();
void set_joints_original(Vector<GLTFNodeIndex> p_joints_original);
TypedArray<Transform3D> get_inverse_binds();
void set_inverse_binds(TypedArray<Transform3D> p_inverse_binds);
Vector<GLTFNodeIndex> get_joints();
void set_joints(Vector<GLTFNodeIndex> p_joints);
Vector<GLTFNodeIndex> get_non_joints();
void set_non_joints(Vector<GLTFNodeIndex> p_non_joints);
Vector<GLTFNodeIndex> get_roots();
void set_roots(Vector<GLTFNodeIndex> p_roots);
int get_skeleton();
void set_skeleton(int p_skeleton);
Dictionary get_joint_i_to_bone_i();
void set_joint_i_to_bone_i(Dictionary p_joint_i_to_bone_i);
Dictionary get_joint_i_to_name();
void set_joint_i_to_name(Dictionary p_joint_i_to_name);
Ref<Skin> get_godot_skin();
void set_godot_skin(Ref<Skin> p_godot_skin);
Dictionary to_dictionary();
Error from_dictionary(const Dictionary &dict);
};

View File

@@ -0,0 +1,57 @@
/**************************************************************************/
/* gltf_texture.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_texture.h"
void GLTFTexture::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_src_image"), &GLTFTexture::get_src_image);
ClassDB::bind_method(D_METHOD("set_src_image", "src_image"), &GLTFTexture::set_src_image);
ClassDB::bind_method(D_METHOD("get_sampler"), &GLTFTexture::get_sampler);
ClassDB::bind_method(D_METHOD("set_sampler", "sampler"), &GLTFTexture::set_sampler);
ADD_PROPERTY(PropertyInfo(Variant::INT, "src_image"), "set_src_image", "get_src_image"); // int
ADD_PROPERTY(PropertyInfo(Variant::INT, "sampler"), "set_sampler", "get_sampler"); // int
}
GLTFImageIndex GLTFTexture::get_src_image() const {
return src_image;
}
void GLTFTexture::set_src_image(GLTFImageIndex val) {
src_image = val;
}
GLTFTextureSamplerIndex GLTFTexture::get_sampler() const {
return sampler;
}
void GLTFTexture::set_sampler(GLTFTextureSamplerIndex val) {
sampler = val;
}

View File

@@ -0,0 +1,52 @@
/**************************************************************************/
/* gltf_texture.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "../gltf_defines.h"
#include "core/io/resource.h"
class GLTFTexture : public Resource {
GDCLASS(GLTFTexture, Resource);
private:
GLTFImageIndex src_image = -1;
GLTFTextureSamplerIndex sampler = -1;
protected:
static void _bind_methods();
public:
GLTFImageIndex get_src_image() const;
void set_src_image(GLTFImageIndex val);
GLTFTextureSamplerIndex get_sampler() const;
void set_sampler(GLTFTextureSamplerIndex val);
};

View File

@@ -0,0 +1,47 @@
/**************************************************************************/
/* gltf_texture_sampler.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "gltf_texture_sampler.h"
void GLTFTextureSampler::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_mag_filter"), &GLTFTextureSampler::get_mag_filter);
ClassDB::bind_method(D_METHOD("set_mag_filter", "filter_mode"), &GLTFTextureSampler::set_mag_filter);
ClassDB::bind_method(D_METHOD("get_min_filter"), &GLTFTextureSampler::get_min_filter);
ClassDB::bind_method(D_METHOD("set_min_filter", "filter_mode"), &GLTFTextureSampler::set_min_filter);
ClassDB::bind_method(D_METHOD("get_wrap_s"), &GLTFTextureSampler::get_wrap_s);
ClassDB::bind_method(D_METHOD("set_wrap_s", "wrap_mode"), &GLTFTextureSampler::set_wrap_s);
ClassDB::bind_method(D_METHOD("get_wrap_t"), &GLTFTextureSampler::get_wrap_t);
ClassDB::bind_method(D_METHOD("set_wrap_t", "wrap_mode"), &GLTFTextureSampler::set_wrap_t);
ADD_PROPERTY(PropertyInfo(Variant::INT, "mag_filter"), "set_mag_filter", "get_mag_filter");
ADD_PROPERTY(PropertyInfo(Variant::INT, "min_filter"), "set_min_filter", "get_min_filter");
ADD_PROPERTY(PropertyInfo(Variant::INT, "wrap_s"), "set_wrap_s", "get_wrap_s");
ADD_PROPERTY(PropertyInfo(Variant::INT, "wrap_t"), "set_wrap_t", "get_wrap_t");
}

View File

@@ -0,0 +1,159 @@
/**************************************************************************/
/* gltf_texture_sampler.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "scene/resources/material.h"
class GLTFTextureSampler : public Resource {
GDCLASS(GLTFTextureSampler, Resource);
public:
enum FilterMode {
NEAREST = 9728,
LINEAR = 9729,
NEAREST_MIPMAP_NEAREST = 9984,
LINEAR_MIPMAP_NEAREST = 9985,
NEAREST_MIPMAP_LINEAR = 9986,
LINEAR_MIPMAP_LINEAR = 9987
};
enum WrapMode {
CLAMP_TO_EDGE = 33071,
MIRRORED_REPEAT = 33648,
REPEAT = 10497,
DEFAULT = REPEAT
};
int get_mag_filter() const {
return mag_filter;
}
void set_mag_filter(const int filter_mode) {
mag_filter = (FilterMode)filter_mode;
}
int get_min_filter() const {
return min_filter;
}
void set_min_filter(const int filter_mode) {
min_filter = (FilterMode)filter_mode;
}
int get_wrap_s() const {
return wrap_s;
}
void set_wrap_s(const int wrap_mode) {
wrap_s = (WrapMode)wrap_mode;
}
int get_wrap_t() const {
return wrap_t;
}
void set_wrap_t(const int wrap_mode) {
wrap_s = (WrapMode)wrap_mode;
}
StandardMaterial3D::TextureFilter get_filter_mode() const {
using TextureFilter = StandardMaterial3D::TextureFilter;
switch (min_filter) {
case NEAREST:
return TextureFilter::TEXTURE_FILTER_NEAREST;
case LINEAR:
return TextureFilter::TEXTURE_FILTER_LINEAR;
case NEAREST_MIPMAP_NEAREST:
case NEAREST_MIPMAP_LINEAR:
return TextureFilter::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS;
case LINEAR_MIPMAP_NEAREST:
case LINEAR_MIPMAP_LINEAR:
default:
return TextureFilter::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
}
}
void set_filter_mode(StandardMaterial3D::TextureFilter mode) {
using TextureFilter = StandardMaterial3D::TextureFilter;
switch (mode) {
case TextureFilter::TEXTURE_FILTER_NEAREST:
min_filter = FilterMode::NEAREST;
mag_filter = FilterMode::NEAREST;
break;
case TextureFilter::TEXTURE_FILTER_LINEAR:
min_filter = FilterMode::LINEAR;
mag_filter = FilterMode::LINEAR;
break;
case TextureFilter::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
case TextureFilter::TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC:
min_filter = FilterMode::NEAREST_MIPMAP_LINEAR;
mag_filter = FilterMode::NEAREST;
break;
case TextureFilter::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS:
case TextureFilter::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC:
default:
min_filter = FilterMode::LINEAR_MIPMAP_LINEAR;
mag_filter = FilterMode::LINEAR;
break;
}
}
bool get_wrap_mode() const {
// BaseMaterial3D presents wrapping as a boolean property. Either the texture is repeated
// in both dimensions, non-mirrored, or it isn't repeated at all. This will cause oddities
// when people import models having other wrapping mode combinations.
return (wrap_s == WrapMode::REPEAT) && (wrap_t == WrapMode::REPEAT);
}
void set_wrap_mode(bool mat_repeats) {
if (mat_repeats) {
wrap_s = WrapMode::REPEAT;
wrap_t = WrapMode::REPEAT;
} else {
wrap_s = WrapMode::CLAMP_TO_EDGE;
wrap_t = WrapMode::CLAMP_TO_EDGE;
}
}
protected:
static void _bind_methods();
private:
FilterMode mag_filter = FilterMode::LINEAR;
FilterMode min_filter = FilterMode::LINEAR_MIPMAP_LINEAR;
WrapMode wrap_s = WrapMode::REPEAT;
WrapMode wrap_t = WrapMode::REPEAT;
};
VARIANT_ENUM_CAST(GLTFTextureSampler::FilterMode);
VARIANT_ENUM_CAST(GLTFTextureSampler::WrapMode);

View File

@@ -0,0 +1,147 @@
{
"asset":{
"generator":"Khronos glTF Blender I/O v4.2.70",
"version":"2.0"
},
"scene":0,
"scenes":[
{
"name":"Scene",
"nodes":[
1
]
}
],
"nodes":[
{
"mesh":0,
"name":"mesh_instance_3d"
},
{
"children":[
0
],
"name":"_Node3D_6"
}
],
"materials":[
{
"name":"material",
"pbrMetallicRoughness":{
"baseColorFactor":[
0.9999998807907104,
0.9999998807907104,
0.9999998807907104,
1
],
"baseColorTexture":{
"index":0
},
"metallicFactor":0
}
}
],
"meshes":[
{
"name":"Mesh_0",
"primitives":[
{
"attributes":{
"POSITION":0,
"NORMAL":1,
"TEXCOORD_0":2
},
"indices":3,
"material":0
}
]
}
],
"textures":[
{
"sampler":0,
"source":0
}
],
"images":[
{
"mimeType":"image/png",
"name":"material_albedo000",
"uri":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAAXNSR0IArs4c6QAAABZJREFUCJljZGBg+P+fgYGBBUIxMAAAKCAEAplLvcoAAAAASUVORK5CYII="
}
],
"accessors":[
{
"bufferView":0,
"componentType":5126,
"count":4,
"max":[
1,
0,
1
],
"min":[
-1,
0,
-1
],
"type":"VEC3"
},
{
"bufferView":1,
"componentType":5126,
"count":4,
"type":"VEC3"
},
{
"bufferView":2,
"componentType":5126,
"count":4,
"type":"VEC2"
},
{
"bufferView":3,
"componentType":5123,
"count":6,
"type":"SCALAR"
}
],
"bufferViews":[
{
"buffer":0,
"byteLength":48,
"byteOffset":0,
"target":34962
},
{
"buffer":0,
"byteLength":48,
"byteOffset":48,
"target":34962
},
{
"buffer":0,
"byteLength":32,
"byteOffset":96,
"target":34962
},
{
"buffer":0,
"byteLength":12,
"byteOffset":128,
"target":34963
}
],
"samplers":[
{
"magFilter":9729,
"minFilter":9987
}
],
"buffers":[
{
"byteLength":140,
"uri":"data:application/octet-stream;base64,AACAPwAAAAAAAIA/AACAvwAAAAAAAIA/AACAPwAAAAAAAIC/AACAvwAAAAAAAIC/AAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAACAPwAAgD8AAAAAAACAPwAAgD8AAAAAAAAAAAAAAAACAAEAAAACAAMAAQA="
}
]
}

View File

@@ -0,0 +1,147 @@
{
"asset":{
"generator":"Khronos glTF Blender I/O v4.2.70",
"version":"2.0"
},
"scene":0,
"scenes":[
{
"name":"Scene",
"nodes":[
1
]
}
],
"nodes":[
{
"mesh":0,
"name":"mesh_instance_3d"
},
{
"children":[
0
],
"name":"_Node3D_6"
}
],
"materials":[
{
"name":"material",
"pbrMetallicRoughness":{
"baseColorFactor":[
0.9999998807907104,
0.9999998807907104,
0.9999998807907104,
1
],
"baseColorTexture":{
"index":0
},
"metallicFactor":0
}
}
],
"meshes":[
{
"name":"Mesh_0",
"primitives":[
{
"attributes":{
"POSITION":0,
"NORMAL":1,
"TEXCOORD_0":2
},
"indices":3,
"material":0
}
]
}
],
"textures":[
{
"sampler":0,
"source":0
}
],
"images":[
{
"mimeType":"image/png",
"name":"material_albedo000",
"uri":"texture.png",
}
],
"accessors":[
{
"bufferView":0,
"componentType":5126,
"count":4,
"max":[
1,
0,
1
],
"min":[
-1,
0,
-1
],
"type":"VEC3"
},
{
"bufferView":1,
"componentType":5126,
"count":4,
"type":"VEC3"
},
{
"bufferView":2,
"componentType":5126,
"count":4,
"type":"VEC2"
},
{
"bufferView":3,
"componentType":5123,
"count":6,
"type":"SCALAR"
}
],
"bufferViews":[
{
"buffer":0,
"byteLength":48,
"byteOffset":0,
"target":34962
},
{
"buffer":0,
"byteLength":48,
"byteOffset":48,
"target":34962
},
{
"buffer":0,
"byteLength":32,
"byteOffset":96,
"target":34962
},
{
"buffer":0,
"byteLength":12,
"byteOffset":128,
"target":34963
}
],
"samplers":[
{
"magFilter":9729,
"minFilter":9987
}
],
"buffers":[
{
"byteLength":140,
"uri":"data:application/octet-stream;base64,AACAPwAAAAAAAIA/AACAvwAAAAAAAIA/AACAPwAAAAAAAIC/AACAvwAAAAAAAIC/AAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAACAPwAAgD8AAAAAAACAPwAAgD8AAAAAAAAAAAAAAAACAAEAAAACAAMAAQA="
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 B

View File

@@ -0,0 +1,147 @@
{
"asset":{
"generator":"Khronos glTF Blender I/O v4.2.70",
"version":"2.0"
},
"scene":0,
"scenes":[
{
"name":"Scene",
"nodes":[
1
]
}
],
"nodes":[
{
"mesh":0,
"name":"mesh_instance_3d"
},
{
"children":[
0
],
"name":"_Node3D_6"
}
],
"materials":[
{
"name":"material",
"pbrMetallicRoughness":{
"baseColorFactor":[
0.9999998807907104,
0.9999998807907104,
0.9999998807907104,
1
],
"baseColorTexture":{
"index":0
},
"metallicFactor":0
}
}
],
"meshes":[
{
"name":"Mesh_0",
"primitives":[
{
"attributes":{
"POSITION":0,
"NORMAL":1,
"TEXCOORD_0":2
},
"indices":3,
"material":0
}
]
}
],
"textures":[
{
"sampler":0,
"source":0
}
],
"images":[
{
"mimeType":"image/png",
"name":"material_albedo000",
"uri":"../texture.png",
}
],
"accessors":[
{
"bufferView":0,
"componentType":5126,
"count":4,
"max":[
1,
0,
1
],
"min":[
-1,
0,
-1
],
"type":"VEC3"
},
{
"bufferView":1,
"componentType":5126,
"count":4,
"type":"VEC3"
},
{
"bufferView":2,
"componentType":5126,
"count":4,
"type":"VEC2"
},
{
"bufferView":3,
"componentType":5123,
"count":6,
"type":"SCALAR"
}
],
"bufferViews":[
{
"buffer":0,
"byteLength":48,
"byteOffset":0,
"target":34962
},
{
"buffer":0,
"byteLength":48,
"byteOffset":48,
"target":34962
},
{
"buffer":0,
"byteLength":32,
"byteOffset":96,
"target":34962
},
{
"buffer":0,
"byteLength":12,
"byteOffset":128,
"target":34963
}
],
"samplers":[
{
"magFilter":9729,
"minFilter":9987
}
],
"buffers":[
{
"byteLength":140,
"uri":"data:application/octet-stream;base64,AACAPwAAAAAAAIA/AACAvwAAAAAAAIA/AACAPwAAAAAAAIC/AACAvwAAAAAAAIC/AAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAAAAAAAAgD8AAAAAAACAPwAAgD8AAAAAAACAPwAAgD8AAAAAAAAAAAAAAAACAAEAAAACAAMAAQA="
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 B

View File

@@ -0,0 +1,169 @@
/**************************************************************************/
/* test_gltf.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "tests/test_macros.h"
#ifdef TOOLS_ENABLED
#include "core/os/os.h"
#include "drivers/png/image_loader_png.h"
#include "editor/import/3d/resource_importer_scene.h"
#include "editor/import/resource_importer_texture.h"
#include "editor/inspector/editor_resource_preview.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/skeleton_3d.h"
#include "scene/main/window.h"
#include "scene/resources/3d/primitive_meshes.h"
#include "scene/resources/compressed_texture.h"
#include "scene/resources/material.h"
#include "scene/resources/packed_scene.h"
#include "tests/core/config/test_project_settings.h"
#include "modules/gltf/editor/editor_scene_importer_gltf.h"
#include "modules/gltf/gltf_document.h"
#include "modules/gltf/gltf_state.h"
namespace TestGltf {
static Node *gltf_import(const String &p_file) {
// Setting up importers.
Ref<ResourceImporterScene> import_scene;
import_scene.instantiate("PackedScene", true);
ResourceFormatImporter::get_singleton()->add_importer(import_scene);
Ref<EditorSceneFormatImporterGLTF> import_gltf;
import_gltf.instantiate();
ResourceImporterScene::add_scene_importer(import_gltf);
// Support processing png files in editor import.
Ref<ResourceImporterTexture> import_texture;
import_texture.instantiate(true);
ResourceFormatImporter::get_singleton()->add_importer(import_texture);
// Once editor import convert pngs to ctex, we will need to load it as ctex resource.
Ref<ResourceFormatLoaderCompressedTexture2D> resource_loader_stream_texture;
if (GD_IS_CLASS_ENABLED(CompressedTexture2D)) {
resource_loader_stream_texture.instantiate();
ResourceLoader::add_resource_format_loader(resource_loader_stream_texture);
}
HashMap<StringName, Variant> options(21);
options["nodes/root_type"] = "";
options["nodes/root_name"] = "";
options["nodes/root_script"] = Variant();
options["nodes/apply_root_scale"] = true;
options["nodes/root_scale"] = 1.0;
options["meshes/ensure_tangents"] = true;
options["meshes/generate_lods"] = false;
options["meshes/create_shadow_meshes"] = true;
options["meshes/light_baking"] = 1;
options["meshes/lightmap_texel_size"] = 0.2;
options["meshes/force_disable_compression"] = false;
options["skins/use_named_skins"] = true;
options["animation/import"] = true;
options["animation/fps"] = 30;
options["animation/trimming"] = false;
options["animation/remove_immutable_tracks"] = true;
options["import_script/path"] = "";
options["extract_path"] = "res://";
options["_subresources"] = Dictionary();
options["gltf/naming_version"] = 1;
// Process gltf file, note that this generates `.scn` resource from the 2nd argument.
String scene_file = "res://" + p_file.get_file().get_basename();
Error err = import_scene->import(0, p_file, scene_file, options, nullptr, nullptr, nullptr);
CHECK_MESSAGE(err == OK, "GLTF import failed.");
Ref<PackedScene> packed_scene = ResourceLoader::load(scene_file + ".scn", "", ResourceFormatLoader::CACHE_MODE_REPLACE, &err);
CHECK_MESSAGE(err == OK, "Loading scene failed.");
Node *p_scene = packed_scene->instantiate();
ResourceImporterScene::remove_scene_importer(import_gltf);
ResourceFormatImporter::get_singleton()->remove_importer(import_texture);
if (GD_IS_CLASS_ENABLED(CompressedTexture2D)) {
ResourceLoader::remove_resource_format_loader(resource_loader_stream_texture);
resource_loader_stream_texture.unref();
}
return p_scene;
}
static Node *gltf_export_then_import(Node *p_root, const String &p_test_name) {
String tempfile = TestUtils::get_temp_path(p_test_name);
Ref<GLTFDocument> doc;
doc.instantiate();
Ref<GLTFState> state;
state.instantiate();
Error err = doc->append_from_scene(p_root, state, EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS);
CHECK_MESSAGE(err == OK, "GLTF state generation failed.");
err = doc->write_to_filesystem(state, tempfile + ".gltf");
CHECK_MESSAGE(err == OK, "Writing GLTF to cache dir failed.");
return gltf_import(tempfile + ".gltf");
}
void init(const String &p_test, const String &p_copy_target = String()) {
Error err;
// Setup project settings since it's needed for the import process.
String project_folder = TestUtils::get_temp_path(p_test.get_file().get_basename());
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
da->make_dir_recursive(project_folder.path_join(".godot").path_join("imported"));
// Initialize res:// to `project_folder`.
TestProjectSettingsInternalsAccessor::resource_path() = project_folder;
err = ProjectSettings::get_singleton()->setup(project_folder, String(), true);
if (p_copy_target.is_empty()) {
return;
}
// Copy all the necessary test data files to the res:// directory.
da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
String test_data = String("modules/gltf/tests/data/").path_join(p_test);
da = DirAccess::open(test_data);
CHECK_MESSAGE(da.is_valid(), "Unable to open folder.");
da->list_dir_begin();
for (String item = da->get_next(); !item.is_empty(); item = da->get_next()) {
if (!FileAccess::exists(test_data.path_join(item))) {
continue;
}
Ref<FileAccess> output = FileAccess::open(p_copy_target.path_join(item), FileAccess::WRITE, &err);
CHECK_MESSAGE(err == OK, "Unable to open output file.");
output->store_buffer(FileAccess::get_file_as_bytes(test_data.path_join(item)));
output->close();
}
da->list_dir_end();
}
} //namespace TestGltf
#endif // TOOLS_ENABLED

View File

@@ -0,0 +1,175 @@
/**************************************************************************/
/* test_gltf_extras.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "test_gltf.h"
#include "tests/test_macros.h"
#ifdef TOOLS_ENABLED
#include "core/os/os.h"
#include "editor/import/3d/resource_importer_scene.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/skeleton_3d.h"
#include "scene/main/window.h"
#include "scene/resources/3d/primitive_meshes.h"
#include "scene/resources/material.h"
#include "scene/resources/packed_scene.h"
#include "modules/gltf/editor/editor_scene_importer_gltf.h"
#include "modules/gltf/gltf_document.h"
#include "modules/gltf/gltf_state.h"
namespace TestGltf {
TEST_CASE("[SceneTree][Node] GLTF test mesh and material meta export and import") {
init("gltf_mesh_material_extras");
// Setup scene.
Ref<StandardMaterial3D> original_material = memnew(StandardMaterial3D);
original_material->set_albedo(Color(1.0, .0, .0));
original_material->set_name("material");
Dictionary material_dict;
material_dict["node_type"] = "material";
original_material->set_meta("extras", material_dict);
Ref<PlaneMesh> original_meshdata = memnew(PlaneMesh);
original_meshdata->set_name("planemesh");
Dictionary meshdata_dict;
meshdata_dict["node_type"] = "planemesh";
original_meshdata->set_meta("extras", meshdata_dict);
original_meshdata->surface_set_material(0, original_material);
MeshInstance3D *original_mesh_instance = memnew(MeshInstance3D);
original_mesh_instance->set_mesh(original_meshdata);
original_mesh_instance->set_name("mesh_instance_3d");
Dictionary mesh_instance_dict;
mesh_instance_dict["node_type"] = "mesh_instance_3d";
original_mesh_instance->set_meta("extras", mesh_instance_dict);
Node3D *original = memnew(Node3D);
SceneTree::get_singleton()->get_root()->add_child(original);
original->add_child(original_mesh_instance);
original->set_name("node3d");
Dictionary node_dict;
node_dict["node_type"] = "node3d";
original->set_meta("extras", node_dict);
original->set_meta("meta_not_nested_under_extras", "should not propagate");
original->set_owner(SceneTree::get_singleton()->get_root());
original_mesh_instance->set_owner(SceneTree::get_singleton()->get_root());
// Convert to GLFT and back.
Node *loaded = gltf_export_then_import(original, "gltf_extras");
// Compare the results.
CHECK(loaded->get_name() == "node3d");
CHECK(Dictionary(loaded->get_meta("extras")).size() == 1);
CHECK(Dictionary(loaded->get_meta("extras"))["node_type"] == "node3d");
CHECK_FALSE(loaded->has_meta("meta_not_nested_under_extras"));
CHECK_FALSE(Dictionary(loaded->get_meta("extras")).has("meta_not_nested_under_extras"));
MeshInstance3D *mesh_instance_3d = Object::cast_to<MeshInstance3D>(loaded->find_child("mesh_instance_3d", false, true));
CHECK(mesh_instance_3d->get_name() == "mesh_instance_3d");
CHECK(Dictionary(mesh_instance_3d->get_meta("extras"))["node_type"] == "mesh_instance_3d");
Ref<Mesh> mesh = mesh_instance_3d->get_mesh();
CHECK(Dictionary(mesh->get_meta("extras"))["node_type"] == "planemesh");
Ref<Material> material = mesh->surface_get_material(0);
CHECK(material->get_name() == "material");
CHECK(Dictionary(material->get_meta("extras"))["node_type"] == "material");
memdelete(original_mesh_instance);
memdelete(original);
memdelete(loaded);
}
TEST_CASE("[SceneTree][Node] GLTF test skeleton and bone export and import") {
init("gltf_skeleton_extras");
// Setup scene.
Skeleton3D *skeleton = memnew(Skeleton3D);
skeleton->set_name("skeleton");
Dictionary skeleton_extras;
skeleton_extras["node_type"] = "skeleton";
skeleton->set_meta("extras", skeleton_extras);
skeleton->add_bone("parent");
skeleton->set_bone_rest(0, Transform3D());
Dictionary parent_bone_extras;
parent_bone_extras["bone"] = "i_am_parent_bone";
skeleton->set_bone_meta(0, "extras", parent_bone_extras);
skeleton->add_bone("child");
skeleton->set_bone_rest(1, Transform3D());
skeleton->set_bone_parent(1, 0);
Dictionary child_bone_extras;
child_bone_extras["bone"] = "i_am_child_bone";
skeleton->set_bone_meta(1, "extras", child_bone_extras);
// We have to have a mesh to link with skeleton or it will not get imported.
Ref<PlaneMesh> meshdata = memnew(PlaneMesh);
meshdata->set_name("planemesh");
MeshInstance3D *mesh = memnew(MeshInstance3D);
mesh->set_mesh(meshdata);
mesh->set_name("mesh_instance_3d");
Node3D *original = memnew(Node3D);
SceneTree::get_singleton()->get_root()->add_child(original);
original->add_child(skeleton);
original->add_child(mesh);
original->set_name("node3d");
// Now that both skeleton and mesh are part of scene, link them.
mesh->set_skeleton_path(mesh->get_path_to(skeleton));
mesh->set_owner(SceneTree::get_singleton()->get_root());
original->set_owner(SceneTree::get_singleton()->get_root());
// Convert to GLFT and back.
Node *loaded = gltf_export_then_import(original, "gltf_bone_extras");
// Compare the results.
CHECK(loaded->get_name() == "node3d");
Skeleton3D *result = Object::cast_to<Skeleton3D>(loaded->find_child("Skeleton3D", false, true));
CHECK(result->get_bone_name(0) == "parent");
CHECK(Dictionary(result->get_bone_meta(0, "extras"))["bone"] == "i_am_parent_bone");
CHECK(result->get_bone_name(1) == "child");
CHECK(Dictionary(result->get_bone_meta(1, "extras"))["bone"] == "i_am_child_bone");
memdelete(skeleton);
memdelete(mesh);
memdelete(original);
memdelete(loaded);
}
} //namespace TestGltf
#endif // TOOLS_ENABLED

View File

@@ -0,0 +1,172 @@
/**************************************************************************/
/* test_gltf_images.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#pragma once
#include "test_gltf.h"
#ifdef TOOLS_ENABLED
#include "editor/file_system/editor_file_system.h"
#include "editor/file_system/editor_paths.h"
#include "scene/resources/image_texture.h"
namespace TestGltf {
Ref<Texture2D> _check_texture(Node *p_node) {
MeshInstance3D *mesh_instance_3d = Object::cast_to<MeshInstance3D>(p_node->find_child("mesh_instance_3d", true, true));
Ref<StandardMaterial3D> material = mesh_instance_3d->get_active_material(0);
Ref<Texture2D> texture = material->get_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO);
CHECK_MESSAGE(texture->get_size().x == 2, "Texture width not correct.");
CHECK_MESSAGE(texture->get_size().y == 2, "Texture height not correct.");
// Check if the loaded texture pixels are exactly as we expect.
for (int x = 0; x < 2; ++x) {
for (int y = 0; y < 2; ++y) {
Color c = texture->get_image()->get_pixel(x, y);
CHECK_MESSAGE(c == Color(x, y, y), "Texture content is incorrect.");
}
}
return texture;
}
TEST_CASE("[SceneTree][Node] Export GLTF with external texture and import") {
init("gltf_images_external_export_import");
// Setup scene.
Ref<ImageTexture> original_texture;
original_texture.instantiate();
Ref<Image> image;
image.instantiate();
image->initialize_data(2, 2, false, Image::FORMAT_RGBA8);
for (int x = 0; x < 2; ++x) {
for (int y = 0; y < 2; ++y) {
image->set_pixel(x, y, Color(x, y, y));
}
}
original_texture->set_image(image);
Ref<StandardMaterial3D> original_material;
original_material.instantiate();
original_material->set_texture(StandardMaterial3D::TextureParam::TEXTURE_ALBEDO, original_texture);
original_material->set_name("material");
Ref<PlaneMesh> original_meshdata;
original_meshdata.instantiate();
original_meshdata->set_name("planemesh");
original_meshdata->surface_set_material(0, original_material);
MeshInstance3D *original_mesh_instance = memnew(MeshInstance3D);
original_mesh_instance->set_mesh(original_meshdata);
original_mesh_instance->set_name("mesh_instance_3d");
Node3D *original = memnew(Node3D);
SceneTree::get_singleton()->get_root()->add_child(original);
original->add_child(original_mesh_instance);
original->set_owner(SceneTree::get_singleton()->get_root());
original_mesh_instance->set_owner(SceneTree::get_singleton()->get_root());
// Convert to GLFT and back.
Node *loaded = gltf_export_then_import(original, "gltf_images");
_check_texture(loaded);
memdelete(original_mesh_instance);
memdelete(original);
memdelete(loaded);
}
TEST_CASE("[SceneTree][Node][Editor] Import GLTF from .godot/imported folder with external texture") {
init("gltf_placed_in_dot_godot_imported", "res://.godot/imported");
EditorFileSystem *efs = memnew(EditorFileSystem);
EditorResourcePreview *erp = memnew(EditorResourcePreview);
ERR_PRINT_OFF
Node *loaded = gltf_import("res://.godot/imported/gltf_placed_in_dot_godot_imported.gltf");
Ref<Texture2D> texture = _check_texture(loaded);
ERR_PRINT_ON
// In-editor imports of gltf and texture from .godot/imported folder should end up in res:// if extract_path is defined.
CHECK_MESSAGE(texture->get_path() == "res://gltf_placed_in_dot_godot_imported_material_albedo000.png", "Texture not parsed as resource.");
memdelete(loaded);
memdelete(erp);
memdelete(efs);
}
TEST_CASE("[SceneTree][Node][Editor] Import GLTF with texture outside of res:// directory") {
init("gltf_pointing_to_texture_outside_of_res_folder", "res://");
EditorFileSystem *efs = memnew(EditorFileSystem);
EditorResourcePreview *erp = memnew(EditorResourcePreview);
// Copy texture to the parent folder of res:// - i.e. to res://.. where we can't import from.
String oneup = TestUtils::get_temp_path("texture.png");
Error err;
Ref<FileAccess> output = FileAccess::open(oneup, FileAccess::WRITE, &err);
CHECK_MESSAGE(err == OK, "Unable to open texture file.");
output->store_buffer(FileAccess::get_file_as_bytes("res://texture_source.png"));
output->close();
ERR_PRINT_OFF
Node *loaded = gltf_import("res://gltf_pointing_to_texture_outside_of_res_folder.gltf");
Ref<Texture2D> texture = _check_texture(loaded);
ERR_PRINT_ON
// Imports of gltf with texture from outside of res:// folder should end up being copied to res://
CHECK_MESSAGE(texture->get_path() == "res://gltf_pointing_to_texture_outside_of_res_folder_material_albedo000.png", "Texture not parsed as resource.");
memdelete(loaded);
memdelete(erp);
memdelete(efs);
}
TEST_CASE("[SceneTree][Node][Editor] Import GLTF with embedded texture, check how it got extracted") {
init("gltf_embedded_texture", "res://");
EditorFileSystem *efs = memnew(EditorFileSystem);
EditorResourcePreview *erp = memnew(EditorResourcePreview);
ERR_PRINT_OFF
Node *loaded = gltf_import("res://embedded_texture.gltf");
Ref<Texture2D> texture = _check_texture(loaded);
ERR_PRINT_ON
// In-editor imports of texture embedded in file should end up with a resource.
CHECK_MESSAGE(texture->get_path() == "res://embedded_texture_material_albedo000.png", "Texture not parsed as resource.");
memdelete(loaded);
memdelete(erp);
memdelete(efs);
}
} //namespace TestGltf
#endif // TOOLS_ENABLED