40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
import numpy as np
|
|
from vispy import app, scene
|
|
from vispy.scene.visuals import Mesh
|
|
from vispy.visuals.filters import ShadingFilter, WireframeFilter
|
|
from utils.files.input import ScannedObject
|
|
|
|
|
|
def render3D(obj:ScannedObject,show:bool=True):
|
|
"""
|
|
Render a 3D model using vispy
|
|
:param obj: A ScannedObject to be rendered
|
|
"""
|
|
vertices = np.asarray(obj.get_vertices())
|
|
faces = np.asarray(obj.get_faces())
|
|
canvas = scene.SceneCanvas(keys='interactive', bgcolor='white')
|
|
view = canvas.central_widget.add_view()
|
|
view.camera = 'arcball'
|
|
view.camera.depth_value = 1e3
|
|
color = (0.3, 0.5, 0.8)
|
|
mesh = Mesh(vertices, faces, color=color)
|
|
view.add(mesh)
|
|
wireframe_filter = WireframeFilter(width=0)
|
|
shading_filter = ShadingFilter(shininess=0)
|
|
mesh.attach(wireframe_filter)
|
|
mesh.attach(shading_filter)
|
|
|
|
def attach_headlight(view):
|
|
light_dir = (0, 1, 0, 0)
|
|
shading_filter.light_dir = light_dir[:3]
|
|
initial_light_dir = view.camera.transform.imap(light_dir)
|
|
@view.scene.transform.changed.connect
|
|
def on_transform_change(event):
|
|
transform = view.camera.transform
|
|
shading_filter.light_dir = transform.map(initial_light_dir)[:3]
|
|
attach_headlight(view)
|
|
if show:
|
|
canvas.show()
|
|
app.run()
|
|
else:
|
|
return canvas |