Updated the image gradient component to use a streaming image asset

Signed-off-by: Chris Galvan <chgalvan@amazon.com>
This commit is contained in:
Chris Galvan
2022-02-03 12:29:18 -06:00
parent 65e651388c
commit 6322dd5397
10 changed files with 150 additions and 43 deletions
@@ -7,6 +7,7 @@
*/
#include <GradientSignal/Components/ImageGradientComponent.h>
#include <Atom/RPI.Reflect/Image/Image.h>
#include <AzCore/Asset/AssetManager.h>
#include <AzCore/Asset/AssetSerializer.h>
#include <AzCore/Debug/Profiler.h>
@@ -18,13 +19,100 @@
namespace GradientSignal
{
AZ::JsonSerializationResult::Result JsonImageGradientConfigSerializer::Load(
void* outputValue, [[maybe_unused]] const AZ::Uuid& outputValueTypeId,
const rapidjson::Value& inputValue, AZ::JsonDeserializerContext& context)
{
namespace JSR = AZ::JsonSerializationResult;
auto configInstance = reinterpret_cast<ImageGradientConfig*>(outputValue);
AZ_Assert(configInstance, "Output value for JsonImageGradientConfigSerializer can't be null.");
JSR::ResultCode result(JSR::Tasks::ReadField);
rapidjson::Value::ConstMemberIterator itr = inputValue.FindMember("ImageAsset");
if (itr != inputValue.MemberEnd())
{
// Version 1 stored a custom GradientSignal::ImageAsset as the image asset.
// In Version 2, we changed the image asset to use the generic AZ::RPI::StreamingImageAsset,
// so they are both AZ::Data::Asset but reference different types.
// Using the assetHint, which will be something like "my_test_image.gradimage",
// we need to find the valid streaming image asset product from the same source,
// which will be something like "my_test_image.png.streamingimage"
AZStd::string assetHint;
AZ::Data::AssetId fixedAssetId;
auto it = itr->value.FindMember("assetHint");
if (it != itr->value.MemberEnd())
{
AZ::ScopedContextPath subPath(context, "assetHint");
result.Combine(ContinueLoading(&assetHint, azrtti_typeid<AZStd::string>(), it->value, context));
if (assetHint.ends_with(".gradimage"))
{
// We don't know what image format the original source was, so we need to loop through
// all the supported image extensions to check if they have a valid corresponding
// streaming image asset
for (int i = 0; i < AZ::RPI::s_TotalSupportedImageExtensions; i++)
{
AZStd::string imageExtension(AZ::RPI::s_SupportedImageExtensions[i]);
// The image extensions are stored with a wildcard (e.g. *.png) so we need to strip that off first
AZ::StringFunc::Replace(imageExtension, "*", "");
// Form potential streaming image path (e.g. my_test_image.png.streamingimage)
AZStd::string potentialStreamingImagePath(assetHint);
AZ::StringFunc::Replace(potentialStreamingImagePath, ".gradimage", "");
potentialStreamingImagePath += imageExtension + ".streamingimage";
// Check if there is a valid streaming image asset for this path
AZ::Data::AssetCatalogRequestBus::BroadcastResult(fixedAssetId, &AZ::Data::AssetCatalogRequestBus::Events::GetAssetIdByPath, potentialStreamingImagePath.c_str(), azrtti_typeid<AZ::Data::Asset<AZ::RPI::StreamingImageAsset>>(), false);
if (fixedAssetId.IsValid())
{
break;
}
}
}
}
// Replace the old gradimage with new AssetId for streaming image asset (if we needed to replace)
if (fixedAssetId.IsValid())
{
configInstance->m_imageAsset = AZ::Data::AssetManager::Instance().GetAsset<AZ::RPI::StreamingImageAsset>(fixedAssetId, AZ::Data::AssetLoadBehavior::QueueLoad);
}
// Otherwise, this was already a streaming image asset, so just load it like normal
else
{
result.Combine(ContinueLoadingFromJsonObjectField(
&configInstance->m_imageAsset, azrtti_typeid<decltype(configInstance->m_imageAsset)>(), inputValue, "ImageAsset", context));
}
}
result.Combine(ContinueLoadingFromJsonObjectField(
&configInstance->m_tilingX, azrtti_typeid<decltype(configInstance->m_tilingX)>(), inputValue, "TilingX", context));
result.Combine(ContinueLoadingFromJsonObjectField(
&configInstance->m_tilingY, azrtti_typeid<decltype(configInstance->m_tilingY)>(), inputValue, "TilingY", context));
return context.Report(result,
result.GetProcessing() != JSR::Processing::Halted ?
"Successfully loaded ImageGradientConfig information." :
"Failed to load ImageGradientConfig information.");
}
AZ_CLASS_ALLOCATOR_IMPL(JsonImageGradientConfigSerializer, AZ::SystemAllocator, 0);
void ImageGradientConfig::Reflect(AZ::ReflectContext* context)
{
if (auto jsonContext = azrtti_cast<AZ::JsonRegistrationContext*>(context))
{
jsonContext->Serializer<JsonImageGradientConfigSerializer>()->HandlesType<ImageGradientConfig>();
}
AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
if (serialize)
{
serialize->Class<ImageGradientConfig, AZ::ComponentConfig>()
->Version(1)
->Version(2)
->Field("ImageAsset", &ImageGradientConfig::m_imageAsset)
->Field("TilingX", &ImageGradientConfig::m_tilingX)
->Field("TilingY", &ImageGradientConfig::m_tilingY)
@@ -265,7 +353,7 @@ namespace GradientSignal
{
AZStd::unique_lock<decltype(m_imageMutex)> imageLock(m_imageMutex);
m_configuration.m_imageAsset = AZ::Data::AssetManager::Instance().FindOrCreateAsset(assetId, azrtti_typeid<ImageAsset>(), m_configuration.m_imageAsset.GetAutoLoadBehavior());
m_configuration.m_imageAsset = AZ::Data::AssetManager::Instance().FindOrCreateAsset(assetId, azrtti_typeid<AZ::RPI::StreamingImageAsset>(), m_configuration.m_imageAsset.GetAutoLoadBehavior());
}
SetupDependencies();
+13 -14
View File
@@ -15,6 +15,7 @@
#include <AzCore/Debug/Profiler.h>
#include <Atom/ImageProcessing/PixelFormats.h>
#include <Atom/RPI.Public/RPIUtils.h>
#include <GradientSignal/Util.h>
#include <numeric>
@@ -152,17 +153,15 @@ namespace GradientSignal
return true;
}
float GetValueFromImageAsset(const AZ::Data::Asset<ImageAsset>& imageAsset, const AZ::Vector3& uvw, float tilingX, float tilingY, float defaultValue)
float GetValueFromImageAsset(const AZ::Data::Asset<AZ::RPI::StreamingImageAsset>& imageAsset, const AZ::Vector3& uvw, float tilingX, float tilingY, float defaultValue)
{
if (imageAsset.IsReady())
{
const auto& image = imageAsset.Get();
AZStd::size_t imageSize = image->m_imageWidth * image->m_imageHeight *
static_cast<AZ::u32>(image->m_bytesPerPixel);
if (image->m_imageWidth > 0 &&
image->m_imageHeight > 0 &&
image->m_imageData.size() == imageSize)
auto imageDescriptor = imageAsset->GetImageDescriptor();
auto width = imageDescriptor.m_size.m_width;
auto height = imageDescriptor.m_size.m_height;
if (width > 0 && height > 0)
{
// When "rasterizing" from uvs, a range of 0-1 has slightly different meanings depending on the sampler state.
// For repeating states (Unbounded/None, Repeat), a uv value of 1 should wrap around back to our 0th pixel.
@@ -185,8 +184,8 @@ namespace GradientSignal
// A 16x16 pixel image and tilingX = tilingY = 1 maps the uv range of 0-1 to 0-16 pixels.
// A 16x16 pixel image and tilingX = tilingY = 1.5 maps the uv range of 0-1 to 0-24 pixels.
const AZ::Vector3 tiledDimensions((image->m_imageWidth * tilingX),
(image->m_imageHeight * tilingY),
const AZ::Vector3 tiledDimensions((width * tilingX),
(height * tilingY),
0.0f);
// Convert from uv space back to pixel space
@@ -195,13 +194,13 @@ namespace GradientSignal
// UVs outside the 0-1 range are treated as infinitely tiling, so that we behave the same as the
// other gradient generators. As mentioned above, if clamping is desired, we expect it to be applied
// outside of this function.
size_t x = static_cast<size_t>(pixelLookup.GetX()) % image->m_imageWidth;
size_t y = static_cast<size_t>(pixelLookup.GetY()) % image->m_imageHeight;
auto x = aznumeric_cast<AZ::u32>(pixelLookup.GetX()) % width;
auto y = aznumeric_cast<AZ::u32>(pixelLookup.GetY()) % height;
// Flip the y because images are stored in reverse of our world axes
size_t index = ((image->m_imageHeight - 1) - y) * image->m_imageWidth + x;
y = (height - 1) - y;
return RetrieveValue(image->m_imageData.data(), index, image->m_imageFormat);
return AZ::RPI::GetSubImagePixelValue<float>(imageAsset, x, y);
}
}