Skip to content

Commit

Permalink
feat: proper metalness
Browse files Browse the repository at this point in the history
  • Loading branch information
Latios96 committed Oct 19, 2024
1 parent bacd404 commit 112274e
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 8 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#usda 1.0
(
subLayers = [
@./../_base_scene/_base_scene.usda@
]
)



over "Knob"
{
over "Looks"
{
over "MaterialUnderTestSG"
{
over "MaterialUnderTest"
{
color3f inputs:diffuseColor = (0.5,0.5,0.5)
color3f inputs:specularColor = (0.95, 0.95, 0.95)
int inputs:useSpecularWorkflow = 0
float inputs:metallic = 1
}
}
}
}
6 changes: 6 additions & 0 deletions src/crayg/integrationTests/cato.json
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@
"comparisonSettings": {
"threshold": 0.15
}
},
{
"name": "SimpleMetalMaterial",
"comparisonSettings": {
"threshold": 0.15
}
}
]
}
Expand Down
10 changes: 10 additions & 0 deletions src/crayg/src/integrators/RaytracingIntegrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ Color RaytracingIntegrator::integrate(const Ray &ray, int recursionDepth) {

Color lobesResult = Color::createBlack();
int contributingLobes = 0;

if (!lobes.metallic.weight.isBlack()) {
lobesResult += lobes.metallic.weight * integratorContext.integrateRay(lobes.metallic.sampleDirection);
contributingLobes++;
}

if (!lobes.specular.weight.isBlack()) {
lobesResult += lobes.specular.weight * integratorContext.integrateRay(lobes.specular.sampleDirection);
contributingLobes++;
}
if (!lobes.specular.weight.isBlack()) {
lobesResult += lobes.specular.weight * integratorContext.integrateRay(lobes.specular.sampleDirection);
contributingLobes++;
Expand Down
1 change: 1 addition & 0 deletions src/crayg/src/scene/shadingnetworks/materials/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct Lobe {
};

struct Lobes {
Lobe metallic;
Lobe specular;
Lobe diffuse;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,19 @@ float F_schlick(float ior, const Vector3f &view, const Vector3f &halfVector) {
}

void UsdPreviewSurface::getLobes(const SurfaceInteraction &surfaceInteraction, Lobes &lobes) {
lobes.specular.weight = getReflectivity(surfaceInteraction);
const Color evaluatedDiffuse = diffuseColor.evaluate(surfaceInteraction);
const float evaluatedMetallic = metallic.evaluate(surfaceInteraction);

if (evaluatedMetallic > 0.f) {
lobes.metallic.weight = Color::createGrey(evaluatedMetallic) * evaluatedDiffuse;
lobes.metallic.sampleDirection = surfaceInteraction.spawnReflectionRayFromSurface();
}

if (evaluatedMetallic >= 1.f) {
return;
}

lobes.specular.weight = specularColor.evaluate(surfaceInteraction) * (1.f - evaluatedMetallic);
const Vector3f halfVector =
(lobes.specular.sampleDirection.direction + surfaceInteraction.ray.direction).normalize();

Expand All @@ -54,8 +66,7 @@ void UsdPreviewSurface::getLobes(const SurfaceInteraction &surfaceInteraction, L
reflectance = F_schlick(evaluatedIor, surfaceInteraction.ray.direction, halfVector);
}

lobes.diffuse.weight =
(Color::createWhite() - lobes.specular.weight * reflectance) * diffuseColor.evaluate(surfaceInteraction);
lobes.diffuse.weight = (Color::createWhite() - lobes.specular.weight * reflectance) * evaluatedDiffuse;
if (!lobes.diffuse.weight.isBlack()) {
const Vector3f randomDirOnHemisphere = Sampling::uniformSampleHemisphere();
auto orthonormalBasis = surfaceInteraction.getOrthonormalBasis();
Expand Down
25 changes: 20 additions & 5 deletions src/crayg/tests/TestUsdPreviewSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,32 @@ TEST_CASE("UsdPreviewSurface::evaluate") {

REQUIRE(lobes.diffuse.weight == Color::createGrey(0.5f));
REQUIRE(lobes.specular.weight == Color::createBlack());
REQUIRE(lobes.metallic.weight == Color::createBlack());
}

SECTION("should return weighted reflection") {
UsdPreviewSurface usdPreviewSurface(Color::createBlack());
usdPreviewSurface.metallic = 0.5;
SECTION("should return diffuse and spec for dielectric") {
UsdPreviewSurface usdPreviewSurface(Color::createGrey(0.5f));
usdPreviewSurface.specularColor = Color::createWhite();
usdPreviewSurface.metallic = 0;

Lobes lobes;
usdPreviewSurface.getLobes(surfaceInteraction, lobes);

REQUIRE(lobes.diffuse.weight == Color::createGrey(0));
REQUIRE(lobes.specular.weight == Color::createGrey(0.5));
REQUIRE(lobes.diffuse.weight == Color::createGrey(0.48585278f));
REQUIRE(lobes.specular.weight == Color::createWhite());
REQUIRE(lobes.metallic.weight == Color::createBlack());
}

SECTION("should return only metal for conductor") {
UsdPreviewSurface usdPreviewSurface(Color::createGrey(0.5f));
usdPreviewSurface.metallic = 1;

Lobes lobes;
usdPreviewSurface.getLobes(surfaceInteraction, lobes);

REQUIRE(lobes.diffuse.weight == Color::createBlack());
REQUIRE(lobes.specular.weight == Color::createBlack());
REQUIRE(lobes.metallic.weight == Color::createGrey(0.5));
}
}

Expand Down

0 comments on commit 112274e

Please sign in to comment.