示例#1
0
    public void Init(
        string name,
        float[][] density_grids,
        int grid_width_in_voxels,
        int grid_height_in_voxels,
        int grid_depth_in_voxels,
        Vector3 voxel_size_in_meters,
        int voxel_chunk_dimesnions,
        bool generate_collision,
        float iso_level,
        float density_height_weight,
        LayeredBrush brush,
        bool cast_shadows,
        bool is_liquid,
        Material prepass_material,
        BevelTuning bevel_tuning
        )
    {
        m_voxel_chunk_dimensions = voxel_chunk_dimesnions;
        m_voxel_size_in_meters   = voxel_size_in_meters;
        m_grid_width_in_voxels   = grid_width_in_voxels;
        m_grid_height_in_voxels  = grid_height_in_voxels;
        m_grid_depth_in_voxels   = grid_depth_in_voxels;

        m_empty_sample_grid          = new byte[m_grid_width_in_voxels * m_grid_depth_in_voxels];
        m_voxel_chunk_scratch_buffer = VoxelChunk.ScratchBuffer.CreateScratchBuffer();
        m_layers           = new VoxelLayer[m_grid_height_in_voxels];
        m_brush            = brush;
        m_cast_shadows     = cast_shadows;
        m_prepass_material = prepass_material;


        for (int y = 0; y < m_grid_height_in_voxels; ++y)
        {
            float bot_y = (float)(y - 1) * m_voxel_size_in_meters.y;
            float top_y = (float)y * m_voxel_size_in_meters.y;

            var layer = new VoxelLayer(name, density_grids[y], y, m_grid_width_in_voxels, m_grid_depth_in_voxels, m_voxel_chunk_dimensions, m_voxel_size_in_meters, iso_level, bot_y, top_y, generate_collision, density_height_weight, m_vertex_attribute_descriptors, is_liquid, bevel_tuning);
            m_layers[y] = layer;
        }

        for (int y = 0; y < m_grid_height_in_voxels; ++y)
        {
            var layer_above_occlusion_grid = m_empty_sample_grid;
            var layer_below_occlusion_grid = m_empty_sample_grid;

            if (y > 0)
            {
                layer_below_occlusion_grid = m_layers[y - 1].GetSampleGrid();
            }

            if (y < m_grid_height_in_voxels - 2)
            {
                layer_above_occlusion_grid = m_layers[y + 1].GetSampleGrid();
            }

            m_layers[y].SetAboveAndBelowSampleGrids(layer_above_occlusion_grid, layer_below_occlusion_grid);
        }
    }
示例#2
0
    public VoxelLayer(
        string name,
        float[] density_grid,
        int layer_idx,
        int width_in_voxels,
        int height_in_voxels,
        int voxel_chunk_dimensions,
        Vector3 voxel_size_in_meters,
        float iso_level,
        float bot_y,
        float top_y,
        bool generate_collision,
        float density_height_weight,
        VertexAttributeDescriptor[] vertex_attribute_descriptors,
        bool is_liquid,
        BevelTuning bevel_tuning
        )
    {
        if (width_in_voxels % voxel_chunk_dimensions != 0)
        {
            throw new System.Exception($"width_in_voxels={width_in_voxels} is not a multiple of voxel_chunk_dimensions={voxel_chunk_dimensions}");
        }
        if (height_in_voxels % voxel_chunk_dimensions != 0)
        {
            throw new System.Exception($"width_in_voxels={height_in_voxels} is not a multiple of voxel_chunk_dimensions={voxel_chunk_dimensions}");
        }

        m_density_grid                    = density_grid;
        m_sample_grid                     = new byte[width_in_voxels * height_in_voxels];
        m_width_in_voxels                 = width_in_voxels;
        m_height_in_voxels                = height_in_voxels;
        m_voxel_size_in_meters            = voxel_size_in_meters;
        m_voxel_chunk_dimensions          = voxel_chunk_dimensions;
        m_one_over_voxel_chunk_dimensions = 1f / (float)voxel_chunk_dimensions;
        m_bot_y     = bot_y;
        m_top_y     = top_y;
        m_layer_idx = layer_idx;
        m_vertex_attribute_descriptors = vertex_attribute_descriptors;
        m_bevel_tuning = bevel_tuning;

        m_width_in_chunks  = width_in_voxels / voxel_chunk_dimensions;
        m_height_in_chunks = height_in_voxels / voxel_chunk_dimensions;
        m_voxel_chunks     = new VoxelChunk[m_width_in_chunks * m_height_in_chunks];
        m_bounds_grid      = new BoundsEntry[m_width_in_chunks * m_height_in_chunks];

        for (int y = 0, chunk_idx = 0; y < m_height_in_chunks; ++y)
        {
            for (int x = 0; x < m_width_in_chunks; ++x, ++chunk_idx)
            {
                var bounds = GetChunkBounds(x, y);
                m_voxel_chunks[chunk_idx] = new VoxelChunk(name, x * voxel_chunk_dimensions, y * voxel_chunk_dimensions, voxel_chunk_dimensions, width_in_voxels, height_in_voxels, m_density_grid, m_sample_grid, voxel_size_in_meters, iso_level, bot_y, top_y, generate_collision, density_height_weight, bounds, is_liquid, bevel_tuning);
                m_bounds_grid[chunk_idx]  = new BoundsEntry
                {
                    m_bounds     = bounds,
                    m_is_visible = false
                };
            }
        }
    }