2011-01-12 26 views
5

Tôi đã đập đầu vào vấn đề này hàng giờ liền. Những gì tôi muốn làm là vẽ một khối lập phương, với các họa tiết khác nhau ở mỗi bên; hoặc cụ thể hơn, tôi muốn có thể chỉ định bất kỳ kết cấu nào tôi muốn cho mỗi bên. Tôi đã sử dụng ví dụ here để bắt đầu và sau đó cố gắng phát triển thêm nữa, vì vậy tôi có thể có nhiều kết cấu. Tuy nhiên, bất kể tôi làm gì, nó vẫn chỉ sử dụng kết cấu cuối cùng được áp dụng cho hiệu ứng và không để ý đến bất kỳ bài tập trước nào. Đây là lớp hình dạng của tôi:Vẽ một hình khối kết cấu có nhiều mặt trong XNA 4.0

public class BasicShape { 

public Vector3 shapeSize; 
public Vector3 shapePosition; 
private VertexPositionNormalTexture[][] shapeVertices; 
private int shapeTriangles; 
private VertexBuffer shapeBuffer; 
public Texture2D topTexture; 
public Texture2D frontTexture; 
public Texture2D backTexture; 
public Texture2D leftTexture; 
public Texture2D rightTexture; 
public Texture2D bottomTexture; 

public BasicShape(Vector3 size, Vector3 position) { 
    shapeSize = size; 
    shapePosition = position; 
} 

private void BuildShape() { 
    shapeTriangles = 12; 

    shapeVertices = new VertexPositionNormalTexture[6][]; 
    for(int i = 0; i < 6; i++) { 
     shapeVertices[i] = new VertexPositionNormalTexture[6]; 
    } 

    Vector3 topLeftFront = shapePosition + 
    new Vector3(0.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomLeftFront = shapePosition + 
    new Vector3(0.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 topRightFront = shapePosition + 
    new Vector3(1.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomRightFront = shapePosition + 
    new Vector3(1.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 topLeftBack = shapePosition + 
    new Vector3(0.0f, 1.0f, 1.0f) * shapeSize; 
    Vector3 topRightBack = shapePosition + 
    new Vector3(1.0f, 1.0f, 1.0f) * shapeSize; 
    Vector3 bottomLeftBack = shapePosition + 
    new Vector3(0.0f, 0.0f, 1.0f) * shapeSize; 
    Vector3 bottomRightBack = shapePosition + 
    new Vector3(1.0f, 0.0f, 1.0f) * shapeSize; 

    Vector3 topLeftFront2 = shapePosition + 
    new Vector3(0.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomLeftFront2 = shapePosition + 
    new Vector3(0.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 topRightFront2 = shapePosition + 
    new Vector3(1.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomRightFront2 = shapePosition + 
    new Vector3(1.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 topLeftBack2 = shapePosition + 
    new Vector3(0.0f, 1.0f, 1.0f) * shapeSize; 
    Vector3 topRightBack2 = shapePosition + 
    new Vector3(1.0f, 1.0f, 1.0f) * shapeSize; 
    Vector3 bottomLeftBack2 = shapePosition + 
    new Vector3(0.0f, 0.0f, 1.0f) * shapeSize; 
    Vector3 bottomRightBack2 = shapePosition + 
    new Vector3(1.0f, 0.0f, 1.0f) * shapeSize; 

    Vector3 topLeftFront3 = shapePosition + 
    new Vector3(0.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomLeftFront3 = shapePosition + 
    new Vector3(0.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 topRightFront3 = shapePosition + 
    new Vector3(1.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomRightFront3 = shapePosition + 
    new Vector3(1.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 topLeftBack3 = shapePosition + 
    new Vector3(0.0f, 1.0f, 1.0f) * shapeSize; 
    Vector3 topRightBack3 = shapePosition + 
    new Vector3(1.0f, 1.0f, 1.0f) * shapeSize; 
    Vector3 bottomLeftBack3 = shapePosition + 
    new Vector3(0.0f, 0.0f, 1.0f) * shapeSize; 
    Vector3 bottomRightBack3 = shapePosition + 
    new Vector3(1.0f, 0.0f, 1.0f) * shapeSize; 

    Vector3 frontNormal = new Vector3(0.0f, 0.0f, 1.0f) * shapeSize; 
    Vector3 backNormal = new Vector3(0.0f, 0.0f, -1.0f) * shapeSize; 
    Vector3 topNormal = new Vector3(0.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomNormal = new Vector3(0.0f, -1.0f, 0.0f) * shapeSize; 
    Vector3 leftNormal = new Vector3(-1.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 rightNormal = new Vector3(1.0f, 0.0f, 0.0f) * shapeSize; 

    Vector2 textureTopLeft = new Vector2(1f * shapeSize.X, 0.0f * shapeSize.Y); 
    Vector2 textureTopRight = new Vector2(0.0f * shapeSize.X, 0.0f * shapeSize.Y); 
    Vector2 textureBottomLeft = new Vector2(1f * shapeSize.X, 1f * shapeSize.Y); 
    Vector2 textureBottomRight = new Vector2(0.0f * shapeSize.X, 1f * shapeSize.Y); 

    // Front face. 
    shapeVertices[0][0] = new VertexPositionNormalTexture(
    topLeftFront, frontNormal, textureTopLeft); 
    shapeVertices[0][1] = new VertexPositionNormalTexture(
    bottomLeftFront, frontNormal, textureBottomLeft); 
    shapeVertices[0][2] = new VertexPositionNormalTexture(
    topRightFront, frontNormal, textureTopRight); 
    shapeVertices[0][3] = new VertexPositionNormalTexture(
    bottomLeftFront, frontNormal, textureBottomLeft); 
    shapeVertices[0][4] = new VertexPositionNormalTexture(
    bottomRightFront, frontNormal, textureBottomRight); 
    shapeVertices[0][5] = new VertexPositionNormalTexture(
    topRightFront, frontNormal, textureTopRight); 

    // Back face. 
    shapeVertices[1][0] = new VertexPositionNormalTexture(
    topLeftBack, backNormal, textureTopRight); 
    shapeVertices[1][1] = new VertexPositionNormalTexture(
    topRightBack, backNormal, textureTopLeft); 
    shapeVertices[1][2] = new VertexPositionNormalTexture(
    bottomLeftBack, backNormal, textureBottomRight); 
    shapeVertices[1][3] = new VertexPositionNormalTexture(
    bottomLeftBack, backNormal, textureBottomRight); 
    shapeVertices[1][4] = new VertexPositionNormalTexture(
    topRightBack, backNormal, textureTopLeft); 
    shapeVertices[1][5] = new VertexPositionNormalTexture(
    bottomRightBack, backNormal, textureBottomLeft); 

    // Top face. 
    shapeVertices[2][0] = new VertexPositionNormalTexture(
    topLeftFront2, topNormal, textureBottomLeft); 
    shapeVertices[2][1] = new VertexPositionNormalTexture(
    topRightBack2, topNormal, textureTopRight); 
    shapeVertices[2][2] = new VertexPositionNormalTexture(
    topLeftBack2, topNormal, textureTopLeft); 
    shapeVertices[2][3] = new VertexPositionNormalTexture(
    topLeftFront2, topNormal, textureBottomLeft); 
    shapeVertices[2][4] = new VertexPositionNormalTexture(
    topRightFront2, topNormal, textureBottomRight); 
    shapeVertices[2][5] = new VertexPositionNormalTexture(
    topRightBack2, topNormal, textureTopRight); 

    // Bottom face. 
    shapeVertices[3][0] = new VertexPositionNormalTexture(
    bottomLeftFront2, bottomNormal, textureTopLeft); 
    shapeVertices[3][1] = new VertexPositionNormalTexture(
    bottomLeftBack2, bottomNormal, textureBottomLeft); 
    shapeVertices[3][2] = new VertexPositionNormalTexture(
    bottomRightBack2, bottomNormal, textureBottomRight); 
    shapeVertices[3][3] = new VertexPositionNormalTexture(
    bottomLeftFront2, bottomNormal, textureTopLeft); 
    shapeVertices[3][4] = new VertexPositionNormalTexture(
    bottomRightBack2, bottomNormal, textureBottomRight); 
    shapeVertices[3][5] = new VertexPositionNormalTexture(
    bottomRightFront2, bottomNormal, textureTopRight); 

    // Left face. 
    shapeVertices[4][0] = new VertexPositionNormalTexture(
    topLeftFront3, leftNormal, textureTopRight); 
    shapeVertices[4][1] = new VertexPositionNormalTexture(
    bottomLeftBack3, leftNormal, textureBottomLeft); 
    shapeVertices[4][2] = new VertexPositionNormalTexture(
    bottomLeftFront3, leftNormal, textureBottomRight); 
    shapeVertices[4][3] = new VertexPositionNormalTexture(
    topLeftBack3, leftNormal, textureTopLeft); 
    shapeVertices[4][4] = new VertexPositionNormalTexture(
    bottomLeftBack3, leftNormal, textureBottomLeft); 
    shapeVertices[4][5] = new VertexPositionNormalTexture(
    topLeftFront3, leftNormal, textureTopRight); 

    // Right face. 
    shapeVertices[5][0] = new VertexPositionNormalTexture(
    topRightFront3, rightNormal, textureTopLeft); 
    shapeVertices[5][1] = new VertexPositionNormalTexture(
    bottomRightFront3, rightNormal, textureBottomLeft); 
    shapeVertices[5][2] = new VertexPositionNormalTexture(
    bottomRightBack3, rightNormal, textureBottomRight); 
    shapeVertices[5][3] = new VertexPositionNormalTexture(
    topRightBack3, rightNormal, textureTopRight); 
    shapeVertices[5][4] = new VertexPositionNormalTexture(
    topRightFront3, rightNormal, textureTopLeft); 
    shapeVertices[5][5] = new VertexPositionNormalTexture(
    bottomRightBack3, rightNormal, textureBottomRight); 
} 

public void SetTopTexture(Texture2D tex) { 
    topTexture = tex; 
} 
public void SetSideTexture(Texture2D tex) { 
    frontTexture = tex; 
    backTexture = tex; 
    leftTexture = tex; 
    rightTexture = tex; 
} 
public void SetBottomTexture(Texture2D tex) { 
    bottomTexture = tex; 
} 

public void RenderShape(GraphicsDevice device, Effect effect) { 
    BuildShape(); 


    effect.Parameters["xTexture"].SetValue(topTexture); 
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[2], 0, 2); 
    effect.Parameters["xTexture"].SetValue(bottomTexture); 
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[3], 0, 2); 
    effect.Parameters["xTexture"].SetValue(frontTexture); 
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[0], 0, 2); 
    effect.Parameters["xTexture"].SetValue(backTexture); 
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[1], 0, 2); 
    effect.Parameters["xTexture"].SetValue(leftTexture); 
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[4], 0, 2); 
    effect.Parameters["xTexture"].SetValue(rightTexture); 
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[5], 0, 2); 

} 

}

Và trong phương thức Draw cho trò chơi của tôi:

cubeEffect.CurrentTechnique = cubeEffect.Techniques["Textured"]; 
    foreach(EffectPass pass in cubeEffect.CurrentTechnique.Passes) { 
     pass.Apply(); 
     BasicShape s = new BasicShape(new Vector3(1, 1, 1), new Vector3(0, 0, 3)); 
     s.SetTopTexture(TextureLoader.GetTexture(4)); 
     s.SetSideTexture(TextureLoader.GetTexture(35)); 
     s.SetBottomTexture(TextureLoader.GetTexture(4)); 
     s.RenderShape(GraphicsDevice, cubeEffect); 

    } 

Như bạn thấy, tôi đang tải kết cấu khác nhau, nhưng kết quả là đây :

my cube http://www.tinyimg.org/images/769MinecraftClassic_2011_.bmp

Tôi chắc chắn rằng kết cấu là khác nhau, nhưng cùng một kết cấu được vẽ trên tất cả các bên. Tôi có cần một hiệu ứng riêng biệt cho mỗi bên không? Điều đó chắc chắn có vẻ như quá mức cần thiết.

+0

Bạn đã xác minh rằng 'TextureLoader.GetTexture (35)' đang trở lại những gì bạn mong đợi nó quay trở lại? – ChrisF

+0

@ChrisF - Vâng, kết cấu 35 là kết cấu duy nhất hiển thị, vì vậy tôi cho rằng đó là. Ngoài ra, nếu tôi lấy hai hàng đầu tiên của mã vẽ trong RenderShape và đặt nó ở cuối, nó ám kết cấu 4. Vì vậy, nó chỉ là kết xuất được áp dụng cuối cùng. – Bevin

+0

Điều đó có vẻ như thông tin hữu ích mà bạn nên đưa vào câu hỏi của mình. Tôi không thể giúp đỡ với câu trả lời mặc dù - Tôi không quen với xna. – ChrisF

Trả lời

4

Bất kỳ thông số nào được đặt trên hiệu ứng sẽ không được áp dụng cho đến khi bạn gọi hàm EffectPass.Apply(). Điều này là do áp dụng các thay đổi cho một hiệu ứng là tốn kém và bạn có thể muốn thực hiện nhiều thay đổi trong một lần.

chức năng RenderShape của bạn sẽ giống như:

public void RenderShape(GraphicsDevice device, Effect effect) 
{ 
    BuildShape(); 

    foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
    { 
     effect.Parameters["xTexture"].SetValue(topTexture); 
     pass.Apply(); 
     device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[2], 0, 2); 

     effect.Parameters["xTexture"].SetValue(bottomTexture); 
     pass.Apply(); 
     device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[3], 0, 2); 

     effect.Parameters["xTexture"].SetValue(frontTexture); 
     pass.Apply(); 
     device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[0], 0, 2); 

     effect.Parameters["xTexture"].SetValue(backTexture); 
     pass.Apply(); 
     device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[1], 0, 2); 

     effect.Parameters["xTexture"].SetValue(leftTexture); 
     pass.Apply(); 
     device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[4], 0, 2); 

     effect.Parameters["xTexture"].SetValue(rightTexture); 
     pass.Apply(); 
     device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[5], 0, 2); 
    } 
} 
+0

@Empyrean - Vì vậy, bạn có nói rằng đây không phải là cách được khuyến nghị để làm điều đó? Điều gì sẽ là một cách nhanh hơn? Tôi sẽ phải vẽ rất nhiều hình khối, vì vậy tôi cần tối ưu hóa nó càng nhiều càng tốt. – Bevin

+0

Bạn thường chỉ có thể thực hiện vài trăm thay đổi trạng thái (được gọi là lô) trước khi bạn gặp sự cố về hiệu suất. Trong mã của bạn, kết xuất của mỗi khối bao gồm 6 lô. Điều đó có nghĩa là bạn có thể sẽ hiển thị ít hơn 100 hình khối trước khi tốc độ khung hình của bạn bắt đầu không thể chấp nhận được. Bạn muốn càng ít các cuộc gọi vẽ càng tốt để vẽ tất cả các mặt khối lập phương với cùng một kết cấu trong một cuộc gọi vẽ. Tốt hơn là kết hợp sáu kết cấu của bạn thành một kết cấu duy nhất (kết cấu bản đồ). Các vertex buffer cũng nhanh hơn nhiều so với các cuộc gọi DrawUserPrimitive. – Empyrean

+0

Cách nhanh nhất là sử dụng bản đồ kết cấu và hiển thị tất cả các hình khối trong một cuộc gọi bằng cách sử dụng bộ đệm đỉnh tĩnh hoặc động tùy thuộc vào việc hình khối của bạn có di chuyển hay không. Nếu một số người trong số họ không di chuyển, sử dụng một bộ đệm vertex tĩnh cho những người và một bộ đệm đỉnh động cho những người làm. Bạn cũng nên sử dụng các chỉ mục để tránh sử dụng các đỉnh trùng lặp. Các đỉnh trùng lặp phải được chuyển đổi và chúng có thể biến đổi một chút khác nhau khi tạo các đường nối trong mắt lưới của bạn. – Empyrean

Các vấn đề liên quan