Commit a8559d26 authored by Dario Seyb's avatar Dario Seyb

started working on clipping planes, but for some reason pythreejs doesn't do...

started working on clipping planes, but for some reason pythreejs doesn't do what I'd expect from ThreeJS. Can't get the clipping to actually activate.
parent c82b0d4c
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import meshvis\n",
"import openmesh as om\n",
"import numpy as np\n",
"\n",
"m = om.PolyMesh()\n",
"\n",
"om.read_mesh(m, 'models/bunny.obj')\n",
"\n",
"vertices = m.points()\n",
"faces = m.face_vertex_indices()\n",
"\n",
"\n",
"mesh = meshvis.Mesh(vertices, faces, \n",
" normals = meshvis.calculateFaceNormals(vertices, faces))\n",
"\n",
"meshvis.display(mesh, shading='flat', clipping_planes=[meshvis.Plane((1.0, 0.0, 0.0) , 0)])"
]
}
],
"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"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
...@@ -11,4 +11,4 @@ from .immediate import * ...@@ -11,4 +11,4 @@ from .immediate import *
from .indexed_attribute import UniformAttribute, PointAttribute, FaceAttribute, HalfEdgeAttribute from .indexed_attribute import UniformAttribute, PointAttribute, FaceAttribute, HalfEdgeAttribute
from .mesh_helper import * from .mesh_helper import *
from .mesh import Mesh, EdgeList, PointList from .mesh import Mesh, EdgeList, PointList
from pythreejs import ImageTexture, DataTexture from pythreejs import ImageTexture, DataTexture, Plane
...@@ -34,14 +34,14 @@ class Context(object): ...@@ -34,14 +34,14 @@ class Context(object):
self.renderer = three.Renderer(camera=self.camera, background=background_color, background_opacity=1, self.renderer = three.Renderer(camera=self.camera, background=background_color, background_opacity=1,
scene=self.scene, controls=[self.orbit_controls, self.click_picker], scene=self.scene, controls=[self.orbit_controls, self.click_picker],
width=width, height=height, antialias=True ) width=width, height=height, antialias=True, localClippingEnabled=True )
def on_picked(change): def on_picked(change):
self.orbit_controls.target = change['new'] self.orbit_controls.target = change['new']
self.click_picker.observe(on_picked, names=['point']) self.click_picker.observe(on_picked, names=['point'])
def draw(self, obj, shading='flat', z_offset=0.5, texture=None, point_size = 1, perspective = False, line_width = 1): def draw(self, obj, shading='flat', z_offset=0.5, texture=None, point_size = 1, perspective = False, line_width = 1, clipping_planes = []):
""" """
Draw the given object. Not all named parameters are relevant for all object types. Draw the given object. Not all named parameters are relevant for all object types.
""" """
...@@ -49,18 +49,18 @@ class Context(object): ...@@ -49,18 +49,18 @@ class Context(object):
obj.prepare_render() obj.prepare_render()
if isinstance(obj, Mesh): if isinstance(obj, Mesh):
return self.draw_faces(obj.vertices, obj.tri_face_indices, obj.normals, obj.colors, obj.uvs, shading, z_offset, texture) return self.draw_faces(obj.vertices, obj.tri_face_indices, obj.normals, obj.colors, obj.uvs, shading, z_offset, texture, clipping_planes)
elif isinstance(obj, EdgeList): elif isinstance(obj, EdgeList):
return self.draw_edges(obj.vertices, obj.edge_indices, obj.colors, obj.uvs, z_offset, texture, line_width) return self.draw_edges(obj.vertices, obj.edge_indices, obj.colors, obj.uvs, z_offset, texture, line_width, clipping_planes)
elif isinstance(obj, PointList): elif isinstance(obj, PointList):
return self.draw_vertices(obj.vertices, obj.colors, obj.uvs, point_size, z_offset, texture, perspective) return self.draw_vertices(obj.vertices, obj.colors, obj.uvs, point_size, z_offset, texture, perspective, clipping_planes)
return self return self
def draw_faces(self, vertices, face_indices, normals=None, colors=None, uvs=None, def draw_faces(self, vertices, face_indices, normals=None, colors=None, uvs=None,
shading='flat', z_offset=0.5, texture=None): shading='flat', z_offset=0.5, texture=None, clipping_planes = []):
""" """
Draw a triangle mesh described by a list of vertex positions and face indices. Draw a triangle mesh described by a list of vertex positions and face indices.
face_indices is expected to be a n x 3 matrix. face_indices is expected to be a n x 3 matrix.
...@@ -79,6 +79,10 @@ class Context(object): ...@@ -79,6 +79,10 @@ class Context(object):
elif shading == 'phong': elif shading == 'phong':
mat = three.MeshPhongMaterial(color='#dddddd') mat = three.MeshPhongMaterial(color='#dddddd')
if len(clipping_planes) != 0:
mat.clipShadows = True
mat.clippingPlanes = tuple(clipping_planes)
mat.wireframe = shading is 'wireframe' mat.wireframe = shading is 'wireframe'
mat.map = texture mat.map = texture
...@@ -127,16 +131,20 @@ class Context(object): ...@@ -127,16 +131,20 @@ class Context(object):
self.scene.add(mesh_obj) self.scene.add(mesh_obj)
return self return self
def draw_edges(self, vertices, edge_indices=None, colors=None, uvs=None, z_offset=0, texture=None, linewidth=1): def draw_edges(self, vertices, edge_indices=None, colors=None, uvs=None, z_offset=0, texture=None, line_width=1, clipping_planes = []):
""" """
Draw a list of edges described by a list of vertex positions and edge indices. Draw a list of edges described by a list of vertex positions and edge indices.
edge_indices is expected to be a n x 2 matrix. If no indices are given, a continous line is connecting the vertices is drawn. edge_indices is expected to be a n x 2 matrix. If no indices are given, a continous line is connecting the vertices is drawn.
""" """
mat = three.LineBasicMaterial(color='#000000') mat = three.LineBasicMaterial(color='#000000')
mat.linewidth = linewidth mat.linewidth = line_width
mat.map = texture mat.map = texture
if len(clipping_planes) != 0:
mat.clipShadows = True
mat.clippingPlanes = clipping_planes
if z_offset is not 0: if z_offset is not 0:
mat.polygonOffset = True mat.polygonOffset = True
mat.polygonOffsetFactor = z_offset mat.polygonOffsetFactor = z_offset
...@@ -179,7 +187,7 @@ class Context(object): ...@@ -179,7 +187,7 @@ class Context(object):
self.scene.add(mesh_obj) self.scene.add(mesh_obj)
return self return self
def draw_vertices(self, vertices, colors=None, uvs=None, point_size=1, z_offset=0, texture=None, perspective=False): def draw_vertices(self, vertices, colors=None, uvs=None, point_size=1, z_offset=0, texture=None, perspective=False, clipping_planes = []):
""" """
Draw a list of unconnected vertices. Draw a list of unconnected vertices.
""" """
...@@ -201,6 +209,10 @@ class Context(object): ...@@ -201,6 +209,10 @@ class Context(object):
mat = three.PointsMaterial(color=matColor, sizeAttenuation=perspective, size=point_size) mat = three.PointsMaterial(color=matColor, sizeAttenuation=perspective, size=point_size)
mat.map = texture mat.map = texture
if len(clipping_planes) != 0:
mat.clipShadows = True
mat.clippingPlanes = clipping_planes
if z_offset is not 0: if z_offset is not 0:
mat.polygonOffset = True mat.polygonOffset = True
mat.polygonOffsetFactor = z_offset mat.polygonOffsetFactor = z_offset
......
...@@ -6,20 +6,20 @@ from .context import Context ...@@ -6,20 +6,20 @@ from .context import Context
def display_faces(vertices, face_indices, normals=None, colors=None, uvs=None, def display_faces(vertices, face_indices, normals=None, colors=None, uvs=None,
shading='flat', z_offset=0.5, texture=None, width=600, height=400, shading='flat', z_offset=0.5, texture=None, width=600, height=400,
background_color = '#dddddd'): background_color = '#dddddd', clipping_planes = []):
Context(width, height, background_color).draw_faces(vertices, face_indices, normals, colors, uvs, shading, z_offset, texture).display() Context(width, height, background_color).draw_faces(vertices, face_indices, normals, colors, uvs, shading, z_offset, texture, clipping_planes).display()
def display_edges(vertices, edge_indices=None, colors=None, uvs=None, def display_edges(vertices, edge_indices=None, colors=None, uvs=None,
z_offset=0, texture=None, linewidth=1, width=600, height=400, z_offset=0, texture=None, line_width=1, width=600, height=400,
background_color = '#dddddd'): background_color = '#dddddd', clipping_planes = []):
Context(width, height, background_color).draw_edges(vertices, edge_indices, colors, uvs, z_offset, texture, linewidth).display() Context(width, height, background_color).draw_edges(vertices, edge_indices, colors, uvs, z_offset, texture, line_width, clipping_planes).display()
def display_vertices(vertices, colors=None, uvs=None, point_size=1, z_offset=0, texture=None, def display_vertices(vertices, colors=None, uvs=None, point_size=1, z_offset=0, texture=None,
perspective=False, width=600, height=400, perspective=False, width=600, height=400,
background_color = '#dddddd'): background_color = '#dddddd', clipping_planes = []):
Context(width, height, background_color).draw_vertices(vertices, colors, uvs, point_size, z_offset, texture, perspective).display() Context(width, height, background_color).draw_vertices(vertices, colors, uvs, point_size, z_offset, texture, perspective, clipping_planes).display()
def display(obj, shading='flat', point_size=1, z_offset=0, texture=None, def display(obj, shading='flat', point_size=1, z_offset=0, texture=None,
perspective=False, width=600, height=400, perspective=False, width=600, height=400,
background_color = '#dddddd'): background_color = '#dddddd', clipping_planes = []):
Context(width, height, background_color).draw(obj, shading= shading, point_size = point_size, z_offset= z_offset, texture = texture, perspective = perspective).display() Context(width, height, background_color).draw(obj, shading= shading, point_size = point_size, z_offset= z_offset, texture = texture, perspective = perspective, clipping_planes = clipping_planes).display()
\ No newline at end of file \ No newline at end of file
...@@ -48,7 +48,7 @@ class Mesh(): ...@@ -48,7 +48,7 @@ class Mesh():
def triangulate(self): def triangulate(self):
if self.is_triangulated: if self.is_triangulated:
self.tri_face_indices = self.face_indices self.tri_face_indices = self.face_indices
self.triangulate_face_to_attribute_face = np.range(len(self.face_indices)) self.triangulate_face_to_attribute_face = np.arange(len(self.face_indices))
return return
new_faces = [] new_faces = []
......
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