问题
How do you find the vector 3 position of a mesh inside of a model in XNA(in global coordinates)?
I've made a model of a simple lamp post in blender. Where the light should be, I've put a single vertice as its own object and named it "Light", in the position a typical light would be in a lamp post.
In my XNA program, I want to put a floating point light at the position from the model. I can find the mesh by name. But need the Vector3 position of the "Light" mesh to do it.
回答1:
It depends how the model is set up in terms of mesh/bone hierarchy. Ultimately, you multiply the light (bulb) matrix by its parent, who was first multiplied by its parent, and so on all the way to the root, then take the translation property of the result. There is a built in method to do this called Model.CopyAbsoluteBoneTransformsTo(Matrix[])
Matrix[] transforms = new Matrix[SimpleLampPostModel.Bones.Count];
SimpleLampPostModel.CopyAbsoluteBoneTransformsTo(transforms);
Vector3 lightPosition = transforms[SimpleLampPostModel.Meshes["light"].parentBone.Index].Translation;
lightPosition += modelWorld.Translation;// if the model is located in some arbitrary location in your 3d
// world as represented by a world matrix (modelWorld)
来源:https://stackoverflow.com/questions/14873609/position-of-a-modelmesh-in-xna