Smuggler/Effects/PBREffect.cs

358 lines
11 KiB
C#
Raw Normal View History

2020-07-31 00:57:57 +00:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Smuggler
{
public struct PBRLight
{
public Vector3 position;
public Vector3 color;
2020-07-31 00:57:57 +00:00
public PBRLight(Vector3 position, Vector3 colour)
2020-07-31 00:57:57 +00:00
{
this.position = position;
this.color = colour;
}
}
public class PBRLightCollection
{
private readonly Vector3[] positions = new Vector3[4];
private readonly Vector3[] colors = new Vector3[4];
readonly EffectParameter lightPositionsParam;
readonly EffectParameter lightColorsParam;
public PBRLightCollection(EffectParameter lightPositionsParam, EffectParameter lightColorsParam)
{
this.lightPositionsParam = lightPositionsParam;
this.lightColorsParam = lightColorsParam;
}
public PBRLight this[int i]
{
get { return new PBRLight(positions[i], colors[i]); }
set
{
positions[i] = value.position;
colors[i] = value.color;
lightPositionsParam.SetValue(positions);
lightColorsParam.SetValue(colors);
}
2020-07-31 00:57:57 +00:00
}
}
public class PBREffect : Effect
{
EffectParameter worldParam;
EffectParameter worldViewProjectionParam;
EffectParameter worldInverseTransposeParam;
EffectParameter albedoTextureParam;
EffectParameter normalTextureParam;
EffectParameter emissionTextureParam;
EffectParameter occlusionTextureParam;
EffectParameter metallicRoughnessTextureParam;
EffectParameter envDiffuseTextureParam;
EffectParameter brdfLutTextureParam;
EffectParameter envSpecularTextureParam;
EffectParameter lightPositionsParam;
EffectParameter lightColorsParam;
EffectParameter albedoParam;
EffectParameter metallicParam;
EffectParameter roughnessParam;
EffectParameter aoParam;
EffectParameter eyePositionParam;
2020-07-31 00:57:57 +00:00
EffectParameter shaderIndexParam;
2020-07-31 00:57:57 +00:00
Matrix world = Matrix.Identity;
Matrix view = Matrix.Identity;
Matrix projection = Matrix.Identity;
PBRLightCollection pbrLightCollection;
Vector3 albedo;
float metallic;
float roughness;
float ao;
bool albedoTextureEnabled = false;
bool metallicRoughnessMapEnabled = false;
bool normalMapEnabled = false;
// FIXME: lazily set properties for performance
2020-07-31 00:57:57 +00:00
public Matrix World
{
get { return world; }
set
{
world = value;
worldParam.SetValue(world);
2020-08-01 20:43:04 +00:00
Matrix.Multiply(ref world, ref view, out Matrix worldView);
Matrix.Multiply(ref worldView, ref projection, out Matrix worldViewProj);
worldViewProjectionParam.SetValue(worldViewProj);
Matrix.Invert(ref world, out Matrix worldInverse);
Matrix.Transpose(ref worldInverse, out Matrix worldInverseTranspose);
worldInverseTransposeParam.SetValue(worldInverseTranspose);
2020-07-31 00:57:57 +00:00
}
}
public Matrix View
{
get { return view; }
set
{
view = value;
2020-08-01 20:43:04 +00:00
Matrix.Multiply(ref world, ref view, out Matrix worldView);
Matrix.Multiply(ref worldView, ref projection, out Matrix worldViewProj);
worldViewProjectionParam.SetValue(worldViewProj);
Matrix.Invert(ref view, out Matrix inverseView);
eyePositionParam.SetValue(inverseView.Translation);
2020-07-31 00:57:57 +00:00
}
}
public Matrix Projection
{
get { return projection; }
set
{
projection = value;
2020-08-01 20:43:04 +00:00
Matrix.Multiply(ref world, ref view, out Matrix worldView);
Matrix.Multiply(ref worldView, ref projection, out Matrix worldViewProj);
worldViewProjectionParam.SetValue(worldViewProj);
2020-07-31 07:20:07 +00:00
}
}
public PBRLightCollection Lights
2020-07-31 07:20:07 +00:00
{
get { return pbrLightCollection; }
internal set { pbrLightCollection = value; }
2020-07-31 07:20:07 +00:00
}
public Vector3 Albedo
2020-07-31 07:20:07 +00:00
{
get { return albedo; }
2020-07-31 07:20:07 +00:00
set
{
albedo = value;
albedoParam.SetValue(albedo);
2020-07-31 07:20:07 +00:00
}
}
public float Metallic
2020-07-31 07:20:07 +00:00
{
get { return metallic; }
2020-07-31 07:20:07 +00:00
set
{
metallic = value;
metallicParam.SetValue(metallic);
2020-07-31 00:57:57 +00:00
}
}
public float Roughness
2020-07-31 07:20:07 +00:00
{
get { return roughness; }
2020-07-31 07:20:07 +00:00
set
{
roughness = value;
roughnessParam.SetValue(roughness);
2020-07-31 07:20:07 +00:00
}
}
2020-07-31 00:57:57 +00:00
public float AO
2020-07-31 00:57:57 +00:00
{
get { return ao; }
2020-07-31 00:57:57 +00:00
set
{
ao = value;
aoParam.SetValue(ao);
2020-07-31 00:57:57 +00:00
}
}
public Texture2D AlbedoTexture
2020-07-31 07:20:07 +00:00
{
get { return albedoTextureParam.GetValueTexture2D(); }
set
{
albedoTextureParam.SetValue(value);
albedoTextureEnabled = value != null;
System.Console.WriteLine(albedoTextureEnabled);
}
2020-07-31 07:20:07 +00:00
}
public Texture2D NormalTexture
{
get { return normalTextureParam.GetValueTexture2D(); }
set
{
normalTextureParam.SetValue(value);
normalMapEnabled = value != null;
}
2020-07-31 07:20:07 +00:00
}
public Texture2D EmissionTexture
2020-07-31 00:57:57 +00:00
{
2020-07-31 07:20:07 +00:00
get { return emissionTextureParam.GetValueTexture2D(); }
set { emissionTextureParam.SetValue(value); }
}
public Texture2D OcclusionTexture
{
get { return occlusionTextureParam.GetValueTexture2D(); }
set { occlusionTextureParam.SetValue(value); }
}
public Texture2D MetallicRoughnessTexture
{
get { return metallicRoughnessTextureParam.GetValueTexture2D(); }
set
{
metallicRoughnessTextureParam.SetValue(value);
metallicRoughnessMapEnabled = value != null;
}
2020-07-31 07:20:07 +00:00
}
public TextureCube EnvDiffuseTexture
{
get { return envDiffuseTextureParam.GetValueTextureCube(); }
set { envDiffuseTextureParam.SetValue(value); }
}
public Texture2D BRDFLutTexture
{
get { return brdfLutTextureParam.GetValueTexture2D(); }
set { brdfLutTextureParam.SetValue(value); }
}
public TextureCube EnvSpecularTexture
{
get { return envSpecularTextureParam.GetValueTextureCube(); }
set { envSpecularTextureParam.SetValue(value); }
2020-07-31 00:57:57 +00:00
}
2020-07-31 08:55:29 +00:00
public PBREffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.PBREffect)
2020-07-31 00:57:57 +00:00
{
CacheEffectParameters(null);
pbrLightCollection = new PBRLightCollection(
Parameters["LightPositions"],
Parameters["LightColors"]
);
2020-07-31 00:57:57 +00:00
}
protected PBREffect(PBREffect cloneSource) : base(cloneSource)
{
CacheEffectParameters(cloneSource);
2020-07-31 00:57:57 +00:00
World = cloneSource.World;
View = cloneSource.View;
Projection = cloneSource.Projection;
2020-07-31 07:20:07 +00:00
Lights = new PBRLightCollection(
Parameters["LightPositions"],
Parameters["LightColors"]
);
2020-07-31 07:20:07 +00:00
for (int i = 0; i < 4; i++)
{
Lights[i] = cloneSource.Lights[i];
}
2020-07-31 07:20:07 +00:00
AlbedoTexture = cloneSource.AlbedoTexture;
2020-07-31 07:20:07 +00:00
NormalTexture = cloneSource.NormalTexture;
EmissionTexture = cloneSource.EmissionTexture;
OcclusionTexture = cloneSource.OcclusionTexture;
MetallicRoughnessTexture = cloneSource.MetallicRoughnessTexture;
EnvDiffuseTexture = cloneSource.EnvDiffuseTexture;
BRDFLutTexture = cloneSource.BRDFLutTexture;
EnvSpecularTexture = cloneSource.EnvSpecularTexture;
Albedo = cloneSource.Albedo;
Metallic = cloneSource.Metallic;
Roughness = cloneSource.Roughness;
AO = cloneSource.AO;
2020-07-31 00:57:57 +00:00
}
public override Effect Clone()
{
return new PBREffect(this);
}
// FIXME: do param applications here for performance
protected override void OnApply()
{
int shaderIndex = 0;
if (albedoTextureEnabled && metallicRoughnessMapEnabled && normalMapEnabled)
{
System.Console.WriteLine("all enabled");
shaderIndex = 7;
}
else if (metallicRoughnessMapEnabled && normalMapEnabled)
{
shaderIndex = 6;
}
else if (albedoTextureEnabled && normalMapEnabled)
{
shaderIndex = 5;
}
else if (albedoTextureEnabled && metallicRoughnessMapEnabled)
{
shaderIndex = 4;
}
else if (normalMapEnabled)
{
shaderIndex = 3;
}
else if (metallicRoughnessMapEnabled)
{
shaderIndex = 2;
}
else if (albedoTextureEnabled)
{
shaderIndex = 1;
}
System.Console.WriteLine(shaderIndex);
shaderIndexParam.SetValue(shaderIndex);
2020-07-31 00:57:57 +00:00
}
void CacheEffectParameters(PBREffect cloneSource)
{
worldParam = Parameters["World"];
worldViewProjectionParam = Parameters["WorldViewProjection"];
worldInverseTransposeParam = Parameters["WorldInverseTranspose"];
albedoTextureParam = Parameters["AlbedoTexture"];
normalTextureParam = Parameters["NormalTexture"];
emissionTextureParam = Parameters["EmissionTexture"];
occlusionTextureParam = Parameters["OcclusionTexture"];
metallicRoughnessTextureParam = Parameters["MetallicRoughnessTexture"];
envDiffuseTextureParam = Parameters["EnvDiffuseTexture"];
brdfLutTextureParam = Parameters["BrdfLutTexture"];
envSpecularTextureParam = Parameters["EnvSpecularTexture"];
lightPositionsParam = Parameters["LightPositions"];
lightColorsParam = Parameters["LightColors"];
albedoParam = Parameters["AlbedoValue"];
metallicParam = Parameters["MetallicValue"];
roughnessParam = Parameters["RoughnessValue"];
aoParam = Parameters["AO"];
eyePositionParam = Parameters["EyePosition"];
shaderIndexParam = Parameters["ShaderIndex"];
}
2020-07-31 00:57:57 +00:00
}
}