Пример #1
0
    def position_to_indices(self, position: Vec3):

        offset = position - Vec3(
            self.min_chunk.x * self.level.chunk_unit_width,
            self.level.chunk_unit_height_offset,
            self.min_chunk.z * self.level.chunk_unit_depth)

        return Vec3(int(offset.x // self.voxel_unit_size.x),
                    int(offset.y // self.voxel_unit_size.y),
                    int(offset.z // self.voxel_unit_size.z))
Пример #2
0
    def load_chunk(self, chunk, task):

        voxel_real_size = Vec3(self.level.chunk_unit_width / self.level.chunk_width, self.level.chunk_unit_height / self.level.chunk_voxel_height,
                               self.level.chunk_unit_depth / self.level.chunk_depth)

        chunk_size = Vec3(self.level.chunk_width, self.level.chunk_voxel_height, self.level.chunk_depth)

        chunk = VChunk(chunk_size, voxel_real_size, chunk)
        chunk.show(self)

        return task.done
Пример #3
0
def visualize_layers(reachable_layers):

    layers = {}

    for (x, y, z), label in label_matrix.voxels.items():

        pos = Vec3(x, y, z)

        if label == 0 or label not in reachable_layers:
            continue

        if label in layers:
            layers[label][pos] = True
            continue

        layers[label] = np.zeros(label_matrix.shape, dtype=bool)
        layers[label][pos] = True

    color_gen = randomcolor.RandomColor()
    colors = color_gen.generate(count=len(layers), format_='rgb')

    for i in range(len(colors)):
        values = map(lambda e: int(e), re.findall('\\d+', colors[i]))
        values = list(values)
        colors[i] = (values[0] / 255, values[1] / 255, values[2] / 255)

    meshes = []

    i = 0
    for label in layers:
        color = colors[i]
        meshes.append(Mesh.from_voxel_grid(layers[label], colors=color))
        i += 1

    show(meshes)
Пример #4
0
 def __init__(self, level, voxels, shape, min_chunk, max_chunk):
     self.level = level
     self.voxels = voxels
     self.shape = shape
     self.min_chunk = min_chunk
     self.max_chunk = max_chunk
     self.voxel_unit_size = Vec3(
         self.level.chunk_unit_width / self.level.chunk_width,
         self.level.chunk_unit_height / self.level.chunk_voxel_height,
         self.level.chunk_unit_depth / self.level.chunk_depth)
Пример #5
0
    def __setitem__(self, key, value):

        if not isinstance(key, tuple) or len(key) != 3:
            raise TypeError('item is is a valid index')

        self.validate_index(key[0], 0)
        self.validate_index(key[1], 1)
        self.validate_index(key[2], 2)

        key = Vec3(key[0], key[1], key[2])

        self.voxels[key] = value
        return value
Пример #6
0
    def __getitem__(self, key):

        if not isinstance(key, tuple) or len(key) != 3:
            raise TypeError('item is not a valid index')

        self.validate_index(key[0], 0)
        self.validate_index(key[1], 1)
        self.validate_index(key[2], 2)

        key = Vec3(key[0], key[1], key[2])

        value = self.voxels.get(key)

        if not value:
            return 0

        return value
Пример #7
0
    def from_level(level, center_chunk, distance):

        chunks = [
            chunk for chunk in level.chunks
            if max(abs(center_chunk.x - chunk.x), abs(center_chunk.z -
                                                      chunk.z)) <= distance
        ]

        min_chunk_x = float('inf')
        min_chunk_z = float('inf')
        max_chunk_x = float('-inf')
        max_chunk_z = float('-inf')

        for chunk in chunks:
            min_chunk_x = min(min_chunk_x, chunk.x)
            min_chunk_z = min(min_chunk_z, chunk.z)
            max_chunk_x = max(max_chunk_x, chunk.x)
            max_chunk_z = max(max_chunk_z, chunk.z)

        matrix_chunk_size = Vec2(max_chunk_x - min_chunk_x + 1,
                                 max_chunk_z - min_chunk_z + 1)

        voxels = {}

        for chunk in chunks:

            buffer = BytesIO(chunk.data)

            filled_positions = pickle.load(buffer)

            for x, y, z, v in filled_positions:
                abs_x = (level.chunk_width * (chunk.x - min_chunk_x) + x)
                abs_z = (level.chunk_depth * (chunk.z - min_chunk_z) + z)

                voxels[Vec3(abs_x, y, abs_z)] = v

        shape = (matrix_chunk_size.x * level.chunk_width,
                 level.chunk_voxel_height,
                 matrix_chunk_size.z * level.chunk_depth)

        return SparseLevel(level, voxels, shape, Vec2(min_chunk_x,
                                                      min_chunk_z),
                           Vec2(max_chunk_x, max_chunk_z))
Пример #8
0
from collections import OrderedDict

import numpy as np
import randomcolor
from simple_3dviz import Mesh
from simple_3dviz.window import show

from api.models.database import SessionLocal
from api.models.level import Level
from layering.helper import is_accessible, is_inside
from shared.vector import Vec2, Vec3
from layering.sparselevel import SparseLevel

LEVEL_ID = 2
CENTER_CHUNK = Vec2(-50, 44)
SEED_POSITION = Vec3(-344.275391, 69.154152, 309.750977)
CHUNK_DISTANCE = 10

PLAYER_RADIUS = 0.35
PLAYER_HEIGHT = 1.8

session = SessionLocal()

level = session.query(Level).get(LEVEL_ID)

if not Level:
    print('A level with the given id does not exist, exiting...')
    sys.exit(-1)

voxel_unit_size = Vec3(level.chunk_unit_width / level.chunk_width,
                       level.chunk_unit_height / level.chunk_voxel_height,