Skip to content

Commit

Permalink
fix: importing files without baseMap but using texture transforms for…
Browse files Browse the repository at this point in the history
… normal or emissive would result in texture transforms not being used
  • Loading branch information
hybridherbst committed Dec 14, 2022
1 parent 74c594e commit 06e86eb
Showing 1 changed file with 43 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,43 @@ protected virtual async Task ConstructMaterial(GLTFMaterial def, int materialInd
uniformMapper.NormalXRotation = ext.Rotation;
uniformMapper.NormalXScale = ext.Scale.ToUnityVector2Raw();
uniformMapper.NormalXTexCoord = ext.TexCoord;

mapper.Material.SetKeyword("_TEXTURE_TRANSFORM", true);

// workaround for PBRGraph not supporting per-texture transforms right now
if (mapper is IMetalRoughUniformMap map && !map.BaseColorTexture)
{
map.BaseColorXScale = ext.Scale.ToUnityVector2Raw();
map.BaseColorXOffset = offset;
}
}
}

if (def.EmissiveTexture != null)
{
TextureId textureId = def.EmissiveTexture.Index;
await ConstructTexture(textureId.Value, textureId.Id, !KeepCPUCopyOfTexture, false);
uniformMapper.EmissiveTexture = _assetCache.TextureCache[textureId.Id].Texture;
uniformMapper.EmissiveTexCoord = def.EmissiveTexture.TexCoord;

var ext = GetTextureTransform(def.EmissiveTexture);
if (ext != null)
{
var offset = ext.Offset.ToUnityVector2Raw();
offset.y = 1 - ext.Scale.Y - offset.y;
uniformMapper.EmissiveXOffset = offset;
uniformMapper.EmissiveXRotation = ext.Rotation;
uniformMapper.EmissiveXScale = ext.Scale.ToUnityVector2Raw();
uniformMapper.EmissiveXTexCoord = ext.TexCoord;

mapper.Material.SetKeyword("_TEXTURE_TRANSFORM", true);

// workaround for PBRGraph not supporting per-texture transforms right now
if (mapper is IMetalRoughUniformMap map && !map.BaseColorTexture && !map.NormalTexture)
{
map.BaseColorXScale = ext.Scale.ToUnityVector2Raw();
map.BaseColorXOffset = offset;
}
}
}

Expand All @@ -331,25 +368,13 @@ protected virtual async Task ConstructMaterial(GLTFMaterial def, int materialInd
// mapper.OcclusionXTexCoord = ext.TexCoord;

mapper.Material.SetKeyword("_TEXTURE_TRANSFORM", true);
}
}

if (def.EmissiveTexture != null)
{
TextureId textureId = def.EmissiveTexture.Index;
await ConstructTexture(textureId.Value, textureId.Id, !KeepCPUCopyOfTexture, false);
uniformMapper.EmissiveTexture = _assetCache.TextureCache[textureId.Id].Texture;
uniformMapper.EmissiveTexCoord = def.EmissiveTexture.TexCoord;

var ext = GetTextureTransform(def.EmissiveTexture);
if (ext != null)
{
var offset = ext.Offset.ToUnityVector2Raw();
offset.y = 1 - ext.Scale.Y - offset.y;
uniformMapper.EmissiveXOffset = offset;
uniformMapper.EmissiveXRotation = ext.Rotation;
uniformMapper.EmissiveXScale = ext.Scale.ToUnityVector2Raw();
uniformMapper.EmissiveXTexCoord = ext.TexCoord;
// workaround for PBRGraph not supporting per-texture transforms right now
if (mapper is IMetalRoughUniformMap map && !map.BaseColorTexture && !map.NormalTexture && !map.EmissiveTexture)
{
map.BaseColorXScale = ext.Scale.ToUnityVector2Raw();
map.BaseColorXOffset = offset;
}
}
}

Expand Down

0 comments on commit 06e86eb

Please sign in to comment.