我创建了一个模型(立方体)并将其添加到我的视口中,它可以正常工作而没有任何问题。
this.mainViewport.Children.Add(model);
每当我使用以下代码时,立方体变成黑色而不是原来的颜色。
this.mainViewport.Children.Clear(); this.mainViewport.Children.Add(model);
我需要在下一秒将立方体添加到另一个位置时清除视口。任何解决该问题的建议将不胜感激。
I know that it is an old question, but right answer might help somebody. The cube gets black because
this.mainViewportChildren.Clear()
removes also light source (any of), to make cube get original color you must add to your code something like:
this.mainViewport.Children.Add(new ModelVisual3D() { Content = new AmbientLight(Colors.White) });
我会做这样的事情:第一次在它的开始位置创建立方体:
ModelVisual3D model = new ModelVisual3D();
model.Content = cube;
然后在计时器中,我将使用新位置更改变换:
model.Transform = new TranslateTransform3D(X, Y, Z); // model need to be a field in your class
其他可用的变压器是:
ScaleTransform3D
旋转变换3D
矩阵变换3D
变换3D组
//if you use ScreenSpaceLines3D
for (int i = _mainViewport.Children.Count - 1; i >= 0; i--)
{
ModelVisual3D modelVisual3D = ModelVisual3D)_mainViewport.Children[i];
if (modelVisual3D is ScreenSpaceLines3D)
{
ScreenSpaceLines3D line3D = ScreenSpaceLines3D)_mainViewport.Children[i];
line3D.Points.Clear();
continue;
}
if (modelVisual3D.Content is DirectionalLight == false)
{
mainViewport.Children.Remove(modelVisual3D);
}
}