Skip to content

Commit

Permalink
Add flag to texture loader for sRGB textures
Browse files Browse the repository at this point in the history
  • Loading branch information
azrogers committed Jan 30, 2024
1 parent 99c6a02 commit ffd05a0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
14 changes: 8 additions & 6 deletions native~/Runtime/src/TextureLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using namespace DotNet;
namespace CesiumForUnityNative {

UnityEngine::Texture
TextureLoader::loadTexture(const CesiumGltf::ImageCesium& image) {
TextureLoader::loadTexture(const CesiumGltf::ImageCesium& image, bool sRGB) {
CESIUM_TRACE("TextureLoader::loadTexture");
std::int32_t mipCount =
image.mipPositions.empty() ? 1 : std::int32_t(image.mipPositions.size());
Expand Down Expand Up @@ -72,7 +72,7 @@ TextureLoader::loadTexture(const CesiumGltf::ImageCesium& image) {
}

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

Unity::Collections::NativeArray1<std::uint8_t> textureData =
Expand Down Expand Up @@ -112,25 +112,27 @@ TextureLoader::loadTexture(const CesiumGltf::ImageCesium& image) {

UnityEngine::Texture TextureLoader::loadTexture(
const CesiumGltf::Model& model,
std::int32_t textureIndex) {
std::int32_t textureIndex,
bool sRGB) {
const Texture* pTexture = Model::getSafe(&model.textures, textureIndex);
if (pTexture) {
return TextureLoader::loadTexture(model, *pTexture);
return TextureLoader::loadTexture(model, *pTexture, sRGB);
} else {
return UnityEngine::Texture(nullptr);
}
}

UnityEngine::Texture TextureLoader::loadTexture(
const CesiumGltf::Model& model,
const CesiumGltf::Texture& texture) {
const CesiumGltf::Texture& texture,
bool sRGB) {
const Image* pImage = Model::getSafe(&model.images, texture.source);
if (!pImage) {
return UnityEngine::Texture(nullptr);
}

const ImageCesium& imageCesium = pImage->cesium;
UnityEngine::Texture unityTexture = loadTexture(imageCesium);
UnityEngine::Texture unityTexture = loadTexture(imageCesium, sRGB);

const Sampler* pSampler = Model::getSafe(&model.samplers, texture.sampler);
if (pSampler) {
Expand Down
11 changes: 7 additions & 4 deletions native~/Runtime/src/TextureLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ namespace CesiumForUnityNative {
class TextureLoader {
public:
static ::DotNet::UnityEngine::Texture
loadTexture(const CesiumGltf::ImageCesium& image);
loadTexture(const CesiumGltf::ImageCesium& image, bool sRGB);

static ::DotNet::UnityEngine::Texture
loadTexture(const CesiumGltf::Model& model, std::int32_t textureIndex);
static ::DotNet::UnityEngine::Texture loadTexture(
const CesiumGltf::Model& model,
std::int32_t textureIndex,
bool sRGB);

static ::DotNet::UnityEngine::Texture loadTexture(
const CesiumGltf::Model& model,
const CesiumGltf::Texture& texture);
const CesiumGltf::Texture& texture,
bool sRGB);
};

} // namespace CesiumForUnityNative
23 changes: 15 additions & 8 deletions native~/Runtime/src/UnityPrepareRendererResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1189,8 +1189,10 @@ void* UnityPrepareRendererResources::prepareInMainThread(
auto texCoordIndexIt =
primitiveInfo.uvIndexMap.find(baseColorTexture->texCoord);
if (texCoordIndexIt != primitiveInfo.uvIndexMap.end()) {
UnityEngine::Texture texture =
TextureLoader::loadTexture(gltf, baseColorTexture->index);
UnityEngine::Texture texture = TextureLoader::loadTexture(
gltf,
baseColorTexture->index,
true);
if (texture != nullptr) {
material.SetTexture(
shaderProperty.getBaseColorTextureID(),
Expand All @@ -1208,8 +1210,10 @@ void* UnityPrepareRendererResources::prepareInMainThread(
auto texCoordIndexIt =
primitiveInfo.uvIndexMap.find(metallicRoughness->texCoord);
if (texCoordIndexIt != primitiveInfo.uvIndexMap.end()) {
UnityEngine::Texture texture =
TextureLoader::loadTexture(gltf, metallicRoughness->index);
UnityEngine::Texture texture = TextureLoader::loadTexture(
gltf,
metallicRoughness->index,
false);
if (texture != nullptr) {
material.SetTexture(
shaderProperty.getMetallicRoughnessTextureID(),
Expand All @@ -1229,7 +1233,8 @@ void* UnityPrepareRendererResources::prepareInMainThread(
if (texCoordIndexIt != primitiveInfo.uvIndexMap.end()) {
UnityEngine::Texture texture = TextureLoader::loadTexture(
gltf,
pMaterial->normalTexture->index);
pMaterial->normalTexture->index,
false);
if (texture != nullptr) {
material.SetTexture(
shaderProperty.getNormalMapTextureID(),
Expand All @@ -1250,7 +1255,8 @@ void* UnityPrepareRendererResources::prepareInMainThread(
if (texCoordIndexIt != primitiveInfo.uvIndexMap.end()) {
UnityEngine::Texture texture = TextureLoader::loadTexture(
gltf,
pMaterial->occlusionTexture->index);
pMaterial->occlusionTexture->index,
false);
if (texture != nullptr) {
material.SetTexture(
shaderProperty.getOcclusionTextureID(),
Expand Down Expand Up @@ -1286,7 +1292,8 @@ void* UnityPrepareRendererResources::prepareInMainThread(
if (texCoordIndexIt != primitiveInfo.uvIndexMap.end()) {
UnityEngine::Texture texture = TextureLoader::loadTexture(
gltf,
pMaterial->emissiveTexture->index);
pMaterial->emissiveTexture->index,
true);
if (texture != nullptr) {
material.SetTexture(
shaderProperty.getEmissiveTextureID(),
Expand Down Expand Up @@ -1460,7 +1467,7 @@ void* UnityPrepareRendererResources::prepareRasterInMainThread(
Cesium3DTilesSelection::RasterOverlayTile& rasterTile,
void* pLoadThreadResult) {
auto pTexture = std::make_unique<UnityEngine::Texture>(
TextureLoader::loadTexture(rasterTile.getImage()));
TextureLoader::loadTexture(rasterTile.getImage(), true));
pTexture->wrapMode(UnityEngine::TextureWrapMode::Clamp);
pTexture->filterMode(UnityEngine::FilterMode::Trilinear);
pTexture->anisoLevel(16);
Expand Down

0 comments on commit ffd05a0

Please sign in to comment.