說我有這樣的接口:XNA - 相當於SpriteBatch.Draw轉換的矩陣?
interface Drawable {
Vector2 DrawPosition { get; }
Texture2D Texture { get; }
float Rotation { get; }
Vector2 Origin { get; }
Vector2 Scale { get; }
bool FlipHorizontally { get; }
}
,並在擴展Microsoft.Xna.Framework.Game一類,我重寫抽獎(GameTime)和這個代碼是某處有:
Drawable d = ...;
spriteBatch.Begin();
spriteBatch.Draw(d.Texture, d.DrawPosition, new Rectangle(0, 0, d.Texture.Width, d.Texture.Height), Color.White, d.Rotation, d.Origin, d.Scale, d.FlipHorizontally ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0);
spriteBatch.End();
這使用SpriteBatch.Draw(Texture2D紋理,Vector2位置,Nullable sourceRectangle,Color顏色,浮動旋轉,Vector2原點,Vector2縮放,SpriteEffects效果,float layerDepth)重載。
假設我有一組頂點,它們構成了由d.Texture返回的圖像的粗略輪廓(也就是說,如果我在Microsoft Paint中打開圖像並從頂點集中的每個點上畫鉛筆,會非常吻合)。如果我想繪製這些點,以便它們使用GraphicsDevice.DrawUserPrimitives()檢查紋理,是否有方法僅使用矩陣變換頂點?關鍵是它只能使用矩陣,並且我沒有其他的繪圖選擇,因爲我實際上也需要爲其他事物使用轉換的頂點。我已經試過類似
Matrix.CreateTranslation(new Vector3(-d.Origin, 0))
* Matrix.CreateScale(new Vector3(d.Scale, 0))
* Matrix.CreateRotationZ(d.Rotation)
* Matrix.CreateTranslation(new Vector3(d.DrawPosition, 0)));
但它很難失敗。有沒有解決這個問題的方法?
「它失敗很難」並不足以描述發生的事情。 – 2012-03-01 07:45:14