Question

I am facing issue while drawing semitransparent object with RenderToSurface(While it working file when i am drawing object direct on device). Issue is when i m drawing a object with Alpha value 50% on RenderToSurface, and when i am drawing surface to device then transparency of object is not valid. My code is as follow.

[code] RenderingSurface.BeginScene(RenderTexture.GetSurfaceLevel(0), view);

                _device.Clear(ClearFlags.Target| ClearFlags.ZBuffer, Color.FromArgb(0, Color.Black), 1.0f, 0);
                using (Sprite s = new Sprite(_device))
                {
                        s.Begin(SpriteFlags.DoNotSaveState);
                    s.Draw(ObjecTexture, new Microsoft.DirectX.Vector3(0, 0, 0), new Microsoft.DirectX.Vector3(0, 1, 0), Color.White.ToArgb());
                    s.End();
                }
                RenderingSurface.EndScene(Filter.None);

RenderSurface have same shape with 50% tranparency.

Code to Draw Surface.

_device.BeginScene();
        _device.Clear(ClearFlags.Target | ClearFlags.ZBuffer | ClearFlags.Stencil, BackgroundColor, 1, 0);

  using (Sprite s = new Sprite(_device))
    {
        s.Begin(SpriteFlags.DoNotSaveState);
        s.Draw(RenderTexture, new Microsoft.DirectX.Vector3(0, 0, 0), new Microsoft.DirectX.Vector3(0, 1, 0), Color.White.ToArgb());
        s.End();
    }
Was it helpful?

Solution

Make sure your RenderSurface render target is created with a PixelFormat that has an alpha channel (A8R8G8B8 rather than X8R8G8B8).

Also, when rendering in the render target, make sure the resulting alpha is being written to the surface using the right blend mode render states for the alpha channel. Please note that blend modes for alpha (AlphaDestinationBlend, AlphaSourceBlend, ...) and colors (DestinationBlend, SourceBlend, ...) are different; make sure you set both.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top