C# 如何在XNA中存储模型每个实例的矩阵状态?

C# 如何在XNA中存储模型每个实例的矩阵状态?,c#,xna,C#,Xna,我的下一个代码部分工作,绘制模型, 我正在创建它的几个实例,并将模型的每个实例渲染为不同的rendertarget。 我的问题是如何存储每个实例的矩阵?因为当我想旋转一个矩阵时,它似乎是在引用矩阵,旋转时它会引用一个更新的矩阵。 谢谢 问候 using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; using Micro

我的下一个代码部分工作,绘制模型, 我正在创建它的几个实例,并将模型的每个实例渲染为不同的rendertarget。 我的问题是如何存储每个实例的矩阵?因为当我想旋转一个矩阵时,它似乎是在引用矩阵,旋转时它会引用一个更新的矩阵。
谢谢 问候

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using gamacherclone.Sources.Model;

namespace gamacherclone
{
    public class Case360 : DrawableComponent 
    {
        private Model _model ;//= new Model();
        Matrix[] _boneTransforms,world;
        public int coverflowposition = 0;
        public bool rotate = false;
        Matrix view,  proj,worldtransform,tempbones;
        public Matrix[] boneTransforms;
        public Matrix[] originalBoneTransforms;
        Matrix[] worldTransforms;
        float i = 0;

        public DashBoard dash;
        Cover cover;


        public Case360(Game game, Cover _cover)
            : base(game)
        {
            this.dash = (DashBoard)game;
            this.cover = _cover;
        }

        protected override void LoadContent(){
            this._model = this.dash.Content.Load<Model>(@"Models\case360");
            this.initialize();  
         }
        public void initialize() {

            this._boneTransforms = new Matrix[this._model.Bones.Count];
            this.originalBoneTransforms = new Matrix[this._model.Bones.Count];
            this.worldTransforms = new Matrix[this._model.Bones.Count];
            this._model.CopyAbsoluteBoneTransformsTo(_boneTransforms);
            this._model.CopyAbsoluteBoneTransformsTo(originalBoneTransforms);
        }



        public override void Update(GameTime gameTime)
        {

            // Calculate the new position of the forks.
            float time = (float)gameTime.TotalGameTime.TotalSeconds;

           if (this.rotate) {
                worldtransform = worldTransforms[0];
                UpdateWorldTransforms(time);
            }


            base.Update(gameTime);
        }

        public void UpdateWorldTransforms(float time)
        {

            worldTransforms[0] = _boneTransforms[0] * Matrix.CreateRotationY(time * 2f);

        }


        public override void Draw(GameTime gameTime)
        {
            this._model.CopyAbsoluteBoneTransformsTo(_boneTransforms);
             base.GraphicsDevice.SetRenderTarget(cover.caseRender);
             base.GraphicsDevice.Clear(Color.Transparent);
             base.GraphicsDevice.BlendState = BlendState.AlphaBlend;
             //GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
             base.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

             view = Matrix.CreateLookAt(new Vector3(1, 10, 10), new Vector3(0, 7, -10), Vector3.Up);
             proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f),
                                        GraphicsDevice.Viewport.Width / GraphicsDevice.Viewport.Height,
                                        1f, 1000.0f);


            // Draw the model.

            foreach (ModelMesh mesh in this._model.Meshes)
            {
               foreach (BasicEffect mesheffect in mesh.Effects)
                {
                    mesheffect.Parameters["Texture"].SetValue(this.cover.coverImage);
                    mesheffect.EnableDefaultLighting();

                    mesheffect.View = view;
                    mesheffect.Projection = proj;
                    mesheffect.World = worldtransform;

                }

                mesh.Draw();
            }

            base.GraphicsDevice.SetRenderTarget(null);
            base.GraphicsDevice.SetRenderTarget(dash.renderTarget2D);
            base.Draw(gameTime);
        }




    }
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.Graphics;
使用gamacherclone.Sources.Model;
名称空间gamacherclone
{
公共类案例360:可绘图组件
{
私有模型_Model;//=新模型();
矩阵[]_boneTransforms,世界;
公共int coverflowposition=0;
公共布尔旋转=假;
矩阵视图、项目、worldtransform、tempbones;
公共矩阵[]BonetTransforms;
公共矩阵[]原始骨转换;
矩阵变换;
浮点数i=0;
公共仪表板;
覆盖;
公案360(游戏、封面_封面)
:基地(游戏)
{
this.dash=(DashBoard)游戏;
this.cover=\u cover;
}
受保护的覆盖void LoadContent(){
this.\u model=this.dash.Content.Load(@“Models\case360”);
这是初始化();
}
公共无效初始化(){
this._boneTransforms=新矩阵[this._model.Bones.Count];
this.originalBoneTransforms=新矩阵[this.\u model.Bones.Count];
this.worldTransforms=新矩阵[this.u model.Bones.Count];
此._model.copyAbsoluteBonetTransformsTo(_BonetTransforms);
此._model.copyAbsoluteBonetTransformsTo(原始BonetTransforms);
}
公共覆盖无效更新(游戏时间游戏时间)
{
//计算叉的新位置。
浮动时间=(浮动)gameTime.TotalGameTime.TotalSeconds;
如果(这个旋转){
worldtransform=worldTransforms[0];
更新RLD传输(时间);
}
更新(游戏时间);
}
public void UpdateWorldTransforms(浮动时间)
{
worldTransforms[0]=\u BonetTransforms[0]*Matrix.CreateRotationY(时间*2f);
}
公共覆盖无效绘制(游戏时间游戏时间)
{
此._model.copyAbsoluteBonetTransformsTo(_BonetTransforms);
base.GraphicsDevice.SetRenderTarget(cover.caseRender);
基础。图形设备。清晰(颜色。透明);
base.GraphicsDevice.BlendState=BlendState.AlphaBlend;
//GraphicsDevice.RasterizerState=逆时针方向的RasterizerState.Cull;
base.GraphicsDevice.DepthStencilState=DepthStencilState.Default;
视图=矩阵.CreateLookAt(新向量3(1,10,10),新向量3(0,7,-10),向量3.Up);
proj=矩阵.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f),
GraphicsDevice.Viewport.Width/GraphicsDevice.Viewport.Height,
1f,1000.0f);
//画模型。
foreach(此中为ModelMesh网格。\u model.mesh)
{
foreach(网格中的基本效果网格效果。效果)
{
mesheffect.Parameters[“Texture”].SetValue(this.cover.coverImage);
mesheffect.EnableDefaultLighting();
视图=视图;
网格效应。投影=项目;
世界=世界变换;
}
mesh.Draw();
}
base.GraphicsDevice.SetRenderTarget(null);
base.GraphicsDevice.SetRenderTarget(dash.renderTarget2D);
基础。抽签(游戏时间);
}
}
}

据我所知,您正在使用变量
worldtransform
进行下一行

mesheffect.World = worldtransform;
并在更新时设置该值:
worldtransform=worldTransforms[0]

您遇到的问题是,它是一个结构,将通过值(而不是引用)传递。因此,当您将数组矩阵
worldTransforms[0]
分配给变量
worldtransform
时,您正在创建一个全新的实例

您可以通过执行以下操作来解决此问题:

UpdateWorldTransforms(time);
worldtransform = worldTransforms[0];
或者通过引用传递矩阵并直接修改变量

public void UpdateWorldTransforms(float time, ref Matrix worldMatrix)
{
    worldMatrix = _boneTransforms[0] * Matrix.CreateRotationY(time * 2f);
}
更新

对于存储多个实例,您可以使用如下模型:

public class WorldObject
{
    public Matrix World { get; set; }
    public Model GameModel { get; set; }
}
并在该类中存储其他信息。它可以处理更新/绘图信息,并对世界变换执行操作

查看“XNA实例”,但其中一些可能超出您的需要