コード例 #1
0
    private World()
    {
        // Get the extents of the world.
        var  blocks = Component.FindObjectsOfType <Block>();
        Int3 minPos = new Int3(500, 500, 500);
        Int3 maxPos = new Int3(-500, -500, -500);

        foreach (Block block in blocks)
        {
            Debug.Log("Block at: " + block.transform.position);
            minPos = Int3.Min(minPos, new Int3(block.transform.position));
            maxPos = Int3.Max(maxPos, new Int3(block.transform.position));
        }

        Origin = minPos;
        Size   = maxPos - minPos + Int3.One;
        Debug.Log("Origin: " + minPos);
        Debug.Log("Size: " + Size);
        Debug.Log("Max Pos: " + maxPos);
        // Fill the array.
        Blocks = new Block[Size.x, Size.y, Size.z];
        //Debug.Log("Blocks dimension: " + Blocks.GetLength(0) + ", ")
        foreach (Block block in blocks)
        {
            Int3 pos = new Int3(block.transform.position) - Origin;
            Blocks[pos.x, pos.y, pos.z] = block;
        }
    }
コード例 #2
0
        public void Include(BitCube other, Int3 offset)
        {
            if ((offset >= size).Any)
            {
                return;
            }

            Int3 sourceStart = Int3.Max(-offset, 0);
            Int3 sourceCount = other.Size - sourceStart;

            Int3 destStart = Int3.Max(offset, 0);

            sourceCount = Int3.Min(destStart + sourceCount, size) - destStart;

            Int3 at = new Int3();

            for (at.X = 0; at.X < sourceCount.X; at.X++)
            {
                for (at.Y = 0; at.Y < sourceCount.Y; at.Y++)
                {
                    for (at.Z = 0; at.Z < sourceCount.Z; at.Z++)
                    {
                        this[at + destStart] |= other[at + sourceStart];
                    }
                }
            }
        }
コード例 #3
0
    public IEnumerable <Util.Tuple <Cell, CellValue> > GetFilledCells(Cell cellA, Cell cellB)
    {
        Int3 cellIntA = cellA.ToInt3();
        Int3 cellIntB = cellB.ToInt3();

        Int3 min = Int3.Min(cellIntA, cellIntB);
        Int3 max = Int3.Max(cellIntA, cellIntB) + Int3.one();

        // Int3 mins3 = mins.ToInt3();
        // Int3 maxs = inclusiveMaxs.ToInt3() + Int3.one();
        foreach (Int3 u in Int3.Enumerate(min, max))
        {
            CellValue val = GetCellValue(new Cell(u));
            if (val.blockType != BlockShape.Empty)
            {
                yield return(new Util.Tuple <Cell, CellValue>(new Cell(u), val));
            }
        }
    }
コード例 #4
0
        public void SetOne(Int3 offset, Int3 count)
        {
            Int3 sourceStart = Int3.Max(-offset, 0);
            Int3 sourceCount = count - sourceStart;
            Int3 destStart   = Int3.Max(offset, 0);

            sourceCount = Int3.Min(destStart + sourceCount, size) - destStart;

            Int3 at = new Int3();

            for (at.X = 0; at.X < sourceCount.X; at.X++)
            {
                for (at.Y = 0; at.Y < sourceCount.Y; at.Y++)
                {
                    for (at.Z = 0; at.Z < sourceCount.Z; at.Z++)
                    {
                        this[at + destStart] = true;
                    }
                }
            }
        }
コード例 #5
0
        public int OneCountIn(Int3 offset, Int3 count)
        {
            Int3 count3 = count - Int3.Max(-offset, 0);

            offset = Int3.Max(offset, 0);
            count3 = Int3.Min(offset + count3, size) - offset;

            Int3 at = new Int3();
            int  rs = 0;

            for (at.X = 0; at.X < count3.X; at.X++)
            {
                for (at.Y = 0; at.Y < count3.Y; at.Y++)
                {
                    for (at.Z = 0; at.Z < count3.Z; at.Z++)
                    {
                        rs += this[at + offset] ? 1 : 0;
                    }
                }
            }
            return(rs);
        }
コード例 #6
0
        public void PostProcess(RenderDrawContext drawContext, ShaderSource[] mipmapShaders)
        {
            if (mipmapShaders.Length != LayoutSize)
            {
                return;
            }

            if (VoxelMipmapSimple == null)
            {
                VoxelMipmapSimple = new Stride.Rendering.ComputeEffect.ComputeEffectShader(drawContext.RenderContext)
                {
                    ShaderSourceName = "Voxel2x2x2MipmapEffect"
                };
            }

            if (VoxelMipmapSimpleGroups == null || VoxelMipmapSimpleGroups.Length != LayoutSize || VoxelMipmapSimpleGroups[0].Length != TempMipMaps.Length)
            {
                if (VoxelMipmapSimpleGroups != null)
                {
                    for (int axis = 0; axis < LayoutSize; axis++)
                    {
                        if (VoxelMipmapSimpleGroups[axis] != null)
                        {
                            foreach (var shader in VoxelMipmapSimpleGroups[axis])
                            {
                                shader.Dispose();
                            }
                        }
                    }
                }
                VoxelMipmapSimpleGroups = new Stride.Rendering.ComputeEffect.ComputeEffectShader[LayoutSize][];
                for (int axis = 0; axis < LayoutSize; axis++)
                {
                    VoxelMipmapSimpleGroups[axis] = new Stride.Rendering.ComputeEffect.ComputeEffectShader[TempMipMaps.Length];
                    for (int i = 0; i < VoxelMipmapSimpleGroups[axis].Length; i++)
                    {
                        VoxelMipmapSimpleGroups[axis][i] = new Stride.Rendering.ComputeEffect.ComputeEffectShader(drawContext.RenderContext)
                        {
                            ShaderSourceName = "Voxel2x2x2MipmapEffect"
                        };
                    }
                }
            }

            int offsetIndex = 0;
            //Mipmap detailed clipmaps into less detailed ones
            Vector3 totalResolution   = ClipMapResolution * new Vector3(1, LayoutSize, 1);
            Int3    threadGroupCounts = new Int3(32, 32, 32);

            if (DownsampleFinerClipMaps)
            {
                for (int i = 0; i < ClipMapCount - 1; i++)
                {
                    Vector3 Offset = MippingOffset[offsetIndex];

                    VoxelMipmapSimple.ThreadNumbers     = new Int3(8);
                    VoxelMipmapSimple.ThreadGroupCounts = (Int3)((ClipMapResolution / 2f) / (Vector3)VoxelMipmapSimple.ThreadNumbers);

                    for (int axis = 0; axis < LayoutSize; axis++)
                    {
                        VoxelMipmapSimple.Parameters.Set(Voxel2x2x2MipmapKeys.ReadTex, ClipMaps);
                        VoxelMipmapSimple.Parameters.Set(Voxel2x2x2MipmapKeys.WriteTex, TempMipMaps[0]);
                        VoxelMipmapSimple.Parameters.Set(Voxel2x2x2MipmapKeys.ReadOffset, -(Vector3.Mod(Offset, new Vector3(2))) + new Vector3(0, (int)totalResolution.Y * i + (int)ClipMapResolution.Y * axis, 0));
                        VoxelMipmapSimple.Parameters.Set(Voxel2x2x2MipmapKeys.WriteOffset, new Vector3(0, ClipMapResolution.Y / 2 * axis, 0));
                        VoxelMipmapSimple.Parameters.Set(Voxel2x2x2MipmapKeys.mipmapper, mipmapShaders[axis]);
                        ((RendererBase)VoxelMipmapSimple).Draw(drawContext);
                    }

                    Offset -= Vector3.Mod(Offset, new Vector3(2));
                    //Copy each axis, ignoring the top and bottom plane
                    for (int axis = 0; axis < LayoutSize; axis++)
                    {
                        int axisOffset = axis * (int)ClipMapResolution.Y;

                        Int3 CopySize = new Int3((int)ClipMapResolution.X / 2 - 2, (int)ClipMapResolution.Y / 2 - 2, (int)ClipMapResolution.Z / 2 - 2);


                        Int3 DstMinBound = new Int3((int)ClipMapResolution.X / 4 + (int)Offset.X / 2 + 1, (int)totalResolution.Y * (i + 1) + axisOffset + (int)ClipMapResolution.Y / 4 + 1 + (int)Offset.Y / 2, (int)ClipMapResolution.Z / 4 + (int)Offset.Z / 2 + 1);
                        Int3 DstMaxBound = DstMinBound + CopySize;

                        DstMaxBound = Int3.Min(DstMaxBound, new Int3((int)totalResolution.X, (int)totalResolution.Y * (i + 2), (int)totalResolution.Z));
                        DstMinBound = Int3.Min(DstMinBound, new Int3((int)totalResolution.X, (int)totalResolution.Y * (i + 2), (int)totalResolution.Z));
                        DstMaxBound = Int3.Max(DstMaxBound, new Int3(0, (int)totalResolution.Y * (i + 1), 0));
                        DstMinBound = Int3.Max(DstMinBound, new Int3(0, (int)totalResolution.Y * (i + 1), 0));

                        Int3 SizeBound = DstMaxBound - DstMinBound;

                        Int3 SrcMinBound = new Int3(1, axisOffset / 2 + 1, 1);
                        Int3 SrcMaxBound = SrcMinBound + SizeBound;

                        if (SizeBound.X > 0 && SizeBound.Y > 0 && SizeBound.Z > 0)
                        {
                            drawContext.CommandList.CopyRegion(TempMipMaps[0], 0,
                                                               new ResourceRegion(
                                                                   SrcMinBound.X, SrcMinBound.Y, SrcMinBound.Z,
                                                                   SrcMaxBound.X, SrcMaxBound.Y, SrcMaxBound.Z
                                                                   ),
                                                               ClipMaps, 0,
                                                               DstMinBound.X, DstMinBound.Y, DstMinBound.Z);
                        }
                    }
                    offsetIndex++;
                }
            }
            Vector3 resolution = ClipMapResolution;

            offsetIndex = ClipMapCount - 1;
            //Mipmaps for the largest clipmap
            for (int i = 0; i < TempMipMaps.Length - 1; i++)
            {
                Vector3 Offset = MippingOffset[offsetIndex];
                resolution /= 2;

                Vector3 threadNums = Vector3.Min(resolution, new Vector3(8));

                for (int axis = 0; axis < LayoutSize; axis++)
                {
                    var mipmapShader = VoxelMipmapSimpleGroups[axis][i];
                    mipmapShader.ThreadNumbers     = (Int3)(threadNums);
                    mipmapShader.ThreadGroupCounts = (Int3)(resolution / threadNums);
                    if (i == 0)
                    {
                        mipmapShader.Parameters.Set(Voxel2x2x2MipmapKeys.ReadTex, ClipMaps);
                        mipmapShader.Parameters.Set(Voxel2x2x2MipmapKeys.ReadOffset, -Offset + new Vector3(0, (int)ClipMapResolution.Y * LayoutSize * (ClipMapCount - 1) + (int)ClipMapResolution.Y * axis, 0));
                        mipmapShader.Parameters.Set(Voxel2x2x2MipmapKeys.WriteOffset, new Vector3(0, resolution.Y * axis, 0));
                    }
                    else
                    {
                        mipmapShader.Parameters.Set(Voxel2x2x2MipmapKeys.ReadTex, TempMipMaps[i - 1]);
                        mipmapShader.Parameters.Set(Voxel2x2x2MipmapKeys.ReadOffset, -Offset + new Vector3(0, resolution.Y * axis * 2, 0));
                        mipmapShader.Parameters.Set(Voxel2x2x2MipmapKeys.WriteOffset, new Vector3(0, resolution.Y * axis, 0));
                    }
                    mipmapShader.Parameters.Set(Voxel2x2x2MipmapKeys.WriteTex, TempMipMaps[i]);
                    mipmapShader.Parameters.Set(Voxel2x2x2MipmapKeys.mipmapper, mipmapShaders[axis]);
                    ((RendererBase)mipmapShader).Draw(drawContext);
                }
                //Don't seem to be able to read and write to the same texture, even if the views
                //point to different mipmaps.
                drawContext.CommandList.CopyRegion(TempMipMaps[i], 0, null, MipMaps, i);
                offsetIndex++;
            }
            Array.Copy(PerMapOffsetScale, PerMapOffsetScaleCurrent, PerMapOffsetScale.Length);
        }