| 1 | import argparse |
| 2 | import simplepbr |
| 3 | import gltf |
| 4 | from sys import platform, argv |
| 5 | from logging import info |
| 6 | from os.path import exists |
| 7 | from os import makedirs |
| 8 | from panda3d.core import Filename, load_prc_file_data, AntialiasAttrib |
| 9 | from panda3d.bullet import BulletWorld, BulletDebugNode |
| 10 | from direct.showbase.ShowBase import ShowBase |
| 11 | from pmachines.scene import Scene |
| 12 | |
| 13 | |
| 14 | class Pmachines: |
| 15 | |
| 16 | def __init__(self): |
| 17 | self._configure() |
| 18 | self.base = ShowBase() |
| 19 | info('platform: %s' % platform) |
| 20 | info('exists main.py: %s' % exists('main.py')) |
| 21 | self._prepare_window() |
| 22 | args = self._parse_args() |
| 23 | self.updating = args.update |
| 24 | self.version = args.version |
| 25 | if args.update: |
| 26 | return |
| 27 | self._set_physics() |
| 28 | Scene(self.world) |
| 29 | |
| 30 | def _configure(self): |
| 31 | load_prc_file_data('', 'window-title pmachines') |
| 32 | load_prc_file_data('', 'framebuffer-multisample 1') |
| 33 | load_prc_file_data('', 'multisamples 4') |
| 34 | load_prc_file_data('', 'framebuffer-srgb true') |
| 35 | |
| 36 | def _parse_args(self): |
| 37 | parser = argparse.ArgumentParser() |
| 38 | parser.add_argument('--update', action='store_true') |
| 39 | parser.add_argument('--version', action='store_true') |
| 40 | cmd_line = [arg for arg in iter(argv[1:]) if not arg.startswith('-psn_')] |
| 41 | args = parser.parse_args(cmd_line) |
| 42 | return args |
| 43 | |
| 44 | def _prepare_window(self): |
| 45 | data_path = '' |
| 46 | if (platform.startswith('win') or platform.startswith('linux')) and ( |
| 47 | not exists('main.py') or __file__.startswith('/app/bin/')): |
| 48 | # it is the deployed version for windows |
| 49 | data_path = str(Filename.get_user_appdata_directory()) + '/pmachines' |
| 50 | home = '/home/flavio' # we must force this for wine |
| 51 | if data_path.startswith('/c/users/') and exists(home + '/.wine/'): |
| 52 | data_path = home + '/.wine/drive_' + data_path[1:] |
| 53 | info('creating dirs: %s' % data_path) |
| 54 | makedirs(data_path, exist_ok=True) |
| 55 | gltf.patch_loader(base.loader) |
| 56 | pipeline = simplepbr.init( |
| 57 | use_normal_maps=True, |
| 58 | use_emission_maps=False, |
| 59 | use_occlusion_maps=True, |
| 60 | msaa_samples=4) |
| 61 | render.setAntialias(AntialiasAttrib.MAuto) |
| 62 | self.base.set_background_color(0, 0, 0, 1) |
| 63 | self.base.disable_mouse() |
| 64 | |
| 65 | def _set_physics(self): |
| 66 | debug_node = BulletDebugNode('Debug') |
| 67 | debug_node.show_wireframe(True) |
| 68 | debug_node.show_constraints(True) |
| 69 | debug_node.show_bounding_boxes(True) |
| 70 | debug_node.show_normals(True) |
| 71 | debug_np = render.attach_new_node(debug_node) |
| 72 | debug_np.show() |
| 73 | self.world = BulletWorld() |
| 74 | self.world.set_gravity((0, 0, -9.81)) |
| 75 | self.world.set_debug_node(debug_np.node()) |
| 76 | def update(task): |
| 77 | dt = globalClock.get_dt() |
| 78 | self.world.do_physics(dt) |
| 79 | return task.cont |
| 80 | taskMgr.add(update, 'update') |