diff --git a/README.md b/README.md index 155f9cb..990dcd2 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![License](https://img.shields.io/crates/l/blenvy)](https://github.com/kaosat-dev/Blenvy/blob/main/LICENSE.md) [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/F1F5TO32O) -# BLENVY: a friendly Blender <=> Bevy workflow ! +# BLENVY: a friendly Blender <=> Bevy workflow ![demo](./docs/blender_bevy.png) @@ -13,6 +13,10 @@ inside Blender. Aka "Blender as editor for Bevy" It also allows you to setup 'blueprints' in Blender by using collections (the recomended way to go most of the time), or directly on single use objects . +## Quickstart + +Want to jump right in? See the [quickstart guide](./docs/quickstart/readme.md) for how to setup a basic project as fast as possible. + ## Features * Useful if you want to use Blender as your Editor @@ -23,8 +27,8 @@ It also allows you to setup 'blueprints' in Blender by using collections (the re * automatically load all assets for each blueprint (gltf files, manually added assets), with no setup required * hot reload of your levels & blueprints * minimal setup & code, you can have something basic running fast -* minimal dependencies: Bevy, Serde & Ron only ! -* opensource +* minimal dependencies: Bevy, Serde & RON only! +* opensource > If you were previously using the individual bevy_gltf_xxx crates & Blender add-ons please see the [migration guide](./Migration_guide.md) @@ -32,26 +36,26 @@ It also allows you to setup 'blueprints' in Blender by using collections (the re One crate to rule them all ! -- [blenvy](./crates/blenvy/) This crate allows you to - * define components direclty inside gltf files and instanciate/inject the components on the Bevy side. - * export your project's Bevy registry to json, in order to be able to generate custom component UIs on the Blender side in the Blender [blenvy](./tools/blenvy/README.md) add-on - * define Blueprints/Prefabs for Bevy inside gltf files and spawn them in Bevy. With the ability to override and add components when spawning, efficient "level" loading etc - * the ability to save & load your game state in a relatively simple way, by leveraging the blueprint functionality to only save a minimal subset of dynamic data, seperating dynamic & static parts of levels etc. +* [blenvy](./crates/blenvy/) This crate allows you to + * define components direclty inside gltf files and instanciate/inject the components on the Bevy side. + * export your project's Bevy registry to json, in order to be able to generate custom component UIs on the Blender side in the Blender [blenvy](./tools/blenvy/README.md) add-on + * define Blueprints/Prefabs for Bevy inside gltf files and spawn them in Bevy. With the ability to override and add components when spawning, efficient "level" loading etc + * the ability to save & load your game state in a relatively simple way, by leveraging the blueprint functionality to only save a minimal subset of dynamic data, seperating dynamic & static parts of levels etc. OLD videos: There is a [video tutorial/explanation](https://youtu.be/-lcScjQCA3c) if you want, or you can read the crate docs. There is a [video tutorial/explanation](https://youtu.be/CgyNtwgYwdM) for this one too, or you can read the crate docs - The examples for the crate are [here](./examples/blenvy/) + The examples for the crate are [here](./examples/blenvy/) ## Tools ### Blender: blenvy -- an all in one [Blender addon](./tools/blenvy/README.md) for the Blender side of the workflow: - - allow easilly adding & editing Bevy components , using automatically generated UIs for each component - - automatically exports your level/world from Blender to gltf whenever you save your Blend file - - automatically export your [Gltf blueprints](./crates/blenvy/README.md) & assets +* an all in one [Blender addon](./tools/blenvy/README.md) for the Blender side of the workflow: + * allow easilly adding & editing Bevy components , using automatically generated UIs for each component + * automatically exports your level/world from Blender to gltf whenever you save your Blend file + * automatically export your [Gltf blueprints](./crates/blenvy/README.md) & assets ## Examples @@ -67,37 +71,42 @@ you can find all examples, [here](./examples/blenvy) The workflow goes as follows (once you got your Bevy code setup) - -- create & register all your components you want to be able to set from the Blender side (this is basic Bevy, no specific work needed) +* create & register all your components you want to be able to set from the Blender side (this is basic Bevy, no specific work needed) ![component registration](./docs/component_registration.png) -- setup & then use the Blenvy [Bevy crate](./crates/blenvy/README.md) -- setup & then use the Blenvy [Blender add-on](./tools/blenvy/README.md) -- iterate -- have fun ! +* setup & then use the Blenvy [Bevy crate](./crates/blenvy/README.md) +* setup & then use the Blenvy [Blender add-on](./tools/blenvy/README.md) +* iterate +* have fun ! -- then add your components to objects in Blender **with a nice UI** see [here](./README-workflow-ui.md) for more details +* then add your components to objects in Blender **with a nice UI** see [here](./README-workflow-ui.md) for more details + +See the [quickstart](./docs/quickstart/readme.md) for a full step-by-step guide. + +## Third Party Integration + +Read about the [Avian Physics Integration](docs/avian/readme) to learn how to setup colliders in Blender that will be used by the Avian physics engine in Bevy. ## Limitations / issues -- Some of `avian` or `bevy_rapier` /physics code / ways to define colliders could perhaps be done better/visually within Blender +* Some of `avian` or `bevy_rapier` /physics code / ways to define colliders could perhaps be done better/visually within Blender ## Contributors Thanks to all the contributors helping out with this project ! Big kudos to you, contributions are always appreciated ! :) -- [GitGhillie](https://github.com/GitGhillie) -- [Azorlogh](https://github.com/Azorlogh) -- [BSDGuyShawn](https://github.com/BSDGuyShawn) -- [yukkop](https://github.com/yukkop) -- [killercup](https://github.com/killercup) -- [janhohenheim ](https://github.com/janhohenheim) -- [BUGO07](https://github.com/BUGO07) +* [GitGhillie](https://github.com/GitGhillie) +* [Azorlogh](https://github.com/Azorlogh) +* [BSDGuyShawn](https://github.com/BSDGuyShawn) +* [yukkop](https://github.com/yukkop) +* [killercup](https://github.com/killercup) +* [janhohenheim](https://github.com/janhohenheim) +* [BUGO07](https://github.com/BUGO07) ## License This repo, all its code, contents & assets is Dual-licensed under either of -- Apache License, Version 2.0, ([LICENSE-APACHE](./LICENSE_APACHE.md) or https://www.apache.org/licenses/LICENSE-2.0) -- MIT license ([LICENSE-MIT](./LICENSE_MIT.md) or https://opensource.org/licenses/MIT) +* Apache License, Version 2.0, ([LICENSE-APACHE](./LICENSE_APACHE.md) or ) +* MIT license ([LICENSE-MIT](./LICENSE_MIT.md) or ) diff --git a/docs/avian/img/create_cylinder.png b/docs/avian/img/create_cylinder.png new file mode 100644 index 0000000..7989d7c Binary files /dev/null and b/docs/avian/img/create_cylinder.png differ diff --git a/docs/avian/img/create_cylinder_options.png b/docs/avian/img/create_cylinder_options.png new file mode 100644 index 0000000..90ca88f Binary files /dev/null and b/docs/avian/img/create_cylinder_options.png differ diff --git a/docs/avian/img/cube_primitive.png b/docs/avian/img/cube_primitive.png new file mode 100644 index 0000000..190a1a6 Binary files /dev/null and b/docs/avian/img/cube_primitive.png differ diff --git a/docs/avian/img/cylinder.png b/docs/avian/img/cylinder.png new file mode 100644 index 0000000..fc41f79 Binary files /dev/null and b/docs/avian/img/cylinder.png differ diff --git a/docs/avian/img/cylinder_collider.png b/docs/avian/img/cylinder_collider.png new file mode 100644 index 0000000..427c2dd Binary files /dev/null and b/docs/avian/img/cylinder_collider.png differ diff --git a/docs/avian/img/cylinder_collider_on_mesh.png b/docs/avian/img/cylinder_collider_on_mesh.png new file mode 100644 index 0000000..b23a082 Binary files /dev/null and b/docs/avian/img/cylinder_collider_on_mesh.png differ diff --git a/docs/avian/img/data.png b/docs/avian/img/data.png new file mode 100644 index 0000000..2f8005a Binary files /dev/null and b/docs/avian/img/data.png differ diff --git a/docs/avian/img/dimensions.png b/docs/avian/img/dimensions.png new file mode 100644 index 0000000..cf44348 Binary files /dev/null and b/docs/avian/img/dimensions.png differ diff --git a/docs/avian/img/dynamic.png b/docs/avian/img/dynamic.png new file mode 100644 index 0000000..f1443ec Binary files /dev/null and b/docs/avian/img/dynamic.png differ diff --git a/docs/avian/img/empty_child.png b/docs/avian/img/empty_child.png new file mode 100644 index 0000000..1737788 Binary files /dev/null and b/docs/avian/img/empty_child.png differ diff --git a/docs/avian/img/empty_collections.png b/docs/avian/img/empty_collections.png new file mode 100644 index 0000000..efce220 Binary files /dev/null and b/docs/avian/img/empty_collections.png differ diff --git a/docs/avian/img/empty_scaled.png b/docs/avian/img/empty_scaled.png new file mode 100644 index 0000000..187d189 Binary files /dev/null and b/docs/avian/img/empty_scaled.png differ diff --git a/docs/avian/img/empty_selected.png b/docs/avian/img/empty_selected.png new file mode 100644 index 0000000..a9e36c9 Binary files /dev/null and b/docs/avian/img/empty_selected.png differ diff --git a/docs/avian/img/empty_world.png b/docs/avian/img/empty_world.png new file mode 100644 index 0000000..60c20d5 Binary files /dev/null and b/docs/avian/img/empty_world.png differ diff --git a/docs/avian/img/falling.gif b/docs/avian/img/falling.gif new file mode 100644 index 0000000..770e722 Binary files /dev/null and b/docs/avian/img/falling.gif differ diff --git a/docs/avian/img/falling_dyn.gif b/docs/avian/img/falling_dyn.gif new file mode 100644 index 0000000..c072863 Binary files /dev/null and b/docs/avian/img/falling_dyn.gif differ diff --git a/docs/avian/img/ground collider.png b/docs/avian/img/ground collider.png new file mode 100644 index 0000000..c1575da Binary files /dev/null and b/docs/avian/img/ground collider.png differ diff --git a/docs/avian/img/hiding.png b/docs/avian/img/hiding.png new file mode 100644 index 0000000..a65042f Binary files /dev/null and b/docs/avian/img/hiding.png differ diff --git a/docs/avian/img/parenting.png b/docs/avian/img/parenting.png new file mode 100644 index 0000000..659ccce Binary files /dev/null and b/docs/avian/img/parenting.png differ diff --git a/docs/avian/img/select_mesh.png b/docs/avian/img/select_mesh.png new file mode 100644 index 0000000..9f41f8e Binary files /dev/null and b/docs/avian/img/select_mesh.png differ diff --git a/docs/avian/img/three_object_collection.png b/docs/avian/img/three_object_collection.png new file mode 100644 index 0000000..dc74b4d Binary files /dev/null and b/docs/avian/img/three_object_collection.png differ diff --git a/docs/avian/img/torus.png b/docs/avian/img/torus.png new file mode 100644 index 0000000..17ca216 Binary files /dev/null and b/docs/avian/img/torus.png differ diff --git a/docs/avian/img/torus_component.png b/docs/avian/img/torus_component.png new file mode 100644 index 0000000..2631933 Binary files /dev/null and b/docs/avian/img/torus_component.png differ diff --git a/docs/avian/img/wireframe.png b/docs/avian/img/wireframe.png new file mode 100644 index 0000000..277fee4 Binary files /dev/null and b/docs/avian/img/wireframe.png differ diff --git a/docs/avian/readme.md b/docs/avian/readme.md new file mode 100644 index 0000000..9d61008 --- /dev/null +++ b/docs/avian/readme.md @@ -0,0 +1,408 @@ +# Avian Physics Integration + +This guide assumes that you have a basic Blenvy setup ready to tinker in. +If you don't have that yet, please refer to the [quickstart](../quickstart/readme.md) guide. + +## Add Avian to Bevy + +No big surprises here. Simply add `avian3d` as a dependency by running the following from your project root: + +```sh +cargo add avian3d +``` + +Then, where you add plugins to your Bevy app, add the `PhysicsPlugins::default()`. +The most basic `main.rs` that contains a full setup looks like this: + +```rust +use avian3d::prelude::*; +use bevy::prelude::*; +use blenvy::*; + +fn main() -> AppExit { + App::new() + .add_plugins(( + DefaultPlugins, + BlenvyPlugin::default(), + PhysicsPlugins::default(), + )) + .add_systems(Startup, setup) + .run() +} + +fn setup(mut commands: Commands) { + commands.spawn(( + BlueprintInfo::from_path("levels/World.glb"), + SpawnBlueprint, + HideUntilReady, + GameWorldTag, + )); +} +``` + +Run this once with `cargo.run` to generate a `registry.json` that contains the Avian components. + +## Prepare the Blueprints + +Set up your `World` and `Library` scenes in Blender. Switch to the `Library` scene. +If you're coming from the [quickstart](../quickstart/readme.md) guide, you may now delete the `Player` collection by +right-clicking it in the outliner and selecting `Delete Hierarchy` as we don't need it in this guide. +Remember, you can find the outliner all the way to the right. + +We will be showing different ways to add colliders, so we need to add a blueprint for each approach. +Create three new collections in the outliner by doing `rightclick` -> `New Collection` and name them as follows: + +- Cube +- Board +- Cylinder + +Your outliner should now look like this: + +
+Our empty collections + +
+ +If you accidentally created a collection as a child of another, simply drag-and-drop them around to reorder them until they look like the image above. + +### Cube + +Click on the `Cube` collection we just created to select it. Then, go to `Add` -> `Mesh` -> `Cube` in the upper left corner to add a cube to the collection. Leave it at the default transform. + +### Board + +Click on the `Board` collection. Again, go to `Add` -> `Mesh` -> `Cube`. This time, scale it until it looks like a flat board: + +
+The cylinder in Blender + +
+ +> [!TIP] +> The above screenshot was made after disabling the visibility of the `Cube` collection by clicking the eye icon in the outliner. +> +>
+> Hiding objects +> +>
+> +> Hiding other collections becomes quickly essential when working with blueprints. + +The scaling we used was the following: + +- X: `2.5` +- Y: `0.5` +- Z: `1.5` + +### Cylinder + +Finally, click on the `Cylinder` collection. Go to `Add` -> `Mesh` -> `Cylinder`. Leave it at the default transform. + +You should now have three collections with different shapes in them: +
+Collections with objects in the outliner + +
+ +## Add RigidBody Components + +Avian makes a distinction between a *rigid body* and its associated *colliders*. +In general, the best practice is to have a parent object be a rigid body and then have at least one descendant object be a collider. + +Adding the `RigidBody` is the same for all approaches: + +- select the object in the viewport +- go to the Blenvy menu's component manager. Remember, if are missing the side menu, you can open it with `N`. +- type `rigidbody` in the search bar +- select `avian3d::dynamics::rigid_body::RigidBody` +- add it + +> [!TIP] +> If you do not see `avian3d::dynamics::rigid_body::RigidBody` in the list of components, make sure you have run a `cargo run` after the `PhysicsPlugins::default()` was added to your Bevy app as described above. +> If you still do not see the component, manually refresh the registry as described in the [quickstart section "Create a blueprint"](../quickstart/readme.md#create-a-blueprint). + +The result should look like this: + +
+A rigid body on the cube + +
+ +The default value for `RigidBody` is `Dynamic`, which is what we want for all three objects. +It means that they will be affected by gravity and other forces. Repeat this step for the `Board` and `Cylinder` objects. + +## Add Primitive Colliders + +Colliders come in two flavors: primitive and dynamic. Primitives are made up of simple shapes like cubes, spheres, and cylinders. Dynamic colliders are created at runtime from the mesh of the object they are attached to. In general, it is *way* more efficient to use primitives and placing them manually. You may think that this is a lot of work, but usually you can get away with a very rough more or less boxy shape. We will show you how this approach first. + +There are three different ways to add primitive colliders to the objects, in order of increasing complexity. + +### Quick and Dirty + +Select the cube and search in the components for `colliderconstructor`. Select `avian3d::collision::collider::constructor::ColliderConstructor` and add it. +By default, the collider will be of the variant `Sphere`. Change it to `Cuboid`. +Since the standard cube in Blender is of size 2 m, set the `x_length`, `y_length`, and `z_length` all to `2.0`: +
+A collider on the cube + +
+ +That's already it. + +> [!CAUTION] +> This method brings a major footgun: Blender uses Z-up coordinates, while Bevy uses Y-up coordinates. +> The information you enter into the `ColliderConstructor` is in Bevy's coordinate system, so don't mix them up! + +### Using Empties + +You'll notice that the last variant does not actually show you a preview of the collider. Let's fix that. +Click on the `Board` and then select `Add` -> `Empty` -> `Cube`. +To make its properties a bit nice to work with, go to the `Data` tab of the `Properties` window in the lower right: + +
+Where to find the data tab + +
+ +You'll notice that it says "Size: 1m". This is a little bit misleading, as we've seen before, since the default cube is actually 2x2x2. The "Size" actually refers to the half-extent of the cube. Set it to `0.5` to make the cube a nice 1x1x1 cube. + +
+Where to find the data tab + +
+ +Add a collider to this empty like you did in the ["Quick and Dirty" section](#quick-and-dirty). +Set its lengths to `1` this time. + +If you have only the `Empty` set to visible and selected it, your viewport should now look as follows: + +
+The empty with the right size and collider + +
+ +The important bit here is that the empty's outlines perferctly match the attached collider's size. + +Now, drag and drop the empty into the `Board` collection. With the empty selected, hold `CTRL` and select the `Board` object. + +> [!IMPORTANT] +> It is essential that you *first* select the `Empty` and *then* select the `Board`. The order is key! + +With both objects selected, press `CTRL P` to bring up the parenting menu: + +
+The screen after creating a new empty + +
+ +> [!NOTE] +> Note how the color-coding in the screenshot above shows how `Board` has been selected last. +> Make sure this looks the same on your screen. + +In the popup, select the first option, namely `Object`. If everything went right, you should be able to "fold open" the `Board` to find your `Empty` as a child in there: + +
+The board is the parent of the empty + +
+ +This hierarchy will exported to Bevy as well! + +After this setup, we now have visible collider outlines that we can freely transform. Simply select the empty and transform it however you want. Whatever you do with this empty, the collider generated by Avian will look exactly like the outlines visible in Blender. + +While you could (and sometimes should) scale this manually, there is a nice way of finding the right scale. Click on the `Board` object. Then, in the side menu, head to the `Item` tab. Check out the `Dimensions` reported there: + +
+The dimensionality of the board + +
+ +> [!TIP] +> If you are not seeing this screen, you have probably clicked on the `Board` collection, and not on the item within it. + +As you can see, its dimensions are: + +- X: `5` +- Y: `1` +- Z: `3` + +You can just use these values as the scale for the `Empty`. After everything is done, your final object should look like this in the viewport, when only the `Board` and its children are visible: + +
+Finished board + +
+ +Note that the orange collider outlines should align nicely with the board's mesh. + +### Using Wireframes + +The last variant is a bit of a workaround for the fact that empties in Blender cannot have an arbitrary shape. +For example, a cylinder is not supported. So, we are going to create a new cylinder preview by hand. +Click on `Add` -> `Mesh` -> `Cylinder`. Don't click away yet! +Right after you create an object in Blender, you can modify how it should be generated. In the lower left, you should see the following popup: + +
+Post-creation popup + +
+ +> [!NOTE] +> If you cannot see this popup, you cae changed Blender's focus after creating the object. +> You have to remove the cylinder and recreate it again. + +Open up the popup to reveal a menu with some options for how to create a cylinder. +To again have a collider that nicely fits into a 1x1x1 space, set the `Radius` to `0.5` and the `Depth` to `1`. +To improve performance in Blender, you can also reduce the vertices, but this is not really important until you have hundreds of these colliders. + +
+Settings for the cylinder + +
+ +Hide everything except the newly created cylinder. Press `Tab` to enter the edit mode. Press `A` to select all vertices. +Press `X` to open the deletion menu. Select `Only Faces`. Press `Tab` again to go back into object mode. +You should now have the wireframe of a cylinder. + +
+Wireframe + +
+ +Now add a `ColliderConstructor` to it. This time, use the `Cylinder` variant. Set its `height` to `1` and `radius` to `0.5`, just as you did in the menu before. + +
+Cylinder collider + +
+ +The rest of the steps are identical to the empty: Drag-and-drop the cylinder collider into the `Cylinder` collection, make it a child of your `Cylinder` object and scale it accordingly. The result should look like this: + +
+Cylinder collider on mesh + +
+ +> [!TIP] +> Blender does not support creating all shapes that a collider would want. +> A notable omission is a capsule. +> You can use the builtin [Add Mesh Extra Objects](https://docs.blender.org/manual/en/latest/addons/add_mesh/mesh_extra_objects.html) +> extension to fill this gap. + +## Populate the world + +Go into your `World` scene. If you are coming from the [quickstart guide](../quickstart/readme.md), you can remove the `Player` empty that is left over. +If you have created this scene yourself in advance, make sure that it contains a camera, a light, and some kind of ground. +For reference, this is how our world setup looks: + +
+The world setup before adding any physics + +
+ +Before we add any objects, we'll make the ground a rigid body as well. Add a `RigidBody` component as described before to it, but this time set it to `Static`. Add a collider to it in any of the ways described above. We used the `Quick and Dirty` method for this: + +
+Ground collider + +
+ +> [!CAUTION] +> As mentioned before, when using this method you should be aware that the component +> is in Bevy's coordinate system, so set the `y_length` to the height of the ground. + +Now add instances of the `Cube`, `Board`, and `Cylinder` to the world by selecting `Add` -> `Collection Instance`. + +Since the objects are quite big, you may need to move the camera a bit further away to see them all. +We set its Y location to `-15` and the X rotation to `90` for this reason. +Pressing `0` on your numpad will show you a preview of what the camera sees. + +Save the scene to let Blenvy export everything. Run your game with `cargo run` and you should see some objects falling onto the ground! + +
+Objects falling onto the ground + +
+ +> [!TIP] +> If your scene is doing something weird, try adding Avian's +> [`PhysicsDebugPlugin`](https://docs.rs/avian3d/latest/avian3d/debug_render/struct.PhysicsDebugPlugin.html) +> to your Bevy app to see the colliders at runtime. + +## Add Dynamic Colliders + +Now let's go for some more complex shapes. +Remember, most of the time you'll want to approximate the shape with a primitive collider, but sometimes you need the exact shape +or just quickly want to test something. For this, we are going to use dynamic colliders. + +### Convex + +Go back to the `Library` scene, add a new collection, and name it `Torus`. Select `Add` -> `Mesh` -> `Torus`. Leave it at the default transform. Add a `RigidBody` to it. Your scene should now look like this: + +
+A simple torus + +
+ +We will now dynamically generate a convex hull around this torus. +You can imagine the result like how it would look like if you tightly wrapped the torus up as a christmas present. +This means that the hole in the middle will be treated as solid, which is okay for our case. +When using dynamic colliders, try to prefer convex shapes, as they are much faster to calculate than concave shapes. + +To use a dynamic collider, we must proceed a bit differently from before. +Instead of adding the component to the torus *object*, we add it to the *mesh*. +You can access it by expanding your object in the outliner. Its icon is a green triangle: + +
+The selected mesh + +
+ +With the *mesh* selected, add a `ColliderConstructor` to it. Set the variant to `ConvexHullFromMesh`. +If you did everything correctly, the component manager should say "Components for Torus (MESH)" at the top: + +
+The component manager for the torus mesh + +
+ +That's all for now + +### Concave + +Add a new collection and name it `Monkey`. Select `Add` -> `Mesh` -> `Monkey`. +Yes, Blender has a builtin method for creating Suzanne, its monkey mascot. Isn't it great? +Anyways, add a rigid body to it. Afterwards, just as before, select the *mesh* of the monkey. +Add a `ColliderConstructor` to it. This time, set the variant to `TrimeshFromMesh`. + +> [!CAUTION] +> While `TrimeshFromMesh` can deal with any kind of mesh, it is also the slowest collider to run. +> Additionally, the generated collider will always be treated as if it was hollow. +> That means that any objects that are completely inside the mesh will not collide with it. +> Only use a concave collider if you *really* need it. + +## Add the Dynamic Colliders to the World + +Save the scene to let Blenvy export everything. +Go back to the `World` scene. Add instances of the `Torus` and `Monkey` collections to the world and run the game with `cargo run`. +They should now fall onto the ground and interact with the other objects: + +
+The primitive and dynamic colliders falling down + +
+ +> [!TIP] +> Is your game crashing with `Tried to add a collider to entity Torus via that requires a mesh, but no mesh handle was found`? +> That means you added your `ColliderConstructor` to the object instead of the mesh. +> Go back to the screenshots above and make sure you have the mesh selected when adding the component. + +## Other useful components + +The object holding the `ColliderConstructor` can hold some additional components that are useful for tweaking the physics behavior. + +- `ColliderDensity` will set the density of the collider and indirectly change the rigid body's mass. +- `Sensor` allow other objects to pass through the collider. It will still report the collision to the physics system so you can react to it. +- `CollisionLayers` controls which other colliders this collider will interact with. Note that since this is a bitflag, manipulating it in Blender is a bit cumbersome. You probably want to set up some kind of `enum` that can be used in Blender and then add the proper `CollisionLayers` in Bevy. + +This is just a small selection. Refer to the [Avian documentation](https://docs.rs/avian3d/latest/avian3d/) for more information. diff --git a/docs/quickstart/img/art_file_structure.png b/docs/quickstart/img/art_file_structure.png new file mode 100644 index 0000000..7b4cb2a Binary files /dev/null and b/docs/quickstart/img/art_file_structure.png differ diff --git a/docs/quickstart/img/blenvy_after_setup.png b/docs/quickstart/img/blenvy_after_setup.png new file mode 100644 index 0000000..392645c Binary files /dev/null and b/docs/quickstart/img/blenvy_after_setup.png differ diff --git a/docs/quickstart/img/blenvy_menu.png b/docs/quickstart/img/blenvy_menu.png new file mode 100644 index 0000000..57e79ca Binary files /dev/null and b/docs/quickstart/img/blenvy_menu.png differ diff --git a/docs/quickstart/img/blueprint_file.png b/docs/quickstart/img/blueprint_file.png new file mode 100644 index 0000000..ac9be45 Binary files /dev/null and b/docs/quickstart/img/blueprint_file.png differ diff --git a/docs/quickstart/img/camera_transform.png b/docs/quickstart/img/camera_transform.png new file mode 100644 index 0000000..2bb358c Binary files /dev/null and b/docs/quickstart/img/camera_transform.png differ diff --git a/docs/quickstart/img/component_manager.png b/docs/quickstart/img/component_manager.png new file mode 100644 index 0000000..6fed8ac Binary files /dev/null and b/docs/quickstart/img/component_manager.png differ diff --git a/docs/quickstart/img/create_scene.png b/docs/quickstart/img/create_scene.png new file mode 100644 index 0000000..e84df1e Binary files /dev/null and b/docs/quickstart/img/create_scene.png differ diff --git a/docs/quickstart/img/default_collection.png b/docs/quickstart/img/default_collection.png new file mode 100644 index 0000000..3dba938 Binary files /dev/null and b/docs/quickstart/img/default_collection.png differ diff --git a/docs/quickstart/img/final_bevy.png b/docs/quickstart/img/final_bevy.png new file mode 100644 index 0000000..5cc05e3 Binary files /dev/null and b/docs/quickstart/img/final_bevy.png differ diff --git a/docs/quickstart/img/final_blender.png b/docs/quickstart/img/final_blender.png new file mode 100644 index 0000000..12d1d24 Binary files /dev/null and b/docs/quickstart/img/final_blender.png differ diff --git a/docs/quickstart/img/install.png b/docs/quickstart/img/install.png new file mode 100644 index 0000000..6fd6117 Binary files /dev/null and b/docs/quickstart/img/install.png differ diff --git a/docs/quickstart/img/level_asset.png b/docs/quickstart/img/level_asset.png new file mode 100644 index 0000000..054e1d8 Binary files /dev/null and b/docs/quickstart/img/level_asset.png differ diff --git a/docs/quickstart/img/lots_of_components.png b/docs/quickstart/img/lots_of_components.png new file mode 100644 index 0000000..6e6f338 Binary files /dev/null and b/docs/quickstart/img/lots_of_components.png differ diff --git a/docs/quickstart/img/player_collection.png b/docs/quickstart/img/player_collection.png new file mode 100644 index 0000000..b08868f Binary files /dev/null and b/docs/quickstart/img/player_collection.png differ diff --git a/docs/quickstart/img/player_component.png b/docs/quickstart/img/player_component.png new file mode 100644 index 0000000..5a70d2f Binary files /dev/null and b/docs/quickstart/img/player_component.png differ diff --git a/docs/quickstart/img/registry.png b/docs/quickstart/img/registry.png new file mode 100644 index 0000000..cc14cfa Binary files /dev/null and b/docs/quickstart/img/registry.png differ diff --git a/docs/quickstart/img/registry_settings.png b/docs/quickstart/img/registry_settings.png new file mode 100644 index 0000000..509ecba Binary files /dev/null and b/docs/quickstart/img/registry_settings.png differ diff --git a/docs/quickstart/img/scene_name.png b/docs/quickstart/img/scene_name.png new file mode 100644 index 0000000..a245390 Binary files /dev/null and b/docs/quickstart/img/scene_name.png differ diff --git a/docs/quickstart/img/scenes.png b/docs/quickstart/img/scenes.png new file mode 100644 index 0000000..3b03aa3 Binary files /dev/null and b/docs/quickstart/img/scenes.png differ diff --git a/docs/quickstart/readme.md b/docs/quickstart/readme.md new file mode 100644 index 0000000..266a1a6 --- /dev/null +++ b/docs/quickstart/readme.md @@ -0,0 +1,310 @@ +# Quickstart + +This guide assumes you use Blender 4.2 or newer and have set it to English. + +> [!NOTE] +> This is not a Blender tutorial. No worries, we will make sure you find all necessary buttons to click, but we will not explain in detail what Blender concepts like "collections" or "scenes" are. If you are not familiar with Blender, you might want to look up some basic tutorials first. + +## Table of Contents + +- [Install the Blender Addon](#install-the-blender-addon) +- [Setup the Bevy side](#setup-the-bevy-side) +- [Setup the Blender addon for your project](#setup-the-blender-addon-for-your-project) +- [Create a blueprint](#create-a-blueprint) +- [Compose the world](#compose-the-world) +- [Run your game](#run-your-game) +- [Next Steps](#next-steps) + +## Install the Blender Addon + +- Download `blenvy.zip` from the [release page](https://github.com/kaosat-dev/Blenvy/releases/tag/blenvy_v0.1.0_pre_alpha) +- Open Blender +- Drag and drop `blenvy.zip` into Blender + -
+ This window should show up + + + + +
+- Leave all settings as is and click on `OK` + +Leave the scene open, we will get back to it later. + +## Setup the Bevy side + +Run the following commands: + +```sh +cargo new my_game +cd my_game +cargo add bevy +cargo add blenvy --git https://github.com/kaosat-dev/Blenvy/ --branch blenvy +``` + +This guide will tell you to `cargo run` at multiple points. We expect that you are still in the `my_game` directory when you do so. + +Now, replace the contents of `src/main.rs` with the following: + +
+src/main.rs + +```rust +use bevy::prelude::*; +use blenvy::*; + +fn main() -> AppExit { + App::new() + .add_plugins((DefaultPlugins, BlenvyPlugin::default())) + // We need to register components to make them visible to Blenvy + .register_type::() + .add_systems(Startup, setup) + .run() +} + +#[derive(Component, Reflect)] +#[reflect(Component)] +struct Player { + strength: f32, + perception: f32, + endurance: f32, + charisma: f32, + intelligence: f32, + agility: f32, + luck: f32, +} + +fn setup(mut commands: Commands) { + commands.spawn(( + BlueprintInfo::from_path("levels/World.glb"), + SpawnBlueprint, + HideUntilReady, + GameWorldTag, + )); +} +``` + +
+ +`Player` is the component that we will add to our entities in Blender. It can contain any fields you want, but you need to remember to call `register_type` with it. + +Running this won't work yet because the `levels/World.glb` we reference doesn't exist yet. We will create it in the next step. + +## Setup the Blender addon for your project + +Create a directory under `my_game` called `art`. Hop back into Blender and save the default scene as `my_game.blend` in the `art` directory we just created. Your file structure should now look like this: + +
+File structure + + +
+ +Now, clear the default scene of any objects or collections. +The fastest way to do this is to look for the collection named simply `Collection` all the way on the right of Blender. Right-click on it and select `Delete Hierachy`. For future reference, the place where you just did this is called the *outliner*. + +
+The default collection to delete + + +
+ +Rename the default scene from `Scene` to `World` by clicking on "Scene" in the upper right corner of Blender and typing a new name. + +
+Where to rename the scene + + +
+ +Next, create a new scene by clicking on the icon that looks a bit like two pages, next to where you just renamed the scene, and then selecting `New`. Rename this scene to `Library`. + +
+Where to create a new scene + + +
+ +If everything went alright, entering the scene selector by pressing the icon next to the scene name should look like this: + +
+Scene selector + + +
+ +If you switched around scenes to see if everything worked, make sure to switch back to the `Library` scene now. + +Press `N`. This will open a side window. In it, click on the tab `Blenvy`. This is the main window through which you will interact with Blenvy. + +
+Blenvy menu + +
+ +You will see that two options are highlighted red because they are not yet configured. + +For `level scenes`, click on the selector, select `World` and hit `+`. For `library scenes`, select `Library` and again hit `+`. + +Your menu should now look like this: + +
+Correctly setup Blenvy + +
+ +Save your Blender file by pressing `Ctrl + S` on Windows or Linux and `Cmd + S` on macOS. You should now have a new asset under `my_game/assets/levels/World.glb` and a companion file containing metadata. + +
+The newly created level assets + +
+Run, run your game with `cargo run`. It may crash because the scene is empty, but don't worry. The point is that now will have generated another new file, namely `assets/registry.json`. This file contains the information about all components exported by your game so that Blenvy can pso them in Blender. + +
+The final file structure for an empty world + +
+ +This concludes the setup portion of the guide. + +## Create a blueprint + +Alright, let's jump into the actual workflow you'll be using to create your game. +We will first create an object for the `Player` component we defined earlier. +While still in the `Library` scene, right-click on the `Scene Collection` in the outliner to the right. Select `New Collection`. Double-click on the new collection that appeared and rename it to `Player`. Click on it to have it selected. + +
+The player collection + +
+ +Now, on the upper left of Blender, click on `Add` -> `Mesh` -> `Cube`. This will be our player. We will now add the `Player` component to it. Go to the Blenvy menu we opened earlier. Select the icon in its upper left corner. This is where components are managed. + +
+The component manager + +
+ +If your component manager says "Select an object to edit its components", make sure you have the *Cube* we just created selected by clicking on it in the 3D viewport. + +Click on `Components`. If it says "No results found", we need to explicitly reload the registry. In the Blenvy menu, click on the gear icon (the fourth one from the left). In the submenu, click on the second icon. Your menu should now look like this: + +
+The registry settings + +
+ +Click on `reload registry` to reload it manually. +Go back to the component manager and you should now be greeted by a whole bunch of components: + +
+The registry settings + +
+ +Type in `Player` and click on `my_game::Player`. + +> [!TIP] +> If this does not show up, you have forgotten to call `register_type` with it. + +Now click `Add`. You can now set all the player's fields in a neat Blender UI. Note that while we are only using `f32` here, you can use any kind of field, even complicated nested enums or structs. + +
+The player component in Blender + +
+ +Congratulations, you have just created your first blueprint. + +## Compose the world + +Let's populate our world now. Switch back to the `World` scene in Blender as described before. + +We will add the following kinds of objects to our world: + +- Some basic ground, which we will create directly in the world +- A camera +- A light +- The player character, which will be a blueprint instance + +First, let's add a camera through `Add` -> `Camera`. Blender will make the camera face whatever direction the viewport is aimed at, which will probably be completely arbitrary. +Move the camera a bit so that it faces the world origin and is rotated the right way. +Pressing `0` on your numpad will show you a preview of what the camera sees. +You can just copy our transform if you like: + +- Location: + - X: `0` + - Y: `-10` + - Z: `2` +- Rotation + - X: `80` + - Y: `0` + - Z: `0` +- Scale + - X: `1` + - Y: `1` + - Z: `1` + + All transforms we are using assume that you are using the `XYZ Euler` mode for rotation. + +
+The camera transform in Blender + +
+ +Now, let's add a ground with `Add` -> `Mesh` -> `Cube`. Again scale and move it so that it lies neatly under the camera. Our transform looks like this: + +- Location: + - X: `0` + - Y: `0` + - Z: `0` +- Rotation + - X: `0` + - Y: `0` + - Z: `0` +- Scale + - X: `5` + - Y: `5` + - Z: `0.1` + +Add a directional light with `Add` -> `Light` -> `Sun`. +This light's position and scale do not matter, only its rotation is actually used. We are using the following: + +- Rotation + - X: `20` + - Y: `20` + - Z: `0` + +Finally, it is time to create an instance of our player blueprint! For this, go to `Add` -> `Collection Instance` and select `Player`. It will spawn at origin, which means directly inside the ground for us. Move it a bit up. A location of `Z: 1.1` should placed it just a tiny bit above the ground. + +And with that, our Blender scene is finished! The final result should look like this: + +
+The final Blender world scene + +
+ +Save your Blender file again. This will update the `World.glb` file in the `assets` directory and create a new file for our player blueprint under `assets/blueprints/Player.glb`, including again a metadata file. + +
+The new blueprint files + +
+ +## Run your game + +Congrats, you're done! Just run your game with `cargo run` and you should see your world in all its glory: + +
+The final Bevy render + +
+ +Okay, maybe not that much glory. But the important part is that the player is visible in the scene and has a `Player` component on it. You can now add more components to the player, create more blueprints, and populate your world with them. They can have animations, materials, etc. Have fun! + +## Next Steps + +- Read the [Blevy for Bevy](../../crates/blenvy/README.md) documentation for more features on the Bevy side. +- Read the [Blevy for Blender](../../tools/blenvy/README.md) documentation for more features on the Blender side. +- Read about the [Avian Physics Integration](../avian/readme.md) to learn how to setup colliders in Blender that will be used by the Avian physics engine in Bevy.