Skip to content

Commit 3d4f187

Browse files
committed
Flip normals at negative node scale
1 parent 108691d commit 3d4f187

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Source/RunActivity/Content/SceneryShader.fx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ float4 _PSTex2D(sampler s, float4 inTexCoords, float texCoordsSelector)
788788

789789
float3 _PSGetNormal(in VERTEX_OUTPUT_PBR In, bool hasTangents, float normalScale, sampler normalSampler, float texCoordsSelector, bool isFrontFace)
790790
{
791-
bool hasNormalMap = normalScale > 0;
791+
bool hasNormalMap = -1 <= normalScale && normalScale <= 1;
792792
float3x3 tbn = float3x3(In.Tangent.xyz, In.Bitangent.xyz, In.Normal_Light.xyz);
793793
if (!hasTangents || !HasNormals)
794794
{
@@ -841,7 +841,9 @@ float3 _PSGetNormal(in VERTEX_OUTPUT_PBR In, bool hasTangents, float normalScale
841841
n = normalize(mul((n * float3(normalScale, normalScale, 1.0)), tbn));
842842
}
843843
else
844-
n = tbn[2].xyz;
844+
{
845+
n = tbn[2].xyz * sign(normalScale);
846+
}
845847

846848
return n;
847849
}

Source/RunActivity/Viewer3D/GltfShape.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,12 +1090,15 @@ public GltfSubObject(MeshPrimitive meshPrimitive, string name, int hierarchyInde
10901090
if (meshPrimitive.Targets != null && meshPrimitive.Targets.Length > 0)
10911091
options |= SceneryMaterialOptions.PbrHasMorphTargets;
10921092

1093+
var flipNormals = 1f;
10931094
if (!shape.MsfsFlavoured && distanceLevel.Matrices.ElementAt(hierarchyIndex).Determinant() > 0)
10941095
// This is according to the glTF spec
10951096
options |= SceneryMaterialOptions.PbrCullClockWise;
10961097
else if (shape.MsfsFlavoured && distanceLevel.Matrices.ElementAt(hierarchyIndex).Determinant() < 0)
10971098
// Msfs seems to be using this reversed
10981099
options |= SceneryMaterialOptions.PbrCullClockWise;
1100+
else
1101+
flipNormals = -1f;
10991102

11001103
var referenceAlpha = 0f;
11011104
var doubleSided = material.DoubleSided;
@@ -1167,12 +1170,12 @@ public GltfSubObject(MeshPrimitive meshPrimitive, string name, int hierarchyInde
11671170
var baseColorFactor = MemoryMarshal.Cast<float, Vector4>(material.PbrMetallicRoughness?.BaseColorFactor ?? new[] { 1f, 1f, 1f, 1f })[0];
11681171
var metallicFactor = material.PbrMetallicRoughness?.MetallicFactor ?? 1f;
11691172
var roughtnessFactor = material.PbrMetallicRoughness?.RoughnessFactor ?? 1f;
1170-
var normalScale = material.NormalTexture?.Scale ?? 0; // Must be 0 only if the textureInfo is missing, otherwise it must have the default value 1.
1173+
var normalScale = flipNormals * (material.NormalTexture?.Scale ?? 2f); // 2 indicates the textureInfo is missing, otherwise it get default 1
11711174
var occlusionStrength = material.OcclusionTexture?.Strength ?? 0; // Must be 0 only if the textureInfo is missing, otherwise it must have the default value 1.
11721175
var emissiveFactor = MemoryMarshal.Cast<float, Vector3>(material.EmissiveFactor ?? new[] { 0f, 0f, 0f })[0] * emissiveStrength;
11731176
var clearcoatFactor = clearcoat?.ClearcoatFactor ?? 0;
11741177
var clearcoatRoughnessFactor = clearcoat?.ClearcoatRoughnessFactor ?? 0;
1175-
var clearcoatNormalScale = clearcoat?.ClearcoatNormalTexture?.Scale ?? 1;
1178+
var clearcoatNormalScale = flipNormals * (clearcoat?.ClearcoatNormalTexture?.Scale ?? 2f);
11761179

11771180
switch (baseColorSamplerState.Item2)
11781181
{
@@ -1844,7 +1847,7 @@ public void Animate(int animationNumber, float time, Matrix[] animatedMatrices)
18441847
{ "MorphPrimitivesTest".ToLower(), Matrix.CreateScale(2) * Matrix.CreateTranslation(0, 1, 0) },
18451848
{ "MosquitoInAmber".ToLower(), Matrix.CreateScale(25) * Matrix.CreateTranslation(0, 1, 0) },
18461849
{ "MultiUVTest".ToLower(), Matrix.CreateTranslation(0, 2, 0) },
1847-
{ "NegativeScaleTest".ToLower(), Matrix.CreateTranslation(0, 3, 0) },
1850+
{ "NegativeScaleTest".ToLower(), Matrix.CreateTranslation(0, 4, 0) },
18481851
{ "NormalTangentMirrorTest".ToLower(), Matrix.CreateScale(2) * Matrix.CreateTranslation(0, 2, 0) },
18491852
{ "NormalTangentTest".ToLower(), Matrix.CreateScale(2) * Matrix.CreateTranslation(0, 2, 0) },
18501853
{ "OrientationTest".ToLower(), Matrix.CreateScale(0.2f) * Matrix.CreateTranslation(0, 2, 0) },
@@ -1857,9 +1860,11 @@ public void Animate(int animationNumber, float time, Matrix[] animatedMatrices)
18571860
{ "SciFiHelmet".ToLower(), Matrix.CreateTranslation(0, 2, 0) },
18581861
{ "SheenChair".ToLower(), Matrix.CreateScale(2) },
18591862
{ "SheenCloth".ToLower(), Matrix.CreateScale(50) },
1863+
{ "SheenTestGrid".ToLower(), Matrix.CreateScale(5) * Matrix.CreateTranslation(0, 4, 0)},
18601864
{ "SpecGlossVsMetalRough".ToLower(), Matrix.CreateScale(7) * Matrix.CreateTranslation(0, 2, 0) },
18611865
{ "SpecularTest".ToLower(), Matrix.CreateScale(5) * Matrix.CreateTranslation(0, 2, 0) },
18621866
{ "StainedGlassLamp".ToLower(), Matrix.CreateScale(3) },
1867+
{ "SunglassesKhronos".ToLower(), Matrix.CreateScale(30) },
18631868
{ "Suzanne".ToLower(), Matrix.CreateTranslation(0, 2, 0) },
18641869
{ "TextureCoordinateTest".ToLower(), Matrix.CreateTranslation(0, 2, 0) },
18651870
{ "TextureEncodingTest".ToLower(), Matrix.CreateScale(0.5f) * Matrix.CreateTranslation(0, 3, 0) },

0 commit comments

Comments
 (0)