diff --git a/CHANGES.md b/CHANGES.md index 357b20d2..309652d3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -25,6 +25,7 @@ - Removed the "Universal Additional Camera Data" script from DynamicCamera, as it shows up as a missing script in other render pipelines. - Fixed a bug where adding a `CesiumSubScene` as the child of an existing `CesiumGeoreference` in editor would cause the parent `CesiumGeoreference` to have its coordinates reset to the default. - Fixed the "DynamicCamera is not nested inside a game object with a CesiumGeoreference" warning when adding a new DynamicCamera in the editor. +- Fixed support for loading textures with less than four channels. ##### Deprecated :hourglass_flowing_sand: diff --git a/native~/Runtime/src/TextureLoader.cpp b/native~/Runtime/src/TextureLoader.cpp index dde3c490..03f61aac 100644 --- a/native~/Runtime/src/TextureLoader.cpp +++ b/native~/Runtime/src/TextureLoader.cpp @@ -19,56 +19,69 @@ using namespace DotNet; namespace CesiumForUnityNative { -UnityEngine::Texture -TextureLoader::loadTexture(const CesiumGltf::ImageCesium& image) { - CESIUM_TRACE("TextureLoader::loadTexture"); - std::int32_t mipCount = - image.mipPositions.empty() ? 1 : std::int32_t(image.mipPositions.size()); - - UnityEngine::TextureFormat textureFormat; - +namespace { +UnityEngine::TextureFormat +getCompressedPixelFormat(const CesiumGltf::ImageCesium& image) { switch (image.compressedPixelFormat) { case GpuCompressedPixelFormat::ETC1_RGB: - textureFormat = UnityEngine::TextureFormat::ETC_RGB4; - break; + return UnityEngine::TextureFormat::ETC_RGB4; case GpuCompressedPixelFormat::ETC2_RGBA: - textureFormat = UnityEngine::TextureFormat::ETC2_RGBA8; - break; + return UnityEngine::TextureFormat::ETC2_RGBA8; case GpuCompressedPixelFormat::BC1_RGB: - textureFormat = UnityEngine::TextureFormat::DXT1; - break; + return UnityEngine::TextureFormat::DXT1; case GpuCompressedPixelFormat::BC3_RGBA: - textureFormat = UnityEngine::TextureFormat::DXT5; - break; + return UnityEngine::TextureFormat::DXT5; case GpuCompressedPixelFormat::BC4_R: - textureFormat = UnityEngine::TextureFormat::BC4; - break; + return UnityEngine::TextureFormat::BC4; case GpuCompressedPixelFormat::BC5_RG: - textureFormat = UnityEngine::TextureFormat::BC5; - break; + return UnityEngine::TextureFormat::BC5; case GpuCompressedPixelFormat::BC7_RGBA: - textureFormat = UnityEngine::TextureFormat::BC7; - break; + return UnityEngine::TextureFormat::BC7; case GpuCompressedPixelFormat::ASTC_4x4_RGBA: - textureFormat = UnityEngine::TextureFormat::ASTC_4x4; - break; + return UnityEngine::TextureFormat::ASTC_4x4; case GpuCompressedPixelFormat::PVRTC1_4_RGB: - textureFormat = UnityEngine::TextureFormat::PVRTC_RGB4; - break; + return UnityEngine::TextureFormat::PVRTC_RGB4; case GpuCompressedPixelFormat::PVRTC1_4_RGBA: - textureFormat = UnityEngine::TextureFormat::PVRTC_RGBA4; - break; + return UnityEngine::TextureFormat::PVRTC_RGBA4; case GpuCompressedPixelFormat::ETC2_EAC_R11: - textureFormat = UnityEngine::TextureFormat::EAC_R; - break; + return UnityEngine::TextureFormat::EAC_R; case GpuCompressedPixelFormat::ETC2_EAC_RG11: - textureFormat = UnityEngine::TextureFormat::EAC_RG; - break; + return UnityEngine::TextureFormat::EAC_RG; case GpuCompressedPixelFormat::PVRTC2_4_RGB: case GpuCompressedPixelFormat::PVRTC2_4_RGBA: default: - textureFormat = UnityEngine::TextureFormat::RGBA32; - break; + return UnityEngine::TextureFormat::RGBA32; + } +} + +UnityEngine::TextureFormat +getUncompressedPixelFormat(const CesiumGltf::ImageCesium& image) { + switch (image.channels) { + case 1: + return UnityEngine::TextureFormat::R8; + case 2: + return UnityEngine::TextureFormat::RG16; + case 3: + return UnityEngine::TextureFormat::RGB24; + case 4: + default: + return UnityEngine::TextureFormat::RGBA32; + } +} + +} // namespace + +UnityEngine::Texture +TextureLoader::loadTexture(const CesiumGltf::ImageCesium& image) { + CESIUM_TRACE("TextureLoader::loadTexture"); + std::int32_t mipCount = + image.mipPositions.empty() ? 1 : std::int32_t(image.mipPositions.size()); + + UnityEngine::TextureFormat textureFormat; + if (image.compressedPixelFormat != GpuCompressedPixelFormat::NONE) { + textureFormat = getCompressedPixelFormat(image); + } else { + textureFormat = getUncompressedPixelFormat(image); } UnityEngine::Texture2D