void                                    UpdateMeshDatas(BiomeMap2D biomes)
    {
        List <Vector4> blendInfos = new List <Vector4>();

        for (int x = 0; x < chunkSize; x++)
        {
            for (int z = 0; z < chunkSize; z++)
            {
                Vector4 biomeInfo  = Vector4.zero;
                var     biomePoint = biomes.GetBiomeBlendInfo(x, z);

                biomeInfo.x = biomePoint.firstBiomeId;
                biomeInfo.y = biomePoint.firstBiomeBlendPercent;
                biomeInfo.z = biomePoint.secondBiomeId;
                biomeInfo.w = biomePoint.secondBiomeBlendPercent;
                blendInfos.Add(biomeInfo);
            }
        }
        topDownTerrainMesh.SetUVs(1, blendInfos);
    }
Esempio n. 2
0
        public override void OnNodeProcess()
        {
            if (mergedBiomeTerrain == null)
            {
                mergedBiomeTerrain = new FinalTerrain();
            }

            if (inputBlendedTerrain.biomeData == null)
            {
                Debug.LogError("[PWBiomeMerger] Can't find BiomeData, did you forgot to specify the BiomeGraph in a Biome node");
                return;
            }

            var inputTerrain = inputBlendedTerrain.biomeData.GetSampler(BiomeSamplerName.terrainHeight);

            finalTerrain = inputTerrain.Clone(finalTerrain);

            if (finalTerrain.type == SamplerType.Sampler2D)
            {
                BiomeMap2D biomeMap = inputBlendedTerrain.biomeData.biomeMap;

                (finalTerrain as Sampler2D).Foreach((x, y, val) => {
                    float ret = 0;

                    var biomeInfo = biomeMap.GetBiomeBlendInfo(x, y);
                    foreach (var biome in inputBlendedTerrain.biomes)
                    {
                        var terrain = biome.modifiedTerrain as Sampler2D;

                        if (terrain == null)
                        {
                            PWUtils.LogErrorMax("[PWNodeMerger] can't access to the terrain of the biome " + biome.id + "(" + biome.name + ")", 100);
                            continue;
                        }

                        //TODO: test this blending !
                        for (int i = 0; i < biomeInfo.length; i++)
                        {
                            if (biomeInfo.biomeIds[i] == biome.id)
                            {
                                ret += terrain[x, y] * biomeInfo.biomeBlends[i] / biomeInfo.totalBlend;
                            }
                        }
                    }

                    return(ret);
                });
            }
            else if (finalTerrain.type == SamplerType.Sampler3D)
            {
                Debug.Log("TODO: 3D Terrains");
            }

            mergedBiomeTerrain.biomeData        = inputBlendedTerrain.biomeData;
            mergedBiomeTerrain.mergedTerrain    = finalTerrain;
            mergedBiomeTerrain.materializerType = mainGraphRef.materializerType;

            mergedBiomeTerrain.biomeSurfacesList.Clear();
            foreach (var biome in inputBlendedTerrain.biomes)
            {
                if (mergedBiomeTerrain.biomeSurfacesList.ContainsKey(biome.id))
                {
                    Debug.LogError("[PWBiomeMerger] Duplicate biome in the biome graph: " + biome.name + ", id: " + biome.id);
                }
                mergedBiomeTerrain.biomeSurfacesList[biome.id] = biome.biomeSurfaceGraph;
            }

            update = true;
        }
        static IEnumerable <Texture2D> GenerateBiomeBlendMaps(BlendedBiomeTerrain b)
        {
            List <Texture2D> texs = new List <Texture2D>();

                        #if UNITY_EDITOR
            Stopwatch sw = new Stopwatch();
            sw.Start();
                        #endif

            int        chunkSize    = b.terrain.size;
            BiomeMap2D biomeMap     = b.biomeMap;
            int        textureCount = 0;
            foreach (var kp in b.biomeTree.GetBiomes())
            {
                if (kp.Value.biomeSurfaces != null)
                {
                    foreach (var layer in kp.Value.biomeSurfaces.biomeLayers)
                    {
                        textureCount += layer.slopeMaps.Count;
                    }
                }
            }
            if (blackTexture == null || blackTexture.Length != chunkSize * chunkSize)
            {
                blackTexture = new Color[chunkSize * chunkSize];
            }

            for (int i = 0; i <= textureCount / 4; i++)
            {
                Texture2D tex = new Texture2D(chunkSize, chunkSize, TextureFormat.RGBA32, true, false);
                tex.SetPixels(blackTexture);
                tex.filterMode = FilterMode.Point;
                tex.Apply();
                texs.Add(tex);
            }

            for (int x = 0; x < chunkSize; x++)
            {
                for (int y = 0; y < chunkSize; y++)
                {
                    var bInfo = biomeMap.GetBiomeBlendInfo(x, y);
                    if (bInfo.firstBiomeId == -1 || bInfo.secondBiomeId == -1)
                    {
                        continue;
                    }

                    //TODO: biome blening
                    int   texIndex = bInfo.firstBiomeId / 4;
                    int   texChan  = bInfo.firstBiomeId % 4;
                    Color c        = texs[texIndex].GetPixel(x, y);
                    c[texChan] = bInfo.firstBiomeBlendPercent;
                    texs[texIndex].SetPixel(x, y, c);
                }
            }

            foreach (var tex in texs)
            {
                tex.Apply();
            }

                        #if UNITY_EDITOR
            sw.Stop();
            // Debug.Log(sw.ElapsedMilliseconds + "ms taken to generate blend maps");
                        #endif

            return(texs);
        }