removed all references to effect processing etc

PBR
Evan Hemsley 2020-08-05 12:15:02 -07:00
parent 39fec5062c
commit d670a0ecbd
14 changed files with 101 additions and 999 deletions

View File

@ -1,14 +0,0 @@
using System;
namespace Smuggler
{
[Flags]
internal enum EffectDirtyFlags
{
WorldViewProj = 1,
World = 2,
EyePosition = 4,
ShaderIndex = 8,
All = -1
}
}

View File

@ -1,57 +0,0 @@
//-----------------------------------------------------------------------------
// Macros.fxh
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#ifdef SM4
// Macros for targetting shader model 4.0 (DX11)
#define BEGIN_CONSTANTS cbuffer Parameters : register(b0) {
#define MATRIX_CONSTANTS }; cbuffer ProjectionMatrix : register(b1) {
#define END_CONSTANTS };
#define _vs(r)
#define _ps(r)
#define _cb(r) : packoffset(r)
#define DECLARE_TEXTURE(Name, index) \
Texture2D<float4> Name : register(t##index); \
sampler Name##Sampler : register(s##index)
#define DECLARE_CUBEMAP(Name, index) \
TextureCube<float4> Name : register(t##index); \
sampler Name##Sampler : register(s##index)
#define SAMPLE_TEXTURE(Name, texCoord) Name.Sample(Name##Sampler, texCoord)
#define SAMPLE_CUBEMAP(Name, texCoord) Name.Sample(Name##Sampler, texCoord)
#define SAMPLE_CUBEMAP_LOD(Name, texCoord) Name.SampleLevel(Name##Sampler, texCoord.xyz, texCoord.w)
#else
// Macros for targetting shader model 2.0 (DX9)
#define BEGIN_CONSTANTS
#define MATRIX_CONSTANTS
#define END_CONSTANTS
#define _vs(r) : register(vs, r)
#define _ps(r) : register(ps, r)
#define _cb(r)
#define DECLARE_TEXTURE(Name, index) \
texture2D Name; \
sampler Name##Sampler : register(s##index) = sampler_state { Texture = (Name); };
#define DECLARE_CUBEMAP(Name, index) \
textureCUBE Name; \
sampler Name##Sampler : register(s##index) = sampler_state { Texture = (Name); };
#define SAMPLE_TEXTURE(Name, texCoord) tex2D(Name##Sampler, texCoord)
#define SAMPLE_CUBEMAP(Name, texCoord) texCUBE(Name##Sampler, texCoord)
#define SAMPLE_CUBEMAP_LOD(Name, texCoord) texCUBElod(Name##Sampler, texCoord)
#endif

View File

@ -1,374 +0,0 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Smuggler
{
public struct PBRLight
{
public Vector3 position;
public Vector3 color;
public PBRLight(Vector3 position, Vector3 colour)
{
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);
}
}
}
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;
EffectParameter shaderIndexParam;
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;
EffectDirtyFlags dirtyFlags= EffectDirtyFlags.All;
// FIXME: lazily set properties for performance
public Matrix World
{
get { return world; }
set
{
world = value;
dirtyFlags |= EffectDirtyFlags.World | EffectDirtyFlags.WorldViewProj;
}
}
public Matrix View
{
get { return view; }
set
{
view = value;
dirtyFlags |= EffectDirtyFlags.WorldViewProj | EffectDirtyFlags.EyePosition;
}
}
public Matrix Projection
{
get { return projection; }
set
{
projection = value;
dirtyFlags |= EffectDirtyFlags.WorldViewProj;
}
}
public PBRLightCollection Lights
{
get { return pbrLightCollection; }
internal set { pbrLightCollection = value; }
}
public Vector3 Albedo
{
get { return albedo; }
set
{
albedo = value;
albedoParam.SetValue(albedo);
}
}
public float Metallic
{
get { return metallic; }
set
{
metallic = value;
metallicParam.SetValue(metallic);
}
}
public float Roughness
{
get { return roughness; }
set
{
roughness = value;
roughnessParam.SetValue(roughness);
}
}
public float AO
{
get { return ao; }
set
{
ao = value;
aoParam.SetValue(ao);
}
}
public Texture2D AlbedoTexture
{
get { return albedoTextureParam.GetValueTexture2D(); }
set
{
albedoTextureParam.SetValue(value);
albedoTextureEnabled = value != null;
dirtyFlags |= EffectDirtyFlags.ShaderIndex;
}
}
public Texture2D NormalTexture
{
get { return normalTextureParam.GetValueTexture2D(); }
set
{
normalTextureParam.SetValue(value);
normalMapEnabled = value != null;
dirtyFlags |= EffectDirtyFlags.ShaderIndex;
}
}
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);
metallicRoughnessMapEnabled = value != null;
dirtyFlags |= EffectDirtyFlags.ShaderIndex;
}
}
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) : base(graphicsDevice, Resources.PBREffect)
{
CacheEffectParameters(null);
pbrLightCollection = new PBRLightCollection(
Parameters["LightPositions"],
Parameters["LightColors"]
);
}
protected PBREffect(PBREffect cloneSource) : base(cloneSource)
{
CacheEffectParameters(cloneSource);
World = cloneSource.World;
View = cloneSource.View;
Projection = cloneSource.Projection;
Lights = new PBRLightCollection(
Parameters["LightPositions"],
Parameters["LightColors"]
);
for (int i = 0; i < 4; i++)
{
Lights[i] = cloneSource.Lights[i];
}
AlbedoTexture = cloneSource.AlbedoTexture;
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;
}
public override Effect Clone()
{
return new PBREffect(this);
}
protected override void OnApply()
{
if ((dirtyFlags & EffectDirtyFlags.World) != 0)
{
worldParam.SetValue(world);
Matrix.Invert(ref world, out Matrix worldInverse);
Matrix.Transpose(ref worldInverse, out Matrix worldInverseTranspose);
worldInverseTransposeParam.SetValue(worldInverseTranspose);
dirtyFlags &= ~EffectDirtyFlags.World;
}
if ((dirtyFlags & EffectDirtyFlags.WorldViewProj) != 0)
{
Matrix.Multiply(ref world, ref view, out Matrix worldView);
Matrix.Multiply(ref worldView, ref projection, out Matrix worldViewProj);
worldViewProjectionParam.SetValue(worldViewProj);
dirtyFlags &= ~EffectDirtyFlags.WorldViewProj;
}
if ((dirtyFlags & EffectDirtyFlags.EyePosition) != 0)
{
Matrix.Invert(ref view, out Matrix inverseView);
eyePositionParam.SetValue(inverseView.Translation);
dirtyFlags &= ~EffectDirtyFlags.EyePosition;
}
if ((dirtyFlags & EffectDirtyFlags.ShaderIndex) != 0)
{
int shaderIndex = 0;
if (albedoTextureEnabled && metallicRoughnessMapEnabled && normalMapEnabled)
{
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;
}
shaderIndexParam.SetValue(shaderIndex);
dirtyFlags &= ~EffectDirtyFlags.ShaderIndex;
}
}
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"];
}
}
}

View File

@ -1,313 +0,0 @@
#include "Macros.fxh" //from FNA
static const float PI = 3.141592653589793;
// Samplers
DECLARE_TEXTURE(AlbedoTexture, 0);
DECLARE_TEXTURE(NormalTexture, 1);
DECLARE_TEXTURE(EmissionTexture, 2);
DECLARE_TEXTURE(OcclusionTexture, 3);
DECLARE_TEXTURE(MetallicRoughnessTexture, 4);
DECLARE_CUBEMAP(EnvDiffuseTexture, 8);
DECLARE_TEXTURE(BrdfLutTexture, 9);
DECLARE_CUBEMAP(EnvSpecularTexture, 10);
BEGIN_CONSTANTS
// PBR Values
float3 AlbedoValue _ps(c0) _cb(c0);
float MetallicValue _ps(c1) _cb(c1);
float RoughnessValue _ps(c2) _cb(c2);
float AO _ps(c3) _cb(c3);
// Light Info
float3 LightPositions[4] _ps(c4) _cb(c4);
float3 LightColors[4] _ps(c8) _cb(c8);
float3 EyePosition _ps(c12) _cb(c12);
float4x4 World _vs(c0) _cb(c16);
float4x4 WorldInverseTranspose _vs(c4) _cb(c20);
MATRIX_CONSTANTS
float4x4 WorldViewProjection _vs(c8) _cb(c0);
END_CONSTANTS
struct VertexShaderInput
{
float4 Position : POSITION;
float3 Normal : NORMAL;
float2 TexCoord : TEXCOORD0;
};
struct PixelShaderInput
{
float4 Position : SV_Position;
float2 TexCoord : TEXCOORD0;
float3 PositionWS : TEXCOORD1;
float3 NormalWS : TEXCOORD2;
};
PixelShaderInput main_vs(VertexShaderInput input)
{
PixelShaderInput output;
output.TexCoord = input.TexCoord;
output.PositionWS = mul(input.Position, World).xyz;
output.NormalWS = mul(input.Normal, (float3x3)WorldInverseTranspose).xyz;
output.Position = mul(input.Position, WorldViewProjection);
return output;
}
float3 FresnelSchlick(float cosTheta, float3 F0)
{
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
}
float DistributionGGX(float3 N, float3 H, float roughness)
{
float a = roughness * roughness;
float a2 = a * a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH * NdotH;
float num = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return num / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r * r) / 8.0;
float num = NdotV;
float denom = NdotV * (1.0 - k) + k;
return num / denom;
}
float GeometrySmith(float3 N, float3 V, float3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
// Easy trick to get tangent-normals to world-space to keep PBR code simplified.
float3 GetNormalFromMap(float3 worldPos, float2 texCoords, float3 normal)
{
float3 tangentNormal = SAMPLE_TEXTURE(NormalTexture, texCoords).xyz * 2.0 - 1.0;
float3 Q1 = ddx(worldPos);
float3 Q2 = ddy(worldPos);
float2 st1 = ddx(texCoords);
float2 st2 = ddy(texCoords);
float3 N = normalize(normal);
float3 T = normalize(Q1*st2.y - Q2*st1.y);
float3 B = -normalize(cross(N, T));
float3x3 TBN = float3x3(T, B, N);
return normalize(mul(tangentNormal, TBN));
}
float4 ComputeColor(
float3 worldPosition,
float3 worldNormal,
float3 albedo,
float metallic,
float roughness
) {
float3 V = normalize(EyePosition - worldPosition);
float3 N = worldNormal;
float3 F0 = float3(0.04, 0.04, 0.04);
F0 = lerp(F0, albedo, metallic);
float3 Lo = float3(0.0, 0.0, 0.0);
for (int i = 0; i < 4; i++)
{
float3 lightDir = LightPositions[i] - worldPosition;
float3 L = normalize(lightDir);
float3 H = normalize(V + L);
float distance = length(lightDir);
float attenuation = 1.0 / (distance * distance);
float3 radiance = LightColors[i] * attenuation;
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
float3 F = FresnelSchlick(max(dot(H, V), 0.0), F0);
float3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
float3 specular = numerator / max(denominator, 0.001);
float3 kS = F;
float3 kD = float3(1.0, 1.0, 1.0) - kS;
kD *= 1.0 - metallic;
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
}
float3 ambient = float3(0.03, 0.03, 0.03) * albedo * AO;
float3 color = ambient + Lo;
color = color / (color + float3(1.0, 1.0, 1.0));
float exposureConstant = 1.0 / 2.2;
color = pow(color, float3(exposureConstant, exposureConstant, exposureConstant));
return float4(color, 1.0);
}
// The case where we have no texture maps for any PBR data
float4 None(PixelShaderInput input) : SV_TARGET0
{
return ComputeColor(
input.PositionWS,
input.NormalWS,
AlbedoValue,
MetallicValue,
RoughnessValue
);
}
float4 AlbedoMapPS(PixelShaderInput input) : SV_TARGET
{
float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
return ComputeColor(
input.PositionWS,
input.NormalWS,
albedo,
MetallicValue,
RoughnessValue
);
}
float4 MetallicRoughnessPS(PixelShaderInput input) : SV_TARGET
{
float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg;
return ComputeColor(
input.PositionWS,
input.NormalWS,
AlbedoValue,
metallicRoughness.r,
metallicRoughness.g
);
}
float4 NormalPS(PixelShaderInput input) : SV_TARGET
{
float3 normal = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS);
return ComputeColor(
input.PositionWS,
normal,
AlbedoValue,
MetallicValue,
RoughnessValue
);
}
float4 AlbedoMetallicRoughnessMapPS(PixelShaderInput input) : SV_TARGET
{
float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg;
return ComputeColor(
input.PositionWS,
input.NormalWS,
albedo,
metallicRoughness.r,
metallicRoughness.g
);
}
float4 AlbedoNormalPS(PixelShaderInput input) : SV_TARGET
{
float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
float3 normal = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS);
return ComputeColor(
input.PositionWS,
normal,
albedo,
MetallicValue,
RoughnessValue
);
}
float4 MetallicRoughnessNormalPS(PixelShaderInput input) : SV_TARGET
{
float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg;
float3 normal = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS);
return ComputeColor(
input.PositionWS,
normal,
AlbedoValue,
metallicRoughness.r,
metallicRoughness.g
);
}
float4 AlbedoMetallicRoughnessNormalMapPS(PixelShaderInput input) : SV_TARGET
{
float3 albedo = pow(SAMPLE_TEXTURE(AlbedoTexture, input.TexCoord), 2.2).rgb;
float2 metallicRoughness = SAMPLE_TEXTURE(MetallicRoughnessTexture, input.TexCoord).rg;
float3 normal = GetNormalFromMap(input.PositionWS, input.TexCoord, input.NormalWS);
return ComputeColor(
input.PositionWS,
normal,
albedo,
metallicRoughness.r,
metallicRoughness.g
);
}
PixelShader PSArray[8] =
{
compile ps_3_0 None(),
compile ps_3_0 AlbedoMapPS(),
compile ps_3_0 MetallicRoughnessPS(),
compile ps_3_0 NormalPS(),
compile ps_3_0 AlbedoMetallicRoughnessMapPS(),
compile ps_3_0 AlbedoNormalPS(),
compile ps_3_0 MetallicRoughnessNormalPS(),
compile ps_3_0 AlbedoMetallicRoughnessNormalMapPS()
};
int PSIndices[8] =
{
0, 1, 2, 3, 4, 5, 6, 7
};
int ShaderIndex = 0;
Technique PBR
{
Pass
{
VertexShader = compile vs_3_0 main_vs();
PixelShader = (PSArray[PSIndices[ShaderIndex]]);
}
}

Binary file not shown.

View File

@ -7,18 +7,18 @@ namespace Smuggler
{ {
public static class Importer public static class Importer
{ {
public static Model ImportGLB(GraphicsDevice graphicsDevice, Stream stream) public static ModelData ImportGLB(GraphicsDevice graphicsDevice, Stream stream)
{ {
var sharpModel = SharpGLTF.Schema2.ModelRoot.ReadGLB(stream); var sharpModel = SharpGLTF.Schema2.ModelRoot.ReadGLB(stream);
var vertexType = InferVertexType(sharpModel); var vertexType = InferVertexType(sharpModel);
if (vertexType == null) { return null; } if (vertexType == null) { return null; }
var meshes = new List<Mesh>(); var meshes = new List<MeshData>();
foreach (var mesh in sharpModel.LogicalMeshes) foreach (var mesh in sharpModel.LogicalMeshes)
{ {
var meshParts = new List<MeshPart>(); var meshParts = new List<MeshPartData>();
foreach (var primitive in mesh.Primitives) foreach (var primitive in mesh.Primitives)
{ {
@ -27,10 +27,10 @@ namespace Smuggler
); );
} }
meshes.Add(new Mesh(meshParts.ToArray())); meshes.Add(new MeshData(meshParts.ToArray()));
} }
var model = new Model(meshes.ToArray()); var model = new ModelData(meshes.ToArray());
return model; return model;
} }
@ -98,7 +98,7 @@ namespace Smuggler
return null; return null;
} }
private static MeshPart ImportMeshPart(GraphicsDevice graphicsDevice, System.Type vertexType, SharpGLTF.Schema2.MeshPrimitive primitive) private static MeshPartData ImportMeshPart(GraphicsDevice graphicsDevice, System.Type vertexType, SharpGLTF.Schema2.MeshPrimitive primitive)
{ {
var indices = Indices(primitive); var indices = Indices(primitive);
var positions = Positions(primitive); var positions = Positions(primitive);
@ -137,7 +137,12 @@ namespace Smuggler
ImportVertexPositionTexture(primitive, vertexBuffer, indices, positions); ImportVertexPositionTexture(primitive, vertexBuffer, indices, positions);
} }
var effect = new PBREffect(graphicsDevice); var meshPartData = new MeshPartData(
vertexBuffer,
indexBuffer,
positions,
triangles
);
if (primitive.Material != null) if (primitive.Material != null)
{ {
@ -146,53 +151,19 @@ namespace Smuggler
{ {
if (normalChannel.Value.Texture != null) if (normalChannel.Value.Texture != null)
{ {
effect.NormalTexture = Texture2D.FromStream( meshPartData.NormalTexture = Texture2D.FromStream(
graphicsDevice, graphicsDevice,
normalChannel.Value.Texture.PrimaryImage.Content.Open() normalChannel.Value.Texture.PrimaryImage.Content.Open()
); );
} }
} }
//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 albedoChannel = primitive.Material.FindChannel("BaseColor"); var albedoChannel = primitive.Material.FindChannel("BaseColor");
if (albedoChannel.HasValue) if (albedoChannel.HasValue)
{ {
if (albedoChannel.Value.Texture != null) if (albedoChannel.Value.Texture != null)
{ {
effect.AlbedoTexture = Texture2D.FromStream( meshPartData.AlbedoTexture = Texture2D.FromStream(
graphicsDevice, graphicsDevice,
albedoChannel.Value.Texture.PrimaryImage.Content.Open() albedoChannel.Value.Texture.PrimaryImage.Content.Open()
); );
@ -200,7 +171,7 @@ namespace Smuggler
var parameter = albedoChannel.Value.Parameter; var parameter = albedoChannel.Value.Parameter;
effect.Albedo = new Vector3( meshPartData.Albedo = new Vector3(
parameter.X, parameter.X,
parameter.Y, parameter.Y,
parameter.Z parameter.Z
@ -212,7 +183,7 @@ namespace Smuggler
{ {
if (metallicRoughnessChannel.Value.Texture != null) if (metallicRoughnessChannel.Value.Texture != null)
{ {
effect.MetallicRoughnessTexture = Texture2D.FromStream( meshPartData.MetallicRoughnessTexture = Texture2D.FromStream(
graphicsDevice, graphicsDevice,
metallicRoughnessChannel.Value.Texture.PrimaryImage.Content.Open() metallicRoughnessChannel.Value.Texture.PrimaryImage.Content.Open()
); );
@ -220,55 +191,12 @@ namespace Smuggler
var parameter = metallicRoughnessChannel.Value.Parameter; var parameter = metallicRoughnessChannel.Value.Parameter;
effect.Metallic = parameter.X; meshPartData.Metallic = parameter.X;
effect.Roughness = parameter.Y; meshPartData.Roughness = parameter.Y;
} }
} }
effect.Albedo = new Vector3(0.5f, 0, 0); return meshPartData;
effect.AO = 1f;
effect.Lights[0] = new PBRLight(
new Vector3(-10f, 10f, -5f),
new Vector3(300f, 300f, 300f)
);
effect.Lights[1] = new PBRLight(
new Vector3(10f, 10f, 5f),
new Vector3(300f, 300f, 300f)
);
effect.Lights[2] = new PBRLight(
new Vector3(-10f, -10f, 5f),
new Vector3(300f, 300f, 300f)
);
effect.Lights[3] = new PBRLight(
new Vector3(10f, -10f, 5f),
new Vector3(300f, 300f, 300f)
);
/* 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)
{
}
*/
return new MeshPart(
vertexBuffer,
indexBuffer,
positions,
triangles,
effect
);
} }
/* Attribute Getters */ /* Attribute Getters */
@ -361,7 +289,8 @@ namespace Smuggler
VertexBuffer vertexBuffer, VertexBuffer vertexBuffer,
uint[] indices, uint[] indices,
Vector3[] positions Vector3[] positions
) { )
{
var colors = Colors(primitive); var colors = Colors(primitive);
var vertices = new VertexPositionColor[positions.Length]; var vertices = new VertexPositionColor[positions.Length];
@ -385,7 +314,8 @@ namespace Smuggler
VertexBuffer vertexBuffer, VertexBuffer vertexBuffer,
uint[] indices, uint[] indices,
Vector3[] positions Vector3[] positions
) { )
{
var colors = Colors(primitive); var colors = Colors(primitive);
var texcoords = TexCoords(primitive); var texcoords = TexCoords(primitive);
@ -412,7 +342,8 @@ namespace Smuggler
VertexBuffer vertexBuffer, VertexBuffer vertexBuffer,
uint[] indices, uint[] indices,
Vector3[] positions Vector3[] positions
) { )
{
var normals = Normals(primitive); var normals = Normals(primitive);
var texcoords = TexCoords(primitive); var texcoords = TexCoords(primitive);
@ -439,7 +370,8 @@ namespace Smuggler
VertexBuffer vertexBuffer, VertexBuffer vertexBuffer,
uint[] indices, uint[] indices,
Vector3[] positions Vector3[] positions
) { )
{
var texcoords = TexCoords(primitive); var texcoords = TexCoords(primitive);
var vertices = new VertexPositionTexture[positions.Length]; var vertices = new VertexPositionTexture[positions.Length];

23
Mesh.cs
View File

@ -1,23 +0,0 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Smuggler
{
public class Mesh
{
public MeshPart[] MeshParts { get; }
public Mesh(MeshPart[] meshParts)
{
MeshParts = meshParts;
}
public void Draw(GraphicsDevice graphicsDevice, Matrix world, Matrix view, Matrix projection)
{
foreach (var meshPart in MeshParts)
{
meshPart.Draw(graphicsDevice, world, view, projection);
}
}
}
}

12
MeshData.cs Normal file
View File

@ -0,0 +1,12 @@
namespace Smuggler
{
public class MeshData
{
public MeshPartData[] MeshParts { get; }
public MeshData(MeshPartData[] meshParts)
{
MeshParts = meshParts;
}
}
}

View File

@ -1,47 +0,0 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Smuggler
{
public class MeshPart
{
public IndexBuffer IndexBuffer { get; }
public VertexBuffer VertexBuffer { get; }
public Triangle[] Triangles { get; }
public PBREffect Effect { get; }
public Vector3[] Positions { get; }
public MeshPart(VertexBuffer vertexBuffer, IndexBuffer indexBuffer, Vector3[] positions, Triangle[] triangles, PBREffect effect)
{
VertexBuffer = vertexBuffer;
IndexBuffer = indexBuffer;
Positions = positions;
Triangles = triangles;
Effect = effect;
}
public void Draw(GraphicsDevice graphicsDevice, Matrix world, Matrix view, Matrix projection)
{
graphicsDevice.SetVertexBuffer(VertexBuffer);
graphicsDevice.Indices = IndexBuffer;
Effect.World = world;
Effect.View = view;
Effect.Projection = projection;
foreach (var pass in Effect.CurrentTechnique.Passes)
{
pass.Apply();
graphicsDevice.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
0,
0,
VertexBuffer.VertexCount,
0,
Triangles.Length
);
}
}
}
}

34
MeshPartData.cs Normal file
View File

@ -0,0 +1,34 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Smuggler
{
public class MeshPartData
{
public IndexBuffer IndexBuffer { get; }
public VertexBuffer VertexBuffer { get; }
public Triangle[] Triangles { get; }
public Vector3[] Positions { get; }
public Texture2D AlbedoTexture { get; set; } = null;
public Texture2D NormalTexture { get; set; } = null;
public Texture2D MetallicRoughnessTexture { get; set; } = null;
public Vector3 Albedo { get; set; } = Vector3.One;
public float Metallic { get; set; } = 0.5f;
public float Roughness { get; set; } = 0.5f;
public MeshPartData(
VertexBuffer vertexBuffer,
IndexBuffer indexBuffer,
Vector3[] positions,
Triangle[] triangles
)
{
VertexBuffer = vertexBuffer;
IndexBuffer = indexBuffer;
Positions = positions;
Triangles = triangles;
}
}
}

View File

@ -1,24 +0,0 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Smuggler
{
public class Model
{
public Mesh[] Meshes { get; }
public Texture2D[] Textures { get; }
public Model(Mesh[] meshes)
{
Meshes = meshes;
}
public void Draw(GraphicsDevice graphicsDevice, Matrix world, Matrix view, Matrix projection)
{
foreach (var mesh in Meshes)
{
mesh.Draw(graphicsDevice, world, view, projection);
}
}
}
}

15
ModelData.cs Normal file
View File

@ -0,0 +1,15 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Smuggler
{
public class ModelData
{
public MeshData[] Meshes { get; }
public ModelData(MeshData[] meshes)
{
Meshes = meshes;
}
}
}

View File

@ -1,33 +0,0 @@
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

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