2012-12-15 10:18:42 +00:00
|
|
|
/*
|
|
|
|
* Adapted from Open Shading Language with this license:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Modifications Copyright 2011, Blender Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Sony Pictures Imageworks nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2013-05-16 15:47:09 +00:00
|
|
|
#ifndef __BSDF_UTIL_H__
|
|
|
|
#define __BSDF_UTIL_H__
|
2012-12-15 10:18:42 +00:00
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2014-05-04 16:19:08 +00:00
|
|
|
ccl_device float fresnel_dielectric(
|
|
|
|
float eta, const float3 N,
|
|
|
|
const float3 I, float3 *R, float3 *T,
|
2012-12-15 10:18:42 +00:00
|
|
|
#ifdef __RAY_DIFFERENTIALS__
|
2014-05-04 16:19:08 +00:00
|
|
|
const float3 dIdx, const float3 dIdy,
|
|
|
|
float3 *dRdx, float3 *dRdy,
|
|
|
|
float3 *dTdx, float3 *dTdy,
|
2012-12-15 10:18:42 +00:00
|
|
|
#endif
|
2014-05-04 16:19:08 +00:00
|
|
|
bool *is_inside)
|
2012-12-15 10:18:42 +00:00
|
|
|
{
|
|
|
|
float cos = dot(N, I), neta;
|
|
|
|
float3 Nn;
|
2013-05-26 22:34:45 +00:00
|
|
|
|
2012-12-15 10:18:42 +00:00
|
|
|
// check which side of the surface we are on
|
|
|
|
if(cos > 0) {
|
|
|
|
// we are on the outside of the surface, going in
|
|
|
|
neta = 1 / eta;
|
|
|
|
Nn = N;
|
|
|
|
*is_inside = false;
|
|
|
|
}
|
|
|
|
else {
|
2013-05-26 22:34:45 +00:00
|
|
|
// we are inside the surface
|
2012-12-15 10:18:42 +00:00
|
|
|
cos = -cos;
|
|
|
|
neta = eta;
|
|
|
|
Nn = -N;
|
|
|
|
*is_inside = true;
|
|
|
|
}
|
2013-05-26 22:34:45 +00:00
|
|
|
|
|
|
|
// compute reflection
|
2012-12-15 10:18:42 +00:00
|
|
|
*R = (2 * cos)* Nn - I;
|
2013-05-26 22:34:45 +00:00
|
|
|
#ifdef __RAY_DIFFERENTIALS__
|
|
|
|
*dRdx = (2 * dot(Nn, dIdx)) * Nn - dIdx;
|
|
|
|
*dRdy = (2 * dot(Nn, dIdy)) * Nn - dIdy;
|
|
|
|
#endif
|
|
|
|
|
2012-12-15 10:18:42 +00:00
|
|
|
float arg = 1 -(neta * neta *(1 -(cos * cos)));
|
|
|
|
if(arg < 0) {
|
|
|
|
*T = make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
#ifdef __RAY_DIFFERENTIALS__
|
|
|
|
*dTdx = make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
*dTdy = make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
#endif
|
|
|
|
return 1; // total internal reflection
|
|
|
|
}
|
|
|
|
else {
|
2016-07-16 18:48:12 +00:00
|
|
|
float dnp = max(sqrtf(arg), 1e-7f);
|
2012-12-15 10:18:42 +00:00
|
|
|
float nK = (neta * cos)- dnp;
|
|
|
|
*T = -(neta * I)+(nK * Nn);
|
|
|
|
#ifdef __RAY_DIFFERENTIALS__
|
|
|
|
*dTdx = -(neta * dIdx) + ((neta - neta * neta * cos / dnp) * dot(dIdx, Nn)) * Nn;
|
|
|
|
*dTdy = -(neta * dIdy) + ((neta - neta * neta * cos / dnp) * dot(dIdy, Nn)) * Nn;
|
|
|
|
#endif
|
|
|
|
// compute Fresnel terms
|
|
|
|
float cosTheta1 = cos; // N.R
|
|
|
|
float cosTheta2 = -dot(Nn, *T);
|
|
|
|
float pPara = (cosTheta1 - eta * cosTheta2)/(cosTheta1 + eta * cosTheta2);
|
|
|
|
float pPerp = (eta * cosTheta1 - cosTheta2)/(eta * cosTheta1 + cosTheta2);
|
|
|
|
return 0.5f * (pPara * pPara + pPerp * pPerp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float fresnel_dielectric_cos(float cosi, float eta)
|
2012-12-15 10:18:42 +00:00
|
|
|
{
|
|
|
|
// compute fresnel reflectance without explicitly computing
|
|
|
|
// the refracted direction
|
|
|
|
float c = fabsf(cosi);
|
|
|
|
float g = eta * eta - 1 + c * c;
|
|
|
|
if(g > 0) {
|
|
|
|
g = sqrtf(g);
|
|
|
|
float A = (g - c)/(g + c);
|
|
|
|
float B = (c *(g + c)- 1)/(c *(g - c)+ 1);
|
|
|
|
return 0.5f * A * A *(1 + B * B);
|
|
|
|
}
|
|
|
|
return 1.0f; // TIR(no refracted component)
|
|
|
|
}
|
|
|
|
|
2014-09-07 16:28:59 +00:00
|
|
|
ccl_device float3 fresnel_conductor(float cosi, const float3 eta, const float3 k)
|
2012-12-15 10:18:42 +00:00
|
|
|
{
|
Cycles: Add multi-scattering, energy-conserving GGX as an option to the Glossy, Anisotropic and Glass BSDFs
This commit adds a new distribution to the Glossy, Anisotropic and Glass BSDFs that implements the
multiple-scattering microfacet model described in the paper "Multiple-Scattering Microfacet BSDFs with the Smith Model".
Essentially, the improvement is that unlike classical GGX, which only models single scattering and assumes
the contribution of multiple bounces to be zero, this new model performs a random walk on the microsurface until
the ray leaves it again, which ensures perfect energy conservation.
In practise, this means that the "darkening problem" - GGX materials becoming darker with increasing
roughness - is solved in a physically correct and efficient way.
The downside of this model is that it has no (known) analytic expression for evalation. However, it can be
evaluated stochastically, and although the correct PDF isn't known either, the properties of MIS and the
balance heuristic guarantee an unbiased result at the cost of slightly higher noise.
Reviewers: dingto, #cycles, brecht
Reviewed By: dingto, #cycles, brecht
Subscribers: bliblubli, ace_dragon, gregzaal, brecht, harvester, dingto, marcog, swerner, jtheninja, Blendify, nutel
Differential Revision: https://developer.blender.org/D2002
2016-06-23 20:56:43 +00:00
|
|
|
float3 cosi2 = make_float3(cosi*cosi, cosi*cosi, cosi*cosi);
|
2014-09-07 16:28:59 +00:00
|
|
|
float3 one = make_float3(1.0f, 1.0f, 1.0f);
|
|
|
|
float3 tmp_f = eta * eta + k * k;
|
|
|
|
float3 tmp = tmp_f * cosi2;
|
|
|
|
float3 Rparl2 = (tmp - (2.0f * eta * cosi) + one) /
|
|
|
|
(tmp + (2.0f * eta * cosi) + one);
|
|
|
|
float3 Rperp2 = (tmp_f - (2.0f * eta * cosi) + cosi2) /
|
|
|
|
(tmp_f + (2.0f * eta * cosi) + cosi2);
|
2012-12-15 10:18:42 +00:00
|
|
|
return(Rparl2 + Rperp2) * 0.5f;
|
|
|
|
}
|
|
|
|
|
2013-11-15 23:17:10 +00:00
|
|
|
ccl_device float smooth_step(float edge0, float edge1, float x)
|
2012-12-15 10:18:42 +00:00
|
|
|
{
|
|
|
|
float result;
|
|
|
|
if(x < edge0) result = 0.0f;
|
|
|
|
else if(x >= edge1) result = 1.0f;
|
|
|
|
else {
|
|
|
|
float t = (x - edge0)/(edge1 - edge0);
|
|
|
|
result = (3.0f-2.0f*t)*(t*t);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
2013-05-16 15:47:09 +00:00
|
|
|
#endif /* __BSDF_UTIL_H__ */
|
2012-12-15 10:18:42 +00:00
|
|
|
|