Chunks, models, and more
This commit is contained in:
@@ -26,8 +26,8 @@ JSON input (assetsraw/chunks/chunk_X_Y_Z.json):
|
||||
|
||||
Version 3 DCF format (after 8-byte header + tiles):
|
||||
uint8_t meshCount
|
||||
for each mesh:
|
||||
null-terminated string (relative asset path to .dmf)
|
||||
for each model:
|
||||
null-terminated string (relative asset path to .json model)
|
||||
float32[3] (x, y, z offset)
|
||||
|
||||
DMF format:
|
||||
@@ -89,11 +89,11 @@ def tile_index(x, y, z):
|
||||
return x + y * CHUNK_WIDTH + z * CHUNK_WIDTH * CHUNK_HEIGHT
|
||||
|
||||
|
||||
def find_mesh(filename):
|
||||
"""Search ASSETS_DIR/meshes/ recursively for filename.
|
||||
def find_model(filename):
|
||||
"""Search ASSETS_DIR/models/ recursively for filename.
|
||||
Returns the path relative to ASSETS_DIR (forward slashes), or None."""
|
||||
meshes_root = os.path.join(ASSETS_DIR, 'meshes')
|
||||
for dirpath, _, filenames in os.walk(meshes_root):
|
||||
models_root = os.path.join(ASSETS_DIR, 'models')
|
||||
for dirpath, _, filenames in os.walk(models_root):
|
||||
if filename in filenames:
|
||||
rel = os.path.relpath(
|
||||
os.path.join(dirpath, filename), ASSETS_DIR
|
||||
@@ -102,6 +102,17 @@ def find_mesh(filename):
|
||||
return None
|
||||
|
||||
|
||||
def write_model_json(path, mesh_rel, texture_rel, color):
|
||||
"""Write a model JSON descriptor file."""
|
||||
obj = {'mesh': mesh_rel, 'color': color}
|
||||
if texture_rel:
|
||||
obj['texture'] = texture_rel
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
with open(path, 'w') as f:
|
||||
json.dump(obj, f, indent=2)
|
||||
print(f' Wrote model JSON {path}')
|
||||
|
||||
|
||||
def derive_dcf_path(json_path):
|
||||
"""assetsraw/chunks/chunk_X_Y_Z.json -> assets/chunks/X_Y_Z.dcf"""
|
||||
base = os.path.splitext(os.path.basename(json_path))[0]
|
||||
@@ -228,31 +239,45 @@ def from_json(json_path, dcf_path):
|
||||
)
|
||||
|
||||
terrain_verts = build_terrain_verts(tiles)
|
||||
mesh_names = []
|
||||
model_names = []
|
||||
mesh_offsets = []
|
||||
|
||||
# Terrain mesh (only written if non-empty)
|
||||
dcf_base = os.path.splitext(os.path.basename(dcf_path))[0]
|
||||
|
||||
# Terrain mesh + model (only written if non-empty)
|
||||
terrain_dir = os.path.join(ASSETS_DIR, 'meshes', 'chunks')
|
||||
models_dir = os.path.join(ASSETS_DIR, 'models', 'chunks')
|
||||
os.makedirs(terrain_dir, exist_ok=True)
|
||||
os.makedirs(models_dir, exist_ok=True)
|
||||
if terrain_verts:
|
||||
dmf_name = f'chunk_{dcf_base}_0.dmf'
|
||||
write_dmf(os.path.join(terrain_dir, dmf_name), terrain_verts)
|
||||
mesh_names.append(f'meshes/chunks/{dmf_name}')
|
||||
mesh_rel = f'meshes/chunks/{dmf_name}'
|
||||
json_name = f'chunk_{dcf_base}_0.json'
|
||||
write_model_json(
|
||||
os.path.join(models_dir, json_name),
|
||||
mesh_rel,
|
||||
'tiles.png',
|
||||
[255, 255, 255, 255]
|
||||
)
|
||||
model_names.append(f'models/chunks/{json_name}')
|
||||
mesh_offsets.append((0.0, 0.0, 0.0))
|
||||
|
||||
# External mesh references
|
||||
# External mesh references -> look up matching model JSON
|
||||
for mesh in data.get('meshes', []):
|
||||
filename = mesh['file']
|
||||
pos = mesh.get('pos', [0, 0, 0])
|
||||
rel = find_mesh(filename)
|
||||
model_filename = os.path.splitext(filename)[0] + '.json'
|
||||
rel = find_model(model_filename)
|
||||
if rel is None:
|
||||
raise ValueError(f"Mesh not found under assets/meshes/: {filename}")
|
||||
mesh_names.append(rel)
|
||||
raise ValueError(
|
||||
f"Model not found under assets/models/: {model_filename}"
|
||||
)
|
||||
model_names.append(rel)
|
||||
mesh_offsets.append((float(pos[0]), float(pos[1]), float(pos[2])))
|
||||
print(f' Resolved {filename} -> {rel}')
|
||||
|
||||
write_v3(dcf_path, bytes(tiles), mesh_names, mesh_offsets)
|
||||
write_v3(dcf_path, bytes(tiles), model_names, mesh_offsets)
|
||||
|
||||
|
||||
def process_json(json_path):
|
||||
|
||||
Reference in New Issue
Block a user