XNA 消锯齿

这一阵子写程序,被人批评,这么大的锯齿。感觉很不爽,哎。

查了查,XNA是有个可以消锯齿的方法的。在XNA3.1的帮助里有关于消锯齿的一个范例


自己后来搜集了一些原理 参见这篇http://blog.csdn.net/xuehuic/article/details/5804213

 

相关内容 :

(1)what is antialiasing?

Antialiasing is the process of softening hard edges in an image so that polygons appear less jagged.

Antialiasing is normally accomplished by multisampling, which means you use more than one pixel ("sample") from an image to determine the color of a pixel in your final image. The more samples per pixel, the smoother the resulting image.

Both PC and Xbox 360 platforms support Full Screen Antialiasing (FSAA) modes, where extra pixels are rendered to a render target and used as samples to create an antialiased final image. 4x FSAA uses four samples per pixel by rendering the scene to a render target with twice the height and width of the backbuffer. It uses the extra samples in the render target to create an antialiased final image. 2x FSAA is also common on PCs and supported by the Xbox 360.

Use the CheckDeviceMultiSampleType method to query for antialiasing support on your game machine. Also, use the MultiSampleQuality and MultiSampleType properties of the PresentationParameters to select an antialiasing mode for your backbuffer. For more information, see How To: Enable Antialiasing (Multisampling). You can set PreferMultiSampling on the GraphicsDeviceManager to true to let the GraphicsDeviceManager choose the antialiasing mode.

You can use the RenderState property MultiSampleAntiAlias to enable or disable multisampling at run time.

 

 

 

 

( 2)How To: Enable Antialiasing (Multisampling)

 

 

Demonstrates how to use the GraphicsDeviceManager to enable antialiasing for your game. The CheckDeviceMultiSampleType method determines what kind of antialiasing your game computer supports.

Antialiasing is the process of smoothing the edges of triangles by averaging neighboring pixels in a render target. Because this process samples multiple pixels, it is also known as multisampling.

Note
Enabling antialiasing on the Xbox 360 will have a small performance impact. Enabling antialiasing on PCs may have a large impact on your game performance.

The Complete Sample

The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Using Antialiasing

To enable antialiasing in your game

To customize antialiasing in your game

  1. In your Game class constructor, add an event handler to the PreparingDeviceSettings event on your GraphicsDeviceManager.

    If PreferMultiSampling is true, a MultiSampleType will be chosen automatically before the event is raised. Whether or not a MultiSampleType is chosen when the event is raised, you can use the event handler to query and choose your own MultiSampleType.

    C# 
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        graphics.PreferMultiSampling = true;
        graphics.PreparingDeviceSettings +=
          new EventHandler<PreparingDeviceSettingsEventArgs>(
              graphics_PreparingDeviceSettings);
    }
  2. Create a method to use as an event handler for the PreparingDeviceSettings event.

    You will be using the PreparingDeviceSettingsEventArgs instance to access the PresentationParameters and set your antialiasing preferences.

    C# 
    void graphics_PreparingDeviceSettings(object sender, 
        PreparingDeviceSettingsEventArgs e)
    {
  3. If your game is running on Xbox 360, set directly the MultiSampleQuality and MultiSampleType properties, and then return from your event handler.

    The Xbox supports MultiSampleType.TwoSamples and MultiSampleType.FourSamples, each with a MultiSampleQuality of 0 or 1. MultiSampleType.FourSamples is 4x antialiasing; MultiSampleType.TwoSamples is 2x antialiasing.

    C# 
                // Xbox 360 and most PCs support FourSamples/0 
                // (4x) and TwoSamples/0 (2x) antialiasing.
                PresentationParameters pp = 
                    e.GraphicsDeviceInformation.PresentationParameters;
    #if XBOX
                pp.MultiSampleQuality = 0;
                pp.MultiSampleType = MultiSampleType.FourSamples;
                return;
    #else
  4. If your game is running on a PC, use CheckDeviceMultiSampleType to query for antialiasing support.

    Many PCs support 4x antialiasing, and most will support 2x antialiasing. Once you have determined that antialiasing is supported, set the MultiSampleType and MultiSampleQuality properties appropriately, and then return from the event handler.

    C# 
                int quality = 0;
                GraphicsAdapter adapter = e.GraphicsDeviceInformation.Adapter;
                SurfaceFormat format = adapter.CurrentDisplayMode.Format;
                // Check for 4xAA
                if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format,
                    false, MultiSampleType.FourSamples, out quality))
                {
                    // even if a greater quality is returned, we only want quality 0
                    pp.MultiSampleQuality = 0;
                    pp.MultiSampleType =
                        MultiSampleType.FourSamples;
                }
                // Check for 2xAA
                else if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, 
                    format, false, MultiSampleType.TwoSamples, out quality))
                {
                    // even if a greater quality is returned, we only want quality 0
                    pp.MultiSampleQuality = 0;
                    pp.MultiSampleType =
                        MultiSampleType.TwoSamples;
                }
                return;
    #endif
            }

    (3)
    Gets or sets the multisample type.

    Namespace: Microsoft.Xna.Framework.Graphics
    Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll

    C# 
    public MultiSampleType MultiSampleType { get; set; }
    Property Value
    The multisample type. Must be MultiSampleType.None unless SwapEffect has been set to SwapEffect.Discard. Multisampling is supported only if SwapEffect is SwapEffect.Discard.
    (4)
    Gets or sets a value indicating the multisample quality level.

    Namespace: Microsoft.Xna.Framework.Graphics
    Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

    C# 
    public int MultiSampleQuality { get; set; }
    Property Value
    The multisample quality level. The valid range is from 0 to one less than the value returned by CheckDeviceMultiSampleType.

    Paired values of render targets or of depth-stencil surfaces and MultiSampleType must match.

     

    其它相关内容不做赘述 ,附上范例的源代码 此代码为Game1.cs中的代码:


     

     

     

    不过我比较笨 ,最后用了一个超easy的方法,就是直接把图片做成羽化后的图片就可以了 O(∩_∩)O~

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

隔了5个月后我再来看这个资料,补充一条。

当做测试时,如果出现找不到合适的图形卡,请检查设置参数是否正确,请检查shader版本等这些提示问题,应该是高估显卡的性能了。

一般显卡支持4X抗锯齿,质量从0-10不等,但一般选0就好了,就最好了。

 

然后模型上的锯齿还是很明显,目前还没有搞出来,等我搞出来再补上说明。

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

好 隔了半个月,我搞清楚了为什么我的3D模型那么大的锯齿了,原因在于画法不一样,我一直是使用3D模型先画到一个贴图上,再画的贴图,也许3D和2D的抗锯齿效果是有些差别的,即使我全开了4X还是有很明显的锯齿,但是当我直接画3D模型的时候,啧啧,锯齿就小了很多,所有感悟:程序员要敢于打破自己的原有思路,才能有进步。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

好了又隔了一段时间,我又发现rendertarget实际上是有个抗锯齿重载的,汗颜。。。另外如果加了shader的话,要注意shader里面的内容是否有抗锯齿,或者加了锯齿的语句。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值