0

SharpDX - Transparency from image

Anonymous 11 years ago updated by manypage 9 months ago 338
This discussion was imported from CodePlex

geoarsal wrote at 2014-03-07 10:05:

Hi,

how can i enable transparency from image for textures ??

In WPF3D, all i need to do is to create the diffuse material with image brush and use it. In SharpDX fork, I had created PhongMaterial and set its DiffuseMap to the same image i used in WPF3D, but here the transparent region are rendered as Black.

Thanks

Rogad wrote at 2014-03-08 16:05:

I used PNG textures. Also read this about how you need to order transparent parts :

http://www.ericsink.com/wpf3d/2_Transparency.html


I still have problems with layered transparency though, when one part of the model overlaps the other it does not look right.

geoarsal wrote at 2014-03-08 17:48:

Thank you Rogad for your reply.

I would like to know have you used PNG textures to attained transparency in "SharpDX fork of Helix Toolkit" ?? because the link you have provided is about transparency in WPF3D but I need transparency in SharpDX.

P.S. I already got the transparency using image texture in WPF3D but I require same functionality in SharpDX fork using that same image.

Rogad wrote at 2014-03-08 17:56:

Sorry I did not realise you were asking about SharpDX. I have not tried it, because I couldn't see how to get support, so gave up on it.

geoarsal wrote at 2014-03-12 19:04:

After debugging the code, I had resolved this issue. Following are the changes I needed to make in Helix SharpDx.


In the pixel shader "PShaderPhong" method, alpha value is taken explicitly from the diffuse material.

Current Code "Default.fx"
/// set diffuse alpha
I.a = vMaterialDiffuse.a;
Fix "Default.fx"
I.a = vMaterialDiffuse.a * vMaterialTexture[3];
Also the conversion of texture map is done through BMP encoder, however apparantly it looks like BMP encoder doesn't support alpha in texture map. Changing bmp encoder to png encoder will resolve this issue.

Current Code "RenderUtil.cs"
public static byte[] ToByteArray(this System.Windows.Media.Imaging.BitmapSource bitmapSource)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                var encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();
                encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmapSource));
                encoder.Save(ms);
                return ms.ToArray();
            }
        }
Fix "RenderUtil.cs"
public static byte[] ToByteArray(this System.Windows.Media.Imaging.BitmapSource bitmapSource)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                var encoder = new System.Windows.Media.Imaging.PngBitmapEncoder();
                encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmapSource));
                encoder.Save(ms);
                return ms.ToArray();
            }
        }

objo wrote at 2014-04-29 10:57:

I have forwarded this to the guys working on the sharpdx fork.

Rogad wrote at 2014-05-24 18:16:

Is there a similar fix for Helix ?

For my project this is the main area where Helix does not seem to work right.

Despite following various tutorials/guides on transparency, such as putting the transparent textures last I still end up with this :



Some of the transparency is working, but as you can see not all.

Would really like to fix this :)

Rogad wrote at 2014-05-30 16:46:

Anyone ? Objo ? Are you still with us ? :)

objo wrote at 2014-05-30 22:56:

hi Rogad, yes I am following the discussion, but don´t know how to solve this. How are you rendering the hair? Are you using SharpDX or WPF? Can you create a demo application we can include with the library?

Rogad wrote at 2014-05-30 23:08:

Hi :)

I'm using WPF and Helix. I have not used SharpDX for anything yet, I was trying to find some documentation for it or support, but haven't found much yet.

Unfortunately I cannot turn this into something distributable. The models I had to buy from Daz3D and I cannot redistribute them like that.