Maximizar o desempenho do WPF 3D
Como você usa o Windows Presentation Foundation (WPF) para criar controles 3D e incluir cenas 3D em seus aplicativos, é importante considerar a otimização do desempenho. Este tópico fornece uma lista de classes 3D e propriedades que têm implicações de desempenho para seu aplicativo, juntamente com recomendações para otimizar o desempenho quando você usá-los.
This topic assumes an advanced understanding of Windows Presentation Foundation (WPF) 3D features. The suggestions in this document apply to "rendering tier 2"—roughly defined as hardware that supports pixel shader version 2.0 and vertex shader version 2.0. For more details, see Níveis de Renderização Gráfica.
Impacto de desempenho: High
Property |
Recommendation |
Brush speed (fastest to slowest): DrawingBrush (cached) VisualBrush (cached) DrawingBrush (uncached) VisualBrush (uncached) |
|
Definir Viewport3D.ClipToBounds como false, sempre que você não precisa ter Windows Presentation Foundation (WPF) explicitamente clip o conteúdo de um Viewport3D para o Viewport3D retângulo. Windows Presentation Foundation (WPF) o recorte de fundo não suavizado pode ser muito lento, e ClipToBounds está habilitado (lenta) por padrão em Viewport3D. |
|
Definir Viewport3D.IsHitTestVisible como false, sempre que você não precisa Windows Presentation Foundation (WPF) considerar o conteúdo de um o mouse deViewport3D ao executar o teste de hit. Visita de conteúdo 3D teste é feito no software e pode ser lento com grandes malhas. IsHitTestVisible está habilitado (lenta) por padrão em Viewport3D. |
|
Create different models only when they require different Materials or Transforms. Otherwise, try to coalesce many GeometryModel3D instances with the same Materials and Transforms into a few larger GeometryModel3D and MeshGeometry3D instances. |
|
Animação da malha — alterando os vértices individuais de uma malha em uma base por quadro — nem sempre é eficiente em Windows Presentation Foundation (WPF). Para minimizar o impacto de desempenho de notificações de alteração, quando cada vértice é modificado, desanexe da malha da árvore visual antes de executar a modificação de por vértice. Depois que tiver sido modificada a malha, anexe-o para a árvore visual. Além disso, tente minimizar o tamanho de malhas que será animada nesse modo. |
|
3D Antialiasing |
To increase rendering speed, disable multisampling on a Viewport3D by setting the attached property EdgeMode to Aliased. By default, 3D antialiasing is disabled on Microsoft Windows XP and enabled on Windows Vista with 4 samples per pixel. |
Text |
Live text in a 3D scene (live because it’s in a DrawingBrush or VisualBrush) can be slow. Try to use images of the text instead (via RenderTargetBitmap) unless the text will change. |
If you must use a VisualBrush or a DrawingBrush in a 3D scene because the brush’s content is not static, try caching the brush (setting the attached property CachingHint to Cache). Set the minimum and maximum scale invalidation thresholds (with the attached properties CacheInvalidationThresholdMinimum and CacheInvalidationThresholdMaximum) so that the cached brushes won’t be regenerated too frequently, while still maintaining your desired level of quality. By default, DrawingBrush and VisualBrush are not cached, meaning that every time something painted with the brush has to be re-rendered, the entire content of the brush must first be re-rendered to an intermediate surface. |
|
BitmapEffect forces all affected content to be rendered without hardware acceleration. For best performance, do not use BitmapEffect. |
Impacto de desempenho: Medium
Property |
Recommendation |
When a mesh is defined as abutting triangles with shared vertices and those vertices have the same position, normal, and texture coordinates, define each shared vertex only once and then define your triangles by index with TriangleIndices. |
|
Try to minimize texture sizes when you have explicit control over the size (when you’re using a RenderTargetBitmap and/or an ImageBrush). Note that lower resolution textures can decrease visual quality, so try to find the right balance between quality and performance. |
|
Opacity |
When rendering translucent 3D content (such as reflections), use the opacity properties on brushes or materials (via Opacity or Color) instead of creating a separate translucent Viewport3D by setting Viewport3D.Opacity to a value less than 1. |
Minimize the number of Viewport3D objects you’re using in a scene. Put many 3D models in the same Viewport3D rather than creating separate Viewport3D instances for each model. |
|
Typically it’s beneficial to reuse MeshGeometry3D, GeometryModel3D, Brushes, and Materials. All are multiparentable since they’re derived from Freezable. |
|
Call the Freeze method on Freezables when their properties will remain unchanged in your application. Freezing can decrease working set and increase speed. |
|
Use ImageBrush instead of VisualBrush or DrawingBrush when the content of the brush will not change. 2D content can be converted to an Image via RenderTargetBitmap and then used in an ImageBrush. |
|
Don’t use BackMaterial unless you actually need to see the back faces of your GeometryModel3D. |
|
Light speed (fastest to slowest): |
|
Try to keep mesh sizes under these limits: Positions: 20.001 Point3D instâncias TriangleIndices: 60,003 Int32 instâncias |
|
Material speed (fastest to slowest): |
|
Windows Presentation Foundation (WPF)3D não sair pincéis invisíveis (preto a temperatura ambiente pincéis, desmarque pincéis, etc.) de maneira consistente. Considere a possibilidade de omissão-los em sua cena. |
|
Each Material in a MaterialGroup causes another rendering pass, so including many materials, even simple materials, can dramatically increase the fill demands on your GPU. Minimize the number of materials in your MaterialGroup. |
Impacto de desempenho: Low
Property |
Recommendation |
When you don’t need animation or data binding, instead of using a transform group containing multiple transforms, use a single MatrixTransform3D, setting it to be the product of all the transforms that would otherwise exist independently in the transform group. |
|
Minimize the number of lights in your scene. Muitas luzes em uma cena forçará Windows Presentation Foundation (WPF) retorne para o processamento de software. Os limites são aproximadamente 110 DirectionalLight objetos, 70 PointLight objetos, ou 40 SpotLight objetos. |
|
Separate moving objects from static objects by putting them in separate ModelVisual3D instances. ModelVisual3D is "heavier" than GeometryModel3D because it caches transformed bounds. GeometryModel3D is optimized to be a model; ModelVisual3D is optimized to be a scene node. Use ModelVisual3D to put shared instances of GeometryModel3D into the scene. |
|
Minimize the number of times you change the number of lights in the scene. Each change of light count forces a shader regeneration and recompilation unless that configuration has existed previously (and thus had its shader cached). |
|
Light |
Black lights won’t be visible, but they will add to render time; consider omitting them. |
To minimize the construction time of large collections in Windows Presentation Foundation (WPF), such as a MeshGeometry3D’s Positions, Normals, TextureCoordinates, and TriangleIndices, pre-size the collections before value population. If possible, pass the collections’ constructors prepopulated data structures such as arrays or Lists. |