Blender V5.3
instancer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "instancer.hh"
6
7#include <pxr/imaging/hd/instancedBySchema.h>
8#include <pxr/imaging/hd/instancerTopologySchema.h>
9#include <pxr/imaging/hd/primvarSchema.h>
10#include <pxr/imaging/hd/primvarsSchema.h>
11#include <pxr/imaging/hd/retainedDataSource.h>
12#include <pxr/imaging/hd/tokens.h>
13#include <pxr/imaging/hd/xformSchema.h>
14
15#include "BLI_assert.hh"
16
17#include "util.hh"
18
19namespace blender::io::hydra {
20
21pxr::HdContainerDataSourceHandle build_instanced_by_data_source(const pxr::SdfPath &instancer_path,
22 const pxr::SdfPath &prototype_root)
23{
24 return pxr::HdInstancedBySchema::Builder()
25 .SetPaths(
26 pxr::HdRetainedTypedSampledDataSource<pxr::VtArray<pxr::SdfPath>>::New({instancer_path}))
27 .SetPrototypeRoots(
28 pxr::HdRetainedTypedSampledDataSource<pxr::VtArray<pxr::SdfPath>>::New({prototype_root}))
29 .Build();
30}
31
32pxr::HdContainerDataSourceHandle build_instancer_prim_data_source(
33 Span<pxr::SdfPath> prototypes,
34 Span<pxr::VtIntArray> instance_indices,
35 const pxr::VtMatrix4dArray &transforms)
36{
37 pxr::VtArray<pxr::SdfPath> proto_array(prototypes.begin(), prototypes.end());
38
39 /* Per-prototype subset of top-level instance indices. */
40 BLI_assert(!instance_indices.is_empty());
42 idx_values.reserve(instance_indices.size());
43 for (const pxr::VtIntArray &indices : instance_indices) {
44 idx_values.append(pxr::HdRetainedTypedSampledDataSource<pxr::VtIntArray>::New(indices));
45 }
46 pxr::HdVectorDataSourceHandle indices_ds = pxr::HdRetainedSmallVectorDataSource::New(
47 idx_values.size(), idx_values.data());
48
49 pxr::HdContainerDataSourceHandle topology =
50 pxr::HdInstancerTopologySchema::Builder()
51 .SetPrototypes(
52 pxr::HdRetainedTypedSampledDataSource<pxr::VtArray<pxr::SdfPath>>::New(proto_array))
53 .SetInstanceIndices(indices_ds)
54 .Build();
55
56 /* Instance transforms primvar. */
57 pxr::HdContainerDataSourceHandle xforms_primvar =
58 pxr::HdPrimvarSchema::Builder()
59 .SetPrimvarValue(
60 pxr::HdRetainedTypedSampledDataSource<pxr::VtMatrix4dArray>::New(transforms))
61 .SetInterpolation(pxr::HdPrimvarSchema::BuildInterpolationDataSource(
62 pxr::HdPrimvarSchemaTokens->instance))
63 .SetRole(pxr::HdPrimvarSchema::BuildRoleDataSource(pxr::HdPrimvarRoleTokens->none))
64 .Build();
65 pxr::HdContainerDataSourceHandle primvars = pxr::HdRetainedContainerDataSource::New(
66 pxr::HdInstancerTokens->instanceTransforms, xforms_primvar);
67
68 pxr::HdContainerDataSourceHandle xform =
69 pxr::HdXformSchema::Builder()
70 .SetMatrix(
71 pxr::HdRetainedTypedSampledDataSource<pxr::GfMatrix4d>::New(pxr::GfMatrix4d(1.0)))
72 .SetResetXformStack(pxr::HdRetainedTypedSampledDataSource<bool>::New(true))
73 .Build();
74
76 b.add(pxr::HdInstancerTopologySchema::GetSchemaToken(), topology);
77 b.add(pxr::HdPrimvarsSchema::GetSchemaToken(), primvars);
78 b.add(pxr::HdXformSchema::GetSchemaToken(), xform);
79 return b.build();
80}
81
82} // namespace blender::io::hydra
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T * end() const
Definition BLI_span.hh:228
constexpr const T * begin() const
Definition BLI_span.hh:224
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t size() const
void append(const T &value)
void reserve(const int64_t min_capacity)
pxr::HdContainerDataSourceHandle build_instanced_by_data_source(const pxr::SdfPath &instancer_path, const pxr::SdfPath &prototype_root)
Definition instancer.cc:21
pxr::HdContainerDataSourceHandle build_instancer_prim_data_source(Span< pxr::SdfPath > prototypes, Span< pxr::VtIntArray > instance_indices, const pxr::VtMatrix4dArray &transforms)
Definition instancer.cc:32
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)