Skip to content

Commit 9c33ca8

Browse files
committed
WIP use BufferGeometry for the plain geometry.
We probably should have a separate PlainGeometry and PlainBufferGeometry. Note that with this change, faceColors is *actually* a list of colors for each vertex. Also, it seems that the cube example has the normals slightly off.
1 parent 4900ac5 commit 9c33ca8

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

examples/Examples.ipynb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,13 @@
327327
},
328328
{
329329
"cell_type": "code",
330-
"execution_count": 22,
330+
"execution_count": 16,
331331
"metadata": {},
332332
"outputs": [
333333
{
334334
"data": {
335335
"application/vnd.jupyter.widget-view+json": {
336-
"model_id": "14b729d1bb674cefb80aa5a9d0a3f990"
336+
"model_id": "e447091aeb744189850523e511566d2c"
337337
}
338338
},
339339
"metadata": {},
@@ -375,13 +375,11 @@
375375
" [5, 6, 7]\n",
376376
" ])\n",
377377
"\n",
378-
"vertexcolors = [(0,0,0), (0,0,1), (0,1,0), (1,0,0), (0,1,1), (1,0,1), (1,1,0), (1,1,1)]\n",
379-
"faceColors=np.asarray([[vertexcolors[i] for i in f] for f in faces])\n",
378+
"vertexcolors = np.asarray([(1,1,1)]*8)\n",
380379
"\n",
380+
"cubeGeometry = PlainGeometry(vertices=vertices, faces=faces, faceColors = vertexcolors)\n",
381381
"\n",
382-
"cubeGeometry = PlainGeometry(vertices=vertices, faces=faces, faceColors = faceColors)\n",
383-
"\n",
384-
"myobjectCube = Mesh(geometry=cubeGeometry, material = PhongMaterial(color='white', vertexColors = 'VertexColors'))\n",
382+
"myobjectCube = Mesh(geometry=cubeGeometry, material = PhongMaterial( vertexColors = 'VertexColors'))\n",
385383
"cCube = PerspectiveCamera(position=[3, 3, 3], fov=20,\n",
386384
" children=[DirectionalLight(color='#ffffff', position=[-3, 5, 1], intensity=0.5)])\n",
387385
"sceneCube = Scene(children=[myobjectCube, AmbientLight(color='#dddddd')])\n",

js/src/jupyter-threejs.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,14 +591,14 @@ define(["jupyter-js-widgets", "underscore", "three", "ndarray"],
591591

592592
var PlainGeometryView = ThreeView.extend({
593593
update: function() {
594-
var geometry = new THREE.Geometry();
594+
var geometry = new THREE.BufferGeometry();
595595
var vertices = this.model.get('vertices');
596596
var faces = this.model.get('faces');
597597
var colors = this.model.get('colors');
598598
var faceColors = this.model.get('faceColors');
599599
var faceNormals = this.model.get('faceNormals');
600-
var faceVertexUvs = this.model.get('faceVertexUvs')
601-
600+
var faceVertexUvs = this.model.get('faceVertexUvs');
601+
/*
602602
if (faceNormals.length === 0) {
603603
faceNormals = void 0;
604604
}
@@ -616,7 +616,14 @@ define(["jupyter-js-widgets", "underscore", "three", "ndarray"],
616616
var i, len;
617617
var f;
618618
var face;
619-
for(i = 0, len=vertices.shape[0]; i<len; i+=1) {
619+
*/
620+
geometry.addAttribute('position', new THREE.BufferAttribute(vertices.data, 3));
621+
geometry.addAttribute('color', new THREE.BufferAttribute(faceColors.data, 3));
622+
geometry.setIndex(new THREE.BufferAttribute(faces.data, 1));
623+
geometry.computeVertexNormals();
624+
geometry.computeBoundingSphere();
625+
626+
/*for(i = 0, len=vertices.shape[0]; i<len; i+=1) {
620627
geometry.vertices.push(toVec(vertices.pick(i)));
621628
}
622629
for(i=0, len=faces.shape[0]; i<len; i+=1) {
@@ -639,14 +646,17 @@ define(["jupyter-js-widgets", "underscore", "three", "ndarray"],
639646
for(i=0, len=colors.length; i<len; i+=1) {
640647
geometry.colors.push(new THREE.Color(colors[i]));
641648
}
649+
*/
642650
// TODO: faceVertexUvs
643-
geometry.verticesNeedUpdate = true;
644-
geometry.elementsNeedUpdate = true;
651+
geometry.attributes.position.needsUpdate = true;
652+
geometry.attributes.color.needsUpdate = true;
653+
/*geometry.elementsNeedUpdate = true;
645654
geometry.uvsNeedUpdate = true;
646655
geometry.normalsNeedUpdate = true;
647656
geometry.tangentsNeedUpdate = true;
648657
geometry.colorsNeedUpdate = true;
649658
geometry.lineDistancesNeedUpdate = true;
659+
*/
650660
this.replace_obj(geometry);
651661
},
652662
});

pythreejs/pythreejs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class PlainGeometry(Geometry):
274274
vertices = Array(dtype='float32').tag(sync=True, **array_serialization).valid(shape_constraints(None,3))
275275
faces = Array(dtype='uint32').tag(sync=True, **array_serialization).valid(shape_constraints(None,3))
276276
# list of [[v1_r,v1_g,v1_b], [v2_r,v2_g,v2_b], [v3_r,v3_g,v3_b]] for each face
277-
faceColors = Array(dtype='float32').tag(sync=True, **array_serialization).valid(shape_constraints(None,3,3))
277+
faceColors = Array(dtype='float32').tag(sync=True, **array_serialization).valid(shape_constraints(None,3))
278278
#vertices = List(vector3(CFloat)).tag(sync=True)
279279
colors = List(Color).tag(sync=True)
280280
#faces = List(List(CFloat)).tag(sync=True)

0 commit comments

Comments
 (0)