Skip to content

Instantly share code, notes, and snippets.

@PBrockmann
Last active April 5, 2019 19:32
Show Gist options
  • Save PBrockmann/f155a779382e2b8dec7543f3466e0798 to your computer and use it in GitHub Desktop.
Save PBrockmann/f155a779382e2b8dec7543f3466e0798 to your computer and use it in GitHub Desktop.
Test to update FaceColors BufferGeometry
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pythreejs import *\n",
"from IPython.display import display"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"view_width = 600\n",
"view_height = 600"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 0, 2, 'final')"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pythreejs\n",
"pythreejs.version_info"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"def createColors():\n",
" c = []\n",
" for i in range(0,len(faces)*3):\n",
" color = [random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)]\n",
" c.append(color)\n",
" c.append(color)\n",
" c.append(color)\n",
" color = [random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)]\n",
" c.append(color)\n",
" c.append(color)\n",
" c.append(color)\n",
" \n",
" return c"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import widgets\n",
"def changeMeshColors(ev):\n",
" geometry.attributes['color'].array = createColors()\n",
" \n",
"button = widgets.Button(description=\"Change colors\")\n",
"button.on_click(changeMeshColors)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "83a1bbc837004b199c124e02ce8d1e84",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"UmVuZGVyZXIoY2FtZXJhPVBlcnNwZWN0aXZlQ2FtZXJhKHBvc2l0aW9uPSgzLjAsIDMuMCwgMy4wKSwgcXVhdGVybmlvbj0oMC4wLCAwLjAsIDAuMCwgMS4wKSwgc2NhbGU9KDEuMCwgMS4wLCDigKY=\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7d552d26266d49efa24c4600f6ab34e8",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Button(description=u'Change colors', style=ButtonStyle())"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"vertices = [\n",
" [0., 0., 0.],\n",
" [0., 0., 1.],\n",
" [0., 1., 0.],\n",
" [0., 1., 1.],\n",
" [1., 0., 0.],\n",
" [1., 0., 1.],\n",
" [1., 1., 0.],\n",
" [1., 1., 1.]\n",
"]\n",
"\n",
"faces = [\n",
" [0, 1, 3],\n",
" [0, 3, 2],\n",
" [0, 2, 4],\n",
" [2, 6, 4],\n",
" [0, 4, 1],\n",
" [1, 4, 5],\n",
" [2, 3, 6],\n",
" [3, 7, 6],\n",
" [1, 5, 3],\n",
" [3, 5, 7],\n",
" [4, 6, 5],\n",
" [5, 6, 7]\n",
"]\n",
"\n",
"p = []\n",
"for f in faces:\n",
" for v in f:\n",
" p.append(vertices[v])\n",
"\n",
"# Create the buffer geometry:\n",
"geometry = BufferGeometry(attributes=dict(\n",
" position=BufferAttribute(p),\n",
" color=BufferAttribute(createColors())\n",
"))\n",
"\n",
"# Create a mesh. Note that the material need to be told to use the vertex colors.\n",
"mesh = Mesh(\n",
" geometry=geometry,\n",
" material=MeshBasicMaterial(vertexColors='FaceColors'),\n",
" position=[-0.5, -0.5, -0.5], # Center the cube\n",
")\n",
"\n",
"scene = Scene(children=[mesh], background=\"#cccccc\")\n",
"\n",
"c = PerspectiveCamera(position=[3, 3, 3])\n",
"\n",
"rendererCube = Renderer(camera=c,\n",
" scene=scene, \n",
" controls=[OrbitControls(controlling=c)])\n",
"\n",
"display(rendererCube, button)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment