- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
几个小时以来,我一直在努力解决这个问题。我想做的是画一个立方体,每边都有不同的纹理;或者更具体地说,我希望能够指定每一面我想要的任何纹理。我用了例子 here开始,然后尝试进一步开发它,这样我就可以拥有不止一种纹理。然而,无论我做什么,它仍然只使用应用于效果的最后一个纹理,而不会理会任何先前的分配。这是我的形状类:
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);
}
在我的游戏的 Draw 方法中:
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);
}
如你所见,我正在加载不同的纹理,但结果是这样的:
my cube http://www.tinyimg.org/images/769MinecraftClassic_2011_.bmp
我确信纹理是不同的,但在所有面上都绘制了相同的纹理。每一面都需要单独的效果吗?这绝对看起来有点矫枉过正。
最佳答案
在您调用 EffectPass.Apply() 之前,不会应用为效果设置的任何参数。这是因为对效果应用更改的成本很高,您可能希望一次性执行多项更改。
您的 RenderShape 函数应该类似于:
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);
}
}
关于c# - 在 XNA 4.0 中绘制多边纹理立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4674804/
我可以将内容项目引用到非 XNA 项目(MonoGame,如果它很重要)? 最佳答案 不直接。 将内容项目引用添加到项目的功能仅适用于“XNA 游戏”和“XNA 游戏库”类型的项目。 当我想在 Win
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
您如何看待 xna 和大型多人游戏?例如服务器和 100 个客户端?有没有很好的教程如何在 xna 中创建客户端服务器应用程序? 最佳答案 当然有关于如何做到这一点的教程。我个人使用 Lidgren
好的,所以我正在开发这个游戏,我是 XNA 的新手(我使用的是 4.0),我想做的是每次产生敌人时都有不同的纹理。 所以我有这些图像“image1.png”、“image2.png”等等。我希望它在每
我最近开始检查XNA。一切进展顺利,我什至开始了一个工作样本。困扰我的一件事是,每当我将鼠标放在游戏窗口上时,它就会消失。我不知道这是否是默认行为,但我想更改此设置以便可以看到鼠标。任何和所有建议,不
我刚从 XNA 开始,有一个关于轮换的问题。当你在 XNA 中将一个向量乘以一个旋转矩阵时,它会逆时针旋转。这个我明白。 但是,让我举一个我不明白的例子。假设我将一个随机艺术 Assets 加载到管道
我正在使用 Visual Studio 2010 Ultimate,在谷歌上搜索 XNA 下载后,我找到了这个页面: http://blogs.msdn.com/b/xna/archive/2010/
我有一个字典来存储SoundEffects,例如: public static Dictionary Hangok = new Dictionary(); 我从文件(正常.wav格式)加载声音,例如:
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 10年前关闭。 Improve this
好吧,我正在尝试为 3D XNA 游戏创建自己的物理引擎,但我在计算我应该将物体移动多少时遇到重力问题。 XNA 的游戏计时器每 16 毫秒出现一次,所以经过一些计算,并使用 9.81m/s 作为我的
我正在尝试对以下粒子系统进行修改。 http://create.msdn.com/en-US/education/catalog/sample/particle_3d 我有一个功能,当我按下Space
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
也许这更像是一个数学问题而不是一个编程问题,但我一直在尝试在 XNA 中实现旋转卡尺算法。 我已经使用维基百科上详述的单调链从我的点集中推导出了一个凸包。 现在,我正在尝试对我的算法进行建模,以在此处
我正在 XNA 中制作一个简单的 2 人游戏,并开始研究保存玩家的高分。 我希望游戏能在 XBox 360 和 Windows 上运行,所以我必须使用框架来保存数据。 您似乎将数据保存到特定用户的玩家
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我需要使用不同字体大小的 spritefont,我必须为每个大小创建新的 spritefont 吗? 最佳答案 基本上是的。 有一个overload的 SpriteBatch.DrawString这为
我对在我的游戏中存储 *.txt 级别文件的位置和方式感到非常困惑。 我想要在我的游戏中提供一些随游戏一起安装的“预烘焙”关卡的可用性,以及用户创建自己的关卡的能力。 所以我所做的是在 Visual
我有一个足够简单的着色器,它支持多个点光源。 灯光存储为一组灯光结构(最大大小),当事件灯光发生变化时,我会传入事件灯光的数量。 问题出在 PixelShader 函数中: 这是基本的东西,从纹理中获
我有 19 张图像,它们是我的播放器的动画帧 在下面,我创建了 Frog 纹理数组,这是我的播放器。并且有 19 个图像。如何对它们进行动画处理。 公共(public)类纹理 { 公共(public)
所以,我要直截了本地说,我正在尝试在我正在制作的 XNA 游戏中制作一个聊天系统。我可以很容易地找出正在按下的键但是我无法知道实际键入的是什么。在我看来,他们好像在说:OemShifthello bi
我是一名优秀的程序员,十分优秀!