-
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add spliceNode function to reparent an entire scene or subtree
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Scene, Node } from '@gltf-transform/core'; | ||
|
||
/** | ||
* Create a new node as child of the target {@link Scene} or {@link Node}. | ||
* Its existing children will be reparented under the new node. | ||
* | ||
* Example: | ||
* | ||
* ```javascript | ||
* import { transformScene } from '@gltf-transform/functions'; | ||
* | ||
* let scene = document.getRoot().getDefaultScene() | ||
* // rotate the entire scene a quarter turn around the y axis | ||
* spliceNode(scene).setRotation([0, Math.SQRT1_2, 0, Math.SQRT1_2]); | ||
* ``` | ||
* | ||
* @param parent Node or Scene whose children to reroot | ||
* @returns The new parent node | ||
*/ | ||
export function spliceNode(parent: Scene | Node): Node { | ||
const newNode = new Node(parent.getGraph()); | ||
for (const child of parent.listChildren()) { | ||
if (child.getSkin()) continue; | ||
newNode.addChild(child); | ||
} | ||
parent.addChild(newNode); | ||
return newNode; | ||
} |