Skip to content

Commit

Permalink
Support textures with < 4 channels
Browse files Browse the repository at this point in the history
  • Loading branch information
j9liu committed Feb 8, 2024
1 parent 7820de9 commit 05849fc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
81 changes: 47 additions & 34 deletions native~/Runtime/src/TextureLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 05849fc

Please sign in to comment.