Пример #1
0
    public static RAKTerrainObjectSaveData createSaveData(RAKTerrainObject terrainObject)
    {
        RAKTerrainObjectSaveData saveData = new RAKTerrainObjectSaveData();

        saveData.prefabObjectIndex = RAKUtilities.GetNonTerrainObjectIndex(terrainObject.prefabObjectName);
        Vector3 position = terrainObject.transform.position;

        saveData.position = new RAKTerrainMaster.RAKTerrainSavedData.RAKVector3(position);
        Quaternion rotation = terrainObject.transform.rotation;

        saveData.rotationEulers = new RAKTerrainMaster.RAKTerrainSavedData.RAKVector3(rotation);
        return(saveData);
    }
Пример #2
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();
    }
Пример #3
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();
        }
    }