Skip to content

Commit

Permalink
Fix raster overlays and water mask.
Browse files Browse the repository at this point in the history
  • Loading branch information
kring committed Sep 30, 2024
1 parent aa58bd9 commit 1246c2d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 40 deletions.
23 changes: 18 additions & 5 deletions Source/CesiumRuntime/Private/Cesium3DTileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "Engine/TextureRenderTarget2D.h"
#include "Engine/World.h"
#include "EngineUtils.h"
#include "ExtensionImageCesiumUnreal.h"
#include "GameFramework/PlayerController.h"
#include "Kismet/GameplayStatics.h"
#include "LevelSequenceActor.h"
Expand Down Expand Up @@ -877,19 +878,31 @@ class UnrealResourcePreparer
}
}

CesiumGltf::SharedAsset<CesiumGltf::ImageCesium> imageAsset(
std::move(image));
// TODO: sRGB should probably be configurable on the raster overlay.
bool sRGB = true;

const ExtensionImageCesiumUnreal& extension =
ExtensionImageCesiumUnreal::GetOrCreate(
CesiumAsync::AsyncSystem(nullptr), // TODO
image,
sRGB,
pOptions->useMipmaps,
std::nullopt);

// Because raster overlay images are never shared (at least currently!), the
// future should already be resolved by the time we get here.
check(extension.getFuture().isReady());

auto texture = CesiumTextureUtility::loadTextureAnyThreadPart(
imageAsset,
image,
TextureAddress::TA_Clamp,
TextureAddress::TA_Clamp,
pOptions->filter,
pOptions->useMipmaps,
pOptions->group,
// TODO: sRGB should probably be configurable on the raster overlay.
true,
sRGB,
std::nullopt);

return texture.Release();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ std::optional<EncodedFeatureIdSet> encodeFeatureIdTexture(
CesiumGltf::SharedAsset(CesiumGltf::ImageCesium(*pFeatureIdImage));
encodedFeatureIdTexture.pTexture =
MakeShared<LoadedTextureResult>(std::move(*loadTextureAnyThreadPart(
imageCopy,
*imageCopy,
addressX,
addressY,
TextureFilter::TF_Nearest,
Expand Down Expand Up @@ -534,7 +534,7 @@ EncodedPropertyTable encodePropertyTableAnyThreadPart(
}

encodedProperty.pTexture = loadTextureAnyThreadPart(
image,
*image,
TextureAddress::TA_Clamp,
TextureAddress::TA_Clamp,
TextureFilter::TF_Nearest,
Expand Down Expand Up @@ -661,7 +661,7 @@ EncodedPropertyTexture encodePropertyTextureAnyThreadPart(
CesiumGltf::SharedAsset(CesiumGltf::ImageCesium(*pImage));
encodedProperty.pTexture =
MakeShared<LoadedTextureResult>(std::move(*loadTextureAnyThreadPart(
imageCopy,
*imageCopy,
addressX,
addressY,
// TODO: account for texture filter
Expand Down
6 changes: 3 additions & 3 deletions Source/CesiumRuntime/Private/CesiumEncodedMetadataUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ EncodedMetadataFeatureTable encodeMetadataFeatureTableAnyThreadPart(
}

encodedProperty.pTexture = loadTextureAnyThreadPart(
image,
*image,
TextureAddress::TA_Clamp,
TextureAddress::TA_Clamp,
TextureFilter::TF_Nearest,
Expand Down Expand Up @@ -414,7 +414,7 @@ EncodedFeatureTexture encodeFeatureTextureAnyThreadPart(
CesiumGltf::SharedAsset(CesiumGltf::ImageCesium(*pImage));
encodedFeatureTextureProperty.pTexture =
MakeShared<LoadedTextureResult>(std::move(*loadTextureAnyThreadPart(
imageCopy,
*imageCopy,
TextureAddress::TA_Clamp,
TextureAddress::TA_Clamp,
TextureFilter::TF_Nearest,
Expand Down Expand Up @@ -521,7 +521,7 @@ EncodedMetadataPrimitive encodeMetadataPrimitiveAnyThreadPart(
CesiumGltf::ImageCesium(*pFeatureIdImage));
encodedFeatureIdTexture.pTexture = MakeShared<LoadedTextureResult>(
std::move(*loadTextureAnyThreadPart(
imageCopy,
*imageCopy,
TextureAddress::TA_Clamp,
TextureAddress::TA_Clamp,
TextureFilter::TF_Nearest,
Expand Down
32 changes: 32 additions & 0 deletions Source/CesiumRuntime/Private/CesiumGltfTextures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,38 @@ SharedFuture<void> createTextureInLoadThread(
*pMaterial->occlusionTexture,
false,
imageNeedsMipmaps));

// Initialize water mask if needed.
auto onlyWaterIt = primitive.extras.find("OnlyWater");
auto onlyLandIt = primitive.extras.find("OnlyLand");
if (onlyWaterIt != primitive.extras.end() &&
onlyWaterIt->second.isBool() &&
onlyLandIt != primitive.extras.end() &&
onlyLandIt->second.isBool()) {
bool onlyWater = onlyWaterIt->second.getBoolOrDefault(false);
bool onlyLand = onlyLandIt->second.getBoolOrDefault(true);

if (!onlyWater && !onlyLand) {
// We have to use the water mask
auto waterMaskTextureIdIt = primitive.extras.find("WaterMaskTex");
if (waterMaskTextureIdIt != primitive.extras.end() &&
waterMaskTextureIdIt->second.isInt64()) {
int32_t waterMaskTextureId = static_cast<int32_t>(
waterMaskTextureIdIt->second.getInt64OrDefault(-1));
TextureInfo waterMaskInfo;
waterMaskInfo.index = waterMaskTextureId;
if (waterMaskTextureId >= 0 &&
waterMaskTextureId < gltf.textures.size()) {
futures.emplace_back(createTextureInLoadThread(
asyncSystem,
gltf,
waterMaskInfo,
false,
imageNeedsMipmaps));
}
}
}
}
});

return asyncSystem.all(std::move(futures));
Expand Down
11 changes: 6 additions & 5 deletions Source/CesiumRuntime/Private/CesiumTextureUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ TUniquePtr<LoadedTextureResult> loadTextureFromModelAnyThreadPart(
model.getSafe(model.samplers, texture.sampler);

TUniquePtr<LoadedTextureResult> result =
loadTextureFromImageAndSamplerAnyThreadPart(image.cesium, sampler, sRGB);
loadTextureFromImageAndSamplerAnyThreadPart(*image.cesium, sampler, sRGB);

if (result) {
extension.pTexture = result->pTexture;
Expand Down Expand Up @@ -366,7 +366,7 @@ bool getUseMipmapsIfAvailableFromSampler(const CesiumGltf::Sampler& sampler) {
}

TUniquePtr<LoadedTextureResult> loadTextureFromImageAndSamplerAnyThreadPart(
CesiumGltf::SharedAsset<CesiumGltf::ImageCesium>& image,
const CesiumGltf::ImageCesium& image,
const CesiumGltf::Sampler& sampler,
bool sRGB) {
return loadTextureAnyThreadPart(
Expand Down Expand Up @@ -411,7 +411,7 @@ static UTexture2D* CreateTexture2D(LoadedTextureResult* pHalfLoadedTexture) {
}

TUniquePtr<LoadedTextureResult> loadTextureAnyThreadPart(
CesiumGltf::SharedAsset<CesiumGltf::ImageCesium>& image,
const CesiumGltf::ImageCesium& image,
TextureAddress addressX,
TextureAddress addressY,
TextureFilter filter,
Expand All @@ -421,8 +421,9 @@ TUniquePtr<LoadedTextureResult> loadTextureAnyThreadPart(
std::optional<EPixelFormat> overridePixelFormat) {
// The FCesiumTextureResource for the ImageCesium should already be created at
// this point, if it can be.
ExtensionImageCesiumUnreal* pExtension =
image->getExtension<ExtensionImageCesiumUnreal>();
const ExtensionImageCesiumUnreal* pExtension =
image.getExtension<ExtensionImageCesiumUnreal>();
check(pExtension != nullptr);
if (pExtension == nullptr || pExtension->getTextureResource() == nullptr) {
return nullptr;
}
Expand Down
28 changes: 7 additions & 21 deletions Source/CesiumRuntime/Private/CesiumTextureUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ TUniquePtr<LoadedTextureResult> loadTextureFromModelAnyThreadPart(
* and can be empty.
*/
TUniquePtr<LoadedTextureResult> loadTextureFromImageAndSamplerAnyThreadPart(
CesiumGltf::SharedAsset<CesiumGltf::ImageCesium>& image,
const CesiumGltf::ImageCesium& image,
const CesiumGltf::Sampler& sampler,
bool sRGB);

/**
* @brief Does the asynchronous part of renderer resource preparation for
* this image. Should be called in a background thread.
*
* The `pixelData` will be removed from the image so that it can be
* passed to Unreal's renderer thread without copying it.
* a texture. The given image _must_ be prepared before calling this method by
* calling {@link ExtensionImageCesiumUnreal::GetOrCreate} and then waiting
* for {@link ExtensionImageCesiumUnreal::getFuture} to resolve. This method
* should be called in a background thread.
*
* @param imageCesium The image.
* @param addressX The X addressing mode.
Expand All @@ -138,14 +138,10 @@ TUniquePtr<LoadedTextureResult> loadTextureFromImageAndSamplerAnyThreadPart(
* @param sRGB Whether this texture uses a sRGB color space.
* @param overridePixelFormat The explicit pixel format to use. If std::nullopt,
* the pixel format is inferred from the image.
* @param pExistingImageResource An existing RHI texture resource that has been
* created for this image, or nullptr if one hasn't been created yet. When this
* parameter is not nullptr, the provided image's `pixelData` is not required
* and can be empty.
* @return A future that resolves to the loaded texture.
* @return The loaded texture.
*/
TUniquePtr<LoadedTextureResult> loadTextureAnyThreadPart(
CesiumGltf::SharedAsset<CesiumGltf::ImageCesium>& image,
const CesiumGltf::ImageCesium& image,
TextureAddress addressX,
TextureAddress addressY,
TextureFilter filter,
Expand Down Expand Up @@ -181,16 +177,6 @@ loadTextureGameThreadPart(
CesiumUtility::IntrusivePointer<ReferenceCountedUnrealTexture>
loadTextureGameThreadPart(LoadedTextureResult* pHalfLoadedTexture);

/**
* Creates a `FCesiumTextureResource` for an image. This texture resource is
* intended to be later used with `FCesiumUseExistingTextureResource`, which
* will supply sampler, texture group, and other settings.
*/
TSharedPtr<FCesiumTextureResource> createTextureResource(
CesiumGltf::ImageCesium& imageCesium,
bool sRGB,
std::optional<EPixelFormat> overridePixelFormat);

/**
* @brief Convert a glTF {@link CesiumGltf::Sampler::WrapS} value to an Unreal
* `TextureAddress` value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void CesiumTextureUtilitySpec::Define() {
void CesiumTextureUtilitySpec::RunTests() {
It("ImageCesium non-sRGB", [this]() {
TUniquePtr<LoadedTextureResult> pHalfLoaded = loadTextureAnyThreadPart(
imageCesium,
*imageCesium,
TextureAddress::TA_Mirror,
TextureAddress::TA_Wrap,
TextureFilter::TF_Bilinear,
Expand All @@ -129,7 +129,7 @@ void CesiumTextureUtilitySpec::RunTests() {

It("ImageCesium sRGB", [this]() {
TUniquePtr<LoadedTextureResult> pHalfLoaded = loadTextureAnyThreadPart(
imageCesium,
*imageCesium,
TextureAddress::TA_Clamp,
TextureAddress::TA_Mirror,
TextureFilter::TF_Trilinear,
Expand Down Expand Up @@ -160,7 +160,7 @@ void CesiumTextureUtilitySpec::RunTests() {

TUniquePtr<LoadedTextureResult> pHalfLoaded =
loadTextureFromImageAndSamplerAnyThreadPart(
imageCesium,
*imageCesium,
sampler,
false);
TestNotNull("pHalfLoaded", pHalfLoaded.Get());
Expand Down

0 comments on commit 1246c2d

Please sign in to comment.