2020-07-31 00:57:57 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
|
|
|
namespace Smuggler
|
|
|
|
{
|
|
|
|
public struct PBRLight
|
|
|
|
{
|
|
|
|
public Vector3 direction;
|
2020-07-31 07:20:07 +00:00
|
|
|
public Vector3 colour;
|
2020-07-31 00:57:57 +00:00
|
|
|
|
2020-07-31 07:20:07 +00:00
|
|
|
public PBRLight(Vector3 direction, Vector3 colour)
|
2020-07-31 00:57:57 +00:00
|
|
|
{
|
|
|
|
this.direction = direction;
|
2020-07-31 07:20:07 +00:00
|
|
|
this.colour = colour;
|
2020-07-31 00:57:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class PBREffect : Effect
|
|
|
|
{
|
2020-07-31 07:20:07 +00:00
|
|
|
readonly EffectParameter modelParam;
|
|
|
|
readonly EffectParameter viewParam;
|
|
|
|
readonly EffectParameter projectionParam;
|
|
|
|
readonly EffectParameter lightDirParam;
|
|
|
|
readonly EffectParameter lightColourParam;
|
|
|
|
readonly EffectParameter normalScaleParam;
|
|
|
|
readonly EffectParameter emissiveFactorParam;
|
|
|
|
readonly EffectParameter occlusionStrengthParam;
|
|
|
|
readonly EffectParameter metallicRoughnessValuesParam;
|
|
|
|
readonly EffectParameter baseColorFactorParam;
|
|
|
|
readonly EffectParameter cameraLookParam;
|
2020-07-31 00:57:57 +00:00
|
|
|
|
2020-07-31 07:20:07 +00:00
|
|
|
readonly EffectParameter baseColourTextureParam;
|
|
|
|
readonly EffectParameter normalTextureParam;
|
|
|
|
readonly EffectParameter emissionTextureParam;
|
|
|
|
readonly EffectParameter occlusionTextureParam;
|
|
|
|
readonly EffectParameter metallicRoughnessTextureParam;
|
|
|
|
readonly EffectParameter envDiffuseTextureParam;
|
|
|
|
readonly EffectParameter brdfLutTextureParam;
|
|
|
|
readonly EffectParameter envSpecularTextureParam;
|
2020-07-31 00:57:57 +00:00
|
|
|
|
|
|
|
Matrix world = Matrix.Identity;
|
|
|
|
Matrix view = Matrix.Identity;
|
|
|
|
Matrix projection = Matrix.Identity;
|
2020-07-31 07:20:07 +00:00
|
|
|
PBRLight light = new PBRLight();
|
|
|
|
float normalScale = 1;
|
|
|
|
Vector3 emissiveFactor;
|
|
|
|
float occlusionStrength;
|
|
|
|
Vector2 metallicRoughnessValue;
|
|
|
|
Vector4 baseColorFactor;
|
2020-07-31 00:57:57 +00:00
|
|
|
|
|
|
|
public Matrix World
|
|
|
|
{
|
|
|
|
get { return world; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
world = value;
|
2020-07-31 07:20:07 +00:00
|
|
|
modelParam.SetValue(world);
|
2020-07-31 00:57:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Matrix View
|
|
|
|
{
|
|
|
|
get { return view; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
view = value;
|
2020-07-31 07:20:07 +00:00
|
|
|
viewParam.SetValue(view);
|
2020-07-31 08:55:29 +00:00
|
|
|
cameraLookParam.SetValue(-new Vector3(
|
|
|
|
view.M13,
|
|
|
|
view.M23,
|
|
|
|
view.M33
|
|
|
|
));
|
2020-07-31 00:57:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Matrix Projection
|
|
|
|
{
|
|
|
|
get { return projection; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
projection = value;
|
2020-07-31 07:20:07 +00:00
|
|
|
projectionParam.SetValue(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public PBRLight Light
|
|
|
|
{
|
|
|
|
get { return light; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
light = value;
|
|
|
|
lightDirParam.SetValue(light.direction);
|
|
|
|
lightColourParam.SetValue(light.colour);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public float NormalScale
|
|
|
|
{
|
|
|
|
get { return normalScale; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
normalScale = value;
|
|
|
|
normalScaleParam.SetValue(normalScale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector3 EmissiveFactor
|
|
|
|
{
|
|
|
|
get { return emissiveFactor; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
emissiveFactor = value;
|
|
|
|
emissiveFactorParam.SetValue(emissiveFactor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public float OcclusionStrength
|
|
|
|
{
|
|
|
|
get { return occlusionStrength; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
occlusionStrength = value;
|
|
|
|
occlusionStrengthParam.SetValue(occlusionStrength);
|
2020-07-31 00:57:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:20:07 +00:00
|
|
|
public Vector2 MetallicRoughnessValue
|
|
|
|
{
|
|
|
|
get { return metallicRoughnessValue; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
metallicRoughnessValue = value;
|
|
|
|
metallicRoughnessValuesParam.SetValue(metallicRoughnessValue);
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 00:57:57 +00:00
|
|
|
|
2020-07-31 07:20:07 +00:00
|
|
|
public Vector4 BaseColorFactor
|
2020-07-31 00:57:57 +00:00
|
|
|
{
|
2020-07-31 07:20:07 +00:00
|
|
|
get { return baseColorFactor; }
|
2020-07-31 00:57:57 +00:00
|
|
|
set
|
|
|
|
{
|
2020-07-31 07:20:07 +00:00
|
|
|
baseColorFactor = value;
|
|
|
|
baseColorFactorParam.SetValue(baseColorFactor);
|
2020-07-31 00:57:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:20:07 +00:00
|
|
|
public Texture2D BaseColourTexture
|
|
|
|
{
|
|
|
|
get { return baseColourTextureParam.GetValueTexture2D(); }
|
|
|
|
set { baseColourTextureParam.SetValue(value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public Texture2D NormalTexture
|
|
|
|
{
|
|
|
|
get { return normalTextureParam.GetValueTexture2D(); }
|
|
|
|
set { normalTextureParam.SetValue(value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
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); }
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-07-31 07:20:07 +00:00
|
|
|
modelParam = Parameters["model"];
|
|
|
|
viewParam = Parameters["view"];
|
2020-07-31 08:55:29 +00:00
|
|
|
projectionParam = Parameters["projection"];
|
2020-07-31 07:20:07 +00:00
|
|
|
|
|
|
|
lightDirParam = Parameters["lightDir"];
|
|
|
|
lightColourParam = Parameters["lightColour"];
|
2020-07-31 00:57:57 +00:00
|
|
|
|
2020-07-31 07:20:07 +00:00
|
|
|
normalScaleParam = Parameters["normalScale"];
|
|
|
|
emissiveFactorParam = Parameters["emissiveFactor"];
|
|
|
|
occlusionStrengthParam = Parameters["occlusionStrength"];
|
|
|
|
metallicRoughnessValuesParam = Parameters["metallicRoughnessValues"];
|
|
|
|
baseColorFactorParam = Parameters["baseColorFactor"];
|
|
|
|
cameraLookParam = Parameters["camera"];
|
2020-07-31 00:57:57 +00:00
|
|
|
|
2020-07-31 07:20:07 +00:00
|
|
|
baseColourTextureParam = Parameters["baseColourTexture"];
|
|
|
|
normalTextureParam = Parameters["normalTexture"];
|
|
|
|
emissionTextureParam = Parameters["emissionTexture"];
|
|
|
|
occlusionTextureParam = Parameters["occlusionTexture"];
|
|
|
|
metallicRoughnessTextureParam = Parameters["metallicRoughnessTexture"];
|
|
|
|
envDiffuseTextureParam = Parameters["envDiffuseTexture"];
|
|
|
|
brdfLutTextureParam = Parameters["brdfLutTexture"];
|
|
|
|
envSpecularTextureParam = Parameters["envSpecularTexture"];
|
2020-07-31 00:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected PBREffect(PBREffect cloneSource) : base(cloneSource)
|
|
|
|
{
|
2020-07-31 07:20:07 +00:00
|
|
|
modelParam = Parameters["model"];
|
|
|
|
viewParam = Parameters["view"];
|
|
|
|
projectionParam = Parameters["param"];
|
2020-07-31 00:57:57 +00:00
|
|
|
|
2020-07-31 07:20:07 +00:00
|
|
|
lightDirParam = Parameters["lightDir"];
|
|
|
|
lightColourParam = Parameters["lightColour"];
|
|
|
|
|
|
|
|
normalScaleParam = Parameters["normalScale"];
|
|
|
|
emissiveFactorParam = Parameters["emissiveFactor"];
|
|
|
|
occlusionStrengthParam = Parameters["occlusionStrength"];
|
|
|
|
metallicRoughnessValuesParam = Parameters["metallicRoughnessValues"];
|
|
|
|
baseColorFactorParam = Parameters["baseColorFactor"];
|
|
|
|
cameraLookParam = Parameters["camera"];
|
|
|
|
|
|
|
|
baseColourTextureParam = Parameters["baseColourTexture"];
|
|
|
|
normalTextureParam = Parameters["normalTexture"];
|
|
|
|
emissionTextureParam = Parameters["emissionTexture"];
|
|
|
|
occlusionTextureParam = Parameters["occlusionTexture"];
|
|
|
|
metallicRoughnessTextureParam = Parameters["metallicRoughnessTexture"];
|
|
|
|
envDiffuseTextureParam = Parameters["envDiffuseTexture"];
|
|
|
|
brdfLutTextureParam = Parameters["brdfLutTexture"];
|
|
|
|
envSpecularTextureParam = Parameters["envSpecularTexture"];
|
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
|
|
|
|
|
|
|
Light = cloneSource.Light;
|
|
|
|
|
|
|
|
NormalScale = cloneSource.normalScale;
|
|
|
|
EmissiveFactor = cloneSource.EmissiveFactor;
|
|
|
|
OcclusionStrength = cloneSource.OcclusionStrength;
|
|
|
|
MetallicRoughnessValue = cloneSource.MetallicRoughnessValue;
|
|
|
|
BaseColorFactor = cloneSource.BaseColorFactor;
|
|
|
|
|
|
|
|
BaseColourTexture = cloneSource.BaseColourTexture;
|
|
|
|
NormalTexture = cloneSource.NormalTexture;
|
|
|
|
EmissionTexture = cloneSource.EmissionTexture;
|
|
|
|
OcclusionTexture = cloneSource.OcclusionTexture;
|
|
|
|
MetallicRoughnessTexture = cloneSource.MetallicRoughnessTexture;
|
|
|
|
EnvDiffuseTexture = cloneSource.EnvDiffuseTexture;
|
|
|
|
BRDFLutTexture = cloneSource.BRDFLutTexture;
|
|
|
|
EnvSpecularTexture = cloneSource.EnvSpecularTexture;
|
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()
|
|
|
|
{
|
|
|
|
base.OnApply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|