Skip to content

Commit

Permalink
Accidentally staged half of the commits
Browse files Browse the repository at this point in the history
  • Loading branch information
Derpius committed Sep 16, 2022
1 parent 90429d9 commit bece682
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
26 changes: 26 additions & 0 deletions source/VisTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,30 @@ LUA_FUNCTION(Material_Roughness)
return 0;
}

LUA_FUNCTION(Material_Anisotropy)
{
LUA->CheckType(1, BSDFMaterial::id);
LUA->CheckType(2, Type::Number);

BSDFMaterial* pMat = LUA->GetUserType<BSDFMaterial>(1, BSDFMaterial::id);
// While anisotropy can be mapped -1 to 1
// with rotation it makes more sense to have it 0-1 (easily stored in a texture) and use rotation to change the direction
// This is also how blender's principled BSDF node does it
pMat->anisotropy = glm::clamp(LUA->GetNumber(2), 0., 1. - kMinGGXAlpha);
pMat->anisotropyOverriden = true;
return 0;
}
LUA_FUNCTION(Material_AnisotropicRotation)
{
LUA->CheckType(1, BSDFMaterial::id);
LUA->CheckType(2, Type::Number);

BSDFMaterial* pMat = LUA->GetUserType<BSDFMaterial>(1, BSDFMaterial::id);
pMat->anisotropicRotation = glm::clamp(LUA->GetNumber(2), 0., 1.) * 2. * glm::pi<double>();
pMat->anisotropicRotationOverriden = true;
return 0;
}

LUA_FUNCTION(Material_IoR)
{
LUA->CheckType(1, BSDFMaterial::id);
Expand Down Expand Up @@ -1516,6 +1540,8 @@ GMOD_MODULE_OPEN()

PUSH_C_FUNC(Material, Metalness);
PUSH_C_FUNC(Material, Roughness);
PUSH_C_FUNC(Material, Anisotropy);
PUSH_C_FUNC(Material, AnisotropicRotation);

PUSH_C_FUNC(Material, IoR);

Expand Down
31 changes: 15 additions & 16 deletions source/libraries/BSDF.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "BSDF.h"

#include "glm/ext/scalar_constants.hpp"
#include "glm/gtx/rotate_vector.hpp"

using namespace glm;
using namespace VisTrace;

static const float kMinGGXAlpha = 0.0064f;

int BSDFMaterial::id = -1;

void BSDFMaterial::PrepShadingData(const vec3& hitColour, float hitMetalness, float hitRoughness)
Expand Down Expand Up @@ -801,13 +800,13 @@ bool SampleSpecularTransmission(
}
#pragma endregion

inline vec3 to_local(const vec3& T, const vec3& B, const vec3& N, const vec3& v)
inline vec3 to_local(const BSDFMaterial& data, const vec3& T, const vec3& B, const vec3& N, const vec3& v)
{
return vec3(dot(v, T), dot(v, B), dot(v, N));
return vec3(dot(v, rotate(T, data.anisotropicRotation, N)), dot(v, rotate(B, data.anisotropicRotation, N)), dot(v, N));
}
inline vec3 from_local(const vec3& T, const vec3& B, const vec3& N, const vec3& v)
inline vec3 from_local(const BSDFMaterial& data, const vec3& T, const vec3& B, const vec3& N, const vec3& v)
{
return T * v.x + B * v.y + N * v.z;
return rotate(T, data.anisotropicRotation, N) * v.x + rotate(B, data.anisotropicRotation, N) * v.y + N * v.z;
}

bool SampleBSDF(
Expand All @@ -823,7 +822,7 @@ bool SampleBSDF(
float lobeSelect = sg->GetFloat();

const bool entering = dot(incidentWorld, normal) >= 0.f;
const vec3 incident = entering ? to_local(tangent, binormal, normal, incidentWorld) : to_local(-tangent, -binormal, -normal, incidentWorld);
const vec3 incident = entering ? to_local(data, tangent, binormal, normal, incidentWorld) : to_local(data, -tangent, -binormal, -normal, incidentWorld);

if (lobeSelect < pDiffuse) {
if (!SampleDiffuse(data, incident, sg, result.lobe, result.scattered, result.weight, result.pdf)) return false;
Expand Down Expand Up @@ -863,7 +862,7 @@ bool SampleBSDF(
if (pSpecTrans > 0.f) result.pdf += pSpecTrans * EvalSpecularTransmissionPDF(data, incident, result.scattered, entering);
}

result.scattered = entering ? from_local(tangent, binormal, normal, result.scattered) : from_local(-tangent, -binormal, -normal, result.scattered);
result.scattered = entering ? from_local(data, tangent, binormal, normal, result.scattered) : from_local(data, -tangent, -binormal, -normal, result.scattered);
return true;
}

Expand All @@ -878,11 +877,11 @@ vec3 EvalBSDF(

const bool entering = dot(incidentWorld, normal) >= 0.f;
const vec3 incident = entering ?
to_local(tangent, binormal, normal, incidentWorld) :
to_local(-tangent, -binormal, -normal, incidentWorld);
to_local(data, tangent, binormal, normal, incidentWorld) :
to_local(data, -tangent, -binormal, -normal, incidentWorld);
const vec3 scattered = entering ?
to_local(tangent, binormal, normal, scatteredWorld) :
to_local(-tangent, -binormal, -normal, scatteredWorld);
to_local(data, tangent, binormal, normal, scatteredWorld) :
to_local(data, -tangent, -binormal, -normal, scatteredWorld);

vec3 result{ 0, 0, 0 };
if (pDiffuse > 0.f)
Expand All @@ -909,11 +908,11 @@ float EvalPDF(

const bool entering = dot(incidentWorld, normal) >= 0.f;
const vec3 incident = entering ?
to_local(tangent, binormal, normal, incidentWorld) :
to_local(-tangent, -binormal, -normal, incidentWorld);
to_local(data, tangent, binormal, normal, incidentWorld) :
to_local(data, -tangent, -binormal, -normal, incidentWorld);
const vec3 scattered = entering ?
to_local(tangent, binormal, normal, scatteredWorld) :
to_local(-tangent, -binormal, -normal, scatteredWorld);
to_local(data, tangent, binormal, normal, scatteredWorld) :
to_local(data, -tangent, -binormal, -normal, scatteredWorld);

float pdf = 0.f;
if (pDiffuse > 0.f)
Expand Down
4 changes: 4 additions & 0 deletions source/libraries/BSDF.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "vistrace/ISampler.h"

constexpr float kMinGGXAlpha = 0.0064f;

// Most significant bit of each nibble reserved
enum class LobeType : uint8_t
{
Expand Down Expand Up @@ -78,6 +80,8 @@ struct BSDFMaterial

bool anisotropyOverriden = false;
float anisotropy = 0.f;
bool anisotropicRotationOverriden = false;
float anisotropicRotation = 0.f;

float diffuseTransmission = 0.f;
float specularTransmission = 0.f;
Expand Down

1 comment on commit bece682

@Derpius
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes*

Please sign in to comment.