Skip to content

Commit f1995ab

Browse files
committed
Finish PlainBufferGeometry.from_geometry
1 parent 9f20998 commit f1995ab

File tree

2 files changed

+68
-33
lines changed

2 files changed

+68
-33
lines changed

examples/BufferAttributes and BufferGeometry.ipynb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,42 @@
204204
"plain"
205205
]
206206
},
207+
{
208+
"cell_type": "code",
209+
"execution_count": null,
210+
"metadata": {},
211+
"outputs": [],
212+
"source": []
213+
},
214+
{
215+
"cell_type": "code",
216+
"execution_count": null,
217+
"metadata": {},
218+
"outputs": [],
219+
"source": [
220+
"# Now from a non-buffer geometry:\n",
221+
"box = PlainBufferGeometry.from_geometry(BoxGeometry())"
222+
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": null,
227+
"metadata": {},
228+
"outputs": [],
229+
"source": [
230+
"box"
231+
]
232+
},
207233
{
208234
"cell_type": "code",
209235
"execution_count": null,
210236
"metadata": {},
211237
"outputs": [],
212238
"source": [
213-
"plain.attributes"
239+
"# This shows that when converting from a regular geometry\n",
240+
"# to a buffer geometry, it does not use indexing (position attribute\n",
241+
"# is expanded):\n",
242+
"box.attributes"
214243
]
215244
},
216245
{

js/src/geometries/PlainBufferGeometry.js

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var AutogenPlainBufferGeometryModel = require('../geometries/PlainBufferGeometry
66
var core = require('../core')
77
var BufferGeometryModel = core.BufferGeometryModel;
88
var BufferAttributeModel = core.BufferAttributeModel;
9+
var GeometryModel = core.GeometryModel;
910

1011

1112
var PlainBufferGeometryModel = AutogenPlainBufferGeometryModel.extend({
@@ -17,13 +18,11 @@ var PlainBufferGeometryModel = AutogenPlainBufferGeometryModel.extend({
1718

1819
constructFromRef: function(ref) {
1920
var result = new THREE.BufferGeometry();
20-
// Copy ref. This will create new buffers!
21-
result.copy(ref.obj);
2221

2322
var chain = ref.initPromise.bind(this);
2423
var toSet = {};
2524
if (ref instanceof PlainBufferGeometryModel) {
26-
// TODO: Review this!
25+
// Ensure ref three obj attributes are actually created:
2726
chain = chain.then(
2827
// Wait for all attributes
2928
Promise.all(_.map(_.values(ref.get('attributes')), attr => {
@@ -34,46 +33,53 @@ var PlainBufferGeometryModel = AutogenPlainBufferGeometryModel.extend({
3433
Promise.all(_.map(_.values(ref.get('morphAttributes')), attr => {
3534
return attr.initPromise;
3635
}))
37-
).then(() => {
36+
);
37+
}
38+
39+
// Create three.js BufferAttributes from ref.
40+
if (ref instanceof BufferGeometryModel) {
41+
chain = chain.then(function() {
3842
// Copy ref. This will create new buffers!
3943
result.copy(ref.obj);
4044
});
41-
} else if (ref instanceof BufferGeometryModel) {
42-
// We have a ref that is some other kind of buffergeometry
43-
// This ref will then not have 'attributes' as models
44-
// We need to:
45-
// - Create models for each bufferattribute
46-
// - Set attribute dicts on model
47-
// Create models for all attributes:
45+
} else if (ref instanceof GeometryModel) {
46+
// Copy from geometry. This will create new buffers.
4847
chain = chain.then(function() {
49-
// Copy ref. This will create new buffers!
50-
result.copy(ref.obj);
51-
52-
return Promise.all(_.map(_.pairs(result.attributes), kv => {
53-
return createModel(BufferAttributeModel, this.widget_manager, kv[1]).then(model => {
54-
return [kv[0], model];
55-
});
56-
}));
57-
}).then((attribModelKVs) => {
58-
toSet.attributes = _.object(attribModelKVs);
48+
result.fromGeometry(ref.obj);
49+
});
50+
} else {
51+
throw new Error('Invalid reference geometry:', ref);
52+
}
5953

60-
// Then create models for all morphAttributes:
61-
}).then(Promise.all(_.map(_.pairs(result.morphAttributes), kv => {
54+
// Result now has all the attributes, but they do not
55+
// currently have corresponding pythreejs models.
56+
// We need to:
57+
// - Create models for each buffer attribute
58+
// - Set attribute dicts on model
59+
return chain.then(() => {
60+
// Create models for all attributes:
61+
return Promise.all(_.map(_.pairs(result.attributes), kv => {
6262
return createModel(BufferAttributeModel, this.widget_manager, kv[1]).then(model => {
6363
return [kv[0], model];
6464
});
65-
}))).then((attribModelKVs) => {
66-
toSet.morphAttributes = _.object(attribModelKVs);
65+
}));
66+
}).then((attribModelKVs) => {
67+
toSet.attributes = _.object(attribModelKVs);
68+
69+
// Then create models for all morphAttributes:
70+
}).then(Promise.all(_.map(_.pairs(result.morphAttributes), kv => {
71+
return createModel(BufferAttributeModel, this.widget_manager, kv[1]).then(model => {
72+
return [kv[0], model];
6773
});
68-
} else {
69-
// Assume ref is GeometryModel
70-
throw new Error('Geometry -> PlainBufferGeometry not yet supported!');
71-
}
72-
73-
return chain.then(function() {
74+
}))).then((attribModelKVs) => {
75+
toSet.morphAttributes = _.object(attribModelKVs);
7476

75-
// Sync out all copied properties not yet dealt with
77+
// Sync out all properties that have been set:
78+
}).then(() => {
79+
// Add other fields that needs to be synced out:
7680
toSet.name = result.name;
81+
82+
// Perform actual sync to kernel side:
7783
this.set(toSet, 'pushFromThree');
7884
this.save_changes();
7985

0 commit comments

Comments
 (0)