Repository navigation
Expand file tree
/
Copy pathUsdAreaLightOp.cpp
More file actions
391 lines (333 loc) · 15.6 KB
/
Copy pathUsdAreaLightOp.cpp
File metadata and controls
391 lines (333 loc) · 15.6 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "UsdAreaLightOp.h"
#include <DDImage/Knob.h>
#include <cmath>
#include <sstream>
const DD::Image::GeomOp::Description GeoAreaLightOp::description(CLASS, build);
GeoAreaLightOp::GeoAreaLightOp(Node* node)
: DD::Image::GeomOp(node, BuildEngine<Engine>())
{
}
void GeoAreaLightOp::knobs(DD::Image::Knob_Callback f)
{
GeomOp::knobs(f);
DD::Image::Tab_knob(f, "Transform");
DD::Image::Axis_knob(f, &_xform, "transform");
KnobModifiesAttribValues(f);
DD::Image::Tab_knob(f, "Area Light");
DD::Image::Float_knob(f, &_width, DD::Image::IRange(0.001, 100.0), "width", "Width");
DD::Image::Tooltip(f, "Width of the light surface in scene units.");
KnobDefinesGeometry(f);
DD::Image::Float_knob(f, &_height, DD::Image::IRange(0.001, 100.0), "height", "Height");
DD::Image::Tooltip(f, "Height of the light surface in scene units.");
KnobDefinesGeometry(f);
DD::Image::Spacer(f, 10);
DD::Image::Float_knob(f, &_intensity, DD::Image::IRange(0.0, 100.0), "intensity", "Intensity");
DD::Image::Tooltip(f, "Base brightness of the light.\n"
"In Grid mode this is divided equally across all sub-lights, "
"so total illumination stays constant regardless of Shadow Samples.");
KnobModifiesAttribValues(f);
DD::Image::Float_knob(f, &_exposure, DD::Image::IRange(-10.0, 10.0), "exposure", "Exposure");
DD::Image::Tooltip(f, "Exposure bias in stops. Final intensity = intensity * 2^exposure.");
KnobModifiesAttribValues(f);
DD::Image::Color_knob(f, _color, "color", "Color");
DD::Image::Tooltip(f, "Light color. Multiplied with intensity.");
KnobModifiesAttribValues(f);
DD::Image::Bool_knob(f, &_normalize, "normalize", "Normalize");
DD::Image::Tooltip(f, "Scale intensity inversely with light area so brightness stays "
"consistent when resizing the light.");
KnobModifiesAttribValues(f);
DD::Image::Tab_knob(f, "Shadow");
static const char* const kShadowModes[] = {"Stochastic", "Grid", nullptr};
DD::Image::Enumeration_knob(f, &_shadowMode, kShadowModes, "shadow_mode", "Shadow Mode");
DD::Image::Tooltip(f, "Stochastic (default): one USD RectLight, shaded by SoftBox's slrRectLight "
"shader. Shadow rays go to random points on the light surface, like a path "
"tracer, so the penumbra is a smooth gradient that converges with more "
"samples.\n\n"
"Grid: the v1.0/v1.1 method, a grid of hard-shadow DiskLights. The "
"penumbra is built from overlapping copies of the shadow and can band.");
KnobDefinesGeometry(f);
DD::Image::Int_knob(f, &_shadowSamples, "shadow_samples", "Shadow Samples");
DD::Image::Tooltip(f, "Stochastic: shadow rays per shading point, spread over the light in a "
"jittered grid. 1 = noisy (let ScanlineRender2 camera samples clean it "
"up), 16 = smooth preview, 64 = final quality.\n\n"
"Grid: number of sub-lights (rounded up to a square grid). Render time "
"scales linearly with it.\n\n"
"Either way, shadow softness comes from the light's Width and Height; "
"samples only control smoothness.");
KnobDefinesGeometry(f);
DD::Image::Tab_knob(f, "USD");
DD::Image::String_knob(f, &_primPath, "usd_prim_path", "Prim Path");
DD::Image::Tooltip(f, "USD stage path for the light prim.\n"
"Stochastic writes one RectLight here. Grid with more than one sample\n"
"writes DiskLight children (e.g. /Lights/AreaLight/sub_0_0).");
KnobDefinesGeometry(f);
DD::Image::Tab_knob(f, "About");
static const char* const kAbout[] = {
"<b><font size='5'>AreaLight</font></b>",
"<font color='#888'>Soft shadow area light for ScanlineRender2</font>",
" ",
"SoftBox v" SOFTBOX_VERSION,
"Created by <b>Marten Blumen</b>",
" ",
"<b>How it works</b>",
"Nuke's own lights send one shadow ray to the light's centre, so",
"ScanlineRender2 shadows are hard. In Stochastic mode this node writes",
"a USD RectLight, and SoftBox's slrRectLight shader sends each shadow",
"ray to a random point on the light's surface instead. The penumbra",
"comes out as a smooth, physically based gradient, the way a path",
"tracer renders it, from one light.",
" ",
"Grid mode keeps the v1.0 method: a grid of hard-shadow DiskLights",
"whose overlapping shadows fake the penumbra.",
" ",
"<b>Tips</b>",
"- Shadow softness comes from the light's Width and Height",
"- Shadow Samples controls smoothness, not penumbra width",
"- 16 samples for previews, 64 for finals and close-ups",
"- At low samples, raising the render's camera samples also",
" smooths the shadow (and anti-aliases everything else)",
"- Stochastic is faster than Grid for the same sample count",
nullptr};
for (const char* const* line = kAbout; *line; ++line) {
DD::Image::Text_knob(f, *line);
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
}
}
int GeoAreaLightOp::knob_changed(DD::Image::Knob* k)
{
return GeomOp::knob_changed(k);
}
void GeoAreaLightOp::build_handles(DD::Image::ViewerContext* ctx)
{
GeomOp::build_handles(ctx);
build_knob_handles(ctx);
add_draw_handle(ctx);
}
void GeoAreaLightOp::draw_handle(DD::Image::ViewerContext* ctx)
{
if (!ctx) return;
const float hw = _width * 0.5f;
const float hh = _height * 0.5f;
glPushMatrix();
glMultMatrixd(_xform.array());
// Rectangle outline
glColor3f(1.0f, 0.55f, 0.0f);
glLineWidth(1.5f);
glBegin(GL_LINE_LOOP);
glVertex3f(-hw, -hh, 0.f); glVertex3f( hw, -hh, 0.f);
glVertex3f( hw, hh, 0.f); glVertex3f(-hw, hh, 0.f);
glEnd();
// Centre crosshair
glBegin(GL_LINES);
glVertex3f(-hw, 0.f, 0.f); glVertex3f(hw, 0.f, 0.f);
glVertex3f(0.f, -hh, 0.f); glVertex3f(0.f, hh, 0.f);
glEnd();
// Direction arrow
glColor3f(1.0f, 1.0f, 0.0f);
const float arrowLen = std::max(_width, _height) * 0.6f;
const float tipZ = -arrowLen;
const float headL = arrowLen * 0.2f;
glBegin(GL_LINES);
glVertex3f(0.f, 0.f, 0.f); glVertex3f(0.f, 0.f, tipZ);
glVertex3f(0.f, 0.f, tipZ); glVertex3f( headL, 0.f, tipZ + headL);
glVertex3f(0.f, 0.f, tipZ); glVertex3f(-headL, 0.f, tipZ + headL);
glVertex3f(0.f, 0.f, tipZ); glVertex3f(0.f, headL, tipZ + headL);
glVertex3f(0.f, 0.f, tipZ); glVertex3f(0.f, -headL, tipZ + headL);
glEnd();
// Centre dot (light icon)
glColor3f(1.0f, 0.9f, 0.3f);
glPointSize(6.0f);
glBegin(GL_POINTS);
glVertex3f(0.f, 0.f, 0.f);
glEnd();
glPopMatrix();
}
// =========================================================================
// Engine
// =========================================================================
GeoAreaLightOp::Engine::Engine(ndk::GeomOpNode* parent)
: DD::Image::GeomOpEngine(parent)
, _defineTarget(usg::Token("light-define"))
, _modifyTarget(usg::Token("light-modify"))
{
assignDefineGeometryTarget(_defineTarget);
assignModifyValuesTarget(_modifyTarget);
}
void GeoAreaLightOp::Engine::processScenegraph(usg::GeomSceneContext& context)
{
DD::Image::GeomOpEngine::processScenegraph(context);
if (inputEngine0()) {
buildStageFromInput(context, 0);
mergeFromInputs(context, {0});
}
if (context.doGeometryProcessing()) {
writeLightPrims(context);
}
}
void GeoAreaLightOp::Engine::writeLightPrims(usg::GeomSceneContext& context)
{
if (!_editLayer) return;
auto getFloat = [&](const char* name, float def) -> float {
if (DD::Image::Knob* k = firstOp()->knob(name))
return static_cast<float>(k->get_value());
return def;
};
auto getBool = [&](const char* name, bool def) -> bool {
if (DD::Image::Knob* k = firstOp()->knob(name))
return k->get_value() != 0.0;
return def;
};
const float width = getFloat("width", Defaults::WIDTH);
const float height = getFloat("height", Defaults::HEIGHT);
const float intensity = getFloat("intensity", Defaults::INTENSITY);
const float exposure = getFloat("exposure", Defaults::EXPOSURE);
const bool norm = getBool ("normalize", Defaults::NORMALIZE);
int shadowSamples = [&]() -> int {
if (DD::Image::Knob* k = firstOp()->knob("shadow_samples"))
return static_cast<int>(k->get_value());
return Defaults::SHADOW_SAMPLES;
}();
fdk::Vec3f color(1.f, 1.f, 1.f);
if (DD::Image::Knob* ck = firstOp()->knob("color")) {
color.x = static_cast<float>(ck->get_value(0));
color.y = static_cast<float>(ck->get_value(1));
color.z = static_cast<float>(ck->get_value(2));
}
// Read full transform from Axis_knob -- includes translate, rotate, scale, pivot, etc.
const GeoAreaLightOp* op = static_cast<const GeoAreaLightOp*>(firstOp());
fdk::Mat4d xform = op->_xform;
std::string primPathStr = "/Lights/AreaLight";
if (DD::Image::Knob* pk = firstOp()->knob("usd_prim_path")) {
const char* t = pk->get_text();
if (t && t[0]) primPathStr = t;
}
const int shadowMode = [&]() -> int {
if (DD::Image::Knob* k = firstOp()->knob("shadow_mode"))
return static_cast<int>(k->get_value());
return Defaults::SHADOW_MODE;
}();
if (shadowMode == 0) {
auto slash = primPathStr.rfind('/');
if (slash != std::string::npos && slash > 0)
_editLayer->defineScopeParents(usg::Path(primPathStr.substr(0, slash)));
writeRectLight(context, usg::Path(primPathStr), width, height, intensity, exposure,
norm, color, xform, std::max(1, shadowSamples));
return;
}
const float radius = std::max(width, height) * 0.5f;
// =====================================================================
// Soft shadow grid
// =====================================================================
if (shadowSamples < 1) shadowSamples = 1;
const int gridN = std::max(1, (int)std::ceil(std::sqrt((float)shadowSamples)));
const int totalLights = gridN * gridN;
const float subIntensity = intensity / (float)totalLights;
// Extract axes from the matrix (column-major):
// col0 = local X, col1 = local Y, col2 = local Z, col3 = translation
const double* m = xform.array();
// Create parent scope prims
{
auto slash = primPathStr.rfind('/');
if (slash != std::string::npos && slash > 0) {
_editLayer->defineScopeParents(usg::Path(primPathStr.substr(0, slash)));
}
}
if (totalLights > 1) {
_editLayer->defineScopeParents(usg::Path(primPathStr));
}
for (int jj = 0; jj < gridN; ++jj) {
for (int ii = 0; ii < gridN; ++ii) {
std::string subPath;
if (totalLights == 1) {
subPath = primPathStr;
} else {
std::ostringstream oss;
oss << primPathStr << "/sub_" << ii << "_" << jj;
subPath = oss.str();
}
const usg::Path path(subPath);
// Local-space offset on the rect surface (XY plane)
const double localX = (double(ii) - double(gridN - 1) * 0.5)
* (double(width) / double(gridN));
const double localY = (double(jj) - double(gridN - 1) * 0.5)
* (double(height) / double(gridN));
// Build per-sub-light matrix: start with the full xform,
// then offset translation along local X and Y axes
fdk::Mat4d subXform = xform;
double* sm = subXform.array();
// col3 (translation) += localX * col0 + localY * col1
sm[12] += m[0] * localX + m[4] * localY;
sm[13] += m[1] * localX + m[5] * localY;
sm[14] += m[2] * localX + m[6] * localY;
// Define pass
if (context.defineGeometry()) {
usg::lux::DiskLightPrim diskLight =
usg::lux::DiskLightPrim::defineInLayer(_editLayer, path);
if (!diskLight.validate()) continue;
diskLight.createRadiusAttr();
diskLight.createColorAttr();
diskLight.createIntensityAttr();
diskLight.createExposureAttr();
diskLight.createNormalizeAttr();
usg::lux::ShadowAPI shadowApi(diskLight);
shadowApi.apply();
shadowApi.createShadowEnableAttr();
// Single matrix xformOp instead of decomposed translate/rotate/scale
diskLight.createXformOpOrderAttr();
diskLight.createIntrinsicAttr(usg::Token("xformOp:transform"), usg::Value::Matrix4d, usg::TimeVariability::Varying);
usg::TokenArray xformOrder;
xformOrder.push_back(usg::Token("xformOp:transform"));
diskLight.setXformOpOrder(xformOrder);
}
// Value pass
usg::lux::DiskLightPrim diskLight =
usg::lux::DiskLightPrim::defineInLayer(_editLayer, path);
if (!diskLight.validate()) continue;
diskLight.setRadius(radius);
diskLight.setColor(color);
diskLight.setIntensity(subIntensity);
diskLight.setExposure(exposure);
diskLight.setNormalize(norm);
usg::lux::ShadowAPI shadowApi(diskLight);
shadowApi.setShadowEnable(true);
diskLight.setAttr(usg::Token("xformOp:transform"), subXform);
}
}
}
void GeoAreaLightOp::Engine::writeRectLight(usg::GeomSceneContext& context, const usg::Path& path,
float width, float height, float intensity, float exposure,
bool normalize, const fdk::Vec3f& color,
const fdk::Mat4d& xform, int shadowSamples)
{
if (context.defineGeometry()) {
usg::lux::RectLightPrim rect = usg::lux::RectLightPrim::defineInLayer(_editLayer, path);
if (!rect.validate()) return;
rect.createWidthAttr();
rect.createHeightAttr();
rect.createColorAttr();
rect.createIntensityAttr();
rect.createExposureAttr();
rect.createNormalizeAttr();
usg::lux::ShadowAPI shadowApi(rect);
shadowApi.apply();
shadowApi.createShadowEnableAttr();
rect.createIntrinsicAttr(usg::Token("softbox:shadowSamples"), usg::Value::Int, usg::TimeVariability::Varying);
rect.createXformOpOrderAttr();
rect.createIntrinsicAttr(usg::Token("xformOp:transform"), usg::Value::Matrix4d, usg::TimeVariability::Varying);
usg::TokenArray xformOrder;
xformOrder.push_back(usg::Token("xformOp:transform"));
rect.setXformOpOrder(xformOrder);
}
usg::lux::RectLightPrim rect = usg::lux::RectLightPrim::defineInLayer(_editLayer, path);
if (!rect.validate()) return;
rect.setWidth(width);
rect.setHeight(height);
rect.setColor(color);
rect.setIntensity(intensity);
rect.setExposure(exposure);
rect.setNormalize(normalize);
usg::lux::ShadowAPI shadowApi(rect);
shadowApi.setShadowEnable(true);
rect.setAttr(usg::Token("softbox:shadowSamples"), int32_t(shadowSamples));
rect.setAttr(usg::Token("xformOp:transform"), xform);
}