1use std::cell::Cell;
6use std::rc::Rc;
7
8use crate::core::object_ref::{ObjectRef, WeakObjectRef};
9use crate::core::{Layers, ObjectRenderHook, SceneEventListeners};
10use crate::math::{Euler, Matrix4, Quaternion, Vector3};
11use crate::objects::{InstancedBufferAttribute, Mesh, Payload};
12
13fn next_id() -> u32 {
15 thread_local! {
16 static OBJECT3D_ID: Cell<u32> = const { Cell::new(0) };
17 }
18
19 OBJECT3D_ID.with(|id| {
20 let value = id.get();
21 id.set(value + 1);
22 value
23 })
24}
25
26pub struct Object3D {
33 pub id: u32,
35 pub name: String,
37 pub object_type: &'static str,
40 pub parent: Option<WeakObjectRef>,
42 pub children: Vec<ObjectRef>,
44 pub visible: bool,
46 pub layers: Layers,
48 pub render_order: f64,
51 pub frustum_culled: bool,
53 pub matrix_auto_update: bool,
55 pub matrix_world_auto_update: bool,
57 pub matrix_world_needs_update: bool,
59 pub matrix_alias: Option<crate::core::WeakObjectRef>,
66 pub is_camera: bool,
68 pub is_light: bool,
70 pub cast_shadow: bool,
72 pub receive_shadow: bool,
76 pub occlusion_test: bool,
83 pub is_group: bool,
85 pub is_scene: bool,
87 pub position: Vector3,
89 pub rotation: Euler,
92 pub quaternion: Quaternion,
94 pub scale: Vector3,
96 pub up: Vector3,
98 pub matrix: Matrix4,
101 pub matrix_world: Matrix4,
103 pub user_data: serde_json::Map<String, serde_json::Value>,
108 pub payload: Payload,
111 pub listeners: SceneEventListeners,
115 pub on_before_render: Option<ObjectRenderHook>,
122 pub on_after_render: Option<ObjectRenderHook>,
126}
127
128impl std::fmt::Debug for Object3D {
129 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
130 f.debug_struct("Object3D")
131 .field("id", &self.id)
132 .field("name", &self.name)
133 .field("object_type", &self.object_type)
134 .field("parent", &self.parent)
135 .field("children", &self.children)
136 .field("visible", &self.visible)
137 .field("layers", &self.layers)
138 .field("render_order", &self.render_order)
139 .field("frustum_culled", &self.frustum_culled)
140 .field("matrix_auto_update", &self.matrix_auto_update)
141 .field("matrix_world_auto_update", &self.matrix_world_auto_update)
142 .field("matrix_world_needs_update", &self.matrix_world_needs_update)
143 .field("matrix_alias", &self.matrix_alias)
144 .field("is_camera", &self.is_camera)
145 .field("is_light", &self.is_light)
146 .field("cast_shadow", &self.cast_shadow)
147 .field("receive_shadow", &self.receive_shadow)
148 .field("occlusion_test", &self.occlusion_test)
149 .field("is_group", &self.is_group)
150 .field("is_scene", &self.is_scene)
151 .field("position", &self.position)
152 .field("rotation", &self.rotation)
153 .field("quaternion", &self.quaternion)
154 .field("scale", &self.scale)
155 .field("up", &self.up)
156 .field("matrix", &self.matrix)
157 .field("matrix_world", &self.matrix_world)
158 .field("user_data", &self.user_data)
159 .field("payload", &self.payload)
160 .field("listeners", &self.listeners)
161 .field("on_before_render", &self.on_before_render.is_some())
162 .field("on_after_render", &self.on_after_render.is_some())
163 .finish()
164 }
165}
166
167impl Default for Object3D {
168 fn default() -> Self {
169 Self {
170 id: next_id(),
171 name: String::new(),
172 object_type: "Object3D",
173 parent: None,
174 children: Vec::new(),
175 visible: true,
176 layers: Layers::default(),
177 render_order: 0.0,
178 frustum_culled: true,
179 matrix_auto_update: true,
180 matrix_world_auto_update: true,
181 matrix_world_needs_update: false,
182 matrix_alias: None,
183 is_camera: false,
184 is_light: false,
185 cast_shadow: false,
186 receive_shadow: false,
187 occlusion_test: false,
188 is_group: false,
189 is_scene: false,
190 position: Vector3::ZERO,
191 rotation: Euler::default(),
192 quaternion: Quaternion::default(),
193 scale: Vector3::new(1.0, 1.0, 1.0),
194 up: Vector3::new(0.0, 1.0, 0.0),
196 matrix: Matrix4::identity(),
197 matrix_world: Matrix4::identity(),
198 user_data: serde_json::Map::new(),
199 payload: Payload::None,
200 listeners: SceneEventListeners::default(),
201 on_before_render: None,
202 on_after_render: None,
203 }
204 }
205}
206
207impl Clone for Object3D {
213 fn clone(&self) -> Self {
214 Self {
215 id: next_id(),
216 name: self.name.clone(),
217 object_type: self.object_type,
218 parent: None,
219 children: Vec::new(),
220 visible: self.visible,
221 layers: self.layers,
222 render_order: self.render_order,
223 frustum_culled: self.frustum_culled,
224 matrix_auto_update: self.matrix_auto_update,
225 matrix_world_auto_update: self.matrix_world_auto_update,
226 matrix_world_needs_update: self.matrix_world_needs_update,
227 matrix_alias: None,
228 is_camera: self.is_camera,
229 is_light: self.is_light,
230 cast_shadow: self.cast_shadow,
231 receive_shadow: self.receive_shadow,
232 occlusion_test: false,
233 is_group: self.is_group,
234 is_scene: self.is_scene,
235 position: self.position,
236 rotation: self.rotation,
237 quaternion: self.quaternion,
238 scale: self.scale,
239 up: self.up,
240 matrix: self.matrix,
241 matrix_world: self.matrix_world,
242 user_data: self.user_data.clone(),
244 payload: self.payload.clone(),
245 listeners: SceneEventListeners::default(),
246 on_before_render: None,
247 on_after_render: None,
248 }
249 }
250}
251
252impl Object3D {
253 pub fn new_node() -> ObjectRef {
255 ObjectRef::new(Self::default())
256 }
257
258 pub fn into_node(self) -> ObjectRef {
260 ObjectRef::new(self)
261 }
262
263 pub fn set_on_before_render(
268 &mut self,
269 hook: impl FnMut(
270 &ObjectRef,
271 &crate::renderer::Renderer,
272 &crate::objects::Scene,
273 &dyn crate::cameras::RenderCamera,
274 Option<&crate::core::Group>,
275 ) + 'static,
276 ) {
277 self.on_before_render = Some(Box::new(hook));
278 }
279
280 pub fn set_on_after_render(
284 &mut self,
285 hook: impl FnMut(
286 &ObjectRef,
287 &crate::renderer::Renderer,
288 &crate::objects::Scene,
289 &dyn crate::cameras::RenderCamera,
290 Option<&crate::core::Group>,
291 ) + 'static,
292 ) {
293 self.on_after_render = Some(Box::new(hook));
294 }
295
296 pub fn is_mesh(&self) -> bool {
298 self.payload.is_mesh()
299 }
300
301 pub fn is_skinned_mesh(&self) -> bool {
303 self.payload.is_skinned_mesh()
304 }
305
306 pub fn skinned_mesh(&self) -> Option<&crate::objects::SkinnedMesh> {
308 self.payload.skinned_mesh()
309 }
310
311 pub fn skinned_mesh_mut(&mut self) -> Option<&mut crate::objects::SkinnedMesh> {
313 self.payload.skinned_mesh_mut()
314 }
315
316 pub fn is_instanced_mesh(&self) -> bool {
318 self.payload.is_instanced_mesh()
319 }
320
321 pub fn is_line(&self) -> bool {
323 self.payload.is_line()
324 }
325
326 pub fn is_points(&self) -> bool {
328 self.payload.is_points()
329 }
330
331 pub fn is_line_segments(&self) -> bool {
333 self.payload.is_line_segments()
334 }
335
336 pub fn line(&self) -> Option<&crate::objects::Line> {
338 self.payload.line()
339 }
340
341 pub fn line_mut(&mut self) -> Option<&mut crate::objects::Line> {
343 self.payload.line_mut()
344 }
345
346 pub fn geometry(&self) -> Option<&Rc<crate::core::BufferGeometry>> {
348 self.payload.geometry()
349 }
350
351 pub fn material(&self) -> Option<&crate::materials::MeshBasicNodeMaterial> {
353 self.payload.material()
354 }
355
356 pub fn mesh(&self) -> Option<&Mesh> {
358 self.payload.mesh()
359 }
360
361 pub fn mesh_mut(&mut self) -> Option<&mut Mesh> {
363 self.payload.mesh_mut()
364 }
365
366 pub fn light(&self) -> Option<&crate::lights::LightObject> {
369 self.payload.light()
370 }
371
372 pub fn light_mut(&mut self) -> Option<&mut crate::lights::LightObject> {
374 self.payload.light_mut()
375 }
376
377 pub(crate) fn instance_count(&self) -> u32 {
379 self.payload.count()
380 }
381
382 pub fn instance_matrix(&self) -> Option<&InstancedBufferAttribute> {
384 self.payload.instance_matrix()
385 }
386
387 pub fn instance_color(&self) -> Option<&InstancedBufferAttribute> {
389 self.payload.instance_color()
390 }
391
392 pub fn set_matrix_at(&mut self, index: usize, matrix: &Matrix4) {
396 self.payload
397 .instanced_mesh_mut()
398 .expect("three-rs: setMatrixAt on an object that is not an InstancedMesh")
399 .set_matrix_at(index, matrix);
400 }
401
402 pub fn set_color_at(&mut self, index: usize, color: &crate::math::Color) {
406 self.payload
407 .instanced_mesh_mut()
408 .expect("three-rs: setColorAt on an object that is not an InstancedMesh")
409 .set_color_at(index, color);
410 }
411
412 pub fn set_rotation(&mut self, x: f64, y: f64, z: f64) {
415 self.rotation.set(x, y, z);
416 self.quaternion.set_from_euler(&self.rotation);
417 }
418
419 pub(crate) fn sync_rotation_from_quaternion(&mut self) {
424 let (q, order) = (self.quaternion, self.rotation.order);
425 self.rotation.set_from_quaternion(&q, order);
426 }
427
428 pub fn apply_matrix4(&mut self, m: &Matrix4) {
430 if self.matrix_auto_update {
431 self.update_matrix();
432 }
433
434 self.matrix.premultiply(m);
435
436 let matrix = self.matrix;
437 let (mut position, mut quaternion, mut scale) =
438 (self.position, self.quaternion, self.scale);
439 matrix.decompose(&mut position, &mut quaternion, &mut scale);
440 self.position = position;
441 self.quaternion = quaternion;
442 self.scale = scale;
443
444 self.sync_rotation_from_quaternion();
445 }
446
447 pub fn apply_quaternion(&mut self, q: &Quaternion) {
449 self.quaternion.premultiply(q);
450 self.sync_rotation_from_quaternion();
451 }
452
453 pub fn set_rotation_from_axis_angle(&mut self, axis: &Vector3, angle: f64) {
455 self.quaternion.set_from_axis_angle(axis, angle);
457 self.sync_rotation_from_quaternion();
458 }
459
460 pub fn set_rotation_from_euler(&mut self, euler: &Euler) {
462 self.quaternion.set_from_euler(euler);
463 self.sync_rotation_from_quaternion();
464 }
465
466 pub fn set_rotation_from_matrix(&mut self, m: &Matrix4) {
469 self.quaternion.set_from_rotation_matrix(m);
470 self.sync_rotation_from_quaternion();
471 }
472
473 pub fn set_rotation_from_quaternion(&mut self, q: &Quaternion) {
475 self.quaternion.copy(q);
476 self.sync_rotation_from_quaternion();
477 }
478
479 pub fn rotate_on_axis(&mut self, axis: &Vector3, angle: f64) {
481 let mut q1 = Quaternion::default();
482 q1.set_from_axis_angle(axis, angle);
483
484 self.quaternion.multiply(&q1);
485 self.sync_rotation_from_quaternion();
486 }
487
488 pub fn rotate_on_world_axis(&mut self, axis: &Vector3, angle: f64) {
491 let mut q1 = Quaternion::default();
492 q1.set_from_axis_angle(axis, angle);
493
494 self.quaternion.premultiply(&q1);
495 self.sync_rotation_from_quaternion();
496 }
497
498 pub fn rotate_x(&mut self, angle: f64) {
500 self.rotate_on_axis(&Vector3::new(1.0, 0.0, 0.0), angle);
501 }
502
503 pub fn rotate_y(&mut self, angle: f64) {
505 self.rotate_on_axis(&Vector3::new(0.0, 1.0, 0.0), angle);
506 }
507
508 pub fn rotate_z(&mut self, angle: f64) {
510 self.rotate_on_axis(&Vector3::new(0.0, 0.0, 1.0), angle);
511 }
512
513 pub fn translate_on_axis(&mut self, axis: &Vector3, distance: f64) {
515 let mut v1 = *axis;
516 v1.apply_quaternion(&self.quaternion);
517 v1.multiply_scalar(distance);
518
519 self.position.add(&v1);
520 }
521
522 pub fn translate_x(&mut self, distance: f64) {
524 self.translate_on_axis(&Vector3::new(1.0, 0.0, 0.0), distance);
525 }
526
527 pub fn translate_y(&mut self, distance: f64) {
529 self.translate_on_axis(&Vector3::new(0.0, 1.0, 0.0), distance);
530 }
531
532 pub fn translate_z(&mut self, distance: f64) {
534 self.translate_on_axis(&Vector3::new(0.0, 0.0, 1.0), distance);
535 }
536
537 pub fn local_to_world(&mut self, vector: &mut Vector3) {
539 self.update_matrix_world(None);
540 vector.apply_matrix4(&self.matrix_world);
541 }
542
543 pub fn world_to_local(&mut self, vector: &mut Vector3) {
545 self.update_matrix_world(None);
546
547 let mut inverse = self.matrix_world;
548 inverse.invert();
549 vector.apply_matrix4(&inverse);
550 }
551
552 pub fn look_at(&mut self, target: &Vector3) {
556 self.update_matrix_world(None);
557
558 let mut position = Vector3::ZERO;
559 position.set_from_matrix_position(&self.matrix_world);
560
561 let mut m1 = Matrix4::identity();
562 m1.look_at(target, &position, &self.up);
563
564 self.quaternion.set_from_rotation_matrix(&m1);
565 self.sync_rotation_from_quaternion();
566 }
567
568 pub fn get_world_position(&mut self) -> Vector3 {
570 self.update_matrix_world(None);
571
572 let mut target = Vector3::ZERO;
573 target.set_from_matrix_position(&self.matrix_world);
574 target
575 }
576
577 pub fn get_world_quaternion(&mut self) -> Quaternion {
579 self.update_matrix_world(None);
580
581 let mut position = Vector3::ZERO;
582 let mut target = Quaternion::default();
583 let mut scale = Vector3::ZERO;
584 self.matrix_world
585 .decompose(&mut position, &mut target, &mut scale);
586 target
587 }
588
589 pub fn get_world_scale(&mut self) -> Vector3 {
591 self.update_matrix_world(None);
592
593 let mut position = Vector3::ZERO;
594 let mut quaternion = Quaternion::default();
595 let mut target = Vector3::ZERO;
596 self.matrix_world
597 .decompose(&mut position, &mut quaternion, &mut target);
598 target
599 }
600
601 pub fn get_world_direction(&mut self) -> Vector3 {
603 self.update_matrix_world(None);
604
605 let e = &self.matrix_world.elements;
606 let mut target = Vector3::new(e[8], e[9], e[10]);
607 target.normalize();
608 target
609 }
610
611 pub fn update_matrix(&mut self) {
613 let (position, quaternion, scale) = (self.position, self.quaternion, self.scale);
614 self.matrix.compose(&position, &quaternion, &scale);
615
616 self.matrix_world_needs_update = true;
617 }
618
619 pub fn update_matrix_world(&mut self, parent_matrix_world: Option<&Matrix4>) {
622 self.update_matrix_world_forced(parent_matrix_world, false);
623 }
624
625 pub(crate) fn update_matrix_world_forced(
631 &mut self,
632 parent_matrix_world: Option<&Matrix4>,
633 force: bool,
634 ) -> bool {
635 if self.matrix_auto_update {
636 self.update_matrix();
637 }
638
639 if self.matrix_world_needs_update || force {
640 if self.matrix_world_auto_update {
641 match parent_matrix_world {
642 None => self.matrix_world = self.matrix,
643 Some(parent) => {
644 let matrix = self.matrix;
645 self.matrix_world.multiply_matrices(parent, &matrix);
646 }
647 }
648 }
649
650 self.matrix_world_needs_update = false;
651
652 return true;
653 }
654
655 force
656 }
657}