From 181160d16a04026e167d351cd4f97c0e1e9d9f0d Mon Sep 17 00:00:00 2001 From: rdeioris Date: Wed, 6 Dec 2023 19:02:45 +0100 Subject: [PATCH] fixed BuildVolumeTexture --- .../Private/glTFRuntimeParserMaterials.cpp | 182 +++++++++--------- Source/glTFRuntime/Public/glTFRuntimeParser.h | 2 +- 2 files changed, 94 insertions(+), 90 deletions(-) diff --git a/Source/glTFRuntime/Private/glTFRuntimeParserMaterials.cpp b/Source/glTFRuntime/Private/glTFRuntimeParserMaterials.cpp index 4e06fd8a..80236fd9 100644 --- a/Source/glTFRuntime/Private/glTFRuntimeParserMaterials.cpp +++ b/Source/glTFRuntime/Private/glTFRuntimeParserMaterials.cpp @@ -93,76 +93,76 @@ UMaterialInterface* FglTFRuntimeParser::LoadMaterial_Internal(const int32 Index, } auto GetMaterialVector = [](const TSharedRef JsonMaterialObject, const FString& ParamName, const int32 Fields, bool& bHasParam, FLinearColor& ParamValue) - { - const TArray>* JsonValues; - if (JsonMaterialObject->TryGetArrayField(ParamName, JsonValues)) { - if (JsonValues->Num() != Fields) + const TArray>* JsonValues; + if (JsonMaterialObject->TryGetArrayField(ParamName, JsonValues)) { - return; - } + if (JsonValues->Num() != Fields) + { + return; + } - double Values[4]; - // default alpha - Values[3] = 1; + double Values[4]; + // default alpha + Values[3] = 1; - for (int32 Index = 0; Index < Fields; Index++) - { - (*JsonValues)[Index]->TryGetNumber(Values[Index]); - } + for (int32 Index = 0; Index < Fields; Index++) + { + (*JsonValues)[Index]->TryGetNumber(Values[Index]); + } - bHasParam = true; - ParamValue = FLinearColor(Values[0], Values[1], Values[2], Values[3]); - } - }; + bHasParam = true; + ParamValue = FLinearColor(Values[0], Values[1], Values[2], Values[3]); + } + }; auto GetMaterialTexture = [this, MaterialsConfig](const TSharedRef JsonMaterialObject, const FString& ParamName, const bool sRGB, UTexture2D*& ParamTextureCache, TArray& ParamMips, FglTFRuntimeTextureTransform& ParamTransform, FglTFRuntimeTextureSampler& Sampler, const bool bForceNormalMapCompression) -> const TSharedPtr - { - const TSharedPtr* JsonTextureObject; - if (JsonMaterialObject->TryGetObjectField(ParamName, JsonTextureObject)) { - int64 TextureIndex; - if (!(*JsonTextureObject)->TryGetNumberField("index", TextureIndex)) + const TSharedPtr* JsonTextureObject; + if (JsonMaterialObject->TryGetObjectField(ParamName, JsonTextureObject)) { - return nullptr; - } + int64 TextureIndex; + if (!(*JsonTextureObject)->TryGetNumberField("index", TextureIndex)) + { + return nullptr; + } - if (!(*JsonTextureObject)->TryGetNumberField("texCoord", ParamTransform.TexCoord)) - { - ParamTransform.TexCoord = 0; - } + if (!(*JsonTextureObject)->TryGetNumberField("texCoord", ParamTransform.TexCoord)) + { + ParamTransform.TexCoord = 0; + } - ParamTransform.Rotation = (1.0 / (PI * 2)) * GetJsonExtensionObjectNumber(JsonTextureObject->ToSharedRef(), "KHR_texture_transform", "rotation", 0) * -1; - TArray Offset = GetJsonExtensionObjectNumbers(JsonTextureObject->ToSharedRef(), "KHR_texture_transform", "offset"); - if (Offset.Num() >= 2) - { - ParamTransform.Offset = FLinearColor(Offset[0], Offset[1], 0, 0); - } - TArray Scale = GetJsonExtensionObjectNumbers(JsonTextureObject->ToSharedRef(), "KHR_texture_transform", "scale"); - if (Scale.Num() >= 2) - { - ParamTransform.Scale = FLinearColor(Scale[0], Scale[1], 1, 1); - } - ParamTransform.TexCoord = GetJsonExtensionObjectIndex(JsonTextureObject->ToSharedRef(), "KHR_texture_transform", "texCoord", ParamTransform.TexCoord); + ParamTransform.Rotation = (1.0 / (PI * 2)) * GetJsonExtensionObjectNumber(JsonTextureObject->ToSharedRef(), "KHR_texture_transform", "rotation", 0) * -1; + TArray Offset = GetJsonExtensionObjectNumbers(JsonTextureObject->ToSharedRef(), "KHR_texture_transform", "offset"); + if (Offset.Num() >= 2) + { + ParamTransform.Offset = FLinearColor(Offset[0], Offset[1], 0, 0); + } + TArray Scale = GetJsonExtensionObjectNumbers(JsonTextureObject->ToSharedRef(), "KHR_texture_transform", "scale"); + if (Scale.Num() >= 2) + { + ParamTransform.Scale = FLinearColor(Scale[0], Scale[1], 1, 1); + } + ParamTransform.TexCoord = GetJsonExtensionObjectIndex(JsonTextureObject->ToSharedRef(), "KHR_texture_transform", "texCoord", ParamTransform.TexCoord); - if (ParamTransform.TexCoord < 0 || ParamTransform.TexCoord > 3) - { - AddError("LoadMaterial_Internal()", FString::Printf(TEXT("Invalid UV Set for %s: %d"), *ParamName, ParamTransform.TexCoord)); - return nullptr; - } + if (ParamTransform.TexCoord < 0 || ParamTransform.TexCoord > 3) + { + AddError("LoadMaterial_Internal()", FString::Printf(TEXT("Invalid UV Set for %s: %d"), *ParamName, ParamTransform.TexCoord)); + return nullptr; + } - // hack for allowing BC5 compression for plugins - if (bForceNormalMapCompression) - { - FglTFRuntimeImagesConfig& ImagesConfig = const_cast(MaterialsConfig.ImagesConfig); - ImagesConfig.Compression = TextureCompressionSettings::TC_Normalmap; - } + // hack for allowing BC5 compression for plugins + if (bForceNormalMapCompression) + { + FglTFRuntimeImagesConfig& ImagesConfig = const_cast(MaterialsConfig.ImagesConfig); + ImagesConfig.Compression = TextureCompressionSettings::TC_Normalmap; + } - ParamTextureCache = LoadTexture(TextureIndex, ParamMips, sRGB, MaterialsConfig, Sampler); - return *JsonTextureObject; - } - return nullptr; - }; + ParamTextureCache = LoadTexture(TextureIndex, ParamMips, sRGB, MaterialsConfig, Sampler); + return *JsonTextureObject; + } + return nullptr; + }; const TSharedPtr* JsonPBRObject; if (JsonMaterialObject->TryGetObjectField("pbrMetallicRoughness", JsonPBRObject)) @@ -388,7 +388,7 @@ UTexture2D* FglTFRuntimeParser::BuildTexture(UObject* Outer, const TArray& Mips, const int32 TileX, const int32 TileY, const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler) +UVolumeTexture* FglTFRuntimeParser::BuildVolumeTexture(UObject* Outer, const TArray& Mips, const int32 TileZ, const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler) { if (Mips.Num() == 0) { @@ -400,6 +400,7 @@ UVolumeTexture* FglTFRuntimeParser::BuildVolumeTexture(UObject* Outer, const TAr PlatformData->SizeX = Mips[0].Width; PlatformData->SizeY = Mips[0].Height; PlatformData->PixelFormat = Mips[0].PixelFormat; + PlatformData->SetNumSlices(TileZ); #if ENGINE_MAJOR_VERSION > 4 Texture->SetPlatformData(PlatformData); @@ -421,6 +422,7 @@ UVolumeTexture* FglTFRuntimeParser::BuildVolumeTexture(UObject* Outer, const TAr PlatformData->Mips.Add(Mip); Mip->SizeX = MipMap.Width; Mip->SizeY = MipMap.Height; + Mip->SizeZ = TileZ; #if !WITH_EDITOR #if !NO_LOGGING @@ -468,8 +470,10 @@ UVolumeTexture* FglTFRuntimeParser::BuildVolumeTexture(UObject* Outer, const TAr Texture->Filter = Sampler.MagFilter; } - Texture->Source2DTileSizeX = TileX; - Texture->Source2DTileSizeY = TileY; +#if WITH_EDITOR + Texture->Source2DTileSizeX = Mips[0].Width; + Texture->Source2DTileSizeY = Mips[0].Height; +#endif Texture->UpdateResource(); @@ -578,45 +582,45 @@ UMaterialInterface* FglTFRuntimeParser::BuildMaterial(const int32 Index, const F Material->SetScalarParameterValue("alphaCutoff", RuntimeMaterial.AlphaCutoff); auto ApplyMaterialFactor = [this, Material](bool bHasFactor, const FName& FactorName, FLinearColor FactorValue) - { - if (bHasFactor) { - Material->SetVectorParameterValue(FactorName, FactorValue); - } - }; + if (bHasFactor) + { + Material->SetVectorParameterValue(FactorName, FactorValue); + } + }; auto ApplyMaterialFloatFactor = [this, Material](bool bHasFactor, const FName& FactorName, float FactorValue) - { - if (bHasFactor) { - Material->SetScalarParameterValue(FactorName, FactorValue); - } - }; + if (bHasFactor) + { + Material->SetScalarParameterValue(FactorName, FactorValue); + } + }; auto ApplyMaterialTexture = [this, Material, MaterialsConfig](const FName& TextureName, UTexture2D* TextureCache, const TArray& Mips, const FglTFRuntimeTextureSampler& Sampler, const FString& TransformPrefix, const FglTFRuntimeTextureTransform& Transform, const TEnumAsByte Compression, const bool sRGB) - { - UTexture2D* Texture = TextureCache; - if (!Texture) { - if (Mips.Num() > 0) + UTexture2D* Texture = TextureCache; + if (!Texture) { - FglTFRuntimeImagesConfig ImagesConfig = MaterialsConfig.ImagesConfig; - ImagesConfig.Compression = Compression; - ImagesConfig.bSRGB = sRGB; - Texture = BuildTexture(Material, Mips, ImagesConfig, Sampler); + if (Mips.Num() > 0) + { + FglTFRuntimeImagesConfig ImagesConfig = MaterialsConfig.ImagesConfig; + ImagesConfig.Compression = Compression; + ImagesConfig.bSRGB = sRGB; + Texture = BuildTexture(Material, Mips, ImagesConfig, Sampler); + } } - } - if (Texture) - { - Material->SetTextureParameterValue(TextureName, Texture); - FVector4 UVSet = FVector4(0, 0, 0, 0); - UVSet[Transform.TexCoord] = 1; - Material->SetVectorParameterValue(FName(TransformPrefix + "TexCoord"), FLinearColor(UVSet)); - Material->SetVectorParameterValue(FName(TransformPrefix + "Offset"), Transform.Offset); - Material->SetScalarParameterValue(FName(TransformPrefix + "Rotation"), Transform.Rotation); - Material->SetVectorParameterValue(FName(TransformPrefix + "Scale"), Transform.Scale); - } - }; + if (Texture) + { + Material->SetTextureParameterValue(TextureName, Texture); + FVector4 UVSet = FVector4(0, 0, 0, 0); + UVSet[Transform.TexCoord] = 1; + Material->SetVectorParameterValue(FName(TransformPrefix + "TexCoord"), FLinearColor(UVSet)); + Material->SetVectorParameterValue(FName(TransformPrefix + "Offset"), Transform.Offset); + Material->SetScalarParameterValue(FName(TransformPrefix + "Rotation"), Transform.Rotation); + Material->SetVectorParameterValue(FName(TransformPrefix + "Scale"), Transform.Scale); + } + }; ApplyMaterialFactor(RuntimeMaterial.bHasBaseColorFactor, "baseColorFactor", RuntimeMaterial.BaseColorFactor); ApplyMaterialTexture("baseColorTexture", RuntimeMaterial.BaseColorTextureCache, RuntimeMaterial.BaseColorTextureMips, diff --git a/Source/glTFRuntime/Public/glTFRuntimeParser.h b/Source/glTFRuntime/Public/glTFRuntimeParser.h index 5bdd5e9c..15bdfb2f 100644 --- a/Source/glTFRuntime/Public/glTFRuntimeParser.h +++ b/Source/glTFRuntime/Public/glTFRuntimeParser.h @@ -2106,7 +2106,7 @@ class GLTFRUNTIME_API FglTFRuntimeParser : public FGCObject, public TSharedFromT UTexture2D* BuildTexture(UObject* Outer, const TArray& Mips, const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler); UTextureCube* BuildTextureCube(UObject* Outer, const TArray& MipsXP, const TArray& MipsXN, const TArray& MipsYP, const TArray& MipsYN, const TArray& MipsZP, const TArray& MipsZN, const bool bAutoRotate, const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler); UTexture2DArray* BuildTextureArray(UObject* Outer, const TArray& Mips,const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler); - UVolumeTexture* BuildVolumeTexture(UObject* Outer, const TArray& Mips, const int32 TileX, const int32 TileY, const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler); + UVolumeTexture* BuildVolumeTexture(UObject* Outer, const TArray& Mips, const int32 TileZ, const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler); TArray MaterialsVariants;