Repository navigation
Expand file tree
/
Copy pathbind_objects.cpp
More file actions
273 lines (253 loc) · 17.7 KB
/
Copy pathbind_objects.cpp
File metadata and controls
273 lines (253 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Renderable objects and the Scene container.
#include "bindings.hpp"
#include <pybind11/functional.h>
#include <pybind11/stl.h>
#include <pybind11/typing.h>
#include "threepp/core/BufferGeometry.hpp"
#include "threepp/materials/Material.hpp"
#include "threepp/materials/PointsMaterial.hpp"
#include "threepp/materials/SpriteMaterial.hpp"
#include "threepp/objects/GrassMesh.hpp"
#include "threepp/objects/Group.hpp"
#include "threepp/objects/InstancedMesh.hpp"
#include "threepp/objects/Line.hpp"
#include "threepp/objects/LineSegments.hpp"
#include "threepp/objects/Mesh.hpp"
#include "threepp/objects/Points.hpp"
#include "threepp/objects/Sprite.hpp"
#include "threepp/scenes/Fog.hpp"
#include "threepp/scenes/FogExp2.hpp"
#include "threepp/scenes/Scene.hpp"
#include "threepp/textures/Texture.hpp"
using namespace threepp;
namespace threepp_py {
// What a `.material` property hands back, for the STUB'S benefit only.
//
// The object itself is still produced by material_to_py's
// dynamic_pointer_cast chain, which is not an accident: concrete materials
// derive from Material VIRTUALLY, so pybind11 must never be handed a
// shared_ptr<Material> and asked to downcast it — it reinterprets the holder
// and the virtual-base offset makes that pointer wrong. Casting the already
// downcast shared_ptr<Concrete> is the safe form (see bind_materials.cpp).
//
// That returns py::object, which pybind11 annotates `typing.Any` — a type
// checker then offers NO completion and checks nothing. Saying `Material`
// gives back the fields every material shares (name, opacity, side, ...),
// which the base carries for exactly this purpose, and `isinstance` narrows
// to a concrete class for the rest (color, roughness, ...).
using PyMaterial = py::typing::Optional<Material>;
// Mesh/Points/Line derive from Object3D *virtually*. pybind11 mishandles
// EVERY access that crosses that virtual base: inherited def_readwrite
// members (writing std::string `name` corrupts the heap) and methods bound
// via `&Object3D::method` or even a lambda taking `Object3D&` (the self
// pointer is silently wrong). The only reliable access is through the
// concrete type, so bind the whole Object3D surface on the virtual roots
// with concrete (`T&` / `&T::field`) handlers. These shadow the broken
// inherited bindings in the Python MRO. Non-virtual Object3D subclasses
// (Scene/Group/cameras/lights/Sprite) use the base bindings directly, and
// InstancedMesh/LineSegments inherit these via their non-virtual parent.
template<class Cls>
static void bind_object3d_api(Cls& c) {
using T = typename Cls::type;
c.def_readwrite("name", &T::name)
.def_readwrite("position", &T::position)
.def_readwrite("rotation", &T::rotation)
.def_readwrite("quaternion", &T::quaternion)
.def_readwrite("scale", &T::scale)
.def_readwrite("up", &T::up)
.def_readwrite("visible", &T::visible)
.def_readwrite("cast_shadow", &T::castShadow)
.def_readwrite("receive_shadow", &T::receiveShadow)
.def_readwrite("frustum_culled", &T::frustumCulled)
.def_readwrite("render_order", &T::renderOrder)
.def_readwrite("matrix_auto_update", &T::matrixAutoUpdate)
.def_property_readonly("id", [](const T& o) { return o.id; })
.def_property_readonly("uuid", [](const T& o) { return o.uuid; })
.def_property_readonly("parent", [](T& o) { return o.parent; }, py::return_value_policy::reference)
.def_property_readonly("children", [](T& o) { return o.children; }, py::return_value_policy::reference)
.def("add", [](T& self, const py::args& children) {
for (const auto& ch : children) self.add(as_object3d(ch));
})
.def("remove", [](T& self, const py::handle& child) { self.remove(*as_object3d(child)); }, py::arg("object"))
.def("remove_from_parent", [](T& o) { o.removeFromParent(); })
.def("clear", [](T& o) { o.clear(); })
.def("rotate_x", [](T& o, float a) { o.rotateX(a); }, py::arg("angle"))
.def("rotate_y", [](T& o, float a) { o.rotateY(a); }, py::arg("angle"))
.def("rotate_z", [](T& o, float a) { o.rotateZ(a); }, py::arg("angle"))
.def("rotate_on_axis", [](T& o, const Vector3& ax, float a) { o.rotateOnAxis(ax, a); }, py::arg("axis"), py::arg("angle"))
.def("translate_x", [](T& o, float d) { o.translateX(d); }, py::arg("distance"))
.def("translate_y", [](T& o, float d) { o.translateY(d); }, py::arg("distance"))
.def("translate_z", [](T& o, float d) { o.translateZ(d); }, py::arg("distance"))
.def("look_at", [](T& o, float x, float y, float z) { o.lookAt(x, y, z); }, py::arg("x"), py::arg("y"), py::arg("z"))
.def("look_at", [](T& o, const Vector3& v) { o.lookAt(v); }, py::arg("vector"))
.def("get_world_position", [](T& o) { Vector3 v; o.getWorldPosition(v); return v; })
.def("get_world_direction", [](T& o) { Vector3 v; o.getWorldDirection(v); return v; })
// World-space pose — mirrors the Object3D base bindings in bind_core.cpp
// (must be kept in sync: this template shadows them for virtual-base
// leaves Mesh/Points/Line). All refresh the world matrix first.
.def("get_world_quaternion", [](T& o) { Quaternion q; o.getWorldQuaternion(q); return q; })
.def("get_world_scale", [](T& o) { Vector3 v; o.getWorldScale(v); return v; })
.def_property_readonly("matrix_world", [](T& o) { o.updateWorldMatrix(true, false); return *o.matrixWorld; })
.def("local_to_world", [](T& o, Vector3 v) { o.localToWorld(v); return v; }, py::arg("vector"))
.def("world_to_local", [](T& o, Vector3 v) { o.worldToLocal(v); return v; }, py::arg("vector"))
.def("get_object_by_name", [](T& o, const std::string& n) { return o.getObjectByName(n); }, py::arg("name"), py::return_value_policy::reference)
// The lambda takes T& so pybind extracts a correct self pointer;
// the T& -> Object3D& upcast inside is plain C++ and safe.
.def("get_user_data", [](const T& o, const std::string& key) { return user_data_string(o, key); }, py::arg("key"))
.def("set_user_data", [](T& o, const std::string& key, const std::string& value) { set_user_data_string(o, key, value); },
py::arg("key"), py::arg("value"))
.def("traverse", [](T& self, const std::function<void(py::object)>& cb) {
self.traverse([&cb](Object3D& o) { cb(py::cast(&o, py::return_value_policy::reference)); });
}, py::arg("callback"))
.def("update_matrix", [](T& o) { o.updateMatrix(); })
.def("update_matrix_world", [](T& o, bool force) { o.updateMatrixWorld(force); }, py::arg("force") = false)
.def("__repr__", [](const T& o) { return "<threepp." + o.type() + " name='" + o.name + "'>"; });
}
void init_objects(py::module_& m) {
// ---- Mesh ------------------------------------------------------------
auto mesh = py::class_<Mesh, Object3D, std::shared_ptr<Mesh>>(m, "Mesh");
bind_object3d_api(mesh);
mesh.def(py::init([](std::shared_ptr<BufferGeometry> g, const py::object& mat) {
return Mesh::create(std::move(g), as_material(mat));
}),
py::arg("geometry") = std::shared_ptr<BufferGeometry>{}, py::arg("material") = py::none())
.def_property_readonly("geometry", &Mesh::geometry)
.def_property_readonly("material", [](Mesh& self) { return PyMaterial(material_to_py(self.material())); })
.def("set_geometry", &Mesh::setGeometry, py::arg("geometry"))
.def("set_material", [](Mesh& self, const py::object& mat) { self.setMaterial(as_material(mat)); }, py::arg("material"));
// ---- Group -----------------------------------------------------------
py::class_<Group, Object3D, std::shared_ptr<Group>>(m, "Group")
.def(py::init(&Group::create));
// ---- InstancedMesh ---------------------------------------------------
// InstancedMesh inherits Mesh's concrete Object3D API (Mesh is a
// non-virtual base of InstancedMesh, so that access is safe).
auto inst = py::class_<InstancedMesh, Mesh, std::shared_ptr<InstancedMesh>>(m, "InstancedMesh");
inst.def(py::init([](std::shared_ptr<BufferGeometry> g, const py::object& mat, size_t count) {
return InstancedMesh::create(std::move(g), as_material(mat), count);
}),
py::arg("geometry"), py::arg("material"), py::arg("count"))
.def_property_readonly("count", &InstancedMesh::count)
.def("set_count", &InstancedMesh::setCount, py::arg("count"))
.def("set_matrix_at", &InstancedMesh::setMatrixAt, py::arg("index"), py::arg("matrix"))
.def("get_matrix_at", [](InstancedMesh& im, size_t i) {
Matrix4 mtx;
im.getMatrixAt(i, mtx);
return mtx;
}, py::arg("index"))
.def("set_color_at", &InstancedMesh::setColorAt, py::arg("index"), py::arg("color"))
.def("instance_matrix_needs_update", [](InstancedMesh& im) { im.instanceMatrix()->needsUpdate(); })
.def("instance_color_needs_update", [](InstancedMesh& im) {
if (auto* c = im.instanceColor()) c->needsUpdate();
});
// ---- GrassMesh -------------------------------------------------------
// Mesh subclass whose vertices are GPU wind-displaced by the Vulkan path
// tracer's grass-wind compute pass (renders as a plain static Mesh on
// GL). Inherits Mesh's concrete Object3D API via its non-virtual
// Mesh parent (same as InstancedMesh). The geometry must be ONE merged
// blade field carrying a per-vertex float "heightFrac" attribute (0 at a
// blade's base, 1 at its tip) that drives the wind sway weighting.
auto grass = py::class_<GrassMesh, Mesh, std::shared_ptr<GrassMesh>>(m, "GrassMesh");
grass.def(py::init([](std::shared_ptr<BufferGeometry> g, const py::object& mat) {
return GrassMesh::create(std::move(g), as_material(mat));
}),
py::arg("geometry"), py::arg("material"))
.def_property("wind_strength",
[](GrassMesh& g) { return g.params.windStrength; },
[](GrassMesh& g, float v) { g.params.windStrength = v; })
.def_property("wind_dir",
[](GrassMesh& g) { return g.params.windDir; },
[](GrassMesh& g, const Vector2& v) { g.params.windDir = v; })
.def_property("time",
[](GrassMesh& g) { return g.params.time; },
[](GrassMesh& g, float v) { g.params.time = v; },
"Animation clock (seconds); set per frame to advance the wind.");
// ---- Points ----------------------------------------------------------
auto points = py::class_<Points, Object3D, std::shared_ptr<Points>>(m, "Points");
bind_object3d_api(points);
points.def(py::init([](std::shared_ptr<BufferGeometry> g, const py::object& matObj) {
if (!g) g = BufferGeometry::create();
auto mat = as_material(matObj);
if (!mat) mat = PointsMaterial::create();
return Points::create(std::move(g), std::move(mat));
}),
py::arg("geometry") = std::shared_ptr<BufferGeometry>{}, py::arg("material") = py::none())
.def_property_readonly("geometry", &Points::geometry)
.def_property_readonly("material", [](Points& p) { return PyMaterial(material_to_py(p.material())); });
// ---- Line / LineSegments ---------------------------------------------
auto line = py::class_<Line, Object3D, std::shared_ptr<Line>>(m, "Line");
bind_object3d_api(line);
line.def(py::init([](std::shared_ptr<BufferGeometry> g, const py::object& mat) {
return Line::create(std::move(g), as_material(mat));
}),
py::arg("geometry") = std::shared_ptr<BufferGeometry>{}, py::arg("material") = py::none())
.def_property_readonly("geometry", &Line::geometry)
.def_property_readonly("material", [](Line& l) { return PyMaterial(material_to_py(l.material())); })
.def("compute_line_distances", &Line::computeLineDistances);
// LineSegments inherits Line's concrete Object3D API (non-virtual base).
auto lineSeg = py::class_<LineSegments, Line, std::shared_ptr<LineSegments>>(m, "LineSegments");
lineSeg.def(py::init([](std::shared_ptr<BufferGeometry> g, const py::object& mat) {
return LineSegments::create(std::move(g), as_material(mat));
}),
py::arg("geometry") = std::shared_ptr<BufferGeometry>{}, py::arg("material") = py::none());
// ---- Sprite ----------------------------------------------------------
py::class_<Sprite, Object3D, std::shared_ptr<Sprite>>(m, "Sprite")
.def(py::init([](std::shared_ptr<SpriteMaterial> mat) {
return Sprite::create(std::move(mat));
}),
py::arg("material") = std::shared_ptr<SpriteMaterial>{})
.def_readwrite("center", &Sprite::center)
.def_readwrite("screen_space", &Sprite::screenSpace)
.def_readwrite("screen_anchor", &Sprite::screenAnchor)
.def_property_readonly("material", [](Sprite& s) { return PyMaterial(material_to_py(s.material())); });
// ---- Background / Fog ------------------------------------------------
py::class_<Background>(m, "Background")
.def(py::init<int>(), py::arg("color"))
.def(py::init<const Color&>(), py::arg("color"))
.def(py::init<const std::shared_ptr<Texture>&>(), py::arg("texture"))
.def("is_color", &Background::isColor)
.def("is_texture", &Background::isTexture);
py::implicitly_convertible<int, Background>();
py::implicitly_convertible<Color, Background>();
py::class_<Fog>(m, "Fog")
.def(py::init<const Color&, float, float>(), py::arg("color"), py::arg("near") = 1.f, py::arg("far") = 1000.f)
.def_readwrite("color", &Fog::color)
.def_readwrite("near", &Fog::nearPlane)
.def_readwrite("far", &Fog::farPlane);
py::class_<FogExp2>(m, "FogExp2")
.def(py::init<const Color&, float>(), py::arg("color"), py::arg("density") = 0.00025f,
"Exponential (Beer-Lambert) participating-media fog. density = σ_t (extinction per metre). "
"On Vulkan this drives full volumetric single-scattering; on GL it falls back to GL_EXP2.")
.def_readwrite("color", &FogExp2::color)
.def_readwrite("density", &FogExp2::density);
// ---- Scene -----------------------------------------------------------
py::class_<Scene, Object3D, std::shared_ptr<Scene>>(m, "Scene")
.def(py::init(&Scene::create))
// Accepts a hex int / Color (solid color) or a Texture (e.g. an
// equirect HDR from RGBELoader) for an image backdrop.
.def_property("background",
[](Scene& s) { return s.background; },
[](Scene& s, const py::object& v) {
if (py::isinstance<Texture>(v)) {
s.background = Background(v.cast<std::shared_ptr<Texture>>());
} else {
s.background = v.cast<Background>();// int / Color / Background
}
})
// The image-based-lighting environment map (an equirect HDR Texture
// from RGBELoader). Drives IBL on standard/physical materials; the GL
// renderer PMREM-prefilters it. None to clear.
.def_readwrite("environment", &Scene::environment)
.def_readwrite("override_material", &Scene::overrideMaterial)
.def_readwrite("auto_update", &Scene::autoUpdate)
// Convenience: linear distance fog. (scene.fog is a std::variant
// under the hood; this avoids exposing the variant to Python.)
.def("set_fog", [](Scene& s, const Color& c, float near, float far) { s.fog = Fog(c, near, far); },
py::arg("color"), py::arg("near") = 1.f, py::arg("far") = 1000.f)
.def("set_fog_exp2", [](Scene& s, const Color& c, float density) { s.fog = FogExp2(c, density); },
py::arg("color"), py::arg("density") = 0.02f,
"Exponential participating-media fog. On Vulkan: Beer-Lambert + volumetric scattering. "
"Call renderer.fog_anisotropy to tune the Henyey-Greenstein phase (0 = isotropic, "
"+0.9 = forward god-rays, -0.9 = back-scatter halo).")
.def("clear_fog", [](Scene& s) { s.fog.reset(); });
}
}// namespace threepp_py