Skip to content

Commit 05849fc

Browse files
committed
Support textures with < 4 channels
1 parent 7820de9 commit 05849fc

File tree

2 files changed

+48
-34
lines changed

2 files changed

+48
-34
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- Removed the "Universal Additional Camera Data" script from DynamicCamera, as it shows up as a missing script in other render pipelines.
2626
- 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.
2727
- Fixed the "DynamicCamera is not nested inside a game object with a CesiumGeoreference" warning when adding a new DynamicCamera in the editor.
28+
- Fixed support for loading textures with less than four channels.
2829

2930
##### Deprecated :hourglass_flowing_sand:
3031

native~/Runtime/src/TextureLoader.cpp

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,56 +19,69 @@ using namespace DotNet;
1919

2020
namespace CesiumForUnityNative {
2121

22-
UnityEngine::Texture
23-
TextureLoader::loadTexture(const CesiumGltf::ImageCesium& image) {
24-
CESIUM_TRACE("TextureLoader::loadTexture");
25-
std::int32_t mipCount =
26-
image.mipPositions.empty() ? 1 : std::int32_t(image.mipPositions.size());
27-
28-
UnityEngine::TextureFormat textureFormat;
29-
22+
namespace {
23+
UnityEngine::TextureFormat
24+
getCompressedPixelFormat(const CesiumGltf::ImageCesium& image) {
3025
switch (image.compressedPixelFormat) {
3126
case GpuCompressedPixelFormat::ETC1_RGB:
32-
textureFormat = UnityEngine::TextureFormat::ETC_RGB4;
33-
break;
27+
return UnityEngine::TextureFormat::ETC_RGB4;
3428
case GpuCompressedPixelFormat::ETC2_RGBA:
35-
textureFormat = UnityEngine::TextureFormat::ETC2_RGBA8;
36-
break;
29+
return UnityEngine::TextureFormat::ETC2_RGBA8;
3730
case GpuCompressedPixelFormat::BC1_RGB:
38-
textureFormat = UnityEngine::TextureFormat::DXT1;
39-
break;
31+
return UnityEngine::TextureFormat::DXT1;
4032
case GpuCompressedPixelFormat::BC3_RGBA:
41-
textureFormat = UnityEngine::TextureFormat::DXT5;
42-
break;
33+
return UnityEngine::TextureFormat::DXT5;
4334
case GpuCompressedPixelFormat::BC4_R:
44-
textureFormat = UnityEngine::TextureFormat::BC4;
45-
break;
35+
return UnityEngine::TextureFormat::BC4;
4636
case GpuCompressedPixelFormat::BC5_RG:
47-
textureFormat = UnityEngine::TextureFormat::BC5;
48-
break;
37+
return UnityEngine::TextureFormat::BC5;
4938
case GpuCompressedPixelFormat::BC7_RGBA:
50-
textureFormat = UnityEngine::TextureFormat::BC7;
51-
break;
39+
return UnityEngine::TextureFormat::BC7;
5240
case GpuCompressedPixelFormat::ASTC_4x4_RGBA:
53-
textureFormat = UnityEngine::TextureFormat::ASTC_4x4;
54-
break;
41+
return UnityEngine::TextureFormat::ASTC_4x4;
5542
case GpuCompressedPixelFormat::PVRTC1_4_RGB:
56-
textureFormat = UnityEngine::TextureFormat::PVRTC_RGB4;
57-
break;
43+
return UnityEngine::TextureFormat::PVRTC_RGB4;
5844
case GpuCompressedPixelFormat::PVRTC1_4_RGBA:
59-
textureFormat = UnityEngine::TextureFormat::PVRTC_RGBA4;
60-
break;
45+
return UnityEngine::TextureFormat::PVRTC_RGBA4;
6146
case GpuCompressedPixelFormat::ETC2_EAC_R11:
62-
textureFormat = UnityEngine::TextureFormat::EAC_R;
63-
break;
47+
return UnityEngine::TextureFormat::EAC_R;
6448
case GpuCompressedPixelFormat::ETC2_EAC_RG11:
65-
textureFormat = UnityEngine::TextureFormat::EAC_RG;
66-
break;
49+
return UnityEngine::TextureFormat::EAC_RG;
6750
case GpuCompressedPixelFormat::PVRTC2_4_RGB:
6851
case GpuCompressedPixelFormat::PVRTC2_4_RGBA:
6952
default:
70-
textureFormat = UnityEngine::TextureFormat::RGBA32;
71-
break;
53+
return UnityEngine::TextureFormat::RGBA32;
54+
}
55+
}
56+
57+
UnityEngine::TextureFormat
58+
getUncompressedPixelFormat(const CesiumGltf::ImageCesium& image) {
59+
switch (image.channels) {
60+
case 1:
61+
return UnityEngine::TextureFormat::R8;
62+
case 2:
63+
return UnityEngine::TextureFormat::RG16;
64+
case 3:
65+
return UnityEngine::TextureFormat::RGB24;
66+
case 4:
67+
default:
68+
return UnityEngine::TextureFormat::RGBA32;
69+
}
70+
}
71+
72+
} // namespace
73+
74+
UnityEngine::Texture
75+
TextureLoader::loadTexture(const CesiumGltf::ImageCesium& image) {
76+
CESIUM_TRACE("TextureLoader::loadTexture");
77+
std::int32_t mipCount =
78+
image.mipPositions.empty() ? 1 : std::int32_t(image.mipPositions.size());
79+
80+
UnityEngine::TextureFormat textureFormat;
81+
if (image.compressedPixelFormat != GpuCompressedPixelFormat::NONE) {
82+
textureFormat = getCompressedPixelFormat(image);
83+
} else {
84+
textureFormat = getUncompressedPixelFormat(image);
7285
}
7386

7487
UnityEngine::Texture2D

0 commit comments

Comments
 (0)