忍者ブログ

Memeplexes

プログラミング、3DCGとその他いろいろについて

かんたんXNA その22 深度バイアス

このページは古いです
最新版はこちら

3DCGには"z-ファイティング"という現象があります。
カメラからの距離があまりにも同じような物体が2つあると、
深度バッファの計算が狂って(丸め誤差などが原因だそうです)
描画がおかしくなります。

zFighting2.jpgzFighting.jpg

これは、同じ平面上に2つの三角形を重ねてクルクルと回転させて表示したものです。
なんだか縦にギザギザしているのがわかると思います。
これが"z-ファイティング"です。

これが問題になるのは、物が隠れて出来る"影(シャドウ)"を描画するときなどです。
戦闘機が太陽光をさえぎって地面に影を落とすとすると、
地面を表すポリゴンと影を表すポリゴンが重なることになります。
すると、このような変なギザギザが発生してしまうことがあるのです。

この問題を解決する方法のひとつとして、深度バイアスがあります。
深度が同じならプラスαの情報を使って
どっちのポリゴンが前か決めればいいのです。
例えばカメラからの距離が同じ2つの三角形があるとしましょう。
このままだとzファイティングが発生するのですが、
ここで片方の深度バイアスを0.1fに設定してやります。(デフォルトでは0)
そうすると、0.1の方がより奥にあると認識されて0の後ろに隠れるのです。
z-ファイティングになりません。

この深度バイアスをセットするには
RenderState.DepthBiasプロパティを使います。

public float DepthBias { get; set; }

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


class Triangle
{
    VertexPositionColor[] vertices = new VertexPositionColor[3];

    public Triangle(Vector3 p1, Vector3 p2, Vector3 p3, Color color)
    {
        vertices[0] = new VertexPositionColor(p1, color);
        vertices[1] = new VertexPositionColor(p2, color);
        vertices[2] = new VertexPositionColor(p3, color);
    }

    public void Draw(GraphicsDevice graphicsDevice, BasicEffect effect)
    {
        graphicsDevice.VertexDeclaration = new VertexDeclaration(
            graphicsDevice,
            VertexPositionColor.VertexElements);
        effect.VertexColorEnabled = true;

        effect.Begin();

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Begin();

            graphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                PrimitiveType.TriangleList,
                vertices,
                0,
                1
                );

            pass.End();
        }

        effect.End();
    }
}

class MyGame : Game
{
    GraphicsDeviceManager graphics;
    BasicEffect basicEffect;

    Triangle blueTriangle = new Triangle(
        new Vector3(0,1, 0),
        new Vector3(1, -0.5f, 0),
        new Vector3(-1, -0.5f, 0),
        Color.Blue
        );
    Triangle redTriangle = new Triangle(
        new Vector3(1, 0.5f, 0),
        new Vector3(0, -1, 0),
        new Vector3(-1, 0.5f, 0),
        Color.Red
        );


    public MyGame()
    {
        graphics = new GraphicsDeviceManager(this);
    }

    protected override void LoadGraphicsContent(bool loadAllContent)
    {
        if (loadAllContent)
        {
            basicEffect = new BasicEffect(graphics.GraphicsDevice, null);
            basicEffect.View = Matrix.CreateLookAt(
                new Vector3(0, 0, 3),
                new Vector3(0, 0, 0),
                new Vector3(0, 1, 0)
                );
            basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45),
                Window.ClientBounds.Width / (float)Window.ClientBounds.Height,
                1, 100
                );
        }
    }

    protected override void Update(GameTime gameTime)
    {
        basicEffect.World *= Matrix.CreateRotationY(0.01f);
    }

    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
        graphics.GraphicsDevice.RenderState.CullMode = CullMode.None;

        blueTriangle.Draw(graphics.GraphicsDevice, basicEffect);
        graphics.GraphicsDevice.RenderState.DepthBias = 0.1f;
        redTriangle.Draw(graphics.GraphicsDevice, basicEffect);
        graphics.GraphicsDevice.RenderState.DepthBias = 0;
    }
}

depthBias.jpg
深度バイアスをセットして、青い三角形を常に赤い三角形の上に描画するようにしました。
赤い三角形に深度バイアスをセットしたのでより遠くにあると認識されたようです。
カメラからの距離は同じですが、z-ファイティングにはなりません!


拍手[0回]

PR