This adds support for showing geometry passed to the Viewer in the 3d viewport (instead of just in the spreadsheet). The "viewer geometry" bypasses the group output. So it is not necessary to change the final output of the node group to be able to see the intermediate geometry. **Activation and deactivation of a viewer node** * A viewer node is activated by clicking on it. * Ctrl+shift+click on any node/socket connects it to the viewer and makes it active. * Ctrl+shift+click in empty space deactivates the active viewer. * When the active viewer is not visible anymore (e.g. another object is selected, or the current node group is exit), it is deactivated. * Clicking on the icon in the header of the Viewer node toggles whether its active or not. **Pinning** * The spreadsheet still allows pinning the active viewer as before. When pinned, the spreadsheet still references the viewer node even when it becomes inactive. * The viewport does not support pinning at the moment. It always shows the active viewer. **Attribute** * When a field is linked to the second input of the viewer node it is displayed as an overlay in the viewport. * When possible the correct domain for the attribute is determined automatically. This does not work in all cases. It falls back to the face corner domain on meshes and the point domain on curves. When necessary, the domain can be picked manually. * The spreadsheet now only shows the "Viewer" column for the domain that is selected in the Viewer node. * Instance attributes are visualized as a constant color per instance. **Viewport Options** * The attribute overlay opacity can be controlled with the "Viewer Node" setting in the overlays popover. * A viewport can be configured not to show intermediate viewer-geometry by disabling the "Viewer Node" option in the "View" menu. **Implementation Details** * The "spreadsheet context path" was generalized to a "viewer path" that is used in more places now. * The viewer node itself determines the attribute domain, evaluates the field and stores the result in a `.viewer` attribute. * A new "viewer attribute' overlay displays the data from the `.viewer` attribute. * The ground truth for the active viewer node is stored in the workspace now. Node editors, spreadsheets and viewports retrieve the active viewer from there unless they are pinned. * The depsgraph object iterator has a new "viewer path" setting. When set, the viewed geometry of the corresponding object is part of the iterator instead of the final evaluated geometry. * To support the instance attribute overlay `DupliObject` was extended to contain the information necessary for drawing the overlay. * The ctrl+shift+click operator has been refactored so that it can make existing links to viewers active again. * The auto-domain-detection in the Viewer node works by checking the "preferred domain" for every field input. If there is not exactly one preferred domain, the fallback is used. Known limitations: * Loose edges of meshes don't have the attribute overlay. This could be added separately if necessary. * Some attributes are hard to visualize as a color directly. For example, the values might have to be normalized or some should be drawn as arrays. For now, we encourage users to build node groups that generate appropriate viewer-geometry. We might include some of that functionality in future versions. Support for displaying attribute values as text in the viewport is planned as well. * There seems to be an issue with the attribute overlay for pointclouds on nvidia gpus, to be investigated. Differential Revision: https://developer.blender.org/D15954
325 lines
9.7 KiB
C++
325 lines
9.7 KiB
C++
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#pragma once
|
|
|
|
/** \file
|
|
* \ingroup bke
|
|
*
|
|
* Common field utilities and field definitions for geometry components.
|
|
*/
|
|
|
|
#include "BKE_attribute.h"
|
|
#include "BKE_geometry_set.hh"
|
|
|
|
#include "FN_field.hh"
|
|
|
|
struct Mesh;
|
|
struct PointCloud;
|
|
|
|
namespace blender::bke {
|
|
|
|
class CurvesGeometry;
|
|
class GeometryFieldInput;
|
|
|
|
class MeshFieldContext : public fn::FieldContext {
|
|
private:
|
|
const Mesh &mesh_;
|
|
const eAttrDomain domain_;
|
|
|
|
public:
|
|
MeshFieldContext(const Mesh &mesh, const eAttrDomain domain);
|
|
const Mesh &mesh() const
|
|
{
|
|
return mesh_;
|
|
}
|
|
|
|
eAttrDomain domain() const
|
|
{
|
|
return domain_;
|
|
}
|
|
};
|
|
|
|
class CurvesFieldContext : public fn::FieldContext {
|
|
private:
|
|
const CurvesGeometry &curves_;
|
|
const eAttrDomain domain_;
|
|
|
|
public:
|
|
CurvesFieldContext(const CurvesGeometry &curves, const eAttrDomain domain);
|
|
|
|
const CurvesGeometry &curves() const
|
|
{
|
|
return curves_;
|
|
}
|
|
|
|
eAttrDomain domain() const
|
|
{
|
|
return domain_;
|
|
}
|
|
};
|
|
|
|
class PointCloudFieldContext : public fn::FieldContext {
|
|
private:
|
|
const PointCloud &pointcloud_;
|
|
|
|
public:
|
|
PointCloudFieldContext(const PointCloud &pointcloud) : pointcloud_(pointcloud)
|
|
{
|
|
}
|
|
|
|
const PointCloud &pointcloud() const
|
|
{
|
|
return pointcloud_;
|
|
}
|
|
};
|
|
|
|
class InstancesFieldContext : public fn::FieldContext {
|
|
private:
|
|
const InstancesComponent &instances_;
|
|
|
|
public:
|
|
InstancesFieldContext(const InstancesComponent &instances) : instances_(instances)
|
|
{
|
|
}
|
|
|
|
const InstancesComponent &instances() const
|
|
{
|
|
return instances_;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* A field context that can represent meshes, curves, point clouds, or instances,
|
|
* used for field inputs that can work for multiple geometry types.
|
|
*/
|
|
class GeometryFieldContext : public fn::FieldContext {
|
|
private:
|
|
/**
|
|
* Store the geometry as a void pointer instead of a #GeometryComponent to allow referencing data
|
|
* that doesn't correspond directly to a geometry component type, in this case #CurvesGeometry
|
|
* instead of #Curves.
|
|
*/
|
|
const void *geometry_;
|
|
const GeometryComponentType type_;
|
|
const eAttrDomain domain_;
|
|
|
|
friend GeometryFieldInput;
|
|
|
|
public:
|
|
GeometryFieldContext(const GeometryComponent &component, eAttrDomain domain);
|
|
GeometryFieldContext(const void *geometry, GeometryComponentType type, eAttrDomain domain);
|
|
|
|
const void *geometry() const
|
|
{
|
|
return geometry_;
|
|
}
|
|
|
|
GeometryComponentType type() const
|
|
{
|
|
return type_;
|
|
}
|
|
|
|
eAttrDomain domain() const
|
|
{
|
|
return domain_;
|
|
}
|
|
|
|
std::optional<AttributeAccessor> attributes() const;
|
|
const Mesh *mesh() const;
|
|
const CurvesGeometry *curves() const;
|
|
const PointCloud *pointcloud() const;
|
|
const InstancesComponent *instances() const;
|
|
|
|
private:
|
|
GeometryFieldContext(const Mesh &mesh, eAttrDomain domain);
|
|
GeometryFieldContext(const CurvesGeometry &curves, eAttrDomain domain);
|
|
GeometryFieldContext(const PointCloud &points);
|
|
GeometryFieldContext(const InstancesComponent &instances);
|
|
};
|
|
|
|
class GeometryFieldInput : public fn::FieldInput {
|
|
public:
|
|
using fn::FieldInput::FieldInput;
|
|
GVArray get_varray_for_context(const fn::FieldContext &context,
|
|
IndexMask mask,
|
|
ResourceScope &scope) const override;
|
|
virtual GVArray get_varray_for_context(const GeometryFieldContext &context,
|
|
IndexMask mask) const = 0;
|
|
virtual std::optional<eAttrDomain> preferred_domain(const GeometryComponent &component) const;
|
|
};
|
|
|
|
class MeshFieldInput : public fn::FieldInput {
|
|
public:
|
|
using fn::FieldInput::FieldInput;
|
|
GVArray get_varray_for_context(const fn::FieldContext &context,
|
|
IndexMask mask,
|
|
ResourceScope &scope) const override;
|
|
virtual GVArray get_varray_for_context(const Mesh &mesh,
|
|
eAttrDomain domain,
|
|
IndexMask mask) const = 0;
|
|
virtual std::optional<eAttrDomain> preferred_domain(const Mesh &mesh) const;
|
|
};
|
|
|
|
class CurvesFieldInput : public fn::FieldInput {
|
|
public:
|
|
using fn::FieldInput::FieldInput;
|
|
GVArray get_varray_for_context(const fn::FieldContext &context,
|
|
IndexMask mask,
|
|
ResourceScope &scope) const override;
|
|
virtual GVArray get_varray_for_context(const CurvesGeometry &curves,
|
|
eAttrDomain domain,
|
|
IndexMask mask) const = 0;
|
|
virtual std::optional<eAttrDomain> preferred_domain(const CurvesGeometry &curves) const;
|
|
};
|
|
|
|
class PointCloudFieldInput : public fn::FieldInput {
|
|
public:
|
|
using fn::FieldInput::FieldInput;
|
|
GVArray get_varray_for_context(const fn::FieldContext &context,
|
|
IndexMask mask,
|
|
ResourceScope &scope) const override;
|
|
virtual GVArray get_varray_for_context(const PointCloud &pointcloud, IndexMask mask) const = 0;
|
|
};
|
|
|
|
class InstancesFieldInput : public fn::FieldInput {
|
|
public:
|
|
using fn::FieldInput::FieldInput;
|
|
GVArray get_varray_for_context(const fn::FieldContext &context,
|
|
IndexMask mask,
|
|
ResourceScope &scope) const override;
|
|
virtual GVArray get_varray_for_context(const InstancesComponent &instances,
|
|
IndexMask mask) const = 0;
|
|
};
|
|
|
|
class AttributeFieldInput : public GeometryFieldInput {
|
|
private:
|
|
std::string name_;
|
|
|
|
public:
|
|
AttributeFieldInput(std::string name, const CPPType &type)
|
|
: GeometryFieldInput(type, name), name_(std::move(name))
|
|
{
|
|
category_ = Category::NamedAttribute;
|
|
}
|
|
|
|
template<typename T> static fn::Field<T> Create(std::string name)
|
|
{
|
|
const CPPType &type = CPPType::get<T>();
|
|
auto field_input = std::make_shared<AttributeFieldInput>(std::move(name), type);
|
|
return fn::Field<T>{field_input};
|
|
}
|
|
|
|
StringRefNull attribute_name() const
|
|
{
|
|
return name_;
|
|
}
|
|
|
|
GVArray get_varray_for_context(const GeometryFieldContext &context,
|
|
IndexMask mask) const override;
|
|
|
|
std::string socket_inspection_name() const override;
|
|
|
|
uint64_t hash() const override;
|
|
bool is_equal_to(const fn::FieldNode &other) const override;
|
|
std::optional<eAttrDomain> preferred_domain(const GeometryComponent &component) const override;
|
|
};
|
|
|
|
class IDAttributeFieldInput : public GeometryFieldInput {
|
|
public:
|
|
IDAttributeFieldInput() : GeometryFieldInput(CPPType::get<int>())
|
|
{
|
|
category_ = Category::Generated;
|
|
}
|
|
|
|
GVArray get_varray_for_context(const GeometryFieldContext &context,
|
|
IndexMask mask) const override;
|
|
|
|
std::string socket_inspection_name() const override;
|
|
|
|
uint64_t hash() const override;
|
|
bool is_equal_to(const fn::FieldNode &other) const override;
|
|
};
|
|
|
|
VArray<float3> curve_normals_varray(const CurvesGeometry &curves, const eAttrDomain domain);
|
|
|
|
VArray<float3> mesh_normals_varray(const Mesh &mesh, const IndexMask mask, eAttrDomain domain);
|
|
|
|
class NormalFieldInput : public GeometryFieldInput {
|
|
public:
|
|
NormalFieldInput() : GeometryFieldInput(CPPType::get<float3>())
|
|
{
|
|
category_ = Category::Generated;
|
|
}
|
|
|
|
GVArray get_varray_for_context(const GeometryFieldContext &context,
|
|
IndexMask mask) const override;
|
|
|
|
std::string socket_inspection_name() const override;
|
|
|
|
uint64_t hash() const override;
|
|
bool is_equal_to(const fn::FieldNode &other) const override;
|
|
};
|
|
|
|
class AnonymousAttributeFieldInput : public GeometryFieldInput {
|
|
private:
|
|
/**
|
|
* A strong reference is required to make sure that the referenced attribute is not removed
|
|
* automatically.
|
|
*/
|
|
StrongAnonymousAttributeID anonymous_id_;
|
|
std::string producer_name_;
|
|
|
|
public:
|
|
AnonymousAttributeFieldInput(StrongAnonymousAttributeID anonymous_id,
|
|
const CPPType &type,
|
|
std::string producer_name)
|
|
: GeometryFieldInput(type, anonymous_id.debug_name()),
|
|
anonymous_id_(std::move(anonymous_id)),
|
|
producer_name_(producer_name)
|
|
{
|
|
category_ = Category::AnonymousAttribute;
|
|
}
|
|
|
|
template<typename T>
|
|
static fn::Field<T> Create(StrongAnonymousAttributeID anonymous_id, std::string producer_name)
|
|
{
|
|
const CPPType &type = CPPType::get<T>();
|
|
auto field_input = std::make_shared<AnonymousAttributeFieldInput>(
|
|
std::move(anonymous_id), type, std::move(producer_name));
|
|
return fn::Field<T>{field_input};
|
|
}
|
|
|
|
GVArray get_varray_for_context(const GeometryFieldContext &context,
|
|
IndexMask mask) const override;
|
|
|
|
std::string socket_inspection_name() const override;
|
|
|
|
uint64_t hash() const override;
|
|
bool is_equal_to(const fn::FieldNode &other) const override;
|
|
std::optional<eAttrDomain> preferred_domain(const GeometryComponent &component) const override;
|
|
};
|
|
|
|
class CurveLengthFieldInput final : public CurvesFieldInput {
|
|
public:
|
|
CurveLengthFieldInput();
|
|
GVArray get_varray_for_context(const CurvesGeometry &curves,
|
|
eAttrDomain domain,
|
|
IndexMask mask) const final;
|
|
uint64_t hash() const override;
|
|
bool is_equal_to(const fn::FieldNode &other) const override;
|
|
};
|
|
|
|
bool try_capture_field_on_geometry(GeometryComponent &component,
|
|
const AttributeIDRef &attribute_id,
|
|
const eAttrDomain domain,
|
|
const fn::GField &field);
|
|
|
|
/**
|
|
* Try to find the geometry domain that the field should be evaluated on. If it is not obvious
|
|
* which domain is correct, none is returned.
|
|
*/
|
|
std::optional<eAttrDomain> try_detect_field_domain(const GeometryComponent &component,
|
|
const fn::GField &field);
|
|
|
|
} // namespace blender::bke
|