Commit 174dacff authored by Dario Seyb's avatar Dario Seyb

better shading

parent d950e0e6
This diff is collapsed.
......@@ -3,33 +3,53 @@ import numpy as np
from IPython.display import display
class Context(object):
def __init__(self):
def __init__(self, width=600, height=400, background_color = '#dddddd'):
self.camera = three.PerspectiveCamera(position=[3,3,3], fov=20,
children=[three.DirectionalLight(color='#ffffff', position=[-3, 5, 1], intensity=0.5)])
self.camera.aspect = width/float(height)
self.scene = three.Scene(children=[three.AmbientLight(color='#dddddd')])
self.scene.background = '#dddddd'
self.scene.background = background_color
self.orbit_controls = three.OrbitControls(controlling=self.camera)
self.renderer = three.Renderer(camera=self.camera, background='black', background_opacity=1,
self.renderer = three.Renderer(camera=self.camera, background=background_color, background_opacity=1,
scene=self.scene, controls=[self.orbit_controls],
width=600, height=400 )
width=width, height=height, antialias=True )
def draw_faces(self, vertices, face_indices, normals=None, per_vertex_colors=None, per_face_colors=None,
hidden=False, shading='flat', z_offset=0.5):
shading='flat', z_offset=0.5):
mesh_geom = three.BufferGeometry(attributes=dict(
attributes=dict(
position = three.BufferAttribute(np.asarray(vertices, dtype=np.float32), normalized=False),
index = three.BufferAttribute(np.asarray(face_indices, dtype='uint16').ravel(), normalized=False),
))
index = three.BufferAttribute(np.asarray(face_indices, dtype=np.uint16).ravel(), normalized=False),
)
mat = three.MeshLambertMaterial(color='#dddddd', colorWrite=not hidden)
mat = three.MeshLambertMaterial(color='#dddddd', colorWrite=shading is not 'hidden')
mat.wireframe = shading is 'wireframe'
if z_offset is not 0:
mat.polygonOffset = True
mat.polygonOffsetFactor = z_offset
mat.polygonOffsetUnits = 0.1
colors = None
if per_vertex_colors is not None:
mat.vertexColors = 'VertexColors'
colors = per_vertex_colors
if per_face_colors is not None:
mat.vertexColors = 'FaceColors'
colors = per_face_colors
if colors is not None:
mat.color = 'white'
attributes['color'] = three.BufferAttribute(np.asarray(colors, dtype=np.float32))
mesh_geom = three.BufferGeometry(attributes=attributes)
mesh_obj = three.Mesh(geometry=mesh_geom, material=mat)
self.scene.add(mesh_obj)
......@@ -52,6 +72,20 @@ class Context(object):
mat.polygonOffsetFactor = z_offset
mat.polygonOffsetUnits = 0.1
colors = None
if per_vertex_colors is not None:
mat.vertexColors = 'VertexColors'
colors = per_vertex_colors
if per_edge_colors is not None:
mat.vertexColors = 'FaceColors'
colors = per_edge_colors
if colors is not None:
mat.color = 'white'
attributes['color'] = three.BufferAttribute(np.asarray(colors, dtype=np.float32))
geom = three.BufferGeometry(attributes = attributes)
mesh_obj = three.LineSegments(geometry=geom, material=mat)
......
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