美文网首页
UE4 CableMesh

UE4 CableMesh

作者: wblong | 来源:发表于2021-03-30 21:32 被阅读0次

UE4 CableMesh

UE4 CableComponent 生成绳索效果的Mesh。

绳索网格

基本思路

给定起始点内插一系列点,然后物理模拟调整点的位置,最后在点周围生成一系列三角网。

生成三角网的代码如下:

/** The vertex type used for dynamic meshes. */
struct FDynamicMeshVertex
{
    FDynamicMeshVertex() {}
    FDynamicMeshVertex( const FVector& InPosition ):
        Position(InPosition),
        TangentX(FVector(1,0,0)),
        TangentZ(FVector(0,0,1)),
        Color(FColor(255,255,255)) 
    {
        //..............................
    }

    FDynamicMeshVertex(const FVector& InPosition, const FVector2D& InTexCoord, const FColor& InColor) :
        Position(InPosition),
        TangentX(FVector(1, 0, 0)),
        TangentZ(FVector(0, 0, 1)),
        Color(InColor)
    {
        //...................................
    }

    FDynamicMeshVertex(const FVector& InPosition,const FVector& InTangentX,const FVector& InTangentZ,const FVector2D& InTexCoord, const FColor& InColor):
        Position(InPosition),
        TangentX(InTangentX),
        TangentZ(InTangentZ),
        Color(InColor)
    {
        //.......................
    }

    FDynamicMeshVertex(const FVector& InPosition, const FVector& LayerTexcoords, const FVector2D& WeightmapTexcoords)
        : Position(InPosition)
        , TangentX(FVector(1, 0, 0))
        , TangentZ(FVector(0, 0, 1))
        , Color(FColor::White)
    {
        //...............
    }
   //顶点位置
    FVector Position;
   //纹理坐标
    FVector2D TextureCoordinate[MAX_STATIC_TEXCOORDS];
   //切线
    FPackedNormal TangentX;
   //法线
    FPackedNormal TangentZ;
    FColor Color;
};
void BuildCableMesh(const TArray<FVector>& InPoints, TArray<FDynamicMeshVertex>& OutVertices, TArray<int32>& OutIndices)
    {
        const FColor VertexColor(255,255,255);
        const int32 NumPoints = InPoints.Num();
        const int32 SegmentCount = NumPoints-1;

        // Build vertices

        // We double up the first and last vert of the ring, because the UVs are different
        int32 NumRingVerts = NumSides+1;

        // For each point along spline..
        for(int32 PointIdx=0; PointIdx<NumPoints; PointIdx++)
        {
            const float AlongFrac = (float)PointIdx/(float)SegmentCount; // Distance along cable

            // Find direction of cable at this point, by averaging previous and next points
            const int32 PrevIndex = FMath::Max(0, PointIdx-1);
            const int32 NextIndex = FMath::Min(PointIdx+1, NumPoints-1);
            const FVector ForwardDir = (InPoints[NextIndex] - InPoints[PrevIndex]).GetSafeNormal();

            // Find quat from up (Z) vector to forward
            const FQuat DeltaQuat = FQuat::FindBetween(FVector(0, 0, -1), ForwardDir);

            // Apply quat orth vectors
            const FVector RightDir = DeltaQuat.RotateVector(FVector(0, 1, 0));
            const FVector UpDir = DeltaQuat.RotateVector(FVector(1, 0, 0));

            // Generate a ring of verts
            for(int32 VertIdx = 0; VertIdx<NumRingVerts; VertIdx++)
            {
                const float AroundFrac = float(VertIdx)/float(NumSides);
                // Find angle around the ring
                const float RadAngle = 2.f * PI * AroundFrac;
                // Find direction from center of cable to this vertex
                const FVector OutDir = (FMath::Cos(RadAngle) * UpDir) + (FMath::Sin(RadAngle) * RightDir);

                FDynamicMeshVertex Vert;
                Vert.Position = InPoints[PointIdx] + (OutDir * 0.5f * CableWidth);
                Vert.TextureCoordinate[0] = FVector2D(AlongFrac * TileMaterial, AroundFrac);
                Vert.Color = VertexColor;
                Vert.SetTangents(ForwardDir, OutDir ^ ForwardDir, OutDir);
                OutVertices.Add(Vert);
            }
        }

        // Build triangles
        for(int32 SegIdx=0; SegIdx<SegmentCount; SegIdx++)
        {
            for(int32 SideIdx=0; SideIdx<NumSides; SideIdx++)
            {
                int32 TL = GetVertIndex(SegIdx, SideIdx);
                int32 BL = GetVertIndex(SegIdx, SideIdx+1);
                int32 TR = GetVertIndex(SegIdx+1, SideIdx);
                int32 BR = GetVertIndex(SegIdx+1, SideIdx+1);

                OutIndices.Add(TL);
                OutIndices.Add(BL);
                OutIndices.Add(TR);

                OutIndices.Add(TR);
                OutIndices.Add(BL);
                OutIndices.Add(BR);
            }
        }
    }

参考链接

UE4 CableMesh

相关文章

  • UE4 CableMesh

    UE4 CableMesh UE4 CableComponent 生成绳索效果的Mesh。 基本思路 给定起始点内...

  • UE4 毛发渲染参考文章

    《UE4 头发制作流程》《ue4 4.24测试功能groom头发》《Digital Human UE4 TEST ...

  • 插件和模块

    UE4 插件和模块[https://zhuanlan.zhihu.com/p/366559009] UE4[C++...

  • UE4买量视频设计教程

    ue4蓝图[https://search.bilibili.com/all?keyword=UE4%E8%93%9...

  • 【UE4】(一)UE4介绍&下载安装

    UE4介绍 ◆1.UE4是什么? ◆2.UE4有哪些功能? 1 UE4是什么? UE4也叫虚幻4,是由Epic G...

  • UE4资料

    UE4命名规范 https://github.com/ericzhou9/ue4-style-guide Ue4蓝...

  • 关于Unity的UV2

    这几天在学UE4,模型导入到UE4里面,最好是有UV2的(虽然UE4也可以像unity一样自动生成lightmap...

  • 3dmax导出插件

    UE4 使用UnrealDatasmithMaxExporter插件解决3DMax导入ue4坐标问题 当需要从3D...

  • UE4学习路线

    1.ue4介绍 ue4介绍ue4和unity对比 2.模板介绍和工程创建 安装ue4创建工程模块选择,编辑器介绍 ...

  • Unreal Engine4关卡切换

    参考原文:《UE4中关卡切换》 新建项目在UE4中创建新的项目,选择模板BluePrint——ThirdPerso...

网友评论

      本文标题:UE4 CableMesh

      本文链接:https://www.haomeiwen.com/subject/rccnhltx.html