Skip to content

Commit 929c8f2

Browse files
authored
Invoke transform function for constant attributes (#9741)
1 parent 8e0d49e commit 929c8f2

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

modules/core/src/lib/attribute/attribute-manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export default class AttributeManager {
208208
} else if (
209209
typeof accessorName === 'string' &&
210210
!buffers[accessorName] &&
211-
attribute.setConstantValue(props[accessorName])
211+
attribute.setConstantValue(context, props[accessorName])
212212
) {
213213
// Step 3: try set constant value from props
214214
// Note: if buffers[accessorName] is supplied, ignore props[accessorName]
@@ -380,7 +380,7 @@ export default class AttributeManager {
380380
// The attribute is flagged as constant outside of an update cycle
381381
// Skip allocation and updater call
382382
// @ts-ignore value can be set to an array by user but always cast to typed array during attribute update
383-
attribute.setConstantValue(attribute.value);
383+
attribute.setConstantValue(opts.context, attribute.value);
384384
return;
385385
}
386386

modules/core/src/lib/attribute/attribute.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export default class Attribute extends DataColumn<AttributeOptions, AttributeInt
254254

255255
// Use generic value
256256
// Returns true if successful
257-
setConstantValue(value?: NumericArray): boolean {
257+
setConstantValue(context: any, value?: any): boolean {
258258
// TODO(ibgreen): WebGPU does not support constant values,
259259
// they will be emulated as buffers instead for now.
260260
const isWebGPU = this.device.type === 'webgpu';
@@ -270,7 +270,9 @@ export default class Attribute extends DataColumn<AttributeOptions, AttributeInt
270270
return false;
271271
}
272272

273-
const hasChanged = this.setData({constant: true, value});
273+
const transformedValue =
274+
this.settings.transform && context ? this.settings.transform.call(context, value) : value;
275+
const hasChanged = this.setData({constant: true, value: transformedValue});
274276

275277
if (hasChanged) {
276278
this.setNeedsRedraw();

test/modules/core/lib/attribute/attribute.spec.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ test('Attribute#allocate', t => {
103103
t.ok(attribute.allocate(4), 'allocate successful');
104104
t.is(attribute.value, allocatedValue, 'reused the same typed array');
105105

106-
attribute.setConstantValue([1, 1]);
106+
attribute.setConstantValue(this, [1, 1]);
107107
t.deepEquals(attribute.value, [1, 1], 'value overwritten by external constant');
108108

109-
attribute.setConstantValue(undefined);
109+
attribute.setConstantValue(this, undefined);
110110
t.ok(attribute.allocate(4), 'allocate successful');
111111
t.is(attribute.value, allocatedValue, 'reused the same typed array');
112112

@@ -136,14 +136,14 @@ test('Attribute#setConstantValue', t => {
136136
t.ok(attribute.getValue().positions instanceof Buffer, 'attribute is using packed buffer');
137137
attribute.needsRedraw({clearChangedFlags: true});
138138

139-
attribute.setConstantValue([0, 0, 0]);
139+
attribute.setConstantValue(this, [0, 0, 0]);
140140
t.deepEqual(attribute.getValue().positions, [0, 0, 0], 'attribute set to constant value');
141141
t.ok(attribute.needsRedraw({clearChangedFlags: true}), 'attribute marked needs redraw');
142142

143-
attribute.setConstantValue([0, 0, 0]);
143+
attribute.setConstantValue(this, [0, 0, 0]);
144144
t.notOk(attribute.needsRedraw({clearChangedFlags: true}), 'attribute should not need redraw');
145145

146-
attribute.setConstantValue([0, 0, 1]);
146+
attribute.setConstantValue(this, [0, 0, 1]);
147147
t.deepEqual(attribute.getValue().positions, [0, 0, 1], 'attribute set to constant value');
148148
t.ok(attribute.needsRedraw({clearChangedFlags: true}), 'attribute marked needs redraw');
149149

@@ -156,7 +156,7 @@ test('Attribute#setConstantValue', t => {
156156
accessor: 'getColor'
157157
});
158158

159-
attribute.setConstantValue([255, 255, 0]);
159+
attribute.setConstantValue(this, [255, 255, 0]);
160160
t.deepEqual(attribute.getValue().colors, [1, 1, 0], 'constant value is normalized');
161161

162162
t.end();
@@ -277,7 +277,8 @@ test('Attribute#updateBuffer', t => {
277277
{id: 'E', value: 0, color: undefined}
278278
],
279279
getColor: d => d.color,
280-
getValue: d => d.value
280+
getValue: d => d.value,
281+
getValueConstant: [20]
281282
};
282283

283284
const TEST_PARAMS = [
@@ -341,6 +342,18 @@ test('Attribute#updateBuffer', t => {
341342
standard: [20, 40, 14, 0],
342343
'variable size': [20, 20, 40, 14, 14, 14, 14, 0, 0, 0]
343344
},
345+
{
346+
title: 'constant accessor with transform',
347+
attribute: new Attribute(device, {
348+
id: 'values',
349+
type: 'float32',
350+
size: 1,
351+
accessor: 'getValueConstant',
352+
transform: x => x * 2
353+
}),
354+
standard: [40, 40, 40, 40],
355+
'variable size': [40, 40, 40, 40, 40, 40, 40, 40, 40, 40]
356+
},
344357
{
345358
title: 'custom accessor',
346359
attribute: new Attribute(device, {
@@ -1114,7 +1127,7 @@ test('Attribute#updateBuffer', t => {
11141127
t.is(bounds, attribute.getBounds(), 'bounds is cached');
11151128

11161129
attribute.setNeedsUpdate();
1117-
attribute.setConstantValue([-1, 0, 1]);
1130+
attribute.setConstantValue(this, [-1, 0, 1]);
11181131

11191132
bounds = attribute.getBounds();
11201133
t.deepEqual(

0 commit comments

Comments
 (0)