Exemple #1
0
    public void overlayCurrentChunk()
    {
        Vector3 pt          = camera.transform.position;
        Vec3Int chunkCoords = vxe.getChunkCoords(pt);


        for (int x = -1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                for (int z = -1; z <= 1; z++)
                {
                    Vec3Int camchunk = chunkCoords + new Vec3Int(x, y, z);
                    Chunks  chunk    = vxe.grid.voxelGrid[camchunk.x, camchunk.y, camchunk.z];

                    GameObject refChunk        = vxe.chunkGameObjects [camchunk.x, camchunk.y, camchunk.z];
                    GameObject overlayInstance = overlayInstances[x + 1, y + 1, z + 1];
                    overlayInstance.transform.position = refChunk.transform.position - camera.transform.forward * 0.01f;
                    overlayInstance.GetComponent <MeshRenderer> ().material = material;

                    if (chunk == null)
                    {
                        continue;
                    }
                    buildChunkMesh(chunk, camchunk, overlayInstance.GetComponent <MeshFilter>().mesh);
                }
            }
        }
    }
        public void GenerateMesh(Mesh mesh, Func <Vector3, float> getSDF, float step, Bounds bounds, Vector3 finalOffset = default)
        {
            if (isBusy)
            {
                throw  new Exception("Generation is busy. Errors might occure");
            }

            this.mesh        = mesh;
            this.bounds      = bounds;
            this.finalOffset = finalOffset;
            getSdfFunc       = getSDF;

            this.resolution = new Vec3Int(
                Mathf.CeilToInt(bounds.size.x / step),
                Mathf.CeilToInt(bounds.size.y / step),
                Mathf.CeilToInt(bounds.size.z / step));

            var scale = new Vector3(
                bounds.size.x / resolution.x,
                bounds.size.y / resolution.y,
                bounds.size.z / resolution.z);

            gridToWorldMatrix = Matrix4x4.TRS(bounds.min, Quaternion.identity, scale);

            isBusy = true;
            if (Application.isPlaying)
            {
                GenerateMeshAsync();
            }
            else
            {
                GenerateMeshSync();
            }
        }
Exemple #3
0
    /// <summary>
    /// Changes the biome.
    /// </summary>
    /// <returns><c>true</c>, if biome was changed, <c>false</c> otherwise.</returns>
    bool checkChangeBiome()
    {
        playerCC = vxe.getChunkCoords (playerTrans.position);
        mybiome = biome.biomeMap [playerCC.x, playerCC.z];

        return (mybiome != prevBiome);
    }
Exemple #4
0
    // Update is called once per frame
    void Update()
    {
        framecount++;
        if (framecount % spawnInterval != 0 || count >= spawnLimit)
        {
            return;
        }

        Random.seed = (int)(Time.deltaTime * 1000);

        Vector3 pos = new Vector3(), normal = new Vector3();

        if (vxe.RayCast(camera.transform.position, camera.transform.forward, 64, ref pos, ref normal))
        {
            Vec3Int chunkcoord = vxe.ToGrid(pos) / vxe.chunk_size;
            BIOMES  mybiome    = biome.biomeMap[chunkcoord.x, chunkcoord.z];

            int        animal      = Random.Range(0, spawns[(int)mybiome].obj.Length);
            GameObject spawnObject = spawns[(int)mybiome].obj[animal];

            if (spawns[(int)mybiome].sticksToWalls[animal] || Vector3.Dot(normal, Vector3.up) > 0.999f)
            {
                GameObject newsphere = (GameObject)Instantiate(spawnObject, pos + normal * VoxelExtractionPointCloud.Instance.voxel_size * 0.5f, Quaternion.identity);
                newsphere.SetActive(true);
                //newsphere.GetComponent<GrowScript>().init(pos, normal, (Vector3.Dot (normal,Vector3.up) > 0.999f) );
                count++;
            }
        }
    }
Exemple #5
0
    /// <summary>
    /// Changes the biome.
    /// </summary>
    /// <returns><c>true</c>, if biome was changed, <c>false</c> otherwise.</returns>
    bool checkChangeBiome()
    {
        playerCC = vxe.getChunkCoords(playerTrans.position);
        mybiome  = biome.biomeMap [playerCC.x, playerCC.z];

        return(mybiome != prevBiome);
    }
        private int getIndexMask(Vec3Int ijk)
        {
            var result = 0;

            //Parallel.For(0, 8, Iterate);
            for (int i = 0; i < 8; i++)
            {
                Iterate(i);
            }

            return(result);

            void Iterate(int i)
            {
                var x = (i & 1) == 1 ? 1 : 0;
                var y = (i & 2) == 2 ? 1 : 0;
                var z = (i & 4) == 4 ? 1 : 0;

                var sdf = getCachedSDFInCell(ijk + new Vec3Int(x, y, z));

                if (sdf < 0)
                {
                    result |= 1 << i;
                }
            }
        }
Exemple #7
0
    /// <summary>
    /// Spawns a GameObject on many voxels in a chunk.
    /// </summary>
    /// <param name="chunk">Chunk.</param>
    /// <param name="chunkGridCoord">Chunk grid coordinate.</param>
    /// <param name="obj">Object.</param>
    void onManyVoxelPosition(Chunks chunk, Vec3Int chunkGridCoord, GameObject obj)
    {
        Vector3 chunkBCoords = new Vector3(chunkGridCoord.x, chunkGridCoord.y, chunkGridCoord.z) * vxe.chunk_size;

        Random.seed = (int)Time.frameCount;

        for (int x = 0; x < vxe.chunk_size; x++)
        {
            for (int z = 0; z < vxe.chunk_size; z++)
            {
                for (int y = vxe.chunk_size - 1; y >= 0; y--)
                {
                    Voxel vx = chunk.getVoxel(new Vec3Int(x, y, z));

                    if (vx.isOccupied() && vxe.voxelHasSurface(vx, VF.VX_TOP_SHOWN))
                    {
                        Vector3 voxelCoords = vxe.FromGridUnTrunc(chunkBCoords + new Vector3(x, y, z));

                        if (Random.Range(0, 20) == 0)
                        {
                            GameObject newObj = Instantiate(obj, voxelCoords + Vector3.up * vxe.voxel_size * 1.0f, Quaternion.AngleAxis(Random.Range(0, 360), Vector3.up)) as GameObject;
                            newObj.transform.parent = obj.transform.parent;
                            newObj.SetActive(true);
                            spawns.push(newObj.GetComponent <SpawnObject> ());
                            spawnCount++;
                            return;
                        }
                    }
                }
            }
        }
    }
Exemple #8
0
        private void OnDrawGizmos()
        {
            Gizmos.matrix = transform.localToWorldMatrix;
            Gizmos.color  = Colors.VolumeOutline;
            Gizmos.DrawWireCube(0.5f * ContainerSize, ContainerSize);

            Gizmos.color = Colors.VolumeSolid;
            if (SolidRegions != null)
            {
                foreach (var solid in SolidRegions)
                {
                    Gizmos.DrawCube(0.5f * (solid.Max + solid.Min), solid.Max - solid.Min);
                }
            }

            Gizmos.color = Colors.CellSource;
            if (Sources != null)
            {
                foreach (var source in Sources)
                {
                    Gizmos.DrawCube(GridStepSize * ((Vector3)Vec3Int.FloorToInt(source.Position / GridStepSize) + Vector3.one * 0.5f),
                                    Vector3.one * GridStepSize);
                }
            }

            Gizmos.color = Colors.CellSink;
            if (Sinks != null)
            {
                foreach (var sink in Sinks)
                {
                    Gizmos.DrawCube(GridStepSize * ((Vector3)Vec3Int.FloorToInt(sink / GridStepSize) + Vector3.one * 0.5f),
                                    Vector3.one * GridStepSize);
                }
            }
        }
Exemple #9
0
    // Use this for initialization
    protected override void Start()
    {
        base.Start();
        biomeScript = BiomeScript.Instance;
        Vec3Int chunkCoords = vxe.getChunkCoords(transform.position);

        vxe.chunkGameObjects [chunkCoords.x, chunkCoords.y, chunkCoords.z].GetComponent <MeshRenderer> ().material = vxe.debugMaterial;
    }
Exemple #10
0
 /// <summary>
 /// Waits to initialize.
 /// </summary>
 /// <returns>The to initialize.</returns>
 IEnumerator waitToInitialize()
 {
     while (biome.biomeMap == null)
     {
         yield return(new WaitForEndOfFrame());
     }
     playerCC = vxe.getChunkCoords(playerTrans.position);
     mybiome  = biome.biomeMap [playerCC.x, playerCC.z];
 }
Exemple #11
0
    /// <summary>
    /// Sets the environment list based on biome.
    /// </summary>
    /// <returns>The environment list based on biome.</returns>
    /// <param name="Pos">Position.</param>
    GameObject[] SetEnvironmentListBasedOnBiome(Vector3 Pos)
    {
        playerCC = vxe.getChunkCoords(Pos);
        chunkx   = playerCC.x;
        chunkz   = playerCC.z;

        mybiome = biome.biomeMap [chunkx, chunkz];
        return(getAssetListBasedOnBiome(mybiome));
    }
Exemple #12
0
 IEnumerator waitToInitialize()
 {
     while (biome.biomeMap == null) {
         yield return new WaitForEndOfFrame ();
     }
     playerCC = vxe.getChunkCoords (playerTrans.position);
     mybiome=biome.getBiomeFromCoords (playerCC);
     prevBiome = mybiome;
     switchBiomeAudio (mybiome);
 }
Exemple #13
0
        public void AddSink(Vector3 pos)
        {
            var p = Vec3Int.FloorToInt(pos / StepSize);

            if (!ValidIndex(p))
            {
                throw new IndexOutOfRangeException($"pos {pos} => p {p} is out of bounds");
            }
            CellType[Idx(p)] = CTYPE_SINK;
        }
Exemple #14
0
 IEnumerator waitToInitialize()
 {
     while (biome.biomeMap == null)
     {
         yield return(new WaitForEndOfFrame());
     }
     playerCC  = vxe.getChunkCoords(playerTrans.position);
     mybiome   = biome.getBiomeFromCoords(playerCC);
     prevBiome = mybiome;
     switchBiomeAudio(mybiome);
 }
Exemple #15
0
        public void AddSource(Source source)
        {
            var p = Vec3Int.FloorToInt(source.Position / StepSize);

            if (!ValidIndex(p))
            {
                throw new IndexOutOfRangeException($"pos {source.Position} => p {p} is out of bounds");
            }
            CellType[Idx(p)] = CTYPE_SOURCE;
            sources.Add(p, source);
        }
Exemple #16
0
        //[MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool ValidIndex(Vec3Int p)
        {
            var valid = p.x >= 0 && p.x < GridSize.x &&
                        p.y >= 0 && p.y < GridSize.y &&
                        p.z >= 0 && p.z < GridSize.z;

            if (!valid)
            {
                Debug.LogError("Invalid Index!");
            }
            return(valid);
        }
        float getCachedSDFInCell(Vec3Int index)
        {
            if (cache.TryGetSdf(index, out var cachedSdf))
            {
                return(cachedSdf);
            }

            var sdf = getSDF(index);

            cache.SetSdf(index, sdf);

            return(sdf);
        }
Exemple #18
0
        // For values that are staggered on the grid (i.e. stored for each face, not for each
        // cell center), for each axis an extra value is valid.
        //[MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool ValidIndexStaggered(Vec3Int p, Axis axis)
        {
            var maxX  = axis == Axis.X ? GridSize.x + 1: GridSize.x;
            var maxY  = axis == Axis.Y ? GridSize.y + 1: GridSize.y;
            var maxZ  = axis == Axis.Z ? GridSize.z + 1: GridSize.z;
            var valid = p.x >= 0 && p.x < maxX &&
                        p.y >= 0 && p.y < maxY &&
                        p.z >= 0 && p.z < maxZ;

            if (!valid)
            {
                Debug.LogError("Invalid Staggered Index!");
            }
            return(valid);
        }
        private Vector3 ConvertVertexIndexToPosition(int index, Vec3Int ijk)
        {
            switch (index)
            {
            case 0: return(new Vector3(g(ijk, Vec3Int.right), 0, 0));

            case 1: return(new Vector3(0, g(ijk, Vec3Int.up), 0));

            case 2: return(new Vector3(g(ijk + Vec3Int.up, Vec3Int.right), 1, 0));

            case 3: return(new Vector3(1, g(ijk + Vec3Int.right, Vec3Int.up), 0));

            case 4: return(new Vector3(0, 1, g(ijk + Vec3Int.up, Vec3Int.forward)));

            case 5: return(new Vector3(1, 1, g(ijk + new Vec3Int(1, 1, 0), Vec3Int.forward)));

            case 6: return(new Vector3(1, 0, g(ijk + Vec3Int.right, Vec3Int.forward)));

            case 7: return(new Vector3(0, 0, g(ijk, Vec3Int.forward)));

            case 8: return(new Vector3(g(ijk + Vec3Int.forward, Vec3Int.right), 0, 1));

            case 9: return(new Vector3(0, g(ijk + Vec3Int.forward, Vec3Int.up), 1));

            case 10: return(new Vector3(g(ijk + new Vec3Int(0, 1, 1), Vec3Int.right), 1, 1));

            case 11: return(new Vector3(1, g(ijk + new Vec3Int(1, 0, 1), Vec3Int.up), 1));
            }

            throw new Exception("Wrong vertex index");

            float f(float a, float b)
            {
                float res = a / (a - b);

                //res = Mathf.Clamp01(res);
                if (res < 0 || res > 1)
                {
                    ;
                }
                return(res);
            }

            float g(Vec3Int ind, Vec3Int offset)
            {
                return(f(getCachedSDFInCell(ind), getCachedSDFInCell(ind + offset)));
            }
        }
Exemple #20
0
    /// <summary>
    /// Does a the whole pull spawn process in one place.
    /// </summary>
    /// <returns>The pull spawn.</returns>
    IEnumerator FullPullSpawn()
    {
        Vector3 chunkBaseCoords;

        GameObject[] assetList;

        while (true)
        {
            for (int i = vxe.occupiedChunks.getCount() - 1; i > -1; i--)
            {
                //Chunk Voxel Coordinates from the Occupied Chunk Stack
                Vec3Int chunkVXCoords = vxe.occupiedChunks.peek(i);

                //Get the Correct Environment Assets based on Biome
                assetList = GetEnvironmentListBasedOnBiome(chunkVXCoords);

                //Chunks World Coords

                chunk = vxe.grid.voxelGrid [chunkVXCoords.x, chunkVXCoords.y, chunkVXCoords.z];

                if (chunk == null || assetList.Length == 0)
                {
                    continue;
                }

                if (spawnCount > max_spawns)
                {
                    yield break;
                }
                //If the chunk is a surface and is not in the HashTable, do Spawning code
                if (!chunk.spawnPopulated && chunk.voxel_count > minVoxelCount)
                {
                    if (Random.Range(0, 3) == 0)
                    {
                        GameObject gbj = assetList [Random.Range(0, assetList.Length)];
                        onManyVoxelPosition(chunk, chunkVXCoords, gbj);
                    }

                    chunk.spawnPopulated = true;
                }
                yield return(null);
            }
            yield return(new WaitForSeconds(1.0f));
        }
    }
Exemple #21
0
        public static float LerpCenters(MacGrid g, float[] q, Vector3 pos)
        {
            Vec3Int p0 = Vec3Int.FloorToInt(pos);
            // Because we're using grid coordinates here, not fluid space, we can rely on the
            // fact the spacing (on one axis) between two samples values is always 1.
            Vector3 alpha = pos - p0;

            // This is just simple trilinear interpolation, written out in full.
            float q00 = (1f - alpha.x) * q[g.Idx(p0.x, p0.y, p0.z)] + alpha.x * q[g.Idx(p0.x + 1, p0.y, p0.z)];
            float q10 = (1f - alpha.x) * q[g.Idx(p0.x, p0.y + 1, p0.z)] + alpha.x * q[g.Idx(p0.x + 1, p0.y + 1, p0.z)];
            float q01 = (1f - alpha.x) * q[g.Idx(p0.x, p0.y, p0.z + 1)] + alpha.x * q[g.Idx(p0.x + 1, p0.y, p0.z + 1)];
            float q11 = (1f - alpha.x) * q[g.Idx(p0.x, p0.y + 1, p0.z + 1)] + alpha.x * q[g.Idx(p0.x + 1, p0.y + 1, p0.z + 1)];

            float q0 = (1f - alpha.y) * q00 + alpha.y * q10;
            float q1 = (1f - alpha.y) * q01 + alpha.y * q11;

            return((1f - alpha.z) * q0 + alpha.z * q1);
        }
Exemple #22
0
        public MacGrid(Vector3 size, float step)
        {
            Size     = size;
            StepSize = step;

            GridSize = Vec3Int.FloorToInt(Size / StepSize);

            Pressure  = new float[GridSize.x * GridSize.y * GridSize.z];
            VelocityX = new float[(GridSize.x + 1) * GridSize.y * GridSize.z];
            VelocityY = new float[GridSize.x * (GridSize.y + 1) * GridSize.z];
            VelocityZ = new float[GridSize.x * GridSize.y * (GridSize.z + 1)];
            CellType  = new byte[GridSize.x * GridSize.y * GridSize.z];

            sources = new Dictionary <Vec3Int, Source>();

            PressureNext  = new float[GridSize.x * GridSize.y * GridSize.z];
            VelocityNextX = new float[(GridSize.x + 1) * GridSize.y * GridSize.z];
            VelocityNextY = new float[GridSize.x * (GridSize.y + 1) * GridSize.z];
            VelocityNextZ = new float[GridSize.x * GridSize.y * (GridSize.z + 1)];
        }
Exemple #23
0
        // Only one of the stagger flags can be set!
        public static float Lerp(MacGrid g, float[] q, Vector3 pos,
                                 bool staggerX = false, bool staggerY = false, bool staggerZ = false)
        {
            Vec3Int staggerAdjustment;

            if (staggerX)
            {
                staggerAdjustment = Vec3Int.right;
            }
            else if (staggerY)
            {
                staggerAdjustment = Vec3Int.up;
            }
            else if (staggerZ)
            {
                staggerAdjustment = Vec3Int.forward;
            }
            else
            {
                staggerAdjustment = Vec3Int.zero;
            }

            // p0 gives us the array-coordinates of lower-back-left sample point next to the given pos.
            Vec3Int p0 = Vec3Int.FloorToInt(pos - staggerAdjustment * 0.5f) + staggerAdjustment;
            // The actual position in grid coordinates is as above but with 0.5f * staggerAdjustment added on the end:
            // p0 is correct for indexing to the array, but the staggering causes a mismatch between indices and coordinates.
            // Because we're using grid based coordinates here, not fluid space, we can rely on the
            // fact the spacing (on one axis) between two samples values is always 1.
            Vector3 alpha = pos - p0 - staggerAdjustment * 0.5f;

            // This is just simple trilinear interpolation, written out in full.
            float q00 = (1f - alpha.x) * q[g.Idx(p0.x, p0.y, p0.z)] + alpha.x * q[g.Idx(p0.x + 1, p0.y, p0.z)];
            float q10 = (1f - alpha.x) * q[g.Idx(p0.x, p0.y + 1, p0.z)] + alpha.x * q[g.Idx(p0.x + 1, p0.y + 1, p0.z)];
            float q01 = (1f - alpha.x) * q[g.Idx(p0.x, p0.y, p0.z + 1)] + alpha.x * q[g.Idx(p0.x + 1, p0.y, p0.z + 1)];
            float q11 = (1f - alpha.x) * q[g.Idx(p0.x, p0.y + 1, p0.z + 1)] + alpha.x * q[g.Idx(p0.x + 1, p0.y + 1, p0.z + 1)];

            float q0 = (1f - alpha.y) * q00 + alpha.y * q10;
            float q1 = (1f - alpha.y) * q01 + alpha.y * q11;

            return((1f - alpha.z) * q0 + alpha.z * q1);
        }
Exemple #24
0
    bool checkForJumpPositions(Vector3 dir, out Vector3 out_minAngleDir)
    {
        Vec3Int jumpCoords = vxe.ToGrid(transform.position);

        bool    canJump     = false;
        float   maxdotprod  = float.MinValue;
        Vector3 minAngleDir = Vector3.zero;

        for (int i = -JUMP_RANGE; i <= JUMP_RANGE; i++)
        {
            for (int j = -JUMP_RANGE; j <= JUMP_RANGE; j++)
            {
                for (int k = MAX_JUMP_HEIGHT; k >= 1; k--)
                {
                    Vec3Int vcoords = jumpCoords + new Vec3Int(i, k, j);
                    Voxel   vx      = vxe.grid.getVoxel(vcoords);
                    if (vx.isOccupied() && vxe.voxelHasSurface(vx, VF.VX_TOP_SHOWN))
                    {
                        Vector3 wrldcoords = vxe.FromGridUnTrunc(vcoords.ToVec3() + new Vector3(0.5f, 1.0f, 0.5f));
                        Vector3 vdir       = Vector3.ProjectOnPlane((wrldcoords - transform.position), Vector3.up).normalized;
                        float   dotprod    = Vector3.Dot(dir, vdir);
                        if (dotprod > 0)
                        {
                            canJump = true;
                            if (dotprod > maxdotprod)
                            {
                                maxdotprod   = dotprod;
                                minAngleDir  = vdir;
                                jumpPosition = wrldcoords;
                            }
                        }
                    }
                }
            }
        }



        out_minAngleDir = minAngleDir;
        return(canJump);
    }
Exemple #25
0
    // Update is called once per frame
    void Update()
    {
        framecount++;
        if (spawned || framecount % spawnInterval != 0)
        {
            return;
        }

        Vector3 pos = new Vector3(), normal = new Vector3();

        if (vxe.RayCast(camera.transform.position, camera.transform.forward, 64, ref pos, ref normal))
        {
            Vec3Int chunkcoord = vxe.ToGrid(pos) / vxe.chunk_size;

            if (Vector3.Dot(normal, Vector3.up) > 0.999f)
            {
                GameObject newsphere = (GameObject)Instantiate(pet, pos + normal * VoxelExtractionPointCloud.Instance.voxel_size * 0.5f, Quaternion.identity);
                newsphere.SetActive(true);
                spawned = true;
            }
        }
    }
Exemple #26
0
    public void addChunkToBiomeOccChunks(Vec3Int cc)
    {
        int biomeIndex = (int)biomeMap [cc.x, cc.z];

        biomeOccupiedChunks [biomeIndex].push(cc);
    }
Exemple #27
0
    IEnumerator SpawnItems()
    {
        yield return(new WaitForSeconds(5.0f));

        Vector3 coords = Vector3.zero, norm = Vector3.zero;

        bool hitsomething = false;

        while (!hitsomething)
        {
            hitsomething = vxe.RayCast(camera.transform.position, Vector3.down, 64, ref coords, ref norm, 1.0f);
            yield return(null);
        }

        Random.seed = System.DateTime.Now.Millisecond;
        floorChunkY = vxe.getChunkCoords(coords).y;
        Vec3Int prevcc  = new Vec3Int(vxe.num_chunks_x / 2, vxe.num_voxels_y / 2, vxe.num_chunks_z / 2);
        Vector2 prevdir = Vector2.zero;

        for (int s = 0; s < stages.Length; s++)
        {
            currentStage = s;
            ItemInfo[] items = stages[s].items;
            nextStage = false;

            for (int i = 0; i < items.Length; i++)
            {
                int biomeIndex = (int)items[i].biome;
                IndexStack <Vec3Int> occupiedChunks =
                    items[i].biome == BIOMES.none ? vxe.occupiedChunks : BiomeScript.Instance.biomeOccupiedChunks[biomeIndex];

                bool spawned  = false;
                int  maxdist  = 3;
                int  attempts = 0;
                while (!spawned)
                {
                    int chunkx;
                    int chunkz;

                    while (true)
                    {
                        int count  = occupiedChunks.getCount();
                        int period = Random.Range(0, count);

                        Vec3Int randomCC = occupiedChunks.peek(period);
                        chunkx = randomCC.x;
                        chunkz = randomCC.z;


                        bool isFarEnough = true;
                        int  start       = Mathf.Max(0, prevpositions.getCount() - 3);

                        for (int k = start; k < prevpositions.getCount(); k++)
                        {
                            Vec3Int pcc  = prevpositions.peek(k);
                            int     dist = (chunkx - pcc.x) * (chunkx - pcc.x) + (chunkz - pcc.z) * (chunkz - pcc.z);
                            Debug.Log(dist);
                            if (dist < maxdist * maxdist)
                            {
                                isFarEnough = false;
                                break;
                            }
                        }


                        if (SpawnCount == 0 || isFarEnough)
                        {
                            break;
                        }

                        attempts++;

                        if (attempts % 20 == 0)
                        {
                            if (maxdist > 1)
                            {
                                maxdist--;
                            }
                        }

                        yield return(null);
                    }



                    Chunks chunk = null;

                    for (int k = floorChunkY + range; k >= floorChunkY; k--)
                    {
                        chunk = vxe.grid.voxelGrid [chunkx, k, chunkz];
                        Chunks chunkup   = vxe.grid.voxelGrid [chunkx, k + 1, chunkz];
                        bool   isthereUp = (chunkup != null && chunkup.voxel_count > 3);

                        if (!isthereUp && chunk != null && chunk.voxel_count > 60 && vxe.isChunkASurface(DIR.DIR_UP, chunk, 0.5f))
                        {
                            Vector3 chunkBaseCoords = new Vector3(chunkx, k, chunkz) * vxe.chunk_size;



                            for (int ox = 0; ox < vxe.chunk_size; ox++)
                            {
                                int x = (ox + vxe.chunk_size / 2 - 1) % vxe.chunk_size;
                                for (int oz = 0; oz < vxe.chunk_size; oz++)
                                {
                                    int z = (oz + vxe.chunk_size / 2 - 1) % vxe.chunk_size;
                                    for (int y = vxe.chunk_size - 1; y >= 0; y--)
                                    {
                                        Voxel vx = chunk.getVoxel(new Vec3Int(x, y, z));

                                        if (vx.isOccupied() && vxe.voxelHasSurface(vx, VF.VX_TOP_SHOWN))
                                        {
                                            Vector3 voxelCoords = vxe.FromGridUnTrunc(chunkBaseCoords + new Vector3(x, y, z));
                                            if (voxelCoords.y < coords.y + items [i].minSpawnHeightOffFloor * vxe.voxel_size ||
                                                voxelCoords.y > coords.y + items [i].maxSpawnHeightOffFloor * vxe.voxel_size)
                                            {
                                                continue;
                                            }

                                            GameObject newItem = (GameObject)Instantiate(items [i].item, voxelCoords + new Vector3(0, vxe.voxel_size, 0), Quaternion.identity);
                                            newItem.SetActive(true);


                                            newItem.GetComponent <VoxelParent>().chunkCoords = new Vec3Int(chunkx, k, chunkz);

                                            prevpositions.push(new Vec3Int(chunkx, 0, chunkz));

                                            SpawnCount++;

                                            spawned = true;
                                            Debug.Log("spawned!");
                                            canSpawn = false;
                                            goto imout;
                                        }
                                        yield return(null);
                                    }
                                }
                            }
                        }
                    }

imout:

                    if (spawned && !stages[s].allAtOnce)
                    {
                        while (!canSpawn)
                        {
                            yield return(new WaitForSeconds(1.0f));
                        }
                    }
                    else
                    {
                        yield return(null);
                    }
                }
            }

            while (stages[s].stageWait && !nextStage)
            {
                yield return(new WaitForSeconds(1.0f));
            }
        }
    }
Exemple #28
0
    void buildChunkMesh(Chunks chunk, Vec3Int chunkCoords, Mesh _mesh)
    {
        int[]            indices = new int[ChunkTemplate.Instance.vertex_count * 3];
        IndexStack <int> istack  = new IndexStack <int>(indices);


        for (int x = 0; x < vxe.chunk_size; x++)
        {
            for (int y = 0; y < vxe.chunk_size; y++)
            {
                for (int z = 0; z < vxe.chunk_size; z++)
                {
                    Vec3Int vcoord = new Vec3Int(x, y, z);
                    Voxel   voxel  = chunk.getVoxel(vcoord);

                    if (voxel.isOccupied())
                    {
                        Vector3 vwrldcoord = vxe.FromGrid(chunkCoords * vxe.chunk_size + vcoord);
                        if ((vwrldcoord - camera.transform.position).sqrMagnitude > 0.64f)
                        {
                            continue;
                        }

                        //front
                        if (voxel.getFace(VF.VX_FRONT_SHOWN))
                        {
                            //front
                            istack.push(chunk.getIndex(x, y, z + 1, DIR.DIR_FRONT));
                            istack.push(chunk.getIndex(x + 1, y, z + 1, DIR.DIR_FRONT));
                            istack.push(chunk.getIndex(x + 1, y + 1, z + 1, DIR.DIR_FRONT));
                            istack.push(chunk.getIndex(x, y + 1, z + 1, DIR.DIR_FRONT));
                        }

                        if (voxel.getFace(VF.VX_RIGHT_SHOWN))
                        {
                            //right
                            istack.push(chunk.getIndex(x + 1, y, z, DIR.DIR_RIGHT));
                            istack.push(chunk.getIndex(x + 1, y + 1, z, DIR.DIR_RIGHT));
                            istack.push(chunk.getIndex(x + 1, y + 1, z + 1, DIR.DIR_RIGHT));
                            istack.push(chunk.getIndex(x + 1, y, z + 1, DIR.DIR_RIGHT));
                        }

                        if (voxel.getFace(VF.VX_BACK_SHOWN))
                        {
                            //back
                            istack.push(chunk.getIndex(x, y, z, DIR.DIR_BACK));
                            istack.push(chunk.getIndex(x, y + 1, z, DIR.DIR_BACK));
                            istack.push(chunk.getIndex(x + 1, y + 1, z, DIR.DIR_BACK));
                            istack.push(chunk.getIndex(x + 1, y, z, DIR.DIR_BACK));
                        }

                        if (voxel.getFace(VF.VX_LEFT_SHOWN))
                        {
                            //left
                            istack.push(chunk.getIndex(x, y, z, DIR.DIR_LEFT));
                            istack.push(chunk.getIndex(x, y, z + 1, DIR.DIR_LEFT));
                            istack.push(chunk.getIndex(x, y + 1, z + 1, DIR.DIR_LEFT));
                            istack.push(chunk.getIndex(x, y + 1, z, DIR.DIR_LEFT));
                        }

                        if (voxel.getFace(VF.VX_TOP_SHOWN))
                        {
                            //top
                            istack.push(chunk.getIndex(x, y + 1, z, DIR.DIR_UP));
                            istack.push(chunk.getIndex(x, y + 1, z + 1, DIR.DIR_UP));
                            istack.push(chunk.getIndex(x + 1, y + 1, z + 1, DIR.DIR_UP));
                            istack.push(chunk.getIndex(x + 1, y + 1, z, DIR.DIR_UP));
                        }

                        if (voxel.getFace(VF.VX_BOTTOM_SHOWN))
                        {
                            //bottom
                            istack.push(chunk.getIndex(x, y, z, DIR.DIR_DOWN));
                            istack.push(chunk.getIndex(x + 1, y, z, DIR.DIR_DOWN));
                            istack.push(chunk.getIndex(x + 1, y, z + 1, DIR.DIR_DOWN));
                            istack.push(chunk.getIndex(x, y, z + 1, DIR.DIR_DOWN));
                        }
                    }
                }
            }
        }

        int[] indexArray = new int[istack.getCount()];
        System.Array.Copy(istack.getArray(), indexArray, istack.getCount());

        _mesh.SetIndices(indexArray, MeshTopology.Quads, 0);
    }
Exemple #29
0
    // Update is called once per frame
    void Update()
    {
        framecount++;

        //
        if ((framecount % 10 != 0 || (thingsSpawned.Count > maxToSpawnInBiome * 4)) && portal.activeInHierarchy)
        {
            return;
        }

        Random.seed = (int)(Time.deltaTime * 1000);

        //A Random Point in your Camera View Port
        Vector3 ranPt = new Vector3(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), backCam.nearClipPlane);
        //The World Point you see thru camera
        Vector3 startpt = backCam.ViewportToWorldPoint(ranPt);
        //The Direction vector from Camera to start point
        Vector3 dir = startpt - backCam.transform.position;
        Vector3 pos = new Vector3(), normal = new Vector3();

        if (vxe.RayCast(startpt, dir, 64, ref pos, ref normal))
        {
            //SPAWN PORTAL:*********************************************************************
            //If the portal is not active, try spawning one
            if (!portal.activeInHierarchy)
            {
                spawnPortal(pos, 0.6f, chunkFloorPos.y);
            }

            ///SPAWNING FOR ANIMALS*********************************************************************
            //Convert the voxel position into a Chunk World Position
            Vec3Int chunkcoord = vxe.ToGrid(pos) / vxe.chunk_size;

            //Loads up the BiomeMap through the grid size
            BIOMES mybiome = biome.biomeMap [chunkcoord.x, chunkcoord.z];

            //If there are too many spawns in this Biome, do not spawn more
            if (spawnCountInBoime [(int)mybiome] > maxToSpawnInBiome)
            {
                return;
            }

            //randomly chooses an animal to spawn
            int        animalIndex = Random.Range(0, spawnTable [mybiome].SpawnList.Length);
            GameObject spawnObject = spawnTable [mybiome].SpawnList [animalIndex].gameObject;

            Chunks chunk     = vxe.getChunkFromPt(pos);
            bool   isSurface = vxe.isChunkASurface(DIR.DIR_UP, chunk, .65f);

            if (spawnTable [mybiome].SpawnList [animalIndex].sticksToWalls || isSurface)               // Vector3.Dot (normal, Vector3.up) > 0.999f) {
            {
                GameObject newsphere = (GameObject)Instantiate(spawnObject, pos + normal * VoxelExtractionPointCloud.Instance.voxel_size * 0.5f, Quaternion.identity);
                newsphere.SetActive(true);
                //SimpleAI ai = newsphere.GetComponent<SimpleAI> ();
                thingsSpawned.Add(newsphere);
                //newsphere.GetComponent<GrowScript>().init(pos, normal, (Vector3.Dot (normal,Vector3.up) > 0.999f) );

                spawnCountInBoime [(int)mybiome]++;
            }
        }
    }
    /// <summary>
    /// Sets the environment list based on biome.
    /// </summary>
    /// <returns>The environment list based on biome.</returns>
    /// <param name="Pos">Position.</param>
    List<GameObject> SetEnvironmentListBasedOnBiome(Vector3 Pos)
    {
        playerCC = vxe.getChunkCoords (Pos);
        chunkx = playerCC.x;
        chunkz = playerCC.z;

        mybiome = biome.biomeMap [chunkx, chunkz];
        return getNextEnvironmentGameObject (mybiome);
    }
 /// <summary>
 /// Waits to initialize.
 /// </summary>
 /// <returns>The to initialize.</returns>
 IEnumerator waitToInitialize()
 {
     while (biome.biomeMap == null) {
         yield return new WaitForEndOfFrame ();
     }
     playerCC = vxe.getChunkCoords (playerTrans.position);
     mybiome = biome.biomeMap [playerCC.x, playerCC.z];
 }
    /// <summary>
    /// Spawns a GameObject on many voxels in a chunk.
    /// </summary>
    /// <param name="chunk">Chunk.</param>
    /// <param name="chunkGridCoord">Chunk grid coordinate.</param>
    /// <param name="obj">Object.</param>
    void onManyVoxelPosition(Chunks chunk, Vec3Int chunkGridCoord, GameObject obj)
    {
        Vector3 chunkBCoords = new Vector3 (chunkGridCoord.x, chunkGridCoord.y, chunkGridCoord.z) * vxe.chunk_size;

        Random.seed = (int)Time.frameCount;

        for (int x=0; x<vxe.chunk_size; x++)
            for (int z=0; z<vxe.chunk_size; z++)
            {
                for (int y=vxe.chunk_size-1; y>=0; y--)
                {
                    Voxel vx = chunk.getVoxel (new Vec3Int (x, y, z));

                    if (vx.isOccupied () && vxe.voxelHasSurface (vx, VF.VX_TOP_SHOWN))
                    {
                        Vector3 voxelCoords = vxe.FromGridUnTrunc (chunkBCoords + new Vector3 (x, y, z));

                        if (Random.Range (0, 20) == 0)
                        {
                            GameObject newObj = Instantiate (obj, voxelCoords + Vector3.up * vxe.voxel_size * 1.0f, Quaternion.AngleAxis(Random.Range (0,360),Vector3.up)) as GameObject;
                            newObj.transform.parent = obj.transform.parent;
                            newObj.SetActive (true);
                            spawns.push(newObj.GetComponent<SpawnObject>());
                            spawnCount++;
                            break;
                        }
                    }
                }
            }
    }
 /// <summary>
 /// Senvironment list based on biome.
 /// </summary>
 /// <returns>The environment list based on biome.</returns>
 /// <param name="Pos">Position.</param>
 List<GameObject> GetEnvironmentListBasedOnBiome(Vec3Int Pos)
 {
     return getNextEnvironmentGameObject (biome.biomeMap [Pos.x, Pos.z]);
 }
Exemple #34
0
 public BIOMES getBiomeFromCoords(Vec3Int cc)
 {
     return(biomeMap [cc.x, cc.z]);
 }
Exemple #35
0
 public Material getBiomeMaterialFromCoords(Vec3Int cc)
 {
     return(biomeInfo [(int)biomeMap [cc.x, cc.z]].getRandomMat());
 }
Exemple #36
0
    /// <summary>
    /// Gets environment list based on biome.
    /// </summary>
    /// <returns>The environment list based on biome.</returns>
    /// <param name="Pos">Position.</param>
    GameObject[] GetEnvironmentListBasedOnBiome(Vec3Int Pos)
    {
        return(getAssetListBasedOnBiome((biome.getBiomeFromCoords(Pos))));

        //return getNextEnvironmentGameObject (biome.biomeMap [Pos.x, Pos.z]);
    }