Exemplo n.º 1
0
    void UpdateHeight(TerrainImport terrainImport)
    {
        // Height
        if (terrainImport.heightSetup.heightMap == null)
        {
            return;
        }
        var format = terrainImport.heightSetup.heightMap.format;


        GameDebug.Log("Update height");

        // TODO: use Texture2D.GetRawTextureData ??


        var terrain     = terrainImport.GetComponent <Terrain>();
        var terrainData = terrain.terrainData;

        terrainData.heightmapResolution = terrainImport.heightSetup.heightMap.width;
        terrainData.size = terrainImport.generalSetup.size;

        var time = stopwatch.Elapsed;
        var data = new float[terrainImport.heightSetup.heightMap.width, terrainImport.heightSetup.heightMap.height];

        GameDebug.Log("Allocated buffer [" + (stopwatch.Elapsed - time).Milliseconds + "ms]");

        ReadChannelFromTexture(terrainImport.heightSetup.heightMap, 0, ref data);

        time = stopwatch.Elapsed;
        terrainData.SetHeights(0, 0, data);
        EditorUtility.SetDirty(terrainData);
        EditorUtility.SetDirty(terrain);
        GameDebug.Log("Set heights [" + (stopwatch.Elapsed - time).Milliseconds + "ms]");
    }
Exemplo n.º 2
0
    //private GameObject terrain;

    public void MapTabClicked()
    {
        string selectedMap = gameObject.transform.GetChild(0).GetComponent <Text>().text;

        //terrain = GameObject.Find("[UI Manager]");
        TerrainImport terrainImport = UIManager.staticUIManager.GetComponent <TerrainImport>();

        terrainImport.MapSelected(selectedMap);
    }
Exemplo n.º 3
0
    public void MapTabClicked()
    {
        string selectedMap = gameObject.transform.GetChild(0).GetComponent <UnityEngine.UI.Text>().text;

        terrain = GameObject.Find("[UI Manager]");
        TerrainImport terrainImport = terrain.GetComponent <TerrainImport>();

        terrainImport.MapSelected(selectedMap);
    }
Exemplo n.º 4
0
    void UpdateAlphaMaps(TerrainImport terrainImport)
    {
        var terrain     = terrainImport.GetComponent <Terrain>();
        var terrainData = terrain.terrainData;

        if (terrainImport.materialSetup.baseMaterial == null)
        {
            Debug.LogError("No baselayer defined!");
            return;
        }

        var width      = 0;
        var height     = 0;
        var sizeSet    = false;
        var layerCount = 1;

        // Get size of data
        for (int i = 0; i < terrainImport.materialSetup.layers.Count; i++)
        {
            var layerData = terrainImport.materialSetup.layers[i].data;
            if (layerData != null)
            {
                width  = layerData.width;
                height = layerData.height;
                break;
            }
        }



        for (int i = terrainImport.materialSetup.layers.Count - 1; i >= 0; i--)
        {
            var layer = terrainImport.materialSetup.layers[i];

            if (IsLayerValid(layer, width, height, true))
            {
                layerCount++;
            }
        }

        Debug.Log("Updating " + layerCount + " layers. Size:" + width + "," + height);

        var alphaMaps = new float[width, height, layerCount];

        float[,] data = new float[width, height];
        var terrainLayers = new List <TerrainLayer>();

        // Add base layer
        terrainLayers.Add(terrainImport.materialSetup.baseMaterial);
        Clear(alphaMaps, 0);


        for (int i = terrainImport.materialSetup.layers.Count - 1; i >= 0; i--)
        {
            var layer = terrainImport.materialSetup.layers[i];
            if (!IsLayerValid(terrainImport.materialSetup.layers[i], width, height, false))
            {
                continue;
            }

            var layerIndex = terrainLayers.Count;
            terrainLayers.Add(layer.material);
            ReadChannelFromTexture(layer.data, layer.dataChannel, ref data);
            Blend(alphaMaps, layerIndex, data);
        }

        terrainData.terrainLayers      = terrainLayers.ToArray();
        terrainData.alphamapResolution = width;
        terrainData.SetAlphamaps(0, 0, alphaMaps);

        EditorUtility.SetDirty(terrainData);
        EditorUtility.SetDirty(terrain);
    }
Exemplo n.º 5
0
    void UpdatePropLayer(TerrainImport terrainImport, TerrainImport.PropLayer layer)
    {
        var time = stopwatch.Elapsed;

        var terrain     = terrainImport.GetComponent <Terrain>();
        var terrainData = terrain.terrainData;


        if (layer.propSet == null)
        {
            return;
        }

        var instanceList = new List <TreeInstance>(terrainData.treeInstances);


        foreach (var propData in layer.propSet.props)
        {
            sampler.Setup(new float2(terrainData.size.x, terrainData.size.z), Mathf.Min(propData.distanceStart, propData.distanceEnd));

            if (layer.map != null)
            {
                float[,] mapData = new float[layer.map.width, layer.map.height];
                ReadChannelFromTexture(layer.map, 0, ref mapData);
                sampler.MapDesity(ref mapData, propData.minDensity, propData.maxDensity, propData.distanceStart, propData.distanceEnd);
            }

            sampler.Calculate();

            var protoIndex = FindTreeProtoIndex(terrainData, propData.prefab);
            if (protoIndex == -1)
            {
                var protoList = new List <TreePrototype>(terrainData.treePrototypes);
                protoIndex = protoList.Count;
                protoList.Add(new TreePrototype
                {
                    prefab = propData.prefab
                });
                terrainData.treePrototypes = protoList.ToArray();
                EditorUtility.SetDirty(terrainData);
                EditorUtility.SetDirty(terrain);
            }


            for (int i = 0; i < sampler.propData.Count; i++)
            {
                var prop     = sampler.propData[i];
                var gridData = sampler.propGrid[prop.gridIndex];

                var pos3D    = new Vector3(prop.pos.x, 0, prop.pos.y);
                var worldPos = terrainImport.transform.TransformPoint(pos3D);

                var height = terrain.SampleHeight(worldPos);

                var pos = new float3(sampler.propData[i].pos.x / terrainData.size.x, height / terrainData.size.y,
                                     sampler.propData[i].pos.y / terrainData.size.z);

                var densityFactor = 1.0f / (propData.maxDensity - propData.minDensity);
                var scaleRange    = propData.scaleEnd - propData.scaleStart;
                var scale         = propData.scaleStart + (gridData.density - propData.minDensity) * densityFactor * scaleRange;

                scale = scale + Random.Range(0, scale * propData.scaleRandomFraction);

                instanceList.Add(new TreeInstance
                {
                    prototypeIndex = protoIndex,
                    widthScale     = scale,
                    heightScale    = scale,
                    rotation       = Random.Range(0, 360),
                    color          = propData.color,
                    lightmapColor  = propData.lightmapColor,
                    position       = pos
                });
            }
        }



        terrainData.treeInstances = instanceList.ToArray();
        EditorUtility.SetDirty(terrainData);
        EditorUtility.SetDirty(terrain);

        GameDebug.Log("Prop layers applied [" + (stopwatch.Elapsed - time).Milliseconds + "ms]");
    }