basically chunk loading
This commit is contained in:
@@ -14,7 +14,7 @@ TILE_WIDTH = 16.0
|
||||
TILE_HEIGHT = 16.0
|
||||
TILE_DEPTH = 11.36
|
||||
|
||||
def createQuadForTile(model, tileIndex, x=0, y=0, z=0):
|
||||
def createQuadForTile(tileIndex, x=0, y=0, z=0):
|
||||
vertices = []
|
||||
indices = []
|
||||
|
||||
@@ -48,35 +48,33 @@ def createQuadForTile(model, tileIndex, x=0, y=0, z=0):
|
||||
'indices': indices
|
||||
}
|
||||
|
||||
def processMap(asset):
|
||||
cache = assetGetCache(asset['path'])
|
||||
if cache is not None:
|
||||
def processChunk(path):
|
||||
cache = assetGetCache(path)
|
||||
if cache:
|
||||
return cache
|
||||
|
||||
|
||||
# Read input file as JSON
|
||||
with open(asset['path'], 'r') as f:
|
||||
with open(path, 'r') as f:
|
||||
inData = json.load(f)
|
||||
|
||||
# Create output object 'map' with default tile indexes and models array
|
||||
map = {
|
||||
chunk = {
|
||||
'tiles': [0] * CHUNK_TILE_COUNT,
|
||||
'models': []
|
||||
}
|
||||
|
||||
# Create a simple 3D model object
|
||||
model = {
|
||||
baseModel = {
|
||||
'vertices': [],
|
||||
'indices': [],
|
||||
'vertexCount': 0,
|
||||
'indexCount': 0
|
||||
}
|
||||
|
||||
# Append the model to map.models
|
||||
map['models'].append(model)
|
||||
# Append the model to chunk.models
|
||||
chunk['models'].append(baseModel)
|
||||
|
||||
for i, tile in enumerate(inData['tiles']):
|
||||
# Set to map
|
||||
map['tiles'][i] = tile
|
||||
# Set to chunk
|
||||
chunk['tiles'][i] = tile
|
||||
|
||||
# Calculate x, y, z from i
|
||||
x = i % CHUNK_WIDTH
|
||||
@@ -84,27 +82,27 @@ def processMap(asset):
|
||||
z = i // (CHUNK_WIDTH * CHUNK_HEIGHT)
|
||||
|
||||
# Add tile 3D model
|
||||
result = createQuadForTile(model, tile, x, y, z)
|
||||
result = createQuadForTile(tile, x, y, z)
|
||||
if len(result['vertices']) > 0:
|
||||
base = len(model['vertices'])
|
||||
base = len(baseModel['vertices'])
|
||||
quad_indices = [base + idx for idx in result['indices']]
|
||||
model['vertices'].extend(result['vertices'])
|
||||
model['indices'].extend(quad_indices)
|
||||
model['vertexCount'] = len(model['vertices'])
|
||||
model['indexCount'] = len(model['indices'])
|
||||
baseModel['vertices'].extend(result['vertices'])
|
||||
baseModel['indices'].extend(quad_indices)
|
||||
baseModel['vertexCount'] = len(baseModel['vertices'])
|
||||
baseModel['indexCount'] = len(baseModel['indices'])
|
||||
|
||||
# Generate binary buffer for efficient output
|
||||
buffer = bytearray()
|
||||
buffer.extend(b'DMF')# Header
|
||||
buffer.extend(len(map['tiles']).to_bytes(4, 'little')) # Number of tiles
|
||||
buffer.extend(len(map['models']).to_bytes(1, 'little')) # Number of models
|
||||
buffer.extend(b'DCF')# Header
|
||||
buffer.extend(len(chunk['tiles']).to_bytes(4, 'little')) # Number of tiles
|
||||
buffer.extend(len(chunk['models']).to_bytes(1, 'little')) # Number of models
|
||||
|
||||
# Buffer tile data as array of uint8_t
|
||||
for tileIndex in map['tiles']:
|
||||
for tileIndex in chunk['tiles']:
|
||||
buffer.append(tileIndex.to_bytes(1, 'little')[0])
|
||||
|
||||
# For each model
|
||||
for model in map['models']:
|
||||
for model in chunk['models']:
|
||||
# Write vertex count and index count
|
||||
buffer.extend(model['vertexCount'].to_bytes(4, 'little'))
|
||||
# buffer.extend(model['indexCount'].to_bytes(4, 'little'))
|
||||
@@ -129,17 +127,42 @@ def processMap(asset):
|
||||
buffer.extend(bytearray(struct.pack('<f', z)))
|
||||
|
||||
# Write out map file
|
||||
relative = getAssetRelativePath(asset['path'])
|
||||
fileNameWithoutExt = os.path.splitext(os.path.basename(asset['path']))[0]
|
||||
outputFileRelative = os.path.join(os.path.dirname(relative), f"{fileNameWithoutExt}.dmf")
|
||||
relative = getAssetRelativePath(path)
|
||||
fileNameWithoutExt = os.path.splitext(os.path.basename(path))[0]
|
||||
outputFileRelative = os.path.join(os.path.dirname(relative), f"{fileNameWithoutExt}.dcf")
|
||||
outputFilePath = os.path.join(args.output_assets, outputFileRelative)
|
||||
os.makedirs(os.path.dirname(outputFilePath), exist_ok=True)
|
||||
with open(outputFilePath, "wb") as f:
|
||||
f.write(buffer)
|
||||
|
||||
outMap = {
|
||||
outChunk = {
|
||||
'files': [ outputFilePath ],
|
||||
'map': map
|
||||
'chunk': chunk
|
||||
}
|
||||
|
||||
return assetCache(path, outChunk)
|
||||
|
||||
|
||||
def processMap(asset):
|
||||
cache = assetGetCache(asset['path'])
|
||||
if cache is not None:
|
||||
return cache
|
||||
|
||||
# Path provided should be a directory.
|
||||
if not os.path.isdir(asset['path']):
|
||||
print(f"Error: Asset path {asset['path']} is not a directory.")
|
||||
sys.exit(1)
|
||||
|
||||
# List files
|
||||
chunkFiles = []
|
||||
for fileName in os.listdir(asset['path']):
|
||||
if not fileName.endswith('.json'):
|
||||
continue
|
||||
result = processChunk(os.path.join(asset['path'], fileName))
|
||||
chunkFiles.extend(result['files'])
|
||||
|
||||
outMap = {
|
||||
'files': chunkFiles
|
||||
}
|
||||
|
||||
return assetCache(asset['path'], outMap)
|
||||
Reference in New Issue
Block a user