mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
UnbindTexture, PBR changes, Test scene
This commit is contained in:
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* This File contains Function for PBR.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __PBR_HLSL__
|
||||||
|
#define __PBR_HLSL__
|
||||||
|
|
||||||
|
#include "ShaderHelpers.hlsl"
|
||||||
|
|
||||||
|
// #define PBR_IBL
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normal Distribution Function. (Trowbridge-Reits GGX)
|
||||||
|
* Calculates the relative surface area of microfacets exactly aligned to the halfway vector.
|
||||||
|
* @param normal The surface normal.
|
||||||
|
* @param halfway The halfway vector between the surface normal and the view direction.
|
||||||
|
* @param roughness Roughness value.
|
||||||
|
* @returns The relative surface area of microfacets exactly aligned to the halfway vector.
|
||||||
|
*/
|
||||||
|
float NormalDistributionGGX(float3 normal, float3 halfway, float roughness)
|
||||||
|
{
|
||||||
|
// Square roughness because it looks better
|
||||||
|
float a = roughness * roughness;
|
||||||
|
float aa = a * a;
|
||||||
|
|
||||||
|
float n_dot_h = max(dot(normal, halfway), 0.0f);
|
||||||
|
|
||||||
|
float denom = (n_dot_h * n_dot_h) * (aa - 1.0f) + 1.0f;
|
||||||
|
denom = PI * denom * denom;
|
||||||
|
|
||||||
|
return aa / denom;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Geometry Function calculating the overshadowing of microfacets based on roughness. (Schlick-Beckmann GGX).
|
||||||
|
* @param dot-product of normal vector and vector from surface to camera.
|
||||||
|
* @param k Roughness value.
|
||||||
|
*/
|
||||||
|
float GeometrySchlickGGX(float n_dot_v, float k)
|
||||||
|
{
|
||||||
|
return n_dot_v / (n_dot_v * (1 - k) + k);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Geometry Function calculating the overshadowing of microfacets based on roughness. (Smith)
|
||||||
|
* @param normal The surface normal.
|
||||||
|
* @param viewDir Vector from surface to viewer.
|
||||||
|
* @param lightDir Vector from surface to light source.
|
||||||
|
* @param roughness Roughness value.
|
||||||
|
*/
|
||||||
|
float GeometrySmith(float3 normal, float3 viewDir, float3 lightDir, float roughness)
|
||||||
|
{
|
||||||
|
#ifdef PBR_IBL
|
||||||
|
// IBL
|
||||||
|
float k = roughness * roughness / 2;
|
||||||
|
#else
|
||||||
|
// Direct lighting
|
||||||
|
float k = (roughness + 1.0f);
|
||||||
|
k = (k * k) / 8;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
const float n_dot_v = max(dot(normal, viewDir), 0.0f);
|
||||||
|
float n_dot_l = max(dot(normal, lightDir), 0.0f);
|
||||||
|
|
||||||
|
return GeometrySchlickGGX(n_dot_v, k) * GeometrySchlickGGX(n_dot_l, k);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the fresnel value.
|
||||||
|
* @param n_dot_v Dot product of the normal and view direction
|
||||||
|
* @param F0 base reflectivity.
|
||||||
|
*/
|
||||||
|
float3 FresnelSchlick(float n_dot_v, float3 F0)
|
||||||
|
{
|
||||||
|
return F0 + (1.0 - F0) * pow(clamp(1.0 - n_dot_v, 0.0, 1.0), 5.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // __PBR_HLSL__
|
||||||
+6
@@ -1,3 +1,7 @@
|
|||||||
|
#ifndef __SHADER_HELPERS_HLSL__
|
||||||
|
#define __SHADER_HELPERS_HLSL__
|
||||||
|
|
||||||
|
#define PI 3.14159265358979323846f
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Calculates the weighted sum of two normal vectors.
|
* Calculates the weighted sum of two normal vectors.
|
||||||
@@ -82,3 +86,5 @@ float GetBitangentHandedness(float tangentz)
|
|||||||
uint z = asuint(tangentz);
|
uint z = asuint(tangentz);
|
||||||
return (z & 1) > 0 ? 1.0 : -1.0;
|
return (z & 1) > 0 ? 1.0 : -1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif // __SHADER_HELPERS_HLSL__
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#include "ShaderHelpers.hlsl"
|
||||||
|
|
||||||
Texture2D AlbedoTexture : register(t0);
|
Texture2D AlbedoTexture : register(t0);
|
||||||
SamplerState AlbedoSampler : register(s0);
|
SamplerState AlbedoSampler : register(s0);
|
||||||
|
|
||||||
@@ -25,8 +27,11 @@ cbuffer ObjectConstants
|
|||||||
|
|
||||||
cbuffer Constants
|
cbuffer Constants
|
||||||
{
|
{
|
||||||
//float4 BaseColor = float4(1, 0, 1, 1);
|
float4 AlbedoColor = float4(1.0, 1.0, 1.0, 1.0);
|
||||||
//float3 LightDir = float3(0, 1, 0);
|
float2 NormalScaling = float2(1.0, 1.0);
|
||||||
|
float MetallicFactor = 1.0;
|
||||||
|
float RoughnessFactor = 1.0;
|
||||||
|
// float AmbientFactor = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VS_IN
|
struct VS_IN
|
||||||
@@ -98,6 +103,10 @@ PS_OUT PS(PS_IN input)
|
|||||||
//float3 objectNormal = mul(tangentTransform, texNormal);
|
//float3 objectNormal = mul(tangentTransform, texNormal);
|
||||||
//float3 worldNormal = mul(objectNormal, (float3x3)Transform);
|
//float3 worldNormal = mul(objectNormal, (float3x3)Transform);
|
||||||
|
|
||||||
|
float4 finalAlbedo = texAlbedo * AlbedoColor;
|
||||||
|
float3 finalNormal = ScaleNormal(texNormal, NormalScaling);
|
||||||
|
float finalMetallic = texMetallic * MetallicFactor;
|
||||||
|
float finalRoughness = texRoughness * RoughnessFactor;
|
||||||
|
|
||||||
/////////////TODO: REMOVEME
|
/////////////TODO: REMOVEME
|
||||||
|
|
||||||
@@ -109,12 +118,12 @@ PS_OUT PS(PS_IN input)
|
|||||||
/////////////TODO: END_REMOVEME
|
/////////////TODO: END_REMOVEME
|
||||||
|
|
||||||
PS_OUT output;
|
PS_OUT output;
|
||||||
output.Albedo = texAlbedo;
|
output.Albedo = finalAlbedo;
|
||||||
//output.Normal = float4(objectNormal, 1.0);
|
//output.Normal = float4(objectNormal, 1.0);
|
||||||
output.Normal = float4(texNormal.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.Material = float4(texMetallic, texRoughness, 1.0, 0);
|
output.Material = float4(finalMetallic, finalRoughness, 1.0, 0);
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
#include "ShaderFunctions.hlsl"
|
#include "ShaderHelpers.hlsl"
|
||||||
|
#include "PBR.hlsl"
|
||||||
|
|
||||||
#define PI 3.14159265358979323846f
|
#define Render 0
|
||||||
|
#define Inspect_NormalDistribution 1
|
||||||
|
#define Inspect_GeometryFunction 2
|
||||||
|
#define Inspect_Fresnel 3
|
||||||
|
#define Inspect_Normal 4
|
||||||
|
|
||||||
|
#define OUTPUT Render
|
||||||
|
|
||||||
SamplerState Sampler : register(s0);
|
SamplerState Sampler : register(s0);
|
||||||
|
|
||||||
@@ -11,14 +18,16 @@ Texture2D GBuffer_Position : register(t3);
|
|||||||
Texture2D GBuffer_Material : register(t4);
|
Texture2D GBuffer_Material : register(t4);
|
||||||
|
|
||||||
cbuffer Constants
|
cbuffer Constants
|
||||||
|
{
|
||||||
|
float3 CameraPos;
|
||||||
|
float2 Scaling;
|
||||||
|
}
|
||||||
|
|
||||||
|
cbuffer LightConstants
|
||||||
{
|
{
|
||||||
float3 LightColor;
|
float3 LightColor;
|
||||||
float Illuminance;
|
float Illuminance;
|
||||||
float3 LightDir;
|
float3 LightDir;
|
||||||
|
|
||||||
float3 CameraPos;
|
|
||||||
|
|
||||||
float2 Scaling;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VS_IN
|
struct VS_IN
|
||||||
@@ -43,84 +52,10 @@ PS_IN VS(VS_IN input)
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Normal Distribution Function. (Trowbridge-Reits GGX)
|
|
||||||
* Calculates the relative surface area of microfacets exactly aligned to the halfway vector.
|
|
||||||
* @param normal The surface normal.
|
|
||||||
* @param halfway The halfway vector between the surface normal and the view direction.
|
|
||||||
* @param roughness Roughness value.
|
|
||||||
* @returns The relative surface area of microfacets exactly aligned to the halfway vector.
|
|
||||||
*/
|
|
||||||
float NormalDistributionGGX(float3 normal, float3 halfway, float roughness)
|
|
||||||
{
|
|
||||||
// Square roughness because it looks better
|
|
||||||
float a = roughness * roughness;
|
|
||||||
float aa = a * a;
|
|
||||||
|
|
||||||
float n_dot_h = max(dot(normal, halfway), 0.0f);
|
|
||||||
|
|
||||||
float denom = (n_dot_h * n_dot_h) * (aa - 1.0f) + 1.0f;
|
|
||||||
denom = PI * denom * denom;
|
|
||||||
|
|
||||||
return aa / denom;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Geometry Function calculating the overshadowing of microfacets based on roughness. (Schlick-Beckmann GGX).
|
|
||||||
* @param dot-product of normal vector and vector from surface to camera.
|
|
||||||
* @param k Roughness value.
|
|
||||||
*/
|
|
||||||
float GeometrySchlickGGX(float n_dot_v, float k)
|
|
||||||
{
|
|
||||||
return n_dot_v / (n_dot_v * (1 - k) + k);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Geometry Function calculating the overshadowing of microfacets based on roughness. (Smith)
|
|
||||||
* @param normal The surface normal.
|
|
||||||
* @param viewDir Vector from surface to viewer.
|
|
||||||
* @param lightDir Vector from surface to light source.
|
|
||||||
* @param roughness Roughness value.
|
|
||||||
*/
|
|
||||||
float GeometrySmith(float3 normal, float3 viewDir, float3 lightDir, float roughness)
|
|
||||||
{
|
|
||||||
// Direct lighting
|
|
||||||
float k = (roughness + 1.0f);
|
|
||||||
k = (k * k) / 8;
|
|
||||||
|
|
||||||
// IBL
|
|
||||||
// float k = alpha * alpha / 2
|
|
||||||
|
|
||||||
float n_dot_v = max(dot(normal, viewDir), 0.0f);
|
|
||||||
float n_dot_l = max(dot(normal, lightDir), 0.0f);
|
|
||||||
|
|
||||||
return GeometrySchlickGGX(n_dot_v, k) * GeometrySchlickGGX(n_dot_l, k);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculates the fresnel value.
|
|
||||||
* @param h_dot_v Dot product of the normal and view direction
|
|
||||||
* @param F0 base reflectivity
|
|
||||||
*/
|
|
||||||
float3 FresnelSchlick(float cosTheta, float3 F0)
|
|
||||||
{
|
|
||||||
return F0 + (1.0f - F0) * pow(clamp(1.0f - cosTheta, 0.0f, 1.0f), 5.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Reconstructs the z-component of a normalized normal vector from a two-component value
|
|
||||||
* cnrm: The x- and y-components of a normalized normal vector
|
|
||||||
*/
|
|
||||||
//float3 DecompressNormal(float2 cnrm)
|
|
||||||
//{
|
|
||||||
// return float3(cnrm, sqrt(1.0 - cnrm.x * cnrm.x - cnrm.y * cnrm.y));
|
|
||||||
//}
|
|
||||||
|
|
||||||
float4 PS(PS_IN input) : SV_TARGET
|
float4 PS(PS_IN input) : SV_TARGET
|
||||||
{
|
{
|
||||||
// Load Data from GBuffer
|
// Load Data from GBuffer
|
||||||
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);
|
||||||
@@ -152,6 +87,7 @@ float4 PS(PS_IN input) : SV_TARGET
|
|||||||
float3 halfway = normalize(lightDir + viewDir);
|
float3 halfway = normalize(lightDir + viewDir);
|
||||||
|
|
||||||
float n_dot_v = max(dot(surfaceNormal, viewDir), 0.0f);
|
float n_dot_v = max(dot(surfaceNormal, viewDir), 0.0f);
|
||||||
|
float n_dot_h = max(dot(surfaceNormal, halfway), 0.0f);
|
||||||
float n_dot_l = max(dot(surfaceNormal, lightDir), 0.0f);
|
float n_dot_l = max(dot(surfaceNormal, lightDir), 0.0f);
|
||||||
|
|
||||||
float nrmDist = NormalDistributionGGX(surfaceNormal, halfway, roughness);
|
float nrmDist = NormalDistributionGGX(surfaceNormal, halfway, roughness);
|
||||||
@@ -159,49 +95,40 @@ float4 PS(PS_IN input) : SV_TARGET
|
|||||||
|
|
||||||
float3 F0 = 0.04f;
|
float3 F0 = 0.04f;
|
||||||
F0 = lerp(F0, albedo, metallic);
|
F0 = lerp(F0, albedo, metallic);
|
||||||
float3 fresnel = FresnelSchlick(n_dot_v, F0);
|
float3 fresnel = FresnelSchlick(n_dot_h, F0);
|
||||||
|
|
||||||
// if (InspectNrmDist)
|
float3 ks = fresnel;
|
||||||
// return float4(nrmDist.xxx, 1);
|
float3 kd = 1.0f - ks;
|
||||||
// else if (InspectGeo)
|
|
||||||
// return float4(geo.xxx, 1);
|
|
||||||
// else if (InspectFresnel)
|
|
||||||
// return float4(fresnel, 1);
|
|
||||||
// else if (CookTorrance)
|
|
||||||
// {
|
|
||||||
float3 ks = fresnel;
|
|
||||||
float3 kd = 1.0f - ks;
|
|
||||||
|
|
||||||
// Metals have no diffuse light
|
// Metals have no diffuse light
|
||||||
kd *= 1.0f - metallic;
|
kd *= 1.0f - metallic;
|
||||||
|
|
||||||
float3 diffuse = albedo / PI;
|
float3 diffuse = albedo / PI;
|
||||||
float3 specular = (nrmDist * fresnel * geo) / max(4 * n_dot_v * n_dot_l, 0.0001f);
|
float3 specular = (nrmDist * fresnel * geo) / max(4 * n_dot_v * n_dot_l, 0.0001f);
|
||||||
|
|
||||||
float3 luminanceColor = LightColor * Illuminance;
|
float3 luminanceColor = LightColor * Illuminance;
|
||||||
|
|
||||||
float3 cook = (kd * diffuse + specular) * luminanceColor * n_dot_l;
|
float3 cook = (kd * diffuse + specular) * luminanceColor * n_dot_l;
|
||||||
|
|
||||||
float3 final = cook;
|
float3 final = cook;
|
||||||
|
|
||||||
// Tone mapping // TODO: do in postprocessing
|
// Tone mapping // TODO: do in postprocessing
|
||||||
final = final / (final + 1.0f);
|
final = final / (final + 1.0f);
|
||||||
|
|
||||||
// Gamma correction // TODO: do in postprocessing/hardware
|
// Gamma correction // TODO: do in postprocessing/hardware
|
||||||
final = pow(final, 1.0f / 2.2f);
|
final = pow(final, 1.0f / 2.2f);
|
||||||
|
|
||||||
/////////////TODO: REMOVEME
|
#if OUTPUT == Inspect_NormalDistribution
|
||||||
|
final = max(final - 10000000, nrmDist.xxx);
|
||||||
|
#elif OUTPUT == Inspect_GeometryFunction
|
||||||
|
final = max(final - 10000000, geo.xxx);
|
||||||
|
#elif OUTPUT == Inspect_Fresnel
|
||||||
|
final = max(final - 10000000, fresnel);
|
||||||
|
#elif OUTPUT == Inspect_Normal
|
||||||
|
final = max(final - 10000000, surfaceNormal / 2 + 0.5f);
|
||||||
|
#endif
|
||||||
|
|
||||||
//final = max(final - 10000000, nrmDist.xxx);
|
return float4(final, 1);
|
||||||
//final = max(final - 10000000, geo.xxx);
|
|
||||||
//final = max(final - 10000000, fresnel);
|
|
||||||
//final = max(final - 10000000, surfaceNormal / 2 + 0.5f);
|
|
||||||
//final = max(final - 10000000, abs(normal - surfaceNormal) / 2 + 0.5f);
|
|
||||||
|
|
||||||
/////////////TODO: END_REMOVEME
|
|
||||||
|
|
||||||
return float4(final, 1);
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#effect[VS=VS,PS=PS]
|
#effect[VS=VS,PS=PS]
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 119 B |
Binary file not shown.
|
After Width: | Height: | Size: 119 B |
@@ -116,16 +116,18 @@ namespace GlitchyEditor
|
|||||||
|
|
||||||
private void TestEntitiesWithModels()
|
private void TestEntitiesWithModels()
|
||||||
{
|
{
|
||||||
using (Effect myEffect = new Effect("content/Shaders/myEffect.hlsl"))
|
var fxLib = Application.Get().EffectLibrary;
|
||||||
|
|
||||||
|
using (Effect myEffect = fxLib.Load("content/Shaders/myEffect.hlsl"))
|
||||||
using (Texture2D albedo = new Texture2D("Textures/TestMat/rustediron2_albedo.png", true))
|
using (Texture2D albedo = new Texture2D("Textures/TestMat/rustediron2_albedo.png", true))
|
||||||
using (Texture2D normal = new Texture2D("Textures/TestMat/rustediron2_normal.png"))
|
using (Texture2D normal = new Texture2D("Textures/TestMat/rustediron2_normal.png"))
|
||||||
using (Texture2D rough = new Texture2D("Textures/TestMat/rustediron2_roughness.png"))
|
using (Texture2D rough = new Texture2D("Textures/TestMat/rustediron2_roughness.png"))
|
||||||
using (Texture2D metal = new Texture2D("Textures/TestMat/rustediron2_metallic.png"))
|
using (Texture2D metal = new Texture2D("Textures/TestMat/rustediron2_metallic.png"))
|
||||||
{
|
{
|
||||||
albedo.SamplerState = SamplerStateManager.AnisotropicClamp;
|
albedo.SamplerState = SamplerStateManager.AnisotropicWrap;
|
||||||
normal.SamplerState = SamplerStateManager.AnisotropicClamp;
|
normal.SamplerState = SamplerStateManager.AnisotropicWrap;
|
||||||
rough.SamplerState = SamplerStateManager.AnisotropicClamp;
|
rough.SamplerState = SamplerStateManager.AnisotropicWrap;
|
||||||
metal.SamplerState = SamplerStateManager.AnisotropicClamp;
|
metal.SamplerState = SamplerStateManager.AnisotropicWrap;
|
||||||
|
|
||||||
List<AnimationClip> clips = scope .();
|
List<AnimationClip> clips = scope .();
|
||||||
|
|
||||||
@@ -138,10 +140,14 @@ namespace GlitchyEditor
|
|||||||
//mat.SetVariable("BaseColor", Vector4(1, 0, 1, 1));
|
//mat.SetVariable("BaseColor", Vector4(1, 0, 1, 1));
|
||||||
//mat.SetVariable("LightDir", Vector3(1, 1, 0).Normalized());
|
//mat.SetVariable("LightDir", Vector3(1, 1, 0).Normalized());
|
||||||
|
|
||||||
ModelLoader.LoadModel("content/Models/sphere.glb", myEffect, mat, _scene.[Friend]_ecsWorld, clips);
|
EcsEntity e = ModelLoader.LoadModel("content/Models/sphere.glb", myEffect, mat, _scene.[Friend]_ecsWorld, clips, "Sphere 1");
|
||||||
|
|
||||||
|
Entity entity = .(e, _scene);
|
||||||
|
var transform = entity.GetComponent<TransformComponent>();
|
||||||
|
transform.Position = .(5, 0, 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
using (Material mat = new .(myEffect))
|
/*using (Material mat = new .(myEffect))
|
||||||
{
|
{
|
||||||
mat.SetTexture("AlbedoTexture", albedo);
|
mat.SetTexture("AlbedoTexture", albedo);
|
||||||
mat.SetTexture("NormalTexture", normal);
|
mat.SetTexture("NormalTexture", normal);
|
||||||
@@ -152,6 +158,41 @@ namespace GlitchyEditor
|
|||||||
|
|
||||||
|
|
||||||
ModelLoader.LoadModel("content/Models/sphere.glb", myEffect, mat, _scene.[Friend]_ecsWorld, clips);
|
ModelLoader.LoadModel("content/Models/sphere.glb", myEffect, mat, _scene.[Friend]_ecsWorld, clips);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
ClearAndReleaseItems!(clips);
|
||||||
|
}
|
||||||
|
|
||||||
|
using (Effect myEffect = fxLib.Get("myEffect"))
|
||||||
|
using (Texture2D white = new Texture2D("Textures/White.png"))
|
||||||
|
using (Texture2D normal = new Texture2D("Textures/DefaultNormal.png"))
|
||||||
|
{
|
||||||
|
white.SamplerState = SamplerStateManager.PointClamp;
|
||||||
|
normal.SamplerState = SamplerStateManager.PointClamp;
|
||||||
|
|
||||||
|
List<AnimationClip> clips = scope .();
|
||||||
|
|
||||||
|
for (int x < 10)
|
||||||
|
for (int y < 10)
|
||||||
|
{
|
||||||
|
using (Material mat = new .(myEffect))
|
||||||
|
{
|
||||||
|
mat.SetTexture("AlbedoTexture", white);
|
||||||
|
mat.SetTexture("NormalTexture", normal);
|
||||||
|
mat.SetTexture("MetallicTexture", white);
|
||||||
|
mat.SetTexture("RoughnessTexture", white);
|
||||||
|
|
||||||
|
mat.SetVariable("AlbedoColor", Vector4(1, 0, 0, 1));
|
||||||
|
mat.SetVariable("NormalScaling", Vector2(1.0f));
|
||||||
|
mat.SetVariable("RoughnessFactor", (x + 1) / 10.0f);
|
||||||
|
mat.SetVariable("MetallicFactor", y / 9.0f);
|
||||||
|
|
||||||
|
EcsEntity e = ModelLoader.LoadModel("content/Models/sphere.glb", myEffect, mat, _scene.[Friend]_ecsWorld, clips, scope $"Sphere {x} {y}");
|
||||||
|
|
||||||
|
Entity entity = .(e, _scene);
|
||||||
|
var transform = entity.GetComponent<TransformComponent>();
|
||||||
|
transform.Position = .(x * 1.5f, y * 1.5f, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ClearAndReleaseItems!(clips);
|
ClearAndReleaseItems!(clips);
|
||||||
@@ -241,7 +282,7 @@ namespace GlitchyEditor
|
|||||||
|
|
||||||
private bool OnImGuiRender(ImGuiRenderEvent event)
|
private bool OnImGuiRender(ImGuiRenderEvent event)
|
||||||
{
|
{
|
||||||
viewer.ViewTexture(Renderer.[Friend]_gBuffer.Normal);
|
//viewer.ViewTexture(Renderer.[Friend]_gBuffer.Normal);
|
||||||
|
|
||||||
ImGui.Begin("Test");
|
ImGui.Begin("Test");
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ namespace GlitchyEngine.Content
|
|||||||
{
|
{
|
||||||
static readonly Matrix RightToLeftHand = .Scaling(1, 1, -1);
|
static readonly Matrix RightToLeftHand = .Scaling(1, 1, -1);
|
||||||
|
|
||||||
public static void LoadModel(String filename, Effect validationEffect, Material material, EcsWorld world,
|
public static EcsEntity LoadModel(String filename, Effect validationEffect, Material material, EcsWorld world,
|
||||||
List<AnimationClip> outClips)
|
List<AnimationClip> outClips, StringView entityName = StringView())
|
||||||
{
|
{
|
||||||
CGLTF.Options options = .();
|
CGLTF.Options options = .();
|
||||||
CGLTF.Data* data;
|
CGLTF.Data* data;
|
||||||
@@ -25,38 +25,47 @@ namespace GlitchyEngine.Content
|
|||||||
|
|
||||||
Log.EngineLogger.Assert(result == .Success, "Failed to load buffers");
|
Log.EngineLogger.Assert(result == .Success, "Failed to load buffers");
|
||||||
|
|
||||||
|
(EcsEntity entity, ?) = CreateEntity(world, entityName, .InvalidEntity);
|
||||||
|
|
||||||
for(var node in data.Scenes[0].Nodes)
|
for(var node in data.Scenes[0].Nodes)
|
||||||
{
|
{
|
||||||
NodesToEntities(data, node, null, world, validationEffect, material, outClips);
|
NodesToEntities(data, node, entity, world, validationEffect, material, outClips);
|
||||||
}
|
}
|
||||||
|
|
||||||
CGLTF.Free(data);
|
CGLTF.Free(data);
|
||||||
|
|
||||||
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void NodesToEntities(CGLTF.Data* data, CGLTF.Node* node, EcsEntity? parentEntity, EcsWorld world, Effect validationEffect, Material material, List<AnimationClip> clips)
|
private static (EcsEntity Entity, TransformComponent* Transform) CreateEntity(EcsWorld world, StringView? name, EcsEntity parent)
|
||||||
{
|
{
|
||||||
EcsEntity entity = world.NewEntity();
|
EcsEntity entity = world.NewEntity();
|
||||||
|
|
||||||
//#if DEBUG
|
|
||||||
var nameComponent = world.AssignComponent<DebugNameComponent>(entity);
|
var nameComponent = world.AssignComponent<DebugNameComponent>(entity);
|
||||||
|
|
||||||
if (node.Name != null)
|
if (name != null && name.Value.Ptr != null)
|
||||||
{
|
{
|
||||||
nameComponent.SetName(StringView(node.Name));
|
nameComponent.SetName(name.Value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
nameComponent.SetName("Unnamed Node");
|
nameComponent.SetName("Unnamed Node");
|
||||||
}
|
}
|
||||||
//#endif
|
|
||||||
|
|
||||||
if(parentEntity.HasValue)
|
/////TODO: !!!!!!!!REPORT!!!!!!!!!!!!!
|
||||||
{
|
// This works
|
||||||
var childParent = world.AssignComponent<ParentComponent>(entity);
|
TransformComponent cmp = .();
|
||||||
childParent.Entity = parentEntity.Value;
|
var childTransform = world.AssignComponent<TransformComponent>(entity, cmp);
|
||||||
}
|
// This trashes the stack
|
||||||
|
//var childTransform = world.AssignComponent<TransformComponent>(entity);
|
||||||
|
childTransform.Parent = parent;
|
||||||
|
|
||||||
var childTransform = world.AssignComponent<TransformComponent>(entity);
|
return (entity, childTransform);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void NodesToEntities(CGLTF.Data* data, CGLTF.Node* node, EcsEntity parentEntity, EcsWorld world, Effect validationEffect, Material material, List<AnimationClip> clips)
|
||||||
|
{
|
||||||
|
(EcsEntity entity, TransformComponent* childTransform) = CreateEntity(world, node.Name == null ? null : StringView(node.Name), parentEntity);
|
||||||
|
|
||||||
if(node.HasMatrix)
|
if(node.HasMatrix)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -185,6 +185,11 @@ namespace GlitchyEngine.Renderer
|
|||||||
NativeContext.InputAssembler.SetPrimitiveTopology((DirectX.Common.PrimitiveTopology)primitiveTopology);
|
NativeContext.InputAssembler.SetPrimitiveTopology((DirectX.Common.PrimitiveTopology)primitiveTopology);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private uint32 _ps_FirstTexture;
|
||||||
|
private uint32 _ps_BoundTextures;
|
||||||
|
private uint32 _vs_FirstTexture;
|
||||||
|
private uint32 _vs_BoundTextures;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Binds the given shader to the corresponding shader stage.
|
* Binds the given shader to the corresponding shader stage.
|
||||||
* @param shader The shader that will be bound to the graphics context.
|
* @param shader The shader that will be bound to the graphics context.
|
||||||
@@ -219,9 +224,15 @@ namespace GlitchyEngine.Renderer
|
|||||||
// TODO: bind uavs
|
// TODO: bind uavs
|
||||||
NativeContext.PixelShader.SetShaderResources(_firstTexture, _textureCount, &_textures[_firstTexture]);
|
NativeContext.PixelShader.SetShaderResources(_firstTexture, _textureCount, &_textures[_firstTexture]);
|
||||||
NativeContext.PixelShader.SetSamplers(_firstTexture, _textureCount, &_samplers[_firstTexture]);
|
NativeContext.PixelShader.SetSamplers(_firstTexture, _textureCount, &_samplers[_firstTexture]);
|
||||||
|
|
||||||
|
_ps_FirstTexture = _firstTexture;
|
||||||
|
_ps_BoundTextures = _textureCount;
|
||||||
case typeof(VertexShader):
|
case typeof(VertexShader):
|
||||||
NativeContext.VertexShader.SetShaderResources(_firstTexture, _textureCount, &_textures[_firstTexture]);
|
NativeContext.VertexShader.SetShaderResources(_firstTexture, _textureCount, &_textures[_firstTexture]);
|
||||||
NativeContext.VertexShader.SetSamplers(_firstTexture, _textureCount, &_samplers[_firstTexture]);
|
NativeContext.VertexShader.SetSamplers(_firstTexture, _textureCount, &_samplers[_firstTexture]);
|
||||||
|
|
||||||
|
_vs_FirstTexture = _firstTexture;
|
||||||
|
_vs_BoundTextures = _textureCount;
|
||||||
default:
|
default:
|
||||||
Runtime.FatalError(scope $"Shader stage \"{typeof(TShader)}\" not implemented.");
|
Runtime.FatalError(scope $"Shader stage \"{typeof(TShader)}\" not implemented.");
|
||||||
}
|
}
|
||||||
@@ -243,6 +254,14 @@ namespace GlitchyEngine.Renderer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void UnbindTextures()
|
||||||
|
{
|
||||||
|
void** voidArray = scope void*[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT]*;
|
||||||
|
|
||||||
|
NativeContext.PixelShader.SetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, (.)voidArray);
|
||||||
|
NativeContext.VertexShader.SetShaderResources(0, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, (.)voidArray);
|
||||||
|
}
|
||||||
|
|
||||||
public override void SetVertexShader(VertexShader vertexShader)
|
public override void SetVertexShader(VertexShader vertexShader)
|
||||||
{
|
{
|
||||||
BindShaderToStage(vertexShader);
|
BindShaderToStage(vertexShader);
|
||||||
|
|||||||
@@ -142,6 +142,11 @@ namespace GlitchyEngine.Renderer
|
|||||||
|
|
||||||
_context.SetViewport(viewport);
|
_context.SetViewport(viewport);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void UnbindTextures()
|
||||||
|
{
|
||||||
|
_context.UnbindTextures();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -117,5 +117,7 @@ namespace GlitchyEngine.Renderer
|
|||||||
public extern void SetVertexShader(VertexShader vertexShader);
|
public extern void SetVertexShader(VertexShader vertexShader);
|
||||||
|
|
||||||
public extern void SetPixelShader(PixelShader pixelShader);
|
public extern void SetPixelShader(PixelShader pixelShader);
|
||||||
|
|
||||||
|
public extern void UnbindTextures();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,5 +109,10 @@ namespace GlitchyEngine.Renderer
|
|||||||
{
|
{
|
||||||
SetViewport(.(left, top, width, height, minDepth, maxDepth));
|
SetViewport(.(left, top, width, height, minDepth, maxDepth));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void UnbindTextures()
|
||||||
|
{
|
||||||
|
_rendererAPI.UnbindTextures();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,10 +48,10 @@ namespace GlitchyEngine.Renderer
|
|||||||
RenderTarget2DDescription albedoDesc = .(.R8G8B8A8_UNorm, width, height, 1, 1, .D24_UNorm_S8_UInt);
|
RenderTarget2DDescription albedoDesc = .(.R8G8B8A8_UNorm, width, height, 1, 1, .D24_UNorm_S8_UInt);
|
||||||
Albedo = new RenderTarget2D(albedoDesc);
|
Albedo = new RenderTarget2D(albedoDesc);
|
||||||
|
|
||||||
RenderTarget2DDescription normalDesc = .(.R8G8B8A8_SNorm, width, height);
|
RenderTarget2DDescription normalDesc = .(.R16G16B16A16_SNorm, width, height);
|
||||||
Normal = new RenderTarget2D(normalDesc);
|
Normal = new RenderTarget2D(normalDesc);
|
||||||
|
|
||||||
RenderTarget2DDescription tangentDesc = .(.R8G8B8A8_SNorm, width, height);
|
RenderTarget2DDescription tangentDesc = .(.R16G16B16A16_SNorm, width, height);
|
||||||
Tangent = new RenderTarget2D(tangentDesc);
|
Tangent = new RenderTarget2D(tangentDesc);
|
||||||
|
|
||||||
RenderTarget2DDescription positionDesc = .(.R32G32B32A32_Float, width, height);
|
RenderTarget2DDescription positionDesc = .(.R32G32B32A32_Float, width, height);
|
||||||
@@ -289,6 +289,7 @@ namespace GlitchyEngine.Renderer
|
|||||||
// {
|
// {
|
||||||
_gBuffer.EnsureSize(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height);
|
_gBuffer.EnsureSize(_sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height);
|
||||||
_gBuffer.Clear();
|
_gBuffer.Clear();
|
||||||
|
RenderCommand.UnbindRenderTargets();
|
||||||
_gBuffer.Bind();
|
_gBuffer.Bind();
|
||||||
|
|
||||||
RenderCommand.SetViewport(0, 0, _sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height);
|
RenderCommand.SetViewport(0, 0, _sceneConstants.CameraTarget.Width, _sceneConstants.CameraTarget.Height);
|
||||||
@@ -334,6 +335,8 @@ namespace GlitchyEngine.Renderer
|
|||||||
s_quadGeometry.Bind();
|
s_quadGeometry.Bind();
|
||||||
RenderCommand.DrawIndexed(s_quadGeometry);
|
RenderCommand.DrawIndexed(s_quadGeometry);
|
||||||
|
|
||||||
|
RenderCommand.UnbindTextures();
|
||||||
|
|
||||||
// TODO: Draw lights to camera target
|
// TODO: Draw lights to camera target
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|||||||
@@ -55,5 +55,7 @@ namespace GlitchyEngine.Renderer
|
|||||||
public extern void DrawIndexedInstanced(GeometryBinding geometry);
|
public extern void DrawIndexedInstanced(GeometryBinding geometry);
|
||||||
|
|
||||||
public extern void SetViewport(Viewport viewport);
|
public extern void SetViewport(Viewport viewport);
|
||||||
|
|
||||||
|
public extern void UnbindTextures();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user