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 foliage instancer #340

Merged
merged 3 commits into from
Jun 9, 2024
Merged

Add foliage instancer #340

merged 3 commits into from
Jun 9, 2024

Conversation

TokisanGames
Copy link
Owner

@TokisanGames TokisanGames commented Mar 24, 2024

Partially addresses #43
Partially addresses #368

This PR extends the asset dock to include mesh assets.
A PackedScene can be dropped in, meaning a .glb, .fbx asset, or a .tscn. It will search the scene for all MeshInstance3Ds and currently selects the first one found.

General Changes Done

  • Opacity renamed to strength, and >100 is allowed for all tools (merged)
  • Tool settings are now shown instead of hidden, including brush selector (merged)
  • CTRL Key negative input for Enable (merged)
  • Undo/redo redesign system (merged)
  • Redesign flexible asset dock that can move to all positions and save settings in EditorSettings
  • Rename Terrain3DTexture -> Terrain3DTextureAsset
  • Rename Terrain3DTextureList -> Terrain3DAssets

Foliage Instancer Done

  • Quadmesh populates where painted into an MMI, with correct height and normal
  • Restrict foliage brush to ring
  • Instancing evenly distributed within circle indicator, fix square placement
  • Remove MM instances (with CTRL, or Enable unchecked)
  • Create an Instancer resource class that can be saved
  • Create UI for mesh assets and textures
  • Create instanceable mesh object
  • Create MMIs in chunks
  • Move data to Storage
  • Create instanced object settings for:
    • Height offset (adjust origin)
    • Quad mesh w/ texture and/or material
    • Custom mesh
  • Tool settings toggle for applying ground normal, height offset
  • Check default asset dock position w/ default settings, place bottom right
  • Change generated mesh card option
  • Undo/redo for MMIs
  • Document

Pushed to future PRs

  • Drag and drop into asset dock for meshes
  • Adjust heights on sculpting
  • Improve generated grass: enum, Meshless, vertex count, pointy, bend, gradient, wind
  • AO/sample ground?
  • Create & destroy small MMI segments dynamically. Waiting on WIP: Make collision generation dynamic #278
  • Smaller MMI chunks 32-64/region
  • Collision generation
  • Apply transforms in scene file
  • LODs, except what is provided by MMI and autolod
  • Handle other meshes in scene file? - probably not

image

@TokisanGames TokisanGames marked this pull request as draft March 24, 2024 08:30
@TokisanGames TokisanGames force-pushed the foliage-instancer branch 2 times, most recently from 1815d8b to 1ab45f0 Compare March 25, 2024 09:15
@Saul2022
Copy link

Looking great as far as i tested(ik it pretty early , but couldn't wait to check it out) even with shadows seem's like performance increase a bit as it hiding the terrain mesh, though something i notice is that using transform in terrain changes the grass location

@TokisanGames
Copy link
Owner Author

Alright, I have disabled transforms.

@TokisanGames
Copy link
Owner Author

TokisanGames commented Apr 7, 2024

I did a major overhaul on the asset dock and need testers. I'll merge this commit into the main tree once it's polished, before this foliage PR is finished.

image

What should work and needs testing:

  • Move asset dock to bottom or any sidebar position
  • Resize icons
  • Texture selection/adding/removing/editing settings should still work like normal
  • Switching between scenes w/ separate Terrain3D instances open the texture list should update
  • Save the layout settings and reload them on restart - Note you must save your project before this will work, as this configuration is saved in Project Settings
  • Popout to a window w/ following behavior:
    • Pin that window so its always on top or not
    • Saving the project (CTRL+S) while the window is focused
    • Window Focus follows the mouse between godot and asset dock so that the mouse cursor and painting works fine in the viewport

Using these options do work, but they don't fire any signals that I can detect without hacks
image

But I made my own buttons which should be used for moving position or popping out
image

Other notes:

  • Mesh/Texture buttons do nothing, but will act like tabs
  • Needs to be cleaned up and prints a lot of troubleshooting to the console.

This binary can be tested:
https://github.com/TokisanGames/Terrain3D/actions/runs/8587341009

@Saul2022
Copy link

Saul2022 commented Apr 17, 2024

Testing:

1 Moving the dock on every sidebar or bottom seem´s to work well through when the terrain 3d node is selected and the asset dock is at the bottom it not possible to see the other tabs, meanwhile when you diselect the node and click on the tab it works well without overlapping the other docks. ( althought that is fixed when you make the editor fullscreen.

editor_screenshot_2024-04-17T184339
editor_screenshot_2024-04-17T184550
editor_screenshot_2024-04-17T184651

2 Resizing works but when you make it way too big it not possible to edit the corresponding texture as you can´t acces the three dots or delete.

editor_screenshot_2024-04-17T184847

3 Works as usual

4 I am not sure what you mean, but as far as i tried having two scenes with the same textures/ material list save to save, for example i add one new material to a scene, then save and then on the other scene the textures remain as positioned and the same as the one i recently saved.

5 Don´t have layout.

6not sure if it fine or made something wrong but sseems that when pinned it stays on screen within other tabs and when saving it stays on the same position it was put before saving, though when minimizing the godot app the screen stays in other programs unless you minimize it too, so maybe when minimizing godot you could make it so the pinned dock is also minimized, same when you open it again.

@TokisanGames
Copy link
Owner Author

Thanks for testing @Saul2022 .

  1. when the terrain 3d node is selected and the asset dock is at the bottom it not possible to see the other tabs,

This when Godot is in a window, or maybe a narrow height screen resolution? This is due to the minimum height of the panel. The other bottom tabs do the same thing. Especially click Audio, which has a very tall minimum height. The bar drops way below.

2 Resizing works but when you make it way too big it not possible to edit the corresponding texture as you can´t acces the three dots or delete.

You can right-click to edit or middle click to clear/delete.

4 I am not sure what you mean,

Two scenes open: Demo and imported. Each has a copy of Terrain3D, with different storage and asset dock. Switching back and forth between the scenes, the asset dock should populate with their respective textures, at least if Terrain3D is selected.

5 Don´t have layout.

Layout is the dock position, size, pinned. This is saved to project settings so should be retained when you restart godot, as long as it doesn't crash.

6 not sure if it fine or made something wrong but sseems that when pinned it stays on screen within other tabs

The pin means always on top. It should stay on screen.

and when saving it stays on the same position it was put before saving,

The dock should stay at the position it was in before you saved. I don't understand. You move the doc to bottom right, then save it, it should stay at the bottom right.

so maybe when minimizing godot you could make it so the pinned dock is also minimized, same when you open it again.

That's a good idea. I don't know if I have access to detect that, though.

@TokisanGames
Copy link
Owner Author

Updates:

  • Reduced maximum size of tiles so the delete/edit buttons don't slide off the sidebar
  • Windowed asset dock now hides when Godot is minimized
  • Window focusing much more subtle and effective

Bottom bar disappearing I'll ignore for now as I think it's a godot issue, as they all do it.

@TokisanGames TokisanGames force-pushed the foliage-instancer branch 3 times, most recently from e5d450f to 5e1855b Compare April 26, 2024 03:46
@darrylryan
Copy link

I see no reason why you couldn't also paint trees and bushes using this same system. The only differences would be:

  1. The spacing would need to be calculated from the AABB of the mesh
  2. The density would need to be capped much lower for large meshes.
  3. We couldn't draw complex meshes into the far distance and far away chunks couldn't just "disappear" for items large enough to be seen from miles away or it would look weird. A good solution would be to use this: https://github.com/zhangjt93/godot-imposter to generate an impostor for each mesh and then set the MM to the real tree for closest chunk and all the further away chunks could just be impostors (which in effect work much like the current grass system).

@TokisanGames TokisanGames force-pushed the foliage-instancer branch 3 times, most recently from 2167432 to e7d2212 Compare April 28, 2024 13:45
@TokisanGames
Copy link
Owner Author

TokisanGames commented May 3, 2024

Update:

  • The asset dock can now load packed scenes for meshes. It will automatically locate the first mesh to use for instancing.
  • There's an option for generated grass, which is currently just the basic green quad.
  • Multiple MMIs are created or destroyed when painting, based on location and mesh id. Currently this breaks down by region, but will be smaller segments later. This is saveable.

image

@Saul2022
Copy link

Saul2022 commented May 9, 2024

Tested on latest action and seems like when deleting an mesh instance, engine crashes inmediatly.

Also modifying the new properties like color, height etc,seems to not work with neither the generated blade nor a custom mesh.

Furthermore when you try replace the terrain plugin folder on an existing project that had it already(which in this case was the one used to test last weeks grass project) it made the scene corrupted for some reason.

And grass keeps dissapearing whenever i switch between scenes ( in this case a custom billboarder scene)

@TokisanGames
Copy link
Owner Author

Tested on latest action and seems like when deleting an mesh instance, engine crashes inmediatly.

Ok, I see, thanks. You mean removing a MeshAsset from the asset dock, not removing an instance with CTRL.

Also modifying the new properties like color, height etc,seems to not work with neither the generated blade nor a custom mesh.

Only the UI elements for the tool settings are there. The functionality hasn't been implemented yet.

Furthermore when you try replace the terrain plugin folder on an existing project that had it already(which in this case was the one used to test last weeks grass project) it made the scene corrupted for some reason.

Not "corrupted", just unparseable because you saved a resource with a different name inside. Easily fixable by editing the file to the new resource name.

Whatever you tested on last week is incompatible. Assuming you're using the demo, reset the project back to the main branch. If using your own project, that's dangerous. Reset your terrain3D resource files.

And grass keeps dissapearing whenever i switch between scenes ( in this case a custom billboarder scene)

Ok, I see.

Thanks for the reports.

@Saul2022
Copy link

Saul2022 commented May 9, 2024

Tested on latest action and seems like when deleting an mesh instance, engine crashes inmediatly.

Ok, I see, thanks. You mean removing a MeshAsset from the asset dock, not removing an instance with CTRL.

The first yea

@TokisanGames TokisanGames self-assigned this May 25, 2024
@TokisanGames
Copy link
Owner Author

TokisanGames commented May 27, 2024

Update:

  • Fixed meshes disappearing when changing scenes
  • Fixed crash on deleting MeshAsset from dock
  • Countless improvements
  • Implemented all paintable instancer settings:
    • height offset (per mesh and painted), scale, rotation w/ random options
    • Vertex color, randomized hue shift and darken
    • Materials must have vertex_color_use_as_albedo enabled for this to work.
    • Random hue shift shifts the color specified in vertex color. If saturation is 0, the hue shift won't be visible.

image

image

image

  • Data from older versions of this PR are invalid, revert terrain_storage.res to main

I need help with one thing in this bit of code: fixed

// Orientation
Vector3 normal = Vector3(0.f, 1.f, 0.f);
if (align_to_normal) {
normal = _terrain->get_storage()->get_normal(position);
if (std::isnan(normal.x)) {
normal = Vector3(0.f, 1.f, 0.f);
} else {
normal = normal.normalized();
Vector3 z_axis = Vector3(0.f, 0.f, 1.f);
Vector3 crossp = -z_axis.cross(normal);
t.basis = Basis(crossp, normal, z_axis).orthonormalized();
}
}
real_t rotation = (fixed_rotation + random_rotation * UtilityFunctions::randf()) * Math_PI / 180.f;
Vector3 rotation_axis;
if (rotate_y_only) {
rotation_axis = normal;
t.basis = t.basis.rotated(rotation_axis, rotation);
} else {
rotation_axis = Vector3(UtilityFunctions::randf() - .5f, UtilityFunctions::randf() * .5f,
UtilityFunctions::randf() - .5f);
rotation_axis = rotation_axis.normalized();
t.basis = t.basis.looking_at(rotation_axis);
}
// Scale
real_t t_scale = CLAMP(fixed_scale + random_scale * (UtilityFunctions::randf() - .5f), 0.01f, 100.f);
t = t.scaled(Vector3(t_scale, t_scale, t_scale));
// Position
real_t offset = height_offset + mesh_asset->get_height_offset() * t_scale + random_height * (UtilityFunctions::randf() - .5f);
position += rotation_axis * offset; // Offset along rotation axis
t = t.translated(position);

  • align_to_normal: works, object grows perpendicular to terrain face
  • rotate_y_only: works, object rotates around it's local y, even if align_to_normal is enabled
  • height_offset: works for both above options and offsets the mesh along its normal

What doesn't work is if rotate_y_only is disabled, regardless of align_to_normal or not, I want the base to stick in the ground and the top to point in any direction.

You can see the tops do go in any direction, but some of the bases float above the ground.

image

@TokisanGames TokisanGames mentioned this pull request May 27, 2024
14 tasks
@TokisanGames
Copy link
Owner Author

Fixed rotation, refined other settings, and changed the generated mesh to a default card that can be textured.

This PR can start being tested with this build. Backup your storage and texture list resources, and scene.

The last feature to add is undo/redo, which will come soon.

@TokisanGames
Copy link
Owner Author

TokisanGames commented Jun 1, 2024

Undo/redo now works with the instancer. This PR is feature complete. We need testers so we can merge it in a few days. Test this build. Backup your scene/storage/asset resources.

https://github.com/TokisanGames/Terrain3D/actions/runs/9331382106

@TokisanGames TokisanGames changed the title WIP: Add foliage instancer Add foliage instancer Jun 1, 2024
@TokisanGames TokisanGames marked this pull request as ready for review June 1, 2024 12:59
@JekSun97
Copy link

JekSun97 commented Jun 1, 2024

@TokisanGames everything works fine, except that the player cannot collide with static objects, although there is collision, or is that intended?

I also noticed a strange thing, I created this cube, slightly elongated on the sides
1
but in the docking station, and when it is placed, it becomes not elongated, but an ordinary cube
2

Now I edited the Mesh itself and not the MeshInstance3D and everything became fine

@TokisanGames
Copy link
Owner Author

Thanks for testing. As stated, no collision generation in this PR.

The PR takes the mesh from the first meshinstance in the file. Did you make the boxmesh long in its settings? Or did you scale it? Do the former.

@JekSun97
Copy link

JekSun97 commented Jun 1, 2024

Everything is fine, I just didn’t immediately understand that only the size of the mesh itself is taken into account, and not its scale in Transform.

Are there any plans in the future to consider the possibility of scattering scenes with all their data throughout the scene, despite the loss of optimization?

@TokisanGames
Copy link
Owner Author

Read the bottom of the top post for plans. All data, no. Meshes, static collision, possibly transforms.
This isn't a scene placer. It's a multimeshinstance painter. To place scenes use scatter or AssetPlacer.

@Saul2022
Copy link

Saul2022 commented Jun 5, 2024

Testing a bit, and as far as i can check undo redo, seems to not worl( doing Crtl z and nothing happens with the meshinstance, neither it does on history tab).

Then when i tried using a tree ( that composed of two meshes, the foliage and the trunk itself)as an instance, it only took into account the foliage, without the poor trunk .

editor_screenshot_2024-06-05T120252

Then when i joined those in blender using the append function, and importing to godot as a glb it rotation worked just fine in blender and using godot base transform, but when appilying as an terrain3d instance, this happens. And adjusting the rotation in godot doesn´´t help.

editor_screenshot_2024-06-05T115557

@TokisanGames
Copy link
Owner Author

Thanks for testing. 4.3 is definitely not supported, please test with 4.2.2. Your undo tab should fill up with every operation, including instancing.

It currently uses only the first mesh instance it finds in the scene file.

It looks like through your importing and exporting you roasted the transform of the tree. I bet in your scene it's currently rotated, and the default transform is laying flat. Reset the transforms in blender and import again, ensuring all nodes in your scene has an empty transform.

@Saul2022
Copy link

Saul2022 commented Jun 5, 2024

Thanks for testing. 4.3 is definitely not supported, please test with 4.2.2. Your undo tab should fill up with every operation, including instancing.

ok fine though 4.3 as far as it goes really looks like working (except on that damm 4.3 beta 1) though ye at the end downloading a new version of 4.2 worked, though for 4.3 beta 1 everything with parses broke(not plugin fault).

@TokisanGames
Copy link
Owner Author

ok fine though 4.3 as far as it goes really looks like working

Except your undo/redo doesn't work, and many things in the new version, and now it "upgraded" and broke your scenes so you can't go back easily. That's why we won't touch it until the RCs.

I'm going to merge this in. Thanks to the couple of testers. More improvements will come as in subsequent phases but this should be usable now for more testers.

@Saul2022
Copy link

Saul2022 commented Jun 9, 2024

Except your undo/redo doesn't work, and many things in the new version, and now it "upgraded" and broke your scenes so you can't go back easily. That's why we won't touch it until the RCs

Well as for scenes breaking it was me being a complete blind(disability dumb stuff) not seeing that i installed 4.1 beta instead of 4.3 beta1 😅, atleast i had had copies alr so i didn't loose anything.

I'm going to merge this in. Thanks to the couple of testers.

Nice

@TokisanGames TokisanGames merged commit b7d0976 into main Jun 9, 2024
13 checks passed
@TokisanGames
Copy link
Owner Author

Merged. Track additional improvements in #43

@TokisanGames TokisanGames deleted the foliage-instancer branch June 9, 2024 11:00
@TokisanGames
Copy link
Owner Author

Except your undo/redo doesn't work

I have no problems using 4.3 beta 1 with a nightly build with this PR merged in. Mouse cursor, Undo/redo works fine.

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

Successfully merging this pull request may close these issues.

4 participants