Skip to content

Commit

Permalink
Added code for runtime vertex painting
Browse files Browse the repository at this point in the history
  • Loading branch information
orfeasel committed Oct 18, 2018
1 parent 29228e9 commit ec61977
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
Binary file added RuntimeVertexPainting/BP_Cube.uasset
Binary file not shown.
Binary file added RuntimeVertexPainting/M_VertexColor.uasset
Binary file not shown.
3 changes: 3 additions & 0 deletions RuntimeVertexPainting/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-rh
23 changes: 23 additions & 0 deletions RuntimeVertexPainting/VertexPainting.Build.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Fill out your copyright notice in the Description page of Project Settings.

using UnrealBuildTool;

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

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "RenderCore" });

PrivateDependencyModuleNames.AddRange(new string[] { });

// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");

// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
46 changes: 46 additions & 0 deletions RuntimeVertexPainting/VertexPaintingFunctionLibrary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Fill out your copyright notice in the Description page of Project Settings.

#include "VertexPaintingFunctionLibrary.h"

void UVertexPaintingFunctionLibrary::PaintSMVertices(UStaticMeshComponent* SMComp)
{
if (!SMComp) return;

//Get the static mesh that we're going to paint
UStaticMesh* SM = SMComp->GetStaticMesh();
if (SM)
{
//Get the vertex buffer from the 1st lod
//FPositionVertexBuffer* PositionVertexBuffer = &SM->RenderData->LODResources[0].VertexBuffers.PositionVertexBuffer;

//Make sure that we have at least 1 LOD
SMComp->SetLODDataCount(1, SMComp->LODData.Num());
FStaticMeshComponentLODInfo* LODInfo = &SMComp->LODData[0]; //We're going to modify the 1st LOD only

//Empty the painted vertices and assign a new color vertex buffer which will contain the new colors for each vertex
LODInfo->PaintedVertices.Empty();
LODInfo->OverrideVertexColors = new FColorVertexBuffer();

//We're going to use the LODResources to get the total number of vertices that the provided mesh has
FStaticMeshLODResources& LodResources = SM->RenderData->LODResources[0];

//Creating a color array
TArray<FColor> RandomColorArray;
//Since we know beforehand the number of elements we might as well reserve the memory now
RandomColorArray.Reserve(LodResources.GetNumVertices() - 1);

for (int32 i = 0; i < LodResources.GetNumVertices(); i++)
{
//Generate a random color for the current vertex
RandomColorArray.Add(FColor::MakeRandomColor());
}

//Initialize the new vertex colros with the array we created above
LODInfo->OverrideVertexColors->InitFromColorArray(RandomColorArray);

//Initialize resource and mark render state of object as dirty in order for the engine to re-render it
BeginInitResource(LODInfo->OverrideVertexColors);
SMComp->MarkRenderStateDirty();
}
}

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

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "VertexPaintingFunctionLibrary.generated.h"

/**
*
*/
UCLASS()
class VERTEXPAINTING_API UVertexPaintingFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()

public:

UFUNCTION(BlueprintCallable, Category = VertexPainting)
static void PaintSMVertices(UStaticMeshComponent* SMComp);


};

0 comments on commit ec61977

Please sign in to comment.