将osg::Geometry转换为FMeshDescription,通过FMeshDescription即可构建UStaticMesh,下面这段代码主要是为了转换倾斜摄影的osgb数据,但是倾斜摄影数据中本身不携带法线数据,也不需要计算法线,因此如果要转换其他携带或需要计算法线的osg::Geometry的数据,请另行将法线信息补充到FMeshDescription中文章来源地址https://www.toymoban.com/news/detail-859001.html
FMeshDescription ConvertOsgGeometryToMeshDescription(osg::ref_ptr<osg::Geometry> InGeometry)
{
FMeshDescription result;
//顶点
osg::MixinVector<osg::Vec3f>* vertices = dynamic_cast<osg::MixinVector<osg::Vec3f>*>(InGeometry->getVertexArray());
if (!vertices)
return result;
//纹理坐标
osg::MixinVector<osg::Vec2f>* texCoords = dynamic_cast<osg::MixinVector<osg::Vec2f>*>(InGeometry->getTexCoordArray(0));
assert(vertices->size() == texCoords->size());
//法线,倾斜摄影数据没有法线,其实不需要
osg::MixinVector<osg::Vec3f>* normals = dynamic_cast<osg::MixinVector<osg::Vec3f>*>(InGeometry->getNormalArray());
//索引
osg::MixinVector<unsigned int>* indices = dynamic_cast<osg::MixinVector<unsigned int>*>(InGeometry->getPrimitiveSet(0));
if (!indices)
return result;
assert(indices->size() / 3 == 0);
FStaticMeshAttributes attributes(result);
//注册默认的静态模型属性
attributes.Register();
//FMeshDescriptionBuilder用于设置顶点、法线、UV坐标等信息
FMeshDescriptionBuilder meshDescBuilder;
meshDescBuilder.SetMeshDescription(&result);
//允许使用多边形组
meshDescBuilder.EnablePolyGroups();
//设置UV坐标的级数为1
meshDescBuilder.SetNumUVLayers(1);
FPolygonGroupID GroupId = meshDescBuilder.AppendPolygonGroup();
const uint32 vertexCount = vertices->size();
TArray<FVertexID> vertexIDs;
vertexIDs.SetNum(vertices->size());
const float unit = 100.0;
// 定义一个Lambda表达式,用于将OSG坐标转换为UE坐标
auto ConvertOSGToUE = [&unit](const osg::Vec3f& OSGCoordinate) -> FVector
{
float X = OSGCoordinate.x() * unit;
float Y = -OSGCoordinate.y() * unit;
float Z = OSGCoordinate.z() * unit;
return FVector(X, Y, Z);
};
for (uint32 i = 0; i < vertexCount; ++i) {
const osg::Vec3f vertex = vertices->at(i);
vertexIDs[i] = meshDescBuilder.AppendVertex(ConvertOSGToUE(vertex));
}
const uint32 indiceCount = indices->size();
assert(indiceCount / 3 == 0);
const uint32 triangleCount = indiceCount / 3;
for (uint32 i = 0; i < triangleCount; ++i) {
const uint32 indice1 = indices->at(i * 3);
const FVertexInstanceID vertexInstanceId1 = meshDescBuilder.AppendInstance(vertexIDs[indice1]);
const osg::Vec2f texCoord1 = texCoords->at(indice1);
meshDescBuilder.SetInstanceUV(vertexInstanceId1, FVector2D(texCoord1.x(), 1 - texCoord1.y()), 0);
const uint32 indice2 = indices->at(i * 3 + 1);
const FVertexInstanceID vertexInstanceId2 = meshDescBuilder.AppendInstance(vertexIDs[indice2]);
const osg::Vec2f texCoord2 = texCoords->at(indice2);
meshDescBuilder.SetInstanceUV(vertexInstanceId2, FVector2D(texCoord2.x(), 1 - texCoord2.y()), 0);
const uint32 indice3 = indices->at(i * 3 + 2);
const FVertexInstanceID vertexInstanceId3 = meshDescBuilder.AppendInstance(vertexIDs[indice3]);
const osg::Vec2f texCoord3 = texCoords->at(indice3);
meshDescBuilder.SetInstanceUV(vertexInstanceId3, FVector2D(texCoord3.x(), 1 - texCoord3.y()), 0);
meshDescBuilder.AppendTriangle(vertexInstanceId1, vertexInstanceId2, vertexInstanceId3, GroupId);
}
//FStaticMeshOperations::ComputeTriangleTangentsAndNormals(result);//倾斜摄影数据没有法线,不必计算
return result;
}
文章来源:https://www.toymoban.com/news/detail-859001.html
到了这里,关于10、将osg的Geometry转换为虚幻引擎的UStaticMesh的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!