287 lines
9.4 KiB
C#
287 lines
9.4 KiB
C#
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
namespace Smuggler
|
|
{
|
|
public struct PBRLight
|
|
{
|
|
public Vector3 direction;
|
|
public Vector3 colour;
|
|
|
|
public PBRLight(Vector3 direction, Vector3 colour)
|
|
{
|
|
this.direction = direction;
|
|
this.colour = colour;
|
|
}
|
|
}
|
|
|
|
public class PBREffect : Effect
|
|
{
|
|
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;
|
|
|
|
readonly EffectParameter baseColourTextureParam;
|
|
readonly EffectParameter normalTextureParam;
|
|
readonly EffectParameter emissionTextureParam;
|
|
readonly EffectParameter occlusionTextureParam;
|
|
readonly EffectParameter metallicRoughnessTextureParam;
|
|
readonly EffectParameter envDiffuseTextureParam;
|
|
readonly EffectParameter brdfLutTextureParam;
|
|
readonly EffectParameter envSpecularTextureParam;
|
|
|
|
Matrix world = Matrix.Identity;
|
|
Matrix view = Matrix.Identity;
|
|
Matrix projection = Matrix.Identity;
|
|
PBRLight light = new PBRLight();
|
|
float normalScale = 1;
|
|
Vector3 emissiveFactor;
|
|
float occlusionStrength;
|
|
Vector2 metallicRoughnessValue;
|
|
Vector4 baseColorFactor;
|
|
Vector3 cameraLook;
|
|
|
|
public Matrix World
|
|
{
|
|
get { return world; }
|
|
set
|
|
{
|
|
world = value;
|
|
modelParam.SetValue(world);
|
|
}
|
|
}
|
|
|
|
public Matrix View
|
|
{
|
|
get { return view; }
|
|
set
|
|
{
|
|
view = value;
|
|
viewParam.SetValue(view);
|
|
}
|
|
}
|
|
|
|
public Matrix Projection
|
|
{
|
|
get { return projection; }
|
|
set
|
|
{
|
|
projection = value;
|
|
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);
|
|
}
|
|
}
|
|
|
|
public Vector2 MetallicRoughnessValue
|
|
{
|
|
get { return metallicRoughnessValue; }
|
|
set
|
|
{
|
|
metallicRoughnessValue = value;
|
|
metallicRoughnessValuesParam.SetValue(metallicRoughnessValue);
|
|
}
|
|
}
|
|
|
|
public Vector4 BaseColorFactor
|
|
{
|
|
get { return baseColorFactor; }
|
|
set
|
|
{
|
|
baseColorFactor = value;
|
|
baseColorFactorParam.SetValue(baseColorFactor);
|
|
}
|
|
}
|
|
|
|
public Vector3 CameraLook
|
|
{
|
|
get { return cameraLook; }
|
|
set
|
|
{
|
|
cameraLook = value;
|
|
cameraLookParam.SetValue(cameraLook);
|
|
}
|
|
}
|
|
|
|
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
|
|
{
|
|
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); }
|
|
}
|
|
|
|
public PBREffect(GraphicsDevice graphicsDevice, byte[] effectCode) : base(graphicsDevice, effectCode)
|
|
{
|
|
modelParam = Parameters["model"];
|
|
viewParam = Parameters["view"];
|
|
projectionParam = Parameters["param"];
|
|
|
|
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"];
|
|
}
|
|
|
|
protected PBREffect(PBREffect cloneSource) : base(cloneSource)
|
|
{
|
|
modelParam = Parameters["model"];
|
|
viewParam = Parameters["view"];
|
|
projectionParam = Parameters["param"];
|
|
|
|
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"];
|
|
|
|
World = cloneSource.World;
|
|
View = cloneSource.View;
|
|
Projection = cloneSource.Projection;
|
|
|
|
Light = cloneSource.Light;
|
|
|
|
NormalScale = cloneSource.normalScale;
|
|
EmissiveFactor = cloneSource.EmissiveFactor;
|
|
OcclusionStrength = cloneSource.OcclusionStrength;
|
|
MetallicRoughnessValue = cloneSource.MetallicRoughnessValue;
|
|
BaseColorFactor = cloneSource.BaseColorFactor;
|
|
CameraLook = cloneSource.CameraLook;
|
|
|
|
BaseColourTexture = cloneSource.BaseColourTexture;
|
|
NormalTexture = cloneSource.NormalTexture;
|
|
EmissionTexture = cloneSource.EmissionTexture;
|
|
OcclusionTexture = cloneSource.OcclusionTexture;
|
|
MetallicRoughnessTexture = cloneSource.MetallicRoughnessTexture;
|
|
EnvDiffuseTexture = cloneSource.EnvDiffuseTexture;
|
|
BRDFLutTexture = cloneSource.BRDFLutTexture;
|
|
EnvSpecularTexture = cloneSource.EnvSpecularTexture;
|
|
}
|
|
|
|
public override Effect Clone()
|
|
{
|
|
return new PBREffect(this);
|
|
}
|
|
|
|
// FIXME: do param applications here for performance
|
|
protected override void OnApply()
|
|
{
|
|
base.OnApply();
|
|
}
|
|
}
|
|
}
|