Пример #1
0
    public void Initialize(int resolution, float size)
    {
        this.m_resolution = resolution;
        m_gridSize = size;
        m_voxelSize = size / resolution;
        m_voxels = new Voxel[resolution * resolution];
        m_voxelMaterials = new Material[m_voxels.Length];

        for (int i = 0, y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++, i++)
            {
                CreateVoxel(i, x, y);
            }
        }

        m_dummyX = new Voxel();
        m_dummyY = new Voxel();
        m_dummyT = new Voxel();

        //offset the grid to center it inside the screen
        Vector3 gridPosition = new Vector3(-0.5f * size, -0.5f * size, 0);
        this.transform.localPosition = gridPosition;

        GetComponent<MeshFilter>().mesh = m_mesh = new Mesh();
        m_mesh.name = "VoxelGrid Mesh";
        m_vertices = new List<Vector3>();
        m_triangles = new List<int>();
        Refresh();
    }
Пример #2
0
    // Given new chunk of voxels, reset and rebuild mesh
    public void UseVoxels(Voxel[] src_voxels, int chunkx, int chunky)
    {
        // If this chunk already displays given voxels, do nothing
        if (this.voxels == src_voxels) {
            return;
        }

        this.chunkX = chunkx;
        this.chunkY = chunky;
        this.voxels = src_voxels;
        RebuildMesh();
    }
Пример #3
0
 // Use this for initialization
 private void Start()
 {
     if (PicaVoxelVolume == null) return;
     originalVoxels = new Voxel[PicaVoxelVolume.XSize*PicaVoxelVolume.YSize*PicaVoxelVolume.ZSize];
     Helper.CopyVoxelsInBox(ref PicaVoxelVolume.GetCurrentFrame().Voxels, ref originalVoxels,
         new PicaVoxelPoint(PicaVoxelVolume.XSize, PicaVoxelVolume.YSize, PicaVoxelVolume.ZSize),
         new PicaVoxelPoint(PicaVoxelVolume.XSize, PicaVoxelVolume.YSize, PicaVoxelVolume.ZSize), true);
     PicaVoxelVolume.GetCurrentFrame().Voxels =
         new Voxel[PicaVoxelVolume.XSize*PicaVoxelVolume.YSize*PicaVoxelVolume.ZSize];
     PicaVoxelVolume.UpdateAllChunks();
     radius = StartRadius;
     gameObject.SetActive(false);
 }
Пример #4
0
 public Vector3 GetSpawnPoint()
 {
     int i;
     Voxel voxel = null;
     while(voxel == null && voxels.Length > 0) {
         i = Random.Range(0,voxels.Length);
         voxel = voxels[i];
         if(voxel == null) {
             voxels = GetComponentsInChildren<Voxel>();
         }
     }
     Vector3 v = Vector3.zero;
     if(voxel != null) {
         v = voxel.transform.position;
         v.y = 3;
     }
     return v;
 }
Пример #5
0
        public Region(VoxWorld w)
        {
            World = w;
            loc = Point.Zero;

            // Create Empty Region
            voxels = new Voxel[VOXEL_COUNT];
            for(int i = 0; i < VOXEL_COUNT; i++)
                voxels[i] = Voxel.Empty;

            // Non-calculated Boundaries
            rBS = new RegionBoundaryState[4];
            for(int i = 0; i < 4; i++)
                rBS[i] = new RegionBoundaryState();

            // No Neighbors
            rNX = rPX = rNZ = rPZ = null;
        }
Пример #6
0
    public void ResetBoard()
    {
        foreach(Transform t in transform) {
            Destroy(t.gameObject);
        }

        for(int w = -width; w < width; w++) {
            for(int h = -height; h < height; h++) {
                GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                go.AddComponent<Voxel>();
                go.transform.parent = transform;
                go.transform.localPosition = new Vector3(w,0,h);
            }
        }

        voxels = GetComponentsInChildren<Voxel>();

        delta = 0;
        deltaMax = Random.Range(0.5f,1.5f);
    }
Пример #7
0
        public VoxelGrid(ComputeCommandQueue commandQueue, float gridWidth, int gridResolution)
        {
            _commandQueue = commandQueue;

            _gridWidth = gridWidth;
            GridResolution = gridResolution;
            CellSize = gridWidth / gridResolution;

            Vector3 halfGridWidth = new Vector3(gridWidth/2.0f, gridWidth/2.0f, gridWidth/2.0f);
            _gridOrigin = -halfGridWidth;

            // Create voxel grid. gridResolution^3 cells
            int cellCount = gridResolution * gridResolution * gridResolution;
            _voxelArray = new Voxel[cellCount];

            unsafe
            {
                fixed (Voxel* gridData = _voxelArray)
                {
                    _grid = new ComputeImage3D(_commandQueue.Context,
                        ComputeMemoryFlags.CopyHostPointer | ComputeMemoryFlags.ReadOnly,
                        _imageFormat,
                        GridResolution, GridResolution, GridResolution,
                        0, 0,
                        (IntPtr)gridData);
                }
            }

            // Create array to hold primitives.
            VectorsPerVoxel = 16;	// Low value for testing;
            _geometryArray = new Triangle[cellCount * VectorsPerVoxel];
            Geometry = new ComputeBuffer<Triangle>(_commandQueue.Context, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.UseHostPointer, _geometryArray);

            // Create array for lights
            _pointLightArray = new SimplePointLight[InitialPointLightArraySize];
            PointLightCount = 0;
            _pointLightBuffer = new ComputeBuffer<SimplePointLight>(_commandQueue.Context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.UseHostPointer, _pointLightArray);

            syncBuffers();
        }
Пример #8
0
    public void ResetBoard()
    {
        foreach(Transform t in transform) {
            Destroy(t.gameObject);
        }

        for(int w = -radius; w < radius; w++) {
            for(int h = -radius; h < radius; h++) {
                if(Mathf.Sqrt(w*w+h*h) < radius && (w != 0 || h != 0)) {
                    GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                    go.AddComponent<Voxel>();
                    go.transform.parent = transform;
                    go.transform.localPosition = new Vector3(w,0,h);
                }
            }
        }

        voxels = GetComponentsInChildren<Voxel>();

        delta = 0;
        deltaMax = Random.Range(0.5f,1.5f);
    }
Пример #9
0
    public void Initialize(int resolution, float size)
    {
        this.resolution = resolution;
        gridSize = size;
        voxelSize = size / resolution;
        voxels = new Voxel[resolution * resolution];
        voxelMaterials = new Material[voxels.Length];

        dummyX = new Voxel();
        dummyY = new Voxel();
        dummyT = new Voxel();

        for (int i = 0, y = 0; y < resolution; y++) {
            for (int x = 0; x < resolution; x++, i++) {
                CreateVoxel(i, x, y);
            }
        }

        GetComponent<MeshFilter>().mesh = mesh = new Mesh();
        mesh.name = "VoxelGrid Mesh";
        vertices = new List<Vector3>();
        triangles = new List<int>();
        Refresh();
    }
Пример #10
0
 // Update is called once per frame
 void Update()
 {
     delta += Time.deltaTime;
     if(delta > deltaMax) {
         delta = 0;
         deltaMax = Random.Range(0.5f,1.5f);
         int i = Random.Range(0,voxels.Length);
         if(voxels.Length > 0 && voxels[i] != null) {
             voxels[i].Drop();
         }
         else {
             voxels = GetComponentsInChildren<Voxel>();
         }
     }
 }
Пример #11
0
        public Grid(List<IPrimitive> primitives, bool refineImmediately)
        {
            if (refineImmediately)
            {
                foreach (IPrimitive primitive in primitives)
                {
                    primitive.FullyRefine (ref Primitives);
                }
            }
            else
                Primitives = primitives;

            foreach (IPrimitive primitive in Primitives)
                Bounds = BoundingBox.Union (Bounds, primitive.WorldBound);

            Vector delta = Bounds.pMax - Bounds.pMin;

            int maxAxis = Bounds.MaximumExtent;
            double invMaxWidth = 1.0 / delta[maxAxis];
            double cubeRoot = 3.0 * Math.Pow (Primitives.Count, 1.0 / 3.0);
            double voxelsPerUnitDistance = cubeRoot * invMaxWidth;

            for (int axis = 0; axis < 3; ++axis)
            {
                nVoxels[axis] = Util.RoundToInt (delta[axis] * voxelsPerUnitDistance);
                nVoxels[axis] = Util.Clamp (nVoxels[axis], 1, 64);
            }

            for (int axis = 0; axis < 3; ++axis)
            {
                Width[axis] = delta[axis] / nVoxels[axis];
                InvWidth[axis] = (Width[axis] == 0.0) ? 0.0 : 1.0 / Width[axis];
            }
            int nv = nVoxels[0] * nVoxels[1] * nVoxels[2];
            Voxels = new Voxel[nv];

            foreach (IPrimitive primitive in Primitives)
            {
                BoundingBox pb = primitive.WorldBound;
                int[] vmin = new int[3];
                int[] vmax = new int[3];

                for (int axis = 0; axis < 3; ++axis)
                {
                    vmin[axis] = PositionToVoxel (pb.pMin, axis);
                    vmax[axis] = PositionToVoxel (pb.pMax, axis);
                }

                for (int z = vmin[2]; z <= vmax[2]; ++z)
                {
                    for (int y = vmin[1]; y <= vmax[1]; ++y)
                    {
                        for (int x = vmin[0]; x <= vmax[0]; ++x)
                        {
                            int o = Offset (x, y, z);
                            if (Voxels[o] == null)
                            {
                                Voxels[o] = new Voxel (primitive);
                            }
                            else
                            {
                                Voxels[o].AddPrimitive (primitive);
                            }
                        }
                    }
                }
            }
        }
Пример #12
0
 /// <summary>
 /// Initialise this frame with an array of empty voxels
 /// </summary>
 public void GenerateNewFrame()
 {
     ParentVolume = transform.parent.GetComponent<Volume>();
     Voxels = new Voxel[XSize * YSize * ZSize];
     SaveForSerialize();
 }
Пример #13
0
 /// <summary>
 /// Initialise this frame and copy the voxels from a source frame
 /// </summary>
 /// <param name="sourceFrame"></param>
 public void GenerateNewFrame(Frame sourceFrame)
 {
     ParentVolume = transform.parent.GetComponent<Volume>();
     Voxels = new Voxel[XSize * YSize * ZSize];
     Helper.CopyVoxelsInBox(ref sourceFrame.Voxels, ref Voxels, new PicaVoxelPoint(XSize, YSize, ZSize), new PicaVoxelPoint(XSize, YSize, ZSize), false);
     CreateChunks();
     SaveForSerialize();
 }
Пример #14
0
 /// <summary>
 /// Initialises a new instance of the ChunkVoxels class.
 /// </summary>
 public ChunkVoxels()
 {
     this.voxels = new Voxel[Chunk.Width * Chunk.Height];
 }
Пример #15
0
        /// <summary>
        /// Generates a 32x32x32 frame, filled with white voxels of value 128
        /// </summary>
        public void GenerateBasic(FillMode fillMode)
        {
            ParentVolume = ParentVolume.GetComponent<Volume>();
            Voxels = new Voxel[XSize * YSize * ZSize];
            for (int x = 0; x < XSize; x++)
                for (int y = 0; y < YSize; y++)
                    for (int z = 0; z < ZSize; z++)
                    {
                        int index = x + XSize * (y + YSize * z);
                        if (fillMode == FillMode.AllVoxels || (fillMode == FillMode.BaseOnly && y == 0))
                            Voxels[index].State = VoxelState.Active;
                        Voxels[index].Value = 128;
                        Voxels[index].Color = ParentVolume.PaletteColors[0];
                    }

            SaveForSerialize();
        }
Пример #16
0
 /// <summary>
 /// Initialise this frame with an array of empty voxels
 /// </summary>
 public void GenerateNewFrame()
 {
     ParentVolume = transform.parent.GetComponent<Volume>();
     Voxels = new Voxel[XSize * YSize * ZSize];
     for (int i = 0; i < Voxels.Length; i++) Voxels[i].Value = 128;
     SaveForSerialize();
 }
Пример #17
0
        public void FromCompressedByteArray(byte[] compressed)
        {
            byte[] streambuff = new byte[XSize * YSize * ZSize * Voxel.BYTE_SIZE];

            using (var ms = new MemoryStream(compressed))
            {

                using (var gzs = new GZipInputStream(ms))
                {
                    gzs.Read(streambuff, 0, streambuff.Length);
                }
            }

            Voxels = new Voxel[XSize * YSize * ZSize];

            int o = 0;
            byte[] buffer = new byte[Voxel.BYTE_SIZE];
            for (int x = 0; x < XSize; x++)
                for (int y = 0; y < YSize; y++)
                    for (int z = 0; z < ZSize; z++)
                    {
                        for (int i = 0; i < Voxel.BYTE_SIZE; i++) buffer[i] = streambuff[o + i];
                            Voxels[x + XSize * (y + YSize * z)] = new Voxel(buffer);
                            o += Voxel.BYTE_SIZE;

                    }
        }
Пример #18
0
        public void FromCompressedByteArray(byte[] compressed)
        {
            byte[] streambuff = new byte[XSize * YSize * ZSize * Voxel.BYTE_SIZE];

            using (var ms = new MemoryStream(compressed))
            {

                using (var gzs = new GZipInputStream(ms))
                {
                    gzs.Read(streambuff, 0, streambuff.Length);
                }
            }

            bool convertAlpha = true;
            for (int i = 0; i < (streambuff.Length<50?streambuff.Length:50); i++)
            {
                if (streambuff[(streambuff.Length - 1) - i] != 0) convertAlpha = false;
            }

            Voxels = new Voxel[XSize * YSize * ZSize];

            int o = 0;
            byte[] buffer = new byte[Voxel.BYTE_SIZE];
            for (int x = 0; x < XSize; x++)
                for (int y = 0; y < YSize; y++)
                    for (int z = 0; z < ZSize; z++)
                    {
                        if (!convertAlpha)
                        {
                            for (int i = 0; i < Voxel.BYTE_SIZE; i++) buffer[i] = streambuff[o + i];
                            Voxels[x + XSize * (y + YSize * z)] = new Voxel(buffer);
                            o += Voxel.BYTE_SIZE;
                        }
                        else
                        {
                            for (int i = 0; i < Voxel.BYTE_SIZE - 1; i++) buffer[i] = streambuff[o + i];
                            buffer[5] = 255;
                            Voxels[x + XSize * (y + YSize * z)] = new Voxel(buffer);
                            o += Voxel.BYTE_SIZE - 1;
                        }
                    }
        }
Пример #19
0
 IEnumerator Start()
 {
     randSeed = Random.Range(-1000.0f, 1000.0f);
     tempVoxels = new Voxel[chunkSizeA2 * chunkSizeA2P2];
     chunkParent = new GameObject("_CHUNKS").transform;
     //
     for (int x = 0; x < 5; ++x) {
         for (int y = 0; y < 5; ++y) {
             for (int z = 0; z < 5; ++z) {
                 CreateChunk(x, y, z);
                 yield return null;
             }
         }
     }
 }