ya2 · news · projects · code · about

better arrangement of the 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 from lib.lib.p3d.gfx import P3dGfxMgr
5
6
7 class SidePanel:
8
9 def __init__(self, world, plane_node, top_l, bottom_r, y):
10 self._world = world
11 self._plane_node = plane_node
12 self._set((-1, 1), y)
13
14 def update(self, items):
15 p_from, p_to = P3dGfxMgr.world_from_to((-1, 1))
16 for hit in self._world.ray_test_all(p_from, p_to).get_hits():
17 if hit.get_node() == self._plane_node:
18 pos = hit.get_hit_pos()
19 y = 0
20 corner = -20, 20
21 for item in items:
22 if not item._instantiated:
23 bounds = item._np.get_tight_bounds()
24 if bounds[1][1] > y:
25 y = bounds[1][1]
26 icorner = item.get_corner()
27 icorner = P3dGfxMgr.screen_coord(icorner)
28 if icorner[0] > corner[0]:
29 corner = icorner[0], corner[1]
30 if icorner[1] < corner[1]:
31 corner = corner[0], icorner[1]
32 self._set((pos[0], pos[2]), y)
33 bounds = self._np.get_tight_bounds()
34 corner3d = bounds[1][0], bounds[1][1], bounds[0][2]
35 corner2d = P3dGfxMgr.screen_coord(corner3d)
36 def __update(dscale):
37 scale = self._np.get_scale()
38 self._np.set_scale(scale + dscale)
39 bounds = self._np.get_tight_bounds()
40 corner3d = bounds[1][0], bounds[1][1], bounds[0][2]
41 return P3dGfxMgr.screen_coord(corner3d)
42 while corner2d[0] < corner[0] + .01:
43 corner2d = __update((.01, 0, 0))
44 while corner2d[1] > corner[1] - .01:
45 corner2d = __update((0, 0, .01))
46
47 def _set(self, pos, y):
48 if hasattr(self, '_np'):
49 self._np.remove_node()
50 vdata = GeomVertexData('quad', GeomVertexFormat.get_v3(), Geom.UHStatic)
51 vdata.setNumRows(2)
52 vertex = GeomVertexWriter(vdata, 'vertex')
53 vertex.add_data3(.5, 0, -.5)
54 vertex.add_data3(.5, 0, .5)
55 vertex.add_data3(-.5, 0, .5)
56 vertex.add_data3(-.5, 0, -.5)
57 prim = GeomTriangles(Geom.UHStatic)
58 prim.add_vertices(0, 1, 2)
59 prim.add_vertices(0, 2, 3)
60 prim.close_primitive()
61 geom = Geom(vdata)
62 geom.add_primitive(prim)
63 node = GeomNode('gnode')
64 node.add_geom(geom)
65 self._np = render.attach_new_node(node)
66 self._np.setTransparency(True)
67 self._np.set_pos(pos[0], y, pos[1])
68 vert = '''\
69 #version 150
70 uniform mat4 p3d_ModelViewProjectionMatrix;
71 in vec4 p3d_Vertex;
72 void main() {
73 gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex; }'''
74 frag = '''\
75 #version 150
76 out vec4 p3d_FragColor;
77 void main() {
78 p3d_FragColor = vec4(.04, .04, .04, .08); }'''
79 self._np.set_shader(Shader.make(Shader.SL_GLSL, dedent(vert), dedent(frag)))
80 # mat = Material()
81 # mat.set_base_color((1, 1, 1, 1))
82 # mat.set_emission((1, 1, 1, 1))
83 # mat.set_metallic(.5)
84 # mat.set_roughness(.5)
85 # np.set_material(mat)
86 # texture_sz = 64
87 # base_color_pnm = PNMImage(texture_sz, texture_sz)
88 # base_color_pnm.fill(.1, .1, .1)
89 # base_color_pnm.add_alpha()
90 # base_color_pnm.alpha_fill(.04)
91 # base_color_tex = Texture('base color')
92 # base_color_tex.load(base_color_pnm)
93 # ts = TextureStage('base color')
94 # ts.set_mode(TextureStage.M_modulate)
95 # np.set_texture(ts, base_color_tex)
96 # emission_pnm = PNMImage(texture_sz, texture_sz)
97 # emission_pnm.fill(0, 0, 0)
98 # emission_tex = Texture('emission')
99 # emission_tex.load(emission_pnm)
100 # ts = TextureStage('emission')
101 # ts.set_mode(TextureStage.M_emission)
102 # np.set_texture(ts, emission_tex)
103 # metal_rough_pnm = PNMImage(texture_sz, texture_sz)
104 # ambient_occlusion = 1
105 # roughness = .5
106 # metallicity = .5
107 # metal_rough_pnm.fill(ambient_occlusion, roughness, metallicity)
108 # metal_rough_tex = Texture('ao metal roughness')
109 # metal_rough_tex.load(metal_rough_pnm)
110 # ts = TextureStage('ao metal roughness')
111 # ts.set_mode(TextureStage.M_selector)
112 # np.set_texture(ts, metal_rough_tex)
113 # normal_pnm = PNMImage(texture_sz, texture_sz)
114 # normal_pnm.fill(.5, .5, .1)
115 # normal_tex = Texture('normals')
116 # normal_tex.load(normal_pnm)
117 # ts = TextureStage('normals')
118 # ts.set_mode(TextureStage.M_normal)
119 # np.set_texture(ts, normal_tex)