c# - 球体的透明度不适用于我的立方体的所有侧面 - Helixtoolkit

标签 c# wpf xaml 3d helix-3d-toolkit

这里的第一个问题 - 希望我的格式正确。
因此,我尝试使用 Visual Studio 2015 中的 Helix-Toolkit 在 WPF 中创建一个包含 512 个 LED (8x8x8) 的 3D LED 立方体。我对 WPF 中的整个 3D 内容很陌生,所以我在这里可能会有一些误解。基本上我现在正在做的是:

  • 我的 XAML 代码中有一个名为“cube”的 ModelVisual3D。
  • 我有一个运行 512 次的循环 - 它创建一个 SphereVisual3D 并设置其 Material
  • 我将 LED 添加到 ModelVisual3D

我希望我的 LED 几乎是透明的,这样我就可以看到立方体内部的 LED 何时亮起。这就是我使用这种 Material 的原因:

 
DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(Color.FromArgb(50,255,255,255)));
sphere.Material = material;
 

Transparency is kind of working. Example: Transparency is working on the right/front side of the cube (pushing the button 'turns on' the led). But it is not working on the left or back side of the cube. Can someone explain me why? I think it is related to the lightsource of this scene. Transparency of the front side: Front-View transparent left side:Left side non-transparent

This is my MainWindow.xaml:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:h="http://helix-toolkit.org/wpf"
    xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" x:Class="WpfApplication1.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="720.268" Width="894.578"
    xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf" >
<Grid>
    <!-- Using the HelixToolkit -->
    <HelixToolkit:HelixViewport3D x:Name="viewport" ZoomExtentsWhenLoaded="True" ShowFrameRate="True" ShowCoordinateSystem="True" Margin="0,0,0,45" ClipToBounds="False" InfiniteSpin="True">
        <!-- Using the SunLight. Strange thing is: If I use the DefaultLight my Performance gets really bad -->
        <HelixToolkit:SunLight Brightness="1" ShowLights="True">
        </HelixToolkit:SunLight>
        <!-- This ModelVisual3D Object should contain my LED cube -->
        <ModelVisual3D x:Name="cube">
            </ModelVisual3D>
    </HelixToolkit:HelixViewport3D>
    <!-- The button turns on a LED - for transparecy testing purpose -->
    <Button Margin="377,650,386,0" Content="Push Me!" Click="Button_Click"/>
</Grid>

这是我的 Mainwindow.xaml.cs 隐藏代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HelixToolkit.Wpf;
using System.Windows.Media.Media3D;

namespace WpfApplication1
{
    /// <summary>
    /// Creating LEDs
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //Create 512 LEDs
            for (int x = 0; x < 7; x++)
            {
                for (int y = 7; y > 0; y--)
                {
                    for(int z = 0; z < 7; z++)
                    {
                        addSphere(x, z, y);
                    }
                }
            }
        }
        public void addSphere(double x, double z, double y)
        {
            SphereVisual3D sphere = new SphereVisual3D(); //Creating new 'LED'
            sphere.Radius = 0.25;
            sphere.Center = new Point3D(x, y, z);
            DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(Color.FromArgb(50,255,255,255)));
            sphere.Material = material;
            sphere.BackMaterial = material;
            //sphere.Fill = new SolidColorBrush(Colors.DarkBlue);
            cube.Children.Add(sphere);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Turns one LED red to test transparancy
            SphereVisual3D model = cube.Children[60] as SphereVisual3D;
            DiffuseMaterial material = new DiffuseMaterial(new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)));
            model.Material = material;
        }
    }
}

另一个奇怪的事情是,当我使用“HelixToolkit:DefaultLights”时,性能非常糟糕。我的帧速率约为 0-2 fps。可能是什么原因?

最佳答案

尝试添加 SortingVisual3D

    <h:HelixViewport3D ShowFrameRate="True" ShowTriangleCountInfo="True" ShowViewCube="True" >

        <h:SunLight/>

            <h:SortingVisual3D x:Name="view3d" Method="BoundingBoxCorners" SortingFrequency="30" CheckForOpaqueVisuals="True">

            <h:GridLinesVisual3D x:Name="grid" Width="500" Length="500" MajorDistance="50" MinorDistance="10"/>

            Add yours spheres here!

        </h:SortingVisual3D>
    </h:HelixViewport3D>

为了获得更好的性能,您应该减少球体上的三角形数量。 例如

        sphere.ThetaDiv = 10;
        sphere.PhiDiv = 10;

关于c# - 球体的透明度不适用于我的立方体的所有侧面 - Helixtoolkit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36356164/

相关文章:

c# - WPF 文本框中的必填字段验证

wpf - PasswordBox和TextBox的通用样式

c# - ListView 项目文本被截断并且没有全部显示

c# - NHibernate SQL 创建导致错误

c# - 无法加载文件或程序集 'WPG, PublicKeyToken=null' 或其依赖项之一

wpf - 如何实现一个多平台的多媒体应用?

c# - 如何设置多个viewbox最大的尺寸?

c# - Caliburn micro 将 message.attach 指向所需的 View 模型

c# - 设置标题的 MaxWidth 后,对所有 DataGridColumns 应用相同的高度

c# - WPF改变ListView滚动速度