Skip to content

Commit 6b78894

Browse files
committed
Expose face vertex colors and normals.
1 parent 78f71c5 commit 6b78894

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

js/src/jupyter-threejs.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,23 +588,58 @@ define(["jupyter-js-widgets", "underscore", "three"],
588588
},
589589
});
590590

591+
591592

592593
var PlainGeometryView = ThreeView.extend({
593594
update: function() {
594595
var geometry = new THREE.Geometry();
595596
var vertices = this.model.get('vertices');
596597
var faces = this.model.get('faces');
597598
var colors = this.model.get('colors');
599+
var faceColors = this.model.get('faceColors');
600+
var faceNormals = this.model.get('faceNormals');
598601
var faceVertexUvs = this.model.get('faceVertexUvs')
599602

603+
if (faceNormals.length === 0) {
604+
faceNormals = void 0;
605+
}
606+
if (faceColors.length === 0) {
607+
faceColors = void 0;
608+
}
609+
610+
var toVec = function(a) {
611+
return new THREE.Vector3(a[0], a[1], a[2]);
612+
}
613+
var toColor = function(a) {
614+
return new THREE.Color(a);
615+
}
616+
600617
var i, len;
601618
var f;
619+
var face;
602620
for(i = 0, len=vertices.length; i<len; i+=1) {
603-
geometry.vertices.push((new THREE.Vector3()).fromArray(vertices[i]));
621+
geometry.vertices.push(toVec(vertices[i]));
604622
}
605623
for(i=0, len=faces.length; i<len; i+=1) {
606624
f = faces[i];
607-
geometry.faces.push(new THREE.Face3(f[0], f[1], f[2]));
625+
normal = faceNormals && faceNormals[i];
626+
color = faceColors && faceColors[i];
627+
if (normal) {
628+
if (Array.isArray(normal[0])) {
629+
normal = normal.map(toVec);
630+
} else {
631+
normal = toVec(normal);
632+
}
633+
}
634+
if (color) {
635+
if (Array.isArray(color)) {
636+
color = color.map(toColor);
637+
} else {
638+
color = toColor(color);
639+
}
640+
}
641+
face = new THREE.Face3(f[0], f[1], f[2], normal, color);
642+
geometry.faces.push(face);
608643
}
609644
for(i=0, len=colors.length; i<len; i+=1) {
610645
geometry.colors.push(new THREE.Color(colors[i]));
@@ -1629,7 +1664,9 @@ define(["jupyter-js-widgets", "underscore", "three"],
16291664

16301665
vertices: [],
16311666
colors: [],
1632-
faces: []
1667+
faces: [],
1668+
faceColors: [],
1669+
faceNormals: []
16331670
// todo: faceVertexUvs
16341671
})
16351672
});

pythreejs/pythreejs.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"""
1414

1515
from ipywidgets import Widget, DOMWidget, widget_serialization, Color
16-
from traitlets import (Unicode, CInt, Instance, Enum, List, Dict, Float,
16+
from traitlets import (Unicode, CInt, Instance, Enum, List, Tuple, Dict, Float,
1717
CFloat, Bool)
1818
from ._package import npm_pkg_name
1919
from math import pi, sqrt
@@ -271,6 +271,10 @@ class PlainGeometry(Geometry):
271271
vertices = List(vector3(CFloat)).tag(sync=True)
272272
colors = List(Color).tag(sync=True)
273273
faces = List(List(CFloat)).tag(sync=True)
274+
# TODO: type this better. Can handle lists of string colors, or lists of lists of string colors.
275+
faceColors = Tuple().tag(sync=True)
276+
# TODO: type this better. Can handle lists of vector3(CFloat), or lists of lists of vector3(CFloat).
277+
faceNormals = Tuple().tag(sync=True)
274278
# todo: faceVertexUvs = List(vector3(vector2(CFloat))).tag(sync=True)
275279

276280

0 commit comments

Comments
 (0)