-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisneydiffuse.cpp
196 lines (161 loc) · 5.47 KB
/
disneydiffuse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <mitsuba/render/bsdf.h>
#include <mitsuba/render/texture.h>
#include <mitsuba/hw/basicshader.h>
#include <mitsuba/core/warp.h>
MTS_NAMESPACE_BEGIN
class DisneyDiffuse : public BSDF {
public:
DisneyDiffuse(const Properties &props)
: BSDF(props) {
m_base_color = new ConstantSpectrumTexture(
props.getSpectrum("base_color", Spectrum(0.5f)));
m_roughness = props.getFloat("roughness", 0.0f);
}
DisneyDiffuse(Stream *stream, InstanceManager *manager)
: BSDF(stream, manager) {
m_base_color = static_cast<Texture *>(manager->getInstance(stream));
m_roughness = stream -> readFloat();
configure();
}
void configure() {
m_components.clear();
m_components.push_back(EGlossyReflection | EFrontSide);
m_components.push_back(EDiffuseReflection | EFrontSide);
m_usesRayDifferentials = false;
BSDF::configure();
}
Spectrum eval(const BSDFSamplingRecord &bRec, EMeasure measure) const {
bool hasDiffuse = (bRec.typeMask & EDiffuseReflection)
&& (bRec.component == -1 || bRec.component == 1);
if ((!hasDiffuse)
|| Frame::cosTheta(bRec.wo) <= 0 || Frame::cosTheta(bRec.wi) <= 0) {
//std::cout << "no diffuse" << endl;
return Spectrum(0.0f);
}
/* eval diffusse */
Spectrum result(0.0f);
if (hasDiffuse) {
Vector H = normalize(bRec.wo + bRec.wi);
if (Frame::cosTheta(H) > 0.0f)
{
//half vector
const Vector Phi = bRec.wo + bRec.wi;
const Vector H = normalize(Phi);
const Float Hwi = dot(bRec.wi, H);
const Float Hwo = dot(bRec.wo, H);
//Fresnel Term
const Float F_D90 = 0.5f + 2.0f * m_roughness * Hwo * Hwo;
const Float Fwi = fresnel(F_D90, Frame::cosTheta(bRec.wi));
const Float Fwo = fresnel(F_D90, Frame::cosTheta(bRec.wo));
//Diffuse
result += m_base_color->eval(bRec.its) * INV_PI * Fwi * Fwo * Frame::cosTheta(bRec.wo);
}
}
return result;
}
Float pdf(const BSDFSamplingRecord &bRec, EMeasure measure) const {
if (Frame::cosTheta(bRec.wi) <= 0 ||
Frame::cosTheta(bRec.wo) <= 0 || measure != ESolidAngle)
return 0.0f;
bool hasDiffuse = (bRec.typeMask & EDiffuseReflection)
&& (bRec.component == -1 || bRec.component == 1);
Float diffuseProb = 0.0f;
//diffuse pdf
if (hasDiffuse)
diffuseProb = warp::squareToCosineHemispherePdf(bRec.wo);
if (hasDiffuse)
return diffuseProb;
//subsurface pdf
else
return 0.0f;
}
Spectrum sample(BSDFSamplingRecord &bRec, Float &pdf, const Point2 &_sample) const {
Point2 sample(_sample);
bool hasDiffuse = (bRec.typeMask & EDiffuseReflection)
&& (bRec.component == -1 || bRec.component == 1);
if (!hasDiffuse)
return Spectrum(0.0f);
//sample diffuse
bRec.wo = warp::squareToCosineHemisphere(sample);
bRec.sampledComponent = 1;
bRec.sampledType = EDiffuseReflection;
bRec.eta = 1.0f;
pdf = DisneyDiffuse::pdf(bRec, ESolidAngle);
/* unoptimized evaluation, explicit division of evaluation / pdf. */
if (pdf == 0 || Frame::cosTheta(bRec.wo) <= 0)
return Spectrum(0.0f);
else
return eval(bRec, ESolidAngle) / pdf;
}
Spectrum sample(BSDFSamplingRecord &bRec, const Point2 &sample) const {
Float pdf;
return DisneyDiffuse::sample(bRec, pdf, sample);
}
void serialize(Stream *stream, InstanceManager *manager) const {
BSDF::serialize(stream, manager);
manager->serialize(stream, m_base_color.get());
stream->writeFloat(m_roughness);
}
std::string toString() const {
std::ostringstream oss;
oss << "DisneyDiffuse[" << endl
<< " id = \"" << getID() << "\"," << endl
<< " base_color = " << indent(m_base_color->toString()) << ", " << endl
<< " roughness = " << m_roughness << ", " << endl
<< "]";
return oss.str();
}
Shader *createShader(Renderer *renderer) const;
MTS_DECLARE_CLASS()
private:
//helper method
inline Float fresnel(const Float& F_D90, const Float& c) const
{
return 1.0f + (F_D90 - 1.0f)*pow(1.0 - c, 5.0f);
}
//attributes
ref<const Texture> m_base_color;
Float m_roughness;
};
// ================ Hardware shader implementation ================
class DisneyDiffuseShader : public Shader {
public:
DisneyDiffuseShader(Renderer *renderer, const Texture *diffuseColor)
: Shader(renderer, EBSDFShader), m_base_color(diffuseColor) {
m_base_colorShader = renderer->registerShaderForResource(m_base_color.get());
m_flags = ETransparent;
}
bool isComplete() const {
return m_base_color.get() != NULL;
}
void cleanup(Renderer *renderer) {
renderer->unregisterShaderForResource(m_base_color.get());
}
void putDependencies(std::vector<Shader *> &deps) {
deps.push_back(m_base_colorShader.get());
}
void generateCode(std::ostringstream &oss,
const std::string &evalName,
const std::vector<std::string> &depNames) const {
oss << "vec3 " << evalName << "(vec2 uv, vec3 wi, vec3 wo) {" << endl
<< " if (cosTheta(wi) < 0.0 || cosTheta(wo) < 0.0)" << endl
<< " return vec3(0.0);" << endl
<< " return " << depNames[0] << "(uv) * inv_pi * cosTheta(wo);" << endl
<< "}" << endl
<< endl
<< "vec3 " << evalName << "_diffuse(vec2 uv, vec3 wi, vec3 wo) {" << endl
<< " return " << evalName << "(uv, wi, wo);" << endl
<< "}" << endl;
}
MTS_DECLARE_CLASS()
private:
ref<const Texture> m_base_color;
ref<Shader> m_base_colorShader;
};
Shader *DisneyDiffuse::createShader(Renderer *renderer) const {
return new DisneyDiffuseShader(renderer, m_base_color.get());
}
MTS_IMPLEMENT_CLASS(DisneyDiffuseShader, false, Shader)
MTS_IMPLEMENT_CLASS_S(DisneyDiffuse, false, BSDF)
MTS_EXPORT_PLUGIN(DisneyDiffuse, "Disney diffuse BRDF")
MTS_NAMESPACE_END