diff --git a/Source/MultiLine/MultiLine.Build.cs b/Source/MultiLine/MultiLine.Build.cs index 328587ebfcff354bc9eab0070a6f9f77ca26a21e..b6efa063d6d0bb6f6ae0759643a0fddda3ef3609 100644 --- a/Source/MultiLine/MultiLine.Build.cs +++ b/Source/MultiLine/MultiLine.Build.cs @@ -26,6 +26,7 @@ public class MultiLine : ModuleRules new string[] { "Core", + "ProceduralMeshComponent" // ... add other public dependencies that you statically link with here ... } ); diff --git a/Source/MultiLine/Private/MultiLineActor.cpp b/Source/MultiLine/Private/MultiLineActor.cpp index aeec4a00121d441d67234a0342322f1818e322bf..d3e335db8a2670edc41e3bd9b5d06029570b00fa 100644 --- a/Source/MultiLine/Private/MultiLineActor.cpp +++ b/Source/MultiLine/Private/MultiLineActor.cpp @@ -13,19 +13,20 @@ AMultiLineActor::AMultiLineActor() mesh_ = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("MultiLineMesh")); static ConstructorHelpers::FObjectFinder<UMaterial> material(TEXT("/MultiLine/vertex_color.vertex_color")); - if(material.Succeeded()) + if (material.Succeeded()) material_ = material.Object; RootComponent = mesh_; } + #pragma optimize( "", off ) -void AMultiLineActor::CreateMesh(TArray<LineProps> line_props_vec, bool has_collision_context) -{ +void AMultiLineActor::CreateMesh(TArray<LineProps> & line_props_vec, bool has_collision_context) +{ TArray<FLinearColor> vertex_colors; FOccluderVertexArray vertices; TArray<int32> triangles; auto index_offset = 0; - for(auto i = 0; i < line_props_vec.Num(); ++i) + for (auto i = 0; i < line_props_vec.Num(); ++i) { auto & line_props = line_props_vec[i]; @@ -33,10 +34,10 @@ void AMultiLineActor::CreateMesh(TArray<LineProps> line_props_vec, bool has_coll const auto added_lines_cnt = (line_props.points.Num() - 1); const auto added_vertex_cnt = added_lines_cnt * 8; - + SetUpTriangles(index_offset, triangles); index_offset += 4; - for(size_t j = 1; j < added_lines_cnt; ++j) + for (size_t j = 1; j < added_lines_cnt; ++j) { SetUpTriangles(index_offset, triangles); index_offset += 4; @@ -55,10 +56,10 @@ void AMultiLineActor::CreateMesh(TArray<LineProps> line_props_vec, bool has_coll mesh_->CreateMeshSection_LinearColor(0, vertices, triangles, normals, uv, vertex_colors, tangents, false); mesh_->ContainsPhysicsTriMeshData(false); mesh_->SetMaterial(0, dynamic_cast<UMaterialInterface*>(material_)); - + } -void AMultiLineActor::UpdateMesh(TArray<LineProps> line_props_vec) +void AMultiLineActor::UpdateMesh(TArray<LineProps> & line_props_vec) { auto uv = TArray<FVector2D>(); auto tangents = TArray<FProcMeshTangent>(); @@ -74,41 +75,49 @@ void AMultiLineActor::UpdateMesh(TArray<LineProps> line_props_vec) vertex_colors.Add(line_props.linear_color); } - mesh_->UpdateMeshSection_LinearColor(0, vertices, normals, uv, vertex_colors, tangents ); + mesh_->UpdateMeshSection_LinearColor(0, vertices, normals, uv, vertex_colors, tangents); } + void AMultiLineActor::SetFlatShading(bool use_flat_shading) { - if(use_flat_shading) + if (use_flat_shading) material_->SetShadingModel(EMaterialShadingModel::MSM_Unlit); else material_->SetShadingModel(EMaterialShadingModel::MSM_DefaultLit); } + +void AMultiLineActor::Destroyed() +{ + if (mesh_) + mesh_->ClearAllMeshSections(); + AActor::Destroyed(); + +} #pragma optimize( "", on ) -// Called when the game starts or when spawned void AMultiLineActor::BeginPlay() { - Super::BeginPlay(); + Super::BeginPlay(); } -void AMultiLineActor::CreateVertices(LineProps line_props, FOccluderVertexArray & vertices) +void AMultiLineActor::CreateVertices(LineProps & line_props, FOccluderVertexArray & vertices) { - for(size_t i = 1; i < line_props.points.Num(); ++i) + for (size_t i = 1; i < line_props.points.Num(); ++i) { - auto line = line_props.points[i] - line_props.points[i-1]; + auto line = line_props.points[i] - line_props.points[i - 1]; line.Normalize(); auto tangent = FVector::CrossProduct(line, FVector(0.0f, 0.0f, 1.0f)); tangent.Normalize(); auto bi_tangent = FVector::CrossProduct(line, tangent); bi_tangent.Normalize(); - auto & p0 = line_props.points[i-1]; + auto & p0 = line_props.points[i - 1]; auto & p1 = line_props.points[i]; vertices.Add(p0 + line_props.width * FVector(tangent - bi_tangent)); vertices.Add(p0 + line_props.width * FVector(tangent + bi_tangent)); - + vertices.Add(p0 - line_props.width * FVector(tangent + bi_tangent)); vertices.Add(p0 - line_props.width * FVector(tangent - bi_tangent)); diff --git a/Source/MultiLine/Public/MultiLineActor.h b/Source/MultiLine/Public/MultiLineActor.h index 807f633fdd22949d17e0043620d3364aad34e215..aff4e8fad79636a963981d2d40a978da8ae8e2fa 100644 --- a/Source/MultiLine/Public/MultiLineActor.h +++ b/Source/MultiLine/Public/MultiLineActor.h @@ -12,26 +12,28 @@ UCLASS() class MULTILINE_API AMultiLineActor : public AActor { GENERATED_BODY() - -public: + +public: // Sets default values for this actor's properties AMultiLineActor(); - void CreateMesh(TArray<LineProps> line_props_vec, bool has_collision_context = false); + void CreateMesh(TArray<LineProps> & line_props_vec, bool has_collision_context = false); - void UpdateMesh(TArray<LineProps> line_props_vec); + void UpdateMesh(TArray<LineProps> & line_props_vec); UFUNCTION(BlueprintCallable, Category = "SunShine") void SetFlatShading(bool use_flat_shading); + virtual void Destroyed() override; + protected: // Called when the game starts or when spawned virtual void BeginPlay() override; - void CreateVertices(LineProps line_props, FOccluderVertexArray & vertices); + void CreateVertices(LineProps & line_props, FOccluderVertexArray & vertices); void SetUpTriangles(size_t index_offset, TArray<int32> & triangles); -public: +public: // Called every frame virtual void Tick(float DeltaTime) override;