2023-05-31 16:19:06 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Foundation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
Geometry Nodes: add simulation support
This adds support for building simulations with geometry nodes. A new
`Simulation Input` and `Simulation Output` node allow maintaining a
simulation state across multiple frames. Together these two nodes form
a `simulation zone` which contains all the nodes that update the simulation
state from one frame to the next.
A new simulation zone can be added via the menu
(`Simulation > Simulation Zone`) or with the node add search.
The simulation state contains a geometry by default. However, it is possible
to add multiple geometry sockets as well as other socket types. Currently,
field inputs are evaluated and stored for the preceding geometry socket in
the order that the sockets are shown. Simulation state items can be added
by linking one of the empty sockets to something else. In the sidebar, there
is a new panel that allows adding, removing and reordering these sockets.
The simulation nodes behave as follows:
* On the first frame, the inputs of the `Simulation Input` node are evaluated
to initialize the simulation state. In later frames these sockets are not
evaluated anymore. The `Delta Time` at the first frame is zero, but the
simulation zone is still evaluated.
* On every next frame, the `Simulation Input` node outputs the simulation
state of the previous frame. Nodes in the simulation zone can edit that
data in arbitrary ways, also taking into account the `Delta Time`. The new
simulation state has to be passed to the `Simulation Output` node where it
is cached and forwarded.
* On a frame that is already cached or baked, the nodes in the simulation
zone are not evaluated, because the `Simulation Output` node can return
the previously cached data directly.
It is not allowed to connect sockets from inside the simulation zone to the
outside without going through the `Simulation Output` node. This is a necessary
restriction to make caching and sub-frame interpolation work. Links can go into
the simulation zone without problems though.
Anonymous attributes are not propagated by the simulation nodes unless they
are explicitly stored in the simulation state. This is unfortunate, but
currently there is no practical and reliable alternative. The core problem
is detecting which anonymous attributes will be required for the simulation
and afterwards. While we can detect this for the current evaluation, we can't
look into the future in time to see what data will be necessary. We intend to
make it easier to explicitly pass data through a simulation in the future,
even if the simulation is in a nested node group.
There is a new `Simulation Nodes` panel in the physics tab in the properties
editor. It allows baking all simulation zones on the selected objects. The
baking options are intentially kept at a minimum for this MVP. More features
for simulation baking as well as baking in general can be expected to be added
separately.
All baked data is stored on disk in a folder next to the .blend file. #106937
describes how baking is implemented in more detail. Volumes can not be baked
yet and materials are lost during baking for now. Packing the baked data into
the .blend file is not yet supported.
The timeline indicates which frames are currently cached, baked or cached but
invalidated by user-changes.
Simulation input and output nodes are internally linked together by their
`bNode.identifier` which stays the same even if the node name changes. They
are generally added and removed together. However, there are still cases where
"dangling" simulation nodes can be created currently. Those generally don't
cause harm, but would be nice to avoid this in more cases in the future.
Co-authored-by: Hans Goudey <h.goudey@me.com>
Co-authored-by: Lukas Tönne <lukas@blender.org>
Pull Request: https://projects.blender.org/blender/blender/pulls/104924
2023-05-03 13:18:51 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* \ingroup bke
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "DNA_node_types.h"
|
|
|
|
|
|
|
|
#include "BLI_vector.hh"
|
|
|
|
|
|
|
|
namespace blender::bke::node_tree_zones {
|
|
|
|
|
|
|
|
struct TreeZone {
|
|
|
|
TreeZones *owner = nullptr;
|
|
|
|
/** Index of the zone in the array of all zones in a node tree. */
|
|
|
|
int index = -1;
|
|
|
|
/** Zero for top level zones, one for a nested zone, and so on. */
|
|
|
|
int depth = -1;
|
|
|
|
/** Input node of the zone. */
|
|
|
|
const bNode *input_node = nullptr;
|
|
|
|
/** Output node of the zone. */
|
|
|
|
const bNode *output_node = nullptr;
|
|
|
|
/** Direct parent of the zone. If this is null, this is a top level zone. */
|
|
|
|
TreeZone *parent_zone = nullptr;
|
|
|
|
/** Direct children zones. Does not contain recursively nested zones. */
|
|
|
|
Vector<TreeZone *> child_zones;
|
|
|
|
/** Direct children nodes. Does not contain recursively nested nodes. */
|
|
|
|
Vector<const bNode *> child_nodes;
|
|
|
|
|
|
|
|
bool contains_node_recursively(const bNode &node) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TreeZones {
|
|
|
|
public:
|
|
|
|
Vector<std::unique_ptr<TreeZone>> zones;
|
|
|
|
Map<int, int> parent_zone_by_node_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
const TreeZones *get_tree_zones(const bNodeTree &tree);
|
|
|
|
|
|
|
|
} // namespace blender::bke::node_tree_zones
|