none
Viewport2DVisual3D能设置背面材质吗? RRS feed

  • 问题

  • 如题, 我需要将一些图片添加到三维场景中, 由于已经对这些图片应用了触控逻辑, 所以直接添加到Viewport2DVisual3D中的话就能够非常简单的完成场景的构建并且能够响应触摸事件.

    但是现在的问题是, 当摄像机旋转到这些图片的背面时, 不会有任何东西显示, 而Viewport2DVisual3D也没有相应的BackMaterial或BackVisual.

    所以我想知道:如果我将一个控件(如按钮)添加到三维场景, 如何既能够显示其正面, 又能够显示其背面, 仅使用Viewport2DVisual3D是否能够实现这样的功能.

    想了几种办法, 都觉得不太好:

    1.直接用ModelVisual3D, 将控件转换为画刷刷到GeometryModel3D的正面和背面, 但是这样就不能使用控件原有的触摸事件响应器.

    2.使用Viewport2DVisual3D, 用另外的一个Visual3D来设置其背面, 但是这样需要添加额外的模型, 比较繁琐且可能影响性能.

    有什么好的办法呢, 求教.

    2014年11月19日 7:17

答案

  • 你好 Lymim,

    我搜了下MSDN发现了一个人的例子,还不错,你可以试试:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/52e4c43f-85f8-4e03-903d-3e7c1c60d117/seeing-the-back-of-the-viewport2dvisual3d

    <Page
    
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <DockPanel>
    
    <ScrollBar Name="horz" DockPanel.Dock="Bottom" Orientation="Horizontal" 
    
    Minimum="-180" Maximum="180" 
    
    LargeChange="10" SmallChange="1" /> 
    
    <Viewport3D>
    
    <Viewport3D.Camera>
    
    <PerspectiveCamera Position="0, 0, 4"/>
    
    </Viewport3D.Camera>
    
    <!-- Button on 3D -->
    
    <Viewport2DVisual3D>
    
    <!-- Give the plane a slight rotation -->
    
    <Viewport2DVisual3D.Transform>
    
    <RotateTransform3D>
    
    <RotateTransform3D.Rotation>
    
    <AxisAngleRotation3D Angle="{Binding ElementName=horz, Path=Value}" Axis="0, 1, 0" />
    
    </RotateTransform3D.Rotation>
    
    </RotateTransform3D>
    
    </Viewport2DVisual3D.Transform>
    
    <!-- The Geometry, Material, and Visual for the Viewport2DVisual3D -->
    
    <Viewport2DVisual3D.Geometry>
    
    <MeshGeometry3D Positions="-1,1,0 -1,-1,0 1,-1,0 1,1,0"
    
    TextureCoordinates="0,0 0,1 1,1 1,0" TriangleIndices="0 1 2 0 2 3"/>
    
    </Viewport2DVisual3D.Geometry>
    
    <Viewport2DVisual3D.Material>
    
    <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="White"/>
    
    </Viewport2DVisual3D.Material> 
    
    <Button>Hello, 3D</Button>
    
    </Viewport2DVisual3D>
    
    <Viewport2DVisual3D>
    
    <!-- Give the plane a slight rotation -->
    
    <Viewport2DVisual3D.Transform>
    
    <RotateTransform3D>
    
    <RotateTransform3D.Rotation>
    
    <AxisAngleRotation3D Angle="{Binding ElementName=horz, Path=Value}" Axis="0, 1, 0" />
    
    </RotateTransform3D.Rotation>
    
    </RotateTransform3D>
    
    </Viewport2DVisual3D.Transform>
    
    <!-- The Geometry, Material, and Visual for the Viewport2DVisual3D -->
    
    <Viewport2DVisual3D.Geometry>
    
    <MeshGeometry3D Positions="-1,1,0 -1,-1,0 1,-1,0 1,1,0"
    
    TextureCoordinates="0,0 0,1 1,1 1,0" TriangleIndices="0 2 1 0 3 2"/>
    
    </Viewport2DVisual3D.Geometry>
    
    <Viewport2DVisual3D.Material>
    
    <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="White"/>
    
    </Viewport2DVisual3D.Material> 
    
    <Button>Hello, 3D back</Button>
    
    </Viewport2DVisual3D>
    
     
    
    <!-- Lights -->
    
    <ModelVisual3D>
    
    <ModelVisual3D.Content>
    
    <DirectionalLight Color="#FFFFFFFF" Direction="0,0,-1"/>
    
    </ModelVisual3D.Content>
    
    </ModelVisual3D>
    
    </Viewport3D>
    
    </DockPanel>
    
    </Page>
    

    你看看效果先,满足不满足要求。



    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年11月20日 6:53
    版主

全部回复

  • 你好 Lymim,

    我搜了下MSDN发现了一个人的例子,还不错,你可以试试:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/52e4c43f-85f8-4e03-903d-3e7c1c60d117/seeing-the-back-of-the-viewport2dvisual3d

    <Page
    
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <DockPanel>
    
    <ScrollBar Name="horz" DockPanel.Dock="Bottom" Orientation="Horizontal" 
    
    Minimum="-180" Maximum="180" 
    
    LargeChange="10" SmallChange="1" /> 
    
    <Viewport3D>
    
    <Viewport3D.Camera>
    
    <PerspectiveCamera Position="0, 0, 4"/>
    
    </Viewport3D.Camera>
    
    <!-- Button on 3D -->
    
    <Viewport2DVisual3D>
    
    <!-- Give the plane a slight rotation -->
    
    <Viewport2DVisual3D.Transform>
    
    <RotateTransform3D>
    
    <RotateTransform3D.Rotation>
    
    <AxisAngleRotation3D Angle="{Binding ElementName=horz, Path=Value}" Axis="0, 1, 0" />
    
    </RotateTransform3D.Rotation>
    
    </RotateTransform3D>
    
    </Viewport2DVisual3D.Transform>
    
    <!-- The Geometry, Material, and Visual for the Viewport2DVisual3D -->
    
    <Viewport2DVisual3D.Geometry>
    
    <MeshGeometry3D Positions="-1,1,0 -1,-1,0 1,-1,0 1,1,0"
    
    TextureCoordinates="0,0 0,1 1,1 1,0" TriangleIndices="0 1 2 0 2 3"/>
    
    </Viewport2DVisual3D.Geometry>
    
    <Viewport2DVisual3D.Material>
    
    <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="White"/>
    
    </Viewport2DVisual3D.Material> 
    
    <Button>Hello, 3D</Button>
    
    </Viewport2DVisual3D>
    
    <Viewport2DVisual3D>
    
    <!-- Give the plane a slight rotation -->
    
    <Viewport2DVisual3D.Transform>
    
    <RotateTransform3D>
    
    <RotateTransform3D.Rotation>
    
    <AxisAngleRotation3D Angle="{Binding ElementName=horz, Path=Value}" Axis="0, 1, 0" />
    
    </RotateTransform3D.Rotation>
    
    </RotateTransform3D>
    
    </Viewport2DVisual3D.Transform>
    
    <!-- The Geometry, Material, and Visual for the Viewport2DVisual3D -->
    
    <Viewport2DVisual3D.Geometry>
    
    <MeshGeometry3D Positions="-1,1,0 -1,-1,0 1,-1,0 1,1,0"
    
    TextureCoordinates="0,0 0,1 1,1 1,0" TriangleIndices="0 2 1 0 3 2"/>
    
    </Viewport2DVisual3D.Geometry>
    
    <Viewport2DVisual3D.Material>
    
    <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="White"/>
    
    </Viewport2DVisual3D.Material> 
    
    <Button>Hello, 3D back</Button>
    
    </Viewport2DVisual3D>
    
     
    
    <!-- Lights -->
    
    <ModelVisual3D>
    
    <ModelVisual3D.Content>
    
    <DirectionalLight Color="#FFFFFFFF" Direction="0,0,-1"/>
    
    </ModelVisual3D.Content>
    
    </ModelVisual3D>
    
    </Viewport3D>
    
    </DockPanel>
    
    </Page>
    

    你看看效果先,满足不满足要求。



    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年11月20日 6:53
    版主
  • 如我所说的方案2, 这种方法是另外构建一个Visual3D来绘制背面.
    但是这样有两个不足的地方:
    1.由于正反面必须(至少在外观上)相同, 所以当控件比较复杂的时候, 需要编写代码来实现将正面内容拷贝到背面的工作.
    2.由于正反面是不同的两个元素, 所以需要各自订阅事件(如果背面是控件而不是截图的话), 还需要设置绑定来同步他们的状态.
    正是考虑到这两方面, 所以没有第一时间采用这种办法.
    虽然没有达到我最初的目标, 但是参照这个代码还是能实现最终功能的.
    感谢您的回答!!!
    2014年11月20日 9:15