Commit 9e7cbedf authored by Dario Seyb's avatar Dario Seyb

API improvements

parent 728b11f3
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Combined Camera\n",
"\n",
"A combined camera allows you to easily switch between a perspective camera and an orthographic camera."
]
},
{
"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": [
"mesh1 = Mesh(SphereBufferGeometry(20, 16, 16), MeshPhysicalMaterial(color='red'), position=[-20, 0, 0])\n",
"mesh2 = Mesh(SphereBufferGeometry(20, 16, 16), MeshPhysicalMaterial(color='green'), position=[20, 0, 0])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"view_width = 600\n",
"view_height = 400\n",
"camera = CombinedCamera(position=[0, 0, 60], width=view_width, height=view_height)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e92db2bb7f02499bb24f79af5f03be32",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>Renderer</code>.</p>\n",
"<p>\n",
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another frontend (for example, a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"Renderer(camera=CombinedCamera(height=400.0, position=(0.0, 0.0, 60.0), projectionMatrix=(1.4296712803397058, 0.0, 0.0, 0.0, 0.0, 2.1445069205095586, 0.0, 0.0, 0.0, 0.0, -1.00010000500025, -1.0, 0.0, 0.0, -0.200010000500025, 0.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), type=u'PerspectiveCamera', up=(0.0, 1.0, 0.0), width=600.0), controls=[OrbitControls(controlling=CombinedCamera(height=400.0, position=(0.0, 0.0, 60.0), projectionMatrix=(1.4296712803397058, 0.0, 0.0, 0.0, 0.0, 2.1445069205095586, 0.0, 0.0, 0.0, 0.0, -1.00010000500025, -1.0, 0.0, 0.0, -0.200010000500025, 0.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), type=u'PerspectiveCamera', up=(0.0, 1.0, 0.0), width=600.0))], scene=Scene(children=(Mesh(geometry=SphereBufferGeometry(heightSegments=16, radius=20.0, type=u'SphereBufferGeometry', widthSegments=16), material=MeshPhysicalMaterial(alphaMap=None, aoMap=None, bumpMap=None, color=u'red', defines={'PHYSICAL': ''}, displacementMap=None, emissiveMap=None, envMap=None, lightMap=None, map=None, metalnessMap=None, normalMap=None, normalScale=(1.0, 1.0), roughnessMap=None, type=u'MeshPhysicalMaterial'), position=(-20.0, 0.0, 0.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), Mesh(geometry=SphereBufferGeometry(heightSegments=16, radius=20.0, type=u'SphereBufferGeometry', widthSegments=16), material=MeshPhysicalMaterial(alphaMap=None, aoMap=None, bumpMap=None, color=u'green', defines={'PHYSICAL': ''}, displacementMap=None, emissiveMap=None, envMap=None, lightMap=None, map=None, metalnessMap=None, normalMap=None, normalScale=(1.0, 1.0), roughnessMap=None, type=u'MeshPhysicalMaterial'), position=(20.0, 0.0, 0.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), PointLight(position=(-100.0, 100.0, 100.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), AmbientLight(intensity=0.4, quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), CombinedCamera(height=400.0, position=(0.0, 0.0, 60.0), projectionMatrix=(1.4296712803397058, 0.0, 0.0, 0.0, 0.0, 2.1445069205095586, 0.0, 0.0, 0.0, 0.0, -1.00010000500025, -1.0, 0.0, 0.0, -0.200010000500025, 0.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), type=u'PerspectiveCamera', up=(0.0, 1.0, 0.0), width=600.0)), fog=None, overrideMaterial=None, quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), shadowMap=WebGLShadowMap())"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"key_light = PointLight(position=[-100, 100, 100])\n",
"ambient_light = AmbientLight(intensity=0.4)\n",
"scene = Scene(children=[mesh1, mesh2, key_light, ambient_light, camera])\n",
"renderer = Renderer(scene=scene, camera=camera, controls=[OrbitControls(controlling=camera)],\n",
" width=view_width, height=view_height)\n",
"display(renderer)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"camera.mode = 'orthographic'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"camera.mode = 'perspective'"
]
},
{
"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.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
This diff is collapsed.
This diff is collapsed.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Combined Camera\n",
"\n",
"A combined camera allows you to easily switch between a perspective camera and an orthographic camera."
]
},
{
"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": [
"mesh1 = Mesh(SphereBufferGeometry(20, 16, 16), MeshPhysicalMaterial(color='red'), position=[-20, 0, 0])\n",
"mesh2 = Mesh(SphereBufferGeometry(20, 16, 16), MeshPhysicalMaterial(color='green'), position=[20, 0, 0])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"view_width = 600\n",
"view_height = 400\n",
"camera = CombinedCamera(position=[0, 0, 60], width=view_width, height=view_height)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "27b0f193dd444c589b0a0b59c5411f9f",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>Renderer</code>.</p>\n",
"<p>\n",
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another frontend (for example, a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"Renderer(camera=CombinedCamera(height=400.0, position=(0.0, 0.0, 60.0), projectionMatrix=(1.4296712803397058, 0.0, 0.0, 0.0, 0.0, 2.1445069205095586, 0.0, 0.0, 0.0, 0.0, -1.00010000500025, -1.0, 0.0, 0.0, -0.200010000500025, 0.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), type=u'PerspectiveCamera', up=(0.0, 1.0, 0.0), width=600.0), controls=[OrbitControls(controlling=CombinedCamera(height=400.0, position=(0.0, 0.0, 60.0), projectionMatrix=(1.4296712803397058, 0.0, 0.0, 0.0, 0.0, 2.1445069205095586, 0.0, 0.0, 0.0, 0.0, -1.00010000500025, -1.0, 0.0, 0.0, -0.200010000500025, 0.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), type=u'PerspectiveCamera', up=(0.0, 1.0, 0.0), width=600.0))], scene=Scene(children=(Mesh(geometry=SphereBufferGeometry(heightSegments=16, radius=20.0, type=u'SphereBufferGeometry', widthSegments=16), material=MeshPhysicalMaterial(alphaMap=None, aoMap=None, bumpMap=None, color=u'red', defines={'PHYSICAL': ''}, displacementMap=None, emissiveMap=None, envMap=None, lightMap=None, map=None, metalnessMap=None, normalMap=None, normalScale=(1.0, 1.0), roughnessMap=None, type=u'MeshPhysicalMaterial'), position=(-20.0, 0.0, 0.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), Mesh(geometry=SphereBufferGeometry(heightSegments=16, radius=20.0, type=u'SphereBufferGeometry', widthSegments=16), material=MeshPhysicalMaterial(alphaMap=None, aoMap=None, bumpMap=None, color=u'green', defines={'PHYSICAL': ''}, displacementMap=None, emissiveMap=None, envMap=None, lightMap=None, map=None, metalnessMap=None, normalMap=None, normalScale=(1.0, 1.0), roughnessMap=None, type=u'MeshPhysicalMaterial'), position=(20.0, 0.0, 0.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), PointLight(position=(-100.0, 100.0, 100.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), AmbientLight(intensity=0.4, quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), CombinedCamera(height=400.0, position=(0.0, 0.0, 60.0), projectionMatrix=(1.4296712803397058, 0.0, 0.0, 0.0, 0.0, 2.1445069205095586, 0.0, 0.0, 0.0, 0.0, -1.00010000500025, -1.0, 0.0, 0.0, -0.200010000500025, 0.0), quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), type=u'PerspectiveCamera', up=(0.0, 1.0, 0.0), width=600.0)), fog=None, overrideMaterial=None, quaternion=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), up=(0.0, 1.0, 0.0)), shadowMap=WebGLShadowMap())"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"key_light = PointLight(position=[-100, 100, 100])\n",
"ambient_light = AmbientLight(intensity=0.4)\n",
"scene = Scene(children=[mesh1, mesh2, key_light, ambient_light, camera])\n",
"renderer = Renderer(scene=scene, camera=camera, controls=[OrbitControls(controlling=camera)],\n",
" width=view_width, height=view_height)\n",
"display(renderer)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"camera.mode = 'orthographic'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"camera.mode = 'perspective'"
]
},
{
"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.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
This diff is collapsed.
This diff is collapsed.
......@@ -4,7 +4,5 @@ except ImportError as e:
print('OpenMesh not available')
print(e)
from pythreejs import *
def test():
return "Hi!"
from .context import *
from .immediate import *
\ No newline at end of file
import pythreejs as three
import numpy as np
from IPython.display import display
class Context(object):
def __init__(self):
self.camera = three.PerspectiveCamera(position=[3,3,3], fov=20,
children=[three.DirectionalLight(color='#ffffff', position=[-3, 5, 1], intensity=0.5)])
self.scene = three.Scene(children=[three.AmbientLight(color='#dddddd')])
self.orbit_controls = three.OrbitControls(controlling=self.camera)
self.renderer = three.Renderer(camera=self.camera, background='black', background_opacity=1,
scene=self.scene, controls=[self.orbit_controls],
width=600, height=400 )
def draw_faces(self, vertices, face_indices, normals=None, per_vertex_colors=None, per_face_colors=None, shading='flat', z_offset=0):
face_indices = np.asarray(face_indices, dtype='uint16').ravel()
mesh_geom = three.BufferGeometry(attributes=dict(
position = three.BufferAttribute(vertices, normalized=False),
index = three.BufferAttribute(face_indices, normalized=False),
))
mesh_obj = three.Mesh(geometry=mesh_geom, material=three.MeshLambertMaterial(color='#dddddd'))
self.scene.add(mesh_obj)
return self
def draw_edges(self, vertices, edge_indices=None, per_vertex_colors=None, per_edge_colors=None, z_offset=1e-3):
if edge_indices is None:
edge_indices = np.arange(0, len(vertices)-1)
edge_indices = np.asarray(edge_indices, dtype='uint16')
geom = three.BufferGeometry(attributes=dict(
position = three.BufferAttribute(vertices, normalized=False),
index = three.BufferAttribute(edge_indices, normalized=False),
))
material = three.LineBasicMaterial(color='#000000')
mesh_obj = three.Line(geometry=geom, material=material)
self.scene.add(mesh_obj)
return self
def draw_vertices(self, vertices, per_vertex_colors=None, point_size=1, z_offset=0):
geometry = three.Geometry(vertices=vertices)
if per_vertex_colors is not None:
geometry.colors = per_vertex_colors
material = three.PointsMaterial(color='#000000', sizeAttenuation=False, size=point_size)
mesh_obj = three.Points(geometry=geometry, material=material)
self.scene.add(mesh_obj)
return self
def display(self):
display(self.renderer)
\ No newline at end of file
from .context import *
def display_faces(vertices, face_indices, normals=None, per_vertex_colors=None, per_face_colors=None, shading='flat', z_offset=0):
Context().draw_faces(vertices, face_indices, normals, per_vertex_colors, per_face_colors, shading, z_offset).display()
def display_edges(vertices, edge_indices=None, per_vertex_colors=None, per_edge_colors=None, z_offset=1e-3):
Context().draw_edges(vertices, edge_indices, per_vertex_colors, per_edge_colors, z_offset).display()
def display_vertices(vertices, per_vertex_colors=None, point_size=1, z_offset=0):
Context().draw_vertices(vertices, per_vertex_colors, point_size, z_offset).display()
import openmesh as om
from pythreejs import *
from .immediate import *
def display_openmesh(mesh):
meshGeom = PlainGeometry(vertices=mesh.points(), faces=mesh.face_indices())
meshObj = Mesh(geometry=meshGeom, material=LambertMaterial())
cam = PerspectiveCamera(position=[3,3,3], fov=20, children=[DirectionalLight(color='#ffffff', position=[-3, 5, 1], intensity=0.5)])
meshScene = Scene(children=[meshObj, AmbientLight(color='#dddddd')])
obc = OrbitControls(controlling=cam)
meshRenderer = Renderer(camera=cam, background='black', background_opacity=1, scene=meshScene, controls=[obc] )
display(meshRenderer)
display_faces(mesh.points(), mesh.face_indices())
def display_file(path):
m = om.TriMesh()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment