[英]Rotate and translate an object
I need to rotate and translate an object.我需要旋转和平移一个对象。 I have a problem with that, I can only do one of them.
我有一个问题,我只能做其中之一。 I use this code:
我使用这个代码:
RotateTransform3D myRotate = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), Convert.ToDouble(180)), new Point3D(0, 0, 0));
TranslateTransform3D myTranslate = new TranslateTransform3D(0, 0, 100);
ModelVisual3D device3D2 = new ModelVisual3D();
device3D2.Content = Display3d(MODEL_PATH2);
device3D2.Transform = myRotate;
device3D2.Transform = myTranslate;
viewPort3d.Children.Add(device3D2);
The problem is that it only does the last transform.问题是它只做最后一次转换。 I mean, if I apply "myRotate" the second one, it rotates the object, but it doesn't apply the "myTranslate" operation.
我的意思是,如果我应用“myRotate”第二个,它会旋转对象,但它不会应用“myTranslate”操作。 I need to do both transforms.
我需要做两个转换。
I am using HelixToolkit too.我也在使用 HelixToolkit。
You have to mess with the Transform
fields directly, instead of replacing the entire Transform
, ie transform.position
, transform.rotation
, transform.localPosition
, transform.localEulerAngles
, transform.scale
, transform.localScale
, etc.您必须直接处理
Transform
字段,而不是替换整个Transform
,即transform.position
、 transform.rotation
、 transform.localPosition
、 transform.localEulerAngles
、 transform.scale
、 transform.localScale
等。
device3D2.Transform.rotation *= new Quaternion.AngleAxis(180f, new Vector3(0f, 0f, 1f));
device3D2.Transform.Translate(0, 0, 100);
You need to use a Transform3DGroup object.您需要使用 Transform3DGroup 对象。 You then add the different transformations to it:
然后向其添加不同的转换:
Transform3DGroup myTransformer = new Transform3DGroup;
RotateTransform3D myRotate = new RotateTransform3D(new AxisAngleRotation3D (new Vector3D(0, 0, 1), Convert.ToDouble(180)), new Point3D(0, 0, 0));
TranslateTransform3D myTranslate = new TranslateTransform3D(0, 0, 100);
myTransformer.Children.Add(myRotate);
myTransformer.Children.Add(myTranslate);
ModelVisual3D device3D2 = new ModelVisual3D();
device3D2.Content = Display3d(MODEL_PATH2);
device3D2.Transform = myTransformer;
viewPort3d.Children.Add(device3D2);
Pay attention to the sequence in which you add the individual transformers.请注意添加各个变压器的顺序。
I am using HelixToolkit as well.我也在使用 HelixToolkit。
Here I create model and translate it on scene:在这里,我创建模型并在现场进行翻译:
ModelVisual3D mdl = new ModelVisual3D();
mdl.Content = getModel3D();
if ((thecurrentBox.upperLeft.X != 0)||(thecurrentBox.bottomRight.Y!=0)) {
Matrix3D mm = mdl.Transform.Value;
mm.Translate(new Vector3D(-thecurrentBox.upperLeft.X, 0, -thecurrentBox.bottomRight.Y));
// you can do even more transformations here.
//you can make mm as private field and transform it whenever you like
mdl.Transform = new MatrixTransform3D(mm);
}
// add mdl to Children of scene
I get a current value of transformation matrix, do ops on it then set up a transform on ModelVisual3D instance.我得到转换矩阵的当前值,对其进行操作,然后在 ModelVisual3D 实例上设置转换。 Here is link to another example: https://github.com/wolfoerster/WFTools3D/blob/50cc33f9f9929d4651d0855c386d38e6861382b2/WFTools3D/Basics/Object3D.cs#L142
这是另一个示例的链接: https : //github.com/wolfoerster/WFTools3D/blob/50cc33f9f9929d4651d0855c386d38e6861382b2/WFTools3D/Basics/Object3D.cs#L142
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.