Skip to content
Merged
4 changes: 2 additions & 2 deletions modules/core/src/lib/attribute/attribute-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export default class AttributeManager {
} else if (
typeof accessorName === 'string' &&
!buffers[accessorName] &&
attribute.setConstantValue(props[accessorName])
attribute.setConstantValue(context, props[accessorName])
) {
// Step 3: try set constant value from props
// Note: if buffers[accessorName] is supplied, ignore props[accessorName]
Expand Down Expand Up @@ -380,7 +380,7 @@ export default class AttributeManager {
// The attribute is flagged as constant outside of an update cycle
// Skip allocation and updater call
// @ts-ignore value can be set to an array by user but always cast to typed array during attribute update
attribute.setConstantValue(attribute.value);
attribute.setConstantValue(context, attribute.value);
return;
}

Expand Down
7 changes: 5 additions & 2 deletions modules/core/src/lib/attribute/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export default class Attribute extends DataColumn<AttributeOptions, AttributeInt

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

const hasChanged = this.setData({constant: true, value});
const transformedValue = this.settings.transform
? this.settings.transform.call(context, value)
: value;
const hasChanged = this.setData({constant: true, value: transformedValue});

if (hasChanged) {
this.setNeedsRedraw();
Expand Down
29 changes: 21 additions & 8 deletions test/modules/core/lib/attribute/attribute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ test('Attribute#allocate', t => {
t.ok(attribute.allocate(4), 'allocate successful');
t.is(attribute.value, allocatedValue, 'reused the same typed array');

attribute.setConstantValue([1, 1]);
attribute.setConstantValue(this, [1, 1]);
t.deepEquals(attribute.value, [1, 1], 'value overwritten by external constant');

attribute.setConstantValue(undefined);
attribute.setConstantValue(this, undefined);
t.ok(attribute.allocate(4), 'allocate successful');
t.is(attribute.value, allocatedValue, 'reused the same typed array');

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

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

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

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

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

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

t.end();
Expand Down Expand Up @@ -277,7 +277,8 @@ test('Attribute#updateBuffer', t => {
{id: 'E', value: 0, color: undefined}
],
getColor: d => d.color,
getValue: d => d.value
getValue: d => d.value,
getValueConstant: [20]
};

const TEST_PARAMS = [
Expand Down Expand Up @@ -341,6 +342,18 @@ test('Attribute#updateBuffer', t => {
standard: [20, 40, 14, 0],
'variable size': [20, 20, 40, 14, 14, 14, 14, 0, 0, 0]
},
{
title: 'constant accessor with transform',
attribute: new Attribute(device, {
id: 'values',
type: 'float32',
size: 1,
accessor: 'getValueConstant',
transform: x => x * 2
}),
standard: [40, 40, 40, 40],
'variable size': [40, 40, 40, 40, 40, 40, 40, 40, 40, 40]
},
{
title: 'custom accessor',
attribute: new Attribute(device, {
Expand Down Expand Up @@ -1114,7 +1127,7 @@ test('Attribute#updateBuffer', t => {
t.is(bounds, attribute.getBounds(), 'bounds is cached');

attribute.setNeedsUpdate();
attribute.setConstantValue([-1, 0, 1]);
attribute.setConstantValue(this, [-1, 0, 1]);

bounds = attribute.getBounds();
t.deepEqual(
Expand Down
Loading