more PBR implementation

PBR
Evan Hemsley 2020-07-31 01:55:29 -07:00
parent 466f35b1ee
commit f18a8f38be
7 changed files with 153 additions and 36 deletions

View File

@ -47,7 +47,6 @@ namespace Smuggler
float occlusionStrength;
Vector2 metallicRoughnessValue;
Vector4 baseColorFactor;
Vector3 cameraLook;
public Matrix World
{
@ -66,6 +65,11 @@ namespace Smuggler
{
view = value;
viewParam.SetValue(view);
cameraLookParam.SetValue(-new Vector3(
view.M13,
view.M23,
view.M33
));
}
}
@ -140,16 +144,6 @@ namespace Smuggler
}
}
public Vector3 CameraLook
{
get { return cameraLook; }
set
{
cameraLook = value;
cameraLookParam.SetValue(cameraLook);
}
}
public Texture2D BaseColourTexture
{
get { return baseColourTextureParam.GetValueTexture2D(); }
@ -198,11 +192,11 @@ namespace Smuggler
set { envSpecularTextureParam.SetValue(value); }
}
public PBREffect(GraphicsDevice graphicsDevice, byte[] effectCode) : base(graphicsDevice, effectCode)
public PBREffect(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.PBREffect)
{
modelParam = Parameters["model"];
viewParam = Parameters["view"];
projectionParam = Parameters["param"];
projectionParam = Parameters["projection"];
lightDirParam = Parameters["lightDir"];
lightColourParam = Parameters["lightColour"];
@ -260,7 +254,6 @@ namespace Smuggler
OcclusionStrength = cloneSource.OcclusionStrength;
MetallicRoughnessValue = cloneSource.MetallicRoughnessValue;
BaseColorFactor = cloneSource.BaseColorFactor;
CameraLook = cloneSource.CameraLook;
BaseColourTexture = cloneSource.BaseColourTexture;
NormalTexture = cloneSource.NormalTexture;

View File

@ -32,14 +32,6 @@ namespace Smuggler
var model = new Model(meshes.ToArray());
/* TODO: BasicEffect only supports one texture but GLTF supports several */
if (sharpModel.LogicalTextures.Count > 0)
{
var fnaTexture = Texture2D.FromStream(graphicsDevice, sharpModel.LogicalTextures[0].PrimaryImage.Content.Open());
model.SetTexture(fnaTexture);
}
return model;
}
@ -145,9 +137,114 @@ namespace Smuggler
ImportVertexPositionTexture(primitive, vertexBuffer, indices, positions);
}
/* TODO: We need a new Effect subclass to support some GLTF features */
var effect = new PBREffect(graphicsDevice);
if (primitive.Material != null)
{
var normalChannel = primitive.Material.FindChannel("Normal");
if (normalChannel.HasValue)
{
if (normalChannel.Value.Texture != null)
{
effect.NormalTexture = Texture2D.FromStream(
graphicsDevice,
normalChannel.Value.Texture.PrimaryImage.Content.Open()
);
}
effect.NormalScale = normalChannel.Value.Parameter.X;
}
var occlusionChannel = primitive.Material.FindChannel("Occlusion");
if (occlusionChannel.HasValue)
{
if (occlusionChannel.Value.Texture != null)
{
effect.OcclusionTexture = Texture2D.FromStream(
graphicsDevice,
occlusionChannel.Value.Texture.PrimaryImage.Content.Open()
);
}
effect.OcclusionStrength = occlusionChannel.Value.Parameter.X;
}
var emissiveChannel = primitive.Material.FindChannel("Emissive");
if (emissiveChannel.HasValue)
{
if (emissiveChannel.Value.Texture != null)
{
effect.EmissionTexture = Texture2D.FromStream(
graphicsDevice,
emissiveChannel.Value.Texture.PrimaryImage.Content.Open()
);
}
var parameter = emissiveChannel.Value.Parameter;
effect.EmissiveFactor = new Vector3(
parameter.X,
parameter.Y,
parameter.Z
);
}
var baseColorChannel = primitive.Material.FindChannel("BaseColor");
if (baseColorChannel.HasValue)
{
if (baseColorChannel.Value.Texture != null)
{
effect.BaseColourTexture = Texture2D.FromStream(
graphicsDevice,
baseColorChannel.Value.Texture.PrimaryImage.Content.Open()
);
}
var parameter = emissiveChannel.Value.Parameter;
effect.BaseColorFactor = new Vector4(
parameter.X,
parameter.Y,
parameter.Z,
parameter.W
);
}
var metallicRoughnessChannel = primitive.Material.FindChannel("MetallicRoughness");
if (metallicRoughnessChannel.HasValue)
{
if (metallicRoughnessChannel.Value.Texture != null)
{
effect.MetallicRoughnessTexture = Texture2D.FromStream(
graphicsDevice,
metallicRoughnessChannel.Value.Texture.PrimaryImage.Content.Open()
);
}
var parameter = metallicRoughnessChannel.Value.Parameter;
effect.MetallicRoughnessValue = new Vector2(
parameter.X,
parameter.Y
);
}
}
effect.Light = new PBRLight(new Vector3(-0.5f, -0.5f, -0.5f), Color.White.ToVector3());
/* FIXME: how to load cube maps from GLTF? */
/*
var diffuseChannel = primitive.Material.FindChannel("Diffuse");
if (diffuseChannel.HasValue)
{
}
var specularChannel = primitive.Material.FindChannel("SpecularGlossiness");
if (specularChannel.HasValue)
{
}
*/
var effect = new PBREffect(graphicsDevice, File.ReadAllBytes("Effects/pbr.fxo"));
return new MeshPart(
vertexBuffer,

View File

@ -13,18 +13,6 @@ namespace Smuggler
Meshes = meshes;
}
public void SetTexture(Texture2D texture)
{
foreach (var mesh in Meshes)
{
foreach (var meshPart in mesh.MeshParts)
{
meshPart.Effect.TextureEnabled = true;
meshPart.Effect.Texture = texture;
}
}
}
public void Draw(GraphicsDevice graphicsDevice, Matrix world, Matrix view, Matrix projection)
{
foreach (var mesh in Meshes)

33
Resources.cs Normal file
View File

@ -0,0 +1,33 @@
using System.IO;
namespace Smuggler
{
internal class Resources
{
public static byte[] PBREffect
{
get
{
if (pbrEffect == null)
{
pbrEffect = GetResource("PBREffect");
}
return pbrEffect;
}
}
private static byte[] pbrEffect;
private static byte[] GetResource(string name)
{
Stream stream = typeof(Resources).Assembly.GetManifestResourceStream(
"Smuggler.Resources." + name + ".fxb"
);
using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms);
return ms.ToArray();
}
}
}
}

View File

@ -18,4 +18,10 @@
<ProjectReference Include="..\FNA\FNA.Core.csproj"/>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Effects\PBREffect.fxb">
<LogicalName>Smuggler.Resources.PBREffect.fxb</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Project>