Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic preview generation, item removal/reordering and automatic mesh scaling to the MeshLibrary editor #11493

Open
gdevgodot opened this issue Jan 5, 2025 · 2 comments

Comments

@gdevgodot
Copy link

gdevgodot commented Jan 5, 2025

Describe the project you are working on

This proposal aims to improve the usability of the MeshLibrary system in Godot by streamlining the process of adding, managing, and previewing meshes. Key features include automating preview generation, adding methods to remove specific items, and improving the overall workflow to make it more intuitive and efficient.

Describe the problem or limitation you are having in your project

The current MeshLibrary workflow in Godot is functional but cumbersome, particularly when dealing with larger libraries. Specific issues include:

  1. Complex item addition: Users must manually add a Mesh and configure its preview texture, making the process time-consuming and error-prone.
  2. Lack of basic management tools: There is no direct method to remove a specific item or reorder items in the library, complicating library maintenance.
  3. Manual preview creation: Preview textures must be manually generated and assigned, even though this step could be automated by the engine.
  4. Mesh scaling issues: Currently, there is no integration between MeshLibrary and GridMap to automatically adjust mesh sizes to match the grid size. Users must manually ensure their meshes are scaled properly, leading to inconsistencies.

These issues limit the usability of MeshLibrary, especially for beginners or projects requiring large numbers of meshes.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Introduce a more intuitive mechanism for adding items to the MeshLibrary:

  • Add a button labeled "Add Item" to the MeshLibrary inspector.
  • Prompt the user to select a Mesh file through a file dialog.
  • Automatically assign a unique ID, link the mesh, and generate its preview.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

  1. Streamline Mesh Addition
@export_storage var idx : int = 0
@export_group("Add Item")
@export var name : String =  ""
@export var mesh : Mesh
@export var cell_size : Vector3 = Vector3(1, 1, 1)
@export_tool_button("Add") var add_item = _add_item

func _add_item():
  create_item(idx)
  set_item_mesh(idx, mesh)
  set_item_name(idx, name)
  set_item_mesh_transform(idx, Transform3D(Vector3(cell_size.x, 0, 0), Vector3(0, cell_size.y, 0), Vector3(0, 0, cell_size.z), 
  Vector3(0, 0, 0)))
  set_item_preview(idx, generate_preview_from_mesh(mesh))
  idx += 1
  1. Add Methods for Item Management Enhance the MeshLibrary API to include methods for:

Removing items by ID:

func remove_item(id: int):
    if id in get_item_list():
        erase_item(id)
        return true
    return false

Reordering items:

func reorder_items(new_order: Array):
    var current_items = get_item_list()
    for i in range(new_order.size()):
        if new_order[i] in current_items:
            move_item(new_order[i], i)
  1. Automate Preview Generation

Implement a system to generate previews directly from the provided Mesh. This could utilize Godot’s rendering pipeline to automatically render the mesh in a Viewport and use the resulting texture as the preview.

  1. Automatically Adjust Mesh Size to GridMap Cell Size
    Introduce functionality to dynamically adjust the size of meshes in the MeshLibrary to match the cell size of the associated GridMap.

Implementation Steps:

  • Add a method to the MeshLibrary API to retrieve the GridMap’s cell size.
  • Automatically scale meshes when they are added to the library to fit the grid dimensions.
func adjust_mesh_size_to_grid(mesh: Mesh, grid_cell_size: Vector3) -> Mesh:
    var original_aabb = mesh.get_aabb()
    var scale_factor = grid_cell_size / original_aabb.size
    var scaled_mesh = mesh.duplicate()
    scaled_mesh.scale(scale_factor)
    return scaled_mesh

Editor Integration:

When a Mesh is added to the library, the editor checks the associated GridMap and adjusts the mesh size accordingly.
A toggle in the MeshLibrary inspector allows users to enable or disable automatic size adjustment.

An additional example with a custom feature:

custom_mesh_library.mp4

If this enhancement will not be used often, can it be worked around with a few lines of script?

Can it be worked around with a few lines of script?
Yes, the issues outlined can be partially addressed with custom scripts or add-ons. For example:

  • Automating preview generation can be achieved with a small script that renders meshes in a Viewport to produce textures.
  • Adding or removing items programmatically can be done with basic scripting.

However, these solutions require extra work from users for what should ideally be built-in functionality. Integrating these features into the core would provide a unified, consistent, and out-of-the-box experience.

Is there a reason why this should be core and not an add-on in the asset library?

  1. Core relevance: MeshLibrary is an integral part of Godot’s 3D workflow, particularly for GridMap usage. Improving its usability aligns with Godot’s philosophy of providing powerful, intuitive tools natively.
  2. Ease of use: A built-in solution ensures that all users benefit from these enhancements without needing to install or maintain external add-ons.
  3. Standardization: Making this part of the core ensures a consistent experience for all users and avoids potential compatibility issues that might arise with third-party add-ons.
@Calinou Calinou changed the title Improving MeshLibrary Usability in Godot Add automatic preview generation, item removal/reordering and automatic mesh scaling to the MeshLibrary editor Jan 5, 2025
@Calinou
Copy link
Member

Calinou commented Jan 5, 2025

@Hiiamwilliam
Copy link

  1. Lack of basic management tools: There is no direct method to remove a specific item or reorder items in the library

There already is a method to remove items though? Or, do you mean a method to remove items by name?

About reordering: I assume that's not available because of how GridMap relies on item ID to work. So if you reordered the MeshLibrary item IDs, a GridMap that uses said MeshLibrary would repaint cells with different meshes.

  1. Automatically Adjust Mesh Size to GridMap Cell Size

A toggle in the MeshLibrary inspector allows users to enable or disable automatic size adjustment.

I imagine a minority of meshes would benefit from this, it seems too specific.

Also, generally Resources have no awareness of the Nodes using them, so a setting to make the MeshLibrary adjust its meshes based on the GridMap's cell size is odd.
If the meshes are potentially changing based on the GridMap, might as well make this a GridMap option. That would make more sense, even though I still think it's a functionality with limited impact on usability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants