PointCloud.cpp 23.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
// Copyright 2018 Michal Cieciura. All Rights Reserved.

#include "PointCloud.h"
#include "PointCloudHelper.h"
#include "PointCloudShared.h"
#include "Misc/ScopedSlowTask.h"
#include "Materials/MaterialInstance.h"
#include "Materials/MaterialInstanceDynamic.h"
#include "Engine/Texture2D.h"
#include "UObject/ConstructorHelpers.h"
#include "Logging/MessageLog.h"

#define IS_PROPERTY(Name) PropertyChangedEvent.MemberProperty->GetName().Equals(#Name)

#define LOCTEXT_NAMESPACE "PointCloud"
#define MIN_SCALE 0.001f

/////////////////////////////////////////////////
// UPointCloudSettings

UPointCloudSettings::UPointCloudSettings(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
	, Offset(EPointCloudOffset::Center)
	, Translation(FVector::ZeroVector)
	, Scale(FVector::OneVector * 100)
	, bUseLowPrecision(false)
	, bVertexBufferPreloading(false)
	, DensityReductionDistance(0)
	, NoiseReductionDistance(0)
	, NoiseReductionDensity(5)
{
	//Make sure we are transactional to allow undo redo
	this->SetFlags(RF_Transactional);
	FileHeader = FPointCloudFileHeader();
}

void UPointCloudSettings::LoadFileHeader(const FString& Filename)
{
	FileHeader = FPointCloudHelper::ReadFileHeader(Filename);
	FileHeader.bEnabled = true;
}

#if WITH_EDITOR

bool UPointCloudSettings::CanEditChange(const UProperty* InProperty) const
{
	const bool ParentVal = Super::CanEditChange(InProperty);

	return ParentVal;
}

void UPointCloudSettings::PostEditChangeProperty(FPropertyChangedEvent & PropertyChangedEvent)
{
	if (PropertyChangedEvent.MemberProperty)
	{
	}

	Super::PostEditChangeProperty(PropertyChangedEvent);
}

#endif

/////////////////////////////////////////////////
// UPointCloudImportSettings

UPointCloudImportSettings::UPointCloudImportSettings(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
	, bImportAll(false)
{
}

/////////////////////////////////////////////////
// UPointCloud

UPointCloud::UPointCloud(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
	, SinglePolySpriteMinimumLOD(1)
	, MinimumNodePointCount(3)
	, bEnableLOD(true)
	, ViewDistance(2500.0f)
	, LODDistribution(1.6f)
	, LODReduction(0.5f)
	, PreferredNodeSize(100.0f)
	, RenderMethod(EPointCloudRenderMethod::Sprite_Lit_RGB)
	, ColorOverride(EPointCloudColorOverride::None)
	, Brightness(0)
	, Contrast(1)
	, Saturation(1)
	, Gamma(2.2f)
	, Color(FLinearColor::White)
	, Intensity(1)
	, SpriteSize(FVector2D(10, 150))
	, SpriteSizeBias(0.75f)
	, bLerpSpriteSize(true)
	, SinglePolyScale(FVector4(1, 1, 3, 1))
	, SpriteMask(EPointCloudSpriteMask::None)
	, bDebugLOD(false)
	, Octree()
	, bDirty(true)
	, bTransformDirty(true)
	, bReductionDirty(true)
	, bRenderDataDirty(true)
	, bTreeDirty(true)
	, bMaterialDirty(true)
	, bWarningScale(true)
	, bWarningColor(true)
{
	static ConstructorHelpers::FObjectFinder<UMaterial> M_PointCloud(TEXT("/PointCloudPlugin/Materials/M_PointCloud"));
	MasterMaterial = M_PointCloud.Object;
	RenderData.PointCloud = this;
	Octree.SetPointCloud(this);
}

UPointCloud::~UPointCloud()
{
}

void UPointCloud::Serialize(FArchive& Ar)
{
	DECLARE_SCOPE_CYCLE_COUNTER(TEXT("UPointCloud::Serialize"), STAT_PointCLoud_Serialize, STATGROUP_LoadTime);

	Super::Serialize(Ar);
	
	// Do not run full serialization if only changing properties
	if (Ar.IsTransacting())
	{
		return;
	}	

	bool bCooked = Ar.IsCooking();
	bool bLoading = Ar.IsLoading();
	Ar << bCooked;

	Ar << ColorMode;

	// Flags
	/* Do not use the first bit, for backward compatibility purposes. */
	uint8 Flags = (uint8)(bUsesSprites * 2) + (uint8)(bUseLowPrecision * 4);
	Ar << Flags;

	// Num Points
	int32 NumPoints = GetPointCount(bCooked);
	Ar << NumPoints;

	if (bLoading)
	{
		bUsesSprites = ((Flags & 2) == 2);
		bUseLowPrecision = ((Flags & 4) == 4);

		Points.AddUninitialized(NumPoints);
	}

	for (int32 i = 0; i < (bLoading ? NumPoints : Points.Num()); i++)
	{
		// Skip unnecessary points when saving cooked version
		if (!bLoading && bCooked && !Points[i].IsEnabled())
		{
			continue;
		}

		Ar << Points[i].OriginalLocation;
		Ar << Points[i].Color;

		// Flags
		if (!bCooked)
		{
			Ar << Points[i].Flags;
		}

		if (bLoading && bCooked)
		{
			Points[i].SetEnabled(true);
		}
	}

	if (bLoading)
	{
		bTransformDirty = true;
		bTreeDirty = true;
		bReductionDirty = true;
		bDirty = true;
		bMaterialDirty = true;
		bRenderDataDirty = true;
	}
}

void UPointCloud::GetAssetRegistryTags(TArray<FAssetRegistryTag>& OutTags) const
{
	OutTags.Add(FAssetRegistryTag("PointCountTotal", PointCloudAssetRegistryCache.PointCountTotal, FAssetRegistryTag::TT_Numerical));
	OutTags.Add(FAssetRegistryTag("PointCountEnabled", PointCloudAssetRegistryCache.PointCountEnabled, FAssetRegistryTag::TT_Numerical));
	OutTags.Add(FAssetRegistryTag("ApproxSize", PointCloudAssetRegistryCache.ApproxSize, FAssetRegistryTag::TT_Dimensional));
	OutTags.Add(FAssetRegistryTag("Usage", PointCloudAssetRegistryCache.Usage, FAssetRegistryTag::TT_Alphabetical));

	Super::GetAssetRegistryTags(OutTags);
}

void UPointCloud::PostLoad()
{
	Super::PostLoad();
	Rebuild();
}

void UPointCloud::BeginDestroy()
{
	Super::BeginDestroy();
	RenderData.ReleaseResources(EVBReleaseMode::Full);
}

bool UPointCloud::IsReadyForFinishDestroy()
{
	return ReleaseResourcesFence.IsFenceComplete();
}

#if WITH_EDITOR

bool UPointCloud::CanEditChange(const UProperty* InProperty) const
{
	const bool ParentVal = Super::CanEditChange(InProperty);

	//if (InProperty->GetFName() == GET_MEMBER_NAME_CHECKED(UPointCloud, bPreloadVertexBuffer))
	//{
	//	return false;
	//}

	return ParentVal;
}

void UPointCloud::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
	bool bCommonDirty = true;

	if (PropertyChangedEvent.MemberProperty)
	{
		LODReduction = FMath::Clamp(LODReduction, 0.0f, 1.0f);
		ViewDistance = FMath::Max(ViewDistance, 1.0f);
		LODDistribution = FMath::Max(LODDistribution, 1.0f);
		MinimumNodePointCount = FMath::Max(MinimumNodePointCount, 1);
		PreferredNodeSize = FMath::Max(PreferredNodeSize, 1.0f);
		SpriteSizeBias = FMath::Max(SpriteSizeBias, 0.0f);

		if (IS_PROPERTY(bDebugLOD))
		{
			bCommonDirty = false;
		}

		if (IS_PROPERTY(SourcePath))
		{
			SetNewSourcePath(SourcePath.FilePath);
		}

		if (IS_PROPERTY(DensityReductionDistance) || IS_PROPERTY(NoiseReductionDistance) || IS_PROPERTY(NoiseReductionDensity))
		{
			bReductionDirty = true;
		}

		if (IS_PROPERTY(ViewDistance) || IS_PROPERTY(LODDistribution))
		{
			bCommonDirty = false;
			CalculateDistanceThresholds();
			ApplyRenderingParameters();
		}

		if (IS_PROPERTY(LODReduction) || IS_PROPERTY(MinimumNodePointCount) || IS_PROPERTY(SinglePolySpriteMinimumLOD))
		{
			bTreeDirty = true;
		}

		if (IS_PROPERTY(PreferredNodeSize) || IS_PROPERTY(bEnableLOD))
		{
			bTreeDirty = true;
			bMaterialDirty = true;
		}

		if (IS_PROPERTY(Offset) || IS_PROPERTY(Translation) || IS_PROPERTY(Scale))
		{
			bTransformDirty = true;
			bTreeDirty = true;
			bMaterialDirty = true;
		}

		if (IS_PROPERTY(Scale))
		{
			bWarningScale = true;
		}

		if (IS_PROPERTY(bUseLowPrecision))
		{
			bReductionDirty = true;
			bTransformDirty = true;
		}

		if (IS_PROPERTY(Brightness) || IS_PROPERTY(Contrast) || IS_PROPERTY(Intensity) || IS_PROPERTY(Saturation) || IS_PROPERTY(Gamma) || IS_PROPERTY(Color) || IS_PROPERTY(SpriteSizeBias) || IS_PROPERTY(SpriteSize) || IS_PROPERTY(ColorOverride) || IS_PROPERTY(SpriteMask) || IS_PROPERTY(SpriteTexture) || IS_PROPERTY(SinglePolyScale) || IS_PROPERTY(bLerpSpriteSize))
		{
			ApplyRenderingParameters();
			bCommonDirty = false;
		}
		
		if (IS_PROPERTY(CustomMaterial))
		{
			bMaterialDirty = true;
		}

		if (IS_PROPERTY(bVertexBufferPreloading))
		{
			bRenderDataDirty = true;
		}

		if (IS_PROPERTY(RenderMethod))
		{
			// Only require full rebuild if Point/Sprite mode switched, otherwise simply update material params
			DetermineSpriteUsage();
			if (bUsesSprites == bUsesSpritesCached)
			{
				ApplyRenderingParameters();
				bCommonDirty = false;
			}
			else
			{
				bTreeDirty = true;
				bRenderDataDirty = true;
				bMaterialDirty = true;
			}

			bWarningColor = true;
			ProcessWarningMessages();
		}

		if (IS_PROPERTY(FileHeader) || IS_PROPERTY(SourcePath))
		{
			bCommonDirty = false;
		}

		if (bCommonDirty)
		{
			bDirty = true;
			OnPointCloudChangedEvent.Broadcast();
		}
	}

	Super::PostEditChangeProperty(PropertyChangedEvent);
}
#endif

void UPointCloud::ApplyRenderingParameters()
{
	bool bUnlit = !UsesLit();
	bool bRGB = UsesRGB();

	FMaterialInstanceBasePropertyOverrides Overrides;
	Overrides.bOverride_ShadingModel = bUnlit;
	Overrides.ShadingModel = EMaterialShadingModel::MSM_Unlit;

	FVector BoundsSize = GetBounds().GetBox().GetSize();
	if (BoundsSize == FVector::ZeroVector)
	{
		BoundsSize = FVector::OneVector;
	}

	float LODFactor = LODReductionCached < 1 ? SpriteSizeBias / (1 - LODReductionCached) : 0;
	FVector4 ScaleAndOffset = FVector4(FVector2D(SinglePolyScale.Y + SinglePolyScale.W, SinglePolyScale.X + SinglePolyScale.Z) * 0.5f, FVector2D(SinglePolyScale.Y - SinglePolyScale.W, SinglePolyScale.X - SinglePolyScale.Z) * 0.25f);

	for (int32 i = 0; i < GetNumMaterials(); i++)
	{
		UMaterialInstanceDynamic *DynMaterial = Cast<UMaterialInstanceDynamic>(Materials[i]);
		if (DynMaterial)
		{
			DynMaterial->SetScalarParameterValue("PC__Brightness", Brightness);
			DynMaterial->SetScalarParameterValue("PC__Contrast", Contrast);
			DynMaterial->SetScalarParameterValue("PC__Saturation", Saturation);
			DynMaterial->SetScalarParameterValue("PC__Gamma", Gamma);
			DynMaterial->SetVectorParameterValue("PC__Color", bUseLODColoration ? FPointCloudHelper::GetLODColors(i) : Color);
			DynMaterial->SetScalarParameterValue("PC__Intensity", Intensity);
			DynMaterial->SetScalarParameterValue("PC__UseRGB", bRGB);
			DynMaterial->SetScalarParameterValue("PC__UseLODColoration", bUseLODColoration);
			DynMaterial->SetScalarParameterValue("PC__UseLerping", bLerpSpriteSize);
			DynMaterial->SetScalarParameterValue("PC__UseEmissive", bUnlit);
			DynMaterial->SetScalarParameterValue("PC__UseSprites", bUsesSpritesCached);
			DynMaterial->SetScalarParameterValue("PC__UseWorldColor", ColorOverride == EPointCloudColorOverride::Position);
			DynMaterial->SetScalarParameterValue("PC__UseHeightColor", ColorOverride == EPointCloudColorOverride::Height);
			DynMaterial->SetScalarParameterValue("PC__UseCircle", SpriteMask == EPointCloudSpriteMask::Circle);
			DynMaterial->SetScalarParameterValue("PC__UseMask", SpriteMask == EPointCloudSpriteMask::Texture);
			DynMaterial->SetVectorParameterValue("PC__Bounds", BoundsSize);
			DynMaterial->SetScalarParameterValue("PC__UseTexture", IsValid(SpriteTexture));
			if (SpriteTexture)
			{
				DynMaterial->SetTextureParameterValue("PC__Texture", SpriteTexture);
			}

			bool bUseSinglePolySprites = SinglePolySpriteMinimumLODCached <= i;
			float Size = FMath::Min(SpriteSize.X * FMath::Pow(LODFactor, i), SpriteSize.Y);

			DynMaterial->SetScalarParameterValue("PC__UseSinglePolySprites", bUseSinglePolySprites);
			DynMaterial->SetVectorParameterValue("PC__SizeAndOffset", (bUseSinglePolySprites ? FLinearColor(ScaleAndOffset.X, ScaleAndOffset.Y, ScaleAndOffset.Z, ScaleAndOffset.W) : FLinearColor(1, 1, 0, 0)) * Size);
			
			// Lerping
			// FinalSize = Size * (1 + (x - LODStartDistance) * LerpMulti)
			float LerpMulti = 0;
			float DistanceStart = i > 0 ? DistanceThresholds[i - 1] : 0;
			if (i < GetNumMaterials() - 1)
			{
				// LerpMulti = DeltaSize / (DeltaDistace * Size)
				LerpMulti = (FMath::Min(SpriteSize.X * FMath::Pow(LODFactor, i + 1), SpriteSize.Y) - Size) / ((DistanceThresholds[i] - DistanceStart) * Size);
			}
			DynMaterial->SetScalarParameterValue("PC__LODStartDistance", DistanceStart);
			DynMaterial->SetScalarParameterValue("PC__LerpMulti", LerpMulti);

			DynMaterial->BasePropertyOverrides = Overrides;
		}
	}
}

void UPointCloud::SetLODColoration(bool bEnable)
{
	if (bUseLODColoration != bEnable)
	{
		bUseLODColoration = bEnable;
		ApplyRenderingParameters();
	}
}

int32 UPointCloud::GetPointCount(bool bCountOnlyEnabled) const
{
	return bCountOnlyEnabled ? FPointCloudHelper::CountEnabledPoints(Points) : Points.Num();
}

void UPointCloud::Rebuild(bool bForced)
{
	if (bForced)
	{
		bRenderDataDirty = bTransformDirty = bReductionDirty = bTreeDirty = bMaterialDirty = true;
	}

	bTransformDirty |= bReductionDirty;
	bRenderDataDirty |= bTransformDirty;
	bTreeDirty |= bReductionDirty;
	bool bBoundsDirty = bTransformDirty;

	float MaxProgress = (2 * bTransformDirty) + (2 * bReductionDirty) + bTreeDirty + bMaterialDirty + bBoundsDirty + bRenderDataDirty + 1;

	FScopedSlowTask Progress(MaxProgress, LOCTEXT("Rebuild", "Rebuilding Cloud..."));
	Progress.MakeDialog();

	if (bTransformDirty)
	{
		Progress.EnterProgressFrame(1.f, LOCTEXT("RebuildPrep", "Preparing Data"));

		// Apply Min constraints to scale
		if (FMath::Abs(Scale.X) < MIN_SCALE) Scale.X = Scale.X >= 0 ? MIN_SCALE : -MIN_SCALE;
		if (FMath::Abs(Scale.Y) < MIN_SCALE) Scale.Y = Scale.Y >= 0 ? MIN_SCALE : -MIN_SCALE;
		if (FMath::Abs(Scale.Z) < MIN_SCALE) Scale.Z = Scale.Z >= 0 ? MIN_SCALE : -MIN_SCALE;

		// Flips X axis
		FVector CorrectedScale = FVector(-Scale.X, Scale.Y, Scale.Z);

		if (bReductionDirty)
		{
			// Prepare the points to affect reduction distances correctly.
			for (int32 i = 0; i < Points.Num(); i++)
			{
				Points[i].Location = Points[i].OriginalLocation * CorrectedScale;
461
				//Points[i].SetEnabled(true);
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
			}

			Progress.EnterProgressFrame(1.f, LOCTEXT("RebuildDensity", "Reducing Density"));
			FPointCloudHelper::DensityReduction(Points, DensityReductionDistance);
			Progress.EnterProgressFrame(1.f, LOCTEXT("RebuildNoise", "Reducing Noise"));
			FPointCloudHelper::NoiseReduction(Points, NoiseReductionDistance, NoiseReductionDensity);
			bReductionDirty = false;

			// Refresh VertexIndex properties
			for (int32 i = 0, idx = 0; i < Points.Num(); i++)
			{
				Points[i].VertexIndex = Points[i].IsEnabled() ? idx++ : -1;
			}
		}

		Progress.EnterProgressFrame(1.f, LOCTEXT("RebuildTransform", "Applying Transformations"));

		for (int i = 0; i < Points.Num(); i++)
		{
			Points[i].Location = Points[i].OriginalLocation;
		}

		FPointCloudHelper::Transform(Points, Offset, Translation, CorrectedScale, UsesLowPrecision());
		bTransformDirty = false;
	}
	
	if (bBoundsDirty)
	{
		Progress.EnterProgressFrame(1.f, LOCTEXT("RebuildBounds", "Calculating Bounds"));

		FBox BoundingBox = FPointCloudHelper::CalculateBounds(Points);
		LocalBounds.BoxExtent = BoundingBox.GetExtent();
		LocalBounds.Origin = BoundingBox.GetCenter();
		LocalBounds.SphereRadius = LocalBounds.BoxExtent.Size();
	}

	// Re-calculate new LOD Count
	LODCountCached = bEnableLOD ? FMath::Max(0, FMath::CeilToInt(FMath::Log2(GetBounds().BoxExtent.GetMax() / PreferredNodeSize))) + 1 : 1;

	if (bMaterialDirty)
	{
		Progress.EnterProgressFrame(1.f, LOCTEXT("RebuildRenderData", "Building Materials"));

		// Using add/remove approach to avoid crashes due to render thread still referencing the data
		int32 DeltaMaterials = GetLODCount() - Materials.Num();
		
		// Add new material slots
		if (DeltaMaterials > 0)
		{
			for (int32 i = 0; i < DeltaMaterials; i++)
			{
				Materials.Add(UMaterialInstanceDynamic::Create(CustomMaterial ? CustomMaterial : MasterMaterial, nullptr));
			}
		}
		// Remove unnecessary slots
		else if (DeltaMaterials < 0)
		{
			Materials.RemoveAt(GetLODCount(), -DeltaMaterials);
		}

		for (int32 i = 0; i < GetLODCount(); i++)
		{
			Materials[i] = UMaterialInstanceDynamic::Create(CustomMaterial ? CustomMaterial : MasterMaterial, nullptr);
		}

		DetermineSpriteUsage();
		bUsesSpritesCached = bUsesSprites;

		bMaterialDirty = false;
	}

	if (bRenderDataDirty)
	{
		Progress.EnterProgressFrame(1.f, LOCTEXT("RebuildRenderData", "Building Render Data"));

		BuildRenderData();

		bRenderDataDirty = false;
	}

	if (bTreeDirty)
	{
		Progress.EnterProgressFrame(1.f, LOCTEXT("RebuildTree", "Building Tree"));
		
		CalculateDistanceThresholds();

		Octree.Rebuild();
		Octree.PrintStats();	// #debug

		// If there are valid points but no tree data has been generated, indicate something went wrong
		if (GetPointCount(true) && !Octree.HasData())
		{
			FMessageLog MessageLog("AssetCheck");
			MessageLog.Error(FText::FromString(TEXT("No Octree data has been built! Something went wrong, please report this.")));
			MessageLog.Open();
		}

		LODReductionCached = LODReduction;
		SinglePolySpriteMinimumLODCached = SinglePolySpriteMinimumLOD;
		bTreeDirty = false;
	}

	Progress.EnterProgressFrame(1.f, LOCTEXT("RebuildFinalize", "Finalizing Data"));

	ApplyRenderingParameters();

	bDirty = false;
	OnPointCloudChangedEvent.Broadcast();
	OnPointCloudRebuiltEvent.Broadcast();

	CacheAssetRegistryTags();

	ProcessWarningMessages();
}

void UPointCloud::Bake()
{
	// Remove existing components?

	FScopedSlowTask Progress(1.f, LOCTEXT("Bake", "Baking Cloud..."));
	Progress.MakeDialog();

	Progress.EnterProgressFrame(1.f, LOCTEXT("Bake1", "Baking Cloud..."));
	
	TArray<FPointCloudPoint> NewPoints;
	NewPoints.AddUninitialized(FPointCloudHelper::CountEnabledPoints(Points));
	for (int32 i = 0, idx = 0; i < Points.Num(); i++)
	{
		if (Points[i].IsEnabled())
		{
			NewPoints[idx++] = Points[i];
		}
	}
	Points = NewPoints;
	
	bDirty = true;
	bRenderDataDirty = true;
	bTreeDirty = true;
	bMaterialDirty = true;
	OnPointCloudChangedEvent.Broadcast();
}

bool UPointCloud::Reimport(bool bRebuild)
{
	if (FPaths::FileExists(SourcePath.FilePath))
	{
		FScopedSlowTask Progress(1, LOCTEXT("Reimport", "Reimporting Cloud..."));
		Progress.MakeDialog();
		Progress.EnterProgressFrame(1.f);

		if (FPointCloudHelper::ImportAuto(SourcePath.FilePath, Points, ColorMode, (UPointCloudImportSettings*)(UPointCloudSettings*)this))
		{
			bReductionDirty = true;
			bDirty = true;
			bTransformDirty = true;
			bRenderDataDirty = true;
			bTreeDirty = true;
			bMaterialDirty = true;
			OnPointCloudChangedEvent.Broadcast();

			bWarningScale = true;

			AdjustColorMode();
			AdjustPerformanceSettings();

			if (bRebuild)
			{
				Rebuild();
			}

			return true;
		}
		else
		{
			PC_ERROR("Reimport failed.");
		}
	}
	else
	{
		PC_ERROR("Reimport failed, provided source path '%s' could not be found.", *SourcePath.FilePath);
	}

	return false;
}

void UPointCloud::SetNewSourcePath(FString NewSourcePath, bool bProcess)
{
	SourcePath.FilePath = NewSourcePath;

	if (!SourcePath.FilePath.IsEmpty() && FPaths::FileExists(SourcePath.FilePath) && FPaths::IsRelative(SourcePath.FilePath))
	{
		SourcePath.FilePath = FPaths::ConvertRelativePathToFull(SourcePath.FilePath);
	}

	if (bProcess)
	{
		if (!SourcePath.FilePath.IsEmpty() && FPaths::FileExists(SourcePath.FilePath))
		{
			LoadFileHeader(SourcePath.FilePath);			
		}
		else
		{
			FileHeader = FPointCloudFileHeader();
		}

		OnPointCloudSourceChangedEvent.Broadcast();
	}
}

void UPointCloud::SetRenderingMethod(EPointCloudRenderMethod NewRenderMethod, bool bRebuild)
{
	RenderMethod = NewRenderMethod;

	// Only require full rebuild if Point/Sprite mode switched, otherwise simply update material params
	DetermineSpriteUsage();
	if (bUsesSprites == bUsesSpritesCached)
	{
		ApplyRenderingParameters();
	}
	else
	{
		bDirty = true;
		bTreeDirty = true;
		bMaterialDirty = true;

		if (bRebuild)
		{
			Rebuild();
		}
	}
}

void UPointCloud::SetCustomMaterial(UMaterialInterface *InCustomMaterial, bool bRebuild /*= false*/)
{
	CustomMaterial = InCustomMaterial;

	bMaterialDirty = true;
	bDirty = true;

	if (bRebuild)
	{
		Rebuild();
	}
}

void UPointCloud::SetPointCloudData(TArray<FPointCloudPoint> &InPoints, bool bRebuildCloud)
{
	// Only process if there is any data available
	if (InPoints.Num() > 0)
	{
		Points = InPoints;
		bRenderDataDirty = bTransformDirty = bReductionDirty = bTreeDirty = bMaterialDirty = true;

		AdjustPerformanceSettings();

		if (bRebuildCloud)
		{
			Rebuild();
		}
	}
}

void UPointCloud::SetSettings(UPointCloudSettings *Settings)
{
	if (Settings)
	{
		bUseLowPrecision = Settings->bUseLowPrecision;
		DensityReductionDistance = Settings->DensityReductionDistance;
		NoiseReductionDistance = Settings->NoiseReductionDistance;
		NoiseReductionDensity = Settings->NoiseReductionDensity;
		Offset = Settings->Offset;
		Translation = Settings->Translation;
		Scale = Settings->Scale;
		FileHeader = Settings->FileHeader;

		ApplyRenderingParameters();
	}
}

void UPointCloud::SetColorMode(EPointCloudColorMode NewMode)
{
	ColorMode = NewMode;
	AdjustColorMode();
}

void UPointCloud::CalculateDistanceThresholds()
{
	DistanceThresholds.Reset();
	
	for (int32 i = 0; i < GetLODCount(); i++)
	{
		DistanceThresholds.Add(ViewDistance * FMath::Pow(LODDistribution, i));
	}

	Octree.CalculateDistanceThresholds();
}

void UPointCloud::CacheAssetRegistryTags()
{
	FBoxSphereBounds Bounds = GetBounds();

	PointCloudAssetRegistryCache.PointCountTotal = FString::FromInt(GetPointCount(false));
	PointCloudAssetRegistryCache.PointCountEnabled = FString::FromInt(GetPointCount(true));
	PointCloudAssetRegistryCache.ApproxSize = FString::Printf(TEXT("%dx%dx%d"), FMath::RoundToInt(Bounds.BoxExtent.X * 2.0f), FMath::RoundToInt(Bounds.BoxExtent.Y * 2.0f), FMath::RoundToInt(Bounds.BoxExtent.Z * 2.0f));
	PointCloudAssetRegistryCache.Usage = UsesSprites() ? "Sprites" : "Points";
}

void UPointCloud::DetermineSpriteUsage()
{
	switch (RenderMethod)
	{
	case EPointCloudRenderMethod::Sprite_Unlit:
	case EPointCloudRenderMethod::Sprite_Unlit_RGB:
	case EPointCloudRenderMethod::Sprite_Lit:
	case EPointCloudRenderMethod::Sprite_Lit_RGB:
		bUsesSprites = true;
		break;

	default:
		bUsesSprites = false;
		break;
	}
}

void UPointCloud::BuildRenderData()
{
	// Release render data if existing
	RenderData.ReleaseResources(EVBReleaseMode::None);

	// Wait for the render data to be released
	ReleaseResourcesFence.BeginFence();
	ReleaseResourcesFence.Wait();

	// If we have registered proxies, init the resources automatically
	if (RenderData.HasRegisteredProxies() || bVertexBufferPreloading)
	{
		RenderData.InitResources();
	}
}

void UPointCloud::AdjustColorMode()
{
	if (UsesRGB() && ColorMode != EPointCloudColorMode::RGB)
	{
		switch (RenderMethod)
		{
		case EPointCloudRenderMethod::Point_Unlit_RGB:
			SetRenderingMethod(EPointCloudRenderMethod::Point_Unlit);
			break;

		case EPointCloudRenderMethod::Point_Lit_RGB:
			SetRenderingMethod(EPointCloudRenderMethod::Point_Lit);
			break;

		case EPointCloudRenderMethod::Sprite_Unlit_RGB:
			SetRenderingMethod(EPointCloudRenderMethod::Sprite_Unlit);
			break;

		case EPointCloudRenderMethod::Sprite_Lit_RGB:
			SetRenderingMethod(EPointCloudRenderMethod::Sprite_Lit);
			break;
		}
	}
}

void UPointCloud::AdjustPerformanceSettings()
{
	if (Points.Num() < 75000)
	{
		bEnableLOD = false;
		MinimumNodePointCount = Points.Num() < 50000 ? 1 : 2;

		FMessageLog MessageLog("AssetCheck");
		MessageLog.Info(FText::FromString(FString::Printf(TEXT("Point Cloud appears to be quite small.\nLOD system will be disabled by default (you can re-enable it in the Point Cloud settings).\nMinimum Node Point Count will be reduced to %d."), MinimumNodePointCount)));
		//MessageLog.Open();
	}
}

void UPointCloud::ProcessWarningMessages()
{
	FMessageLog MessageLog("AssetCheck");
	bool bOpenWindow = false;

	if (bWarningScale && LocalBounds.SphereRadius <= 200)
	{
		MessageLog.Info(FText::FromString(TEXT("The Point Cloud appears to be very small, did you use correct scale?")));

		if (Points.Num() > 100000)
		{
			MessageLog.PerformanceWarning(FText::FromString(TEXT("Small and dense clouds should have their Preferred Node Size reduced to improve performance.")));
		}

		if (UsesSprites())
		{
			MessageLog.Info(FText::FromString(TEXT("Smaller clouds may benefit from reduced Sprite Size")));
		}

		bWarningScale = false;
		bOpenWindow = true;
	}

	if (bWarningColor && ColorMode == EPointCloudColorMode::Intensity && UsesRGB())
	{
		MessageLog.Warning(FText::FromString(TEXT("The Point Cloud contains Intensity information but is set to use RGB Render Method - this may cause incorrect coloring results.")));
		bWarningColor = false;
		bOpenWindow = true;
	}

	if (bOpenWindow)
	{
		//MessageLog.Open();
	}
}

#undef LOCTEXT_NAMESPACE