Skip to content

Commit da85660

Browse files
[Rendering] WIP on re-adding lighting support;
1 parent 1d5e93d commit da85660

File tree

19 files changed

+555
-344
lines changed

19 files changed

+555
-344
lines changed

BuiltinResources/Hidden/Shaders/Default/Standard.shader

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ texture displacementTexture
1313
texture emissiveTexture
1414
texture heightTexture
1515
texture specularTexture
16-
float3 viewPosition;
1716
color diffuseColor = #FFFFFFFF
1817
color emissiveColor
1918
color specularColor
@@ -41,7 +40,6 @@ Begin Common
4140
[[vk::binding(StapleUniformBufferStart, StapleUniformBufferSet)]]
4241
cbuffer Uniforms
4342
{
44-
float3 viewPosition;
4543
float4 diffuseColor;
4644
float4 emissiveColor;
4745
float4 specularColor;
@@ -53,7 +51,9 @@ struct VertexOutput
5351
{
5452
float4 position : SV_Position;
5553
float3 worldPosition;
54+
#ifdef LIT
5655
float3 lightNormal;
56+
#endif
5757
float2 coords;
5858
float3 normal;
5959
#ifdef NORMALMAP
@@ -111,26 +111,25 @@ VertexOutput VertexMain(Input input)
111111

112112
output.coords = input.coords;
113113
output.normal = input.normal;
114+
114115
#ifdef NORMALMAP
115116
output.tangent = input.tangent;
116117
output.bitangent = input.bitangent;
117118
#endif
119+
120+
#ifdef LIT
118121
output.lightNormal = StapleLightNormal(input.normal, model);
122+
#endif
123+
119124
output.instanceID = input.instanceID;
120125

121-
//TODO: handle light array
122-
/*
123126
#if defined(LIT) && defined(PER_VERTEX_LIGHTING)
124-
output.color = float4x4(diffuseColor.rgb * StapleProcessLights(viewPosition, output.worldPosition, input.normal), diffuseColor.a);
127+
output.color = float4(diffuseColor.rgb * StapleProcessLights(output.worldPosition, output.lightNormal), diffuseColor.a);
125128

126129
#ifdef VERTEX_COLORS
127130
output.color = input.color * output.color;
128131
#endif
129-
#else
130-
output.color = input.color;
131-
#endif
132-
*/
133-
#if defined(VERTEX_COLORS) || defined(PER_VERTEX_LIGHTING)
132+
#elif defined(VERTEX_COLORS) || defined(PER_VERTEX_LIGHTING)
134133
output.color = input.color;
135134
#endif
136135

@@ -168,28 +167,24 @@ float4 FragmentMain(VertexOutput input) : SV_Target
168167
}
169168
#endif
170169

171-
//TODO: handle light array
172-
/*
173170
#if defined(LIT) && defined(PER_VERTEX_LIGHTING)
174171
return diffuse;
175172
#elif defined(LIT)
176173

177174
#ifdef NORMALMAP
178175
float3x3 tbn = float3x3(normalize(input.tangent), normalize(input.bitangent), normalize(input.normal));
179176

180-
float3 normalMapNormal = normalize(normalTexture.Sample(v_texcoord0).xyz * 2.0 - 1.0);
177+
float3 normalMapNormal = normalize(normalTexture.Sample(input.coords).xyz * 2.0 - 1.0);
181178

182-
float3 light = StapleProcessLightsTangent(viewPosition, input.worldPosition, normalMapNormal, tbn);
179+
float3 light = StapleProcessLightsTangent(input.worldPosition, normalMapNormal, tbn);
183180
#else
184-
float3 light = StapleProcessLights(viewPosition, input.worldPosition, input.lightNormal);
181+
float3 light = StapleProcessLights(input.worldPosition, input.lightNormal);
185182
#endif
186183

187184
return float4(light, 1) * diffuse;
188185
#else
189186
return diffuse;
190187
#endif
191-
*/
192-
return diffuse;
193188
}
194189

195190
End Fragment

Engine/Staple.Core/Math/Color.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public Color(string value)
9797

9898
public static implicit operator Vector4(Color v) => new(v.r, v.g, v.b, v.a);
9999

100+
public static implicit operator Vector3(Color v) => new(v.r, v.g, v.b);
101+
100102
public static Color operator+(Color a, Color b) => new(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a);
101103

102104
public static Color operator-(Color a, Color b) => new(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a);

Engine/Staple.Core/Math/Color32.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public Color32(uint value)
9898

9999
public static implicit operator Vector4(Color32 v) => new(v.r / 255.0f, v.g / 255.0f, v.b / 255.0f, v.a / 255.0f);
100100

101+
public static implicit operator Vector3(Color32 v) => new(v.r / 255.0f, v.g / 255.0f, v.b / 255.0f);
102+
101103
public static Color32 operator+(Color32 a, Color32 b) => new((byte)Math.Clamp(a.r + b.r, 0, 255),
102104
(byte)Math.Clamp(a.g + b.g, 0, 255),
103105
(byte)Math.Clamp(a.b + b.b, 0, 255),

Engine/Staple.Core/Rendering/Animation/SkinnedMeshRenderSystem.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,9 @@ void SetupMaterial()
317317

318318
renderer.mesh.SetActive(ref renderState, j);
319319

320-
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting);
320+
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting, ref renderState);
321321

322-
var buffer = instance.boneBuffer;
323-
324-
renderState.storageBuffers = [(0, buffer)];
322+
renderState.ApplyStorageBufferIfNeeded("StapleBoneMatrices", instance.boneBuffer);
325323

326324
RenderSystem.Submit(renderState, renderer.mesh.SubmeshTriangleCount(j), 1);
327325
}

Engine/Staple.Core/Rendering/Graphics.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static void RenderGeometry(VertexBuffer vertex, IndexBuffer index,
7676
return;
7777
}
7878

79-
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting);
79+
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting, ref renderState);
8080

8181
RenderSystem.Submit(renderState, Mesh.TriangleCount(topology, indexCount), 1);
8282
}
@@ -121,7 +121,7 @@ public static void RenderSimple<T>(Span<T> vertices, VertexLayout layout, Span<u
121121
return;
122122
}
123123

124-
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting);
124+
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting, ref renderState);
125125

126126
RenderSystem.Backend.RenderTransient(vertices, layout, indices, renderState);
127127
}
@@ -166,7 +166,7 @@ public static void RenderSimple<T>(Span<T> vertices, VertexLayout layout, Span<u
166166
return;
167167
}
168168

169-
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting);
169+
lightSystem?.ApplyLightProperties(material, RenderSystem.CurrentCamera.Item2.Position, lighting, ref renderState);
170170

171171
RenderSystem.Backend.RenderTransient(vertices, layout, indices, renderState);
172172
}

0 commit comments

Comments
 (0)