Skip to content

Commit

Permalink
fixed BuildVolumeTexture
Browse files Browse the repository at this point in the history
  • Loading branch information
rdeioris committed Dec 6, 2023
1 parent dc35c0c commit 181160d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 90 deletions.
182 changes: 93 additions & 89 deletions Source/glTFRuntime/Private/glTFRuntimeParserMaterials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,76 +93,76 @@ UMaterialInterface* FglTFRuntimeParser::LoadMaterial_Internal(const int32 Index,
}

auto GetMaterialVector = [](const TSharedRef<FJsonObject> JsonMaterialObject, const FString& ParamName, const int32 Fields, bool& bHasParam, FLinearColor& ParamValue)
{
const TArray<TSharedPtr<FJsonValue>>* JsonValues;
if (JsonMaterialObject->TryGetArrayField(ParamName, JsonValues))
{
if (JsonValues->Num() != Fields)
const TArray<TSharedPtr<FJsonValue>>* 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<FJsonObject> JsonMaterialObject, const FString& ParamName, const bool sRGB, UTexture2D*& ParamTextureCache, TArray<FglTFRuntimeMipMap>& ParamMips, FglTFRuntimeTextureTransform& ParamTransform, FglTFRuntimeTextureSampler& Sampler, const bool bForceNormalMapCompression) -> const TSharedPtr<FJsonObject>
{
const TSharedPtr<FJsonObject>* JsonTextureObject;
if (JsonMaterialObject->TryGetObjectField(ParamName, JsonTextureObject))
{
int64 TextureIndex;
if (!(*JsonTextureObject)->TryGetNumberField("index", TextureIndex))
const TSharedPtr<FJsonObject>* 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<double> Offset = GetJsonExtensionObjectNumbers(JsonTextureObject->ToSharedRef(), "KHR_texture_transform", "offset");
if (Offset.Num() >= 2)
{
ParamTransform.Offset = FLinearColor(Offset[0], Offset[1], 0, 0);
}
TArray<double> 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<double> Offset = GetJsonExtensionObjectNumbers(JsonTextureObject->ToSharedRef(), "KHR_texture_transform", "offset");
if (Offset.Num() >= 2)
{
ParamTransform.Offset = FLinearColor(Offset[0], Offset[1], 0, 0);
}
TArray<double> 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<FglTFRuntimeImagesConfig&>(MaterialsConfig.ImagesConfig);
ImagesConfig.Compression = TextureCompressionSettings::TC_Normalmap;
}
// hack for allowing BC5 compression for plugins
if (bForceNormalMapCompression)
{
FglTFRuntimeImagesConfig& ImagesConfig = const_cast<FglTFRuntimeImagesConfig&>(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<FJsonObject>* JsonPBRObject;
if (JsonMaterialObject->TryGetObjectField("pbrMetallicRoughness", JsonPBRObject))
Expand Down Expand Up @@ -388,7 +388,7 @@ UTexture2D* FglTFRuntimeParser::BuildTexture(UObject* Outer, const TArray<FglTFR
return Texture;
}

UVolumeTexture* FglTFRuntimeParser::BuildVolumeTexture(UObject* Outer, const TArray<FglTFRuntimeMipMap>& Mips, const int32 TileX, const int32 TileY, const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler)
UVolumeTexture* FglTFRuntimeParser::BuildVolumeTexture(UObject* Outer, const TArray<FglTFRuntimeMipMap>& Mips, const int32 TileZ, const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler)
{
if (Mips.Num() == 0)
{
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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<FglTFRuntimeMipMap>& Mips, const FglTFRuntimeTextureSampler& Sampler, const FString& TransformPrefix, const FglTFRuntimeTextureTransform& Transform, const TEnumAsByte<TextureCompressionSettings> 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,
Expand Down
2 changes: 1 addition & 1 deletion Source/glTFRuntime/Public/glTFRuntimeParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -2106,7 +2106,7 @@ class GLTFRUNTIME_API FglTFRuntimeParser : public FGCObject, public TSharedFromT
UTexture2D* BuildTexture(UObject* Outer, const TArray<FglTFRuntimeMipMap>& Mips, const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler);
UTextureCube* BuildTextureCube(UObject* Outer, const TArray<FglTFRuntimeMipMap>& MipsXP, const TArray<FglTFRuntimeMipMap>& MipsXN, const TArray<FglTFRuntimeMipMap>& MipsYP, const TArray<FglTFRuntimeMipMap>& MipsYN, const TArray<FglTFRuntimeMipMap>& MipsZP, const TArray<FglTFRuntimeMipMap>& MipsZN, const bool bAutoRotate, const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler);
UTexture2DArray* BuildTextureArray(UObject* Outer, const TArray<FglTFRuntimeMipMap>& Mips,const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler);
UVolumeTexture* BuildVolumeTexture(UObject* Outer, const TArray<FglTFRuntimeMipMap>& Mips, const int32 TileX, const int32 TileY, const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler);
UVolumeTexture* BuildVolumeTexture(UObject* Outer, const TArray<FglTFRuntimeMipMap>& Mips, const int32 TileZ, const FglTFRuntimeImagesConfig& ImagesConfig, const FglTFRuntimeTextureSampler& Sampler);


TArray<FString> MaterialsVariants;
Expand Down

0 comments on commit 181160d

Please sign in to comment.