Reputation: 10500
With WPF 3D, is it possible to have a texture with an alpha channel?
I am wanting to make a 3d scene where everything is flat surfaces (like everything is paper cutouts). I thought the easiest way to achieve this would be to create a plane, divide it into quite a few triangle (like a cloth), then apply a texture with a transparent background. This way I could use the same geometry and just swap out the texture to get completely different shapes.
I tried adding a PNG with a transparent background to a model as a brush, but it shows up with a white background. Does WPF 3D provide this functionality? how can it be done?
Upvotes: 4
Views: 4365
Reputation: 1643
It is possible to use texture with an alpha channel, although WPF doesn't handle it well. I did the same as you and got the same white background. This is due to the fact that you have to display your geometry models with a z-order. From bottom to top.
However sometimes it is not possible to really display your models in such an order (this is the case for me) and I really hope they come up with a solution for that.
Upvotes: 2
Reputation:
texture with alpha does works in wpf ( it works for me ! ). are you sure your png alpha is ok ?
a png with alpha in a DiffuseMaterial il all you need. here's a chunk of xaml with a camera over two textured planar meshes.
replace the path of the png, put it in a xaml file and launch. you should be able to see material2 behind alpha 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>
Upvotes: 6