Пример #1
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