我们在做一个3d显示的时候为了突出模型的某些部位以及更好的区别某些模块我们需要渲染各种不同的颜色来体现,
下面代码演示:
public void loade() { spriteBatch = new SpriteBatch(GraphicsDevice); myModel = Content.Load("Models\\" + mainform.filename); //素材管道载入3D模型//download the 3d model TextureCoordinate = Content.Load ("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("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); }
好了下面我就可以为是矩形的模块显示为蓝色了
效果如下: