【问题标题】:XNA CreateBillboard rotates on edge of sphere, not the origin?XNA CreateBillboard 在球体边缘旋转,而不是原点?
【发布时间】:2015-12-08 09:33:43
【问题描述】:

我正在尝试在 3D 世界中创建广告牌纹理,其中广告牌以中心为旋转点旋转。

Matrix.CreateBillboard 函数出于某种原因仅围绕球体旋转对象。所以当使用这个函数时,旋转是围绕一个球体的边缘,但我希望旋转是围绕对象中心的一个点。

这是我现在设置的代码:

effect.World *= Matrix.CreateBillboard(position, position - Camera.Position, Vector3.Up, null);

int topLeftIndex = 0;
int topRightIndex = 1;
int bottomRightIndex = 2;
int bottomLeftIndex = 3;

VertexPositionColorTexture[] vertices = new VertexPositionColorTexture[4];
vertices[topLeftIndex] = new VertexPositionColorTexture(new Vector3(position.X - size.X / 2, position.Y + size.Y / 2, position.Z), Color.White, new Vector2(0, 0));
vertices[topRightIndex] = new VertexPositionColorTexture(new Vector3(position.X + size.X / 2, position.Y + size.Y / 2, position.Z), Color.White, new Vector2(1, 0));
vertices[bottomLeftIndex] = new VertexPositionColorTexture(new Vector3(position.X - size.X / 2, position.Y - size.Y / 2, position.Z), Color.White, new Vector2(0, 1));
vertices[bottomRightIndex] = new VertexPositionColorTexture(new Vector3(position.X + size.X / 2, position.Y - size.Y / 2, position.Z), Color.White, new Vector2(1, 1));

int[] indices = new int[6];
indices[0] = topLeftIndex;
indices[1] = bottomRightIndex;
indices[2] = bottomLeftIndex;
indices[3] = topLeftIndex;
indices[4] = topRightIndex;
indices[5] = bottomRightIndex;

graphicsDeviceManager.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, 4, indices, 0, 2);

position 变量包含对象中心的位置,Camera.Position 是相机位置(显然)。

我可能将错误的坐标传递给函数,但我已经尝试了所有方法(包括将位置转换为对象空间),但到目前为止没有任何效果。

【问题讨论】:

  • XNA不是停产了吗?
  • @Raptor 是的。我只是用它来学习。

标签: c# matrix rotation xna


【解决方案1】:

对我来说更好的解决方案(对于简单的情况)是:

  1. 创建从 (0.5,0.5,0) 到 (-0.5,-0.5,0) 的单个平面

  2. 用适当的纹理渲染这个平面并在着色器中变换它:

    结果 = 旋转 * 缩放 * 平移 * wvp

其中“rotation”是相机的旋转,“scale”和“translation”可以表示为 Vector4 值,其中 XYZ 是平移,W 是广告牌比例,或者作为单个比例+平移矩阵。

很快我就可以提供一个例子了。

【讨论】:

  • 因为我无法让广告牌工作,所以我采用了这个解决方案。最终我确实让它工作了,通过将位置设置为Vector3.Zero 并在创建广告牌矩阵之前转换到对象空间(translate -postion 和转换位置之后)。
  • 在我发布示例之前,您可以查看:goo.gl/DRRCVj, goo.gl/yAFN7S
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多