blender/intern/cycles/kernel/osl/shaders/stdcycles.h
Weizhen Huang df6b084c49 Fix: Shader: wrong coat albedo for fractional coat weight
According to OpenPBR spec v1.1.1 Eq. (9), the correct layering for a
coated substrate is
$$f_\mathrm{layer} = f_\mathrm{coat} + T_\mathrm{coat}(1  - E_\mathrm{coat})f_\mathrm{sub}.$$
For fractional coat weight, we apply Eq. (14), and it becomes
$$
\begin{aligned}
f_\mathrm{weighted-layer}&=(1-w_\mathrm{coat})f_\mathrm{sub}+w_\mathrm{coat}f_\mathrm{layer}\\
&=(1-w_\mathrm{coat})f_\mathrm{sub}+w_\mathrm{coat}(f_\mathrm{coat} + T_\mathrm{coat}(1  - E_\mathrm{coat})f_\mathrm{sub})\\
&=w_\mathrm{coat}f_\mathrm{coat}+(1-w_\mathrm{coat}(1-T_\mathrm{coat}(1-E_\mathrm{coat})))f_\mathrm{sub}
\end{aligned}
$$
Here, \(E_\mathrm{coat}\) does not contain the coat weight nor the
overall weight of the coated closure \(w_\mathrm{layer}\), but our
current way of using `bsdf_albedo()` contains both weight, after which
we apply \(T_\mathrm{coat}\), which is wrong.

To fix this issue, we allow RGB weight for the layer operation, and
directly return a albedo of

$$
A_\mathrm{coat} = w_\mathrm{layer}w_\mathrm{coat}(1-T_\mathrm{coat}(1-E_\mathrm{coat}))
$$
Thus, when we apply the layer operation, we get
$$
\begin{aligned}
&w_\mathrm{layer}\mathrm{layer}(S_\mathrm{sub},S_\mathrm{coat},w_\mathrm{coat})\\
=&w_\mathrm{layer}(1-w_\mathrm{coat})S_\mathrm{sub}+w_\mathrm{layer}w_\mathrm{coat}\mathrm{layer}(S_\mathrm{sub},S_\mathrm{coat})\\
=&w_\mathrm{layer}(1-w_\mathrm{coat})S_\mathrm{sub}+w_\mathrm{layer}w_\mathrm{coat}S_\mathrm{coat} + (w_\mathrm{layer}w_\mathrm{coat}-A_\mathrm{coat})S_\mathrm{sub}\\
=&w_\mathrm{layer}w_\mathrm{coat}S_\mathrm{coat}+(w_\mathrm{layer}-A_\mathrm{coat})S_\mathrm{sub},
\end{aligned}
$$

Which matches the equation for \(f_\mathrm{weighted-layer}\).

A modification to the sheen BSDF was needed to make it not tint the sub
layers.

Pull Request: https://projects.blender.org/blender/blender/pulls/162471
2026-08-26 12:11:08 +02:00

102 lines
3.6 KiB
C

/* SPDX-FileCopyrightText: 2009-2010 Sony Pictures Imageworks Inc., et al. All Rights Reserved.
* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: BSD-3-Clause
*
* Adapted code from Open Shading Language. */
#ifndef CCL_STDCYCLESOSL_H
#define CCL_STDCYCLESOSL_H
#include "stdosl.h"
// Constants
#define FLT_MAX 3.402823466e+38 // max value
/* Default offset of coordinates for evaluating bump node. Unit in pixel. */
#define BUMP_FILTER_WIDTH 0.1
// Declaration of built-in functions and closures, stdosl.h does not make
// these available so we have to redefine them.
#define BUILTIN [[int builtin = 1]]
#define BUILTIN_DERIV [[ int builtin = 1, int deriv = 1 ]]
closure color diffuse_ramp(normal N, color colors[8]) BUILTIN;
closure color phong_ramp(normal N, float exponent, color colors[8]) BUILTIN;
closure color diffuse_toon(normal N, float size, float smooth) BUILTIN;
closure color glossy_toon(normal N, float size, float smooth) BUILTIN;
closure color ashikhmin_velvet(normal N, float sigma) BUILTIN;
closure color ambient_occlusion() BUILTIN;
closure color microfacet_f82_tint(
string distribution, vector N, vector T, float ax, float ay, color f0, color f82) BUILTIN;
/* Needed to pass along the color for multi-scattering saturation adjustment,
* otherwise could be replaced by microfacet() */
closure color microfacet_multi_ggx_glass(normal N, float ag, float eta, color C) BUILTIN;
closure color microfacet_multi_ggx_aniso(normal N, vector T, float ax, float ay, color C) BUILTIN;
closure color thin_glass(normal N,
vector T,
color reflection,
color transmission,
float ior,
float roughness,
float thinfilm_thickness,
float thinfilm_ior) BUILTIN;
closure color
thin_subsurface(normal N, vector T, color C, float anisotropy, float roughness) BUILTIN;
closure color coat_bsdf(color C, float ior, float roughness, normal N) BUILTIN;
// BSSRDF
closure color bssrdf(string method, normal N, vector radius, color albedo) BUILTIN;
// Hair
closure color
hair_reflection(normal N, float roughnessu, float roughnessv, vector T, float offset) BUILTIN;
closure color
hair_transmission(normal N, float roughnessu, float roughnessv, vector T, float offset) BUILTIN;
closure color hair_chiang(normal N,
color sigma,
float roughnessu,
float roughnessv,
float coat,
float alpha,
float eta) BUILTIN;
closure color hair_huang(normal N,
color sigma,
float roughness,
float tilt,
float eta,
float aspect_ratio,
float r_lobe,
float tt_lobe,
float trt_lobe) BUILTIN;
// Volume
closure color henyey_greenstein(float g) BUILTIN;
closure color fournier_forand(float B, float IOR) BUILTIN;
closure color draine(float g, float alpha) BUILTIN;
closure color rayleigh() BUILTIN;
closure color absorption() BUILTIN;
// Ray Portal
closure color ray_portal_bsdf(vector position, vector direction) BUILTIN;
point camera_shader_raster_position()
{
return P;
}
vector camera_shader_random_sample()
{
return vector(N);
}
/* Deprecated, should use `sheen_bsdf()` instead. Can remove in 6.0. */
closure color sheen(normal N, float roughness)
{
return sheen_bsdf(N, color(1.0), roughness);
}
#endif /* CCL_STDOSL_H */