def build_scene(color_array): # Generate positions of each cube cube_position_array, shift = generate_block_positions(args.num_cubes) assert len(cube_position_array) == args.num_cubes # Place block scene = rtx.Scene(ambient_color=(0, 0, 0)) for position in cube_position_array: geometry = rtx.BoxGeometry(1, 1, 1) geometry.set_position(( position[0] - shift[0], position[1] - shift[1], position[2] - shift[2], )) material = rtx.LambertMaterial(0.3) mapping = rtx.SolidColorMapping(random.choice(color_array)) cube = rtx.Object(geometry, material, mapping) scene.add(cube) # Place lights size = 50 group = rtx.ObjectGroup() geometry = rtx.PlainGeometry(size, size) geometry.set_rotation((0, math.pi / 2, 0)) geometry.set_position((-10, 0, 0)) material = rtx.EmissiveMaterial(10, visible=False) mapping = rtx.SolidColorMapping((1, 1, 1)) light = rtx.Object(geometry, material, mapping) group.add(light) geometry = rtx.PlainGeometry(size, size) geometry.set_rotation((0, -math.pi / 2, 0)) geometry.set_position((10, 0, 0)) material = rtx.EmissiveMaterial(1, visible=False) mapping = rtx.SolidColorMapping((1, 1, 1)) light = rtx.Object(geometry, material, mapping) group.add(light) group.set_rotation((-math.pi / 3, math.pi / 4, 0)) scene.add(group) return scene
material = rtx.LambertMaterial(0.4) mapping = rtx.SolidColorMapping((0, 1, 0)) cylinder = rtx.Object(geometry, material, mapping) scene.add(cylinder) # place cone geometry = rtx.ConeGeometry(0.5, 1) geometry.set_position((2, -1, 0)) material = rtx.LambertMaterial(0.4) mapping = rtx.SolidColorMapping((1, 0, 0)) cone = rtx.Object(geometry, material, mapping) scene.add(cone) # Place lights size = 50 group = rtx.ObjectGroup() geometry = rtx.PlainGeometry(size, size) geometry.set_rotation((0, math.pi / 2, 0)) geometry.set_position((-10, 0, 0)) material = rtx.EmissiveMaterial(3, visible=False) mapping = rtx.SolidColorMapping((1, 1, 1)) light = rtx.Object(geometry, material, mapping) group.add(light) group.set_rotation((-math.pi / 3, math.pi / 2, 0)) scene.add(group) screen_width = 64 screen_height = 64 rt_args = rtx.RayTracingArguments()
def build_dice(mnist_images): assert len(mnist_images) == 6 dice = rtx.ObjectGroup() # 1 geometry = rtx.PlainGeometry(1, 1) geometry.set_position((0, 0, 0.5)) material = rtx.LambertMaterial(0.95) mapping = build_mapping(mnist_images[0]) face = rtx.Object(geometry, material, mapping) dice.add(face) # 2 geometry = rtx.PlainGeometry(1, 1) geometry.set_rotation((0, -math.pi, 0)) geometry.set_position((0, 0, -0.5)) material = rtx.LambertMaterial(0.95) mapping = build_mapping(mnist_images[1]) face = rtx.Object(geometry, material, mapping) dice.add(face) # 3 geometry = rtx.PlainGeometry(1, 1) geometry.set_rotation((0, math.pi / 2, 0)) geometry.set_position((0.5, 0, 0)) material = rtx.LambertMaterial(0.95) mapping = build_mapping(mnist_images[2]) face = rtx.Object(geometry, material, mapping) dice.add(face) # 4 geometry = rtx.PlainGeometry(1, 1) geometry.set_rotation((0, -math.pi / 2, 0)) geometry.set_position((-0.5, 0, 0)) material = rtx.LambertMaterial(0.95) mapping = build_mapping(mnist_images[3]) face = rtx.Object(geometry, material, mapping) dice.add(face) # 5 geometry = rtx.PlainGeometry(1, 1) geometry.set_rotation((math.pi / 2, 0, 0)) geometry.set_position((0, -0.5, 0)) material = rtx.LambertMaterial(0.95) mapping = build_mapping(mnist_images[4]) face = rtx.Object(geometry, material, mapping) dice.add(face) # 5 geometry = rtx.PlainGeometry(1, 1) geometry.set_rotation((-math.pi / 2, 0, 0)) geometry.set_position((0, 0.5, 0)) material = rtx.LambertMaterial(0.95) mapping = build_mapping(mnist_images[5]) face = rtx.Object(geometry, material, mapping) dice.add(face) dice.set_scale((2, 2, 2)) return dice
def build_scene(mnist_image_array, wall_texture_filename_array, floor_texture_filename_array, grid_size=8): assert len(mnist_image_array) == 6 wall_height = 3 eps = 0.1 scene = rtx.Scene(ambient_color=(0.5, 1, 1)) texture = load_texture_image(random.choice(wall_texture_filename_array)) mapping = build_mapping(texture, grid_size / wall_height) # 1 geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, 0, 0)) geometry.set_position((0, 0, -grid_size / 2)) material = rtx.LambertMaterial(0.95) wall = rtx.Object(geometry, material, mapping) scene.add(wall) # 2 geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, -math.pi / 2, 0)) geometry.set_position((grid_size / 2, 0, 0)) material = rtx.LambertMaterial(0.95) wall = rtx.Object(geometry, material, mapping) scene.add(wall) # 3 geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, math.pi, 0)) geometry.set_position((0, 0, grid_size / 2)) material = rtx.LambertMaterial(0.95) wall = rtx.Object(geometry, material, mapping) scene.add(wall) # 4 geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, math.pi / 2, 0)) geometry.set_position((-grid_size / 2, 0, 0)) material = rtx.LambertMaterial(0.95) wall = rtx.Object(geometry, material, mapping) scene.add(wall) # floor geometry = rtx.PlainGeometry(grid_size + eps, grid_size + eps) geometry.set_rotation((-math.pi / 2, 0, 0)) geometry.set_position((0, -wall_height / 2, 0)) material = rtx.LambertMaterial(0.95) texture = load_texture_image(random.choice(floor_texture_filename_array)) mapping = build_mapping(texture, scale=0.5) floor = rtx.Object(geometry, material, mapping) scene.add(floor) # Place lights ## Primary light primary_light = rtx.ObjectGroup() geometry = rtx.SphereGeometry(2) material = rtx.EmissiveMaterial(40, visible=False) mapping = rtx.SolidColorMapping((1, 1, 1)) light = rtx.Object(geometry, material, mapping) primary_light.add(light) spread = grid_size / 2 - 1 primary_light.set_position((spread * random.uniform(-1, 1), 8, spread * random.uniform(-1, 1))) scene.add(primary_light) ## Ambient light ambient_lights = rtx.ObjectGroup() geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, 0, 0)) geometry.set_position((0, 0, -grid_size / 2)) material = rtx.EmissiveMaterial(1, visible=False) wall = rtx.Object(geometry, material, mapping) ambient_lights.add(wall) geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, -math.pi / 2, 0)) geometry.set_position((grid_size / 2, 0, 0)) material = rtx.EmissiveMaterial(1, visible=False) wall = rtx.Object(geometry, material, mapping) ambient_lights.add(wall) geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, math.pi, 0)) geometry.set_position((0, 0, grid_size / 2)) material = rtx.EmissiveMaterial(1, visible=False) wall = rtx.Object(geometry, material, mapping) ambient_lights.add(wall) geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, math.pi / 2, 0)) geometry.set_position((-grid_size / 2, 0, 0)) material = rtx.EmissiveMaterial(1, visible=False) wall = rtx.Object(geometry, material, mapping) ambient_lights.add(wall) ambient_lights.set_position((0, wall_height, 0)) scene.add(ambient_lights) # Place a dice dice = build_dice(mnist_image_array) spread = grid_size / 3 dice.set_position((spread * random.uniform(-1, 1), 1 - wall_height / 2, spread * random.uniform(-1, 1))) dice.set_rotation((0, random.uniform(0, math.pi * 2), 0)) scene.add(dice) return scene
def build_scene(color_array, wall_texture_filename_array, floor_texture_filename_array, grid_size, wall_height): eps = 0.1 scene = rtx.Scene(ambient_color=(0.5, 1, 1)) texture = load_texture_image(random.choice(wall_texture_filename_array)) mapping = build_mapping(texture, grid_size / wall_height) # 1 geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, 0, 0)) geometry.set_position((0, 0, -grid_size / 2)) material = rtx.LambertMaterial(0.95) wall = rtx.Object(geometry, material, mapping) scene.add(wall) # 2 geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, -math.pi / 2, 0)) geometry.set_position((grid_size / 2, 0, 0)) material = rtx.LambertMaterial(0.95) wall = rtx.Object(geometry, material, mapping) scene.add(wall) # 3 geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, math.pi, 0)) geometry.set_position((0, 0, grid_size / 2)) material = rtx.LambertMaterial(0.95) wall = rtx.Object(geometry, material, mapping) scene.add(wall) # 4 geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, math.pi / 2, 0)) geometry.set_position((-grid_size / 2, 0, 0)) material = rtx.LambertMaterial(0.95) wall = rtx.Object(geometry, material, mapping) scene.add(wall) # floor geometry = rtx.PlainGeometry(grid_size + eps, grid_size + eps) geometry.set_rotation((-math.pi / 2, 0, 0)) geometry.set_position((0, -wall_height / 2, 0)) material = rtx.LambertMaterial(0.95) texture = load_texture_image(random.choice(floor_texture_filename_array)) mapping = build_mapping(texture, scale=0.5) floor = rtx.Object(geometry, material, mapping) scene.add(floor) # Place lights ## Primary light primary_light = rtx.ObjectGroup() geometry = rtx.SphereGeometry(2) material = rtx.EmissiveMaterial(40, visible=False) mapping = rtx.SolidColorMapping((1, 1, 1)) light = rtx.Object(geometry, material, mapping) primary_light.add(light) spread = grid_size / 2 - 1 primary_light.set_position( (spread * random.uniform(-1, 1), 8, spread * random.uniform(-1, 1))) scene.add(primary_light) ## Ambient light ambient_lights = rtx.ObjectGroup() geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, 0, 0)) geometry.set_position((0, 0, -grid_size / 2)) material = rtx.EmissiveMaterial(1, visible=False) wall = rtx.Object(geometry, material, mapping) ambient_lights.add(wall) geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, -math.pi / 2, 0)) geometry.set_position((grid_size / 2, 0, 0)) material = rtx.EmissiveMaterial(1, visible=False) wall = rtx.Object(geometry, material, mapping) ambient_lights.add(wall) geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, math.pi, 0)) geometry.set_position((0, 0, grid_size / 2)) material = rtx.EmissiveMaterial(1, visible=False) wall = rtx.Object(geometry, material, mapping) ambient_lights.add(wall) geometry = rtx.PlainGeometry(grid_size + eps, wall_height) geometry.set_rotation((0, math.pi / 2, 0)) geometry.set_position((-grid_size / 2, 0, 0)) material = rtx.EmissiveMaterial(1, visible=False) wall = rtx.Object(geometry, material, mapping) ambient_lights.add(wall) ambient_lights.set_position((0, wall_height, 0)) scene.add(ambient_lights) # Place objects r = grid_size // 4 r2 = r * 2 object_positions = generate_object_positions(args.num_objects, r2 - 1) for position_index in object_positions: geometry_type = random.choice(geometry_type_array) geometry = build_geometry_by_type(geometry_type) geometry.set_rotation((0, random.uniform(0, math.pi * 2), 0)) noise = np.random.uniform(-0.125, 0.125, size=2) spread = 1.5 geometry.set_position(( spread * (position_index[0] - r + 1) + noise[0], -wall_height / 2 + 0.5, spread * (position_index[1] - r + 1) + noise[1], )) material = rtx.LambertMaterial(0.9) color = random.choice(color_array) mapping = rtx.SolidColorMapping(color) obj = rtx.Object(geometry, material, mapping) scene.add(obj) return scene
def place_dice(scene, mnist_images, discrete_position=False, rotate_dice=False): indices = np.random.choice(np.arange(len(mnist_images)), replace=False, size=6) dice = rtx.ObjectGroup() # 1 geometry = rtx.PlainGeometry(1, 1) geometry.set_position((0, 0, 0.5)) material = rtx.LambertMaterial(0.95) mapping = generate_texture_mapping(mnist_images[indices[0]]) face = rtx.Object(geometry, material, mapping) dice.add(face) # 2 geometry = rtx.PlainGeometry(1, 1) geometry.set_rotation((0, -math.pi, 0)) geometry.set_position((0, 0, -0.5)) material = rtx.LambertMaterial(0.95) mapping = generate_texture_mapping(mnist_images[indices[1]]) face = rtx.Object(geometry, material, mapping) dice.add(face) # 3 geometry = rtx.PlainGeometry(1, 1) geometry.set_rotation((0, math.pi / 2, 0)) geometry.set_position((0.5, 0, 0)) material = rtx.LambertMaterial(0.95) mapping = generate_texture_mapping(mnist_images[indices[2]]) face = rtx.Object(geometry, material, mapping) dice.add(face) # 4 geometry = rtx.PlainGeometry(1, 1) geometry.set_rotation((0, -math.pi / 2, 0)) geometry.set_position((-0.5, 0, 0)) material = rtx.LambertMaterial(0.95) mapping = generate_texture_mapping(mnist_images[indices[3]]) face = rtx.Object(geometry, material, mapping) dice.add(face) # 5 geometry = rtx.PlainGeometry(1, 1) geometry.set_rotation((math.pi / 2, 0, 0)) geometry.set_position((0, -0.5, 0)) material = rtx.LambertMaterial(0.95) mapping = generate_texture_mapping(mnist_images[indices[4]]) face = rtx.Object(geometry, material, mapping) dice.add(face) # 6 geometry = rtx.PlainGeometry(1, 1) geometry.set_rotation((-math.pi / 2, 0, 0)) geometry.set_position((0, 0.5, 0)) material = rtx.LambertMaterial(0.95) mapping = generate_texture_mapping(mnist_images[indices[5]]) face = rtx.Object(geometry, material, mapping) dice.add(face) dice.set_scale((1.5, 1.5, 1.5)) directions = [-1.0, 0.0, 1.0] available_positions = [] for z in directions: for x in directions: available_positions.append((x, z)) xz = np.array(random.choice(available_positions)) if discrete_position == False: xz += np.random.uniform(-0.25, 0.25, size=xz.shape) dice.set_position((xz[0], 0.75, xz[1])) if rotate_dice: yaw = np.random.uniform(0, math.pi * 2, size=1)[0] dice.set_rotation((0, yaw, 0)) scene.add(dice)