Adding Transformation to the Node
Contents
[
Hide
]
Aspose.3D for .NET offers to rotate objects in 3D space. There are three ways to define object’s rotation in 3D space, Euler angles, Quaternion and Custom Matrix, all of them are supported by the
Transform class.
TSR (Translation/Scaling/Rotation) are most commonly used in 3D scenario, we provided a class Transform to access these in Aspose.3D. Affine transformations include:
- Translation
- Scaling
- Rotation
- Shear mapping
- Squeeze mapping
The
Mesh class object is being used in the code. We can create a Mesh class object as narrated there.
Rotate by Quaternion
// Initialize scene object
Scene scene = new Scene();
// Initialize Node class object
Node cubeNode = new Node("cube");
// Point node to a Box mesh geometry
cubeNode.Entity = new Box().ToMesh();
// Set rotation
cubeNode.Transform.Rotation = Quaternion.FromRotation(new Vector3(0, 1, 0), new Vector3(0.3, 0.5, 0.1));
// Set translation
cubeNode.Transform.Translation = new Vector3(0, 0, 20);
// Add cube to the scene
scene.RootNode.ChildNodes.Add(cubeNode);
// Save 3D scene in the supported file formats
scene.Save("TransformationToNode.fbx");
Rotate by Euler Angles
// Initialize scene object
Scene scene = new Scene();
// Initialize Node class object
Node cubeNode = new Node("cube");
// Point node to a Box mesh geometry
cubeNode.Entity = new Box().ToMesh();
// Euler angles
cubeNode.Transform.EulerAngles = new Vector3(0.3, 0.1, -0.5);
// Set translation
cubeNode.Transform.Translation = new Vector3(0, 0, 20);
// Add cube to the scene
scene.RootNode.ChildNodes.Add(cubeNode);
// Save 3D scene in the supported file formats
scene.Save("TransformationToNode.fbx");
Custom Transformation Matrix
We can also use Matrix directly:
// Initialize scene object
Scene scene = new Scene();
// Initialize Node class object
Node cubeNode = new Node("cube");
// Point node to a Box mesh geometry
cubeNode.Entity = new Box().ToMesh();
// Set custom translation matrix
cubeNode.Transform.TransformMatrix = new Matrix4(
1, -0.3, 0, 0,
0.4, 1, 0.3, 0,
0, 0, 1, 0,
0, 20, 0, 1
);
// Add cube to the scene
scene.RootNode.ChildNodes.Add(cubeNode);
// Save 3D scene in the supported file formats
scene.Save("TransformationToNode.fbx");