def build(self): layout = FloatLayout() # Create renderer. self.renderer = Renderer() # Create scene. scene = Scene() # Create default cube for scene. cube_geo = BoxGeometry(1, 1, 1) cube_mat = Material() self.cube = Mesh(geometry=cube_geo, material=cube_mat) self.cube.pos.z = -5 # Create camera for scene. self.camera = PerspectiveCamera( fov=75, # distance from the screen aspect=0, # "screen" ratio near=1, #nearest rendered point far=10 # farthest rendered point ) # Start rendering the scene and camera. scene.add(self.cube) self.renderer.render(scene, self.camera) # Set renderer ratio if its size changes. # e.g. when added to parent self.renderer.bind(size=self._adjust_aspect) layout.add_widget(self.renderer) Clock.schedule_interval(self.rotate_cube, 0.01) return layout
def test_load_scene(self): scene = Scene() loader = WaveLoader() obj_path = os.path.join(this_dir, 'testnurbs.obj') objects = loader.load(obj_path) scene.add(*objects) self.assertEqual(len(scene.children), 4)
def build(self): root = FloatLayout() self.renderer = Renderer(shader_file='examples/simple.glsl') scene = Scene() camera = PerspectiveCamera(30, 1, 1, 1000) # load obj file loader = OBJLoader() obj_path = os.path.join(os.path.dirname(__file__), "cube.obj") obj = loader.load(obj_path) cube = obj.children[0] #scene.add(*obj.children) #for obj in scene.children: scene.add(cube) cube.pos.z = -20 cube.rot.y = -45 cube.rot.x = 45 cube.material.specular = .35, .35, .35 # set colors to 3d objects scene.children[0].material.color = 0., .7, 0. # green scene.children[0].material.diffuse = 0., .7, 0. # green self.renderer.render(scene, camera) root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) return root
def build(self): self.renderer = Renderer(shader_file=shader_file) scene = Scene() camera = PerspectiveCamera(45, 1, 0.1, 2500) self.renderer.set_clear_color((.2, .2, .2, 1.)) self.camera = camera self.renderer.main_light.intensity = 5000 root = ObjectTrackball(camera, 10) geometry = PrismGeometry(radius=1, height=1, segments=64) material = Material(color=(1., 1., 1.), diffuse=(1., 1., 1.), specular=(.35, .35, .35), shininess=200, transparency=0.8) obj = Mesh(geometry, material) scene.add(obj) # create a grid on the xz plane geometry = GridGeometry(size=(30, 30), spacing=1) material = Material(color=(1., 1., 1.), diffuse=(1., 1., 1.), specular=(.35, .35, .35), transparency=.1) lines = Lines(geometry, material) lines.rotation.x = 90 scene.add(lines) self.renderer.render(scene, camera) self.renderer.main_light.intensity = 500 root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) return root
def build(self): layout = FloatLayout() # create renderer self.renderer = Renderer() # create scene scene = Scene() # create default cube for scene cube_geo = my.Msh('sphere.msh',name='zob') cube_mat = Material() self.cube = Mesh(geometry = cube_geo, material = cube_mat) # default pos == (0,0,0) self.cube.pos.z = 0 # create camera for scene self.camera = PerspectiveCamera( fov = 75, #distance from the screen aspect=0, # "screen" ratio near=1, # nearest rendered point far=100 # furthest rendered point ) # start rendering the scene and camera scene.add(self.cube) self.renderer.render(scene,self.camera) # set renderer ratio if its size changes # e. g. when added to parent self.renderer.bind(size=self._adjust_aspect) layout.add_widget(self.renderer) Clock.schedule_interval(self.rotate_cube, .01) return layout
def _setup_object(self): self.clear_widgets() if is_ios(): # TODO enable this when iOS bug is resolved return shader_file = resource_find(os.path.join('resource', 'models', 'shaders.glsl')) obj_path = resource_find(self.model_path) self.renderer = Renderer(shader_file=shader_file) scene = Scene() camera = PerspectiveCamera(15, 1, 1, 1000) loader = OBJLoader() obj = loader.load(obj_path) scene.add(*obj.children) for obj in scene.children: obj.pos.x = self.position_x obj.pos.y = self.position_y obj.pos.z = self.position_z obj.rotation.x = self.rotation_x obj.rotation.y = self.rotation_y obj.rotation.z = self.rotation_z obj.material.specular = .85, .85, .85 obj.material.color = 1.0, 1.0, 1.0 obj.material.diffuse = 0.5, 0.5, 0.5 obj.material.transparency = 1.0 obj.material.intensity = 0.5 self.imu_obj = obj # obj.material.shininess = 1.0 self.renderer.render(scene, camera) self.renderer.bind(size=self._adjust_aspect) Clock.schedule_once(lambda dt: self.add_widget(self.renderer))
def build(self): root = FloatLayout() self.renderer = Renderer() scene = Scene() camera = PerspectiveCamera(30, 1, 1, 1000) # load obj file loader = OBJLoader() obj_path = os.path.join(os.path.dirname(__file__), "examples/testnurbs.obj") obj = loader.load(obj_path) scene.add(*obj.children) for obj in scene.children: obj.pos.z = -20 obj.material.specular = .35, .35, .35 # set colors to 3d objects scene.children[0].material.color = 0., .7, 0. # green scene.children[1].material.color = .7, 0., 0. # red scene.children[2].material.color = 0., 0., .7 # blue scene.children[3].material.color = .7, .7, 0. # yellow scene.children[0].material.diffuse = 0., .7, 0. # green scene.children[1].material.diffuse = .7, 0., 0. # red scene.children[2].material.diffuse = 0., 0., .7 # blue scene.children[3].material.diffuse = .7, .7, 0. # yellow self.renderer.render(scene, camera) root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) return root
def _setup_object(self): self.clear_widgets() if is_ios(): # TODO enable this when iOS bug is resolved return shader_file = resource_find( os.path.join('resource', 'models', 'shaders.glsl')) obj_path = resource_find(self.model_path) self.renderer = Renderer(shader_file=shader_file) scene = Scene() camera = PerspectiveCamera(15, 1, 1, 1000) loader = OBJLoader() obj = loader.load(obj_path) scene.add(*obj.children) for obj in scene.children: obj.pos.x = self.position_x obj.pos.y = self.position_y obj.pos.z = self.position_z obj.rotation.x = self.rotation_x obj.rotation.y = self.rotation_y obj.rotation.z = self.rotation_z obj.material.specular = .85, .85, .85 obj.material.color = 1.0, 1.0, 1.0 obj.material.diffuse = 0.5, 0.5, 0.5 obj.material.transparency = 1.0 obj.material.intensity = 0.5 self.imu_obj = obj # obj.material.shininess = 1.0 self.renderer.render(scene, camera) self.renderer.bind(size=self._adjust_aspect) Clock.schedule_once(lambda dt: self.add_widget(self.renderer))
def build(self): root = FloatLayout() self.renderer = Renderer(shader_file=shader_file) scene = Scene() camera = PerspectiveCamera(90, 1, 1, 10000) # loader = OBJMTLLoader() # obj = loader.load(obj_file, mtl_file) loader = OBJLoader() obj = loader.load(obj_file) self.renderer.main_light.pos = 1, 20, 50 self.renderer.main_light.intensity = 1000 camera.pos = 0, 0, 50 camera.look_at((0, 0, 0)) scene.add(*obj.children) for obj in scene.children: obj.pos.z = -0. obj.scale = 0.1, 0.1, 0.1 self.renderer.render(scene, camera) self.orion = scene.children root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) Clock.schedule_interval(self._rotate_obj, 1 / 20) # Clock.schedule_interval(self.print_color, 2) return root
def build(self): root = FloatLayout() self.renderer = Renderer() scene = Scene() camera = PerspectiveCamera(15, 1, 1, 1000) # load obj file loader = OBJLoader() obj_path = os.path.join(os.path.dirname(__file__), "./testnurbs.obj") obj = loader.load(obj_path) scene.add(*obj.children) for obj in scene.children: obj.pos.z = -20 obj.material.specular = .35, .35, .35 # set colors to 3d objects scene.children[0].material.color = 0., .7, 0. # green scene.children[1].material.color = .7, 0., 0. # red scene.children[2].material.color = 0., 0., .7 # blue scene.children[3].material.color = .7, .7, 0. # yellow scene.children[0].material.diffuse = 0., .7, 0. # green scene.children[1].material.diffuse = .7, 0., 0. # red scene.children[2].material.diffuse = 0., 0., .7 # blue scene.children[3].material.diffuse = .7, .7, 0. # yellow self.renderer.render(scene, camera) root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) return root
def build(self): root = FloatLayout() self.renderer = Renderer(shader_file=shader_file) self.renderer.set_clear_color((.16, .30, .44, 1.)) scene = Scene() # geometry = CylinderGeometry(0.5, 2) geometry = SphereGeometry(1) # geometry = BoxGeometry(1, 1, 1) material = Material(color=(0.3, 0., 0.3), diffuse=(0.3, 0.3, 0.3), specular=(0., 0., 0.)) loader = STLLoader() obj = loader.load(stl_file, material) self.item = obj scene.add(self.item) self.cube = Mesh(geometry, material) self.item.pos.z = -1.5 #self.cube.pos.z=-5 camera = PerspectiveCamera(75, 0.3, 0.5, 1000) #camera = OrthographicCamera() #scene.add(self.cube) self.renderer.render(scene, camera) root.add_widget(self.renderer) Clock.schedule_interval(self._rotate_cube, 1 / 20) self.renderer.bind(size=self._adjust_aspect) return root
def build(self): root = FloatLayout() self.renderer = Renderer() scene = Scene() # load stl file mesh_a = mesh.Mesh.from_file('./meshes/base_link.STL') geo = STLGeometry(mesh_a) material = Material(color=(0., 0., 1.), diffuse=(0., 0., 0.1), specular=(.1, .1, .1)) obj = Mesh(geo, material) # obj.position.z = 10 self.my_obj = obj # load obj file # loader = OBJLoader() # obj = loader.load(obj_file) # self.monkey = obj.children[0] # scene.add(obj) camera = PerspectiveCamera(15, 1, 1, 1000) self.renderer.render(scene, camera) root.add_widget(self.renderer) Clock.schedule_interval(self._update_obj, 1. / 20) self.renderer.bind(size=self._adjust_aspect) return root
class RS(Widget): def __init__(self, root, **kwargs): super().__init__(root) self.path = 'forge/embyr/' self.glSetup() self.camera = PerspectiveCamera(30, 1, 10, 1000) #self.transform = Transform() self.transform = Transform(pos=[8, 20, 8], lookvec=[40, 10, 40]) pos = kwargs.pop('pos', None) size = kwargs.pop('size', None) #pos_hint = {'right':ww, 'bottom':hh} self.renderer = Renderer(shader_file=self.path + shaderf) #self.renderer.pos_hint = pos_hint self.scene = Scene() #self.renderer.size_hint = (0.5, 0.5) self.transform.size = (0, 0) self.transform.size_hint = (0, 0) self.renderer.render(self.scene, self.camera) self.renderer.bind(size=self._adjust_aspect) self.renderer.size_hint = size self.renderer.pos = pos #self.renderer.pos = (256, 256) self.root.add_widget(self.renderer) self.root.add_widget(self.transform) #self._disabled_count = 0 #self.add_widget(self.renderer) #self.add_widget(self.transform) def glSetup(self): gl.glEnable(gl.GL_CULL_FACE) gl.glCullFace(gl.GL_BACK) def add(self, obj): self.scene.add(obj) self.renderer.add(obj) def update(self, dt): pos, vec = self.transform.update(dt) self.renderer.camera.look_at(vec) x, y, z = pos self.renderer.camera.pos = (-x, -y, -z) return vec def _adjust_aspect(self, inst, val): rsize = self.renderer.size aspect = rsize[0] / float(rsize[1]) self.renderer.camera.aspect = aspect
def build(self): root = FloatLayout() self.renderer = Renderer(shader_file=shader_file) scene = Scene() # load obj file loader = OBJLoader() obj = loader.load(obj_file) self.monkey = obj.children[0] scene.add(*obj.children) camera = PerspectiveCamera(15, 1, 1, 1000) self.renderer.render(scene, camera) root.add_widget(self.renderer) Clock.schedule_interval(self._update_obj, 1. / 20) self.renderer.bind(size=self._adjust_aspect) return root
def build(self): root = FloatLayout() self.renderer = Renderer(shader_file=shader_file) scene = Scene() camera = PerspectiveCamera(15, 1, 1, 1000) # load obj file loader = OBJLoader() obj = loader.load(obj_path) scene.add(*obj.children) for obj in scene.children: obj.pos.z = -20 self.renderer.render(scene, camera) root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) return root
def build(self): self.renderer = Renderer() scene = Scene() camera = PerspectiveCamera(15, 1, 1, 1000) loader = OBJLoader() obj = loader.load(obj_file) self.obj3d = obj self.camera = camera root = ObjectTrackball(camera, 1500) scene.add(obj) self.renderer.render(scene, camera) root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) return root
def build(self): box= BoxLayout(orientation='vertical') layout = GridLayout(cols=5) layout.add_widget(Button(text='rotx',on_press=self.stop_rotx)) layout.add_widget(Button(text='roty',on_press=self.stop_roty)) layout.add_widget(Button(text='up',on_press=self.up)) layout.add_widget(Button(text='down',on_press=self.dn)) layout.add_widget(Button(text='xpos',on_press=self.xp)) layout.add_widget(Button(text='xneg',on_press=self.xm)) layout.add_widget(PongGame(text='keyboard')) box.add_widget(layout) root = FloatLayout() box.add_widget(root) self.renderer = Renderer(shader_file="simple.glsl") print "self.renderr" dir(self.renderer) # hintergrund self.renderer. set_clear_color((0.9,1,1, 1)); scene = Scene() camera = PerspectiveCamera(45, 1, 1, 1000) camera.position=(0,0,90) self.camera=camera #loader = OBJMTLLoader() #obj = loader.load("my_colors.obj", "my_colors.mtl") loader = OBJLoader() #obj = loader.load("my_colors.obj") #obj = loader.load("Cube.obj") #obj = loader.load("Fusion003.obj") obj = loader.load(file) scene.add(*obj.children) self.scene=scene for obj in scene.children: obj.pos.z = -6. self.renderer.render(scene, camera) self.orion = scene.children[0] root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) Clock.schedule_interval(self._rotate_obj, 1 / 20) return box
def build(self): root = FloatLayout() self.renderer = Renderer() scene = Scene() camera = PerspectiveCamera(15, 1, 1, 1000) # load obj file loader = OBJMTLLoader() obj_path = os.path.join(os.path.dirname(__file__), "./testnurbs.obj") obj = loader.load(obj_path, "./testnurbs.mtl") scene.add(*obj.children) for obj in scene.children: obj.pos.z = -20 self.renderer.render(scene, camera) root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) return root
class glContext(BoxLayout): def __init__(self, **kwargs): super().__init__(**kwargs) with self.canvas.before: # sfondo Color(0, 0, 0) self.background = Rectangle() # colore del modello Color(.9, .9, 1) # renderer self.renderer = Renderer(shader_file="glContext\\simple.glsl") # scene self.scene = Scene() # objects self.objects = Objects_list() self.object_father = self.objects.get() # adding objects to scene self.scene.add(self.object_father) # camera self.camera = PerspectiveCamera( fov=75, # distance from the screen aspect=0, # "screen" ratio near=1, # nearest rendered point far=100 # farthest rendered point ) # rendering self.renderer.render(self.scene, self.camera) self.renderer.bind(size=self._adjust_aspect) self.add_widget(self.renderer) def _adjust_aspect(self, *args): rsize = self.renderer.size aspect = rsize[0] / float(rsize[1]) self.renderer.camera.aspect = aspect # adjust background self.background.pos = self.pos self.background.size = self.size
def build(self): root = FloatLayout() self.renderer = Renderer(shader_file=shader_file) scene = Scene() camera = PerspectiveCamera(15, 1, 1, 1000) loader = OBJMTLLoader() obj = loader.load(obj_file, mtl_file) scene.add(*obj.children) for obj in scene.children: obj.pos.z = -20. self.renderer.render(scene, camera) self.orion = scene.children[0] root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) Clock.schedule_interval(self._rotate_obj, 1 / 20) return root
def build(self): layout = FloatLayout() # create renderer self.renderer = Renderer() # create scene scene = Scene() # create default cube for scene for i in range(1, 6): for j in range(1, 6): cube_geo = BoxGeometry(*[1] * 3) cube_mat = Material(color=(randint(0, 10) * .1, randint(0, 10) * .1, randint(0, 10) * .1)) cube = Mesh(geometry=cube_geo, material=cube_mat) # default pos == (0, 0, 0) cube.pos.y = -0.5 cube.pos.z = -i cube.pos.x = j - 3 self.cubes.append(cube) scene.add(cube) # create camera for scene self.camera = PerspectiveCamera( fov=75, # distance from the screen aspect=0, # "screen" ratio near=1, # nearest rendered point far=20 # farthest rendered point ) # start rendering the scene and camera self.renderer.render(scene, self.camera) # set renderer ratio is its size changes # e.g. when added to parent self.renderer.bind(size=self._adjust_aspect) layout.add_widget(self.renderer) #Clock.schedule_interval(self.move_cubes, 1/60) Window.bind(on_key_down=self.handle_keys) return layout
def gsensor_init(self): pilot.send ("GSENSOR ON") self.renderer = Renderer(shader_file="3d/simple.glsl") scene = Scene() # load obj file loader = OBJLoader() obj = loader.load("3d/k9.obj") self.k9obj = obj.children[0] scene.add(*obj.children) camera = PerspectiveCamera(15, 1, 1, 1000) self.renderer.size_hint = (None,None) self.renderer.pos_hint = {'right' : .99, 'y': 0.4} self.renderer.render(scene, camera) self.renderer.size = (300,300) self.add_widget(self.renderer) Clock.schedule_interval(self.gsensor_3d_update, 1. / 20) self.renderer.bind(size=self.gsensor_3d_adjust_aspect)
def build(self): root = FloatLayout() self.renderer = Renderer() self.renderer.set_clear_color((.2, .2, .2, 1.)) scene = Scene() geometry = BoxGeometry(1, 1, 1) material = Material(color=(0., 0., 1.), diffuse=(1., 1., 0.), specular=(.35, .35, .35)) self.cube = Mesh(geometry, material) self.cube.pos.z = -5 camera = PerspectiveCamera(75, 0.3, 1, 1000) scene.add(self.cube) self.renderer.render(scene, camera) root.add_widget(self.renderer) Clock.schedule_interval(self._rotate_cube, 1 / 20) self.renderer.bind(size=self._adjust_aspect) return root
def build(self): self.look_at = Vector3(0, 0, -1) root = FloatLayout() self.renderer = Renderer(shader_file=shader_file) scene = Scene() self.camera = PerspectiveCamera(75, 1, 1, 1000) self.camera.pos.z = 5 loader = OBJMTLLoader() obj = loader.load(obj_file, mtl_file) self._keyboard = Window.request_keyboard(self._keyboard_closed, self) self._keyboard.bind(on_key_down=self._on_keyboard_down) scene.add(*obj.children) self.renderer.render(scene, self.camera) self.orion = scene.children[0] root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) Clock.schedule_interval(self._rotate_obj, 1 / 20) return root
def build(self): root = FloatLayout() self.renderer = Renderer() self.renderer.set_clear_color((.2, .2, .2, 1.)) scene = Scene() geometry = BoxGeometry(1, 1, 1) material = Material(color=(0., 1., 0.), diffuse=(0., 1., 0.), specular=(.35, .35, .35)) self.cube = Mesh(geometry, material) self.cube.pos.z = -3 camera = PerspectiveCamera(75, 0.3, 1, 1000) scene.add(self.cube) self.renderer.render(scene, camera) root.add_widget(self.renderer) Clock.schedule_interval(self._rotate_cube, 1 / 20) self.renderer.bind(size=self._adjust_aspect) return root
def build(self): self.renderer = Renderer() scene = Scene() camera = PerspectiveCamera(45, 1, 0.1, 2500) self.renderer.set_clear_color((.2, .2, .2, 1.)) self.camera = camera root = ObjectTrackball(camera, 10) # add a cube to the environment as an example # NOTE: the grid will be rendered without transparency if it # is added before the box. # This may be because the shader is not called until a 'triangles' mesh is # rendered? Hence the Fragment Shader has not yet been called? geometry = BoxGeometry(1, 1, 1) material = Material(color=(1., 1., 1.), diffuse=(1., 1., 1.), specular=(.35, .35, .35)) obj = Mesh(geometry, material) scene.add(obj) # create a grid on the xz plane geometry = GridGeometry(size=(30, 30), spacing=1) material = Material(color=(1., 1., 1.), diffuse=(1., 1., 1.), specular=(.35, .35, .35), transparency=.1) lines = Lines(geometry, material) lines.rotation.x = 90 scene.add(lines) self.renderer.render(scene, camera) self.renderer.main_light.intensity = 500 root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) return root
class My3DScreen(Screen): layout = ObjectProperty() def _adjust_aspect(self, *args): rsize = self.renderer.size aspect = rsize[0] / float(rsize[1]) self.renderer.camera.aspect = aspect def __init__(self, **kwargs): super(My3DScreen, self).__init__(**kwargs) self.look_at = Vector3(0, 0, -1) self._keyboard = Window.request_keyboard(self._keyboard_closed, self) self._keyboard.bind(on_key_down=self._on_keyboard_down) #root = FloatLayout() self.camera = PerspectiveCamera(75, 0.3, 1, 1000) self.radius = 10 self.phi = 90 self.theta = 0 self._touches = [] self.camera.pos.z = self.radius self.camera.look_at((0, 0, 0)) #root = self.layout def make_3d(self, clock=None): #self.root = FloatLayout() self.renderer = Renderer() self.renderer.set_clear_color((.2, .2, .2, 1.)) self.scene = Scene() #geometry = BoxGeometry(0.5, 0.5, 0.5) geometry = SphereGeometry(0.1) material = Material(color=(0., 0., 1.), diffuse=(1., 1., 0.), specular=(.35, .35, .35)) """ a = 5 liste = [] i = 0 for z in range(-5, -35, -1): for x in range(-5, 5): liste.append(Mesh(geometry, material)) liste[-1].pos.x = x liste[-1].pos.y = -i liste[-1].pos.z = z print(x, -i, z) self.scene.add(liste[-1]) i+=1 """ #!erster test für errecnete Daten liste = [] for z in range(0, int(Global.config["maxy"])): #for z in range(0, 10): #i = 0 test = calculationClass.find_mistakes_along_x_axis(z) #print(test) for x, y, rad in calculationClass.find_mistakes_along_x_axis(z): #for x, y in [[1, 2], [2, 0], [2.314, 5], [3, 0], [3.123, 4]]: #for x, y in [[1, 2], [2, 0], [3, 0]]: #for x in [8.06158101842821, 4.06158101842821, 0.09813725490196079]: #for x in test[:][0]: #for line in test[:10]: #x = line[0] x = float(x) y = float(y) #x = 8.06158101842821 #new_list.append([x, y, z, rad]) rad = 1 #y = 0 z += 5 liste.append(Mesh(geometry, material)) #liste[-1].pos.x = x - 8 liste[-1].pos.x = -z liste[-1].pos.y = y liste[-1].pos.z = x #print(x, y, z) self.scene.add(liste[-1]) #i += 1 self.renderer.render(self.scene, self.camera) self.renderer.bind(size=self._adjust_aspect) self.layout.add_widget(self.renderer) print(liste[0]) print(liste[0].pos) #self.look_at = liste[0].pos #self.look_at.x = liste[0][0] #self.look_at.y = liste[0][1] #self.look_at.z = liste[0][2] #self.camera.look_at(self.look_at) #test = (liste[0][0], liste[0][1], liste[0][2]) #a = tuple(liste[0].pos) #print(a) #self.camera.look_at(a) #self.look_at.x = liste[0].pos.x #self.look_at.y = liste[0].pos.y #self.look_at.z = liste[0].pos.z self.look_at = Vector3(0, 0, -1) #self.camera.look_at(self.look_at) #self.add_widget(self.root) print("here") def _keyboard_closed(self): self._keyboard.unbind(on_key_down=self._on_keyboard_down) self._keyboard = None def _on_keyboard_down(self, keyboard, keycode, text, modifiers): if keycode[1] == 'w': self.camera.pos.y += 0.2 #self.look_at.y += 0.2 elif keycode[1] == 's': self.camera.pos.y -= 0.2 #self.look_at.y -= 0.2 elif keycode[1] == 'a': self.camera.pos.x -= 0.2 #self.look_at.x -= 0.2 elif keycode[1] == 'd': self.camera.pos.x += 0.2 #self.look_at.x += 0.2 elif keycode[1] == '-': self.camera.pos.z += 0.2 #self.look_at.z += 0.2 elif keycode[1] == '+': self.camera.pos.z -= 0.2 #self.look_at.z -= 0.2 elif keycode[1] == 'up': self.look_at.y += 0.2 elif keycode[1] == 'down': self.look_at.y -= 0.2 elif keycode[1] == 'right': self.look_at.x += 0.2 elif keycode[1] == 'left': self.look_at.x -= 0.2 elif keycode[1] == "q": self.camera.rotation.y += 1 self.camera.look_at(self.look_at) def define_rotate_angle(self, touch): theta_angle = (touch.dx / self.width) * -360 phi_angle = -1 * (touch.dy / self.height) * 360 return phi_angle, theta_angle def on_touch_down(self, touch): if touch.is_mouse_scrolling: if touch.button == 'scrolldown': #self.look_at -= 0.2 self.camera.pos -= 0.2 elif touch.button == 'scrollup': #self.look_at += 0.2 self.camera.pos += 0.2 touch.grab(self) self._touches.append(touch) self.camera.look_at(self.look_at) def on_touch_up(self, touch): touch.ungrab(self) self._touches.remove(touch) def on_touch_move(self, touch): if touch in self._touches and touch.grab_current == self: if len(self._touches) == 1: self.do_rotate(touch) elif len(self._touches) == 2: pass def do_rotate(self, touch): d_phi, d_theta = self.define_rotate_angle(touch) self.phi += d_phi self.theta += d_theta _phi = math.radians(self.phi) _theta = math.radians(self.theta) z = self.radius * math.cos(_theta) * math.sin(_phi) x = self.radius * math.sin(_theta) * math.sin(_phi) y = self.radius * math.cos(_phi) self.camera.pos = x, y, z self.camera.look_at((0, 0, 0)) def on_enter(self, *args): super(My3DScreen, self).on_enter(*args) #self.remove_widget(self.renderer) #mat = Material() #box = BoxGeometry(0.5, 0.5, 0.5) #cube = Mesh(box, mat) #cube.pos.z = -4 #self.scene.add(cube) Clock.schedule_once(self.make_3d)
def build(self): layout = GridLayout(cols=3) # create renderer self.renderer = Renderer(size_hint=(5, 5)) self.renderer.set_clear_color((0.1, 0.1, 0.1, 1)) # rgba # create scene scene = Scene() self.cubes = [] # create cubes for scene # # default pure green cube cube_geo = BoxGeometry(.3, .3, .3) cube_mat = Material(color=(0, 0.5, 0) # base color ) self.cubes.append(Mesh(geometry=cube_geo, material=cube_mat)) # default pos == (0, 0, 0) self.cubes[0].pos.z = -5 self.cubes[0].pos.x = 1 self.cubes[0].pos.y = 0.8 self.cubes[0].rotation.x = 45 # black cube, red shadow, half-transparent cube_geo = BoxGeometry(.3, .3, .3) cube_mat = Material( transparency=0.5, color=(0, 0, 0), # base color diffuse=(10, 0, 0), # color of "shadows" specular=(0, 0, 0) # mirror-like reflections ) self.cubes.append(Mesh(geometry=cube_geo, material=cube_mat)) # default pos == (0, 0, 0) self.cubes[1].pos.z = -5 self.cubes[1].pos.x = -1 self.cubes[1].pos.y = 0.8 self.cubes[1].rotation.y = 45 # default pure green cube with red reflections cube_geo = BoxGeometry(.3, .3, .3) cube_mat = Material( transparency=1, color=(0, 0.5, 0), # base color diffuse=(0, 0, 0), # color of "shadows" specular=(10, 0, 0) # mirror-like reflections ) self.cubes.append(Mesh(geometry=cube_geo, material=cube_mat)) # default pos == (0, 0, 0) self.cubes[2].pos.z = -5 self.cubes[2].pos.x = 1 self.cubes[2].pos.y = -0.8 self.cubes[2].rotation.z = 45 # something.obj from Blender loader = OBJLoader() self.cubes.extend( loader.load(join(FOLDER, 'models', 'something.obj')).children) self.cubes[3].pos.z = -5 self.cubes[3].pos.x = -1 self.cubes[3].pos.y = -0.8 self.cubes[3].rotation.x = 45 self.cubes[3].material.color = (0.1, 0.4, 0.1) self.cubes[3].material.texture_ratio = 0.0 # cube object from Blender loader = OBJLoader() self.main_cube = loader.load(join(FOLDER, 'models', 'cube.obj')) self.main_cube.rotation.x = 45 self.main_cube.rotation.y = 45 self.main_cube.pos.z = -5 self.main_cube.scale = (0.5, 0.5, 0.5) scene.add(self.main_cube) planes = [((0, 0, -10), (0, 0, 0)), ((-10, 0, 0), (0, -90, 0)), ((10, 0, 0), (0, 90, 0)), ((0, 0, 10), (0, 180, 0))] # position and rotation changes for plane in planes: geo = BoxGeometry(5, 5, .1) mat = Material(color=(1, 1, 1)) mesh = Mesh(geometry=geo, material=mat) mesh.pos.x += plane[0][0] mesh.pos.y += plane[0][1] mesh.pos.z += plane[0][2] mesh.rot.x += plane[1][0] mesh.rot.y += plane[1][1] mesh.rot.z += plane[1][2] scene.add(mesh) # create camera for scene self.camera = PerspectiveCamera( fov=75, # distance from the screen aspect=0, # "screen" ratio near=.1, # nearest rendered point far=1000 # farthest rendered point ) # start rendering the scene and camera for cube in self.cubes: scene.add(cube) self.renderer.render(scene, self.camera) # set renderer ratio is its size changes # e.g. when added to parent self.renderer.bind(size=self._adjust_aspect) layout.add_widget(Factory.CamRot()) layout.add_widget(Factory.LightPanel()) layout.add_widget(Factory.CamStrafe()) layout.add_widget(Widget()) layout.add_widget(self.renderer) layout.add_widget(Label(text='+\n\nY\n\n-')) layout.add_widget(Factory.CamNav()) layout.add_widget(Label(text='- X +')) layout.add_widget(Factory.ObjNav()) Clock.schedule_interval(self.rotate_cube, .01) Clock.schedule_interval(self.scale_cube, 1) # keyboard listener Listener() return layout
class SceneApp(App): joints = [] def build(self): root = BaseBox() self.renderer = Renderer(shader_file=shader_file) self.renderer.set_clear_color((.2, .2, .2, 1.)) self.scene = Scene() self.manipulator = self.construct_manipulator() camera = PerspectiveCamera(75, 0.01, 0.01, 1500) trackball = ObjectTrackball(camera, 2) self.scene.add(self.manipulator) self.renderer.render(self.scene, camera) self.renderer.main_light.intensity = 3000 trackball.add_widget(self.renderer) root.add_widget(trackball) self.renderer.bind(size=self._adjust_aspect) root.joints = self.joints root.renderer = self.renderer return root def _adjust_aspect(self, inst, val): rsize = self.renderer.size aspect = rsize[0] / float(rsize[1]) self.renderer.camera.aspect = aspect def _rotate_cube(self, dt): self.axis_e.rotation.x += 0 self.axis_e.rotation.y += 1 self.axis_e.rotation.z += 1 def _rotate_rect(self, dt): self.axis_d.rotation.x += 0.5 def construct_manipulator(self): material = Material(color=(0., 0., 1.), diffuse=(1., 1., 0.), specular=(.35, .35, .35)) # axis a # one jaw axis_a_dimensions = JOINT_SIZE_A[0], JOINT_SIZE_A[2], JOINT_SIZE_A[1] axis_a_geometry = create_joint_rectangle(axis_a_dimensions[0], axis_a_dimensions[1], axis_a_dimensions[2]) axis_a_left_mesh = Mesh(axis_a_geometry, material) axis_a_right_mesh = Mesh(axis_a_geometry, material) self.joints.append(axis_a_left_mesh) self.joints.append(axis_a_right_mesh) # axis b wrist axis_b_dimensions = get_joint_hypo_length( JOINT_OFFSET_A_1), JOINT_SIZE_B[1], JOINT_SIZE_B[2] axis_b_geometry = create_joint_rectangle(axis_b_dimensions[0], axis_b_dimensions[1], axis_b_dimensions[2]) material = Material(color=(1., 0., 0.), diffuse=(1., 0., 0.), specular=(.35, .35, .35)) axis_b_mesh = Mesh(axis_b_geometry, material) self.joints.append(axis_b_mesh) axis_b_mesh.add(axis_a_left_mesh) axis_a_left_mesh.pos.x = axis_b_dimensions[0] axis_a_left_mesh.pos.z = axis_b_dimensions[2] / 2 axis_b_mesh.add(axis_a_right_mesh) axis_a_right_mesh.pos.x = axis_b_dimensions[0] axis_a_right_mesh.pos.z = -axis_b_dimensions[2] / 2 # axis c bend axis_c_dimensions = get_joint_hypo_length( JOINT_OFFSET_B), JOINT_SIZE_C[1], JOINT_SIZE_C[2] axis_c_geometry = create_joint_rectangle(axis_c_dimensions[0], axis_c_dimensions[1], axis_c_dimensions[2]) material = Material(color=(1., 1., 0.), diffuse=(1., 1., 0.), specular=(.35, .35, .35)) axis_c_mesh = Mesh(axis_c_geometry, material) self.joints.append(axis_c_mesh) axis_c_mesh.add(axis_b_mesh) axis_b_mesh.pos.x = axis_c_dimensions[0] # axis d bend axis_d_dimensions = get_joint_hypo_length( JOINT_OFFSET_C), JOINT_SIZE_D[1], JOINT_SIZE_D[2] axis_d_geometry = create_joint_rectangle(axis_d_dimensions[0], axis_d_dimensions[1], axis_d_dimensions[2]) material = Material(color=(1., 0., 1.), diffuse=(1., 0., 1.), specular=(.35, .35, .35)) axis_d_mesh = Mesh(axis_d_geometry, material) self.joints.append(axis_d_mesh) axis_d_mesh.add(axis_c_mesh) axis_c_mesh.pos.x = axis_d_dimensions[0] # axis e mesh axis_e_dimensions = get_joint_hypo_length( JOINT_OFFSET_D), JOINT_SIZE_E[1], JOINT_SIZE_E[2] axis_e_geometry = create_joint_rectangle(axis_e_dimensions[0], axis_e_dimensions[1], axis_e_dimensions[2]) material = Material(color=(0., 1., 0.), diffuse=(0., 1., 0.), specular=(.35, .35, .35)) axis_e_mesh = Mesh(axis_e_geometry, material) self.joints.append(axis_e_mesh) axis_e_mesh.add(axis_d_mesh) axis_d_mesh.pos.x = axis_e_dimensions[0] # axis f axis_f_dimensions = get_joint_hypo_length( JOINT_OFFSET_E), JOINT_SIZE_F[1], JOINT_SIZE_F[2] axis_f_geometry = create_joint_rectangle(axis_f_dimensions[0], axis_f_dimensions[1], axis_f_dimensions[2]) material = Material(color=(0., 1., 1.), diffuse=(0., 1., 1.), specular=(.35, .35, .35)) axis_f_mesh = Mesh(axis_f_geometry, material) self.joints.append(axis_f_mesh) axis_f_mesh.add(axis_e_mesh) axis_e_mesh.pos.x = axis_f_dimensions[0] # axis_g axis_g_dimensions = get_joint_hypo_length( JOINT_OFFSET_F), JOINT_SIZE_G[1], JOINT_SIZE_G[2] axis_g_geometry = create_joint_rectangle(axis_g_dimensions[0], axis_g_dimensions[1], axis_g_dimensions[2]) material = Material(color=(1., .4, .1), diffuse=(1., .4, .1), specular=(.35, .35, .35)) axis_g_mesh = Mesh(axis_g_geometry, material) self.joints.append(axis_g_mesh) axis_g_mesh.add(axis_f_mesh) axis_f_mesh.pos.x = axis_g_dimensions[0] axis_f_mesh.rotation.z = -90 # base base_dimensions = 0.05, axis_b_dimensions[1], axis_b_dimensions[2] base_geometry = create_joint_rectangle(base_dimensions[0], base_dimensions[1], base_dimensions[2]) material = Material(color=(0., 1., 1.), diffuse=(0., 1., 1.), specular=(.35, .35, .35)) base_mesh = Mesh(base_geometry, material) self.joints.append(base_mesh) base_mesh.add(axis_g_mesh) base_mesh.rotation.z = 90 axis_g_mesh.pos.x = base_dimensions[0] axis_g_mesh.rotation.z = -90 return base_mesh
class MyApp(Application): def __init__(self, size): super().__init__(size) self.title = 'Projekt: Godsword' self.pan = Pan() self.rotate = Rotate() self.zoom = Zoom() self.loader = OBJMTLLoader() self.blocks = dict( (mat.value.tex, mat.value) for mat in enums.Material) self.vec = np.array([0, 0, 1]) self.t = 0 ''' super(Mesh, self).__init__(**kw) self.geometry = geometry self.material = material self.mtl = self.material # shortcut for material property self.vertex_format = kw.pop("vertex_format", DEFAULT_VERTEX_FORMAT) self.create_mesh() ''' def cube(self, tile): DEFAULT_VERTEX_FORMAT = [(b'v_tc0', 2, 'float'), (b'v_normal', 3, 'float'), (b'v_pos', 3, 'float')] obj = self.blocks[tile].obj T() obj = pywave.Wavefront('tex/block.obj', collect_faces=True) material = obj.materials['grass'] cube = obj.meshes['Cube'] vertices = obj.vertices faces = cube.faces grass = obj.materials['grass'] dirt = obj.materials['dirt'] vertices = grass.vertices + dirt.vertices #indices = np.array(faces).ravel().tolist() indices = np.arange(36).astype(int).tolist() #vertices = np.array(vertices).ravel().tolist() tex = Image('tex/grass.png').texture mat = Material(tex) kw = { "vertices": vertices, "indices": indices, "fmt": DEFAULT_VERTEX_FORMAT, "mode": "triangles", 'texture': tex } #if self.material.map: # kw["texture"] = self.material.map mesh = KivyMesh(**kw) class Meshy(Object3D): def __init__(self, mesh, material): super().__init__() self._mesh = mesh self.material = material self.mtl = material self.vertex_format = DEFAULT_VERTEX_FORMAT cube = Meshy(mesh, tex) #cube.material = orig.material #cube.geometry = orig.geometry orig._mesh = cube._mesh orig.material = mat cube = orig #cube = kivy3.Mesh([], material) if tile == 'lava': cube.pos.y = -0.5 elif tile == 'stone': cube.pos.y = 1 elif tile == 'grass': pass elif tile == 'forest': pass elif tile == 'water': cube.pos.y = -0.33 #cube.material.color = 0., .7, 0. # green #cube.material.diffuse = 0., .7, 0. # green return cube def makeMap(self): tiles = loadTiled('../Projekt-Godsword/resource/maps/map1/map.tmx') n, sz = tiles.shape[0], 1 for i in range(n): for j in range(n): tile = tiles[i, j] cube = self.cube(tile) self.scene.add(cube) #NEVER set cube.pos directly. #It won't do anything cube.pos.x = i - n // 2 + sz // 2 cube.pos.z = j - n // 2 + sz // 2 def glSetup(self): #gl.glEnable(gl.GL_CULL_FACE) #gl.glCullFace(gl.GL_BACK) pass def build(self): camera = PerspectiveCamera(30, 1, 1, 1000) self.renderer = Renderer() self.scene = Scene() root = FloatLayout() obj = self.loader.load('tex/nn.obj', 'tex/nn.mtl') self.scene.add(obj) obj.pos.y = 1 self.makeMap() self.renderer.render(self.scene, camera) self.renderer.camera.look_at(0, 0, 0) root.add_widget(self.renderer) root.add_widget(self.pan) root.add_widget(self.rotate) root.add_widget(self.zoom) self.renderer.bind(size=self._adjust_aspect) self.loop(self.update) return root def update(self, t): print(1 / t) self.t += t rad = 80 sz = 500 pi = 3.14159265 r = self.rotate x = r.x + r.xVol y = r.y + r.yVol x = x / sz y = y / sz yclip = np.clip(y, -pi / 2, 0) xz_x = np.cos(x) xz_z = np.sin(x) yz_y = np.cos(yclip) yz_z = np.sin(yclip) xz = np.array([[xz_x, 0, -xz_z], [0, 1, 0], [xz_z, 0, xz_x]]) yz = np.array([[1, 0, 0], [0, yz_y, -yz_z], [0, yz_z, yz_y]]) #Find cylindrical xz plane rotation rot_xz = rad * np.dot(xz, self.vec) xx, _, zz = rot_xz xz_vec = np.array([xx, 0, zz]) #Find spherical yz plane rotation _, yy, zn = np.dot(yz, self.vec) xz_norm = zn #For x, z: shrink to position of spherical rotation #For y: use height from spherical rotation vec = np.array([xx * xz_norm, -rad * yy, zz * xz_norm]) #Zoom factor zoom = Zoom.clipZoom(self.zoom.zoom) vec = vec * zoom p = self.pan x = p.x + p.xVol z = p.y + p.yVol x = 10 * x / sz z = -10 * z / sz #Horizontal component unit_y = np.array([0, 1, 0]) xx, _, zz = np.cross(rot_xz, unit_y) norm = np.sqrt(xx**2 + zz**2) xh, zh = x * xx / norm, x * zz / norm #Depth component xx, _, zz = -xz_vec norm = np.sqrt(xx**2 + zz**2) xd, zd = z * xx / norm, z * zz / norm xx, yy, zz = vec vec = np.array([xx + xh + xd, yy, zz + zh + zd]) self.renderer.camera.look_at([-xh - xd, 0, -zh - zd]) self.renderer.camera.pos = vec.tolist() def _adjust_aspect(self, inst, val): rsize = self.renderer.size aspect = rsize[0] / float(rsize[1]) self.renderer.camera.aspect = aspect
def build(self): layout = GridLayout(cols=3) # create renderer self.renderer = Renderer(size_hint=(5, 5)) self.renderer.set_clear_color( (0.1, 0.1, 0.1, 1) ) # rgba # create scene scene = Scene() self.cubes = [] # create cubes for scene # # default pure green cube cube_geo = BoxGeometry(.3, .3, .3) cube_mat = Material( color=(0, 0.5, 0) # base color ) self.cubes.append(Mesh( geometry=cube_geo, material=cube_mat )) # default pos == (0, 0, 0) self.cubes[0].pos.z = -5 self.cubes[0].pos.x = 1 self.cubes[0].pos.y = 0.8 self.cubes[0].rotation.x = 45 # black cube, red shadow, half-transparent cube_geo = BoxGeometry(.3, .3, .3) cube_mat = Material( transparency=0.5, color=(0, 0, 0), # base color diffuse=(10, 0, 0), # color of "shadows" specular=(0, 0, 0) # mirror-like reflections ) self.cubes.append(Mesh( geometry=cube_geo, material=cube_mat )) # default pos == (0, 0, 0) self.cubes[1].pos.z = -5 self.cubes[1].pos.x = -1 self.cubes[1].pos.y = 0.8 self.cubes[1].rotation.y = 45 # default pure green cube with red reflections cube_geo = BoxGeometry(.3, .3, .3) cube_mat = Material( transparency=1, color=(0, 0.5, 0), # base color diffuse=(0, 0, 0), # color of "shadows" specular=(10, 0, 0) # mirror-like reflections ) self.cubes.append(Mesh( geometry=cube_geo, material=cube_mat )) # default pos == (0, 0, 0) self.cubes[2].pos.z = -5 self.cubes[2].pos.x = 1 self.cubes[2].pos.y = -0.8 self.cubes[2].rotation.z = 45 # black cube with red reflections # and half-transparent cube_geo = BoxGeometry(.3, .3, .3) cube_mat = Material( transparency=0.5, color=(0, 0, 0), # base color specular=(10, 0, 0) # mirror-like reflections ) self.cubes.append(Mesh( geometry=cube_geo, material=cube_mat )) # default pos == (0, 0, 0) self.cubes[3].pos.z = -5 self.cubes[3].pos.x = -1 self.cubes[3].pos.y = -0.8 self.cubes[3].rotation.x = 45 cube_geo = BoxGeometry(.3, .3, .3) cube_mat = Material( transparency=0.5, color=(0, 0, 0), # base color specular=(10, 0, 0) ) self.main_cube = Mesh( geometry=cube_geo, material=cube_mat ) # default pos == (0, 0, 0) self.main_cube.rotation.x = 45 self.main_cube.rotation.y = 45 self.main_cube.pos.z = -5 scene.add(self.main_cube) plane_geo = BoxGeometry(5, 5, .1) plane_mat = Material( color=(1, 1, 1) # base color ) plane = Mesh( geometry=plane_geo, material=plane_mat ) plane.pos.z = -10 scene.add(plane) # create camera for scene self.camera = PerspectiveCamera( fov=75, # distance from the screen aspect=0, # "screen" ratio near=.1, # nearest rendered point far=1000 # farthest rendered point ) # start rendering the scene and camera for cube in self.cubes: scene.add(cube) self.renderer.render(scene, self.camera) # set renderer ratio is its size changes # e.g. when added to parent self.renderer.bind(size=self._adjust_aspect) layout.add_widget(Factory.CamRot()) layout.add_widget(Widget()) layout.add_widget(Factory.CamStrafe()) layout.add_widget(Widget()) layout.add_widget(self.renderer) layout.add_widget(Label(text='+\n\nY\n\n-')) layout.add_widget(Factory.CamNav()) layout.add_widget(Label(text='- X +')) layout.add_widget(Factory.ObjNav()) Clock.schedule_interval(self.rotate_cube, .01) Clock.schedule_interval(self.scale_cube, 1) # keyboard listener Listener() return layout
def build(self): self.renderer = Renderer(shader_file=shader_file) scene = Scene() camera = PerspectiveCamera(45, 1, 0.1, 2500) self.renderer.set_clear_color(clear_color) self.camera = camera root = ObjectTrackball(camera, 10, self.renderer) id_color = (0, 0, 0x7F) geometry = BoxGeometry(1, 1, 1) material = Material(color=(1., 1., 1.), diffuse=(1., 1., 1.), specular=(.35, .35, .35), id_color=id_color, shininess=1.) obj = Mesh(geometry, material) scene.add(obj) root.object_list.append({'id': id_color, 'obj': obj}) id_color = (0, 0x7F, 0) geometry = BoxGeometry(1, 1, 1) material = Material(color=(0., 0., 1.), diffuse=(0., 0., 1.), specular=(.35, .35, .35), id_color=id_color, shininess=1.) obj = Mesh(geometry, material) obj.position.x = 2 scene.add(obj) root.object_list.append({'id': id_color, 'obj': obj}) # create a grid on the xz plane geometry = GridGeometry(size=(30, 30), spacing=1) material = Material(color=(1., 1., 1.), diffuse=(1., 1., 1.), specular=(.35, .35, .35), transparency=.5) lines = Lines(geometry, material) lines.rotation.x = 90 scene.add(lines) geometry = Geometry() geometry.vertices = [[0.0, 0.0, 0.0], [3.0, 0.0, 0.0]] geometry.lines = [Line2(a=0, b=1)] material = Material(color=(1., 0., 0.), diffuse=(1., 0., 0.), specular=(.35, .35, .35)) lines = Lines(geometry, material) lines.position.y = -0.01 scene.add(lines) geometry = Geometry() geometry.vertices = [[0.0, 0.0, 0.0], [0.0, 3.0, 0.0]] geometry.lines = [Line2(a=0, b=1)] material = Material(color=(0., 1., 0.), diffuse=(0., 1., 0.), specular=(1., 1.0, 1.0)) lines = Lines(geometry, material) scene.add(lines) geometry = Geometry() geometry.vertices = [[0.0, 0.0, 0.0], [0.0, 0.0, 3.0]] geometry.lines = [Line2(a=0, b=1)] material = Material(color=(0., 0., 1.), diffuse=(0., 0., 1.), specular=(.35, .35, .35)) lines = Lines(geometry, material) lines.position.y = -0.01 scene.add(lines) self.renderer.render(scene, camera) self.renderer.main_light.intensity = 1000 self.renderer.main_light.pos = (10, 10, -10) root.add_widget(self.renderer) self.renderer.bind(size=self._adjust_aspect) return root
class Level(object): def __init__(self, game): self.name = "" self.game = game self.colliders = [] self.entities = [] self.time = 0 # create camera for scene self.camera = PerspectiveCamera( fov=80, # distance from the screen aspect=0, # "screen" ratio near=.1, # nearest rendered point far=1000 # farthest rendered point ) self.camera.pos.y = 3 self.scene = Scene() self.player = Player() self.entities.append(self.player) self.scene.add(self.player) def tick(self, df): self.time += df v = self.game.inputVector self.player.velocity.add(v * df * 10 * Vector3(-1, 0, 1)) for e in self.entities: e.tick(df) self.camera.pos.x = self.player.pos.x + 0.3 self.camera.pos.z = self.player.pos.z - 3 self.camera.look_at(self.player.pos) for c in self.colliders: if c.callback is not None: if c.check(self.player.pos, self.player.size): if len(inspect.getargspec(c.callback)[0]) > 1: if c.callback(c): break else: if c.callback(): break def reset(self): for e in self.entities: e.reset() def spawn(self, entity, position=None): if position: entity.dpos = Vector3(position) entity.reset() self.entities.append(entity) self.scene.add(entity) return entity def spawn_text(self, text, position, size=0.1, orientation="y", color=(1, 1, 1, 1)): my_label = CoreLabel(font_size=int(200 * size)) my_label.text = text my_label.refresh() aspect = float(my_label.texture.size[0]) / my_label.texture.size[1] quad = Quad(Material(map=my_label.texture, color=color[:3], transparency=color[3]), size * aspect, size, orientation=orientation) quad.label = my_label self.spawn(quad, position) def spawn_std_cube(self, *args, **kwargs): # return self.spawn_wireframe_cube(*args, **kwargs) return self.spawn_solid_cube(*args, **kwargs) def spawn_wireframe_cube(self, location, size=(1, 1, 1), color=(1, 1, 1, 0.5), callback=None): mat = Material(color=color[0:3], transparency=color[3]) cube = Cube(material=mat, size=size) cube.dpos = Vector3(location) cube.reset() self.spawn(cube) if callback is not None: collider = BoxCollider(p1=p1, p2=p2, parent=cube, callback=callback) self.colliders.append(collider) return (cube, collider) return cube def spawn_solid_cube(self, location, texture=None, size=(1, 1, 1), color=(1, 1, 1, 0.5), callback=None): geo = BoxGeometry(size[0], size[1], size[2]) mat = Material(color=color[0:3], transparency=color[3], map=texture) cube = Mesh(geometry=geo, material=mat) self.spawn(cube, location) if callback is not None: collider = BoxCollider(p1=p1, p2=p2, parent=cube, callback=callback) self.colliders.append(collider) return (cube, collider) return cube def spawn_portal(self, position, radius=0.2, color=(1, 1, 1), callback=None): mat = Material(color=color) portal = self.spawn(PortalPad(mat, radius, 0.02), position) if callback is not None: collider = SphereCollider([0, 0, 0], radius, parent=portal, callback=callback) self.colliders.append(collider) return (portal, collider) return portal def spawn_line(self, p1, p2, width=1, color=(1, 1, 1), callback=None): mat = Material(color=color) line = Lines(mat, [p1, p2], width) self.scene.add(line) if callback is not None: collider = LineCollider(p1=p1, p2=p2, parent=line, callback=callback) self.colliders.append(collider) return (line, collider) return line def spawn_stalker(self, position, size=0.1, callback=None): stalker = Stalker(level=self) stalker.dpos = Vector3(position) stalker.reset() self.entities.append(stalker) self.scene.add(stalker) if callback is not None: collider = SphereCollider(position=(0, 0, 0), radius=size, parent=stalker, callback=callback) self.colliders.append(collider) return (stalker, collider) return stalker
def build(self): self.theflag = 0 self.theflag0 = 0 self.distan = 1000 # дистанция до начальной точки (0,0,-50) что бы ничего не было за экраном (надо будет выстваить на изменение) bl = BoxLayout(orientation='vertical', size_hint=(.15, 1), spacing=10, padding=10) # левая панель al = AnchorLayout(anchor_x='left', anchor_y='center') # основная система интерфейса layout = GridLayout(cols=2, spacing=3, size_hint=(1, 1)) #сетка для кнопок поворота matrix = np.load('matrix0.npy', allow_pickle=True) counter = int(int(matrix.size) / 2) x = np.zeros(counter) y = np.zeros(counter) z = np.zeros(counter) soe = np.zeros((counter, counter)) for i in range(2): if (i == 0): for j in range(counter): for k in range(3): a = matrix[i, j] if (k == 0): x[j] = a[k] * 10 elif (k == 1): y[j] = a[k] * 10 else: z[j] = a[k] * 10 else: for j in range(counter): a = matrix[i, j] for k in range(counter): soe[j][k] = a[k] print(x, y, z) print(soe) # кнопка загрузки координат loader = Button(text='Load', on_press=self.load) bl.add_widget(loader) #starter = Button(text='Построить', on_press = self.letstart) #bl.add_widget(starter) bl.add_widget(Widget()) # create renderer self.renderer = Renderer() # create scene scene = Scene() #lines k0 = 0 k1 = 0 lines_list = [] for i in soe: for j in i: if (j == 1): line0_geo = BoxGeometry( 1, int(((y[k0] - y[k1])**2 + (x[k0] - x[k1])**2 + (z[k0] - z[k1])**2)**0.5), 1) #print(int(((abs(x[k0]-x[k1]) + abs(y[k0]-y[k1])+ abs(z[k0]-z[k1]))**0.5)),'length') #print(int(abs(y[k0]-y[k1]) + abs(x[k0]-x[k1])+ abs(z[k0]-z[k1]))) line0_mat = Material() self.line0 = Mesh( geometry=line0_geo, material=line0_mat) # default pos == (0, 0, 0) self.line0.pos.x = int((x[k0] + x[k1]) / 2) self.line0.pos.y = int((y[k0] + y[k1]) / 2) self.line0.pos.z = int((z[k0] + z[k1]) / 2) - self.distan if y[k0] - y[k1] == 0 and x[k0] - x[ k1] == 0 and z[k0] - z[k1] != 0: self.line0.rotation.x = 90 elif y[k0] - y[k1] == 0 and x[k0] - x[k1] != 0 and z[ k0] - z[k1] == 0: self.line0.rotation.z = 90 elif y[k0] - y[k1] != 0 and x[k0] - x[k1] == 0 and z[ k0] - z[k1] == 0: ### fff = 0 elif y[k0] - y[k1] != 0 and x[k0] - x[k1] != 0 and z[ k0] - z[k1] == 0: self.line0.rotation.z = math.atan( (x[k0] - x[k1]) / (y[k0] - y[k1])) / math.pi * 180 elif y[k0] - y[k1] != 0 and x[k0] - x[ k1] == 0 and z[k0] - z[k1] != 0: #self.line0.rotation.x = math.atan((z[k0]-z[k1])/(y[k0]-y[k1]))/math.pi*180 self.line0.rotation.x = math.acos( abs(y[k0] - y[k1]) / ((x[k0] - x[k1])**2 + (y[k0] - y[k1])**2 + (z[k0] - z[k1])**2)**0.5) / math.pi * 180 #print() elif y[k0] - y[k1] == 0 and x[k0] - x[k1] != 0 and z[ k0] - z[k1] != 0: self.line0.rotation.z = math.atan( (x[k0] - x[k1]) / (z[k0] - z[k1])) / math.pi * 180 * -1 self.line0.rotation.x = 90 ### elif y[k0] - y[k1] != 0 and x[k0] - x[k1] != 0 and z[ k0] - z[k1] != 0: if ((x[k0] < x[k1] and y[k0] < y[k1]) or (x[k0] > x[k1] and y[k0] > y[k1])): #self.line0.rotation.z = math.atan((abs(z[k0]-z[k1]))/1.5/(abs(y[k0]-y[k1])))/math.pi*180 self.line0.rotation.z = math.acos( abs(y[k0] - y[k1]) / ((x[k0] - x[k1])**2 + (y[k0] - y[k1])**2 + (0)**2)**0.5) / math.pi * 180 * -1 #проблема else: self.line0.rotation.z = math.acos( abs(y[k0] - y[k1]) / ((x[k0] - x[k1])**2 + (y[k0] - y[k1])**2 + (0)**2)**0.5) / math.pi * 180 #self.line0.rotation.x = math.atan((1.25*abs(x[k0]-x[k1]))/(abs(y[k0]-y[k1])))/math.pi*180*-1 if ((z[k0] < z[k1] and y[k0] < y[k1]) or (z[k0] > z[k1] and y[k0] > y[k1])): self.line0.rotation.x = math.acos( abs(y[k0] - y[k1]) / ((0)**2 + (y[k0] - y[k1])**2 + (z[k0] - z[k1])**2)**0.5) / math.pi * 180 #проблема else: self.line0.rotation.x = math.acos( abs(y[k0] - y[k1]) / ((0)**2 + (y[k0] - y[k1])**2 + (z[k0] - z[k1])**2)**0.5) / math.pi * 180 * -1 #self.line0.rotation.x = math.acos(abs(y[k0]-y[k1])/((0)**2+(y[k0]-y[k1])**2+(z[k0]-z[k1])**2)**0.5)/math.pi*180*-1#there print(self.line0.rotation.z) print(self.line0.rotation.x) lines_list.append(self.line0) k1 += 1 k0 += 1 k1 = 0 line0_geo = BoxGeometry(1, y[1] - y[0], 1) line0_mat = Material() self.line0 = Mesh(geometry=line0_geo, material=line0_mat) # default pos == (0, 0, 0) self.line0.pos.z = int(z[0]) - self.distan #self.line3.rotation.x = 90 #points point_list = [] sumx = 0 sumy = 0 sumz = 0 sumcount = 0 loader = OBJLoader() for i in range(counter): point_geom = SphereGeometry(1.1) point_mat = Material() self.point0 = Mesh(geometry=point_geom, material=point_mat) self.point0.pos.x = int(x[i]) self.point0.pos.y = int(y[i]) self.point0.pos.z = int(z[i]) - self.distan self.point0.scale = (1, 1, 1) point_list.append(self.point0) sumx += self.point0.pos.x sumy += self.point0.pos.y sumz += self.point0.pos.z sumcount += 1 #scene.add(self.point0) point_geom = SphereGeometry() point_mat = Material() self.point1 = Mesh(geometry=point_geom, material=point_mat) self.point1.pos.x = sumx / sumcount self.point1.pos.y = sumy / sumcount self.point1.pos.z = sumz / sumcount self.point1.scale = (1, 1, 1) #scene.add(self.point1) self.camera = PerspectiveCamera( fov=100, # размер окна т.е. чем больше фов тем больше масштаб aspect=0, # "screen" ratio near=1, # рендер от far=10000 # дистанция рендера ) k0 = 0 self.ll = [] for i in soe: for j in i: if (j == 1): self.ll.append(lines_list[k0]) scene.add(lines_list[k0]) k0 += 1 for i in range(counter): scene.add(point_list[i]) pass self.pp = point_list self.renderer.render(scene, self.camera) self.renderer.bind(size=self._adjust_aspect) al.add_widget(self.renderer) bl.add_widget(Factory.Fov()) bl.add_widget(Factory.CamNav()) al.add_widget(bl) return al