Add Transparent Lighting Modes for material with option to use non-directional shading

This commit is contained in:
Wojciech Figat
2022-07-14 09:21:09 +02:00
parent 047821f7d2
commit 85f351663b
14 changed files with 254 additions and 52 deletions

View File

@@ -439,7 +439,17 @@ void Material::InitCompilationOptions(ShaderCompilationOptions& options)
options.Macros.Add({ "MATERIAL_REFLECTIONS", Numbers[1] });
options.Macros.Add({ "USE_FOG", Numbers[info.FeaturesFlags & MaterialFeaturesFlags::DisableFog ? 0 : 1] });
if (useForward)
{
options.Macros.Add({ "USE_PIXEL_NORMAL_OFFSET_REFRACTION", Numbers[info.FeaturesFlags & MaterialFeaturesFlags::PixelNormalOffsetRefraction ? 1 : 0] });
switch (info.TransparentLightingMode)
{
case MaterialTransparentLightingMode::Surface:
break;
case MaterialTransparentLightingMode::SurfaceNonDirectional:
options.Macros.Add({ "LIGHTING_NO_DIRECTIONAL", "1" });
break;
}
}
// TODO: don't compile VS_Depth for deferred/forward materials if material doesn't use position offset or masking

View File

@@ -23,13 +23,14 @@ public:
static const Upgrader upgraders[] =
{
{ 18, 19, &Upgrade_18_To_19 },
{ 19, 20, &Upgrade_19_To_20 },
};
setup(upgraders, ARRAY_COUNT(upgraders));
}
private:
// ============================================
// Version 18:
// Versions 18,19,20:
// Designed: 7/24/2019
// Custom Data: Header
// Chunk 0: Material Params
@@ -38,20 +39,46 @@ private:
// Chunk 14: Visject Surface data
// Chunk 15: Source code: ANSI text (encrypted)
// ============================================
// Version 18:
// Designed: 9/13/2018
// Custom Data: Header
// Chunk 0: Material Params
// Chunk 1: Internal SM5 cache
// Chunk 2: Internal SM4 cache
// Chunk 14: Visject Surface data
// Chunk 15: Source code: ANSI text (encrypted)
// ============================================
typedef ShaderStorage::Header Header;
typedef ShaderStorage::Header20 Header20;
typedef ShaderStorage::Header19 Header19;
typedef ShaderStorage::Header18 Header18;
static bool Upgrade_19_To_20(AssetMigrationContext& context)
{
ASSERT(context.Input.SerializedVersion == 19 && context.Output.SerializedVersion == 20);
// Convert header
if (context.Input.CustomData.IsInvalid())
return true;
auto& oldHeader = *(Header19*)context.Input.CustomData.Get();
Header20 newHeader;
Platform::MemoryClear(&newHeader, sizeof(newHeader));
if (context.Input.Header.TypeName == TEXT("FlaxEngine.ParticleEmitter"))
{
newHeader.ParticleEmitter.GraphVersion = oldHeader.ParticleEmitter.GraphVersion;
newHeader.ParticleEmitter.CustomDataSize = oldHeader.ParticleEmitter.CustomDataSize;
}
else if (context.Input.Header.TypeName == TEXT("FlaxEngine.Material"))
{
newHeader.Material.GraphVersion = oldHeader.Material.GraphVersion;
newHeader.Material.Info = oldHeader.Material.Info;
}
else if (context.Input.Header.TypeName == TEXT("FlaxEngine.Shader"))
{
}
else
{
LOG(Warning, "Unknown input asset type.");
return true;
}
context.Output.CustomData.Copy(&newHeader);
// Copy all chunks
return CopyChunks(context);
}
static bool Upgrade_18_To_19(AssetMigrationContext& context)
{
ASSERT(context.Input.SerializedVersion == 18 && context.Output.SerializedVersion == 19);

View File

@@ -107,10 +107,10 @@ CreateMaterial::Options::Options()
CreateAssetResult CreateMaterial::Create(CreateAssetContext& context)
{
// Base
IMPORT_SETUP(Material, 19);
IMPORT_SETUP(Material, 20);
context.SkipMetadata = true;
ShaderStorage::Header19 shaderHeader;
ShaderStorage::Header20 shaderHeader;
Platform::MemoryClear(&shaderHeader, sizeof(shaderHeader));
if (context.CustomArg)
{

View File

@@ -18,6 +18,7 @@ namespace FlaxEngine
UsageFlags = MaterialUsageFlags.None,
FeaturesFlags = MaterialFeaturesFlags.None,
DecalBlendingMode = MaterialDecalBlendingMode.Translucent,
TransparentLightingMode = MaterialTransparentLightingMode.SurfaceNonDirectional,
PostFxLocation = MaterialPostFxLocation.AfterPostProcessingPass,
MaskThreshold = 0.3f,
OpacityThreshold = 0.12f,
@@ -61,6 +62,7 @@ namespace FlaxEngine
&& UsageFlags == other.UsageFlags
&& FeaturesFlags == other.FeaturesFlags
&& DecalBlendingMode == other.DecalBlendingMode
&& TransparentLightingMode == other.TransparentLightingMode
&& PostFxLocation == other.PostFxLocation
&& Mathf.NearEqual(MaskThreshold, other.MaskThreshold)
&& Mathf.NearEqual(OpacityThreshold, other.OpacityThreshold)
@@ -86,6 +88,7 @@ namespace FlaxEngine
hashCode = (hashCode * 397) ^ (int)FeaturesFlags;
hashCode = (hashCode * 397) ^ (int)PostFxLocation;
hashCode = (hashCode * 397) ^ (int)DecalBlendingMode;
hashCode = (hashCode * 397) ^ (int)TransparentLightingMode;
hashCode = (hashCode * 397) ^ (int)(MaskThreshold * 1000.0f);
hashCode = (hashCode * 397) ^ (int)(OpacityThreshold * 1000.0f);
hashCode = (hashCode * 397) ^ (int)TessellationMode;

View File

@@ -355,6 +355,22 @@ API_ENUM() enum class MaterialDecalBlendingMode : byte
Emissive = 3,
};
/// <summary>
/// Transparent material lighting modes.
/// </summary>
API_ENUM() enum class MaterialTransparentLightingMode : byte
{
/// <summary>
/// Default directional lighting evaluated per-pixel at the material surface. Use it for semi-transparent surfaces - with both diffuse and specular lighting component active.
/// </summary>
Surface = 0,
/// <summary>
/// Non-directional lighting evaluated per-pixel at material surface. Use it for volumetric objects such as smoke, rain or dust - only diffuse lighting term is active (no specular highlights).
/// </summary>
SurfaceNonDirectional = 1,
};
/// <summary>
/// Material input scene textures. Special inputs from the graphics pipeline.
/// </summary>
@@ -441,6 +457,33 @@ struct MaterialInfo8
bool operator==(const MaterialInfo8& other) const;
};
/// <summary>
/// Material info structure - version 9
/// [Deprecated on 13.07.2022, expires on 13.07.2024]
/// </summary>
struct MaterialInfo9
{
MaterialDomain Domain;
MaterialBlendMode BlendMode;
MaterialShadingModel ShadingModel;
MaterialUsageFlags UsageFlags;
MaterialFeaturesFlags FeaturesFlags;
MaterialDecalBlendingMode DecalBlendingMode;
MaterialPostFxLocation PostFxLocation;
CullMode CullMode;
float MaskThreshold;
float OpacityThreshold;
TessellationMethod TessellationMode;
int32 MaxTessellationFactor;
MaterialInfo9()
{
}
MaterialInfo9(const MaterialInfo8& other);
bool operator==(const MaterialInfo9& other) const;
};
/// <summary>
/// Structure with basic information about the material surface. It describes how material is reacting on light and which graphical features of it requires to render.
/// </summary>
@@ -478,6 +521,11 @@ API_STRUCT() struct FLAXENGINE_API MaterialInfo
/// </summary>
API_FIELD() MaterialDecalBlendingMode DecalBlendingMode;
/// <summary>
/// The transparent material lighting mode.
/// </summary>
API_FIELD() MaterialTransparentLightingMode TransparentLightingMode;
/// <summary>
/// The post fx material rendering location.
/// </summary>
@@ -512,10 +560,10 @@ API_STRUCT() struct FLAXENGINE_API MaterialInfo
{
}
MaterialInfo(const MaterialInfo8& other);
MaterialInfo(const MaterialInfo9& other);
bool operator==(const MaterialInfo& other) const;
};
// The current material info descriptor version used by the material pipeline
typedef MaterialInfo MaterialInfo9;
#define MaterialInfo_Version 9
typedef MaterialInfo MaterialInfo10;
#define MaterialInfo_Version 10

View File

@@ -30,7 +30,7 @@ bool MaterialInfo8::operator==(const MaterialInfo8& other) const
&& MaxTessellationFactor == other.MaxTessellationFactor;
}
MaterialInfo::MaterialInfo(const MaterialInfo8& other)
MaterialInfo9::MaterialInfo9(const MaterialInfo8& other)
{
Domain = other.Domain;
BlendMode = other.BlendMode;
@@ -78,6 +78,39 @@ MaterialInfo::MaterialInfo(const MaterialInfo8& other)
MaxTessellationFactor = other.MaxTessellationFactor;
}
bool MaterialInfo9::operator==(const MaterialInfo9& other) const
{
return Domain == other.Domain
&& BlendMode == other.BlendMode
&& ShadingModel == other.ShadingModel
&& UsageFlags == other.UsageFlags
&& FeaturesFlags == other.FeaturesFlags
&& DecalBlendingMode == other.DecalBlendingMode
&& PostFxLocation == other.PostFxLocation
&& CullMode == other.CullMode
&& Math::NearEqual(MaskThreshold, other.MaskThreshold)
&& Math::NearEqual(OpacityThreshold, other.OpacityThreshold)
&& TessellationMode == other.TessellationMode
&& MaxTessellationFactor == other.MaxTessellationFactor;
}
MaterialInfo::MaterialInfo(const MaterialInfo9& other)
{
Domain = other.Domain;
BlendMode = other.BlendMode;
ShadingModel = other.ShadingModel;
UsageFlags = other.UsageFlags;
FeaturesFlags = other.FeaturesFlags;
DecalBlendingMode = other.DecalBlendingMode;
TransparentLightingMode = MaterialTransparentLightingMode::Surface;
PostFxLocation = other.PostFxLocation;
CullMode = other.CullMode;
MaskThreshold = other.MaskThreshold;
OpacityThreshold = other.OpacityThreshold;
TessellationMode = other.TessellationMode;
MaxTessellationFactor = other.MaxTessellationFactor;
}
bool MaterialInfo::operator==(const MaterialInfo& other) const
{
return Domain == other.Domain
@@ -86,6 +119,7 @@ bool MaterialInfo::operator==(const MaterialInfo& other) const
&& UsageFlags == other.UsageFlags
&& FeaturesFlags == other.FeaturesFlags
&& DecalBlendingMode == other.DecalBlendingMode
&& TransparentLightingMode == other.TransparentLightingMode
&& PostFxLocation == other.PostFxLocation
&& CullMode == other.CullMode
&& Math::NearEqual(MaskThreshold, other.MaskThreshold)

View File

@@ -76,7 +76,7 @@ void ForwardShadingFeature::Bind(MaterialShader::BindParameters& params, Span<by
// Set reflection probe data
EnvironmentProbe* probe = nullptr;
// TODO: optimize env probe searching for a transparent material - use spatial cache for renderer to find it
const Vector3 drawCallOrigin = drawCall.World.GetTranslation() + view.Origin;
const Vector3 drawCallOrigin = drawCall.ObjectPosition + view.Origin;
for (int32 i = 0; i < cache->EnvironmentProbes.Count(); i++)
{
const auto p = cache->EnvironmentProbes[i];
@@ -103,7 +103,7 @@ void ForwardShadingFeature::Bind(MaterialShader::BindParameters& params, Span<by
for (int32 i = 0; i < cache->PointLights.Count() && data.LocalLightsCount < MaxLocalLights; i++)
{
const auto& light = cache->PointLights[i];
if (BoundingSphere(light.Position, light.Radius).Contains(drawCall.World.GetTranslation()) != ContainmentType::Disjoint)
if (BoundingSphere(light.Position, light.Radius).Contains(drawCall.ObjectPosition) != ContainmentType::Disjoint)
{
light.SetupLightData(&data.LocalLights[data.LocalLightsCount], false);
data.LocalLightsCount++;
@@ -112,7 +112,7 @@ void ForwardShadingFeature::Bind(MaterialShader::BindParameters& params, Span<by
for (int32 i = 0; i < cache->SpotLights.Count() && data.LocalLightsCount < MaxLocalLights; i++)
{
const auto& light = cache->SpotLights[i];
if (BoundingSphere(light.Position, light.Radius).Contains(drawCall.World.GetTranslation()) != ContainmentType::Disjoint)
if (BoundingSphere(light.Position, light.Radius).Contains(drawCall.ObjectPosition) != ContainmentType::Disjoint)
{
light.SetupLightData(&data.LocalLights[data.LocalLightsCount], false);
data.LocalLightsCount++;

View File

@@ -69,9 +69,9 @@ public:
/// </summary>
MaterialInfo8 MaterialInfo;
};
/// <summary>
/// File header, version 19
/// [Deprecated on 13.07.2022, expires on 13.07.2024]
/// </summary>
struct Header19
{
@@ -115,8 +115,53 @@ public:
}
};
/// <summary>
/// File header, version 20
/// </summary>
struct Header20
{
static const int32 Version = 20;
union
{
struct
{
} Shader;
struct
{
/// <summary>
/// The material graph version.
/// </summary>
int32 GraphVersion;
/// <summary>
/// The material additional information.
/// </summary>
MaterialInfo10 Info;
} Material;
struct
{
/// <summary>
/// The particle emitter graph version.
/// </summary>
int32 GraphVersion;
/// <summary>
/// The custom particles data size (in bytes).
/// </summary>
int32 CustomDataSize;
} ParticleEmitter;
};
Header20()
{
}
};
/// <summary>
/// Current header type
/// </summary>
typedef Header19 Header;
typedef Header20 Header;
};