3D WPF Textures with Alpha

With WPF 3D, is it possible to have an alpha texture?

I want to make a 3D scene where all the flat surfaces (like all the cutouts for paper). I thought the easiest way to achieve this is to create an airplane, split it into several triangles (like fabric), and then apply a texture with a transparent background. That way, I could use the same geometry and just change the texture to get completely different shapes.

I tried adding PNG with a transparent background to the model in the form of a brush, but it appears on a white background. Does WPF 3D support this functionality? How can I do that?

+4
source share
2 answers

texture with alpha works in wpf (it works for me!). are you sure your png alpha is ok

a png with alpha in DiffuseMaterial il is all you need. here is a piece of xaml with a camera over two textured flat meshes.

replace the png path, put it in the xaml file and run. you should be able to see material2 behind alpha1 of material1.

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="640.0" Height="480.0" > <Grid.Resources> <MeshGeometry3D x:Key="Mesh" Positions="-4.999,-4.999,0.001 5.001,-4.999,0.001 -4.999,5.001,0.001 5.001,5.001,0.001 " TriangleIndices="2 0 3 1 3 0 " TextureCoordinates="0,0 1,0 0,1 1,1 " /> <DiffuseMaterial x:Key="material1" > <DiffuseMaterial.Brush> <ImageBrush ImageSource="C:\mypng.png" Transform="1,0,0,-1,0,1" /> </DiffuseMaterial.Brush> </DiffuseMaterial> <DiffuseMaterial x:Key="material2" > <DiffuseMaterial.Brush> <ImageBrush ImageSource="C:\image_4.png" Transform="1,0,0,-1,0,1" /> </DiffuseMaterial.Brush> </DiffuseMaterial> </Grid.Resources> <Viewport3D Width="640.0" Height="480.0" > <Viewport3D.Camera> <PerspectiveCamera Position="0,0,20"/> </Viewport3D.Camera> <ModelVisual3D> <ModelVisual3D.Content> <Model3DGroup > <AmbientLight /> <GeometryModel3D Geometry="{StaticResource Mesh}" Material="{StaticResource material2}"> <GeometryModel3D.Transform> <MatrixTransform3D Matrix="1.0,0.0,0.0,0,0.0,1.0,0.0,0,0.0,0.0,1.0,0,0.0,0.0,-5.0,1" /> </GeometryModel3D.Transform> </GeometryModel3D> <GeometryModel3D Geometry="{StaticResource Mesh}" Material="{StaticResource material1}"> <GeometryModel3D.Transform> <MatrixTransform3D Matrix="1.0,0.0,0.0,0,0.0,1.0,0.0,0,0.0,0.0,1.0,0,0.0,0.0,0.0,1" /> </GeometryModel3D.Transform> </GeometryModel3D> </Model3DGroup> </ModelVisual3D.Content> </ModelVisual3D> </Viewport3D> </Grid> 
+5
source

You can use a texture with an alpha channel, although WPF cannot handle this. I did the same as you and got the same white background. This is because you must display your geometric models with z-order. Down up.

However, it is sometimes impossible to show your models in this order (this is the case for me), and I really hope that they come up with a solution for this.

+2
source

Source: https://habr.com/ru/post/1285642/


All Articles