Skip to content

Commit 774b093

Browse files
committed
Ensure buffergeom attributes map correctly
1 parent b29fc57 commit 774b093

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

js/src/core/BufferAttribute.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,51 @@ var BufferAttributeAutogen = require('./BufferAttribute.autogen').BufferAttribut
44

55
var BufferAttributeModel = BufferAttributeAutogen.extend({
66

7-
constructThreeObject: function() {
7+
createPropertiesArrays: function() {
8+
BufferAttributeAutogen.prototype.createPropertiesArrays.call(this);
9+
10+
// three.js DataTexture stores the data, width, and height props together in a dict called 'image'
11+
this.property_mappers['BufferAttributeArray'] = 'mapBufferAttributeArray';
12+
delete this.property_converters['array'];
13+
},
814

9-
var array = this.get('array');
10-
if (array.dimension > 2) {
15+
decodeData() {
16+
var rawData = this.get('array');
17+
if (rawData.dimension > 2) {
1118
throw Error('Array has too many dimensions:', array)
1219
}
13-
var itemSize = array.dimension === 1 ? 1 : array.shape[1];
20+
var itemSize = rawData.dimension === 1 ? 1 : rawData.shape[1];
21+
22+
var data = this.convertArrayBufferModelToThree(rawData, 'array');
23+
return {
24+
array: data,
25+
itemSize: itemSize,
26+
}
27+
},
1428

29+
constructThreeObject: function() {
30+
var data = this.decodeData();
1531
var result = new THREE.BufferAttribute(
16-
this.convertArrayBufferModelToThree(array, 'array'),
17-
itemSize,
32+
data.array,
33+
data.itemSize,
1834
this.get('normalized')
1935
);
36+
result.needsUpdate = true;
2037
return Promise.resolve(result);
2138

2239
},
2340

24-
assignAttributeMap: function() {
41+
mapBufferAttributeArrayModelToThree: function() {
42+
var data = this.decodeData();
43+
this.obj.setArray(data.array);
44+
this.obj.needsUpdate = true;
45+
this.set({ version: this.obj.version }, 'pushFromThree');
46+
},
2547

48+
mapBufferAttributeArrayThreeToModel: function() {
49+
var attributeData = this.obj.array;
50+
var modelNDArray = this.get('array');
51+
modelNDArray.data.set(attributeData);
2652
},
2753

2854
}, {

js/src/geometries/PlainBufferGeometry.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
var _ = require('underscore');
12
var AutogenPlainBufferGeometryModel = require('../geometries/PlainBufferGeometry.autogen').PlainBufferGeometryModel;
23

34

45
var PlainBufferGeometryModel = AutogenPlainBufferGeometryModel.extend({
56

7+
createPropertiesArrays: function() {
8+
AutogenPlainBufferGeometryModel.prototype.createPropertiesArrays.call(this);
9+
this.property_assigners['attributes'] = 'assignAttributesMap';
10+
},
11+
612
constructThreeObject: function() {
713

814
var result = new THREE.BufferGeometry();
@@ -33,6 +39,34 @@ var PlainBufferGeometryModel = AutogenPlainBufferGeometryModel.extend({
3339

3440
},
3541

42+
assignAttributesMap: function(obj, key, value) {
43+
44+
var three = obj[key];
45+
var oldKeys = three ? Object.keys(three).sort() : [];
46+
var newKeys = value ? Object.keys(value).sort() : [];
47+
48+
var added = _.difference(newKeys, oldKeys);
49+
var removed = _.difference(oldKeys, newKeys);
50+
var common = _.intersection(oldKeys, newKeys);
51+
52+
if (removed.length > 0) {
53+
console.warn('Cannot remove buffer geometry attributes:', removed);
54+
}
55+
added.forEach(key => {
56+
obj.addAttribute(key, value[key]);
57+
});
58+
59+
var current;
60+
common.forEach(key => {
61+
current = obj.getAttribute(key);
62+
if (current !== value[key]) {
63+
console.warn('Cannot reassign buffer geometry attribute:', key);
64+
return; // continue
65+
}
66+
});
67+
68+
},
69+
3670
});
3771

3872
module.exports = {

0 commit comments

Comments
 (0)