2012-11-28 27 views
0

这次我有这个由三角形组成的立方体。我的代码将纹理赋予每个三角形。如何更改代码,使得只有一面墙会留下纹理,其他则会被涂成黄色?一面有纹理其他彩色黄色XNA C#

public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     private float angle = .9f; 
     private float SecondAngle = -1f; 

     //MyVertexFormat struct to go here 

     struct MyVertexFormat 
     { 
      private Vector3 position; 
      private Vector2 texCoord; 
      public MyVertexFormat(Vector3 position, Vector2 texCoord) 
      { 
       this.position = position; 
       this.texCoord = texCoord; 
      } 

      //Add VertexDeclaration here 

     public readonly static VertexDeclaration VertexDeclaration = new 
    VertexDeclaration(
    new VertexElement(0, VertexElementFormat.Vector3, 
    VertexElementUsage.Position, 0), 
    new VertexElement(sizeof(float) * 3, 
    VertexElementFormat.Vector2, 
    VertexElementUsage.TextureCoordinate, 0)); 
     } 

     GraphicsDeviceManager graphics; 
     GraphicsDevice device; 

     Effect effect; 
     Matrix viewMatrix; 
     Matrix projectionMatrix; 
     VertexBuffer vertexBuffer; 
     Vector3 cameraPos; 
     Texture2D wallTexture; 





     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 
     } 

     protected override void Initialize() 
     { 
      graphics.PreferredBackBufferWidth = 500; 
      graphics.PreferredBackBufferHeight = 500; 
      graphics.IsFullScreen = false; 
      graphics.ApplyChanges(); 
      Window.Title = "HLSL Start"; 

      base.Initialize(); 
     } 

     protected override void LoadContent() 
     { 
      device = GraphicsDevice; 
      wallTexture = Content.Load<Texture2D>("wall"); 
      effect = Content.Load<Effect>("Effect1"); 
      SetUpVertices(); 
      SetUpCamera(); 
     } 



     private void SetUpVertices() 
     { 
      MyVertexFormat[] vertices = new MyVertexFormat[36]; 
      //Back 
      vertices[0] = new MyVertexFormat(new Vector3(-1, -1, -1), 
      new Vector2(1.0f, 0.0f)); 
      vertices[1] = new MyVertexFormat(new Vector3(1, 1, -1), 
      new Vector2(0.0f, 1.0f)); 
      vertices[2] = new MyVertexFormat(new Vector3(-1, 1, -1), 
      new Vector2(1.0f, 1.0f)); 


      vertices[3] = new MyVertexFormat(new Vector3(-1, -1, -1), 
      new Vector2(1.0f, 0.0f)); 
      vertices[4] = new MyVertexFormat(new Vector3(1, -1, -1), 
      new Vector2(0.0f, 0.0f)); 
      vertices[5] = new MyVertexFormat(new Vector3(1, 1, -1), 
      new Vector2(0.0f, 1.0f)); 

      //Bottom 
      vertices[6] = new MyVertexFormat(new Vector3(1, -1, 1), 
      new Vector2(1.0f, 1.0f)); 
      vertices[7] = new MyVertexFormat(new Vector3(1, -1, -1), 
      new Vector2(1.0f, 0.0f)); 
      vertices[8] = new MyVertexFormat(new Vector3(-1, -1, -1), 
      new Vector2(0.0f, 0.0f)); 


      vertices[9] = new MyVertexFormat(new Vector3(-1, -1, -1), 
      new Vector2(0.0f, 0.0f)); 
      vertices[10] = new MyVertexFormat(new Vector3(-1, -1, 1), 
      new Vector2(0.0f, 1.0f)); 
      vertices[11] = new MyVertexFormat(new Vector3(1, -1, 1), 
      new Vector2(1.0f, 1.0f)); 

      //Top 
      vertices[12] = new MyVertexFormat(new Vector3(-1, 1, 1), 
      new Vector2(0.0f, 1.0f)); 
      vertices[13] = new MyVertexFormat(new Vector3(-1, 1, -1), 
      new Vector2(1.0f, 1.0f)); 
      vertices[14] = new MyVertexFormat(new Vector3(1, 1, 1), 
      new Vector2(0.0f, 0.0f)); 


      vertices[15] = new MyVertexFormat(new Vector3(1, 1, 1), 
      new Vector2(0.0f, 0.0f)); 
      vertices[16] = new MyVertexFormat(new Vector3(-1, 1, -1), 
      new Vector2(1.0f, 1.0f)); 
      vertices[17] = new MyVertexFormat(new Vector3(1, 1, -1), 
      new Vector2(1.0f, 0.0f)); 

      //Right side 
      vertices[18] = new MyVertexFormat(new Vector3(1, 1, 1), 
      new Vector2(1.0f, 1.0f)); 
      vertices[19] = new MyVertexFormat(new Vector3(1, -1, -1), 
      new Vector2(0.0f, 0.0f)); 
      vertices[20] = new MyVertexFormat(new Vector3(1, -1, 1), 
      new Vector2(1.0f, 0.0f)); 


      vertices[21] = new MyVertexFormat(new Vector3(1, -1, -1), 
      new Vector2(0.0f, 0.0f)); 
      vertices[22] = new MyVertexFormat(new Vector3(1, 1, 1), 
      new Vector2(1.0f, 1.0f)); 
      vertices[23] = new MyVertexFormat(new Vector3(1, 1, -1), 
      new Vector2(0.0f, 1.0f)); 

      //Left side 
      vertices[24] = new MyVertexFormat(new Vector3(-1, -1, 1), 
      new Vector2(0.0f, 0.0f)); 
      vertices[25] = new MyVertexFormat(new Vector3(-1, -1, -1), 
      new Vector2(1.0f, 0.0f)); 
      vertices[26] = new MyVertexFormat(new Vector3(-1, 1, 1), 
      new Vector2(0.0f, 1.0f)); 

      vertices[29] = new MyVertexFormat(new Vector3(-1, 1, 1), 
      new Vector2(0.0f, 1.0f)); 
      vertices[28] = new MyVertexFormat(new Vector3(-1, 1, -1), 
      new Vector2(1.0f, 1.0f)); 
      vertices[27] = new MyVertexFormat(new Vector3(-1, -1, -1), 
      new Vector2(1.0f, 0.0f)); 

      //Front 
      vertices[30] = new MyVertexFormat(new Vector3(-1, 1, 1), 
      new Vector2(1.0f, 1.0f)); 
      vertices[31] = new MyVertexFormat(new Vector3(1, 1, 1), 
      new Vector2(0.0f, 1.0f)); 
      vertices[32] = new MyVertexFormat(new Vector3(-1, -1, 1), 
      new Vector2(1.0f, 0.0f)); 


      vertices[33] = new MyVertexFormat(new Vector3(1, -1, 1), 
      new Vector2(0.0f, 0.0f)); 
      vertices[34] = new MyVertexFormat(new Vector3(-1, -1, 1), 
      new Vector2(1.0f, 0.0f)); 
      vertices[35] = new MyVertexFormat(new Vector3(1, 1, 1), 
      new Vector2(0.0f, 1.0f)); 

      vertexBuffer = new VertexBuffer(device, 
       MyVertexFormat.VertexDeclaration, vertices.Length, 
      BufferUsage.WriteOnly); 
      vertexBuffer.SetData(vertices); 




     } 

     private void SetUpCamera() 
     { 
      cameraPos = new Vector3(0, 5, 6); 
      viewMatrix = Matrix.CreateLookAt(cameraPos, new Vector3(0, 0, 1), new Vector3(0, 1, 0)); 
      projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 200.0f); 
     } 

     protected override void UnloadContent() 
     { 
     } 

     protected override void Update(GameTime gameTime) 
     { 
      if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
       this.Exit(); 




      viewMatrix = Matrix.CreateRotationY(MathHelper.ToRadians(angle)) * 
         Matrix.CreateRotationX(MathHelper.ToRadians(SecondAngle)) * 
         viewMatrix; 

      base.Update(gameTime); 
     } 

     protected override void Draw(GameTime gameTime) 
     { 
      device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DarkSlateBlue, 1.0f, 0); 

      effect.CurrentTechnique = effect.Techniques["Shaded"]; 
      effect.Parameters["View"].SetValue(viewMatrix); 
      effect.Parameters["Projection"].SetValue(projectionMatrix); 
      effect.Parameters["World"].SetValue(Matrix.Identity); 
      effect.Parameters["myTexture"].SetValue(wallTexture); 

      foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
      { 
       pass.Apply(); 

       device.SetVertexBuffer(vertexBuffer); 
       device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12); 
      } 

      base.Draw(gameTime); 
     } 
    } 
} 

效应文件:

float4x4 World; 
float4x4 View; 
float4x4 Projection; 
Texture myTexture; 

sampler TextureSampler = sampler_state { 
texture = <myTexture>; 
MinFilter = Anisotropic; // Minification Filter 
MagFilter = Anisotropic; // Magnification Filter 
MipFilter = Linear; // Mip-mapping 
AddressU = Wrap; // Address Mode for U Coordinates 
AddressV = Wrap; // Address Mode for V Coordinates 
}; 


struct VertexShaderInput 
{ 
float4 Position : POSITION0; 
float2 UV: TEXCOORD0; 
}; 

struct VertexShaderOutput 
{ 
float4 Position : POSITION0; 
float2 UV: TEXCOORD0; 

}; 

VertexShaderOutput VertexShaderFunction(VertexShaderInput input) 
{ 
VertexShaderOutput output; 
float4 worldPosition = mul(input.Position, World); 
float4 viewPosition = mul(worldPosition, View); 
output.Position = mul(viewPosition, Projection); 
output.UV = input.UV; 

return output; 
} 

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0 
{ 
float3 output = float3(1, 1, 1); 
output *= tex2D(TextureSampler, input.UV); 
return float4(output, 1); 
} 

technique Shaded 
{ 
pass Pass1 
{ 
VertexShader = compile vs_2_0 VertexShaderFunction(); 
PixelShader = compile ps_2_0 PixelShaderFunction(); 
} 
} 

任何想法,任何人吗?

回答

0

将黄色添加到纹理的一个小区域,然后设置要作为黄色的三角形的(U,V)纹理坐标以映射该区域。

[编辑]

添加一个颜色通道的顶点格式结构:

struct MyVertexFormat 
    { 
     private Vector3 position; 
     private Vector2 texCoord; 
     private Color color; 
     public MyVertexFormat(Vector3 position, Vector2 texCoord, Color color) 
     { 
      this.position = position; 
      this.texCoord = texCoord; 
      this.color = color; 
     } 

     //Add VertexDeclaration here 

    public readonly static VertexDeclaration VertexDeclaration = 
     new VertexDeclaration(
      new VertexElement(0, VertexElementFormat.Vector3, 
           VertexElementUsage.Position, 0), 
      new VertexElement(sizeof(float) * 3, 
       VertexElementFormat.Vector2, 
       VertexElementUsage.TextureCoordinate, 0)), 
      new VertexElement(sizeof(float) * 7, 
       VertexElementFormat.Color, 
       VertexElementUsage.Color, 0)); 

      } 

影响

struct VertexShaderInput 
{ 
    float4 Position : POSITION0; 
    float2 UV: TEXCOORD0; 
    float4 Color: Color0; 
}; 

作为说明,我认为从IVertexType继承顶点结构会更好。

+0

这将是作弊我需要颜色解决方案。 – Dave

+0

我已经试过这个,但是当我尝试运行时它会抛出异常。我确实有一些新的想法可以用于绘制循环,我会在我有时间尝试时发布 – Dave