Download

why set material to ProceduralMeshComponent failed

my problem:“myMesh->SetMaterial(0, material);” seems to not work,myMesh always use the default material.You can see the result in the picture
QQ图片20160401004506.png
Here’s my code:


// Fill out your copyright notice in the Description page of Project Settings.

#include "ProceduralMeshTest.h"
#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;

	USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
	RootComponent = SphereComponent;

	myMesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
	myMesh->SetMobility(EComponentMobility::Movable);
	/**
	*	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));
	vertices.Add(FVector(0, 100, 100));

	TArray<int32> Triangles;
	Triangles.Add(0);
	Triangles.Add(1);
	Triangles.Add(2);
	Triangles.Add(3);
	Triangles.Add(2);
	Triangles.Add(1);

	TArray<FVector> normals;
	normals.Add(FVector(-1, 0, 0));
	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(0, 1));
	UV0.Add(FVector2D(1, 0));
	UV0.Add(FVector2D(1, 1));

	TArray<FColor> vertexColors;
	vertexColors.Add(FColor(0, 0, 0, 100));
	vertexColors.Add(FColor(0, 0, 0, 100));
	vertexColors.Add(FColor(100, 100, 100, 100));
	vertexColors.Add(FColor(100, 100, 100, 100));


	TArray<FProcMeshTangent> tangents;
	tangents.Add(FProcMeshTangent(1, 1, 1));
	tangents.Add(FProcMeshTangent(1, 1, 1));
	tangents.Add(FProcMeshTangent(1, 1, 1));
	tangents.Add(FProcMeshTangent(1, 1, 1));


	//myMesh->CreateMeshSection(1, vertices, Triangles, normals, UV0, vertexColors, tangents, false);
	myMesh->CreateMeshSection(1, vertices, Triangles, TArray<FVector>(), UV0, TArray<FColor>(), TArray<FProcMeshTangent>(), false);

	// With default options
	//mesh->CreateMeshSection(1, vertices, Triangles, TArray<FVector>(), TArray<FVector2D>(), TArray<FColor>(), TArray<FProcMeshTangent>(), false);

	myMesh->AttachTo(RootComponent);

}

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

	//FString MaterialAddress = "/Game/ArchVis/Materials/M_WallPainInterior_MAT_Inst.M_WallPainInterior_MAT_Inst";
	FString MaterialAddress = "Material'/Game/ArchVis/Materials/M_Carptet_Mat.M_Carptet_Mat'";
	UMaterial* material; 
	material = Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), nullptr, *MaterialAddress, nullptr, LOAD_None, nullptr));
	myMesh->SetMaterial(0, material);
}

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

}


Finally,i know why.It’s too simple.Just change “myMesh->CreateMeshSection(1” to “myMesh->CreateMeshSection(0”