This repository has been archived by the owner on Mar 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
椎名深雪
committed
Nov 24, 2019
1 parent
8d6c005
commit e8f4d52
Showing
8 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include <api/math.hpp> | ||
#include <api/object.hpp> | ||
|
||
namespace miyuki::core { | ||
class Frensel : public Object { | ||
public: | ||
virtual Float evaluate(const Float cosTheta) const = 0; | ||
}; | ||
} // namespace miyuki::core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "microfacet.h" | ||
#include <api/shader.h> | ||
#include <api/trignometry.hpp> | ||
|
||
namespace miyuki::core { | ||
static float SchlickWeight(float cosTheta) { | ||
float m = std::clamp(1.0 - cosTheta, 0.0, 1.0); | ||
return (m * m) * (m * m) * m; | ||
} | ||
static float Schlick(float R0, float cosTheta) { return mix<float>(R0, 1.0, SchlickWeight(cosTheta)); } | ||
static float GGX_D(float alpha, const Vec3f &m) { | ||
if (m.y <= 0.0f) | ||
return 0.0f; | ||
float a2 = alpha * alpha; | ||
float c2 = Cos2Theta(m); | ||
float t2 = Tan2Theta(m); | ||
float at = (a2 + t2); | ||
return a2 / (Pi * c2 * c2 * at * at); | ||
} | ||
static float GGX_G1(float alpha, const Vec3f &v, const Vec3f &m) { | ||
if (v.dot(m) * v.y <= 0.0f) { | ||
return 0.0f; | ||
} | ||
return 2.0 / (1.0 + sqrt(1.0 + alpha * alpha * Tan2Theta(m))); | ||
} | ||
static float GGX_G(float alpha, const Vec3f &i, const Vec3f &o, const Vec3f &m) { | ||
return GGX_G1(alpha, i, m) * GGX_G1(alpha, o, m); | ||
} | ||
static Vec3f GGX_SampleWh(float alpha, const Vec3f &wo, const Point2f &u) { | ||
float phi = 2.0 * Pi * u.y; | ||
float t2 = alpha * alpha * u.x / (1.0 - u.x); | ||
float cosTheta = 1.0f / sqrt(1.0 + t2); | ||
float sinTheta = sqrt(std::fmax(0.0f, 1.0 - cosTheta * cosTheta)); | ||
return Vec3f(cos(phi) * sinTheta, cosTheta, sin(phi) * sinTheta); | ||
} | ||
Spectrum MicrofacetBSDF::evaluate(const ShadingPoint &point, const Vec3f &wo, const Vec3f &wi) const { | ||
if (!SameHemisphere(wo, wi)) { | ||
return Spectrum(0); | ||
} | ||
float cosThetaO = AbsCosTheta(wo); | ||
float cosThetaI = AbsCosTheta(wi); | ||
Vec3f wh = (wo + wi); | ||
if (cosThetaI == 0 || cosThetaO == 0) | ||
return Spectrum(0); | ||
if (wh.x == 0 && wh.y == 0 && wh.z == 0) | ||
return Spectrum(0); | ||
wh.normalize(); | ||
float F = 1.0; // Schlick(0.4f, abs(dot(wi, wh))); | ||
auto R = color->evaluate(point); | ||
auto alpha = roughness->evaluate(point).x; | ||
alpha *= alpha; | ||
return max(Spectrum(0), R * F * GGX_D(alpha, wh) * GGX_G(alpha, wo, wi, wh) / (4.0f * cosThetaI * cosThetaO)); | ||
} | ||
|
||
void MicrofacetBSDF::sample(Point2f u, const ShadingPoint &sp, BSDFSample &sample) const { | ||
auto alpha = roughness->evaluate(sp).x; | ||
alpha *= alpha; | ||
Normal3f wh = GGX_SampleWh(alpha, sample.wo, u); | ||
sample.wi = Reflect(sample.wo, wh); | ||
sample.f = evaluate(sp, sample.wo, sample.wi); | ||
sample.pdf = evaluatePdf(sp, sample.wo, sample.wi); | ||
} | ||
|
||
Float MicrofacetBSDF::evaluatePdf(const ShadingPoint &point, const Vec3f &wo, const Vec3f &wi) const { | ||
if (!SameHemisphere(wo, wi)) { | ||
return 0.0f; | ||
} | ||
auto wh = (wo + wi).normalized(); | ||
auto alpha = roughness->evaluate(point).x; | ||
alpha *= alpha; | ||
return GGX_D(alpha, wh) * AbsCosTheta(wh); | ||
} | ||
|
||
} // namespace miyuki::core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <api/bsdf.h> | ||
#include <api/serialize.hpp> | ||
|
||
|
||
namespace miyuki::core { | ||
class Fresnel; | ||
class Shader; | ||
|
||
/*GGX microfacet reflection*/ | ||
class MicrofacetBSDF final : public BSDF { | ||
std::shared_ptr<Shader> color, roughness; | ||
|
||
public: | ||
MYK_DECL_CLASS(MicrofacetBSDF, "MicrofacetBSDF", interface = "BSDF") | ||
|
||
MYK_AUTO_SER(color, roughness) | ||
|
||
MYK_AUTO_INIT(color, roughness) | ||
|
||
MicrofacetBSDF() = default; | ||
|
||
[[nodiscard]] Spectrum evaluate(const ShadingPoint &point, const Vec3f &wo, const Vec3f &wi) const override; | ||
|
||
void sample(Point2f u, const ShadingPoint &sp, BSDFSample &sample) const override; | ||
|
||
[[nodiscard]] Float evaluatePdf(const ShadingPoint &point, const Vec3f &wo, const Vec3f &wi) const override; | ||
|
||
[[nodiscard]] Type getBSDFType() const override { return Type(EGlossy | EReflection); } | ||
}; | ||
} // namespace miyuki::core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters