Blender  V3.3
hydra/pointcloud.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2022 NVIDIA Corporation
3  * Copyright 2022 Blender Foundation */
4 
5 #include "hydra/pointcloud.h"
6 #include "hydra/geometry.inl"
7 #include "scene/pointcloud.h"
8 
9 #include <pxr/imaging/hd/extComputationUtils.h>
10 
12 
13 HdCyclesPoints::HdCyclesPoints(const SdfPath &rprimId
14 #if PXR_VERSION < 2102
15  ,
16  const SdfPath &instancerId
17 #endif
18  )
19  : HdCyclesGeometry(rprimId
20 #if PXR_VERSION < 2102
21  ,
22  instancerId
23 #endif
24  )
25 {
26 }
27 
29 {
30 }
31 
33 {
34  HdDirtyBits bits = HdCyclesGeometry::GetInitialDirtyBitsMask();
35  bits |= HdChangeTracker::DirtyPoints | HdChangeTracker::DirtyWidths |
36  HdChangeTracker::DirtyPrimvar;
37  return bits;
38 }
39 
40 HdDirtyBits HdCyclesPoints::_PropagateDirtyBits(HdDirtyBits bits) const
41 {
42  // Points and widths always have to be updated together
43  if (bits & (HdChangeTracker::DirtyPoints | HdChangeTracker::DirtyWidths)) {
44  bits |= HdChangeTracker::DirtyPoints | HdChangeTracker::DirtyWidths;
45  }
46 
47  return bits;
48 }
49 
50 void HdCyclesPoints::Populate(HdSceneDelegate *sceneDelegate, HdDirtyBits dirtyBits, bool &rebuild)
51 {
52  if (dirtyBits & (HdChangeTracker::DirtyPoints | HdChangeTracker::DirtyWidths)) {
53  const size_t numPoints = _geom->num_points();
54 
55  PopulatePoints(sceneDelegate);
56  PopulateWidths(sceneDelegate);
57 
58  rebuild = _geom->num_points() != numPoints;
59 
61  shaders.reserve(_geom->num_points());
62  for (size_t i = 0; i < _geom->num_points(); ++i) {
63  shaders.push_back_reserved(0);
64  }
65 
66  _geom->set_shader(shaders);
67  }
68 
69  if (dirtyBits & HdChangeTracker::DirtyPrimvar) {
70  PopulatePrimvars(sceneDelegate);
71  }
72 }
73 
74 void HdCyclesPoints::PopulatePoints(HdSceneDelegate *sceneDelegate)
75 {
76  VtValue value;
77 
78  for (const HdExtComputationPrimvarDescriptor &desc :
79  sceneDelegate->GetExtComputationPrimvarDescriptors(GetId(), HdInterpolationVertex)) {
80  if (desc.name == HdTokens->points) {
81  auto valueStore = HdExtComputationUtils::GetComputedPrimvarValues({desc}, sceneDelegate);
82  const auto valueStoreIt = valueStore.find(desc.name);
83  if (valueStoreIt != valueStore.end()) {
84  value = std::move(valueStoreIt->second);
85  }
86  break;
87  }
88  }
89 
90  if (value.IsEmpty()) {
91  value = GetPrimvar(sceneDelegate, HdTokens->points);
92  }
93 
94  if (!value.IsHolding<VtVec3fArray>()) {
95  TF_WARN("Invalid points data for %s", GetId().GetText());
96  return;
97  }
98 
99  const auto &points = value.UncheckedGet<VtVec3fArray>();
100 
101  array<float3> pointsDataCycles;
102  pointsDataCycles.reserve(points.size());
103 
104  for (const GfVec3f &point : points) {
105  pointsDataCycles.push_back_reserved(make_float3(point[0], point[1], point[2]));
106  }
107 
108  _geom->set_points(pointsDataCycles);
109 }
110 
111 void HdCyclesPoints::PopulateWidths(HdSceneDelegate *sceneDelegate)
112 {
113  VtValue value = GetPrimvar(sceneDelegate, HdTokens->widths);
114  const HdInterpolation interpolation = GetPrimvarInterpolation(sceneDelegate, HdTokens->widths);
115 
116  if (!value.IsHolding<VtFloatArray>()) {
117  TF_WARN("Invalid widths data for %s", GetId().GetText());
118  return;
119  }
120 
121  const auto &widths = value.UncheckedGet<VtFloatArray>();
122 
123  array<float> radiusDataCycles;
124  radiusDataCycles.reserve(_geom->num_points());
125 
126  if (interpolation == HdInterpolationConstant) {
127  TF_VERIFY(widths.size() == 1);
128 
129  const float constantRadius = widths[0] * 0.5f;
130 
131  for (size_t i = 0; i < _geom->num_points(); ++i) {
132  radiusDataCycles.push_back_reserved(constantRadius);
133  }
134  }
135  else if (interpolation == HdInterpolationVertex) {
136  TF_VERIFY(widths.size() == _geom->num_points());
137 
138  for (size_t i = 0; i < _geom->num_points(); ++i) {
139  radiusDataCycles.push_back_reserved(widths[i] * 0.5f);
140  }
141  }
142 
143  _geom->set_radius(radiusDataCycles);
144 }
145 
146 void HdCyclesPoints::PopulatePrimvars(HdSceneDelegate *sceneDelegate)
147 {
148  Scene *const scene = (Scene *)_geom->get_owner();
149 
150  const std::pair<HdInterpolation, AttributeElement> interpolations[] = {
151  std::make_pair(HdInterpolationVertex, ATTR_ELEMENT_VERTEX),
152  std::make_pair(HdInterpolationConstant, ATTR_ELEMENT_OBJECT),
153  };
154 
155  for (const auto &interpolation : interpolations) {
156  for (const HdPrimvarDescriptor &desc :
157  GetPrimvarDescriptors(sceneDelegate, interpolation.first)) {
158  // Skip special primvars that are handled separately
159  if (desc.name == HdTokens->points || desc.name == HdTokens->widths) {
160  continue;
161  }
162 
163  VtValue value = GetPrimvar(sceneDelegate, desc.name);
164  if (value.IsEmpty()) {
165  continue;
166  }
167 
168  const ustring name(desc.name.GetString());
169 
171  if (desc.role == HdPrimvarRoleTokens->textureCoordinate) {
172  std = ATTR_STD_UV;
173  }
174  else if (interpolation.first == HdInterpolationVertex) {
175  if (desc.name == HdTokens->displayColor || desc.role == HdPrimvarRoleTokens->color) {
177  }
178  else if (desc.name == HdTokens->normals) {
180  }
181  }
182  else if (desc.name == HdTokens->displayColor &&
183  interpolation.first == HdInterpolationConstant) {
184  if (value.IsHolding<VtVec3fArray>() && value.GetArraySize() == 1) {
185  const GfVec3f color = value.UncheckedGet<VtVec3fArray>()[0];
186  _instances[0]->set_color(make_float3(color[0], color[1], color[2]));
187  }
188  }
189 
190  // Skip attributes that are not needed
191  if ((std != ATTR_STD_NONE && _geom->need_attribute(scene, std)) ||
192  _geom->need_attribute(scene, name)) {
193  ApplyPrimvars(_geom->attributes, name, value, interpolation.second, std);
194  }
195  }
196  }
197 }
198 
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a point
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
PXR_NS::HdInterpolation GetPrimvarInterpolation(PXR_NS::HdSceneDelegate *sceneDelegate, const PXR_NS::TfToken &name) const
Definition: geometry.inl:241
PXR_NS::HdDirtyBits GetInitialDirtyBitsMask() const override
Definition: geometry.inl:48
HdCyclesPoints(const PXR_NS::SdfPath &rprimId, const PXR_NS::SdfPath &instancerId={})
~HdCyclesPoints() override
PXR_NS::HdDirtyBits GetInitialDirtyBitsMask() const override
void reserve(size_t newcapacity)
Scene scene
HDCYCLES_NAMESPACE_OPEN_SCOPE void ApplyPrimvars(AttributeSet &attributes, const ustring &name, VtValue value, AttributeElement elem, AttributeStandard std)
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
Definition: hydra/config.h:17
AttributeStandard
Definition: kernel/types.h:612
@ ATTR_STD_UV
Definition: kernel/types.h:616
@ ATTR_STD_VERTEX_NORMAL
Definition: kernel/types.h:614
@ ATTR_STD_NONE
Definition: kernel/types.h:613
@ ATTR_STD_VERTEX_COLOR
Definition: kernel/types.h:619
@ ATTR_ELEMENT_OBJECT
Definition: kernel/types.h:599
@ ATTR_ELEMENT_VERTEX
Definition: kernel/types.h:602
#define make_float3(x, y, z)
Definition: metal/compat.h:204