Hey everyone,
I’m running into an issue with assigning a texture to a dynamically created mesh. I want to create some mesh then use c++ to generate a texture and assign it to the mesh. This simplified version is just two triangles and I am trying to assign a constant color (I will modify to read a file for my actual application to find RGB values). This seems like an easy task but even after reading through the various similar posts I seem to be at a loss and any help would be greatly appreciated!
Here is the header file:
#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include "GameFramework/Actor.h"
#include "ProceduralMeshComponent.h"
#include "Materials/MaterialInterface.h"
#include "MyProceduralMesh.generated.h"
UCLASS()
class MYPROJECT10_API AMyProceduralMesh : public AActor
{
GENERATED_BODY()
UPROPERTY(VisibleAnywhere, Category = "MyProceduralMesh")
UProceduralMeshComponent* pm;
public:
AMyProceduralMesh();
UPROPERTY(EditAnywhere)
UMaterialInterface* Material;
UPROPERTY()
UTexture2D* RuntimeTexture;
UPROPERTY()
TArray<FVector> vertices;
UPROPERTY()
TArray<FVector> normals;
UPROPERTY()
TArray<int32> triangles;
UPROPERTY()
TArray<FVector2D> uvs;
UPROPERTY()
TArray<FLinearColor> vertexColors;
//TArray<FColor> vertexColors;
UPROPERTY()
TArray<FProcMeshTangent> tangents;
virtual void OnConstruction(const FTransform& Transform) override;
void setmaterial();
void BeginPlay();
void ClearMeshData();
};
And the .cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyProceduralMesh.h"
// Sets default values
AMyProceduralMesh::AMyProceduralMesh()
{
PrimaryActorTick.bCanEverTick = true;
pm = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("ProceduralMesh"));
pm->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
}
// Called when the game starts or when spawned
void AMyProceduralMesh::BeginPlay()
{
Super::BeginPlay();
//setmaterial();
}
void AMyProceduralMesh::OnConstruction(const FTransform& Transform)
{
ClearMeshData();
vertices.Add(FVector(0.0f, 0.0f, 0.0f));
vertices.Add(FVector(1000.0f, 0.0f, 0.0f));
vertices.Add(FVector(0.0f, 1000.0f, 0.0f));
vertices.Add(FVector(1000.0f, 1000.0f, 0.0f));
triangles.Add(0);
triangles.Add(2);
triangles.Add(1);
triangles.Add(3);
triangles.Add(1);
triangles.Add(2);
uvs.Add(FVector2D(0.0f, 0.0f));
uvs.Add(FVector2D(1.0f, 0.0f));
uvs.Add(FVector2D(0.0f, 1.0f));
uvs.Add(FVector2D(1.0f, 1.0f));
pm->CreateMeshSection_LinearColor(0, vertices, triangles, normals, uvs, vertexColors, tangents, false);
setmaterial();
}
void AMyProceduralMesh::setmaterial()
{
UTexture2D* MyTexture;
MyTexture = UTexture2D::CreateTransient(64, 64, PF_R8G8B8A8);
MyTexture->UTexture::UpdateResource();
FColor* MipData = static_cast<FColor*>(MyTexture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE));
for (int32 pixelNum = 0; pixelNum < 64 * 64; ++pixelNum)
{
MipData[pixelNum] = FColor(255, 0, 0, 255);
}
MyTexture->PlatformData->Mips[0].BulkData.Unlock();
MyTexture->UpdateResource();
UMaterialInstanceDynamic* MI = UMaterialInstanceDynamic::Create(pm->GetMaterial(1), this);
MI->SetTextureParameterValue("Texture", MyTexture);
pm->SetMaterial(0, MI);
}
void AMyProceduralMesh::ClearMeshData()
{
vertices.Empty();
triangles.Empty();
uvs.Empty();
normals.Empty();
vertexColors.Empty();
tangents.Empty();
}