Commit 3b9dd711 authored by Dario Seyb's avatar Dario Seyb

hooked up attributes to context functions

parent bbc3901e
This diff is collapsed.
......@@ -7,4 +7,4 @@ except ImportError as error:
from .context import *
from .immediate import *
from .indexed_attribute import UniformAttribute, PointAttribute, FaceAttribute, HalfEdgeAttribute, resolve_attributes
from .indexed_attribute import UniformAttribute, PointAttribute, FaceAttribute, HalfEdgeAttribute
......@@ -2,6 +2,7 @@
import pythreejs as three
import numpy as np
from IPython.display import display
from .indexed_attribute import *
class Context(object):
def __init__(self, width=600, height=400, background_color = '#dddddd'):
......@@ -18,16 +19,13 @@ class Context(object):
scene=self.scene, controls=[self.orbit_controls],
width=width, height=height, antialias=True )
def draw_faces(self, vertices, face_indices, normals=None, per_vertex_colors=None, per_face_colors=None,
def draw_faces(self, vertices, face_indices, normals=None, colors=None, uvs=None,
shading='flat', z_offset=0.5):
attributes=dict(
position = three.BufferAttribute(np.asarray(vertices, dtype=np.float32), normalized=False),
index = three.BufferAttribute(np.asarray(face_indices, dtype=np.uint16).ravel(), normalized=False),
)
face_indices = np.array(face_indices)
# Setup Material
mat = three.MeshLambertMaterial(color='#dddddd', colorWrite=shading is not 'hidden')
mat.wireframe = shading is 'wireframe'
if z_offset is not 0:
......@@ -35,19 +33,31 @@ 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_face_colors is not None:
mat.vertexColors = 'FaceColors'
colors = per_face_colors
# Resolve the given attributes, it's ok if they are None
resolved_attribs = resolve_attributes(face_indices, [normals, colors, uvs])
resolved_normals = resolved_attribs[0]
resolved_colors = resolved_attribs[1]
resolved_uvs = resolved_attribs[2]
# De-index our vertices
vertices, face_indices = stretch_vertices(vertices, face_indices)
if colors is not None:
# Create attributes dictionary, always use at least position and index
attributes=dict(
position = three.BufferAttribute(np.asarray(vertices, dtype=np.float32), normalized=False),
index = three.BufferAttribute(np.asarray(face_indices, dtype=np.uint16).ravel(), normalized=False),
)
if resolved_colors is not None:
mat.vertexColors = 'VertexColors'
mat.color = 'white'
attributes['color'] = three.BufferAttribute(np.asarray(colors, dtype=np.float32))
attributes['color'] = three.BufferAttribute(np.asarray(resolved_colors, dtype=np.float32))
if resolved_normals is not None:
attributes['normal'] = three.BufferAttribute(np.asarray(resolved_normals, dtype=np.float32))
if resolved_uvs is not None:
attributes['uv'] = three.BufferAttribute(np.asarray(resolved_uvs, dtype=np.float32))
mesh_geom = three.BufferGeometry(attributes=attributes)
......@@ -56,15 +66,7 @@ class Context(object):
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=0):
if edge_indices is None:
edge_indices = zip(np.arange(0, len(vertices)-1), np.arange(1, len(vertices)))
attributes=dict(
position = three.BufferAttribute(np.asarray(vertices, dtype=np.float32), normalized=False),
index = three.BufferAttribute(np.asarray(edge_indices, dtype=np.uint16).ravel(), normalized=False),
)
def draw_edges(self, vertices, edge_indices=None, colors=None, uvs=None, z_offset=0):
mat = three.LineBasicMaterial(color='#000000')
......@@ -73,19 +75,28 @@ class Context(object):
mat.polygonOffsetFactor = z_offset
mat.polygonOffsetUnits = 0.1
colors = None
if edge_indices is None:
edge_indices = zip(np.arange(0, len(vertices)-1), np.arange(1, len(vertices)))
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:
resolved_attribs = resolve_attributes(edge_indices, [colors, uvs])
resolved_colors = resolved_attribs[0]
resolved_uvs = resolved_attribs[1]
vertices, edge_indices = stretch_vertices(vertices, edge_indices)
attributes=dict(
position = three.BufferAttribute(np.asarray(vertices, dtype=np.float32), normalized=False),
index = three.BufferAttribute(np.asarray(edge_indices, dtype=np.uint16).ravel(), normalized=False),
)
if resolved_colors is not None:
mat.vertexColors = 'VertexColors'
mat.color = 'white'
attributes['color'] = three.BufferAttribute(np.asarray(colors, dtype=np.float32))
attributes['color'] = three.BufferAttribute(np.asarray(resolved_colors, dtype=np.float32))
if resolved_uvs is not None:
attributes['uv'] = three.BufferAttribute(np.asarray(resolved_uvs, dtype=np.float32))
geom = three.BufferGeometry(attributes = attributes)
......
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_faces(vertices, face_indices, normals=None, colors=None, uvs=None, shading='flat', z_offset=0.5):
Context().draw_faces(vertices, face_indices, normals, colors, uvs, 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_edges(vertices, edge_indices=None, colors=None, uvs=None, z_offset=0):
Context().draw_edges(vertices, edge_indices, colors, uvs, z_offset).display()
def display_vertices(vertices, colors=None, point_size=1, z_offset=0):
Context().draw_vertices(vertices, colors, point_size, z_offset).display()
def display_vertices(vertices, colors=None, point_size=1, z_offset=0, perspective=False):
Context().draw_vertices(vertices, colors, point_size, z_offset, perspective).display()
import numpy as np
def stretch_vertices(vertices, face_indices):
stretched_verts = []
for face in face_indices:
for vidx in face:
stretched_verts.append(vertices[vidx])
return stretched_verts, range(0, len(stretched_verts))
def resolve_attributes(face_indices, attribs):
final_attributes = []
for attrib in attribs:
if attrib is None:
final_attributes.append(None)
continue
resolved_attrib = attrib.resolve(face_indices)
final_attributes.append(stretch_attribute(face_indices, resolved_attrib))
......@@ -19,11 +33,10 @@ def stretch_attribute(face_indices, resolved_attrib):
return stretched_attrib
def find(element, list_element):
try:
index_element = list_element.index(element)
return index_element
except ValueError:
return None
index_element = np.where(list_element==element)
if len(index_element[0]) > 0: return index_element[0][0]
return None
class UniformAttribute(object):
def __init__(self, value):
......@@ -39,7 +52,7 @@ class UniformAttribute(object):
class IndexedAttribute(object):
def __init__(self, values, indices):
assert(len(values) is len(indices))
assert(len(values) == len(indices))
self.values = values
self.indices = indices
......
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