Use single color materialinstead of shader for LOD Preview
This commit is contained in:
BIN
Content/Shaders/Editor/LODPreview.flax
(Stored with Git LFS)
BIN
Content/Shaders/Editor/LODPreview.flax
(Stored with Git LFS)
Binary file not shown.
@@ -3,83 +3,46 @@
|
|||||||
#if USE_EDITOR
|
#if USE_EDITOR
|
||||||
|
|
||||||
#include "LODPreview.h"
|
#include "LODPreview.h"
|
||||||
|
#include "Engine/Core/Types/Variant.h"
|
||||||
#include "Engine/Content/Content.h"
|
#include "Engine/Content/Content.h"
|
||||||
#include "Engine/Content/Assets/Model.h"
|
#include "Engine/Content/Assets/Model.h"
|
||||||
#include "Engine/Graphics/GPUDevice.h"
|
#include "Engine/Graphics/GPUDevice.h"
|
||||||
#include "Engine/Graphics/GPUPipelineState.h"
|
|
||||||
#include "Engine/Graphics/Shaders/GPUShader.h"
|
|
||||||
#include "Engine/Graphics/Shaders/GPUConstantBuffer.h"
|
|
||||||
#include "Engine/Graphics/RenderTask.h"
|
#include "Engine/Graphics/RenderTask.h"
|
||||||
#include "Engine/Renderer/DrawCall.h"
|
#include "Engine/Renderer/DrawCall.h"
|
||||||
#include "Engine/Renderer/RenderList.h"
|
#include "Engine/Renderer/RenderList.h"
|
||||||
|
|
||||||
PACK_STRUCT(struct SingleColorShaderData {
|
|
||||||
Matrix ViewProjectionMatrix;
|
|
||||||
Matrix WorldMatrix;
|
|
||||||
Color Color;
|
|
||||||
Vector3 Dummy0;
|
|
||||||
float LODDitherFactor;
|
|
||||||
});
|
|
||||||
|
|
||||||
LODPreviewMaterialShader::LODPreviewMaterialShader()
|
LODPreviewMaterialShader::LODPreviewMaterialShader()
|
||||||
{
|
{
|
||||||
_psModel.CreatePipelineStates();
|
_material = Content::LoadAsyncInternal<Material>(TEXT("Editor/DebugMaterials/Single Color Surface"));
|
||||||
_shader = Content::LoadAsyncInternal<Shader>(TEXT("Shaders/Editor/SingleColor"));
|
|
||||||
if (!_shader)
|
|
||||||
return;
|
|
||||||
#if COMPILE_WITH_DEV_ENV
|
|
||||||
_shader.Get()->OnReloading.Bind<LODPreviewMaterialShader, &LODPreviewMaterialShader::OnShaderReloading>(this);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if COMPILE_WITH_DEV_ENV
|
|
||||||
|
|
||||||
void LODPreviewMaterialShader::OnShaderReloading(Asset* obj)
|
|
||||||
{
|
|
||||||
_psModel.Release();
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
const MaterialInfo& LODPreviewMaterialShader::GetInfo() const
|
const MaterialInfo& LODPreviewMaterialShader::GetInfo() const
|
||||||
{
|
{
|
||||||
return _info;
|
if (_material)
|
||||||
|
return _material->GetInfo();
|
||||||
|
return MaterialInfo();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LODPreviewMaterialShader::IsReady() const
|
bool LODPreviewMaterialShader::IsReady() const
|
||||||
{
|
{
|
||||||
return _shader && _shader->IsLoaded();
|
return _material && _material->IsReady();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LODPreviewMaterialShader::CanUseInstancing(InstancingHandler& handler) const
|
bool LODPreviewMaterialShader::CanUseInstancing(InstancingHandler& handler) const
|
||||||
{
|
{
|
||||||
handler = { SurfaceDrawCallHandler::GetHash, SurfaceDrawCallHandler::CanBatch, SurfaceDrawCallHandler::WriteDrawCall, };
|
return _material->CanUseInstancing(handler);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DrawPass LODPreviewMaterialShader::GetDrawModes() const
|
DrawPass LODPreviewMaterialShader::GetDrawModes() const
|
||||||
{
|
{
|
||||||
return DrawPass::GBuffer;
|
return _material->GetDrawModes();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LODPreviewMaterialShader::Bind(BindParameters& params)
|
void LODPreviewMaterialShader::Bind(BindParameters& params)
|
||||||
{
|
{
|
||||||
auto context = params.GPUContext;
|
|
||||||
auto& drawCall = *params.FirstDrawCall;
|
|
||||||
auto shader = _shader->GetShader();
|
|
||||||
auto cb = shader->GetCB(0);
|
|
||||||
const int32 psIndex = params.DrawCallsCount == 1 ? 0 : 1;
|
|
||||||
auto ps = _psModel[psIndex];
|
|
||||||
if (!ps->IsValid())
|
|
||||||
{
|
|
||||||
auto psDesc = GPUPipelineState::Description::Default;
|
|
||||||
psDesc.VS = shader->GetVS("VS_Model", psIndex);
|
|
||||||
psDesc.PS = shader->GetPS("PS_GBuffer");
|
|
||||||
ps->Init(psDesc);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find the LOD that produced this draw call
|
// Find the LOD that produced this draw call
|
||||||
int32 lodIndex = 0;
|
int32 lodIndex = 0;
|
||||||
|
auto& drawCall = *params.FirstDrawCall;
|
||||||
for (auto& e : Content::GetAssetsRaw())
|
for (auto& e : Content::GetAssetsRaw())
|
||||||
{
|
{
|
||||||
auto model = ScriptingObject::Cast<Model>(e.Value);
|
auto model = ScriptingObject::Cast<Model>(e.Value);
|
||||||
@@ -103,27 +66,17 @@ void LODPreviewMaterialShader::Bind(BindParameters& params)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Bind
|
// Bind
|
||||||
if (cb && cb->GetSize())
|
const Color colors[MODEL_MAX_LODS] = {
|
||||||
{
|
Color::White,
|
||||||
ASSERT_LOW_LAYER(cb->GetSize() == sizeof(SingleColorShaderData));
|
Color::Red,
|
||||||
SingleColorShaderData data;
|
Color::Orange,
|
||||||
Matrix::Transpose(params.RenderContext.View.Frustum.GetMatrix(), data.ViewProjectionMatrix);
|
Color::Yellow,
|
||||||
Matrix::Transpose(drawCall.World, data.WorldMatrix);
|
Color::Green,
|
||||||
data.LODDitherFactor = drawCall.Surface.LODDitherFactor;
|
Color::Blue,
|
||||||
const Color colors[MODEL_MAX_LODS] = {
|
};
|
||||||
Color::White,
|
const Color color(colors[lodIndex]);
|
||||||
Color::Red,
|
_material->SetParameterValue(TEXT("Color"), Variant(color));
|
||||||
Color::Orange,
|
_material->Bind(params);
|
||||||
Color::Yellow,
|
|
||||||
Color::Green,
|
|
||||||
Color::Blue,
|
|
||||||
};
|
|
||||||
ASSERT(lodIndex < MODEL_MAX_LODS);
|
|
||||||
data.Color = colors[lodIndex];
|
|
||||||
context->UpdateCB(cb, &data);
|
|
||||||
context->BindCB(0, cb);
|
|
||||||
}
|
|
||||||
context->SetState(ps);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,9 +5,8 @@
|
|||||||
#if USE_EDITOR
|
#if USE_EDITOR
|
||||||
|
|
||||||
#include "Engine/Content/AssetReference.h"
|
#include "Engine/Content/AssetReference.h"
|
||||||
#include "Engine/Content/Assets/Shader.h"
|
#include "Engine/Content/Assets/Material.h"
|
||||||
#include "Engine/Graphics/Materials/IMaterial.h"
|
#include "Engine/Graphics/Materials/IMaterial.h"
|
||||||
#include "Engine/Graphics/GPUPipelineStatePermutations.h"
|
|
||||||
|
|
||||||
class GPUPipelineState;
|
class GPUPipelineState;
|
||||||
|
|
||||||
@@ -18,9 +17,7 @@ class LODPreviewMaterialShader : public IMaterial
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
|
||||||
AssetReference<Shader> _shader;
|
AssetReference<Material> _material;
|
||||||
GPUPipelineStatePermutations<2> _psModel;
|
|
||||||
MaterialInfo _info;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@@ -29,12 +26,6 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
#if COMPILE_WITH_DEV_ENV
|
|
||||||
void OnShaderReloading(Asset* obj);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// [IMaterial]
|
// [IMaterial]
|
||||||
|
|||||||
@@ -1,89 +0,0 @@
|
|||||||
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
|
||||||
|
|
||||||
#define MATERIAL 1
|
|
||||||
|
|
||||||
#include "./Flax/Common.hlsl"
|
|
||||||
#include "./Flax/MaterialCommon.hlsl"
|
|
||||||
|
|
||||||
META_CB_BEGIN(0, Data)
|
|
||||||
float4x4 ViewProjectionMatrix;
|
|
||||||
float4x4 WorldMatrix;
|
|
||||||
float4 Color;
|
|
||||||
float3 WorldInvScale;
|
|
||||||
float LODDitherFactor;
|
|
||||||
META_CB_END
|
|
||||||
|
|
||||||
struct VertexOutput
|
|
||||||
{
|
|
||||||
float4 Position : SV_Position;
|
|
||||||
float3 WorldNormal : TEXCOORD0;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct PixelInput
|
|
||||||
{
|
|
||||||
float4 Position : SV_Position;
|
|
||||||
float3 WorldNormal : TEXCOORD0;
|
|
||||||
};
|
|
||||||
|
|
||||||
float3x3 RemoveScaleFromLocalToWorld(float3x3 localToWorld)
|
|
||||||
{
|
|
||||||
localToWorld[0] *= WorldInvScale.x;
|
|
||||||
localToWorld[1] *= WorldInvScale.y;
|
|
||||||
localToWorld[2] *= WorldInvScale.z;
|
|
||||||
return localToWorld;
|
|
||||||
}
|
|
||||||
|
|
||||||
float3x3 CalcTangentToWorld(float4x4 world, float3x3 tangentToLocal)
|
|
||||||
{
|
|
||||||
float3x3 localToWorld = RemoveScaleFromLocalToWorld((float3x3)world);
|
|
||||||
return mul(tangentToLocal, localToWorld);
|
|
||||||
}
|
|
||||||
|
|
||||||
float4 PerformFakeLighting(float3 n, float4 c)
|
|
||||||
{
|
|
||||||
c.rgb *= saturate(abs(dot(n, float3(0, 1, 0))) + 0.5f);
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClipLODTransition(float4 svPosition, float ditherFactor)
|
|
||||||
{
|
|
||||||
if (abs(ditherFactor) > 0.001)
|
|
||||||
{
|
|
||||||
float randGrid = cos(dot(floor(svPosition.xy), float2(347.83452793, 3343.28371863)));
|
|
||||||
float randGridFrac = frac(randGrid * 1000.0);
|
|
||||||
half mask = (ditherFactor < 0.0) ? (ditherFactor + 1.0 > randGridFrac) : (ditherFactor < randGridFrac);
|
|
||||||
clip(mask - 0.001);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
META_VS(true, FEATURE_LEVEL_ES2)
|
|
||||||
META_VS_IN_ELEMENT(POSITION, 0, R32G32B32_FLOAT, 0, 0, PER_VERTEX, 0, true)
|
|
||||||
META_VS_IN_ELEMENT(TEXCOORD, 0, R16G16_FLOAT, 1, 0, PER_VERTEX, 0, true)
|
|
||||||
META_VS_IN_ELEMENT(NORMAL, 0, R10G10B10A2_UNORM, 1, ALIGN, PER_VERTEX, 0, true)
|
|
||||||
META_VS_IN_ELEMENT(TANGENT, 0, R10G10B10A2_UNORM, 1, ALIGN, PER_VERTEX, 0, true)
|
|
||||||
META_VS_IN_ELEMENT(TEXCOORD, 1, R16G16_FLOAT, 1, ALIGN, PER_VERTEX, 0, true)
|
|
||||||
VertexOutput VS(ModelInput input)
|
|
||||||
{
|
|
||||||
float bitangentSign = input.Tangent.w ? -1.0f : +1.0f;
|
|
||||||
float3 normal = input.Normal.xyz * 2.0 - 1.0;
|
|
||||||
float3 tangent = input.Tangent.xyz * 2.0 - 1.0;
|
|
||||||
float3 bitangent = cross(normal, tangent) * bitangentSign;
|
|
||||||
float3x3 tangentToLocal = float3x3(tangent, bitangent, normal);
|
|
||||||
float3x3 tangentToWorld = CalcTangentToWorld(WorldMatrix, tangentToLocal);
|
|
||||||
float3 worldPosition = mul(float4(input.Position.xyz, 1), WorldMatrix).xyz;
|
|
||||||
|
|
||||||
VertexOutput output;
|
|
||||||
output.Position = mul(float4(worldPosition, 1), ViewProjectionMatrix);
|
|
||||||
output.WorldNormal = tangentToWorld[2];
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
META_PS(true, FEATURE_LEVEL_ES2)
|
|
||||||
void PS(in PixelInput input, out float4 Light : SV_Target0, out float4 RT0 : SV_Target1, out float4 RT1 : SV_Target2, out float4 RT2 : SV_Target3)
|
|
||||||
{
|
|
||||||
ClipLODTransition(input.Position, LODDitherFactor);
|
|
||||||
Light = PerformFakeLighting(input.WorldNormal, Color);
|
|
||||||
RT0 = float4(0, 0, 0, 0);
|
|
||||||
RT1 = float4(input.WorldNormal * 0.5 + 0.5, SHADING_MODEL_LIT * (1.0 / 3.0));
|
|
||||||
RT2 = float4(0.4, 0, 0.5, 0);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user