Example #1
0
    def __init__(self, resource, parent_node):
        """Constructor.

        :param resource: The terrain resource
        :type resource: :class:`loaders.Resource`

        :param parent_node: Parent node to attach the terrain entity to.
        :type param_node: subclass of :class:`renderer.SceneNode`
        """
        shader = resource['shader']
        mesh = resource['floor_mesh']
        texture = Texture.from_image(resource['floor_texture'])
        # shader params
        params = {
            'tex': texture,
            'animate': 0,
        }
        renderable = Renderable(
            parent_node,
            mesh,
            shader,
            params,
            textures=[texture],
            enable_light=True)

        super().__init__(renderable)

        # FIXME: this offset here is due to the calculation of the walkable
        # matrix that adds one more walkable line on top of the scenario.
        self[Renderable].transform.translate(Vec(0.0, 0.0, 1.0))
Example #2
0
    def __init__(self, resource, position, progress, completed, parent_node):
        """Constructor.

        :param resource: The building resource
        :type resource: :class:`loaders.Resource`

        :param position: The position of the building
        :type position: :class:`tuple`

        :param progress: The current amount of hp and the total one
        :type progress: :class:`tuple`

        :param completed: Whether the building is completed or not
        :type completed: :class:`bool`

        :param parent_node: The parent node in the scene graph
        :type parent_node: :class:`renderer.scene.SceneNode`
        """
        self._position = position
        # Progress is going to be a property used to update only when necessary
        # the health bar.
        self._progress = progress
        self.completed = completed

        shader = resource['shader']
        texture = Texture.from_image(resource['texture'])
        self.mesh_project = resource['model_project']
        self.mesh_complete = resource['model_complete']

        # Setup the group node and add the health bar
        group_node = SceneNode()
        g_transform = group_node.transform
        g_transform.translate(to_scene(*position))
        parent_node.add_child(group_node)

        self.health_bar = HealthBar(
            resource['health_bar'], progress[0] / progress[1], group_node)

        params = {
            'tex': texture,
        }

        # create components
        renderable = Renderable(
            group_node,
            self.mesh,
            shader,
            params,
            textures=[texture],
            enable_light=True)

        # initialize entity
        super().__init__(renderable)

        # FIXME: hardcoded bounding box
        self._bounding_box = Vec(-0.5, 0, -0.5), Vec(0.5, 2, 0.5)
Example #3
0
def create_holder(name, x, y=0, z=0, s=1, r=(0, 0, 0), t=0, taichi_s=0.9):
    material = tc.create_surface_material('pbr')
    rep = Texture.create_taichi_wallpaper(20, rotation=t, scale=taichi_s)
    diff = 0.1 * rep
    spec = 0.6 * rep
    material.initialize(
        P(diffuse_map=diff.id, specular_map=spec.id, glossiness=300))

    mesh = tc.create_mesh()
    mesh.initialize(P(filename=map_filename(name)))
    mesh.set_material(material)
    mesh.translate(Vector(x, y, z))
    mesh.scale_s(s)
    mesh.rotate_euler(Vector(*r))
    return mesh
Example #4
0
    def __init__(self, resource, ref):
        self.w = resource.data['width']
        self.h = resource.data['height']

        mesh = Rect(self.w, self.h)
        texture = Texture.from_image(resource[ref])
        shader = resource['shader']

        params = {
            'tex': texture,
            'width': self.w,
            'height': self.h,
        }

        self.node = GeometryNode(
            mesh,
            shader,
            params=params,
            textures=[texture],
            enable_light=False)
Example #5
0
    def __init__(self, resource, parent_node):
        """Constructor.

        :param resource: Resource containing map data.
        :type resource: :class:`resource_manager.Resource`

        :param parent_node: Node to attach the map to.
        :type parent_node: :class:`renderer.scene.SceneNode`
        """
        shader = resource['shader']
        mesh = resource['walls_mesh']
        texture = Texture.from_image(resource['walls_texture'])
        # shader params
        params = {
            'tex': texture,
            'animate': 0,
            'opacity': 1.0,
            'color_ambient': Vec(0, 0, 0, 1),
            'color_diffuse': Vec(0, 0, 0, 1),
            'color_specular': Vec(0.1, 0.1, 0.1, 1),
        }
        renderable = Renderable(
            parent_node,
            mesh,
            shader,
            params,
            textures=[texture],
            enable_light=True)

        super().__init__(renderable)

        # FIXME: this offset here is due to the calculation of the walkable
        # matrix that adds one more walkable line on top of the scenario.
        self[Renderable].transform.translate(Vec(0.0, 0.0, 1.0))

        self.objects = []

        # Initialize static objects
        for obj in resource.data['objects']:
            self.add_object(
                MapObject(resource[obj['ref']], obj, self[Renderable].node))
Example #6
0
    def __init__(self, resource, parameters, parent_node):
        """Constructor.

        :param resource: Resource containing the object data.
        :type resource: :class:`resource_manager.Resource`

        :param parameters: Parameters for the object.
        :type parameters: :class:`dict`

        :param parent_node: Node to attach the object to.
        :type parent_node: :class:`renderer.scene.SceneNode`
        """
        mesh = resource["model"]
        shader = resource["shader"]
        texture = Texture.from_image(resource["texture"])

        # shader params
        params = {
            "tex": texture,
            "animate": 0,
            "opacity": 1.0,
            "color_ambient": Vec(0, 0, 0, 1),
            "color_diffuse": Vec(0, 0, 0, 1),
            "color_specular": Vec(0.1, 0.1, 0.1, 1),
        }

        renderable = Renderable(parent_node, mesh, shader, params, textures=[texture], enable_light=True)

        super().__init__(renderable)

        self[Renderable].transform.translate(to_scene(*parameters["pos"]))

        if "rotation" in parameters:
            self[Renderable].transform.rotate(Y_AXIS, parameters["rotation"] * pi / 180)

        # FIXME: hardcoded bounding box
        self._position = parameters["pos"]
        self._bounding_box = Vec(-0.5, 0.5, -0.5), Vec(0.5, 1.5, 0.5)
Example #7
0
def load_scene(root, fov):
    ROOT = root
    f = json.load(open(ROOT + 'scene.json'))
    bsdfs = f['bsdfs']
    materials = {}
    for bsdf in bsdfs:
        name = bsdf['name']
        print name
        material = tc.create_surface_material('diffusive')
        params = {}

        albedo = bsdf['albedo']
        if isinstance(albedo, float):
            params['diffuse'] = (albedo, albedo, albedo)
        elif isinstance(albedo, list):
            params['diffuse'] = tuple(albedo)
        else:
            tex = Texture('image', filename=ROOT + albedo)
            params['diffuse_map'] = tex.id

        material.initialize(P(**params))
        materials[name] = material

    meshes = []

    for mesh_node in f['primitives']:
        if 'file' in mesh_node:
            # Object
            mesh = tc.create_mesh()
            fn = ROOT + mesh_node['file'][:-4] + '.obj'
            mesh.initialize(P(filename=fn))
            mesh.set_material(materials[mesh_node['bsdf']])
        else:
            # Light source
            mesh = tc.create_mesh()
            mesh.initialize(P(filename='../assets/meshes/plane.obj'))
            material = tc.create_surface_material('emissive')
            e = 1
            material.initialize(P(color=(e, e, e)))
            mesh.set_material(material)
            if 'transform' in mesh_node:
                trans = mesh_node['transform']
                if 'position' in trans:
                    mesh.translate(Vector(*trans['position']))
                if 'rotation' in trans:
                    mesh.rotate_euler(Vector(*trans['rotation']))
        meshes.append(mesh)

    camera_node = f['camera']
    width, height = camera_node['resolution']
    # the FOV value is ?
    #fov = math.degrees(math.atan(27.2 / camera_node['fov']) * 2)

    camera = Camera('perspective',
                    aspect_ratio=float(width) / height,
                    fov_angle=fov,
                    origin=tuple(camera_node['transform']['position']),
                    look_at=tuple(camera_node['transform']['look_at']),
                    up=tuple(camera_node['transform']['up']))

    return (width, height), meshes, camera
Example #8
0
    def __init__(self, resource, actor_type, health, parent_node):
        """Constructor.

        :param resource: The character resource
        :type resource: :class:`loaders.Resource`

        :param health: The current amount of hp and the total one
        :type health: :class:`tuple`

        :param parent_node: The parent node in the scene graph
        :type parent_node: :class:`renderer.scene.SceneNode`
        """
        self.actor_type = actor_type

        # Health is going to be a property used to update only when necessary
        # the health bar.
        self._health = health

        shader = resource['shader']
        mesh = resource['model']['mesh']
        md = resource['model']['mesh_data']

        # root transformation to apply to the mesh
        self.transform = md.transform

        # instantiate animations
        self.init_animations(md)
        self.current_anim = self.animations[action_anim_index(ActionType.idle)]

        texture = Texture.from_image(resource['texture'])

        # shader params
        params = {
            'tex': texture,
            'opacity': 1.0,
            'color_ambient': Vec(0, 0, 0, 1),
            'color_diffuse': Vec(0, 0, 0, 1),
            'color_specular': Vec(0.1, 0.1, 0.1, 1),
        }

        # Initialize movable component
        movable = Movable((0.0, 0.0))

        # Setup the group node and add the health bar
        # FIXME: I don't like the idea of saving the group node here. We need
        # something better here.
        self.group_node = SceneNode()
        g_transform = self.group_node.transform
        g_transform.translate(to_scene(*movable.position))
        parent_node.add_child(self.group_node)

        self.health_bar = HealthBar(
            resource['health_bar'], health[0] / health[1], self.group_node,
            resource.data.get('hb_y_offset'))

        # create components
        renderable = Renderable(
            self.group_node,
            mesh,
            shader,
            params,
            textures=[texture],
            enable_light=True,
            animation=self.current_anim)

        # by default, start with playing animation
        renderable.animate = True

        # initialize actor
        super().__init__(renderable, movable)

        # FIXME: hardcoded bounding box
        self._bounding_box = Vec(-0.5, 0, -0.5), Vec(0.5, 2, 0.5)

        self.heading = 0.0