在不渲染的情况下对模型应用变换
问题描述:
我正在为图形类构建射线追踪器,我们需要做的一部分设置是在投射射线之前将模型缩放到世界空间中。我已经建立了worldMatrix需要的。不过,我第一次尝试转换模型的结果会改变模型的边界框,但是没有VertexBuffer中的顶点。在不渲染的情况下对模型应用变换
这是我第一次尝试:
foreach (ModelBone b in model.Bones)
{
// apply the model transforms and the worldMatrix transforms to the bone
b.Transform = model.Root.Transform * worldMatrix;
}
我也试着设置在模型中找到网格像每一个模型绘画教程显示效果值,但无济于事。
有没有我应该尝试转换模型的一些其他的方式?
答
b.Transform = root * world;没有考虑骨骼本身的任何数据。
可能你需要:b.Transform = root * b * world;
顶点缓冲区中的数据应该在游戏/应用程序的整个生命周期内保持不变。会发生什么情况是原始(不变的)顶点数据会在顶点着色器中被每个帧重新转换为您通过效果发送的任何不同的世界矩阵。
通常情况下,它会去是这样的:
//class scope fields
Matrix[] modelTransforms;
Matrix worldMatrix;
//in the loadContent method just after loading the model
modelTransforms - new Matrix[model.Bones.Count];
model.CopyAbsoluteTransformsTo(modelTransforms);//this line does what your foreach does but will work on multitiered hierarchy systems
//in the Update methos
worldMatrix = Matrix.CreateScale(scale);
boundingBox.Min *= scale;
boundingBox.Max *= scale
//raycast against the boundingBox here
//in the draw call
eff.World = modelTransforms[mesh.ParentBone.Index] * worldMatrix;