Skip to content

Commit 4849a15

Browse files
committed
Fix uniform colors
1 parent 69e0629 commit 4849a15

File tree

4 files changed

+56
-26
lines changed

4 files changed

+56
-26
lines changed

examples/Shaders.ipynb

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"outputs": [],
1515
"source": [
1616
"from pythreejs import *\n",
17-
"import ipywidgets\n",
17+
"import ipywidgets as widgets\n",
1818
"from IPython.display import display"
1919
]
2020
},
@@ -51,11 +51,13 @@
5151
"outputs": [],
5252
"source": [
5353
"fragment_shader = \"\"\"\n",
54+
"uniform vec3 user_color;\n",
55+
"\n",
5456
"void main() {\n",
55-
" #ifdef OVERRIDE_COLOR\n",
56-
" gl_FragColor = vec4(0, 0.5, 0, 1.0);\n",
57-
" #else\n",
57+
" #ifdef FIX_COLOR\n",
5858
" gl_FragColor = vec4(0.5, 0, 0, 1.0);\n",
59+
" #else\n",
60+
" gl_FragColor = vec4(user_color, 1.0);\n",
5961
" #endif\n",
6062
"}\n",
6163
"\"\"\""
@@ -71,10 +73,11 @@
7173
" uniforms=dict(\n",
7274
" time=dict(value=0.0),\n",
7375
" resolution=dict(value=(1, 1)),\n",
76+
" user_color=dict(value='green'),\n",
7477
" **UniformsLib['lights']\n",
7578
" ),\n",
7679
" defines=dict(\n",
77-
" OVERRIDE_COLOR=1,\n",
80+
" FIX_COLOR=1,\n",
7881
" ),\n",
7982
" vertexShader=vertex_shader,\n",
8083
" fragmentShader=fragment_shader,\n",
@@ -97,6 +100,7 @@
97100
"metadata": {},
98101
"outputs": [],
99102
"source": [
103+
"# Disable fixed color from defines:\n",
100104
"material.defines = dict()\n",
101105
"material.needsUpdate = True"
102106
]
@@ -107,28 +111,30 @@
107111
"metadata": {},
108112
"outputs": [],
109113
"source": [
110-
"def update_time(time):\n",
114+
"# Uniform editors:\n",
115+
"picker = widgets.ColorPicker(value=material.uniforms['user_color']['value'])\n",
116+
"time_slider = widgets.FloatSlider(value=material.uniforms['time']['value'], min=-15, max=15)\n",
117+
"\n",
118+
"# Interactive code:\n",
119+
"def update_uniforms(change):\n",
111120
" uniforms = dict(**material.uniforms)\n",
112-
" uniforms.update(time=dict(value=time))\n",
121+
" uniforms.update(\n",
122+
" time=dict(value=time_slider.value),\n",
123+
" user_color=dict(value=picker.value),\n",
124+
" )\n",
113125
" material.uniforms = uniforms\n",
114-
" material.needsUpdate = True"
115-
]
116-
},
117-
{
118-
"cell_type": "code",
119-
"execution_count": null,
120-
"metadata": {},
121-
"outputs": [],
122-
"source": [
123-
"update_time(-15)"
126+
" material.needsUpdate = True\n",
127+
"\n",
128+
"picker.observe(update_uniforms)\n",
129+
"time_slider.observe(update_uniforms)\n",
130+
"\n",
131+
"# Present \"dashboard\" in VBox:\n",
132+
"widgets.VBox([\n",
133+
" Preview(material),\n",
134+
" picker,\n",
135+
" time_slider,\n",
136+
"])"
124137
]
125-
},
126-
{
127-
"cell_type": "code",
128-
"execution_count": null,
129-
"metadata": {},
130-
"outputs": [],
131-
"source": []
132138
}
133139
],
134140
"metadata": {
@@ -147,7 +153,7 @@
147153
"name": "python",
148154
"nbconvert_exporter": "python",
149155
"pygments_lexer": "ipython3",
150-
"version": "3.5.4"
156+
"version": "3.6.3"
151157
}
152158
},
153159
"nbformat": 4,

js/scripts/prop-types.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,12 @@ class DictType extends BaseType {
389389
}
390390
}
391391

392+
class UniformDict extends DictType {
393+
getPropertyConverterFn() {
394+
return 'convertUniformDict';
395+
}
396+
}
397+
392398
class FunctionType extends BaseType {
393399
constructor(fn) {
394400
super();
@@ -562,6 +568,7 @@ module.exports = {
562568
Array: ArrayType,
563569
ArrayBuffer: ArrayBufferType,
564570
Dict: DictType,
571+
UniformDict: UniformDict,
565572
Function: FunctionType,
566573
Vector2: Vector2,
567574
Vector3: Vector3,

js/scripts/three-class-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ module.exports = {
857857
relativePath: './materials/ShaderMaterial',
858858
superClass: 'Material',
859859
properties: {
860-
uniforms: new Types.Dict(),
860+
uniforms: new Types.UniformDict(),
861861
clipping: new Types.Bool(false),
862862
extensions: new Types.Dict(),
863863
fog: new Types.Bool(false),

js/src/_base/Three.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,23 @@ var ThreeModel = widgets.WidgetModel.extend({
670670
Object.assign(obj[key], value);
671671
},
672672

673+
convertUniformDictModelToThree: function(modelDict) {
674+
// Convert any strings to THREE.Color
675+
// Just modify dict in-place, as it should serialize the same
676+
Object.keys(modelDict).forEach(function(k) {
677+
var value = modelDict[k].value;
678+
if (value && (typeof value === 'string' || value instanceof String)) {
679+
modelDict[k].value = new THREE.Color(value);
680+
}
681+
});
682+
return modelDict;
683+
},
684+
685+
convertUniformDictThreeToModel: function(threeDict) {
686+
// No-op
687+
return threeDict;
688+
},
689+
673690
// ThreeTypeArray
674691
convertThreeTypeArrayModelToThree: function(modelArr, propName) {
675692
if (!Array.isArray(modelArr)) {

0 commit comments

Comments
 (0)