tefusion / godot-subdiv

Fast Subdivision in Godot with opensubdiv
https://godotengine.org/asset-library/asset/1488
MIT License
47 stars 1 forks source link

Set GLTFQuadImporter's global subivision to allow baking #13

Closed fire closed 2 years ago

fire commented 2 years ago

Set res://addons/godot_subdiv/quad_import_plugin.gd 's subdivision from 0 to a integer number to allow baking in the options menu or hard coded.

fire commented 2 years ago

May use convert_meshinstance_to_quad(i, subidivsion_level)

fire commented 2 years ago

Can expose https://github.com/tefusion/godot-subdiv/blob/c059952d75ab9e1ae770c12914edc8dc5db27b87/src/subdiv_types/subdivision_mesh.cpp#L308 as an ImporterMesh

ImporterMesh.add_surface(primitive: PrimitiveType, arrays: Array, blend_shapes: Array [] = [], lods: Dictionary = {}, material: Material = null, name: String = "", flags: int = 0) then ImporterMesh.get_mesh() as an ArrayMesh

tefusion commented 2 years ago

Can expose

https://github.com/tefusion/godot-subdiv/blob/c059952d75ab9e1ae770c12914edc8dc5db27b87/src/subdiv_types/subdivision_mesh.cpp#L308 as an ImporterMesh

ImporterMesh.add_surface(primitive: PrimitiveType, arrays: Array, blend_shapes: Array [] = [], lods: Dictionary = {}, material: Material = null, name: String = "", flags: int = 0) then ImporterMesh.get_mesh() as an ArrayMesh

Seems like a good idea to me. Cause I think baking at import will be done not that often (only benefit I see is not having to keep a large glb/gltf file that already is subdivided around) an import setting would be better here (bool bake and below int level). We should also make a seperate function for this in subdivision_mesh like bake(p_mesh, int level, bool generate_lods, bool bake_skinning) cause there might be a need to bake bones and weights as well. I'll likely seperate the last part https://github.com/tefusion/godot-subdiv/blob/c059952d75ab9e1ae770c12914edc8dc5db27b87/src/subdiv_types/subdivision_mesh.cpp#L272-L306 into an extra function to make that easier for quads.

fire commented 2 years ago

One benefit of baking is I can use the lod system of Godot Engine 4 on a subdivision level set to 1 or 2 and pretend that the godot-subdiv doesn't exist. Like zero configuration of the final subdivision level.

fire commented 2 years ago

@tefusion I had some trouble enabling lod generation. It appears to be an order of import operations problem.

Edited:

I think it needs to be in the importer

Edited 2:

Import baking isn't implemented, but for what I can tell, it's putting the Facebaked mesh into a ImporterMesh3D

tefusion commented 2 years ago

I'm currently working on a SubdivisionBaker class for the baking (which will then be called within the importer for arraymesh). The functions I currently have are:

Ref<ArrayMesh> get_array_mesh(const Ref<ArrayMesh> &p_base, const Ref<TopologyDataMesh> &p_topology_data_mesh, int32_t p_level, bool generate_lods);

and

Ref<ImporterMesh> get_importer_mesh(const Ref<ImporterMesh> &p_base, const Ref<TopologyDataMesh> &p_topology_data_mesh, int32_t p_level);

where the arraymesh method will call the importermesh one and then call generate_lods.

Another question: Do you remember if you got baking the bone/weights data working, it should work just like UV's or no? Haven't got it working yet.

Edit: After doing some more research and finding nothing on how to do it with opensubdiv directly I'll assume it can't handle it and just do it on CPU.

Edit 2: Got it running, but still need to test large bone amounts cause what it does is just keep track of weights to every bone per vertex and interpolate those

fire commented 2 years ago

Note!

If you leave a ImporterMesh and ImporterMesh3D without lods, it will get auto generated by the scene importer system

tefusion commented 2 years ago

Baking for skinning added in 7879bb9cbc52d2e062b5ddadfded22176276e34f and baking for blendshapes added in 7ed344dc4853a637ff4501cc0bd131f62272fe2b. So this is pretty much done now. The only issue left for this is LOD generation. Imo the importer should produce an ArrayMesh, so lods need to be generated by code.

tefusion commented 2 years ago

Lods are now working with 39847c796bc3255f058229bc10f392829d1f924f. The normal scene importer now has a subdivision setting section that bakes before the normal importer stuff starts. Also thanks for telling that the conversion to meshinstance happens later, helped a lot finding this solution.