Skip to content

Commit edb963e

Browse files
committed
Fix calculation of normals when they are missing
1 parent df378f0 commit edb963e

File tree

4 files changed

+41
-38
lines changed

4 files changed

+41
-38
lines changed

Source/RunActivity/Content/SceneryShader.fx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ float OverlayScale;
5656
// MAX_LIGHTS must not be less than 2
5757
// MAX_BONES must not be less than 1
5858
#define MAX_LIGHTS 20
59-
#define MAX_BONES 50
59+
#define MAX_BONES 128
6060
#define CLEARCOAT
6161
#define MAX_MORPH_TARGETS 8
6262

@@ -492,7 +492,9 @@ VERTEX_OUTPUT_PBR VSSkinned(in VERTEX_INPUT_SKINNED In)
492492
Out.Position = mul(In.Position, skinTransform);
493493
float4 worldPosition = Out.Position;
494494
_VSNormalProjection(In.Normal, skinTransform, Out.Position, Out.RelPosition, Out.Normal_Light);
495+
Out.RelPosition.xyz = worldPosition - ViewerPos; // Need to amend the calculation of _VSNormalProjection here, because of the above.
495496
_VSLightsAndShadows(worldPosition, skinTransform, length(Out.Position.xyz), Out.Tangent.w, Out.Shadow);
497+
Out.Shadow = worldPosition; // Need to amend this as well.
496498

497499
_VSNormalMapTransform(In.Tangent, In.Normal, skinTransform, Out);
498500

@@ -542,7 +544,9 @@ VERTEX_OUTPUT_PBR VSMorphing(in VERTEX_INPUT_MORPHED In)
542544
Out.Position = mul(Out.Position, skinTransform);
543545
float4 worldPosition = Out.Position;
544546
_VSNormalProjection(normal, skinTransform, Out.Position, Out.RelPosition, Out.Normal_Light);
547+
Out.RelPosition.xyz = worldPosition - ViewerPos; // Need to amend the calculation of _VSNormalProjection here, because of the above.
545548
_VSLightsAndShadows(worldPosition, skinTransform, length(Out.Position.xyz), Out.Tangent.w, Out.Shadow);
549+
Out.Shadow = worldPosition; // Need to amend this as well.
546550

547551
_VSNormalMapTransform(tangent, normal, skinTransform, Out);
548552

@@ -797,19 +801,18 @@ float3 _PSGetNormal(in VERTEX_OUTPUT_PBR In, bool hasTangents, float normalScale
797801
float3x3 tbn = float3x3(In.Tangent.xyz, In.Bitangent.xyz, In.Normal_Light.xyz);
798802
if (!hasTangents || !HasNormals)
799803
{
800-
float3 pos_dx = ddx(In.Position.xyz);
801-
float3 pos_dy = ddy(In.Position.xyz);
802-
803804
float3 ng;
804805
if (HasNormals)
805806
ng = normalize(In.Normal_Light.xyz);
806807
else
807-
ng = cross(pos_dx, pos_dy);
808+
ng = normalize(cross(ddx(In.RelPosition.xyz), ddy(-In.RelPosition.xyz)));
808809
tbn[2].xyz = ng;
809810

810811
if (hasNormalMap)
811812
{
812-
float3 tex_dx = -ddx(float3(In.TexCoords.xy, 0.0));
813+
float3 pos_dx = ddx(In.Position.xyz);
814+
float3 pos_dy = ddy(In.Position.xyz);
815+
float3 tex_dx = -ddx(float3(In.TexCoords.xy, 0.0));
813816
float3 tex_dy = -ddy(float3(In.TexCoords.xy, 0.0));
814817
float tex_dxy = tex_dx.x * tex_dy.y - tex_dy.x * tex_dx.y;
815818
float3 t = (tex_dy.y * pos_dx - tex_dx.y * pos_dy) / tex_dxy;

Source/RunActivity/Content/ShadowMap.fx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
//////////////////// G L O B A L V A L U E S ///////////////////////////
2525

26-
#define MAX_BONES 50
26+
#define MAX_BONES 128
2727
#define MAX_MORPH_TARGETS 8
2828

2929
float4x4 WorldViewProjection; // model -> world -> view -> projection
@@ -92,7 +92,7 @@ struct VERTEX_INPUT_SKINNED
9292
float3 Normal : NORMAL;
9393
float4 Tangent : TANGENT;
9494
float2 TexCoordsPbr: TEXCOORD1;
95-
uint4 Joints : BLENDINDICES0;
95+
min16uint4 Joints : BLENDINDICES0;
9696
float4 Weights : BLENDWEIGHT0;
9797
float4 Color : COLOR0;
9898
float4x4 Instance : TEXCOORD2;
@@ -296,22 +296,22 @@ float4 PSShadowMapBlur(in VERTEX_OUTPUT_BLUR In) : COLOR0
296296

297297
technique ShadowMap {
298298
pass Pass_0 {
299-
VertexShader = compile vs_4_0_level_9_1 VSShadowMap();
300-
PixelShader = compile ps_4_0_level_9_1 PSShadowMap();
299+
VertexShader = compile vs_4_0 VSShadowMap();
300+
PixelShader = compile ps_4_0 PSShadowMap();
301301
}
302302
}
303303

304304
technique ShadowMapNormalMap {
305305
pass Pass_0 {
306-
VertexShader = compile vs_4_0_level_9_1 VSShadowMapNormalMap();
307-
PixelShader = compile ps_4_0_level_9_1 PSShadowMap();
306+
VertexShader = compile vs_4_0 VSShadowMapNormalMap();
307+
PixelShader = compile ps_4_0 PSShadowMap();
308308
}
309309
}
310310

311311
technique ShadowMapSkinned {
312312
pass Pass_0 {
313-
VertexShader = compile vs_4_0_level_9_1 VSShadowMapSkinned();
314-
PixelShader = compile ps_4_0_level_9_1 PSShadowMap();
313+
VertexShader = compile vs_4_0 VSShadowMapSkinned();
314+
PixelShader = compile ps_4_0 PSShadowMap();
315315
}
316316
}
317317

@@ -324,25 +324,25 @@ technique ShadowMapMorphed {
324324

325325
technique ShadowMapForest {
326326
pass Pass_0 {
327-
VertexShader = compile vs_4_0_level_9_1 VSShadowMapForest();
328-
PixelShader = compile ps_4_0_level_9_1 PSShadowMap();
327+
VertexShader = compile vs_4_0 VSShadowMapForest();
328+
PixelShader = compile ps_4_0 PSShadowMap();
329329
}
330330
}
331331

332332
technique ShadowMapBlocker {
333333
pass Pass_0 {
334-
VertexShader = compile vs_4_0_level_9_1 VSShadowMap();
335-
PixelShader = compile ps_4_0_level_9_1 PSShadowMapBlocker();
334+
VertexShader = compile vs_4_0 VSShadowMap();
335+
PixelShader = compile ps_4_0 PSShadowMapBlocker();
336336
}
337337
}
338338

339339
technique ShadowMapBlur {
340340
pass Blur_X {
341-
VertexShader = compile vs_4_0_level_9_1 VSShadowMapHorzBlur();
342-
PixelShader = compile ps_4_0_level_9_1 PSShadowMapBlur();
341+
VertexShader = compile vs_4_0 VSShadowMapHorzBlur();
342+
PixelShader = compile ps_4_0 PSShadowMapBlur();
343343
}
344344
pass Blur_Y {
345-
VertexShader = compile vs_4_0_level_9_1 VSShadowMapVertBlur();
346-
PixelShader = compile ps_4_0_level_9_1 PSShadowMapBlur();
345+
VertexShader = compile vs_4_0 VSShadowMapVertBlur();
346+
PixelShader = compile ps_4_0 PSShadowMapBlur();
347347
}
348348
}

Source/RunActivity/Viewer3D/GltfShape.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public override Matrix SetRenderMatrices(ShapePrimitive baseShapePrimitive, Matr
204204
public GltfDistanceLevel SetLod(int lodId)
205205
{
206206
var lod = LodControls.FirstOrDefault()?.DistanceLevels?.ElementAtOrDefault(lodId) as GltfDistanceLevel;
207-
if (lodId != LastLod)
207+
if (lodId != LastLod && lod != null)
208208
{
209209
Matrices = lod.Matrices;
210210
Scales = lod.Scales; ;
@@ -239,8 +239,14 @@ public GltfLodControl(GltfShape shape, Dictionary<int, string> externalLods)
239239
foreach (var extensionRequired in gltfFile.ExtensionsRequired)
240240
if (!ExtensionsSupported.Contains(extensionRequired))
241241
unsupportedExtensions.Add($"\"{extensionRequired}\"");
242-
if (unsupportedExtensions.Any() && ShapeWarnings)
243-
Trace.TraceWarning($"glTF required extension {string.Join(", ", unsupportedExtensions)} is unsupported in file {externalLods[id]}");
242+
if (unsupportedExtensions.Any())
243+
{
244+
var message = $"glTF required extension {string.Join(", ", unsupportedExtensions)} is unsupported in file {externalLods[id]}";
245+
if (ConsistGenerator.GltfVisualTestRun)
246+
Trace.TraceWarning(message);
247+
else
248+
throw new NotImplementedException(message);
249+
}
244250
}
245251

246252
if (gltfFile.Asset?.Extensions?.ContainsKey("ASOBO_asset_optimized") ?? false)
@@ -530,9 +536,9 @@ void GetBinaryData(GltfShape shape, Gltf gltfFile, string gltfFileName)
530536
foreach (var bufferView in bufferViews)
531537
{
532538
var byteStride = gltfFile.BufferViews.ElementAtOrDefault(bufferView.Key)?.ByteStride ?? GetSizeInBytes(gltfFile.Accessors[bufferView.First().Value]);
533-
534-
if (GetBufferViewSpan(bufferView.Key, 0) is var buffer && buffer.IsEmpty)
535-
buffer = new Span<byte>(new byte[gltfFile.BufferViews.ElementAtOrDefault(bufferView.Key).ByteLength]);
539+
540+
// Trigger the loading of the binary buffer.
541+
GetBufferViewSpan(bufferView.Key, 0);
536542

537543
var previousOffset = 0;
538544
var attributes = bufferView.GetEnumerator();
@@ -1178,7 +1184,7 @@ public GltfSubObject(MeshPrimitive meshPrimitive, string name, int hierarchyInde
11781184
{
11791185
vertexAttributes.Add(new VertexBufferBinding(new VertexBuffer(shape.Viewer.GraphicsDevice,
11801186
new VertexDeclaration(new VertexElement(0, VertexElementFormat.Color, VertexElementUsage.Normal, 0)), vertexCount, BufferUsage.None) { Name = "NORMAL_DUMMY" }));
1181-
// Do not set the SceneryMaterialOptions.PbrHasNormals flag here, so that the shader will know to calculate its own normals.
1187+
// Do not set the SceneryMaterialOptions.PbrHasNormals flag here, so that the shader will know to calculate its own normals. (See: Fox)
11821188
}
11831189
else
11841190
options |= SceneryMaterialOptions.PbrHasNormals;

Source/RunActivity/Viewer3D/Processes/RenderProcess.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ public class RenderProcess
6767
public static int[] ShadowMapDiameter; // diameter of shadow map
6868
public static float[] ShadowMapLimit; // diameter of shadow map far edge from camera
6969

70-
public static bool LEVEL_9_3;
71-
public static int MAX_BONES;
72-
public static int MAX_LIGHTS; // must not be lower than 2.
70+
public const int MAX_BONES = 128;
71+
public const int MAX_LIGHTS = 20; // must not be lower than 2.
7372
public const int MAX_MORPH_BUFFERS = 8;
74-
public static bool CLEARCOAT;
73+
public const bool CLEARCOAT = true;
7574

7675
internal RenderProcess(Game game)
7776
{
@@ -108,11 +107,6 @@ internal RenderProcess(Game game)
108107
GraphicsDeviceManager.PreferMultiSampling = (AntiAliasingMethod)Game.Settings.AntiAliasing != AntiAliasingMethod.None;
109108
GraphicsDeviceManager.HardwareModeSwitch = false; // for fast full-screen Alt-Tab switching
110109
GraphicsDeviceManager.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(GDM_PreparingDeviceSettings);
111-
112-
LEVEL_9_3 = false;// !Game.Settings.IsDirectXFeatureLevelIncluded(DirectXFeature.Level10_0);
113-
MAX_BONES = LEVEL_9_3 ? 1 : 50;
114-
MAX_LIGHTS = LEVEL_9_3 ? 2 : 20;
115-
CLEARCOAT = LEVEL_9_3 ? false : true;
116110
}
117111

118112
void GDM_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)

0 commit comments

Comments
 (0)