Skip to content

Commit

Permalink
Merge pull request #1315 from CesiumGS/apply_khr_texture_transform
Browse files Browse the repository at this point in the history
Apply texture transformation in UMaterial instead of Native
  • Loading branch information
j9liu authored Jan 31, 2024
2 parents d6a83b7 + 02996a9 commit eb805fa
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Fixed several building warnings when packing in Unreal Engine 5.3.
- Readded backwards compatibility for feature textures from `EXT_feature_metadata`, which was mistakenly removed.
- Fixed a bug that caused nav mesh creation to be slow due to creating duplicate physics meshes.
- Apply `KHR_texture_transform` texture coordinate transformation in UMaterial instead of in native.

##### Additions :tada:

Expand Down
Binary file modified Content/Materials/MaterialFunctions/CesiumGlTFFunction.uasset
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "CesiumIonTokenTroubleshooting.h"
#include "Cesium3DTileset.h"
#include "CesiumCommon.h"
#include "CesiumEditor.h"
#include "CesiumIonClient/Connection.h"
#include "CesiumIonRasterOverlay.h"
Expand All @@ -18,7 +19,6 @@
#include "Widgets/Layout/SBorder.h"
#include "Widgets/Layout/SHeader.h"
#include "Widgets/Text/STextBlock.h"
#include "CesiumCommon.h"

using namespace CesiumIonClient;

Expand Down
2 changes: 2 additions & 0 deletions Source/CesiumRuntime/Private/Cesium3DTileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,8 @@ void ACesium3DTileset::LoadTileset() {
options.contentOptions.ktx2TranscodeTargets =
CesiumGltf::Ktx2TranscodeTargets(supportedFormats, false);

options.contentOptions.applyTextureTransform = false;

switch (this->TilesetSource) {
case ETilesetSource::FromUrl:
UE_LOG(LogCesium, Log, TEXT("Loading tileset from URL %s"), *this->Url);
Expand Down
126 changes: 126 additions & 0 deletions Source/CesiumRuntime/Private/CesiumGltfComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "CesiumGltf/AccessorView.h"
#include "CesiumGltf/ExtensionExtMeshFeatures.h"
#include "CesiumGltf/ExtensionKhrMaterialsUnlit.h"
#include "CesiumGltf/ExtensionKhrTextureTransform.h"
#include "CesiumGltf/ExtensionMeshPrimitiveExtStructuralMetadata.h"
#include "CesiumGltf/ExtensionModelExtFeatureMetadata.h"
#include "CesiumGltf/ExtensionModelExtStructuralMetadata.h"
Expand Down Expand Up @@ -1996,6 +1997,131 @@ static void SetGltfParameterValues(
FMaterialParameterInfo("occlusionTexture", association, index),
loadResult.occlusionTexture.Get());

const ExtensionKhrTextureTransform* pBaseColorTextureTransform =
pbr.baseColorTexture
? pbr.baseColorTexture->getExtension<ExtensionKhrTextureTransform>()
: nullptr;
const ExtensionKhrTextureTransform* pMetallicRoughnessTextureTransform =
pbr.metallicRoughnessTexture
? pbr.metallicRoughnessTexture
->getExtension<ExtensionKhrTextureTransform>()
: nullptr;

if (pBaseColorTextureTransform) {
pMaterial->SetVectorParameterValueByInfo(
FMaterialParameterInfo("baseColorScaleOffset", association, index),
FLinearColor(
pBaseColorTextureTransform->scale[0],
pBaseColorTextureTransform->scale[1],
pBaseColorTextureTransform->offset[0],
pBaseColorTextureTransform->offset[1]));
}

if (pMetallicRoughnessTextureTransform) {
pMaterial->SetVectorParameterValueByInfo(
FMaterialParameterInfo(
"metallicRoughnessScaleOffset",
association,
index),
FLinearColor(
pMetallicRoughnessTextureTransform->scale[0],
pMetallicRoughnessTextureTransform->scale[1],
pMetallicRoughnessTextureTransform->offset[0],
pMetallicRoughnessTextureTransform->offset[1]));
}

if (pBaseColorTextureTransform || pMetallicRoughnessTextureTransform) {
FLinearColor rotationValues(0.0f, 1.0f, 0.0f, 1.0f);
if (pBaseColorTextureTransform) {
rotationValues.R =
float(FMath::Sin(pBaseColorTextureTransform->rotation));
rotationValues.G =
float(FMath::Cos(pBaseColorTextureTransform->rotation));
}
if (pMetallicRoughnessTextureTransform) {
rotationValues.B =
float(FMath::Sin(pMetallicRoughnessTextureTransform->rotation));
rotationValues.A =
float(FMath::Cos(pMetallicRoughnessTextureTransform->rotation));
}

pMaterial->SetVectorParameterValueByInfo(
FMaterialParameterInfo(
"baseColorMetallicRoughnessRotation",
association,
index),
rotationValues);
}

const ExtensionKhrTextureTransform* pEmissiveTextureTransform =
material.emissiveTexture
? material.emissiveTexture
->getExtension<ExtensionKhrTextureTransform>()
: nullptr;
const ExtensionKhrTextureTransform* pNormalTextureTransform =
material.normalTexture
? material.normalTexture->getExtension<ExtensionKhrTextureTransform>()
: nullptr;

if (pEmissiveTextureTransform) {
pMaterial->SetVectorParameterValueByInfo(
FMaterialParameterInfo("emissiveScaleOffset", association, index),
FLinearColor(
pEmissiveTextureTransform->scale[0],
pEmissiveTextureTransform->scale[1],
pEmissiveTextureTransform->offset[0],
pEmissiveTextureTransform->offset[1]));
}

if (pNormalTextureTransform) {
pMaterial->SetVectorParameterValueByInfo(
FMaterialParameterInfo("normalScaleOffset", association, index),
FLinearColor(
pNormalTextureTransform->scale[0],
pNormalTextureTransform->scale[1],
pNormalTextureTransform->offset[0],
pNormalTextureTransform->offset[1]));
}

if (pEmissiveTextureTransform || pNormalTextureTransform) {
FLinearColor rotationValues(0.0f, 1.0f, 0.0f, 1.0f);
if (pEmissiveTextureTransform) {
rotationValues.R = float(FMath::Sin(pEmissiveTextureTransform->rotation));
rotationValues.G = float(FMath::Cos(pEmissiveTextureTransform->rotation));
}
if (pNormalTextureTransform) {
rotationValues.B = float(FMath::Sin(pNormalTextureTransform->rotation));
rotationValues.A = float(FMath::Cos(pNormalTextureTransform->rotation));
}

pMaterial->SetVectorParameterValueByInfo(
FMaterialParameterInfo("emissiveNormalRotation", association, index),
rotationValues);
}

const ExtensionKhrTextureTransform* pOcclusionTransform =
material.occlusionTexture
? material.occlusionTexture
->getExtension<ExtensionKhrTextureTransform>()
: nullptr;

if (pOcclusionTransform) {
pMaterial->SetVectorParameterValueByInfo(
FMaterialParameterInfo("occlusionScaleOffset", association, index),
FLinearColor(
pOcclusionTransform->scale[0],
pOcclusionTransform->scale[1],
pOcclusionTransform->offset[0],
pOcclusionTransform->offset[1]));
pMaterial->SetVectorParameterValueByInfo(
FMaterialParameterInfo("occlusionRotation", association, index),
FLinearColor(
float(FMath::Sin(pOcclusionTransform->rotation)),
float(FMath::Cos(pOcclusionTransform->rotation)),
0.0f,
1.0f));
}

if (material.emissiveFactor.size() >= 3) {
pMaterial->SetVectorParameterValueByInfo(
FMaterialParameterInfo("emissiveFactor", association, index),
Expand Down
2 changes: 2 additions & 0 deletions Source/CesiumRuntime/Private/CesiumMetadataValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <CesiumGltf/MetadataConversions.h>
#include <CesiumGltf/PropertyTypeTraits.h>

using namespace CesiumGltf;

ECesiumMetadataBlueprintType
UCesiumMetadataValueBlueprintLibrary::GetBlueprintType(
UPARAM(ref) const FCesiumMetadataValue& Value) {
Expand Down
15 changes: 7 additions & 8 deletions Source/CesiumRuntime/Private/CesiumSubLevelSwitcherComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "CesiumSubLevelSwitcherComponent.h"
#include "CesiumCommon.h"
#include "CesiumRuntime.h"
#include "CesiumSubLevelComponent.h"
#include "Engine/LevelStreaming.h"
Expand All @@ -7,7 +8,6 @@
#include "LevelInstance/LevelInstanceLevelStreaming.h"
#include "LevelInstance/LevelInstanceSubsystem.h"
#include "Runtime/Launch/Resources/Version.h"
#include "CesiumCommon.h"

#if WITH_EDITOR
#include "Editor.h"
Expand Down Expand Up @@ -136,7 +136,6 @@ void UCesiumSubLevelSwitcherComponent::TickComponent(
if (pSubLevel == this->_pCurrent || pSubLevel == this->_pTarget)
continue;


ULevelStreaming* pStreaming =
this->_getLevelStreamingForSubLevel(pSubLevel);
StreamState state =
Expand Down Expand Up @@ -214,10 +213,10 @@ void UCesiumSubLevelSwitcherComponent::_updateSubLevelStateGame() {
if (IsValid(pStreaming)) {
#if ENGINE_VERSION_5_3_OR_HIGHER
state = pStreaming->GetLevelStreamingState();
#else
#else
state = pStreaming->GetCurrentState();
#endif
#endif

} else if (this->_pCurrent->GetWorldAsset().IsNull()) {
// There is no level associated with the target at all, so mark it
// unloaded but also deactivate it for the benefit of the Editor UI.
Expand Down Expand Up @@ -279,12 +278,12 @@ void UCesiumSubLevelSwitcherComponent::_updateSubLevelStateGame() {

StreamState state = StreamState::Unloaded;
if (IsValid(pStreaming)) {
#if ENGINE_VERSION_5_3_OR_HIGHER
#if ENGINE_VERSION_5_3_OR_HIGHER
state = pStreaming->GetLevelStreamingState();
#else
#else

state = pStreaming->GetCurrentState();
#endif
#endif
} else if (this->_pTarget.Get()->GetWorldAsset().IsNull()) {
// There is no level associated with the target at all, so mark it failed
// to load because this is as loaded as it will ever be.
Expand Down
12 changes: 6 additions & 6 deletions Source/CesiumRuntime/Private/CesiumTextureUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <memory>
#include <stb_image_resize.h>
#include <variant>
#include "CesiumCommon.h"

using namespace CesiumGltf;

Expand Down Expand Up @@ -955,12 +954,13 @@ UTexture2D* loadTextureGameThreadPart(LoadedTextureResult* pHalfLoadedTexture) {
([pTexture, pCesiumTextureResource](FRHICommandListImmediate& RHICmdList) {
pCesiumTextureResource->SetTextureReference(
pTexture->TextureReference.TextureReferenceRHI);
#if ENGINE_VERSION_5_3_OR_HIGHER
pCesiumTextureResource->InitResource(FRHICommandListImmediate::Get()); //Init Resource now requires a command list.
#else
#if ENGINE_VERSION_5_3_OR_HIGHER
pCesiumTextureResource->InitResource(
FRHICommandListImmediate::Get()); // Init Resource now requires a
// command list.
#else
pCesiumTextureResource->InitResource();
#endif

#endif
});

return pTexture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,7 @@ void FCesiumPropertyTablePropertySpec::Define() {
std::vector<int32_t> values{1, 2, 3, 4, 5, 6};
std::vector<std::byte> data = GetValuesAsBytes(values);

std::vector<uint16_t> offsets{
0,
1 * sizeof(int32_t),
3 * sizeof(int32_t),
6 * sizeof(int32_t)};
std::vector<uint16_t> offsets{0, 1, 3, 6};
std::vector<std::byte> offsetsData = GetValuesAsBytes(offsets);

int64_t size = static_cast<int64_t>(offsets.size()) - 1;
Expand Down Expand Up @@ -3762,11 +3758,7 @@ void FCesiumPropertyTablePropertySpec::Define() {
std::vector<int32_t> values{1, 2, 3, 4, 5, 6};
std::vector<std::byte> data = GetValuesAsBytes(values);

std::vector<uint16_t> offsets{
0,
2 * sizeof(int32_t),
3 * sizeof(int32_t),
6 * sizeof(int32_t)};
std::vector<uint16_t> offsets{0, 2, 3, 6};
std::vector<std::byte> offsetsData = GetValuesAsBytes(offsets);
int64 size = static_cast<int64_t>(offsets.size() - 1);

Expand Down

0 comments on commit eb805fa

Please sign in to comment.