WPF 3D Transparency Depth-Order Sorting

原文:WPF 3D Transparency Depth-Order Sorting

?

Just a quick post here - When making WPF 3D apps, transparency can make for some powerful visual effects. The trouble is that any re-orientation of your scene or the camera will ruin the rendering order of your transparent content to obscure content which should be visible under a transparent surface. Below, we have a little carousel of partially transparent photos rendered on DiffuseMaterials. As per the rendering rules with DiffuseMaterials, only content which is closer to the camera will be rendered. The area highlighted in red is being rendered prematurely, which obscures the background content from being rendered at all.

To get over this issue with transparency, we must re-order the sequence of transparent content rendering, such that we progressively go from content most distant to the camera, to content in closest proximity.

To deal with this, I created a simple helper to handle sorting of models to sort in order of distance to camera. Below, we have the helper method interface - it takes the camera position, a collection of models and a World transform to offset the models by(This would be all of the accumulated parent transforms, except for the transforms on the Models themselves), and sorts?this collection of Models in order of distance to the camera. Here‘s the API from the attached Code, followed by a carousel of depth sorted images:

public static void AlphaSort(Point3D CameraPosition, Model3DCollection Models, Transform3D WorldTransform)

Some performance considerations with Depth-Order Sorting favoring transparency:

  1. Limit the scope of models to be sorted to as small a set as possible. Two reasons exist for this. The first is that additional models will require a?linearithmic?growth of sort time - the CLR Array.Sort algorithm?employed with the?attached solution?operates?with an average complexity of O(n log n) and a worst case of O(n^2). The second issue is about GPU fill rate - by sorting in favour of distant objects, we are maximizing the number pixels to render to screen(We get no?hardware culling benefits, because everything is in front of what we have rendered so far).?On the flip side, if you have a? complex scene with lots of opaque, overlapping content, sorting in favour of objects closest to the camera could potentially?get you an improved framerate, if sorting is infrequent.
  2. Frequency of sorting - With any kind of scene, the supplied sorting operation is not a cheap call to use?for real time rendering. As such, I would caution against using this algorithm via CompositionTarget.Rendering.?Instead, you may want to give some thought about an invalidation threshold based sorting mechanism. With a 3D arrangement like a carousel, you can work out?a safe "interval" of change, within which the content can move, without requiring a re-sort.

This sample is just intended as a quick starting point for folks who want to get some depth sorting on their transparent content. It is not tailored to target 3D tree-walking, and most folks can find optimizations for sorting based on domain specific assumptions about their application in terms of graph representation, caching, and all the other common tricks at an application developer‘s disposal.

原文地址:https://www.cnblogs.com/lonelyxmas/p/9844886.html

时间: 2024-10-09 04:31:28

WPF 3D Transparency Depth-Order Sorting的相关文章

WPF 3D中多个模型如何设置某一个在最前?

原文:WPF 3D中多个模型如何设置某一个在最前? 问题:我们的模型包括导入的3D solid模型和axis坐标轴模型,当模型旋转的时候,3D会将axis挡住. 期望:axis一直在最前面,不会被3D挡住. 方法: (1) 将模型设置为透明,<SolidColorBrush Color="#3333CC" Opacity="0.8"/>,同时,将axis使用EmissiveMaterial. 这样实现可以在旋转后看到axis,但是这种方法并没有让axis

WPF 3D 常用类(1)

原文:WPF 3D 常用类(1) 几何数据相关类 Geometry3D 抽象类, 用于定义物体的几何数据, 可用于计算HitTest和BoundingBox MeshGeometry3D Geometry3D的子类, 定义网格的顶点, 三角形顶点, 法线, Texture(纹理)的座标 常用属性: Positions, TriangleIndices, Noramls, TextureCoordinates 模型相关类 (模型=几何数据+变形(位置,旋转,尺寸)+材质) Model3D 抽象类,

WPF 3D: MeshGeometry3D纹理坐标的正确定义

原文 WPF 3D: MeshGeometry3D纹理坐标的正确定义 为了使基于2D的纹理显示在3D对象中,我们必须定义3D Mesh对象的纹理贴图坐标.在WPF中,此项功能则通过MeshGeometry3D.TextureCoordinates属性. 2D纹理的对应坐标和WPF的LinearGradientBrush的StartPoint和EndPoint一样. 下图来自MSDN关于LinearGradientBrush的StartPoint的说明: (0,0)代表整个图形的左上角,(1,1)

WPF 3D:MeshGeometry3D的定义和光照

原文 WPF 3D:MeshGeometry3D的定义和光照 由于WPF计算光照会根据整个平面的方向向量,所以如果在不同面上使用同一个点可能会达到不同的光照效果.让我们用不同的定义Mesh的方法来演示这个问题. 首先要定义两个简单的相交面,为方便定义,整个图形的主视图可以参考下图: 第一个方法就是用最简单的最笨的方法,一次性定义所有的点,这样两个面四个三角形一共12个点,TriangleIndices是从0到11. 如下代码: <MeshGeometry3D Positions="-1 0

WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体

原文:WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体 运行结果: 事实上很简单,定义好一个正方体,处理好纹理.关于MeshGeometry3D的正确定义和纹理这里就不多讲了,可以参考我以前写过的一些文章: WPF 3D: MeshGeometry3D纹理坐标的正确定义 WPF 3D:MeshGeometry3D的定义和光照 接下来就是怎样让它动起来.我们通过3D点动画来改变照相机(Camera类型)的位置(Position属性)从而使正方体动起来(这样的话实际上正方

WPF 3D:使用变换中的TranslateTransform3D

原文:WPF 3D:使用变换中的TranslateTransform3D 程序效果: WPF 3D中的TranslateTransform3D应该是所有3D变换中最简单的变换,使用起来非常简单,先定义好3D对象,接着在适当的位置加入变换就可以了. 当然WPF 3D中变换(Transform3D类型)可以应用在如下属性中: 照相机:Camera类型 3D模型定义:Model3D类型 容纳3D模型的Visual3D:ModelVisual3D类型 对于2D的TranslateTransform,只有

优化WPF 3D性能

Maximize WPF 3D Performance .NET Framework 4.5 As you use the Windows Presentation Foundation (WPF) to build 3D controls and include 3D scenes in your applications, it is important to consider performance optimization. This topic provides a list of 3

WPF 3D 小小小小引擎 - &#183;WPF 3D变换应用

原文:WPF 3D 小小小小引擎 - ·WPF 3D变换应用 WPF可以提供的3D模型使我们可以轻松地创建3D实体,虽然目前来看还很有一些性能上的问题,不过对于一些简单的3D应用应该是可取的,毕竟其开发效率高,而且也容易上手. 下面给大家演示的是使用在WPF 3D上实现视角变换,通过鼠标拖动来变换观察视角,通过滚轮来放缩视距. 有关3D的基础知识可以参考MSDN文档:三维图形概述 首先创建一个3D立方体,立方体是由六个面构成(F1, F2 ....F6)其XAML代码如下: <Viewport3

WPF 3D 知识点大全以及实例

原文:WPF 3D 知识点大全以及实例 引言 现在物联网概念这么火,如果监控的信息能够实时在手机的客服端中以3D形式展示给我们,那种体验大家可以发挥自己的想象. 那生活中我们还有很多地方用到这些,如上图所示的Kinect 在医疗上的应用,当然还有体感游戏等等. 3D 用来增加视觉效果,给人以更加直观,真实的感觉. 3D如此美妙,那我们在WPF中又该从何处入手开启我们的3D编程旅程? WPF中3D开发技术的基础知识应该有以下几点: 3D开发基础知识 WPF中3D开发的基础元素(Elements)