XNA CreateBillboard 在球体边缘而不是原点旋转?
XNA CreateBillboard rotates on edge of sphere, not the origin?
我正在尝试在 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是相机位置(很明显)。
我可能向函数传递了错误的坐标,但我已经尝试了所有方法(包括将位置转换为对象 space),但到目前为止没有任何效果。
至于我更好的解决方案(对于简单的情况)是:
创建从 (0.5,0.5,0) 到 (-0.5,-0.5,0) 的单平面
使用适当的纹理渲染此平面并在着色器中对其进行变换:
结果=旋转*缩放*平移*wvp
其中 "rotation" 是相机的旋转,"scale" 和 "translation" 可以表示为 Vector4 值,其中 XYZ 是平移,W 是广告牌比例,或者作为单一比例+平移矩阵。
很快我就能提供一个例子。
我正在尝试在 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是相机位置(很明显)。
我可能向函数传递了错误的坐标,但我已经尝试了所有方法(包括将位置转换为对象 space),但到目前为止没有任何效果。
至于我更好的解决方案(对于简单的情况)是:
创建从 (0.5,0.5,0) 到 (-0.5,-0.5,0) 的单平面
使用适当的纹理渲染此平面并在着色器中对其进行变换:
结果=旋转*缩放*平移*wvp
其中 "rotation" 是相机的旋转,"scale" 和 "translation" 可以表示为 Vector4 值,其中 XYZ 是平移,W 是广告牌比例,或者作为单一比例+平移矩阵。
很快我就能提供一个例子。