具有正交相机剪裁的 Viewport3D

Posted

技术标签:

【中文标题】具有正交相机剪裁的 Viewport3D【英文标题】:Viewport3D with orthographic camera clipping 【发布时间】:2014-12-09 04:03:22 【问题描述】:

将 ViewPort3D 与正交相机一起使用时,3D 模型正在裁剪,如下面的第二个链接所示。就好像视图是基于透视相机并且不显示其背后的东西,即使它使用的是正交相机。

View of the entire 3D model

Perspective view & Orthographic view

上面使用代码生成图片

Used code to generate pictures above (toggling Orthographic to True/False)

<Window x:Class="HelixToolkitTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:helix="http://helix-toolkit.org/wpf"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <GeometryModel3D x:Key="GeometryModel">

                <GeometryModel3D.Geometry>
                    <MeshGeometry3D
                 TriangleIndices="0,1,2 3,4,5 "
                 Normals="0,0,1 0,0,1 0,0,1 0,0,1 0,0,1 0,0,1 "
                 TextureCoordinates="0,0 1,0 1,1 1,1 0,1 0,0 "
                 Positions="-0.5,-0.5,0.5 0.5,-0.5,0.5 0.5,0.5,0.5 0.5,0.5,0.5 -0.5,0.5,0.5 -0.5,-0.5,0.5 " />
                </GeometryModel3D.Geometry>

                <GeometryModel3D.Material>
                    <MaterialGroup>
                        <DiffuseMaterial>
                            <DiffuseMaterial.Brush>
                                <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                                    <LinearGradientBrush.GradientStops>
                                        <GradientStop Color="Yellow" Offset="0" />
                                        <GradientStop Color="Red" Offset="0.25" />
                                        <GradientStop Color="Blue" Offset="0.75" />
                                        <GradientStop Color="LimeGreen" Offset="1" />
                                    </LinearGradientBrush.GradientStops>
                                </LinearGradientBrush>
                            </DiffuseMaterial.Brush>
                        </DiffuseMaterial>
                    </MaterialGroup>
                </GeometryModel3D.Material>

                <GeometryModel3D.Transform>
                    <ScaleTransform3D ScaleX="50" ScaleY="1" ScaleZ="1"/>
                </GeometryModel3D.Transform>
            </GeometryModel3D>
    <DirectionalLight x:Key="DirectionalLight" Color="#FFFFFF" Direction="-0.612372,-0.5,-0.612372" />
</Window.Resources>
<UniformGrid Columns="3">
    <GroupBox Header="Perspective camera">
        <Viewport3D>
            <Viewport3D.Camera>
                <PerspectiveCamera FarPlaneDistance="Infinity" NearPlaneDistance="0.1" LookDirection="12.064,-1.167,-0.831" UpDirection="-0.73,0.071,0.679"
                            Position="-12.064,1.167,0.831"/>
            </Viewport3D.Camera>
            <ModelVisual3D Content="StaticResource DirectionalLight"/>
            <ModelVisual3D Content="StaticResource GeometryModel"/>
        </Viewport3D>
    </GroupBox>
    <GroupBox Header="Orthographic camera">
        <Viewport3D>
            <Viewport3D.Camera>
                <OrthographicCamera FarPlaneDistance="Infinity" NearPlaneDistance="0.1" LookDirection="12.064,-1.167,-0.831" UpDirection="-0.73,0.071,0.679"
                            Position="-12.064,1.167,0.831" Width="9.313"/>
            </Viewport3D.Camera>
            <ModelVisual3D Content="StaticResource DirectionalLight"/>
            <ModelVisual3D Content="StaticResource GeometryModel"/>
        </Viewport3D>
    </GroupBox>

知道如何解决这个问题吗?

谢谢,

编辑:我实际上找到了解决方案,您只需将正交相机的NearPlaneDistance设置为-Infinity,请参见下面的代码:

<Window x:Class="HelixToolkitTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:helix="http://helix-toolkit.org/wpf"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <GeometryModel3D x:Key="GeometryModel">

                <GeometryModel3D.Geometry>
                    <MeshGeometry3D
                 TriangleIndices="0,1,2 3,4,5 "
                 Normals="0,0,1 0,0,1 0,0,1 0,0,1 0,0,1 0,0,1 "
                 TextureCoordinates="0,0 1,0 1,1 1,1 0,1 0,0 "
                 Positions="-0.5,-0.5,0.5 0.5,-0.5,0.5 0.5,0.5,0.5 0.5,0.5,0.5 -0.5,0.5,0.5 -0.5,-0.5,0.5 " />
                </GeometryModel3D.Geometry>

                <GeometryModel3D.Material>
                    <MaterialGroup>
                        <DiffuseMaterial>
                            <DiffuseMaterial.Brush>
                                <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                                    <LinearGradientBrush.GradientStops>
                                        <GradientStop Color="Yellow" Offset="0" />
                                        <GradientStop Color="Red" Offset="0.25" />
                                        <GradientStop Color="Blue" Offset="0.75" />
                                        <GradientStop Color="LimeGreen" Offset="1" />
                                    </LinearGradientBrush.GradientStops>
                                </LinearGradientBrush>
                            </DiffuseMaterial.Brush>
                        </DiffuseMaterial>
                    </MaterialGroup>
                </GeometryModel3D.Material>

                <GeometryModel3D.Transform>
                    <ScaleTransform3D ScaleX="50" ScaleY="1" ScaleZ="1"/>
                </GeometryModel3D.Transform>
            </GeometryModel3D>
    <DirectionalLight x:Key="DirectionalLight" Color="#FFFFFF" Direction="-0.612372,-0.5,-0.612372" />
</Window.Resources>
<UniformGrid Columns="3">
    <GroupBox Header="Perspective camera">
        <Viewport3D>
            <Viewport3D.Camera>
                <PerspectiveCamera FarPlaneDistance="Infinity" NearPlaneDistance="0.1" LookDirection="12.064,-1.167,-0.831" UpDirection="-0.73,0.071,0.679"
                            Position="-12.064,1.167,0.831"/>
            </Viewport3D.Camera>
            <ModelVisual3D Content="StaticResource DirectionalLight"/>
            <ModelVisual3D Content="StaticResource GeometryModel"/>
        </Viewport3D>
    </GroupBox>
    <GroupBox Header="Orthographic camera">
        <Viewport3D>
            <Viewport3D.Camera>
                <OrthographicCamera FarPlaneDistance="Infinity" NearPlaneDistance="-Infinity" LookDirection="12.064,-1.167,-0.831" UpDirection="-0.73,0.071,0.679"
                            Position="-12.064,1.167,0.831" Width="9.313"/>
            </Viewport3D.Camera>
            <ModelVisual3D Content="StaticResource DirectionalLight"/>
            <ModelVisual3D Content="StaticResource GeometryModel"/>
        </Viewport3D>
    </GroupBox>

【问题讨论】:

你期待什么?我看不出代码和行为有什么问题。您使用了一个 GeometryModel3D,并且您看到了它,形成了两种相机类型。 我期待看到整个模型(带有黄色部分),如下图所示:Perspective view & Orthographic view。但现在没关系,我找到了解决方案(见上面编辑过的问题)。 然后把你的相机移远一点。 而不是设置负的 NearPlaneDistance,微软不建议这样做,而是像 AgentFire 建议的那样将相机移得更远。另外,尝试增加 LookDirection 向量的大小。通常这无关紧要,除了 Helix ZoomToExtents 方法使用 LookDirection 向量作为相对偏移来重新定位相机。 【参考方案1】:

我和你有同样的问题。测试了几个解决方案终于找到了解决方案。

您必须增加或定义PerspectiveCameraFarPlaneDistance属性。

祝你好运

【讨论】:

以上是关于具有正交相机剪裁的 Viewport3D的主要内容,如果未能解决你的问题,请参考以下文章

three.js正交投影照相机

Libgdx之正交相机 OrthographicCamera

Libgdx之正交相机 OrthographicCamera

ThreeJS将相机更改为正交

正交相机和使用光线投射选择对象

在反应三纤维中移动正交相机