XNA CreateBillboard在球体的边缘旋转,而不是原点?

时间:2015-12-08 09:33:43

标签: c# matrix rotation xna

我试图在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是摄像机位置(显然)。

我可能会将错误的坐标传递给函数,但我已尝试过所有内容(包括将位置转换为对象空间),但到目前为止还没有任何工作。

1 个答案:

答案 0 :(得分:1)

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

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

  2. 使用适当的纹理渲染此平面并将其转换为着色器:

    result = rotation * scale * translation * wvp

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

    很快我就能提供一个例子。