Skip to content

Instantly share code, notes, and snippets.

@hanzochang
Created March 1, 2017 00:29
Show Gist options
  • Save hanzochang/9ba43ff0883f27e0a904b5c974ec4523 to your computer and use it in GitHub Desktop.
Save hanzochang/9ba43ff0883f27e0a904b5c974ec4523 to your computer and use it in GitHub Desktop.
#include "ProceduralMeshComponent.h"
// Creating a standard root object.
AMyActor::AMyActor()
{
mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
RootComponent = mesh;
/**
* Create/replace a section for this procedural mesh component.
* @param SectionIndex Index of the section to create or replace.
* @param Vertices Vertex buffer of all vertex positions to use for this mesh section.
* @param Triangles Index buffer indicating which vertices make up each triangle. Length must be a multiple of 3.
* @param Normals Optional array of normal vectors for each vertex. If supplied, must be same length as Vertices array.
* @param UV0 Optional array of texture co-ordinates for each vertex. If supplied, must be same length as Vertices array.
* @param VertexColors Optional array of colors for each vertex. If supplied, must be same length as Vertices array.
* @param Tangents Optional array of tangent vector for each vertex. If supplied, must be same length as Vertices array.
* @param bCreateCollision Indicates whether collision should be created for this section. This adds significant cost.
*/
//UFUNCTION(BlueprintCallable, Category = "Components|ProceduralMesh", meta = (AutoCreateRefTerm = "Normals,UV0,VertexColors,Tangents"))
// void CreateMeshSection(int32 SectionIndex, const TArray<FVector>& Vertices, const TArray<int32>& Triangles, const TArray<FVector>& Normals,
// const TArray<FVector2D>& UV0, const TArray<FColor>& VertexColors, const TArray<FProcMeshTangent>& Tangents, bool bCreateCollision);
TArray<FVector> vertices;
vertices.Add(FVector(0, 0, 0));
vertices.Add(FVector(0, 100, 0));
vertices.Add(FVector(0, 0, 100));
TArray<int32> Triangles;
Triangles.Add(0);
Triangles.Add(1);
Triangles.Add(2);
TArray<FVector> normals;
normals.Add(FVector(1, 0, 0));
normals.Add(FVector(1, 0, 0));
normals.Add(FVector(1, 0, 0));
TArray<FVector2D> UV0;
UV0.Add(FVector2D(0, 0));
UV0.Add(FVector2D(10, 0));
UV0.Add(FVector2D(0, 10));
TArray<FLinearColor> vertexColors;
vertexColors.Add(FLinearColor(0.75, 0.75, 0.75, 1.0));
vertexColors.Add(FLinearColor(0.75, 0.75, 0.75, 1.0));
vertexColors.Add(FLinearColor(0.75, 0.75, 0.75, 1.0));
TArray<FProcMeshTangent> tangents;
tangents.Add(FProcMeshTangent(0, 1, 0));
tangents.Add(FProcMeshTangent(0, 1, 0));
tangents.Add(FProcMeshTangent(0, 1, 0));
mesh->CreateMeshSection_LinearColor(1, vertices, Triangles, normals, UV0, vertexColors, tangents, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment