Transform — Aspose.3D FOSS for Java

Example: com.aspose.threed (aspose-3d-foss)

Transform 控制一个的位置信息、方向和缩放 Node 在 3D 场景中。每个节点恰好有一个 Transform,,通过 node.getTransform().

public class Transform extends A3DObject

访问节点的 Transform

import com.aspose.threed.Scene;

Scene scene = new Scene();
Node node = scene.getRootNode().createChildNode("box");
Transform t = node.getTransform();

Example

ExampleExampleExampleExampleExample
translationVector3getTranslation()setTranslation(Vector3)位置相对于父节点原点的偏移。默认 Vector3(0, 0, 0).
scalingVector3getScaling()setScaling(Vector3)每个轴的非均匀缩放因子。默认 Vector3(1, 1, 1).
rotationQuaterniongetRotation()setRotation(Quaternion)旋转以单位四元数表示。设置此项也会更新欧拉角。.
eulerAnglesVector3getEulerAngles()setEulerAngles(Vector3)以度为单位的 (X, Y, Z) 欧拉角旋转。设置此项也会更新四元数旋转。.
preRotationVector3getPreRotation()setPreRotation(Vector3)欧拉角在主旋转之前应用(用于 FBX 骨架)。.
postRotationVector3getPostRotation()setPostRotation(Vector3)欧拉角在主旋转之后应用。.
geometricTranslationVector3getGeometricTranslation()setGeometricTranslation(Vector3)仅本地平移:影响几何体,但不影响子节点。.
geometricScalingVector3getGeometricScaling()setGeometricScaling(Vector3)仅本地缩放:影响几何体,但不影响子节点。.
geometricRotationVector3getGeometricRotation()setGeometricRotation(Vector3)仅本地旋转:影响几何体,但不影响子节点。.
transformMatrixMatrix4getTransformMatrix()setTransformMatrix(Matrix4)完整的 4x4 组合变换矩阵。已缓存;在任意组件更改时重新计算。设置该矩阵会分解回平移/缩放/旋转。.

流式设置方法

所有 setter 返回 this,,启用链式调用::

ExampleExampleExample
setTranslation(double tx, double ty, double tz)double, double, double设置位置。.
setScale(double sx, double sy, double sz)double, double, double设置缩放。.
setEulerAngles(double rx, double ry, double rz)double, double, double设置旋转(度)。.
setRotation(double rx, double ry, double rz, double rw)double, double, double, double将旋转设置为四元数 (x, y, z, w)。.
setPreRotation(double rx, double ry, double rz)double, double, double设置预旋转(度)。.
setPostRotation(double rx, double ry, double rz)double, double, double设置后旋转(度)。.
setGeometricTranslation(double x, double y, double z)double, double, double设置几何平移。.
setGeometricScaling(double sx, double sy, double sz)double, double, double设置几何缩放。.
setGeometricRotation(double rx, double ry, double rz)double, double, double设置几何旋转(度)。.

使用示例

设置节点位置

import com.aspose.threed.Scene;
import com.aspose.threed.*;

Scene scene = new Scene();
Node node = scene.getRootNode().createChildNode("box");

// Using fluent setters
node.getTransform().setTranslation(5.0, 0.0, -3.0);

// Or via property setter
node.getTransform().setTranslation(new Vector3(5.0, 0.0, -3.0));

使用欧拉角旋转

import com.aspose.threed.Scene;

Scene scene = new Scene();
Node node = scene.getRootNode().createChildNode("rotated");

// Rotate 45 degrees around Y axis
node.getTransform().setEulerAngles(0.0, 45.0, 0.0);

// Chain multiple operations
node.getTransform()
    .setTranslation(2.0, 0.0, 0.0)
    .setScale(2.0, 2.0, 2.0);

使用四元数旋转

import com.aspose.threed.Scene;
import com.aspose.threed.*;
import com.aspose.threed.*;

Scene scene = new Scene();
Node node = scene.getRootNode().createChildNode("q-rotated");

// 90 degrees rotation around the Y axis
double halfAngle = Math.toRadians(45);   // half the total rotation
Quaternion q = new Quaternion(Math.cos(halfAngle), 0.0, Math.sin(halfAngle), 0.0);
node.getTransform().setRotation(q);

System.out.println("Euler: " + node.getTransform().getEulerAngles());   // synced automatically

同时进行缩放和位置设置

import com.aspose.threed.Scene;

Scene scene = new Scene();
Node node = scene.getRootNode().createChildNode("big-offset");

node.getTransform()
    .setTranslation(10.0, 0.0, 0.0)
    .setScale(3.0, 3.0, 3.0)
    .setEulerAngles(0.0, 90.0, 0.0);

几何偏移(枢轴偏移)

几何变换会相对于节点的枢轴移动几何体,而不影响子节点;这对于将网格居中于其节点非常有用::

import com.aspose.threed.Scene;

Scene scene = new Scene();
Node node = scene.getRootNode().createChildNode("centered-mesh");

// Shift the mesh half a unit down so its base sits at y=0
node.getTransform().setGeometricTranslation(0.0, -0.5, 0.0);

读取组合变换矩阵

import com.aspose.threed.Scene;
import com.aspose.threed.*;

Scene scene = new Scene();
Node node = scene.getRootNode().createChildNode("m");
node.getTransform().setTranslation(1.0, 2.0, 3.0);
node.getTransform().setEulerAngles(0.0, 45.0, 0.0);

Matrix4 m = node.getTransform().getTransformMatrix();
System.out.println(m.getClass().getName());   // com.aspose.threed.Matrix4

另见

 中文