Skip to content

Commit b7ea9eb

Browse files
committed
enable flat shading control for default geometry rendering
1 parent ca0941c commit b7ea9eb

File tree

3 files changed

+66
-19
lines changed

3 files changed

+66
-19
lines changed

examples/Geometries.ipynb

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 1,
66
"metadata": {
77
"collapsed": true
88
},
@@ -117,14 +117,13 @@
117117
},
118118
{
119119
"cell_type": "code",
120-
"execution_count": null,
120+
"execution_count": 2,
121121
"metadata": {
122122
"collapsed": false
123123
},
124124
"outputs": [],
125125
"source": [
126-
"# TODO: flat shading\n",
127-
"DodecahedronGeometry(radius=10, detail=0)"
126+
"DodecahedronGeometry(radius=10, detail=0, _flat=True)"
128127
]
129128
},
130129
{
@@ -153,14 +152,13 @@
153152
},
154153
{
155154
"cell_type": "code",
156-
"execution_count": null,
155+
"execution_count": 3,
157156
"metadata": {
158157
"collapsed": true
159158
},
160159
"outputs": [],
161160
"source": [
162-
"# TODO: flat shading\n",
163-
"IcosahedronGeometry(radius=10)"
161+
"IcosahedronGeometry(radius=10, _flat=True)"
164162
]
165163
},
166164
{
@@ -186,18 +184,18 @@
186184
},
187185
{
188186
"cell_type": "code",
189-
"execution_count": null,
187+
"execution_count": 4,
190188
"metadata": {
191189
"collapsed": true
192190
},
193191
"outputs": [],
194192
"source": [
195-
"OctahedronGeometry(radius=10, detail=0)"
193+
"OctahedronGeometry(radius=10, detail=0, _flat=True)"
196194
]
197195
},
198196
{
199197
"cell_type": "code",
200-
"execution_count": null,
198+
"execution_count": 5,
201199
"metadata": {
202200
"collapsed": true
203201
},
@@ -210,7 +208,7 @@
210208
" return new THREE.Vector3(10 * x, 10 * y, x*x - y*y); \n",
211209
" }\"\"\",\n",
212210
" slices=5,\n",
213-
" stacks=10)"
211+
" stacks=10, _flat=True)"
214212
]
215213
},
216214
{
@@ -225,7 +223,7 @@
225223
" width=10,\n",
226224
" height=15,\n",
227225
" widthSegments=5,\n",
228-
" heightSegments=10,)"
226+
" heightSegments=10)"
229227
]
230228
},
231229
{
@@ -240,7 +238,7 @@
240238
" width=10,\n",
241239
" height=15,\n",
242240
" widthSegments=5,\n",
243-
" heightSegments=10,)"
241+
" heightSegments=10)"
244242
]
245243
},
246244
{
@@ -341,14 +339,13 @@
341339
},
342340
{
343341
"cell_type": "code",
344-
"execution_count": null,
342+
"execution_count": 6,
345343
"metadata": {
346344
"collapsed": true
347345
},
348346
"outputs": [],
349347
"source": [
350-
"# TODO: flat shading\n",
351-
"TetrahedronGeometry(radius=10, detail=1)"
348+
"TetrahedronGeometry(radius=10, detail=1, _flat=True)"
352349
]
353350
},
354351
{
@@ -483,6 +480,46 @@
483480
"nbconvert_exporter": "python",
484481
"pygments_lexer": "ipython2",
485482
"version": "2.7.12"
483+
},
484+
"widgets": {
485+
"state": {
486+
"345bb19e86e8492ab984784d5e28b4ea": {
487+
"views": [
488+
{
489+
"cell_index": 10
490+
}
491+
]
492+
},
493+
"91929632315b4de9a9dbc9820e74c5bc": {
494+
"views": [
495+
{
496+
"cell_index": 12
497+
}
498+
]
499+
},
500+
"97beb4476542488885d834cf97285479": {
501+
"views": [
502+
{
503+
"cell_index": 13
504+
}
505+
]
506+
},
507+
"edf315d5a75b42b2909bb96c91e7654a": {
508+
"views": [
509+
{
510+
"cell_index": 22
511+
}
512+
]
513+
},
514+
"fa3df762942744dbbda986a316f570ef": {
515+
"views": [
516+
{
517+
"cell_index": 7
518+
}
519+
]
520+
}
521+
},
522+
"version": "1.2.0"
486523
}
487524
},
488525
"nbformat": 4,

js/src/_base/Three.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,17 @@ var ThreeView = widgets.DOMWidgetView.extend({
112112

113113
} else if (obj instanceof THREE.Geometry || obj instanceof THREE.BufferGeometry) {
114114

115-
var material = new THREE.MeshStandardMaterial({
116-
color: '#888888',
117-
});
115+
var material;
116+
if (this.model.get('_flat')) {
117+
material = new THREE.MeshPhongMaterial({
118+
color: '#888888',
119+
shading: THREE.FlatShading,
120+
});
121+
} else {
122+
material = new THREE.MeshStandardMaterial({
123+
color: '#888888',
124+
});
125+
}
118126
var mesh = new THREE.Mesh(obj, material);
119127
this.scene.add(mesh);
120128

@@ -285,6 +293,7 @@ var ThreeModel = widgets.DOMWidgetModel.extend({
285293

286294
_width: 200,
287295
_height: 200,
296+
_flat: false,
288297
}),
289298

290299
initialize: function(attributes, options) {

pythreejs/_base/Three.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class ThreeWidget(Widget):
1010
# renderer properties
1111
_width = CInt(200).tag(sync=True)
1212
_height = CInt(200).tag(sync=True)
13+
_flat = Bool(False).tag(sync=True)
1314

1415
def __init__(self, **kwargs):
1516
Widget.__init__(self, **kwargs)

0 commit comments

Comments
 (0)