-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_dataset.py
175 lines (154 loc) · 7.44 KB
/
generate_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# This script runs in Blender's Python environment!
import opensimplex
import random
import bpy
from bpy import context
from mathutils import Vector
import numpy as np
import math
import bmesh
import os
import sys
sys.path.append(".") # <--- voxel_sight directory packages
from config import *
# TODO: Should be able to import config package by manually specifying search directory
class TerrainNoiseLayer():
scale = 1.0
multiplier = 1.0
seed = random.randint(0, 10000000)
def __init__(self, scale, multiplier):
self.scale = scale
self.multiplier = multiplier
def set_random_seed(self):
self.seed = random.randint(0, 10000000)
voxel_type_seed = random.randint(0, 10000000)
noise_layers = [TerrainNoiseLayer(1.0, 0.5), TerrainNoiseLayer(0.1, 3.0), TerrainNoiseLayer(0.02, 10.0)]
pool_collection_name = "voxels"
block_size = 0.0
chunk_size = 8
# TODO: Will need to create a distinct placer for each voxel type, or encode objects in vertex data
placer = bpy.data.objects["placer"]
def choose_random_object_from_pool(objects):
return random.choice(objects)
def spawn_voxel_occlusion_heightmap(noise, verts, colors, rgb, start_x, start_y, width, height, scale):
for y in range(height):
for x in range(width):
n = noise[int(start_y) + y][int(start_x) + x]
location = Vector(((start_x + x), (start_y + y), round(n)))
byte_color = [64+int(start_x*2+x), 64+int(start_y*2+y), int(random.randint(0, 255))]
color = [byte_color[0]/255.0, byte_color[1]/255.0, byte_color[2]/255.0, 1.0]
verts.append(location)
colors.append(color)
rgb.append(byte_color)
def spawn_voxel_heightmap(noise, verts, start_x, start_y, width, height, scale):
for y in range(height):
for x in range(width):
n = noise[int(start_y) + y][int(start_x) + x]
location = Vector(((start_x + x), (start_y + y), round(n)))
verts.append(location)
def get_heightmap_baseline():
pass
def spawn_occlusion_chunks(radius, noise, d, dir):
mesh = bpy.data.meshes.new("example")
ob = bpy.data.objects.new("placer", mesh)
bpy.context.collection.objects.link(ob)
ob.location = Vector((0,0,0))
verts = []
rgb = []
colors = []
chunk_origin = Vector((0.0, 0.0, 0.0))
spawn_voxel_occlusion_heightmap(noise, verts, colors, rgb, chunk_origin.x, chunk_origin.y, chunk_size * 2, chunk_size * 2, 1.0)
# for y in range(0, radius):
# for x in range(0, radius*2):
# chunk_origin = Vector((x*chunk_size, y*chunk_size, 0.0))
# spawn_voxel_occlusion_heightmap(noise, verts, colors, rgb, chunk_origin.x, chunk_origin.y, chunk_size, chunk_size, 1.0)
mesh.from_pydata(verts, [], [])
bpy.context.view_layer.objects.active = ob
bpy.ops.object.modifier_add(type='NODES')
bpy.context.object.modifiers['GeometryNodes'].node_group = bpy.data.node_groups['Geometry Nodes']
bpy.context.object.modifiers['GeometryNodes']['Input_2'] = bpy.data.objects['occlusion_voxel']
bpy.ops.geometry.color_attribute_add(name="color", color=(1.0, 0, 0.0, 1))
player_floor = noise[0][chunk_size*radius]
ob.location.z = -player_floor*block_size
ob.scale = Vector((block_size, block_size, block_size))
for i, cd in enumerate(bpy.context.active_object.data.attributes['color'].data):
cd.color = colors[i]
with open(os.path.join(dir, 'voxel_scene_{}'.format(d)), 'w+', encoding='utf-8') as vs:
for i in range(len(verts)):
vs.write("{}/{}/{}\n".format(str(i), str(rgb[i]), str(int((verts[i].z-player_floor)))))
return ob
def spawn_chunks(radius, noise):
terrain = []
verts = []
chunk_origin = Vector((0.0, 0.0, 0.0))
spawn_voxel_heightmap(noise, verts, chunk_origin.x, chunk_origin.y, chunk_size * 2, chunk_size * 2, 1.0)
# for y in range(0, radius):
# for x in range(0, radius*2):
# chunk_origin = Vector((x*chunk_size, y*chunk_size, 0.0))
voxel_types = ['dirt', 'grass', 'sand', 'stone']
voxel_verts = {}
for vt in voxel_types:
voxel_verts[vt] = []
for vert in verts:
voxel_verts[voxel_types[random.randint(0, len(voxel_types)-1)]].append(vert)
for i, vt in enumerate(voxel_types):
mesh = bpy.data.meshes.new(vt + "voxel_geo")
ob = bpy.data.objects.new(vt + "_placer", mesh)
bpy.context.collection.objects.link(ob)
ob.location = Vector((0,0,0))
mesh.from_pydata(voxel_verts[vt], [], [])
bpy.context.view_layer.objects.active = ob
bpy.ops.object.modifier_add(type='NODES')
bpy.context.object.modifiers['GeometryNodes'].node_group = bpy.data.node_groups['Geometry Nodes.001']
bpy.context.object.modifiers['GeometryNodes']['Input_2'] = bpy.data.objects[vt + '_voxel']
ob.scale = Vector((block_size, block_size, block_size))
player_floor = noise[0][chunk_size*radius]
ob.location.z = -player_floor*block_size
terrain.append(ob)
bpy.data.objects["Camera"].location.x = chunk_size*radius*block_size
bpy.data.objects["Camera"].location.y = 0
return terrain
bpy.context.scene.render.image_settings.color_depth = "16"
bpy.context.scene.render.image_settings.compression = 0
bpy.context.scene.render.resolution_x = screen_size[2]
bpy.context.scene.render.resolution_y = screen_size[1]
dir = "train"
data_count = 10
def generate_scene_data(dir, data_count):
global block_size
global noise_layers
for d in range(data_count):
# Recreate noise layers for random seeds!
for layer in noise_layers:
layer.set_random_seed() # TODO: Use a deterministic seed chain for replicability
# Make voxels random sizes to make the prediction robust to distinct voxel grid sizes
# 0.1 to 0.5
block_size = max(0.1, random.random()/2.0)
# block_size = 0.2
bpy.data.objects["Camera"].location.z = block_size * 1.8
bpy.context.scene.view_settings.view_transform = 'Filmic'
# Rotate camera randomly (always pointing forward to ensure reward is met)
bpy.data.objects["Camera"].rotation_euler.x = math.radians(random.randint(45, 115))
bpy.data.objects["Camera"].rotation_euler.z = math.radians(random.randint(-44, 44))
# Spawn random terrain
radius = 1
noise = np.zeros(shape=[chunk_size*radius*2, chunk_size*radius*2])
for layer in noise_layers:
opensimplex.seed(layer.seed)
ix, iy = np.array([x * layer.scale for x in range(chunk_size*radius*2)]), np.array([y * layer.scale for y in range(chunk_size*radius*2)])
noise = np.add(noise, opensimplex.noise2array(ix, iy)*layer.multiplier)
# Render voxel terrain
terrain = spawn_chunks(radius, noise)
bpy.context.scene.render.filepath = dir + "/" + "voxels_%d.png" % d
bpy.ops.render.render(write_still = True)
for ob in terrain:
bpy.data.objects.remove(ob, do_unlink=True)
# Destroy textured voxels
# Render occlusion terrain
occlusion = spawn_occlusion_chunks(radius, noise, d, dir)
bpy.context.scene.view_settings.view_transform = 'Raw'
bpy.context.scene.render.filepath = os.path.join(dir, "occlusion_elements_%d.png" % d)
bpy.ops.render.render(write_still = True)
bpy.data.objects.remove(occlusion, do_unlink=True)
for dataset in datasets.items():
generate_scene_data(os.path.join("data", dataset[0]), dataset[1])