Exemple #1
0
    public Grid(RAKTerrain terrain)
    {
        Vector3 terrainSize       = terrain.terrain.terrainData.size;
        int     numberOfXElements = (int)(terrainSize.x / ELEMENT_SIZE.x);
        int     numberOfZElements = (int)(terrainSize.z / ELEMENT_SIZE.y);

        elements = new GridSector[numberOfXElements * numberOfZElements];
        int     elementCount    = 0;
        Vector3 terrainPosition = terrain.transform.position;

        for (int x = 0; x < numberOfXElements; x++)
        {
            for (int z = 0; z < numberOfZElements; z++)
            {
                Vector2 elementWorldPosition = new Vector2(terrainPosition.x + x * ELEMENT_SIZE.x,
                                                           terrainPosition.z + z * ELEMENT_SIZE.y);
                Vector2 elementWorldPositionEnd = new Vector2(
                    elementWorldPosition.x + ELEMENT_SIZE.x, elementWorldPosition.y + ELEMENT_SIZE.y);
                elements[elementCount] = new GridSector(new Vector2(x, z),
                                                        elementWorldPosition, elementWorldPositionEnd, terrain.name, terrain);
                elementCount++;
            }
        }
        Debug.Log("Grid generation complete");
    }
Exemple #2
0
        public GridSector GetRandomGridSector()
        {
            RAKTerrain[] terrain       = world.masterTerrain.getTerrain();
            int          index         = Random.Range(0, terrain.Length);
            RAKTerrain   chosenTerrain = terrain[index];

            GridSector[] sectorsInTerrain = chosenTerrain.GetGridElements();
            index = Random.Range(0, sectorsInTerrain.Length);
            return(sectorsInTerrain[index]);
        }
Exemple #3
0
    public GridSector(Vector2 gridPosition, Vector2 worldPositionStart,
                      Vector2 worldPositionEnd, string terrainName, RAKTerrain terrain)
    {
        name = (terrainName + "-" + gridPosition.x + "-" + gridPosition.y);
        this.gridPosition       = gridPosition;
        this.worldPositionStart = worldPositionStart;
        this.worldPositionEnd   = worldPositionEnd;
        this.parentTerrain      = terrain;
        float   y     = 0;
        Vector3 start = new Vector3(worldPositionStart.x, y, worldPositionStart.y);
        Vector3 end   = new Vector3(worldPositionEnd.x, y, worldPositionEnd.y);

        Debug.DrawLine(start, new Vector3(end.x, y, start.z), Color.yellow, 3);
        Debug.DrawLine(end, new Vector3(end.x, y, start.z), Color.yellow, 3);
    }
Exemple #4
0
    public RAKTerrain GetClosestTerrainToPoint(Vector3 point)
    {
        RAKTerrain closest         = null;
        float      closestDistance = 20000;

        foreach (RAKTerrain item in terrain)
        {
            float distance = Vector3.Distance(point, item.transform.position);
            if (distance < closestDistance)
            {
                closestDistance = distance;
                closest         = item;
            }
        }
        return(closest);
    }
Exemple #5
0
        public static bool saveTerrainBytes(RAKTerrain terrain, string path)
        {
            TerrainData data = terrain.getTerrainData();

            float[,,] alphaMap = data.GetAlphamaps(0, 0, data.alphamapWidth, data.alphamapHeight);
            float[,] heightMap = data.GetHeights(0, 0, data.heightmapWidth, data.heightmapHeight);
            SplatPrototype[] splats = data.splatPrototypes;
            Debug.LogWarning("Saving terrain");

            Vector3             terrainDataSize = terrain.terrain.terrainData.size;
            RAKTerrainSavedData saveData        = new RAKTerrainSavedData(alphaMap, heightMap, splats, data.treePrototypes,
                                                                          data.treeInstances, terrain.nonTerrainObjects, terrainDataSize);

            saveTerrainObject(saveData, path);
            terrain.savedData = saveData;
            return(true);
        }
Exemple #6
0
        public GridSector GetCurrentGridSector(Transform transform)
        {
            RAKTerrain closestTerrain = world.masterTerrain.GetClosestTerrainToPoint(transform.position);

            GridSector[] sectors        = closestTerrain.GetGridElements();
            float        closestDist    = Mathf.Infinity;
            int          indexOfClosest = -1;

            for (int count = 0; count < sectors.Length; count++)
            {
                float currentDistance = Vector3.Distance(transform.position, sectors[count].GetSectorPosition);
                if (currentDistance < closestDist)
                {
                    closestDist    = currentDistance;
                    indexOfClosest = count;
                }
            }

            return(sectors[indexOfClosest]);
        }
Exemple #7
0
    // Create new unity objects from data from disk //
    private void generateNonTerrainDetailsFromDisk(RAKTerrain terrain)
    {
        // Get all objects from save //
        RAKTerrainObjectSaveData[] objects = terrain.savedData.nonTerrainObjectsSaveData;
        List <RAKTerrainObject>    loadedTerrainObjects = new List <RAKTerrainObject>();

        for (int count = 0; count < objects.Length; count++)
        {
            // Retrieve the prefab and Instantiate //
            Debug.LogWarning(objects[count].prefabObjectIndex);
            if (objects[count].prefabObjectIndex == -1)
            {
                Debug.LogWarning("Object # " + count + " returned -1");
                continue;
            }
            string           prefabName    = RAKUtilities.nonTerrainObjects[objects[count].prefabObjectIndex];
            RAKTerrainObject terrainObject = RAKUtilities.getTerrainObjectPrefab(prefabName).GetComponent <RAKTerrainObject>();
            GameObject       prefab        = (GameObject)Instantiate(terrainObject.gameObject, objects[count].position.getVector3(), Quaternion.identity);//, 2);
            prefab.transform.eulerAngles = objects[count].rotationEulers.getVector3();
            prefab.transform.SetParent(terrain.transform);
            loadedTerrainObjects.Add(terrainObject);
        }
        terrain.nonTerrainObjects = loadedTerrainObjects.ToArray();
    }
Exemple #8
0
        public static GridSector[] GetPiecesOfTerrainCreatureCanSee(Creature requester, float distance,
                                                                    RAKTerrain terrain)
        {
            List <GridSector> elementsWithinRange = new List <GridSector>();

            GridSector[] elements = terrain.GetGridElements();
            foreach (GridSector element in elements)
            {
                if (Vector3.Distance(requester.transform.position, element.GetSectorPosition) <= distance)
                {
                    elementsWithinRange.Add(element);
                }
            }
            if (terrain.neighbors == null)
            {
                return(elementsWithinRange.ToArray());
            }
            for (int count = 0; count < terrain.neighbors.Length; count++)
            {
                Terrain neighbor = terrain.neighbors[count];
                if (neighbor == null)
                {
                    continue;
                }
                RAKTerrain   neighborTerrain = neighbor.gameObject.GetComponent <RAKTerrain>();
                GridSector[] sectors         = neighborTerrain.GetGridElements();
                foreach (GridSector element in elements)
                {
                    if (Vector3.Distance(requester.transform.position, element.GetSectorPosition) <= distance)
                    {
                        elementsWithinRange.Add(element);
                    }
                }
            }
            return(elementsWithinRange.ToArray());
        }
Exemple #9
0
 public float GetTerrainHeightAt(Vector2 position, RAKTerrain terrain)
 {
     return(terrain.GetHeightAt(position));
 }
Exemple #10
0
    // Generate details for a RAKTerrain object //
    private void generateNonTerrainDetailPrefabs(Terrain terrain)
    {
        RAKTerrain  rakTerrain = terrain.GetComponent <RAKTerrain>();
        TerrainData data       = terrain.terrainData;
        RAKBiome    biome      = rakTerrain.getBiome();
        int         sum        = 0;

        Array.ForEach(biome.objectCounts, delegate(int i) { sum += i; });
        int totalNumberOfObjects = sum;

        GameObject[] details            = new GameObject[totalNumberOfObjects];
        int          currentDetailCount = 0;

        for (int currentObjectCount = 0; currentObjectCount < biome.objectCounts.Length; currentObjectCount++)
        {
            GameObject prefab = null;
            try
            {
                prefab = RAKUtilities.getTerrainObjectPrefab(biome.prefabNames[currentObjectCount]);
            }
            catch (Exception e)
            {
                Debug.LogWarning("Unable to get prefab - " + biome.prefabNames[currentObjectCount]);
                continue;
            }
            RAKTerrainObject terrainObject = prefab.GetComponent <RAKTerrainObject>();
            terrainObject.UpdatePropertiesBasedOnName(prefab.name);
            int        maxTries        = terrainObject.numberOfTimesToSearchForLocation;
            float      maxSteepness    = terrainObject.maxSteepness;
            float      minDistFromEdge = terrainObject.minDistFromEdge;
            float      minimiumDistanceBetweenObjects = terrainObject.minimiumDistanceBetweenObjects;
            float      heightOffset = terrainObject.heightOffset;
            MeshFilter meshFilter = terrainObject.GetComponent <MeshFilter>();
            float      detailHeight, detailWidthX, detailWidthZ;
            if (meshFilter == null)
            {
                detailHeight = 1;
                detailWidthX = 1;
                detailWidthZ = 1;
            }
            else
            {
                detailHeight = prefab.GetComponent <MeshFilter>().sharedMesh.bounds.size.y *prefab.transform.localScale.y;
                detailWidthX = prefab.GetComponent <MeshFilter>().sharedMesh.bounds.size.x *prefab.transform.localScale.x;
                detailWidthZ = prefab.GetComponent <MeshFilter>().sharedMesh.bounds.size.z *prefab.transform.localScale.z;
            }
            // Sometimes we need to force a width if the bottom of the object is skinny (trees) //
            if (terrainObject.sizeOverride != Vector3.zero)
            {
                detailWidthX = terrainObject.sizeOverride.x;
                detailWidthZ = terrainObject.sizeOverride.z;
                detailHeight = terrainObject.sizeOverride.y;
            }
            float  widthXNorm  = (detailWidthX / data.heightmapWidth) * 100;
            float  widthZNorm  = (detailWidthZ / data.heightmapHeight) * 100;
            string debugString = "";
            for (int count = 0; count < biome.objectCounts[currentObjectCount]; count++)
            {
                bool  locationValid = false;
                float x             = 0;
                float z             = 0;
                float y             = 0;
                int   tries         = 0;
                while (!locationValid)
                {
                    locationValid = true;
                    tries++;
                    x = UnityEngine.Random.Range(0, width);
                    z = UnityEngine.Random.Range(0, height);
                    float zNormed = (float)z / (float)data.heightmapHeight;
                    float xNormed = (float)x / (float)data.heightmapWidth;
                    y = data.GetInterpolatedHeight(xNormed, zNormed) - detailHeight - heightOffset;
                    if (x < detailWidthX + minDistFromEdge || x > width - detailWidthX - minDistFromEdge)
                    {
                        locationValid = false;
                        debugString   = "Too close to edgeX - " + x;
                        continue;
                    }
                    if (z < detailWidthZ + minDistFromEdge || z > height - detailWidthZ - minDistFromEdge)
                    {
                        locationValid = false;
                        debugString   = "Too close to edgeZ - " + z;
                        continue;
                    }
                    locationValid = true;
                    for (int otherObjectsCount = 0; otherObjectsCount < details.Length; otherObjectsCount++)
                    {
                        if (details[otherObjectsCount] != null)
                        {
                            if (Vector2.Distance(new Vector2(terrain.transform.position.x + x, terrain.transform.position.z + z), new Vector2(details[otherObjectsCount].transform.position.x, details[otherObjectsCount].transform.position.z))
                                < details[otherObjectsCount].GetComponent <RAKTerrainObject>().minimiumDistanceBetweenObjects)
                            {
                                locationValid = false;
                                debugString   = "Too close to other object - " + (Vector2.Distance(new Vector2(terrain.transform.position.x + x, terrain.transform.position.z + z), new Vector2(details[otherObjectsCount].transform.position.x, details[otherObjectsCount].transform.position.z)) +
                                                                                  " - " + (details[otherObjectsCount].GetComponent <RAKTerrainObject>().minimiumDistanceBetweenObjects));
                                break;
                            }
                        }
                    }
                    if (locationValid)
                    {
                        for (int countX = (int)-widthXNorm; countX < widthXNorm; countX++)
                        {
                            for (int countZ = (int)-widthZNorm; countZ < widthZNorm; countZ++)
                            {
                                float steepness = data.GetSteepness((x + countX) / data.alphamapWidth, (z + countZ) / data.alphamapHeight);
                                if (steepness > maxSteepness)
                                {
                                    locationValid = false;
                                    debugString   = "Too steep - " + steepness + " - " + maxSteepness;
                                }
                            }
                        }
                    }
                    if (tries >= maxTries)
                    {
                        Debug.LogWarning(debugString + " - " + terrainObject.name);
                        break;
                    }
                }
                if (locationValid)
                {
                    details[currentDetailCount] = (GameObject)Instantiate(prefab, new Vector3
                                                                              (x + terrain.transform.position.x, y + detailHeight, z + terrain.transform.position.z),
                                                                          Quaternion.identity);//, 3);
                    details[currentDetailCount].gameObject.name = prefab.name;
                    details[currentDetailCount].transform.SetParent(terrain.transform);
                    Debug.LogWarning("Instantiated - " + details[currentDetailCount].name);
                    //details[currentDetailCount].GetComponent<RAKTerrainObject>().setPrefabObjectName(prefab.name);
                    //details[currentDetailCount].transform.position = new Vector3(x + terrain.transform.position.x, y + detailHeight, z + terrain.transform.position.z);
                    if (!terrainObject.freeRotate)
                    {
                        details[currentDetailCount].transform.rotation = Quaternion.Euler(new Vector3(0, UnityEngine.Random.Range(0, 360), 0));
                    }
                    else
                    {
                        details[currentDetailCount].transform.rotation = Quaternion.Euler(new Vector3(UnityEngine.Random.Range(0, 360),
                                                                                                      UnityEngine.Random.Range(0, 360), UnityEngine.Random.Range(0, 360)));
                    }
                }
                currentDetailCount++;
            }
        }
        if (details != null)
        {
            List <RAKTerrainObject> nonTerrainObjects = new List <RAKTerrainObject>();
            for (int count = 0; count < details.Length; count++)
            {
                if (details[count] != null)
                {
                    RAKTerrainObject terrainObject = details[count].GetComponent <RAKTerrainObject>();
                    nonTerrainObjects.Add(terrainObject);
                }
            }
            rakTerrain.nonTerrainObjects = nonTerrainObjects.ToArray();
        }
    }
Exemple #11
0
 public RAKTree(RAKTerrain terrain)
 {
     this.terrain = terrain;
 }