Skip to content

Commit

Permalink
Fixed texture mapping error with Draco meshes.
Browse files Browse the repository at this point in the history
There was a discrepancy between regular and Draco compressed meshes, since regular meshes' UVs were corrected into Unity Standard shader's space (different vertical origin; top vs. bottom).
Now, instead of fixing/changing UV coordinates the material's texture transform is set properly to counter-balance this.
  • Loading branch information
atteneder committed Sep 14, 2019
1 parent 037ff16 commit 8f748ac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 7 additions & 0 deletions Runtime/Scripts/DefaultMaterialGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace GLTFast {

public class DefaultMaterialGenerator : IMaterialGenerator {

static readonly Vector2 TEXTURE_SCALE = new Vector2(1,-1);
static readonly Vector2 TEXTURE_OFFSET = new Vector2(0,1);

Material defaultMaterial;

public UnityEngine.Material GetDefaultMaterial() {
Expand All @@ -23,13 +26,17 @@ public UnityEngine.Material GenerateMaterial( Schema.Material gltfMaterial, Sche
var material = Material.Instantiate<Material>( GetDefaultMaterial() );
material.name = gltfMaterial.name;

material.mainTextureScale = TEXTURE_SCALE;
material.mainTextureOffset = TEXTURE_OFFSET;

if(gltfMaterial.pbrMetallicRoughness!=null) {
material.color = gltfMaterial.pbrMetallicRoughness.baseColor;
material.SetFloat(StandardShaderHelper.metallicPropId, gltfMaterial.pbrMetallicRoughness.metallicFactor );
material.SetFloat(StandardShaderHelper.glossinessPropId, 1-gltfMaterial.pbrMetallicRoughness.roughnessFactor );

var mainTxt = GetTexture(gltfMaterial.pbrMetallicRoughness.baseColorTexture,textures,images);
if(mainTxt!=null) {
mainTxt.wrapMode = TextureWrapMode.Clamp;
material.mainTexture = mainTxt;
}

Expand Down
12 changes: 4 additions & 8 deletions Runtime/Scripts/Jobs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void Execute()
for (var i = 0; i < count; i++)
{
result[i].x = input[i*2] / 255f;
result[i].y = 1 - input[i*2+1] / 255f;
result[i].y = input[i*2+1] / 255f;
}
}
}
Expand All @@ -118,7 +118,7 @@ public void Execute()
for (var i = 0; i < count; i++)
{
result[i].x = input[i*2] / Constants.UINT16_MAX;
result[i].y = 1 - input[i*2+1] / Constants.UINT16_MAX;
result[i].y = input[i*2+1] / Constants.UINT16_MAX;
}
}
}
Expand All @@ -143,10 +143,6 @@ public void Execute() {
count*8,
count*8
);
for (var i = 0; i < count; i++)
{
result[i].y = 1 - result[i].y;
}
}
}

Expand All @@ -173,7 +169,7 @@ public void Execute()
for (var i = 0; i < count; i++)
{
result[i].x = *off / 255f;
result[i].y = 1 - *(off+1) / 255f;
result[i].y = *(off+1) / 255f;
off += byteStride;
}
}
Expand Down Expand Up @@ -203,7 +199,7 @@ public void Execute()
{
System.UInt16* uv = (System.UInt16*) off;
result[i].x = *uv / Constants.UINT16_MAX;
result[i].y = 1 - *(uv+1) / Constants.UINT16_MAX;
result[i].y = *(uv+1) / Constants.UINT16_MAX;
off += byteStride;
}
}
Expand Down

0 comments on commit 8f748ac

Please sign in to comment.