topotopo 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@
2
2
  // meta.render and the CLI flags all map to RenderOptions here, so the three stay in sync.
3
3
  // Only keys that are present are set (renderGeometry's defaults apply to the rest).
4
4
  import { UNDERLAY_FITS, parseViews } from './raster.js';
5
+ import { VERTEX_COLOR_MODES } from './pbr.js';
5
6
  const num = (v, key) => {
6
7
  const n = typeof v === 'string' ? Number(v) : v;
7
8
  if (typeof n !== 'number' || !Number.isFinite(n))
@@ -63,7 +64,7 @@ export function renderArgs(a) {
63
64
  o.points = pts === 'auto' ? 'auto' : bool(pts);
64
65
  for (const [s, c] of [['point_numbers', 'pointNumbers'], ['prim_numbers', 'primNumbers'], ['label_groups', 'labelGroups'], ['label_pieces', 'labelPieces'],
65
66
  ['legend', 'legend'], ['checker', 'checker'], ['grid', 'grid'], ['annotate', 'annotate'], ['section_absolute', 'sectionAbsolute'], ['textures', 'textures'], ['view_labels', 'viewLabels'],
66
- ['transparent', 'transparent'], ['sockets', 'sockets'], ['footer', 'footer']]) {
67
+ ['transparent', 'transparent'], ['sockets', 'sockets'], ['footer', 'footer'], ['grey', 'grey']]) {
67
68
  const v = pick(s, c);
68
69
  if (v !== undefined)
69
70
  o[c] = bool(v);
@@ -90,6 +91,13 @@ export function renderArgs(a) {
90
91
  }
91
92
  if (o.underlayOpacity !== undefined && !(o.underlayOpacity >= 0 && o.underlayOpacity <= 1))
92
93
  throw new Error(`render: underlay_opacity expects 0..1, got ${o.underlayOpacity}`);
94
+ // shading pbr: how Cd enters the base colour (cd, or vertexColors in RenderOptions terms)
95
+ const cd = pick('cd', 'vertexColors');
96
+ if (cd !== undefined) {
97
+ if (!VERTEX_COLOR_MODES.includes(cd))
98
+ throw new Error(`render: cd expects ${VERTEX_COLOR_MODES.join(', ')}, got ${JSON.stringify(cd)}`);
99
+ o.vertexColors = cd;
100
+ }
93
101
  const sa = pick('section_axis', 'sectionAxis');
94
102
  if (sa !== undefined)
95
103
  o.sectionAxis = axis(sa, 'section_axis');
@@ -2,6 +2,20 @@ import type { Geometry } from '../core/geometry.ts';
2
2
  import { Image } from '../core/image.ts';
3
3
  /** External texture files read for a PBR render: path → image, or the reason it is not available. */
4
4
  export type PBRTextures = Map<string, Image | string>;
5
+ /**
6
+ * How vertex colour Cd enters the base colour (RenderOptions.vertexColors): 'auto' = the export rule (a named material
7
+ * wins, Cd colours prims without one), 'ignore' = never, 'multiply' = also multiplies a named material's base colour.
8
+ */
9
+ export type VertexColorMode = 'auto' | 'ignore' | 'multiply';
10
+ export declare const VERTEX_COLOR_MODES: VertexColorMode[];
11
+ /** Options of the PBR preview beyond exposure. */
12
+ export interface PBRSceneOptions {
13
+ vertexColors?: VertexColorMode;
14
+ /** value check: every base colour is a neutral 18% grey (base colour maps give alpha only, Cd nothing) */
15
+ grey?: boolean;
16
+ }
17
+ /** The base colour of grey renders: 18% reflectance (linear). */
18
+ export declare const GREY_ALBEDO = 0.18;
5
19
  /** The normals the exporter writes by default (60° cusp): the frame the normal map and TANGENT belong to. */
6
20
  export declare const PBR_CUSP = 60;
7
21
  /**
@@ -9,12 +23,18 @@ export declare const PBR_CUSP = 60;
9
23
  * (image volumes need no reading) through readFile, usually Host.readFile.
10
24
  */
11
25
  export declare function preparePBR(g: Geometry, readFile?: (path: string) => Promise<Uint8Array>): Promise<PBRTextures>;
26
+ /** Per-triangle alpha handling (PBRScene.triAlpha). */
27
+ export declare const ALPHA_OPAQUE = 0, ALPHA_MASK = 1, ALPHA_BLEND = 2;
12
28
  /** Per-triangle data and the per-pixel shader for one render. */
13
29
  export interface PBRScene {
14
30
  /** materials evaluated, maps used, problems: footer and notes */
15
31
  summary: string;
16
- /** shade a pixel; writes display RGB (0..1) into out */
32
+ /** shade a pixel; writes display RGB (0..1) into out[0..2] and the coverage (alpha, 1 on opaque triangles) into out[3] */
17
33
  shade(t: number, w0: number, w1: number, w2: number, dudx: number, dvdx: number, dudy: number, dvdy: number, view: number[], back: boolean, out: number[]): void;
34
+ /** per triangle: ALPHA_OPAQUE, ALPHA_MASK (discard below cutoff(t)) or ALPHA_BLEND (composited after the opaque pass) */
35
+ triAlpha: Uint8Array;
36
+ /** the MASK cutoff of triangle t's material */
37
+ cutoff(t: number): number;
18
38
  /** uv of triangle t at (perspective-correct) weights, for the derivatives */
19
39
  uvAt(t: number, w0: number, w1: number, w2: number, out: number[]): void;
20
40
  hasUV: boolean;
@@ -25,4 +45,4 @@ export interface PBRScene {
25
45
  * Build the shading data for g's triangles (tris / triPrim from topology.triangulate). Synchronous: MikkTSpace must be
26
46
  * loaded (preparePBR or loadMikkTSpace) and external textures read into `files`; notes receive what is not evaluated.
27
47
  */
28
- export declare function pbrScene(g: Geometry, tris: Uint32Array, triPrim: Uint32Array, files: PBRTextures | undefined, exposure: number, notes: string[]): PBRScene;
48
+ export declare function pbrScene(g: Geometry, tris: Uint32Array, triPrim: Uint32Array, files: PBRTextures | undefined, exposure: number, notes: string[], opts?: PBRSceneOptions): PBRScene;
@@ -1,10 +1,13 @@
1
1
  import { Image, channelsOf, imageRef, referenceableImages, resizeLayer } from '../core/image.js';
2
- import { DEFAULT_MATERIAL, mergeMaterialTables, primMaterials, readMaterials } from '../core/materials.js';
2
+ import { DEFAULT_MATERIAL, materialAlphaMode, mergeMaterialTables, primMaterials, readMaterials } from '../core/materials.js';
3
3
  import { cornerTangents, loadMikkTSpace, mikkTSpaceReady, shadingFrame, vertexUV } from '../core/tangents.js';
4
4
  import { imageRowOfV } from '../core/texcoord.js';
5
5
  import { resolveVertexNormals } from '../core/topology.js';
6
6
  import { decodePNG } from '../io/png-decode.js';
7
7
  import { imageFromPNG } from '../io/textures.js';
8
+ export const VERTEX_COLOR_MODES = ['auto', 'ignore', 'multiply'];
9
+ /** The base colour of grey renders: 18% reflectance (linear). */
10
+ export const GREY_ALBEDO = 0.18;
8
11
  /** The normals the exporter writes by default (60° cusp): the frame the normal map and TANGENT belong to. */
9
12
  export const PBR_CUSP = 60;
10
13
  /**
@@ -85,6 +88,8 @@ function sampleTri(chain, u, v, lod, out) {
85
88
  if (f > 0 && l0 + 1 < chain.length)
86
89
  bilinear(chain[l0 + 1], u, v, out, f);
87
90
  }
91
+ /** Per-triangle alpha handling (PBRScene.triAlpha). */
92
+ export const ALPHA_OPAQUE = 0, ALPHA_MASK = 1, ALPHA_BLEND = 2;
88
93
  const norm = (v) => { const l = Math.hypot(v[0], v[1], v[2]) || 1; return [v[0] / l, v[1] / l, v[2] / l]; };
89
94
  // studio rig in camera space (x right, y up, z towards the viewer): key upper left, fill right, rim behind
90
95
  const RIG = [
@@ -94,12 +99,17 @@ const RIG = [
94
99
  ];
95
100
  // world-up environment for indirect light
96
101
  const SKY = [0.34, 0.37, 0.42], HORIZON = [0.27, 0.27, 0.27], GROUND = [0.08, 0.075, 0.07];
102
+ const ENV = { sky: SKY, horizon: HORIZON, ground: GROUND };
103
+ /** The same light at its luminance (Rec. 709): the neutral rig of grey renders. */
104
+ const neutral = (c) => { const y = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]; return [y, y, y]; };
105
+ const GREY_RIG = RIG.map((l) => ({ d: l.d, c: neutral(l.c) }));
106
+ const GREY_ENV = { sky: neutral(SKY), horizon: neutral(HORIZON), ground: neutral(GROUND) };
97
107
  /** Environment radiance along world direction d, widened by roughness (a cheap stand-in for a prefiltered map). */
98
- function env(dy, rough, out) {
108
+ function env(dy, rough, out, { sky, horizon, ground } = ENV) {
99
109
  const w = 0.25 + rough * 0.9;
100
110
  const t = Math.max(-1, Math.min(1, dy / w));
101
111
  for (let c = 0; c < 3; c++)
102
- out[c] = t >= 0 ? HORIZON[c] + (SKY[c] - HORIZON[c]) * t : HORIZON[c] + (GROUND[c] - HORIZON[c]) * -t;
112
+ out[c] = t >= 0 ? horizon[c] + (sky[c] - horizon[c]) * t : horizon[c] + (ground[c] - horizon[c]) * -t;
103
113
  }
104
114
  /** ACES filmic curve (Narkowicz fit). */
105
115
  const acesCurve = (x) => Math.max(0, Math.min(1, (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14)));
@@ -117,9 +127,12 @@ function tone(c) {
117
127
  * Build the shading data for g's triangles (tris / triPrim from topology.triangulate). Synchronous: MikkTSpace must be
118
128
  * loaded (preparePBR or loadMikkTSpace) and external textures read into `files`; notes receive what is not evaluated.
119
129
  */
120
- export function pbrScene(g, tris, triPrim, files, exposure, notes) {
130
+ export function pbrScene(g, tris, triPrim, files, exposure, notes, opts = {}) {
121
131
  if (!mikkTSpaceReady())
122
132
  throw new Error('shading "pbr" needs the asynchronous render entry (renderGeometryAsync): MikkTSpace is not loaded');
133
+ const vcMode = opts.vertexColors ?? 'auto', grey = !!opts.grey;
134
+ if (!VERTEX_COLOR_MODES.includes(vcMode))
135
+ throw new Error(`render: cd (vertex colours) expects ${VERTEX_COLOR_MODES.join(', ')}, got ${JSON.stringify(vcMode)}`);
123
136
  const nTri = tris.length / 3;
124
137
  const table = readMaterials(g), pm = primMaterials(g), imgs = referenceableImages(g);
125
138
  const uv = vertexUV(g, 'uv');
@@ -151,7 +164,8 @@ export function pbrScene(g, tris, triPrim, files, exposure, notes) {
151
164
  mipCache.set(key, chain);
152
165
  return chain;
153
166
  };
154
- // vertex colour as glTF COLOR_0: per material, only when Cd is not all white over its prims
167
+ // vertex colour per material name, only when Cd is not all white over its prims: replacing the base colour factor on
168
+ // prims without a named material (glTF COLOR_0), multiplying a named material's base colour with vertexColors 'multiply'
155
169
  const cdF = g.findAttrib('Cd', ['vertex', 'point', 'prim', 'detail']);
156
170
  const cdA = cdF && cdF.attr.type === 'float' ? cdF.attr : null;
157
171
  const cdIndex = (v, p) => (cdF.cls === 'vertex' ? v : cdF.cls === 'point' ? vp[v] : cdF.cls === 'prim' ? p : 0);
@@ -161,8 +175,8 @@ export function pbrScene(g, tris, triPrim, files, exposure, notes) {
161
175
  out[1] = s > 1 ? d[i + 1] : d[i];
162
176
  out[2] = s > 2 ? d[i + 2] : d[i];
163
177
  };
164
- const mats = new Map(), vcol = new Map();
165
- const transparent = [];
178
+ const mats = new Map(), vcol = new Map(); // 0 none, 1 replaces the factor, 2 multiplies
179
+ const opaqueMapAlpha = [];
166
180
  const matOf = (name) => {
167
181
  let m = mats.get(name);
168
182
  if (m)
@@ -177,16 +191,15 @@ export function pbrScene(g, tris, triPrim, files, exposure, notes) {
177
191
  emissive: src.emissive.map((e) => srgbToLinear(Math.max(0, Math.min(1, e / emax))) * emax), unlit: src.unlit,
178
192
  baseMap: mapOf(label, 'base colour', t.baseColor, true), normalMap: mapOf(label, 'normal', t.normal, false),
179
193
  ormMap: mapOf(label, 'orm', t.orm, false), roughMap: t.orm ? null : mapOf(label, 'roughness', t.roughness, false), size: [1, 1],
194
+ alpha: Math.max(0, Math.min(1, src.alpha)), cutoff: src.alphaCutoff ?? 0.5,
180
195
  };
181
196
  for (const c of [m.baseMap, m.normalMap, m.ormMap, m.roughMap])
182
197
  if (c) {
183
198
  m.size[0] = Math.max(m.size[0], c[0].w);
184
199
  m.size[1] = Math.max(m.size[1], c[0].h);
185
200
  }
186
- if (src.alpha < 1)
187
- transparent.push(`material "${label}" alpha ${src.alpha}`);
188
- else if (m.baseMap && m.baseMap[0].ch === 4 && m.baseMap[0].data.some((x, i) => i % 4 === 3 && x < 1))
189
- transparent.push(`material "${label}" base colour map alpha`);
201
+ if (materialAlphaMode(src) === 'OPAQUE' && m.baseMap && m.baseMap[0].ch === 4 && m.baseMap[0].data.some((x, i) => i % 4 === 3 && x < 1))
202
+ opaqueMapAlpha.push(label);
190
203
  mats.set(name, m);
191
204
  return m;
192
205
  };
@@ -207,15 +220,56 @@ export function pbrScene(g, tris, triPrim, files, exposure, notes) {
207
220
  cdWhite.set(name, white);
208
221
  }
209
222
  }
223
+ // vertex Alpha (glTF COLOR_0 alpha): where vertex colours may be used, per material name when it is below 1 there
224
+ const alF = g.findAttrib('Alpha', ['vertex', 'point', 'prim', 'detail']);
225
+ const alA = alF && alF.attr.type === 'float' && alF.attr.size === 1 ? alF.attr : null;
226
+ const alphaAt = (v, p) => alA.data[alF.cls === 'vertex' ? v : alF.cls === 'point' ? vp[v] : alF.cls === 'prim' ? p : 0];
227
+ const alLow = new Map();
228
+ if (alA) {
229
+ for (let t = 0; t < nTri; t++) {
230
+ const p = triPrim[t], name = pm ? pm[p] : '';
231
+ if (alLow.get(name))
232
+ continue;
233
+ let low = false;
234
+ for (let k = 0; k < 3 && !low; k++)
235
+ if (alphaAt(tris[t * 3 + k], p) < 1)
236
+ low = true;
237
+ if (low || !alLow.has(name))
238
+ alLow.set(name, low);
239
+ }
240
+ }
241
+ const triVcol = new Uint8Array(nTri), triAlpha = new Uint8Array(nTri), triVA = new Uint8Array(nTri);
242
+ const nameAlpha = new Map(); // [mode, vertex alpha used]
243
+ let cdHidden = false, alphaHidden = false;
210
244
  for (let t = 0; t < nTri; t++) {
211
245
  const name = pm ? pm[triPrim[t]] : '';
212
246
  triMat[t] = matOf(name);
213
- if (!vcol.has(name))
214
- vcol.set(name, !!cdA && cdWhite.get(name) === false);
247
+ let v = vcol.get(name), na = nameAlpha.get(name);
248
+ if (v === undefined || na === undefined) {
249
+ const painted = !!cdA && cdWhite.get(name) === false, named = !!(name && table[name]);
250
+ v = !painted || vcMode === 'ignore' ? 0 : !named ? 1 : vcMode === 'multiply' ? 2 : 0;
251
+ if (painted && named && vcMode === 'auto')
252
+ cdHidden = true;
253
+ vcol.set(name, v);
254
+ const vtx = vcMode !== 'ignore' && (!named || vcMode === 'multiply'), lowA = !!alLow.get(name);
255
+ // the mode is the material's own (vertex Alpha is data, as in export); vertex Alpha multiplies the coverage of
256
+ // mask and blend materials where vertex colours are used
257
+ const mode = materialAlphaMode((named && table[name]) || DEFAULT_MATERIAL);
258
+ if (lowA && !vtx && mode !== 'OPAQUE')
259
+ alphaHidden = true;
260
+ na = [mode === 'MASK' ? ALPHA_MASK : mode === 'BLEND' ? ALPHA_BLEND : ALPHA_OPAQUE, vtx && lowA && mode !== 'OPAQUE' ? 1 : 0];
261
+ nameAlpha.set(name, na);
262
+ }
263
+ triVcol[t] = v;
264
+ triAlpha[t] = na[0];
265
+ triVA[t] = na[1];
215
266
  }
216
- const al = g.findAttrib('Alpha', ['vertex', 'point', 'prim', 'detail']);
217
- if (al && al.attr.type === 'float' && al.attr.data.some((x) => x < 1))
218
- transparent.push('Alpha attribute below 1');
267
+ if (cdHidden)
268
+ notes.push('pbr: Cd on prims with a named material is not shown: the material decides their colour, as in glTF export (cd "multiply" tints the material with it)');
269
+ if (alphaHidden)
270
+ notes.push('pbr: the Alpha attribute is not used on prims with a named material: the material\'s alpha decides, as in glTF export (cd "multiply" uses it)');
271
+ for (const label of new Set(opaqueMapAlpha))
272
+ notes.push(`pbr: material "${label}" has a base colour map with alpha below 1 but is opaque (alpha_mode "mask" or "blend" uses that alpha)`);
219
273
  // MikkTSpace frames for normal-mapped triangles (computed once over every triangle: the frames do not depend on the
220
274
  // material, and one call keeps the soup contiguous)
221
275
  const anyNormal = [...mats.values()].some((m) => m.normalMap);
@@ -228,13 +282,13 @@ export function pbrScene(g, tris, triPrim, files, exposure, notes) {
228
282
  }
229
283
  if (!uv && [...mats.values()].some((m) => m.baseMap || m.normalMap || m.ormMap || m.roughMap))
230
284
  problems.push('the geometry has no uv attribute: texture maps are not shown');
231
- if (transparent.length)
232
- notes.push(`pbr: transparency is not evaluated, shaded opaque (${[...new Set(transparent)].join('; ')})`);
233
285
  for (const pr of new Set(problems))
234
286
  notes.push(`pbr: ${pr}`);
235
- let camL = RIG.map((l) => ({ d: l.d, c: l.c }));
287
+ // grey: the rig and environment at their luminance, so a value check carries no hue from the lights either
288
+ const rig = grey ? GREY_RIG : RIG, envC = grey ? GREY_ENV : ENV;
289
+ let camL = rig.map((l) => ({ d: l.d, c: l.c }));
236
290
  const gain = 2 ** exposure;
237
- const n = [0, 0, 0], f = new Float64Array(9), tx = [0, 0, 0, 0], base = [0, 0, 0], e = [0, 0, 0], c3 = [0, 0, 0];
291
+ const n = [0, 0, 0], f = new Float64Array(9), tx = [0, 0, 0, 0], base = [0, 0, 0], e = [0, 0, 0], c3 = [0, 0, 0], vcRGB = [0, 0, 0];
238
292
  const uvAt = (t, w0, w1, w2, out) => {
239
293
  const a = tris[t * 3] * 2, b = tris[t * 3 + 1] * 2, c = tris[t * 3 + 2] * 2;
240
294
  out[0] = uv[a] * w0 + uv[b] * w1 + uv[c] * w2;
@@ -252,31 +306,39 @@ export function pbrScene(g, tris, triPrim, files, exposure, notes) {
252
306
  if (m.roughMap)
253
307
  mapsUsed.add('roughness');
254
308
  }
255
- const summary = `${mats.size} material${mats.size === 1 ? '' : 's'}${mapsUsed.size ? `, maps ${[...mapsUsed].join('/')}` : ''}`;
309
+ const alphaUsed = [...new Set(triAlpha)].filter((x) => x !== ALPHA_OPAQUE).sort().map((x) => (x === ALPHA_MASK ? 'cutout' : 'blend'));
310
+ const summary = `${mats.size} material${mats.size === 1 ? '' : 's'}${mapsUsed.size ? `, maps ${[...mapsUsed].join('/')}` : ''}${alphaUsed.length ? `, alpha ${alphaUsed.join('/')}` : ''}`;
256
311
  return {
257
- summary, hasUV: !!uv, uvAt,
258
- camera(r, u, fw) { camL = RIG.map((l) => ({ d: [0, 1, 2].map((k) => r[k] * l.d[0] + u[k] * l.d[1] - fw[k] * l.d[2]), c: l.c })); },
312
+ summary, hasUV: !!uv, uvAt, triAlpha,
313
+ cutoff: (t) => triMat[t].cutoff,
314
+ camera(r, u, fw) { camL = rig.map((l) => ({ d: [0, 1, 2].map((k) => r[k] * l.d[0] + u[k] * l.d[1] - fw[k] * l.d[2]), c: l.c })); },
259
315
  shade(t, w0, w1, w2, dudx, dvdx, dudy, dvdy, view, back, out) {
260
316
  const m = triMat[t], a = tris[t * 3], b = tris[t * 3 + 1], c = tris[t * 3 + 2];
261
317
  if (uv)
262
318
  uvAt(t, w0, w1, w2, uvp);
263
319
  const lod = (sz) => 0.5 * Math.log2(Math.max(1e-30, Math.max((dudx * sz[0].w) ** 2 + (dvdx * sz[0].h) ** 2, (dudy * sz[0].w) ** 2 + (dvdy * sz[0].h) ** 2)));
264
320
  // base colour
265
- if (vcol.get(pm ? pm[triPrim[t]] : '')) {
321
+ const vc = triVcol[t];
322
+ if (vc) {
266
323
  cdOf(a, triPrim[t], c3);
267
- base[0] = c3[0] * w0;
268
- base[1] = c3[1] * w0;
269
- base[2] = c3[2] * w0;
324
+ vcRGB[0] = c3[0] * w0;
325
+ vcRGB[1] = c3[1] * w0;
326
+ vcRGB[2] = c3[2] * w0;
270
327
  cdOf(b, triPrim[t], c3);
271
- base[0] += c3[0] * w1;
272
- base[1] += c3[1] * w1;
273
- base[2] += c3[2] * w1;
328
+ vcRGB[0] += c3[0] * w1;
329
+ vcRGB[1] += c3[1] * w1;
330
+ vcRGB[2] += c3[2] * w1;
274
331
  cdOf(c, triPrim[t], c3);
275
- base[0] += c3[0] * w2;
276
- base[1] += c3[1] * w2;
277
- base[2] += c3[2] * w2;
332
+ vcRGB[0] += c3[0] * w2;
333
+ vcRGB[1] += c3[1] * w2;
334
+ vcRGB[2] += c3[2] * w2;
278
335
  for (let k = 0; k < 3; k++)
279
- base[k] = srgbToLinear(Math.max(0, Math.min(1, base[k])));
336
+ vcRGB[k] = srgbToLinear(Math.max(0, Math.min(1, vcRGB[k])));
337
+ }
338
+ if (vc === 1) {
339
+ base[0] = vcRGB[0];
340
+ base[1] = vcRGB[1];
341
+ base[2] = vcRGB[2];
280
342
  }
281
343
  else if (m.baseMap && uv) {
282
344
  base[0] = base[1] = base[2] = 1;
@@ -286,12 +348,25 @@ export function pbrScene(g, tris, triPrim, files, exposure, notes) {
286
348
  base[1] = m.base[1];
287
349
  base[2] = m.base[2];
288
350
  }
351
+ let cov = triAlpha[t] === ALPHA_OPAQUE ? 1 : m.alpha;
289
352
  if (m.baseMap && uv) {
290
353
  sampleTri(m.baseMap, uvp[0], uvp[1], lod(m.baseMap), tx);
291
354
  base[0] *= tx[0];
292
355
  base[1] *= tx[1];
293
356
  base[2] *= tx[2];
357
+ if (triAlpha[t] !== ALPHA_OPAQUE)
358
+ cov *= tx[3];
359
+ }
360
+ if (triVA[t])
361
+ cov *= alphaAt(a, triPrim[t]) * w0 + alphaAt(b, triPrim[t]) * w1 + alphaAt(c, triPrim[t]) * w2;
362
+ out[3] = Math.max(0, Math.min(1, cov));
363
+ if (vc === 2) {
364
+ base[0] *= vcRGB[0];
365
+ base[1] *= vcRGB[1];
366
+ base[2] *= vcRGB[2];
294
367
  }
368
+ if (grey)
369
+ base[0] = base[1] = base[2] = GREY_ALBEDO;
295
370
  if (m.unlit) {
296
371
  out[0] = linearToSrgb(Math.min(1, base[0]));
297
372
  out[1] = linearToSrgb(Math.min(1, base[1]));
@@ -302,8 +377,8 @@ export function pbrScene(g, tris, triPrim, files, exposure, notes) {
302
377
  if (m.ormMap && uv) {
303
378
  sampleTri(m.ormMap, uvp[0], uvp[1], lod(m.ormMap), tx);
304
379
  ao = tx[0];
305
- rough = tx[1];
306
- metal = tx[2];
380
+ rough = tx[1] * m.roughness;
381
+ metal = tx[2] * m.metallic;
307
382
  }
308
383
  else if (m.roughMap && uv) {
309
384
  sampleTri(m.roughMap, uvp[0], uvp[1], lod(m.roughMap), tx);
@@ -359,10 +434,10 @@ export function pbrScene(g, tris, triPrim, files, exposure, notes) {
359
434
  }
360
435
  }
361
436
  // indirect: sky/ground irradiance (diffuse) and the environment along the reflection (specular), both occluded
362
- env(n[1], 1, e);
437
+ env(n[1], 1, e, envC);
363
438
  const ir = [e[0], e[1], e[2]];
364
439
  const RdotN = 2 * NoV, ry = RdotN * n[1] - view[1];
365
- env(ry, rough, e);
440
+ env(ry, rough, e, envC);
366
441
  // Karis' analytic environment BRDF
367
442
  const c0 = [-1, -0.0275, -0.572, 0.022], c1 = [1, 0.0425, 1.04, -0.04];
368
443
  const q = [rough * c0[0] + c1[0], rough * c0[1] + c1[1], rough * c0[2] + c1[2], rough * c0[3] + c1[3]];
@@ -51,6 +51,8 @@ export declare function thumbOptions(g: GraphSpec, size?: number): {
51
51
  shading?: import("./raster.ts").ShadingMode;
52
52
  exposure?: number;
53
53
  pbrTextures?: import("./pbr.ts").PBRTextures;
54
+ vertexColors?: import("./pbr.ts").VertexColorMode;
55
+ grey?: boolean;
54
56
  aoRadius?: number;
55
57
  orthoScale?: number;
56
58
  scale?: number;
@@ -1,6 +1,6 @@
1
1
  import { Geometry } from '../core/geometry.ts';
2
2
  import type { Vec3 } from '../core/math.ts';
3
- import type { PBRTextures } from './pbr.ts';
3
+ import type { PBRTextures, VertexColorMode } from './pbr.ts';
4
4
  /**
5
5
  * 3D views, 'persp@<azimuth>' (a persp view at that azimuth), 'section' (the cut through the section plane,
6
6
  * looking along its axis), plus 'uv': the 0..1 UV square (UDIM tiles) with every polygon drawn at its uv.
@@ -138,6 +138,18 @@ export interface RenderOptions {
138
138
  exposure?: number;
139
139
  /** shading 'pbr': texture files read by renderGeometryAsync (preparePBR); image volumes need none */
140
140
  pbrTextures?: PBRTextures;
141
+ /**
142
+ * shading 'pbr': how vertex colour Cd enters the base colour. 'auto' (default, the glTF export rule): a named
143
+ * material's base colour (factor or map) wins and Cd colours only prims without a material; 'ignore': Cd is never
144
+ * used; 'multiply': Cd also multiplies a named material's base colour (vertex paint as a tint)
145
+ */
146
+ vertexColors?: VertexColorMode;
147
+ /**
148
+ * value check: shading 'pbr' (implied when shading is not given) with every base colour replaced by a neutral 18%
149
+ * grey (linear 0.18); base colour maps and Cd give no colour, while alpha, roughness, metallic, normal and orm maps,
150
+ * emissive and unlit still apply
151
+ */
152
+ grey?: boolean;
141
153
  /** ao sample radius in output pixels (default 10) */
142
154
  aoRadius?: number;
143
155
  /** ortho and section views: world width in metres visible across a view (default: fit the bounds) */