ya2 · news · projects · code · about

side panel
[pmachines.git] / pmachines / sidepanel.py
1 from textwrap import dedent
2 from panda3d.core import GeomVertexData, GeomVertexFormat, Geom, \
3 GeomVertexWriter, GeomTriangles, GeomNode, Shader, Point3
4
5
6 class SidePanel:
7
8 def __init__(self, world, plane_node, top_l, bottom_r, y):
9 self._world = world
10 self._plane_node = plane_node
11 self._set(top_l, bottom_r, y)
12
13 def update(self, items):
14 p_from, p_to = Point3(), Point3() # in camera coordinates
15 base.camLens.extrude((-1, 1), p_from, p_to)
16 p_from = render.get_relative_point(base.cam, p_from) # global coords
17 p_to = render.get_relative_point(base.cam, p_to) # global coords
18 for hit in self._world.ray_test_all(p_from, p_to).get_hits():
19 if hit.get_node() == self._plane_node:
20 pos = hit.get_hit_pos()
21 top_l = pos[0] - .8, pos[2] + .8
22 bottom_r = pos[0], pos[2]
23 y = 1
24 for item in items:
25 if not item._instantiated:
26 bounds = item._np.get_tight_bounds()
27 if bounds[1][0] > bottom_r[0]:
28 bottom_r = bounds[1][0], bottom_r[1]
29 if bounds[0][2] < bottom_r[1]:
30 bottom_r = bottom_r[0], bounds[0][2]
31 if bounds[1][1] > y:
32 y = bounds[1][1]
33 bottom_r = bottom_r[0] + .3, bottom_r[1] - .3
34 self._set(top_l, bottom_r, y)
35
36 def _set(self, top_l, bottom_r, y):
37 if hasattr(self, '_np'):
38 self._np.remove_node()
39 vdata = GeomVertexData('quad', GeomVertexFormat.get_v3(), Geom.UHStatic)
40 vdata.setNumRows(2)
41 vertex = GeomVertexWriter(vdata, 'vertex')
42 vertex.add_data3(bottom_r[0], y, bottom_r[1])
43 vertex.add_data3(bottom_r[0], y, top_l[1])
44 vertex.add_data3(top_l[0], y, top_l[1])
45 vertex.add_data3(top_l[0], y, bottom_r[1])
46 prim = GeomTriangles(Geom.UHStatic)
47 prim.add_vertices(0, 1, 2)
48 prim.add_vertices(0, 2, 3)
49 prim.close_primitive()
50 geom = Geom(vdata)
51 geom.add_primitive(prim)
52 node = GeomNode('gnode')
53 node.add_geom(geom)
54 self._np = render.attach_new_node(node)
55 self._np.setTransparency(True)
56 vert = '''\
57 #version 150
58 uniform mat4 p3d_ModelViewProjectionMatrix;
59 in vec4 p3d_Vertex;
60 void main() {
61 gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex; }'''
62 frag = '''\
63 #version 150
64 out vec4 p3d_FragColor;
65 void main() {
66 p3d_FragColor = vec4(.04, .04, .04, .06); }'''
67 self._np.set_shader(Shader.make(Shader.SL_GLSL, dedent(vert), dedent(frag)))
68 # mat = Material()
69 # mat.set_base_color((1, 1, 1, 1))
70 # mat.set_emission((1, 1, 1, 1))
71 # mat.set_metallic(.5)
72 # mat.set_roughness(.5)
73 # np.set_material(mat)
74 # texture_sz = 64
75 # base_color_pnm = PNMImage(texture_sz, texture_sz)
76 # base_color_pnm.fill(.1, .1, .1)
77 # base_color_pnm.add_alpha()
78 # base_color_pnm.alpha_fill(.04)
79 # base_color_tex = Texture('base color')
80 # base_color_tex.load(base_color_pnm)
81 # ts = TextureStage('base color')
82 # ts.set_mode(TextureStage.M_modulate)
83 # np.set_texture(ts, base_color_tex)
84 # emission_pnm = PNMImage(texture_sz, texture_sz)
85 # emission_pnm.fill(0, 0, 0)
86 # emission_tex = Texture('emission')
87 # emission_tex.load(emission_pnm)
88 # ts = TextureStage('emission')
89 # ts.set_mode(TextureStage.M_emission)
90 # np.set_texture(ts, emission_tex)
91 # metal_rough_pnm = PNMImage(texture_sz, texture_sz)
92 # ambient_occlusion = 1
93 # roughness = .5
94 # metallicity = .5
95 # metal_rough_pnm.fill(ambient_occlusion, roughness, metallicity)
96 # metal_rough_tex = Texture('ao metal roughness')
97 # metal_rough_tex.load(metal_rough_pnm)
98 # ts = TextureStage('ao metal roughness')
99 # ts.set_mode(TextureStage.M_selector)
100 # np.set_texture(ts, metal_rough_tex)
101 # normal_pnm = PNMImage(texture_sz, texture_sz)
102 # normal_pnm.fill(.5, .5, .1)
103 # normal_tex = Texture('normals')
104 # normal_tex.load(normal_pnm)
105 # ts = TextureStage('normals')
106 # ts.set_mode(TextureStage.M_normal)
107 # np.set_texture(ts, normal_tex)