RenderTargets หลายรายการใน DrawableComponents XNA ที่แตกต่างกัน

สวัสดีเพื่อนๆ ฉันมีปัญหานิดหน่อย

ฉันมี 2 DrawableGameComponets นี้ (bigApple, SmallApple) ทั้งคู่ ฉันกำลังวาดเป็น RenderTarget แล้ววาด RenderTarget ใน backbuffer แต่สิ่งนี้เกิดขึ้นอย่างอิสระใน DrawableGameComponent แต่ละตัว

สิ่งที่ฉันต้องการบรรลุคือทั้ง DrawableGameComponents วาดอย่างถูกต้องจากกัน

บางอย่างเช่นนี้:
นี่คือหน้าจอที่มีทั้ง DrawableComponent โดยไม่มีเป้าหมายการแสดงผลในแต่ละองค์ประกอบ ป้อนคำอธิบายรูปภาพที่นี่

แต่แทนที่จะเป็นเช่นนั้น ฉันกลับได้รับสิ่งนี้:
นี่คือหน้าจอที่มีทั้ง DrawableComponent พร้อม rendertargets ในแต่ละองค์ประกอบ ป้อนคำอธิบายรูปภาพที่นี่

นี่เป็นเกมเล็กๆ น้อยๆ ที่ฉันกำลังทำอยู่ ฉันวางแผนที่จะแสดงในองค์ประกอบที่วาดได้และรูปภาพจากกล้อง และในองค์ประกอบเกมที่วาดได้อีกอันหนึ่งของตัวเกมเอง แต่เมื่อฉันเพิ่มอีก GameComponent ในรายการส่วนประกอบ จะไม่สามารถมองเห็นอันที่อยู่เหนือการเพิ่มครั้งล่าสุดได้

นี่คือรหัสจากแต่ละองค์ประกอบที่สามารถวาดได้

สมอลแอปเปิ้ล:

public class SmallApple:DrawableComponent2D
{
    Texture2D apple;

    public SmallApple(Game game)
        : base(game)
    {
        //Do nothing
    }

    protected override void LoadContent()
    {
        apple = Game.Content.Load<Texture2D>("apple");

        this.Size = new Vector2(apple.Width,
            apple.Height);

        renderTarget = new RenderTarget2D(GraphicsDevice,
            (int)Size.X,
            (int)Size.Y,
            false,
            SurfaceFormat.Color,
            DepthFormat.None,
            this.Game.GraphicsDevice.PresentationParameters.MultiSampleCount,
            RenderTargetUsage.PreserveContents);
        base.LoadContent();
    }

    public override void Initialize()
    {
        base.Initialize();
    }

    public override void Draw(GameTime gameTime)
    {
        GraphicsDevice.SetRenderTarget(renderTarget);
        GraphicsDevice.Clear(ClearOptions.Target, Color.Transparent, 1f, 0);

        this.SharedSpriteBatch.Begin(SpriteSortMode.Immediate, null);
        this.SharedSpriteBatch.Draw(this.apple, this.Position, Color.White);
        this.SharedSpriteBatch.End();

        GraphicsDevice.SetRenderTarget(null);

        this.SharedSpriteBatch.Begin();
        this.SharedSpriteBatch.Draw(apple, this.Position,Color.White);
        this.SharedSpriteBatch.End();
        base.Draw(gameTime);
    }

}

-- และคลาส BigApple

public class BigApple:DrawableComponent2D
{

    Texture2D apple;

    public BigApple(Game game)
        : base(game)
    {

    }


    protected override void LoadContent()
    {
        base.LoadContent();

        apple = Game.Content.Load<Texture2D>("apple");

        this.Size = new Vector2(apple.Width, apple.Height);

        renderTarget = new RenderTarget2D(GraphicsDevice, 
            (int)Size.X,
            (int)Size.Y, 
            false, 
            SurfaceFormat.Color, 
            DepthFormat.None,
            this.Game.GraphicsDevice.PresentationParameters.MultiSampleCount,
            RenderTargetUsage.PreserveContents);

    }

    public override void Update(GameTime gameTime)
    {
        base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
        GraphicsDevice.SetRenderTarget(renderTarget);
        GraphicsDevice.Clear(ClearOptions.Target, Color.Transparent, 1f, 0);

        this.SharedSpriteBatch.Begin(SpriteSortMode.Immediate,null);
        this.SharedSpriteBatch.Draw(this.apple, this.Position, Color.White);
        this.SharedSpriteBatch.End();

        GraphicsDevice.SetRenderTarget(null);

        this.SharedSpriteBatch.Begin();
        this.SharedSpriteBatch.Draw(renderTarget,new Rectangle((int)Position.X, (int)Position.Y, (int)GraphicsDevice.Viewport.Width, (int)GraphicsDevice.Viewport.Height), Color.White);
        this.SharedSpriteBatch.End();

        base.Draw(gameTime);
    }
}

คลาส DrawableComponent2D เป็นคลาสที่บรรจุมรดกจาก DrawablegameComponent และมีตัวแปรบางตัวให้ใช้งาน


person Alan Revolver    schedule 11.12.2013    source แหล่งที่มา


คำตอบ (2)


XNA เวอร์ชันของคุณคืออะไร
หากคุณใช้ XNA 3.1 ปัญหาของคุณอาจอยู่ที่นี่ GraphicsDeviceCapabilities.MaxSimultaneousRenderTargets คุณสมบัติ

person pinckerman    schedule 11.12.2013
comment
ฉันใช้ 4.0 อยู่ในขณะนี้ - person Alan Revolver; 12.12.2013
comment
แก้ได้!!. ฉันต้องเพิ่มวิธีการให้กับผู้รับมอบสิทธิ์: Graphics.PreparingDeviceSettings; ดังนั้นนี่คือวิธีการ: โมฆะส่วนตัว GraphicsDevicePreparingDeviceSettings (ผู้ส่งวัตถุ, PreparingDeviceSettingsEventArgs e) { e.GraphicsDeviceInformation.PresentationParameters.RenderTargetUsage = RenderTargetUsage.PreserveContents; } ____ และการเพิ่มลงใน graphicsDeviceManager เป็นเพียงบรรทัดเดียว กราฟิก กำลังเตรียมการตั้งค่าอุปกรณ์ += กราฟิกอุปกรณ์กำลังเตรียมการตั้งค่าอุปกรณ์; เอาล่ะ!!! ขอบคุณสำหรับการสนับสนุนของคุณ! - person Alan Revolver; 12.12.2013

แก้ได้!!. ฉันต้องเพิ่มวิธีการให้กับผู้รับมอบสิทธิ์:

กราฟิก การเตรียมการตั้งค่าอุปกรณ์;

นี่คือวิธีการ:

โมฆะส่วนตัว GraphicsDevicePreparingDeviceSettings (ผู้ส่งวัตถุ, PreparingDeviceSettingsEventArgs e) {

e.GraphicsDeviceInformation.PresentationParameters.RenderTargetUsage = RenderTargetUsage.PreserveContents;

}


และหากต้องการเพิ่มลงในกราฟิกDeviceManager เป็นเพียงบรรทัดเดียว:

กราฟิก กำลังเตรียมการตั้งค่าอุปกรณ์ += กราฟิกอุปกรณ์กำลังเตรียมการตั้งค่าอุปกรณ์;

เอาล่ะ!!! ขอบคุณสำหรับการสนับสนุน

person Alan Revolver    schedule 12.12.2013