2013-05-14 11 views
1

我希望使用XNA-4.0從3D模型的相機視圖動態創建圖像(作爲Texture2D)。我可以設置一個模型和相機來獲得我想要的視圖,但是因爲我需要在每一幀中同時爲許多大型和複雜模型執行此操作,爲獲得最佳性能,我認爲如果我可以以某種方式代替它,計算量會大大減少將該視圖繪製或寫入一次位圖,然後重複將其投影到屏幕上。如何使用XNA從模型生成位圖?

回答

0

使用渲染目標放在視內容到紋理。創建它,在繪製之前綁定它,然後解除綁定並調用GetTexture。它們非常快,可以同時使用(MRT - 多個渲染目標,afaik)。例如,我使用四個渲染目標將我的數據提供給延遲渲染管線。

+0

我認爲渲染目標是我需要使用的。你不會碰巧有一個簡單的例子,你呢? – user2340426 2013-05-14 20:39:41

0

這種技術被稱爲冒名頂替者。

這裏是DirectX 9的鏈接,不應該太難適應XNA。

Gamasutra Article

+0

這是一個偉大的文章,它涵蓋正是我試圖完成,但除了是有關DirectX,實際上它並不提供任何代碼示例(代碼的所有鏈接都被打破)。不錯的概述。 – user2340426 2013-05-14 20:37:59

0

對於其他人尋找一個簡單的例子: -

int _newTextureWidth = 800; 
int _newTextureHeight = 450; 

Texture2D _newTexture = new Texture2D(GraphicsDevice, _newTextureWidth, _newTextureHeight); 
RenderTarget2D _target = new RenderTarget2D(GraphicsDevice, _newTextureWidth, _newTextureHeight); 

// Point the GraphicsDevice to your new RenderTarget so that all Draw calls output here. 
GraphicsDevice.SetRenderTarget(_target); 
GraphicsDevice.Clear(Color.Transparent); 

// Do DrawModel and any other Draw calls here 

// Reset the GraphicsDevice to ouput to the Screen again; 
GraphicsDevice.SetRenderTarget(null); 

//Finally simply cast to your new Texture2D 
_newTexture = (Texture2D)_target;