Skip to content

Commit

Permalink
Added procedural mesh post code
Browse files Browse the repository at this point in the history
  • Loading branch information
orfeasel committed May 12, 2018
1 parent b8264e9 commit 29228e9
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 0 deletions.
Binary file added ProceduralMesh/BP_ProcMeshActor.uasset
Binary file not shown.
Binary file added ProceduralMesh/M_RuntimeMesh.uasset
Binary file not shown.
102 changes: 102 additions & 0 deletions ProceduralMesh/MyActor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Fill out your copyright notice in the Description page of Project Settings.

#include "MyActor.h"


// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;

CustomMesh = CreateDefaultSubobject<UProceduralMeshComponent>("CustomMesh");
SetRootComponent(CustomMesh);
CustomMesh->bUseAsyncCooking = true;

}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
GenerateCubeMesh();
}

void AMyActor::AddTriangle(int32 V1, int32 V2, int32 V3)
{
Triangles.Add(V1);
Triangles.Add(V2);
Triangles.Add(V3);
}


void AMyActor::GenerateCubeMesh()
{
//6 sides on cube, 4 verts each (corners)

//These are relative locations to the placed Actor in the world
Vertices.Add(FVector(0, -100, 0)); //lower left - 0
Vertices.Add(FVector(0, -100, 100)); //upper left - 1
Vertices.Add(FVector(0, 100, 0)); //lower right - 2
Vertices.Add(FVector(0, 100, 100)); //upper right - 3

Vertices.Add(FVector(100, -100, 0)); //lower front left - 4
Vertices.Add(FVector(100, -100, 100)); //upper front left - 5

Vertices.Add(FVector(100, 100, 100)); //upper front right - 6
Vertices.Add(FVector(100, 100, 0)); //lower front right - 7

//Back face of cube
AddTriangle(0, 2, 3); //normal face
//AddTriangle(3, 2, 0); // flipped face
AddTriangle(3, 1, 0);

//Left face of cube
AddTriangle(0, 1, 4);
AddTriangle(4, 1, 5);

//Front face of cube
AddTriangle(4, 5, 7);
AddTriangle(7, 5, 6);

//Right face of cube
AddTriangle(7, 6, 3);
AddTriangle(3, 2, 7);

//Top face
AddTriangle(1, 3, 5);
AddTriangle(6, 5, 3);

//bottom face
AddTriangle(2, 0, 4);
AddTriangle(4, 7, 2);

TArray<FLinearColor> VertexColors;
VertexColors.Add(FLinearColor(0.f, 0.f, 1.f));
VertexColors.Add(FLinearColor(1.f, 0.f, 0.f));
VertexColors.Add(FLinearColor(1.f, 0.f, 0.f));
VertexColors.Add(FLinearColor(0.f, 1.f, 0.f));
VertexColors.Add(FLinearColor(0.5f, 1.f, 0.5f));
VertexColors.Add(FLinearColor(0.f, 1.f, 0.f));
VertexColors.Add(FLinearColor(1.f, 1.f, 0.f));
VertexColors.Add(FLinearColor(0.f, 1.f, 1.f));

CustomMesh->CreateMeshSection_LinearColor(0, Vertices, Triangles, TArray<FVector>(), TArray<FVector2D>(), VertexColors, TArray<FProcMeshTangent>(),true);
}



// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

//Moving the mesh
for (int32 index = 0; index < Vertices.Num(); index++)
{
//Moving each vertex 1 unit forward
Vertices[index] += FVector(1, 0, 0);
}
CustomMesh->UpdateMeshSection(0, Vertices, TArray<FVector>(), TArray<FVector2D>(), TArray<FColor>(), TArray<FProcMeshTangent>());
}

42 changes: 42 additions & 0 deletions ProceduralMesh/MyActor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ProceduralMeshComponent.h"
#include "MyActor.generated.h"

UCLASS()
class PROCEDURALMESH_API AMyActor : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor's properties
AMyActor();

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

UPROPERTY(VisibleAnywhere,BlueprintReadWrite)
UProceduralMeshComponent* CustomMesh;

/* The vertices of the mesh */
TArray<FVector> Vertices;

/* The triangles of the mesh */
TArray<int32> Triangles;

/* Creates a triangle that connects the given vertices */
void AddTriangle(int32 V1, int32 V2, int32 V3);

void GenerateCubeMesh();

public:

// Called every frame
virtual void Tick(float DeltaTime) override;

};
14 changes: 14 additions & 0 deletions ProceduralMesh/ProceduralMesh.Build.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class ProceduralMesh : ModuleRules
{
public ProceduralMesh(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "ProceduralMeshComponent" });
//PrivateDependencyModuleNames.AddRange(new string[] { "ProceduralMeshComponent" });
}
}
17 changes: 17 additions & 0 deletions ProceduralMesh/ProceduralMesh.uproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"FileVersion": 3,
"EngineAssociation": "4.18",
"Category": "",
"Description": "",
"Modules": [
{
"Name": "ProceduralMesh",
"Type": "Runtime",
"LoadingPhase": "Default",
"AdditionalDependencies": [
"Engine",
"ProceduralMeshComponent"
]
}
]
}
3 changes: 3 additions & 0 deletions ProceduralMesh/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Procedural mesh tut

Read the full tutorial here: https://wp.me/p6hvtS-pE

0 comments on commit 29228e9

Please sign in to comment.