xna 渲染3d图片

  我们在做一个3d显示的时候为了突出模型的某些部位以及更好的区别某些模块我们需要渲染各种不同的颜色来体现,

  下面代码演示:

   

        public void loade()
        {
           
            spriteBatch = new SpriteBatch(GraphicsDevice);
            myModel = Content.Load<Model>("Models\\" + mainform.filename);         //素材管道载入3D模型//download the 3d model
            TextureCoordinate = Content.Load<Texture2D>("Models\\test");
            //aaa=new Texture2D(,100,100)

         
               
                aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height;

        }

  为了不发生异常我们还需在lodeconten加一句代码,就可以多次加载了

 

   

      protected override void LoadContent()
        {

            //spriteBatch = new SpriteBatch(GraphicsDevice);
            //myModel = Content.Load<Model>("Models\\"+mainform.filename);         //素材管道载入3D模型//download the 3d model
            //aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width /(float)graphics.GraphicsDevice.Viewport.Height;
            GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;

            loade();
            // TODO: use this.Content to load your game content here
        }

  

 protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            Matrix[] transforms = new Matrix[myModel.Bones.Count];
            myModel.CopyAbsoluteBoneTransformsTo(transforms);
     
            // 绘制模型 
            foreach (ModelMesh mesh in myModel.Meshes)   //遍历模型mesh// for() all the mesh
            {
                //BasicEffect类可以简单的通过设置属性,包含光照、纹理等等就可以在“五分钟”内实现对一个物体的呈现。
                string aaa = mesh.Name;
                if (aaa.ToString().IndexOf("矩形") > 0)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
               
                    effect.EnableDefaultLighting();
                     effect.TextureEnabled = true;//纹理加载
                     effect.Texture =TextureCoordinate;
                    //光照 //light
                    effect.SpecularColor = new Vector3(1, 1, 1);
                    effect.SpecularPower = 24;
                     effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(Program.modelRotationY) * Matrix.CreateRotationX(Program.modelRotationX) * Matrix.CreateTranslation(modelPosition);     //使用World矩阵来改变模型在世界坐标系中的位置//use the World Matrix change the world coordinate
                    effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60.0f), aspectRatio, 1.0f, 10000.0f);
                    //Color[] colors = new Color[3];

                    //for (int i = 0; i < 3; i++)
                    //{
                    //    colors.SetValue(new Color(0, 1, 0), i);
                    //}
                  
                  

                }
                    
                    mesh.Draw();
                }
            }


            //sprites.Begin();
            //sprites.Draw(backgroundTexture, Vector2.Zero, Color.White);
            //sprites.End();
            base.Draw(gameTime);
        }

  好了下面我就可以为是矩形的模块显示为蓝色了

    效果如下:

    

  

 

转载于:https://www.cnblogs.com/mlhelloworld/p/6992858.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值