Skip to content

Commit

Permalink
Merge pull request #347 from CesiumGS/incomplete-mip-chains
Browse files Browse the repository at this point in the history
Handle incomplete mip chains correctly
  • Loading branch information
j9liu authored Jul 19, 2023
2 parents 69711b3 + 30982d9 commit db8b4ba
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

### ? - ?

##### Fixes :wrench:

- Fixed a bug that could lead to incorrect textures when a KTX2 image did not include a complete mip chain.

### v1.4.0 - 2023-07-03

##### Additions :tada:
Expand Down
1 change: 1 addition & 0 deletions Runtime/ConfigureReinterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public void ExposeToCPP()
go.hideFlags = HideFlags.DontSave;

Texture2D texture2D = new Texture2D(256, 256, TextureFormat.RGBA32, false, false);
texture2D = new Texture2D(256, 256, TextureFormat.RGBA32, 1, false);
texture2D.LoadRawTextureData(IntPtr.Zero, 0);
NativeArray<byte> textureBytes = texture2D.GetRawTextureData<byte>();

Expand Down
7 changes: 4 additions & 3 deletions native~/Runtime/src/TextureLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ namespace CesiumForUnityNative {
UnityEngine::Texture
TextureLoader::loadTexture(const CesiumGltf::ImageCesium& image) {
CESIUM_TRACE("TextureLoader::loadTexture");
bool useMipMaps = !image.mipPositions.empty();
std::int32_t mipCount =
image.mipPositions.empty() ? 1 : std::int32_t(image.mipPositions.size());

UnityEngine::TextureFormat textureFormat;

Expand Down Expand Up @@ -71,7 +72,7 @@ TextureLoader::loadTexture(const CesiumGltf::ImageCesium& image) {
}

UnityEngine::Texture2D
result(image.width, image.height, textureFormat, useMipMaps, false);
result(image.width, image.height, textureFormat, mipCount, false);
result.hideFlags(UnityEngine::HideFlags::HideAndDontSave);

Unity::Collections::NativeArray1<std::uint8_t> textureData =
Expand All @@ -83,7 +84,7 @@ TextureLoader::loadTexture(const CesiumGltf::ImageCesium& image) {
size_t textureLength = textureData.Length();
assert(textureLength >= image.pixelData.size());

if (!useMipMaps) {
if (image.mipPositions.empty()) {
// No mipmaps, copy the whole thing and then let Unity generate mipmaps on a
// worker thread.
std::memcpy(pixels, image.pixelData.data(), image.pixelData.size());
Expand Down

0 comments on commit db8b4ba

Please sign in to comment.