C# XNA多重立方体
首先,请原谅我的英语,我是法国人。。。 我写了一个不起作用的代码。 我想画一个多像素块,像Minecraft,但在体素中。 在我的地图的一侧,结果是好的: 但另一方面,我明白了: 某些面通过其他面显示。。。 谁有主意?我的常态 我的函数Remake()未使用,它用于删除面C# XNA多重立方体,c#,xna,voxel,C#,Xna,Voxel,首先,请原谅我的英语,我是法国人。。。 我写了一个不起作用的代码。 我想画一个多像素块,像Minecraft,但在体素中。 在我的地图的一侧,结果是好的: 但另一方面,我明白了: 某些面通过其他面显示。。。 谁有主意?我的常态 我的函数Remake()未使用,它用于删除面 namespace LandOfCube.Models { class Block { VertexBuffer instanceBuffer; public enum
namespace LandOfCube.Models
{
class Block
{
VertexBuffer instanceBuffer;
public enum Form
{
all,
noUp,
noDown,
noRight,
noLeft,
noFront,
noBack
}
public Form cubeForm = Form.all;
GraphicsDevice Graphics;
Effect shader;
private VertexBuffer vb;
Color CubeColor;
public Vector3 pos;
VertexPositionColorNormal[] vertices;
VertexPositionColorNormal[] verticesNoUp;
VertexPositionColorNormal[] verticesNoDown;
VertexPositionColorNormal[] verticesNoFront;
VertexPositionColorNormal[] verticesNoBack;
VertexPositionColorNormal[] verticesNoRight;
VertexPositionColorNormal[] verticesNoLeft;
private VertexPositionColorNormal[] Front = new VertexPositionColorNormal[5];
private VertexPositionColorNormal[] Back = new VertexPositionColorNormal[5];
private VertexPositionColorNormal[] Right = new VertexPositionColorNormal[5];
private VertexPositionColorNormal[] Up = new VertexPositionColorNormal[5];
private VertexPositionColorNormal[] Left = new VertexPositionColorNormal[5];
private VertexPositionColorNormal[] Down = new VertexPositionColorNormal[5];
public Vector3 normalFront = Vector3.Forward;
public Vector3 normalBack = Vector3.Backward;
public Vector3 normalTop = Vector3.Up;
public Vector3 normalBottom = Vector3.Down;
public Vector3 normalLeft = Vector3.Left;
public Vector3 normalRight = Vector3.Right;
public Block(GraphicsDevice graph, Color color, Vector3 position, ContentManager content)
{
Graphics = graph;
shader = content.Load<Effect>("effects");
CubeColor = color;
pos = position;
Init();
SetVertices();
}
bool test = false;
public void Draw(Camera camera)
{
Graphics.SetVertexBuffer(vb);
shader.CurrentTechnique = shader.Techniques["Colored"];
shader.Parameters["xView"].SetValue(camera.View);
shader.Parameters["xProjection"].SetValue(camera.Projection);
shader.Parameters["xWorld"].SetValue(Matrix.Identity);
Vector3 lightDirection = new Vector3(1.0f, -1.0f, -1.0f);
lightDirection.Normalize();
shader.Parameters["xLightDirection"].SetValue(lightDirection);
shader.Parameters["xAmbient"].SetValue(0.1f);
shader.Parameters["xEnableLighting"].SetValue(true);
foreach (EffectPass pass in shader.CurrentTechnique.Passes)
{
pass.Apply();
this.Graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, vertices.Length / 3);
}
}
#region Methode abstraites
private void Init()
{
InitFront();
InitBack();
InitDown();
InitUp();
InitLeft();
InitRight();
}
public void Remake()
{
switch (cubeForm)
{
case Form.noBack:
vertices = verticesNoBack;
break;
case Form.noFront:
vertices = verticesNoFront;
break;
case Form.noUp:
vertices = verticesNoUp;
break;
case Form.noDown:
vertices = verticesNoDown;
break;
case Form.noRight:
vertices = verticesNoRight;
break;
case Form.noLeft:
vertices = verticesNoLeft;
break;
}
vb = new VertexBuffer(Graphics, typeof(VertexPositionColorNormal), vertices.Length, BufferUsage.None);
vb.SetData(vertices);
verticesNoBack = null;
verticesNoDown = null;
verticesNoFront = null;
verticesNoLeft = null;
verticesNoRight = null;
verticesNoUp = null;
test = true;
}
public void SetVertices()
{
this.vertices = new VertexPositionColorNormal[36];
int y = 0;
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Up[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Down[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Front[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Back[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Right[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Left[x]; y++;
}
setNoBack();
setNoFront();
setNoUp();
setNoDown();
setNoRight();
setNoLeft();
vb = new VertexBuffer(Graphics, typeof(VertexPositionColorNormal), vertices.Length, BufferUsage.None);
vb.SetData(vertices);
Clean();
}
#region InitFaces
public void setNoBack()
{
this.verticesNoBack = new VertexPositionColorNormal[30];
int y = 0;
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Up[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Down[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Front[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Right[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Left[x]; y++;
}
}
public void setNoFront()
{
this.verticesNoFront = new VertexPositionColorNormal[30];
int y = 0;
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Up[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Down[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Back[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Right[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Left[x]; y++;
}
}
public void setNoUp()
{
this.verticesNoUp = new VertexPositionColorNormal[30];
int y = 0;
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Front[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Down[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Back[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Right[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Left[x]; y++;
}
}
public void setNoDown()
{
this.verticesNoFront = new VertexPositionColorNormal[30];
int y = 0;
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Up[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Front[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Back[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Right[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Left[x]; y++;
}
}
public void setNoLeft()
{
this.verticesNoLeft = new VertexPositionColorNormal[30];
int y = 0;
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Up[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Down[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Back[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Right[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Front[x]; y++;
}
}
public void setNoRight()
{
this.verticesNoRight = new VertexPositionColorNormal[30];
int y = 0;
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Up[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Down[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Back[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Front[x]; y++;
}
for (int x = 0; x < 6; x++)
{
this.vertices[y] = Left[x]; y++;
}
}
public void InitFront()
{
this.Front = new VertexPositionColorNormal[6];
Front[0].Position = new Vector3(pos.X, pos.Y + 1, pos.Z);
Front[0].Color = Color.Blue;
Front[0].Normal = normalFront;
Front[1].Position = new Vector3(pos.X, pos.Y, pos.Z + 1);
Front[1].Color = Color.Blue;
Front[1].Normal = normalFront;
Front[2].Position = new Vector3(pos.X, pos.Y, pos.Z);
Front[2].Color = Color.Blue;
Front[2].Normal = normalFront;
Front[3].Position = new Vector3(pos.X, pos.Y + 1, pos.Z);
Front[3].Color = Color.Blue;
Front[3].Normal = normalFront;
Front[4].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1);
Front[4].Color = Color.Blue;
Front[4].Normal = normalFront;
Front[5].Position = new Vector3(pos.X, pos.Y, pos.Z + 1);
Front[5].Color = Color.Blue;
Front[5].Normal = normalFront;
}
public void InitBack()
{
this.Back = new VertexPositionColorNormal[6];
Back[0].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1);
Back[0].Color = Color.Red;
Back[0].Normal = normalBack;
Back[1].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z);
Back[1].Color = Color.Red;
Back[1].Normal = normalBack;
Back[2].Position = new Vector3(pos.X + 1, pos.Y, pos.Z);
Back[2].Color = Color.Red;
Back[2].Normal = normalBack;
Back[3].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z + 1);
Back[3].Color = Color.Red;
Back[3].Normal = normalBack;
Back[4].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z);
Back[4].Color = Color.Red;
Back[4].Normal = normalBack;
Back[5].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1);
Back[5].Color = Color.Red;
Back[5].Normal = normalBack;
}
public void InitUp()
{
this.Up = new VertexPositionColorNormal[6];
Up[0].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z);
Up[0].Color = Color.Black;
Up[0].Normal = normalTop;
Up[1].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z + 1);
Up[1].Color = Color.Black;
Up[1].Normal = normalTop;
Up[2].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1);
Up[2].Color = Color.Black;
Up[2].Normal = normalTop;
Up[3].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z);
Up[3].Color = Color.Black;
Up[3].Normal = normalTop;
Up[4].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1);
Up[4].Color = Color.Black;
Up[4].Normal = normalTop;
Up[5].Position = new Vector3(pos.X, pos.Y + 1, pos.Z);
Up[5].Color = Color.Black;
Up[5].Normal = normalTop;
}
public void InitDown()
{
this.Down = new VertexPositionColorNormal[6];
Down[0].Position = new Vector3(pos.X + 1, pos.Y, pos.Z);
Down[0].Color = Color.Orange;
Down[0].Normal = normalBottom;
Down[1].Position = new Vector3(pos.X, pos.Y, pos.Z);
Down[1].Color = Color.Orange;
Down[1].Normal = normalBottom;
Down[2].Position = new Vector3(pos.X, pos.Y, pos.Z + 1);
Down[2].Color = Color.Orange;
Down[2].Normal = normalBottom;
Down[3].Position = new Vector3(pos.X + 1, pos.Y, pos.Z);
Down[3].Color = Color.Orange;
Down[3].Normal = normalBottom;
Down[4].Position = new Vector3(pos.X, pos.Y, pos.Z + 1);
Down[4].Color = Color.Orange;
Down[4].Normal = normalBottom;
Down[5].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1);
Down[5].Color = Color.Orange;
Down[5].Normal = normalBottom;
}
public void InitRight()
{
this.Right = new VertexPositionColorNormal[6];
Right[0].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1);
Right[0].Color = Color.Green;
Right[0].Normal = normalRight;
Right[1].Position = new Vector3(pos.X, pos.Y, pos.Z + 1);
Right[1].Color = Color.Green;
Right[1].Normal = normalRight;
Right[2].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1);
Right[2].Color = Color.Green;
Right[3].Normal = normalRight;
Right[3].Position = new Vector3(pos.X + 1, pos.Y, pos.Z + 1);
Right[3].Color = Color.Green;
Right[3].Normal = normalRight;
Right[4].Position = new Vector3(pos.X, pos.Y + 1, pos.Z + 1);
Right[4].Color = Color.Green;
Right[4].Normal = normalRight;
Right[5].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z + 1);
Right[5].Color = Color.Green;
Right[5].Normal = normalRight;
}
public void InitLeft()
{
this.Left = new VertexPositionColorNormal[6];
Left[0].Position = new Vector3(pos.X, pos.Y, pos.Z);
Left[0].Color = Color.Aqua;
Left[0].Normal = normalLeft;
Left[1].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z);
Left[1].Color = Color.Aqua;
Left[1].Normal = normalLeft;
Left[2].Position = new Vector3(pos.X, pos.Y + 1, pos.Z);
Left[2].Color = Color.Aqua;
Left[2].Normal = normalLeft;
Left[3].Position = new Vector3(pos.X, pos.Y, pos.Z);
Left[3].Color = Color.Aqua;
Left[3].Normal = normalLeft;
Left[4].Position = new Vector3(pos.X + 1, pos.Y, pos.Z);
Left[4].Color = Color.Aqua;
Left[4].Normal = normalLeft;
Left[5].Position = new Vector3(pos.X + 1, pos.Y + 1, pos.Z);
Left[5].Color = Color.Aqua;
Left[5].Normal = normalLeft;
}
#endregion
private void setTriangleNormal(VertexPositionColorNormal v1, VertexPositionColorNormal v2, VertexPositionColorNormal v3)
{
Vector3 side1 = v1.Position - v3.Position;
Vector3 side2 = v1.Position - v2.Position;
Vector3 normal = Vector3.Cross(side1, side2);
v1.Normal += normal;
v2.Normal += normal;
v3.Normal += normal;
v1.Position.Normalize();
v1.Position.Normalize();
v1.Position.Normalize();
}
public void Clean()
{
Front = null;
Back = null;
Right = null;
Left = null;
Up = null;
Down = null;
}
#endregion
}
}
名称空间LandOfCube.Models
{
类块
{
VertexBuffer instanceBuffer;
公共枚举表
{
全部的
努普,
诺敦,
诺赖特,
无左,
诺弗朗特,
诺巴克
}
公共表单cubeForm=Form.all;
图形设备图形;
效果着色器;
私有顶点缓冲区vb;
彩色立方色;
公共向量机3 pos;
VertexPositionColorNormal[]顶点;
VertexositionColorNormal[]verticesNoUp;
VertexPositionColorNormal[]VerticesNodeown;
VertexPositionColorNormal[]verticesNoFront;
VertexPositionColorNormal[]verticesNoBack;
垂直曝光颜色正常[]垂直无光;
VertexPositionColorNormal[]verticesNoLeft;
私有VertexPositionColorNormal[]前端=新的VertexPositionColorNormal[5];
私有VertexPositionColorNormal[]返回=新的VertexPositionColorNormal[5];
私有VertexPositionColorNormal[]右=新的VertexPositionColorNormal[5];
私有VertexPositionColorNormal[]向上=新的VertexPositionColorNormal[5];
私有VertexPositionColorNormal[]左=新VertexPositionColorNormal[5];
私有VertexPositionColorNormal[]向下=新VertexPositionColorNormal[5];
公共向量3 normalFront=Vector3.Forward;
公共向量3 normalBack=Vector3.Backward;
公共向量3 normalTop=Vector3.Up;
公共向量3 normalBottom=Vector3.Down;
公共向量3 normalLeft=Vector3.Left;
公共向量3 normalRight=Vector3.Right;
公共块(图形设备图形、颜色、矢量3位置、ContentManager内容)
{
图形=图形;
着色器=content.Load(“效果”);
立方色=颜色;
pos=位置;
Init();
设置顶点();
}
布尔检验=假;
公共空间绘制(摄影机)
{
图形。SetVertexBuffer(vb);
shader.currentTechnical=shader.technologies[“Colored”];
shader.Parameters[“xView”].SetValue(camera.View);
shader.Parameters[“xProjection”].SetValue(camera.Projection);
shader.Parameters[“xWorld”].SetValue(矩阵.Identity);
Vector3 lightDirection=新Vector3(1.0f,-1.0f,-1.0f);
lightDirection.Normalize();
shader.Parameters[“xLightDirection”].SetValue(lightDirection);
着色器参数[“xAmbient”].SetValue(0.1f);
shader.Parameters[“xenablellighting”].SetValue(true);
foreach(着色器中的EffectPass pass.currentTechnical.passs)
{
pass.Apply();
this.Graphics.DrawPrimitives(PrimitiveType.TriangleList,0,顶点.Length/3);
}
}
#区域法
私有void Init()
{
InitFront();
InitBack();
InitDown();
InitUp();
InitLeft();
InitRight();
}
公共无效重拍()
{
开关(立方形)
{
case Form.noBack:
顶点=垂直方向;
打破
case Form.noFront:
顶点=垂直面;
打破
case Form.noUp:
顶点=垂直方向;
打破
case Form.noDown:
顶点=垂直节点;
打破
案例表格.noRight:
顶点=垂直或垂直;
打破
case Form.noLeft:
顶点=垂直无左;
打破
}
vb=新的VertexBuffer(图形,类型(VertexExpositionColorNormal),顶点.长度,BufferUsage.None);
vb.SetData(顶点);
verticesNoBack=null;
VerticesNodeown=null;
verticesNoFront=null;
verticesNoLeft=null;
verticesNoRight=null;
verticesNoUp=null;
测试=真;
}
公共void SetVertices()
{
this.vertexs=新的VertexositionColorNormal[36];
int y=0;
对于(int x=0;x<6;x++)
{
这个。顶点[y]=Up[x];y++;
}
对于(int x=0;x<6;x++)
{
这个。顶点[y]=向下[x];y++;
}
对于(int x=0;x<6;x++)
{
这个。顶点[y]=前面[x];y++;
}
对于(int x=0;x<6;x++)
{
this.vertices[y]=Back[x];y++;
}
对于(int x=0;x<6;x++)
{
这个。顶点[y]=右[x];y++;
}
对于(int x=0;x<6;x++)
{
这个。顶点[y]=左[x];y++;
}
setNoBack();
setNoFront();
setNoUp();
setNoDown();
setNoRight();
setNoLeft();
vb=新的VertexBuffer(图形,类型(VertexExpositionColorNormal),顶点.长度,BufferUsage.None);
vb.SetData(顶点);
清洁();
}
#面域
public void setNoBack()
{
this.verticesNoBack=新的VertexExpositionColorNormal[30];
int y=0;
对于