Skip to content

Commit

Permalink
Merge pull request #527 from FUSEEProjectTeam/feature/526-enable-vert…
Browse files Browse the repository at this point in the history
…ex-colors

Enable vertex colors
  • Loading branch information
RedImp1470 authored Jun 22, 2022
2 parents d57ff04 + b261ad5 commit dad84ee
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/Engine/Core/Effects/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public FxShardAttribute(ShardCategory category)
}

/// <summary>
/// Used to flag which shader type a <see cref="ShaderEffect"/> property belongs to.
/// Used to flag which shader type a <see cref="ShaderEffect"/> property belongs to.
/// Supports vertex, pixel and geometry shaders.
/// </summary>
[Flags]
Expand Down Expand Up @@ -102,7 +102,7 @@ public enum ShardCategory : ushort
/// </summary>
Main = 128,

/// <summary>
/// <summary>
/// Those are uniforms in the shader code but should not be properties of a <see cref="SurfaceEffectBase"/> because they will be updated by the SceneRenderer.
/// </summary>
InternalUniform = 256
Expand Down
16 changes: 16 additions & 0 deletions src/Engine/Core/Effects/SurfaceEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ namespace Fusee.Engine.Core.Effects
/// </summary>
public class SurfaceEffect : SurfaceEffectBase
{
/// <summary>
/// Specify <see cref="ShaderShards.ColorMode"/> for lighting calculation.
/// </summary>
[FxShader(ShaderCategory.Fragment)]
[FxShard(ShardCategory.Uniform)]
public ColorMode ColorMode
{
get { return _colorMode; }
set
{
_colorMode = value;
SetFxParam(nameof(ColorMode), _colorMode);
}
}
private ColorMode _colorMode;

#region Internal/Global Uniforms (set by the Engine)

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Core/Effects/SurfaceEffectInput.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Fusee.Engine.Common;
using Fusee.Engine.Common;
using Fusee.Engine.Core.ShaderShards;
using Fusee.Math.Core;
using System;
Expand Down
10 changes: 5 additions & 5 deletions src/Engine/Core/MakeEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class MakeEffect
/// <summary>
/// The default <see cref="Effect"/>, that is used if a <see cref="SceneNode"/> has a mesh but no effect.
/// </summary>
public static SurfaceEffectBase Default() => FromDiffuseSpecular(new float4(0.5f, 0.5f, 0.5f, 1.0f), 0f, 22, 1.0f);
public static SurfaceEffect Default() => FromDiffuseSpecular(new float4(0.5f, 0.5f, 0.5f, 1.0f), 0f, 22, 1.0f);

#region Deferred

Expand Down Expand Up @@ -142,11 +142,11 @@ public static ShaderEffect SSAORenderTargetBlurEffect(WritableTexture ssaoRender

/// <summary>
/// ShaderEffect that performs the lighting calculation according to the textures from the Geometry Pass.
/// </summary>
/// </summary>
/// <param name="srcRenderTarget">The source render target.</param>
/// <param name="lc">The light component.</param>
/// <param name="shadowMap">The shadow map.</param>
/// <param name="backgroundColor">Sets the background color. Could be replaced with a texture or other sky color calculations in the future.</param>
/// <param name="backgroundColor">Sets the background color. Could be replaced with a texture or other sky color calculations in the future.</param>
/// <returns></returns>
public static ShaderEffect DeferredLightingPassEffect(IRenderTarget srcRenderTarget, Light lc, float4 backgroundColor, IWritableTexture shadowMap = null)
{
Expand Down Expand Up @@ -187,7 +187,7 @@ public static ShaderEffect DeferredLightingPassEffect(IRenderTarget srcRenderTar

/// <summary>
/// [Parallel light only] ShaderEffect that performs the lighting calculation according to the textures from the Geometry Pass. Shadow is calculated with cascaded shadow maps.
/// </summary>
/// </summary>
/// <param name="srcRenderTarget">The source render target.</param>
/// <param name="lc">The light component.</param>
/// <param name="shadowMap">The cascaded shadow maps.</param>
Expand Down Expand Up @@ -580,7 +580,7 @@ private static string CreateDeferredLightingPixelShader(Light lc, bool isCascade
frag.Append(FragProperties.ColorOut());

//Shadow calculation methods
//--------------------------------------
//--------------------------------------
if (isCascaded)
frag.Append(Lighting.ShadowCalculationCascaded());
else if (lc.Type == LightType.Point)
Expand Down
11 changes: 7 additions & 4 deletions src/Engine/Core/ScenePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void GetTriangle(out float3 a, out float3 b, out float3 c)
b = Mesh.Vertices[Mesh.Triangles[Triangle + 1]];
c = Mesh.Vertices[Mesh.Triangles[Triangle + 2]];
}

/// <summary>
/// Returns the center of the picked triangle.
/// </summary>
Expand Down Expand Up @@ -148,7 +149,7 @@ public float3 CameraPos
}
}
/// <summary>
///
///
/// </summary>
public float2 UV
{
Expand Down Expand Up @@ -315,8 +316,8 @@ public void RenderCanvasTransform(CanvasTransform ctc)
var frustumCorners = new float4[4];

frustumCorners[0] = invProj * new float4(-1, -1, -1, 1); //nbl
frustumCorners[1] = invProj * new float4(1, -1, -1, 1); //nbr
frustumCorners[2] = invProj * new float4(-1, 1, -1, 1); //ntl
frustumCorners[1] = invProj * new float4(1, -1, -1, 1); //nbr
frustumCorners[2] = invProj * new float4(-1, 1, -1, 1); //ntl
frustumCorners[3] = invProj * new float4(1, 1, -1, 1); //ntr

for (var i = 0; i < frustumCorners.Length; i++)
Expand Down Expand Up @@ -499,7 +500,7 @@ public void RenderXFormText(XFormText xfc)
/// <summary>
/// If a TransformComponent is visited the model matrix of the <see cref="RenderContext"/> and <see cref="RendererState"/> is updated.
/// It additionally updates the view matrix of the RenderContext.
/// </summary>
/// </summary>
/// <param name="transform">The TransformComponent.</param>
[VisitMethod]
public void RenderTransform(Transform transform)
Expand All @@ -521,6 +522,7 @@ public void PickMesh(Mesh mesh)
mesh.MeshType != PrimitiveType.TriangleStrip)) return;

var mvp = Projection * View * State.Model;

for (var i = 0; i < mesh.Triangles.Length; i += 3)
{
// a, b c: current triangle's vertices in clip coordinates
Expand All @@ -536,6 +538,7 @@ public void PickMesh(Mesh mesh)
// Point-in-Triangle-Test
if (float2.PointInTriangle(a.xy, b.xy, c.xy, PickPosClip, out var u, out var v))
{

var pickPos = float3.Barycentric(a.xyz, b.xyz, c.xyz, u, v);

if (pickPos.z >= -1 && pickPos.z <= 1)
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Core/SceneRayCaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public IEnumerable<RayCastResult> RayPick(RenderContext rc, float2 pickPos)

/// <summary>
/// If a TransformComponent is visited the model matrix of the <see cref="RenderContext"/> and <see cref="RayCasterState"/> is updated.
/// </summary>
/// </summary>
/// <param name="transform">The TransformComponent.</param>
[VisitMethod]
public void RenderTransform(Transform transform)
Expand Down
46 changes: 36 additions & 10 deletions src/Engine/Core/ShaderShards/Fragment/FragShards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,33 @@
namespace Fusee.Engine.Core.ShaderShards.Fragment
{
/// <summary>
/// Contains pre-defined Shader Shards =
/// content of the Fragment Shader's method that lets us change values of the "out"-struct that is used for the lighting calculation.
/// Contains pre-defined Shader Shards =
/// content of the Fragment Shader's method that lets us change values of the "out"-struct that is used for the lighting calculation.
/// </summary>
public static class FragShards
{

private static List<string> ColorModeSwitch() => new()
{
"vec4 resColor;",
$"switch({UniformNameDeclarations.ColorMode})",
@"{
case 0:
resColor = IN.Albedo;
break;
case 1:
resColor = vColor;
break;
case 2:
resColor = vColor1;
break;
case 3:
resColor = vColor2;
break;
}"
};


/// <summary>
/// Returns a default method body for a given lighting calculation.
/// <param name="surfInput">The surface input class. Needed to receive the shading model and texture setup.</param>
Expand All @@ -18,6 +40,8 @@ internal static List<string> SurfOutBody(SurfaceInput surfInput)
{
var res = new List<string>();

res.AddRange(ColorModeSwitch());

switch (surfInput.ShadingModel)
{
case ShadingModel.Edl:
Expand Down Expand Up @@ -56,13 +80,13 @@ internal static List<string> SurfOutBody(SurfaceInput surfInput)
res.Add($"vec4 texCol = texture(IN.AlbedoTex, {VaryingNameDeclarations.TextureCoordinates} * IN.TexTiles);");
res.Add($"texCol = vec4(DecodeSRGB(texCol.rgb), texCol.a);");
res.Add("float linearLuminance = (0.2126 * texCol.r) + (0.7152 * texCol.g) + (0.0722 * texCol.b);");
res.Add($"vec3 mix = mix(IN.Albedo.rgb * linearLuminance, texCol.xyz, IN.AlbedoMix);");
res.Add($"vec3 mix = mix(resColor.rgb * linearLuminance, texCol.xyz, IN.AlbedoMix);");
res.Add($"OUT.albedo = vec4(mix, texCol.a);");
}
else
{
if (surfInput.ShadingModel != ShadingModel.Edl)
res.Add("OUT.albedo = IN.Albedo;");
res.Add("OUT.albedo = resColor;");
else
{
res.Add("if(ColorMode == 0)");
Expand All @@ -71,7 +95,7 @@ internal static List<string> SurfOutBody(SurfaceInput surfInput)
res.Add("}");
res.Add("else");
res.Add("{");
res.Add(" OUT.albedo = IN.Albedo;");
res.Add(" OUT.albedo = resColor;");
res.Add("}");
}
}
Expand All @@ -89,7 +113,7 @@ internal static List<string> SurfOutBody(SurfaceInput surfInput)
if (surfInput.ShadingModel != ShadingModel.BRDF) return res;

if (surfInput.TextureSetup.HasFlag(TextureSetup.ThicknessMap))
res.Add($"OUT.{SurfaceOut.Thickness.Item2} = texture(IN.ThicknessMap, { VaryingNameDeclarations.TextureCoordinates}).r;");
res.Add($"OUT.{SurfaceOut.Thickness.Item2} = texture(IN.ThicknessMap, {VaryingNameDeclarations.TextureCoordinates}).r;");
else
res.Add($"OUT.{SurfaceOut.Thickness.Item2} = 1.0;");
return res;
Expand All @@ -102,6 +126,8 @@ internal static List<string> SurfOutBody(ShadingModel shadingModel, TextureSetup
{
var res = new List<string>();

res.AddRange(ColorModeSwitch());

switch (shadingModel)
{
case ShadingModel.Unlit:
Expand Down Expand Up @@ -136,13 +162,13 @@ internal static List<string> SurfOutBody(ShadingModel shadingModel, TextureSetup
res.Add($"vec4 texCol = texture(IN.AlbedoTex, {VaryingNameDeclarations.TextureCoordinates} * IN.TexTiles);");
res.Add($"texCol = vec4(DecodeSRGB(texCol.rgb), texCol.a);");
res.Add("float linearLuminance = (0.2126 * texCol.r) + (0.7152 * texCol.g) + (0.0722 * texCol.b);");
res.Add($"vec3 mix = mix(IN.Albedo.rgb * linearLuminance, texCol.xyz, IN.AlbedoMix);");
res.Add($"vec3 mix = mix(resColor.rgb * linearLuminance, texCol.xyz, IN.AlbedoMix);");
res.Add($"OUT.albedo = vec4(mix, texCol.a);");
}
else
{
if (shadingModel != ShadingModel.Edl)
res.Add("OUT.albedo = IN.Albedo;");
res.Add("OUT.albedo = resColor;");
else
{
res.Add("if(ColorMode == 0)");
Expand All @@ -159,7 +185,7 @@ internal static List<string> SurfOutBody(ShadingModel shadingModel, TextureSetup
res.Add("}");
res.Add("else if(ColorMode == 3)");
res.Add("{");
res.Add(" OUT.albedo = IN.Albedo;");
res.Add(" OUT.albedo = resColor;");
res.Add("}");
}
}
Expand All @@ -178,7 +204,7 @@ internal static List<string> SurfOutBody(ShadingModel shadingModel, TextureSetup
if (shadingModel != ShadingModel.BRDF) return res;

if (texSetup.HasFlag(TextureSetup.ThicknessMap))
res.Add($"OUT.{SurfaceOut.Thickness.Item2} = texture(IN.ThicknessMap, { VaryingNameDeclarations.TextureCoordinates}).r;");
res.Add($"OUT.{SurfaceOut.Thickness.Item2} = texture(IN.ThicknessMap, {VaryingNameDeclarations.TextureCoordinates}).r;");
else
res.Add($"OUT.{SurfaceOut.Thickness.Item2} = 1.0;");
return res;
Expand Down
23 changes: 23 additions & 0 deletions src/Engine/Core/ShaderShards/SurfaceOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@ public enum TextureSetup

}

/// <summary>
/// Used to specify which color is used for lighting calculation.
/// /// </summary>
public enum ColorMode
{
/// <summary>
/// Use basic Albedo color (from uniform)
/// </summary>
BaseColor = 0,
/// <summary>
/// Use <see cref="VaryingNameDeclarations.Color"/>
/// </summary>
VertexColor0,
/// <summary>
/// Use <see cref="VaryingNameDeclarations.Color1"/>
/// </summary>
VertexColor1,
/// <summary>
/// Use <see cref="VaryingNameDeclarations.Color2"/>
/// </summary>
VertexColor2
}

/// <summary>
/// Used to create the correct Surface Effect for a given lighting calculation.
/// </summary>
Expand Down
10 changes: 9 additions & 1 deletion src/Engine/Core/ShaderShards/UniformNameDeclarations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ public static class UniformNameDeclarations

#endregion


/// <summary>
/// The var name for the uniform ClippingPlanes variable.
/// </summary>
Expand Down Expand Up @@ -480,6 +479,15 @@ public static class UniformNameDeclarations
/// </summary>
public static int BackgroundColorHash = BackgroundColor.GetHashCode();

/// <summary>
/// The var name for the uniform ColorMode.
/// </summary>
public const string ColorMode = "ColorMode";
/// <summary>
/// Hash code for the <see cref="ColorMode"/> parameter.
/// </summary>
public static int ColorModeHash = ColorMode.GetHashCode();

/// <summary>
/// The var name for the uniform ScreenParams (width and height of the window).
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Engine/Core/ShaderShards/Vertex/VertShards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Fusee.Engine.Core.ShaderShards.Vertex
{
/// <summary>
/// Contains pre-defined Shader Shards.
/// Those are the content of the Vertex Shader's method that lets us change values of the "out"-struct that is used for the position calculation.
/// Contains pre-defined Shader Shards.
/// Those are the content of the Vertex Shader's method that lets us change values of the "out"-struct that is used for the position calculation.
/// </summary>
public static class VertShards
{
Expand Down
2 changes: 1 addition & 1 deletion src/ImGui/Desktop/Fusee.ImGui.Desktop/ImGuiController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Fusee.Base.Core;
using Fusee.Base.Core;
using Fusee.Engine.Core;
using Fusee.Math.Core;
using ImGuiNET;
Expand Down
Loading

0 comments on commit dad84ee

Please sign in to comment.