From f9847bcb3775489d938d1e6e1d8d5f8949952b84 Mon Sep 17 00:00:00 2001 From: Spencer Killen Date: Sat, 25 May 2024 18:27:35 -0600 Subject: [PATCH] Add addon to support grid maps and tweak it to support collision and materials --- .../.gitignore | 24 +++ .../LICENSE | 21 +++ .../README.md | 26 ++++ .../addons/gltf2meshlib/gltf2meshlib.gd | 16 ++ .../addons/gltf2meshlib/gltf2meshlib.png | 3 + .../addons/gltf2meshlib/gltf2meshlib.svg | 93 ++++++++++++ .../addons/gltf2meshlib/importer.gd | 138 ++++++++++++++++++ .../addons/gltf2meshlib/plugin.cfg | 7 + blends/level_mesh_library.blend | 3 + level/level.tscn | 9 +- level/mesh_library/Cube.gdshader | 7 + level/mesh_library/Cube.tres | 8 + level/mesh_library/TallCube.gdshader | 7 + level/mesh_library/TallCube.tres | 8 + level/mesh_library/level_mesh_library.glb | 3 + .../level_mesh_library.glb.import | 14 ++ project.godot | 5 + tests/foo.tscn | 8 + 18 files changed, 399 insertions(+), 1 deletion(-) create mode 100644 addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/.gitignore create mode 100644 addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/LICENSE create mode 100644 addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/README.md create mode 100644 addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/gltf2meshlib.gd create mode 100644 addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/gltf2meshlib.png create mode 100644 addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/gltf2meshlib.svg create mode 100644 addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/importer.gd create mode 100644 addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/plugin.cfg create mode 100644 blends/level_mesh_library.blend create mode 100644 level/mesh_library/Cube.gdshader create mode 100644 level/mesh_library/Cube.tres create mode 100644 level/mesh_library/TallCube.gdshader create mode 100644 level/mesh_library/TallCube.tres create mode 100644 level/mesh_library/level_mesh_library.glb create mode 100644 level/mesh_library/level_mesh_library.glb.import create mode 100644 tests/foo.tscn diff --git a/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/.gitignore b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/.gitignore new file mode 100644 index 0000000..25f7424 --- /dev/null +++ b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/.gitignore @@ -0,0 +1,24 @@ +# this file shouldnt be in your project. If you have it, remove it. + +# Godot 4+ specific ignores +.godot/ + +# Godot-specific ignores +.import/ +export.cfg +export_presets.cfg + +# Imported translations (automatically generated from CSV files) +*.translation + +# Mono-specific ignores +.mono/ +data_*/ +mono_crash.*.json + +# Ignore any import metadata files +*.import + +# Ignore the generated project files +project.godot + diff --git a/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/LICENSE b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/LICENSE new file mode 100644 index 0000000..a29938a --- /dev/null +++ b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Zincles + +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. \ No newline at end of file diff --git a/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/README.md b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/README.md new file mode 100644 index 0000000..2528993 --- /dev/null +++ b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/README.md @@ -0,0 +1,26 @@ +![GLTF2MeshLib](addons/gltf2meshlib/gltf2meshlib.svg) + +# GLTF2MeshLib + +This plugin allows you to import gltf/glb models as MeshLibrary, which saves a lot of time compared to manual importing. + +In the manual case, you would need to create an inherited scene of the gltf scene, reparent all mesh nodes (otherwise only the first mesh will be exported), and manually "export as MeshLibrary". + +With this plugin, you can simply drag and drop gltf/glb files into the editor, select "GLTF To MeshLibrary" in the import settings, and it will automatically import your gltf/glb as a MeshLibrary. This allows you to see instant changes after any modification of the gltf file. + +In my case, I used models made with Blockbench, exported as gltf, and imported into Godot with this plugin, and it seems to work fine as I expected. I am using Godot 4.3. + +If you encounter any problems, or if you have any suggestions, please let me know in the Issues. + +## Installation + +you can simply install this plugin in Godot's built-in asset library. +If you prefers manual installation, you can download the zip file from the release page, and extract it to the root folder of your project. + +## Contributors + +This plugin is made by Zincles with help of many cool people( but I havent asked them if they are okay with me listing their names here, sorry :p ) + +## License + +License under MIT License. diff --git a/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/gltf2meshlib.gd b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/gltf2meshlib.gd new file mode 100644 index 0000000..1b655eb --- /dev/null +++ b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/gltf2meshlib.gd @@ -0,0 +1,16 @@ +@tool +extends EditorPlugin + +var importer: EditorImportPlugin + + +# Init importer +func _enter_tree(): + importer = preload("importer.gd").new() + add_import_plugin(importer) + + +# deactivate importer +func _exit_tree(): + remove_import_plugin(importer) + importer = null diff --git a/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/gltf2meshlib.png b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/gltf2meshlib.png new file mode 100644 index 0000000..9584eac --- /dev/null +++ b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/gltf2meshlib.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ed5ca9721d377ea86597ef005a99f110f60799aefb2248f6aeb313ee169b861d +size 23484 diff --git a/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/gltf2meshlib.svg b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/gltf2meshlib.svg new file mode 100644 index 0000000..2929e4f --- /dev/null +++ b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/gltf2meshlib.svg @@ -0,0 +1,93 @@ + + + + diff --git a/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/importer.gd b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/importer.gd new file mode 100644 index 0000000..e9f0f6f --- /dev/null +++ b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/importer.gd @@ -0,0 +1,138 @@ +@tool +class_name GLTF2MeshLibraryImporter +extends EditorImportPlugin + +#region plugin_definitions + +# In my case the plugin works fine by default. +# If you have any suggestions, please let me know in Github Issues. + + +func _get_importer_name(): + return "zincles.gltf2meshlib" + + +func _get_visible_name(): + return "GLTF To MeshLibrary" + + +func _get_recognized_extensions(): + return ["gltf", "glb"] + + +func _get_save_extension(): + return "meshlib" + + +func _get_resource_type(): + return "MeshLibrary" + + +func _get_preset_count(): + return 1 + + +func _get_preset_name(preset_index): + return "Default" + + +func _get_import_options(path, preset_index): + #return [{"name": "my_option", "default_value": false}] + return [] + + +func _get_option_visibility(path, option_name, options): + return true + + +func _get_import_order(): + return 9999 + + +#endregion + +#region importer_logics. + + +func _is_ImporterMeshInstance3D(node: Node) -> bool: + #print("Node: ", node) + if is_instance_of(node, ImporterMeshInstance3D): + return true + return false + + +# Found all nodes, Pack them into an Array. +# The matcher should accept one argument (Node),returns a bool. +func find_first_matched_nodes(root_node: Node, matcher: Callable) -> Array[Node]: + var result: Array[Node] = [] + + for node: Node in root_node.get_children(): + var matches: bool = matcher.call(node) + if matches == true: + result.append(node) + continue + else: + var matched_nodes := find_first_matched_nodes(node, matcher) + result.append_array(matched_nodes) + return result + + +func _import(gltf_path: String, save_path, options, platform_variants, gen_files): + # Init. + #print("Source File: ", gltf_path) + #print("Save Path: ", save_path) + + var root_node: Node + var meshLib: MeshLibrary = MeshLibrary.new() + var file = FileAccess.open(gltf_path, FileAccess.READ) + if file == null: + print("Error: File Not Found!") + return ERR_PARSE_ERROR + + # load the GLTF file, init as Node. + var gltf_document_load := GLTFDocument.new() + var gltf_state_load := GLTFState.new() + var error := gltf_document_load.append_from_file(gltf_path, gltf_state_load) + if error == OK: + root_node = gltf_document_load.generate_scene(gltf_state_load) + else: + print("Error: %s " % error_string(error)) + return error + + # Get all MeshInstance3D nodes. + var mesh_nodes := find_first_matched_nodes(root_node, _is_ImporterMeshInstance3D) + + # Add all meshes into MeshLibrary. + var i := 0 + for mesh_node: ImporterMeshInstance3D in mesh_nodes: + var mesh: ArrayMesh = mesh_node.mesh.get_mesh() # Gets the mesh + + meshLib.create_item(i) + meshLib.set_item_mesh(i, mesh) + meshLib.set_item_name(i, mesh_node.name) + + # Spencer: Referencing https://github.com/Zincles/godot_gltf2meshlib/pull/1/files + var shape := mesh.create_trimesh_shape() + meshLib.set_item_shapes(i, [shape]) + + # Spencer: Another change (slightly hacky) + for mat_i in range(mesh.get_surface_count()): + var path := gltf_path + "/../" + mesh.surface_get_name(mat_i) + ".tres" + if ResourceLoader.exists(path): + var mat: Material = load(path) + mesh.surface_set_material(mat_i, mat) + else: + print("No material found at path: " + path) + print("Using built-in material instead") + + var preview: Array[Texture2D] = EditorInterface.make_mesh_previews([mesh], 64) + meshLib.set_item_preview(i, preview[0]) + + i += 1 + + # Save + root_node.queue_free() + var filename = save_path + "." + _get_save_extension() + return ResourceSaver.save(meshLib, filename) + +#endregion diff --git a/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/plugin.cfg b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/plugin.cfg new file mode 100644 index 0000000..f16daf8 --- /dev/null +++ b/addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/plugin.cfg @@ -0,0 +1,7 @@ +[plugin] + +name="gltf2meshlib" +description="A plugin that allows you to import gltf/glb as MeshLibrary, without taking a lot of manual steps." +author="Zincles" +version="0.1" +script="gltf2meshlib.gd" diff --git a/blends/level_mesh_library.blend b/blends/level_mesh_library.blend new file mode 100644 index 0000000..db25e89 --- /dev/null +++ b/blends/level_mesh_library.blend @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:633273d7a81029b9cb0983e1d58c8bcc9320cd73c3a8bbbc3a36fc6ed02f14dd +size 941840 diff --git a/level/level.tscn b/level/level.tscn index 013c897..d7fb2eb 100644 --- a/level/level.tscn +++ b/level/level.tscn @@ -1,10 +1,11 @@ -[gd_scene load_steps=8 format=3 uid="uid://b00brfkibo5cj"] +[gd_scene load_steps=9 format=3 uid="uid://b00brfkibo5cj"] [ext_resource type="PackedScene" uid="uid://bq654gwim6col" path="res://level/level.glb" id="1_s37in"] [ext_resource type="Environment" uid="uid://covjrwmk4rplw" path="res://level/world_environment.tres" id="2_ptkl6"] [ext_resource type="Script" path="res://level/level.gd" id="2_s1bx6"] [ext_resource type="Script" path="res://addons/smoother/smoother.gd" id="5_2tyle"] [ext_resource type="Script" path="res://level/PlayerSpawner.gd" id="6_7ww0m"] +[ext_resource type="MeshLibrary" uid="uid://cgh6y5j8wgi36" path="res://level/mesh_library/level_mesh_library.glb" id="6_d34iv"] [sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_ujmev"] margin = 2.067 @@ -42,3 +43,9 @@ script = ExtResource("6_7ww0m") [node name="Smoother" type="Node" parent="." index="5"] script = ExtResource("5_2tyle") + +[node name="GridMap" type="GridMap" parent="." index="6"] +mesh_library = ExtResource("6_d34iv") +data = { +"cells": PackedInt32Array(65533, 65533, 1, 0, 65533, 0, 4, 65532, 0, 4, 65533, 0, 5, 65533, 0, 6, 65534, 0, 6, 65533, 0, 5, 65532, 0, 65535, 65531, 0, 0, 65530, 0, 1, 65530, 0, 4, 65528, 0, 3, 65528, 0, 65534, 65528, 0, 2, 65533, 0, 2, 2, 1, 7, 4, 0, 7, 3, 0, 7, 2, 0, 7, 1, 0, 7, 0, 0, 7, 65535, 0, 65533, 6, 0, 65534, 6, 0, 65535, 6, 0, 0, 6, 0, 1, 6, 0, 4, 9, 0, 4, 8, 0, 4, 7, 0, 4, 6, 0, 3, 7, 1, 8, 6, 1) +} diff --git a/level/mesh_library/Cube.gdshader b/level/mesh_library/Cube.gdshader new file mode 100644 index 0000000..7a18f68 --- /dev/null +++ b/level/mesh_library/Cube.gdshader @@ -0,0 +1,7 @@ +shader_type spatial; + +uniform vec3 color : source_color; + +void fragment() { + ALBEDO = color; +} diff --git a/level/mesh_library/Cube.tres b/level/mesh_library/Cube.tres new file mode 100644 index 0000000..e704b5d --- /dev/null +++ b/level/mesh_library/Cube.tres @@ -0,0 +1,8 @@ +[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://bf3jwbci2dp72"] + +[ext_resource type="Shader" path="res://level/mesh_library/Cube.gdshader" id="1_mi50m"] + +[resource] +render_priority = 0 +shader = ExtResource("1_mi50m") +shader_parameter/color = Color(0.2079, 0.531417, 0.353382, 1) diff --git a/level/mesh_library/TallCube.gdshader b/level/mesh_library/TallCube.gdshader new file mode 100644 index 0000000..7a18f68 --- /dev/null +++ b/level/mesh_library/TallCube.gdshader @@ -0,0 +1,7 @@ +shader_type spatial; + +uniform vec3 color : source_color; + +void fragment() { + ALBEDO = color; +} diff --git a/level/mesh_library/TallCube.tres b/level/mesh_library/TallCube.tres new file mode 100644 index 0000000..beca5f5 --- /dev/null +++ b/level/mesh_library/TallCube.tres @@ -0,0 +1,8 @@ +[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://cd67s24dcpqid"] + +[ext_resource type="Shader" path="res://level/mesh_library/TallCube.gdshader" id="1_3trds"] + +[resource] +render_priority = 0 +shader = ExtResource("1_3trds") +shader_parameter/color = Color(1, 0.734185, 0.691036, 1) diff --git a/level/mesh_library/level_mesh_library.glb b/level/mesh_library/level_mesh_library.glb new file mode 100644 index 0000000..a6d26a3 --- /dev/null +++ b/level/mesh_library/level_mesh_library.glb @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0052af8b890b35fb71668357112e25ca160e8d8729ef349900b083b610d34b92 +size 3448 diff --git a/level/mesh_library/level_mesh_library.glb.import b/level/mesh_library/level_mesh_library.glb.import new file mode 100644 index 0000000..967be56 --- /dev/null +++ b/level/mesh_library/level_mesh_library.glb.import @@ -0,0 +1,14 @@ +[remap] + +importer="zincles.gltf2meshlib" +type="MeshLibrary" +uid="uid://cgh6y5j8wgi36" +path="res://.godot/imported/level_mesh_library.glb-a98c6f38a714c03818db0f0b044793d1.meshlib" + +[deps] + +source_file="res://level/mesh_library/level_mesh_library.glb" +dest_files=["res://.godot/imported/level_mesh_library.glb-a98c6f38a714c03818db0f0b044793d1.meshlib"] + +[params] + diff --git a/project.godot b/project.godot index c0e01f0..31e6bd1 100644 --- a/project.godot +++ b/project.godot @@ -36,6 +36,11 @@ config/icon="res://icon.svg" [autoload] MultiplayerEvents="*res://server/MultiplayerEvents.gd" +FmodManager="*res://addons/fmod/FmodManager.gd" + +[editor_plugins] + +enabled=PackedStringArray("res://addons/fmod/plugin.cfg", "res://addons/godot_gltf2meshlib-6391ec247cc04f5fe89243b24fad68bc2ed2a96b/addons/gltf2meshlib/plugin.cfg") [input] diff --git a/tests/foo.tscn b/tests/foo.tscn new file mode 100644 index 0000000..f6493cf --- /dev/null +++ b/tests/foo.tscn @@ -0,0 +1,8 @@ +[gd_scene load_steps=2 format=3 uid="uid://3ksclx7iiepo"] + +[sub_resource type="MeshLibrary" id="MeshLibrary_8y7oa"] + +[node name="Foo" type="Node3D"] + +[node name="GridMap" type="GridMap" parent="."] +mesh_library = SubResource("MeshLibrary_8y7oa")