-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUsdAreaLightOp.cpp
More file actions
334 lines (287 loc) · 13.7 KB
/
Copy pathUsdAreaLightOp.cpp
File metadata and controls
334 lines (287 loc) · 13.7 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
#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"
"When using soft shadows 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");
DD::Image::Int_knob(f, &_shadowSamples, "shadow_samples", "Shadow Samples");
DD::Image::Tooltip(f, "Controls soft shadow quality by emitting a grid of sub-lights.\n\n"
"1 = single light, hard shadows (fastest)\n"
"4 = 2\xc3\x97""2 grid, basic penumbra\n"
"9 = 3\xc3\x97""3 grid, moderate softness\n"
"16 = 4\xc3\x97""4 grid, smooth penumbras (recommended)\n"
"64 = 8\xc3\x97""8 grid, very smooth (slower)\n\n"
"Render time scales linearly with this value.\n"
"Larger light surfaces naturally produce wider penumbras.");
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(s).\n"
"When Shadow Samples > 1, sub-lights are created as children\n"
"of this path (e.g. /Lights/AreaLight/sub_0_0).");
KnobDefinesGeometry(f);
DD::Image::Tab_knob(f, "About");
DD::Image::Text_knob(f, "<b><font size='5'>AreaLight</font></b>");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "<font color='#888'>Soft shadow area light for ScanlineRender2</font>");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, " ");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "v1.0");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "Created by <b>Marten Blumen</b>");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, " ");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "<b>How it works</b>");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "ScanlineRender2 only supports hard shadows from its");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "built-in light shaders. This node works around that by");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "emitting a grid of DiskLightPrims spread across the light");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "surface. Each sub-light casts its own hard shadow from a");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "slightly different position, and the overlapping penumbras");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "blend into a smooth gradient.");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, " ");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "<b>Tips</b>");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "\xe2\x80\xa2 Start with Shadow Samples = 16 for quality/speed balance");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "\xe2\x80\xa2 Shadow softness comes from light size (Width/Height)");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "\xe2\x80\xa2 Shadow Samples controls smoothness, not penumbra width");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "\xe2\x80\xa2 Increase to 64 for close-ups or final renders");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "\xe2\x80\xa2 Keep at 1 during look-dev for fast iteration");
DD::Image::SetFlags(f, DD::Image::Knob::STARTLINE);
DD::Image::Text_knob(f, "\xe2\x80\xa2 Total illumination is conserved regardless of sample count");
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 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);
}
}
}