|
1 | 1 | using System; |
2 | 2 | using System.Linq; |
3 | 3 |
|
4 | | -namespace CodeCave.Threejs.Entities |
| 4 | +namespace CodeCave.Threejs.Entities; |
| 5 | + |
| 6 | +public static class Object3DExtensions |
5 | 7 | { |
6 | | - public static class Object3DExtensions |
| 8 | + /// <summary>Merges the specified other scene.</summary> |
| 9 | + /// <param name="objectScene">The object scene.</param> |
| 10 | + /// <param name="otherScene">The other scene.</param> |
| 11 | + /// <param name="newPosition">The new position of merged objects after merge.</param> |
| 12 | + /// <returns>Returns the modified <see cref="ObjectScene"/> with other scene merged into it. </returns> |
| 13 | + /// <exception cref="ArgumentNullException">objectScene |
| 14 | + /// or |
| 15 | + /// otherScene.</exception> |
| 16 | + public static ObjectScene Merge(this ObjectScene objectScene, ObjectScene otherScene, Vector3 newPosition = null) |
7 | 17 | { |
8 | | - /// <summary>Merges the specified other scene.</summary> |
9 | | - /// <param name="objectScene">The object scene.</param> |
10 | | - /// <param name="otherScene">The other scene.</param> |
11 | | - /// <param name="newPosition">The new position of merged objects after merge.</param> |
12 | | - /// <returns>Returns the modified <see cref="ObjectScene"/> with other scene merged into it. </returns> |
13 | | - /// <exception cref="ArgumentNullException">objectScene |
14 | | - /// or |
15 | | - /// otherScene.</exception> |
16 | | - public static ObjectScene Merge(this ObjectScene objectScene, ObjectScene otherScene, Vector3 newPosition = null) |
17 | | - { |
18 | | - if (objectScene is null) |
19 | | - throw new ArgumentNullException(nameof(objectScene)); |
| 18 | + if (objectScene is null) |
| 19 | + throw new ArgumentNullException(nameof(objectScene)); |
20 | 20 |
|
21 | | - if (otherScene is null) |
22 | | - throw new ArgumentNullException(nameof(otherScene)); |
| 21 | + if (otherScene is null) |
| 22 | + throw new ArgumentNullException(nameof(otherScene)); |
23 | 23 |
|
24 | | - foreach (var geometry in otherScene.Geometries) |
25 | | - objectScene.AddGeometry(geometry); |
| 24 | + foreach (var geometry in otherScene.Geometries) |
| 25 | + objectScene.AddGeometry(geometry); |
26 | 26 |
|
27 | | - foreach (var material in otherScene.Materials) |
28 | | - objectScene.AddMaterial(material); |
| 27 | + foreach (var material in otherScene.Materials) |
| 28 | + objectScene.AddMaterial(material); |
29 | 29 |
|
30 | | - if (otherScene.Object.Children.Any()) |
31 | | - { |
32 | | - foreach (var child in otherScene.Object.Children) |
33 | | - { |
34 | | - if (newPosition != null) |
35 | | - child.Position = newPosition; |
36 | | - objectScene.Object.AddChild(child); |
37 | | - } |
38 | | - } |
39 | | - else |
| 30 | + if (otherScene.Object.Children.Any()) |
| 31 | + { |
| 32 | + foreach (var child in otherScene.Object.Children) |
40 | 33 | { |
41 | 34 | if (newPosition != null) |
42 | | - otherScene.Object.Position = newPosition; |
43 | | - objectScene.Object.AddChild(otherScene.Object); |
| 35 | + child.Position = newPosition; |
| 36 | + objectScene.Object.AddChild(child); |
44 | 37 | } |
45 | | - |
46 | | - return objectScene; |
47 | 38 | } |
48 | | - |
49 | | - /// <summary>Adds a simple cube (of the given dimensions and color) to the scene.</summary> |
50 | | - /// <param name="objectScene">The object scene.</param> |
51 | | - /// <param name="width">The width.</param> |
52 | | - /// <param name="height">The height.</param> |
53 | | - /// <param name="depth">The depth.</param> |
54 | | - /// <param name="position">The position.</param> |
55 | | - /// <param name="color">The color.</param> |
56 | | - /// <returns>Returns the original <see cref="ObjectScene"/> with cube added into it.</returns> |
57 | | - /// <exception cref="ArgumentNullException">objectScene.</exception> |
58 | | - public static ObjectScene AddCube(this ObjectScene objectScene, double width, double height, double depth, Vector3 position = default, int color = 11674146) |
| 39 | + else |
59 | 40 | { |
60 | | - if (objectScene is null) |
61 | | - throw new ArgumentNullException(nameof(objectScene)); |
| 41 | + if (newPosition != null) |
| 42 | + otherScene.Object.Position = newPosition; |
| 43 | + objectScene.Object.AddChild(otherScene.Object); |
| 44 | + } |
62 | 45 |
|
63 | | - var geometry = new BoxGeometry(Guid.NewGuid().ToString(), width, height, depth); |
64 | | - objectScene.AddGeometry(geometry); |
| 46 | + return objectScene; |
| 47 | + } |
65 | 48 |
|
66 | | - var material = new MeshStandardMaterial(Guid.NewGuid().ToString()) |
67 | | - { |
68 | | - Color = color, |
69 | | - }; |
70 | | - objectScene.AddMaterial(material); |
| 49 | + /// <summary>Adds a simple cube (of the given dimensions and color) to the scene.</summary> |
| 50 | + /// <param name="objectScene">The object scene.</param> |
| 51 | + /// <param name="width">The width.</param> |
| 52 | + /// <param name="height">The height.</param> |
| 53 | + /// <param name="depth">The depth.</param> |
| 54 | + /// <param name="position">The position.</param> |
| 55 | + /// <param name="color">The color.</param> |
| 56 | + /// <returns>Returns the original <see cref="ObjectScene"/> with cube added into it.</returns> |
| 57 | + /// <exception cref="ArgumentNullException">objectScene.</exception> |
| 58 | + public static ObjectScene AddCube(this ObjectScene objectScene, double width, double height, double depth, Vector3 position = default, int color = 11674146) |
| 59 | + { |
| 60 | + if (objectScene is null) |
| 61 | + throw new ArgumentNullException(nameof(objectScene)); |
71 | 62 |
|
72 | | - var cubeObject = new Object3D("Mesh", Guid.NewGuid().ToString(), id: null) |
73 | | - { |
74 | | - CastShadow = true, |
75 | | - ReceiveShadow = true, |
76 | | - GeometryUuid = geometry.Uuid, |
77 | | - MaterialUuid = material.Uuid, |
78 | | - Position = position, |
79 | | - }; |
80 | | - objectScene.Object.AddChild(cubeObject); |
| 63 | + var geometry = new BoxGeometry(Guid.NewGuid().ToString(), width, height, depth); |
| 64 | + objectScene.AddGeometry(geometry); |
81 | 65 |
|
82 | | - return objectScene; |
83 | | - } |
| 66 | + var material = new MeshStandardMaterial(Guid.NewGuid().ToString()) |
| 67 | + { |
| 68 | + Color = color, |
| 69 | + }; |
| 70 | + objectScene.AddMaterial(material); |
84 | 71 |
|
85 | | - internal static System.Collections.Generic.IEnumerable<Object3D> Flatten(this System.Collections.Generic.IEnumerable<Object3D> collection) |
| 72 | + var cubeObject = new Object3D("Mesh", Guid.NewGuid().ToString(), id: null) |
86 | 73 | { |
87 | | - foreach (var obj in collection) |
88 | | - { |
89 | | - if ((obj?.Children?.Count ?? 0) == 0) |
90 | | - yield return obj; |
| 74 | + CastShadow = true, |
| 75 | + ReceiveShadow = true, |
| 76 | + GeometryUuid = geometry.Uuid, |
| 77 | + MaterialUuid = material.Uuid, |
| 78 | + Position = position, |
| 79 | + }; |
| 80 | + objectScene.Object.AddChild(cubeObject); |
91 | 81 |
|
92 | | - var children = obj?.Children?.Flatten() ?? |
| 82 | + return objectScene; |
| 83 | + } |
| 84 | + |
| 85 | + internal static System.Collections.Generic.IEnumerable<Object3D> Flatten(this System.Collections.Generic.IEnumerable<Object3D> collection) |
| 86 | + { |
| 87 | + foreach (var obj in collection) |
| 88 | + { |
| 89 | + if ((obj?.Children?.Count ?? 0) == 0) |
| 90 | + yield return obj; |
| 91 | + |
| 92 | + var children = obj?.Children?.Flatten() ?? |
93 | 93 | #if !NETFRAMEWORK |
94 | | - Array.Empty<Object3D>(); |
| 94 | + Array.Empty<Object3D>(); |
95 | 95 | #else |
96 | | - new Object3D[0]; |
| 96 | + new Object3D[0]; |
97 | 97 | #endif |
98 | | - foreach (var c in children) |
99 | | - yield return c; |
100 | | - } |
| 98 | + foreach (var c in children) |
| 99 | + yield return c; |
101 | 100 | } |
102 | 101 | } |
103 | 102 | } |
0 commit comments