コード例 #1
0
    public void Load(WorldSerialization blob)
    {
        var terrainSize     = new Vector3(blob.world.size, 1000, blob.world.size);
        var terrainPosition = 0.5f * terrainSize;

        Terrain land  = GameObject.FindGameObjectWithTag("Land").GetComponent <Terrain>();
        Terrain water = GameObject.FindGameObjectWithTag("Water").GetComponent <Terrain>();

        WorldConverter.MapInfo terrains = WorldConverter.worldToTerrain(blob);

        land.terrainData.heightmapResolution = terrains.resolution;
        land.terrainData.size = terrains.size;

        water.terrainData.heightmapResolution = terrains.resolution;
        water.terrainData.size = terrains.size;

        land.terrainData.SetHeights(0, 0, terrains.land.heights);
        water.terrainData.SetHeights(0, 0, terrains.water.heights);


        land.terrainData.alphamapResolution = terrains.resolution;
        land.terrainData.size            = terrains.size;
        land.terrainData.splatPrototypes = getTextures();
        land.terrainData.SetAlphamaps(0, 0, terrains.splatMap);

        GameObject defaultObj = Resources.Load <GameObject>("Prefabs/DefaultPrefab");

        for (int i = 0; i < terrains.prefabData.Length; i++)
        {
            Vector3    pos      = new Vector3(terrains.prefabData[i].position.x, terrains.prefabData[i].position.y, terrains.prefabData[i].position.z);
            Vector3    scale    = new Vector3(terrains.prefabData[i].scale.x, terrains.prefabData[i].scale.y, terrains.prefabData[i].scale.z);
            Quaternion rotation = Quaternion.Euler(new Vector3(terrains.prefabData[i].rotation.x, terrains.prefabData[i].rotation.y, terrains.prefabData[i].rotation.z));

            GameObject g = FileSystem.Load <GameObject>(StringPool.Get((blob.world.prefabs[i].id)));

            GameObject newObject = Instantiate(g, pos + terrainPosition, rotation);
            newObject.transform.localScale = scale;
            PrefabDataHolder pdh = newObject.GetComponent <PrefabDataHolder>();
            if (pdh == null)
            {
                newObject.AddComponent <PrefabDataHolder>();
            }
            pdh.prefabData = terrains.prefabData[i];
        }

        GameObject pathObj = Resources.Load <GameObject>("Paths/Path");

        for (int i = 0; i < terrains.pathData.Length; i++)
        {
            Vector3 averageLocation = Vector3.zero;
            for (int j = 0; j < terrains.pathData[i].nodes.Length; j++)
            {
                averageLocation += terrains.pathData[i].nodes[j];
            }
            averageLocation /= terrains.pathData[i].nodes.Length;
            GameObject newObject = Instantiate(pathObj, averageLocation + terrainPosition, Quaternion.identity);
            newObject.GetComponent <PathDataHolder>().pathData = terrains.pathData[i];
            newObject.GetComponent <PathDataHolder>().offset   = terrainPosition;
        }
    }
コード例 #2
0
    public void Load(WorldSerialization blob)
    {
        if (topology == null)
        {
            topology = GameObject.FindGameObjectWithTag("Topology").GetComponent <TopologyMesh>();
        }

        Debug.Log("Map hash: " + blob.Checksum);
        cleanUpMap();
        var terrainSize     = new Vector3(blob.world.size, 1000, blob.world.size);
        var terrainPosition = 0.5f * terrainSize;

        LandData groundLandData   = GameObject.FindGameObjectWithTag("Land").transform.Find("Ground").GetComponent <LandData>();
        LandData biomeLandData    = GameObject.FindGameObjectWithTag("Land").transform.Find("Biome").GetComponent <LandData>();
        LandData alphaLandData    = GameObject.FindGameObjectWithTag("Land").transform.Find("Alpha").GetComponent <LandData>();
        LandData topologyLandData = GameObject.FindGameObjectWithTag("Land").transform.Find("Topology").GetComponent <LandData>();

        Terrain land  = GameObject.FindGameObjectWithTag("Land").GetComponent <Terrain>();
        Terrain water = GameObject.FindGameObjectWithTag("Water").GetComponent <Terrain>();

        TopologyMesh topography = GameObject.FindGameObjectWithTag("Topology").GetComponent <TopologyMesh>();

        WorldConverter.MapInfo terrains = WorldConverter.worldToTerrain(blob);

        topography.InitMesh(terrains.topology);

        land.terrainData.heightmapResolution = terrains.resolution;
        land.terrainData.size = terrains.size;

        water.terrainData.heightmapResolution = terrains.resolution;
        water.terrainData.size = terrains.size;

        land.terrainData.SetHeights(0, 0, terrains.land.heights);
        water.terrainData.SetHeights(0, 0, terrains.water.heights);

        land.terrainData.alphamapResolution = terrains.resolution;
        //land.terrainData.baseMapResolution = terrains.resolution;
        //land.terrainData.SetDetailResolution(terrains.resolution,8);

        land.terrainData.size = terrains.size;

        groundLandData.setData(terrains.splatMap, "ground");

        biomeLandData.setData(terrains.biomeMap, "biome");

        alphaLandData.setData(terrains.alphaMap, "alpha");

        topologyLandData.setData(topology.getSplatMap((int)topologyLayer), "topology");
        changeLandLayer();

        Transform  prefabsParent = GameObject.FindGameObjectWithTag("Prefabs").transform;
        GameObject defaultObj    = Resources.Load <GameObject>("Prefabs/DefaultPrefab");

        for (int i = 0; i < terrains.prefabData.Length; i++)
        {
            GameObject newObj = spawnPrefab(defaultObj, terrains.prefabData[i], prefabsParent);
            newObj.GetComponent <PrefabDataHolder>().prefabData = terrains.prefabData[i];
        }


        Transform  pathsParent = GameObject.FindGameObjectWithTag("Paths").transform;
        GameObject pathObj     = Resources.Load <GameObject>("Paths/Path");

        for (int i = 0; i < terrains.pathData.Length; i++)
        {
            Vector3 averageLocation = Vector3.zero;
            for (int j = 0; j < terrains.pathData[i].nodes.Length; j++)
            {
                averageLocation += terrains.pathData[i].nodes[j];
            }
            averageLocation /= terrains.pathData[i].nodes.Length;
            GameObject newObject = Instantiate(pathObj, averageLocation + terrainPosition, Quaternion.identity, pathsParent);
            newObject.GetComponent <PathDataHolder>().pathData = terrains.pathData[i];
            newObject.GetComponent <PathDataHolder>().offset   = terrainPosition;
        }
    }