Support emissive materials

This commit is contained in:
Simon Lübeß
2023-06-28 19:33:39 +02:00
parent 334c2c0366
commit b31d43820a
9 changed files with 194 additions and 79 deletions
@@ -0,0 +1,59 @@
#pragma EngineBuffer[ Name = "SceneConstants"; Binding = "Scene" ]
cbuffer SceneConstants : register(b0)
{
float4x4 ViewProjection;
}
#pragma EngineBuffer[ Name = "ObjectConstants"; Binding = "Object" ]
cbuffer ObjectConstants : register(b1)
{
float4x4 Transform;
/**
* \brief Inverted and transposed transform matrix.
* \remarks This matrix is used in order to correctly transform normal vectors.
*/
float4x3 Transform_InvT;
#ifdef OutputEntityId
uint EntityId;
#endif
}
/**
* \brief Pixel Shader output for lit surfaces.
*/
struct PS_OUT_Lit
{
/** RGB: Albedo A: Transparency */
float4 Albedo : SV_TARGET0;
/** RG: TextureNormal.XY | BA: GeoNrm.XY */
float4 Normal : SV_TARGET1;
/** R: GeoNrm.Z | GBA: GeoTan.XYZ */
float4 Tangent : SV_TARGET2;
/** RGB: world position XYZ | A: 1.0 */
float4 Position : SV_TARGET3;
/** RGB: emissive light and color | A: unused */
float4 Emissive : SV_TARGET4;
/** R: Metallicity | G: Roughness | B: Ambient | A: Unused */
float4 Material : SV_TARGET5;
#ifdef OutputEntityId
/** Needed for editor picking. Simply pass through the EntityId. */
uint EntityId : SV_TARGET6;
#endif
};
/**
* \brief Pixel Shader output for unlit surfaces.
*/
struct PS_OUT_Unlit
{
/** RGB: Color A: Transparency */
float4 Color : SV_TARGET0;
/** RGB: emissive light and color | A: unused */
float4 Emissive : SV_TARGET1;
/** RGB: world position XYZ | A: 1.0 */
float4 Position : SV_TARGET2;
#ifdef OutputEntityId
uint EntityId : SV_TARGET5;
#endif
};
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}/* No reflection data for GlitchyEditor.Assets.EffectAssetLoaderConfig. Add [BonTarget] or force it */
}
+36 -43
View File
@@ -2,6 +2,8 @@
#include "ShaderHelpers.hlsl" #include "ShaderHelpers.hlsl"
#include "GlitchyEngine.hlsl"
Texture2D AlbedoTexture : register(t0); Texture2D AlbedoTexture : register(t0);
SamplerState AlbedoSampler : register(s0); SamplerState AlbedoSampler : register(s0);
@@ -14,28 +16,11 @@ SamplerState MetallicSampler : register(s2);
Texture2D<float> RoughnessTexture : register(t3); Texture2D<float> RoughnessTexture : register(t3);
SamplerState RoughnessSampler : register(s3); SamplerState RoughnessSampler : register(s3);
// Texture2D<float> AmbientTexture : register(t4); Texture2D<float3> EmissiveTexture : register(t4);
// SamplerState AmbientSampler : register(s4); SamplerState EmissiveSampler : register(s4);
#pragma EngineBuffer[ Name = "SceneConstants"; Binding = "Scene" ] // Texture2D<float> AmbientTexture : register(t5);
cbuffer SceneConstants : register(b0) // SamplerState AmbientSampler : register(s5);
{
float4x4 ViewProjection;
}
#pragma EngineBuffer[ Name = "ObjectConstants"; Binding = "Object" ]
cbuffer ObjectConstants : register(b1)
{
float4x4 Transform;
/**
* \brief Inverted and transposed transform matrix.
* \remarks This matrix is used in order to correctly transform normal vectors.
*/
float4x3 Transform_InvT;
#ifdef OutputEntityId
uint EntityId;
#endif
}
cbuffer MaterialConstants : register(b2) cbuffer MaterialConstants : register(b2)
{ {
@@ -44,9 +29,12 @@ cbuffer MaterialConstants : register(b2)
#pragma EditorVariable[ Name = "NormalScaling"; Preview = "Normal Scaling" ] #pragma EditorVariable[ Name = "NormalScaling"; Preview = "Normal Scaling" ]
float2 NormalScaling = float2(1.0, 1.0); float2 NormalScaling = float2(1.0, 1.0);
#pragma EditorVariable[ Name = "MetallicFactor"; Preview = "Metallic Factor"; Min = 0.0f; Max = 1.0f ] #pragma EditorVariable[ Name = "MetallicFactor"; Preview = "Metallic Factor"; Min = 0.0f; Max = 1.0f ]
float MetallicFactor = 1.0; float MetallicFactor = 0.0;
#pragma EditorVariable[ Name = "RoughnessFactor"; Preview = "Rougness Factor"; Min = 0.0f; Max = 1.0f ] #pragma EditorVariable[ Name = "RoughnessFactor"; Preview = "Rougness Factor"; Min = 0.0f; Max = 1.0f ]
float RoughnessFactor = 1.0; float RoughnessFactor = 0.6;
#pragma EditorVariable[ Name = "EmissiveColor"; Preview = "Emissive Factor"; Type="ColorHDR" ]
float3 EmissiveColor = float3(0.0, 0.0, 0.0);
// # pra gma Editor Variable[ Name = "AmbientFactor"; Preview = "Ambient Factor" ]
// float AmbientFactor = 1.0; // float AmbientFactor = 1.0;
} }
@@ -59,7 +47,7 @@ struct VS_IN
float2 TexCoord : TEXCOORD; float2 TexCoord : TEXCOORD;
}; };
struct PS_IN typedef struct PS_IN
{ {
float4 Position : SV_POSITION; float4 Position : SV_POSITION;
float3 WorldPosition : WORLDPOSITION; float3 WorldPosition : WORLDPOSITION;
@@ -70,11 +58,11 @@ struct PS_IN
#ifdef OutputEntityId #ifdef OutputEntityId
nointerpolation uint EntityId : ENTITYID; nointerpolation uint EntityId : ENTITYID;
#endif #endif
}; } VS_OUT;
PS_IN VS(VS_IN input) VS_OUT VS(VS_IN input)
{ {
PS_IN output; VS_OUT output;
float4 worldPosition = mul(Transform, float4(input.Position, 1)); float4 worldPosition = mul(Transform, float4(input.Position, 1));
@@ -92,22 +80,22 @@ PS_IN VS(VS_IN input)
return output; return output;
} }
struct PS_OUT //struct PS_OUT
{ //{
float4 Albedo : SV_TARGET0; // float4 Albedo : SV_TARGET0;
// RG: TextureNormal.XY BA: GeoNrm.XY // // RG: TextureNormal.XY BA: GeoNrm.XY
float4 Normal : SV_TARGET1; // float4 Normal : SV_TARGET1;
// R: GeoNrm.Z GBA: GeoTan.XYZ // // R: GeoNrm.Z GBA: GeoTan.XYZ
float4 Tangent : SV_TARGET2; // float4 Tangent : SV_TARGET2;
float4 Position : SV_TARGET3; // float4 Position : SV_TARGET3;
// R: Metallicity G: Roughness B: Ambient // // R: Metallicity G: Roughness B: Ambient
float4 Material : SV_TARGET4; // float4 Material : SV_TARGET4;
#ifdef OutputEntityId //#ifdef OutputEntityId
uint EntityId : SV_TARGET5; // uint EntityId : SV_TARGET5;
#endif //#endif
}; //};
PS_OUT PS(PS_IN input) PS_OUT_Lit PS(PS_IN input)
{ {
// Build tangent space // Build tangent space
float3 normal = normalize(input.Normal); float3 normal = normalize(input.Normal);
@@ -124,6 +112,9 @@ PS_OUT PS(PS_IN input)
float texMetallic = MetallicTexture.Sample(MetallicSampler, input.TexCoord); float texMetallic = MetallicTexture.Sample(MetallicSampler, input.TexCoord);
float texRoughness = RoughnessTexture.Sample(RoughnessSampler, input.TexCoord); float texRoughness = RoughnessTexture.Sample(RoughnessSampler, input.TexCoord);
float3 texEmissive = EmissiveTexture.Sample(EmissiveSampler, input.TexCoord);
//float3 objectNormal = mul(tangentTransform, texNormal); //float3 objectNormal = mul(tangentTransform, texNormal);
//float3 worldNormal = mul(objectNormal, (float3x3)Transform); //float3 worldNormal = mul(objectNormal, (float3x3)Transform);
@@ -131,6 +122,7 @@ PS_OUT PS(PS_IN input)
float3 finalNormal = ScaleNormal(texNormal, NormalScaling); float3 finalNormal = ScaleNormal(texNormal, NormalScaling);
float finalMetallic = texMetallic * MetallicFactor; float finalMetallic = texMetallic * MetallicFactor;
float finalRoughness = texRoughness * RoughnessFactor; float finalRoughness = texRoughness * RoughnessFactor;
float3 finalEmissive = texEmissive * EmissiveColor;
/////////////TODO: REMOVEME /////////////TODO: REMOVEME
@@ -141,12 +133,13 @@ PS_OUT PS(PS_IN input)
/////////////TODO: END_REMOVEME /////////////TODO: END_REMOVEME
PS_OUT output; PS_OUT_Lit output;
output.Albedo = finalAlbedo; output.Albedo = finalAlbedo;
//output.Normal = float4(objectNormal, 1.0); //output.Normal = float4(objectNormal, 1.0);
output.Normal = float4(finalNormal.xy, normal.xy); output.Normal = float4(finalNormal.xy, normal.xy);
output.Tangent = float4(normal.z, tangent.xyz); output.Tangent = float4(normal.z, tangent.xyz);
output.Position = float4(input.WorldPosition, 1.0); output.Position = float4(input.WorldPosition, 1.0);
output.Emissive = float4(finalEmissive, 0.0);
output.Material = float4(finalMetallic, finalRoughness, 1.0, 0); output.Material = float4(finalMetallic, finalRoughness, 1.0, 0);
#ifdef OutputEntityId #ifdef OutputEntityId
@@ -15,7 +15,8 @@ Texture2D GBuffer_Albedo : register(t0);
Texture2D GBuffer_Normal : register(t1); Texture2D GBuffer_Normal : register(t1);
Texture2D GBuffer_Tangent : register(t2); Texture2D GBuffer_Tangent : register(t2);
Texture2D GBuffer_Position : register(t3); Texture2D GBuffer_Position : register(t3);
Texture2D GBuffer_Material : register(t4); Texture2D GBuffer_Emissive : register(t4);
Texture2D GBuffer_Material : register(t5);
cbuffer Constants cbuffer Constants
{ {
@@ -58,7 +59,8 @@ float4 PS(PS_IN input) : SV_TARGET
float4 rawAlbedo = GBuffer_Albedo.Sample(Sampler, input.TexCoord); float4 rawAlbedo = GBuffer_Albedo.Sample(Sampler, input.TexCoord);
float4 rawNormal = GBuffer_Normal.Sample(Sampler, input.TexCoord); float4 rawNormal = GBuffer_Normal.Sample(Sampler, input.TexCoord);
float4 rawTangent = GBuffer_Tangent.Sample(Sampler, input.TexCoord); float4 rawTangent = GBuffer_Tangent.Sample(Sampler, input.TexCoord);
float4 rawPosition = GBuffer_Position.Sample(Sampler, input.TexCoord); float4 rawPosition = GBuffer_Position.Sample(Sampler, input.TexCoord);
float4 rawEmissive = GBuffer_Emissive.Sample(Sampler, input.TexCoord);
float4 rawMaterial = GBuffer_Material.Sample(Sampler, input.TexCoord); float4 rawMaterial = GBuffer_Material.Sample(Sampler, input.TexCoord);
// Extract data from GBuffer // Extract data from GBuffer
@@ -108,7 +110,7 @@ float4 PS(PS_IN input) : SV_TARGET
float3 luminanceColor = LightColor * Illuminance; float3 luminanceColor = LightColor * Illuminance;
float3 final = (kd * diffuse + specular) * luminanceColor * n_dot_l; float3 final = (kd * diffuse + specular) * luminanceColor * n_dot_l + rawEmissive;
#if OUTPUT == Inspect_NormalDistribution #if OUTPUT == Inspect_NormalDistribution
final = max(final - 10000000, nrmDist.xxx); final = max(final - 10000000, nrmDist.xxx);
@@ -4,7 +4,8 @@
"AlbedoTexture": "Textures/TestMat/rustediron2_albedo.png", "AlbedoTexture": "Textures/TestMat/rustediron2_albedo.png",
"NormalTexture": "Textures/TestMat/rustediron2_normal.png", "NormalTexture": "Textures/TestMat/rustediron2_normal.png",
"MetallicTexture": "Textures/TestMat/rustediron2_metallic.png", "MetallicTexture": "Textures/TestMat/rustediron2_metallic.png",
"RoughnessTexture": "Textures/TestMat/rustediron2_roughness.png" "RoughnessTexture": "Textures/TestMat/rustediron2_roughness.png",
"EmissiveTexture": "Textures/rocket.png"
], ],
Variables = [ Variables = [
"AlbedoColor": .ColorRGBA{ "AlbedoColor": .ColorRGBA{
@@ -26,6 +27,13 @@
}, },
"RoughnessFactor": .Float{ "RoughnessFactor": .Float{
Value = 1 Value = 1
},
"EmissiveColor": .ColorRGB{
Value = {
R = 1,
G = 0,
B = 0
}
} }
] ]
} }
+53 -18
View File
@@ -99,7 +99,8 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
{ {
for (let (name, arguments) in effect.[Friend]_variableDescriptions) for (let (name, arguments) in effect.[Friend]_variableDescriptions)
{ {
let variable = effect.Variables[name]; if (!effect.Variables.TryGetVariable(name, let variable))
continue;
bool hasPreviewName = TryGetValue(arguments, "Preview", var previewName); bool hasPreviewName = TryGetValue(arguments, "Preview", var previewName);
@@ -107,35 +108,69 @@ class MaterialAssetPropertiesEditor : AssetPropertiesEditor
bool hasPreviewType = TryGetValue(arguments, "Type", var previewType); bool hasPreviewType = TryGetValue(arguments, "Type", var previewType);
if (hasPreviewType && previewType.Get<String>() == "Color") if (hasPreviewType && (previewType.Get<String>() == "Color" || previewType.Get<String>() == "ColorHDR"))
{ {
Log.EngineLogger.AssertDebug(variable.Type == .Float && variable.Rows == 1); if (previewType.Get<String>().Equals("Color", .InvariantCultureIgnoreCase))
if (variable.Columns == 3)
{ {
material.GetVariable<float3>(variable.Name, var value); Log.EngineLogger.AssertDebug(variable.Type == .Float && variable.Rows == 1);
value = (float3)ColorRGB.LinearToSRGB((ColorRGB)value); if (variable.Columns == 3)
if (ImGui.ColorEdit3(displayName.Ptr, *(float[3]*)&value))
{ {
value = (float3)ColorRGB.SRgbToLinear((ColorRGB)value); material.GetVariable<float3>(variable.Name, var value);
material.SetVariable(variable.Name, value);
value = (float3)ColorRGB.LinearToSRGB((ColorRGB)value);
if (ImGui.ColorEdit3(displayName.Ptr, *(float[3]*)&value))
{
value = (float3)ColorRGB.SRgbToLinear((ColorRGB)value);
material.SetVariable(variable.Name, value);
}
}
else if (variable.Columns == 4)
{
material.GetVariable<float4>(variable.Name, var value);
value = (float4)ColorRGBA.LinearToSRGB((ColorRGBA)value);
if (ImGui.ColorEdit4(displayName.Ptr, *(float[4]*)&value))
{
value = (float4)ColorRGBA.SRgbToLinear((ColorRGBA)value);
material.SetVariable(variable.Name, value);
}
} }
} }
else if (variable.Columns == 4) else if (previewType.Get<String>().Equals("ColorHDR", .InvariantCultureIgnoreCase))
{ {
material.GetVariable<float4>(variable.Name, var value); Log.EngineLogger.AssertDebug(variable.Type == .Float && variable.Rows == 1);
value = (float4)ColorRGBA.LinearToSRGB((ColorRGBA)value); if (variable.Columns == 3)
if (ImGui.ColorEdit4(displayName.Ptr, *(float[4]*)&value))
{ {
value = (float4)ColorRGBA.SRgbToLinear((ColorRGBA)value); material.GetVariable<float3>(variable.Name, var value);
material.SetVariable(variable.Name, value);
value = (float3)ColorRGB.LinearToSRGB((ColorRGB)value);
if (ImGui.ColorEdit3(displayName.Ptr, *(float[3]*)&value, .HDR | .Float))
{
value = (float3)ColorRGB.SRgbToLinear((ColorRGB)value);
material.SetVariable(variable.Name, value);
}
}
else if (variable.Columns == 4)
{
material.GetVariable<float4>(variable.Name, var value);
value = (float4)ColorRGBA.LinearToSRGB((ColorRGBA)value);
if (ImGui.ColorEdit4(displayName.Ptr, *(float[4]*)&value, .HDR | .Float))
{
value = (float4)ColorRGBA.SRgbToLinear((ColorRGBA)value);
material.SetVariable(variable.Name, value);
}
} }
} }
} }
//ImGui.ColorEdit3("", null, .HDR | .Float)
else if (variable.Type == .Float && variable.Rows == 1) else if (variable.Type == .Float && variable.Rows == 1)
{ {
bool hasMin = TryGetValue(arguments, "Min", var min); bool hasMin = TryGetValue(arguments, "Min", var min);
+1 -1
View File
@@ -93,7 +93,7 @@ namespace GlitchyEditor
public this(EditorContentManager contentManager) : base("Editor") public this(EditorContentManager contentManager) : base("Editor")
{ {
Application.Get().Window.IsVSync = false; Application.Get().Window.IsVSync = true;
_contentManager = contentManager; _contentManager = contentManager;
@@ -45,6 +45,11 @@ namespace GlitchyEngine.Renderer
} }
} }
public bool TryGetVariable(String name, out BufferVariable variable)
{
return _nameToVariable.TryGetValue(name, out variable);
}
public BufferVariable this[String name] => _nameToVariable[name]; public BufferVariable this[String name] => _nameToVariable[name];
public List<BufferVariable>.Enumerator GetEnumerator() public List<BufferVariable>.Enumerator GetEnumerator()
+21 -12
View File
@@ -40,21 +40,29 @@ namespace GlitchyEngine.Renderer
if (_width == 0 || _height == 0) if (_width == 0 || _height == 0)
{ {
SamplerStateDescription desc = .(); SamplerStateDescription samplerDesc = .();
desc.MinFilter = .Point; samplerDesc.MinFilter = .Point;
desc.MagFilter = .Point; samplerDesc.MagFilter = .Point;
RenderTargetGroupDescription targetDesc = .(width, height, RenderTargetGroupDescription targetDesc = .(width, height,
TargetDescription[]( TargetDescription[](
.(RenderTargetFormat.R8G8B8A8_UNorm){SamplerDescription = desc}, // RGB: Albedo A: Transparency
.(RenderTargetFormat.R16G16B16A16_SNorm){SamplerDescription = desc}, .(RenderTargetFormat.R8G8B8A8_UNorm){SamplerDescription = samplerDesc},
.(RenderTargetFormat.R16G16B16A16_SNorm){SamplerDescription = desc}, // RG: TextureNormal.XY | BA: GeoNrm.XY
.(RenderTargetFormat.R32G32B32A32_Float){SamplerDescription = desc}, .(RenderTargetFormat.R16G16B16A16_SNorm){SamplerDescription = samplerDesc},
.(RenderTargetFormat.R8G8B8A8_UNorm){SamplerDescription = desc}, // R: GeoNrm.Z | GBA: GeoTan.XYZ
.(RenderTargetFormat.R32_UInt){SamplerDescription = desc, ClearColor = .UInt(uint32.MaxValue)}, .(RenderTargetFormat.R16G16B16A16_SNorm){SamplerDescription = samplerDesc},
// RGB: world position XYZ | A: 1.0
.(RenderTargetFormat.R32G32B32A32_Float){SamplerDescription = samplerDesc},
// RGB: emissive light and color | A: unused
.(RenderTargetFormat.R16G16B16A16_Float){SamplerDescription = samplerDesc},
// R: Metallicity | G: Roughness | B: Ambient | A: Unused
.(RenderTargetFormat.R8G8B8A8_UNorm){SamplerDescription = samplerDesc},
// EntityId : Needed for editor picking. Simply pass through the EntityId.
.(RenderTargetFormat.R32_UInt){SamplerDescription = samplerDesc, ClearColor = .UInt(uint32.MaxValue)},
), ),
TargetDescription(.D24_UNorm_S8_UInt){ TargetDescription(.D24_UNorm_S8_UInt){
SamplerDescription = desc, SamplerDescription = samplerDesc,
ClearColor = .DepthStencil(1.0f, 0) ClearColor = .DepthStencil(1.0f, 0)
}); });
Target = new RenderTargetGroup(targetDesc); Target = new RenderTargetGroup(targetDesc);
@@ -348,7 +356,8 @@ namespace GlitchyEngine.Renderer
fsEffect.SetTexture("GBuffer_Normal", _gBuffer.Target, 1); fsEffect.SetTexture("GBuffer_Normal", _gBuffer.Target, 1);
fsEffect.SetTexture("GBuffer_Tangent", _gBuffer.Target, 2); fsEffect.SetTexture("GBuffer_Tangent", _gBuffer.Target, 2);
fsEffect.SetTexture("GBuffer_Position", _gBuffer.Target, 3); fsEffect.SetTexture("GBuffer_Position", _gBuffer.Target, 3);
fsEffect.SetTexture("GBuffer_Material", _gBuffer.Target, 4); fsEffect.SetTexture("GBuffer_Emissive", _gBuffer.Target, 4);
fsEffect.SetTexture("GBuffer_Material", _gBuffer.Target, 5);
fsEffect.Variables["LightColor"].SetData(light.Light.Color); fsEffect.Variables["LightColor"].SetData(light.Light.Color);
fsEffect.Variables["Illuminance"].SetData(light.Light.Illuminance); fsEffect.Variables["Illuminance"].SetData(light.Light.Illuminance);
@@ -369,7 +378,7 @@ namespace GlitchyEngine.Renderer
_lights.Clear(); _lights.Clear();
// Copy EntityIDs to compositionTarget // Copy EntityIDs to compositionTarget
_gBuffer.Target.CopyTo(_sceneConstants.CompositionTarget, 1, Int2.Zero, Int2(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height), Int2.Zero, 5); _gBuffer.Target.CopyTo(_sceneConstants.CompositionTarget, 1, Int2.Zero, Int2(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height), Int2.Zero, 6);
RenderCommand.SetBlendState(_gBufferBlend); RenderCommand.SetBlendState(_gBufferBlend);