コード例 #1
0
        void FillParamRange(BiomeData biomeData)
        {
            builtSamplers.Clear();

            for (int i = 0; i < biomeData.length; i++)
            {
                var dataSampler = biomeData.GetDataSampler(i);

                paramRanges.ranges[i] = new Vector2(dataSampler.dataRef.min, dataSampler.dataRef.max);
                builtSamplers.Add(dataSampler.dataRef);
            }
        }
コード例 #2
0
        public void UpdateIfNeeded(BiomeData biomeData)
        {
            if (blendEnabled != null && biomeData.length == blendEnabled.Length)
            {
                return;
            }

            blendEnabled = new bool[biomeData.length];

            //set all blendEnabled value to true
            for (int i = 0; i < biomeData.length; i++)
            {
                blendEnabled[i] = true;
            }

            int waterIndex = biomeData.GetBiomeIndex(BiomeSamplerName.waterHeight);

            if (waterIndex != -1)
            {
                blendEnabled[waterIndex] = false;
            }
        }
コード例 #3
0
        public bool BuildGraph(BiomeData biomeData)
        {
            ResetGraph();

            BaseNode rootNode = biomeData.biomeSwitchGraphStartPoint;

            Stack <BaseNode> biomeNodes = new Stack <BaseNode>();

            biomeNodes.Push(rootNode);

            BiomeSwitchCell currentCell = null;

            //fill param range dictionary (used to compute weights)
            FillParamRange(biomeData);

            while (biomeNodes.Count > 0)
            {
                BaseNode currentNode = biomeNodes.Pop();
                Type     nodeType    = currentNode.GetType();

                if (nodeType == typeof(NodeBiome))
                {
                    //get all precedent switch data in the order of the tree (start from rootNode)
                    var precedentSwitchDatas = GetPrecedentSwitchDatas(currentNode, rootNode);

                    var biomeNode  = currentNode as NodeBiome;
                    var lastSwitch = precedentSwitchDatas.Last();
                    currentCell = new BiomeSwitchCell();

                    //Set cell and partial biome remaining empty params
                    currentCell.name                   = lastSwitch.name;
                    biomeNode.outputBiome.name         = lastSwitch.name;
                    biomeNode.outputBiome.id           = biomeIdCount++;
                    currentCell.id                     = biomeNode.outputBiome.id;
                    biomeNode.outputBiome.previewColor = lastSwitch.color;
                    currentCell.color                  = lastSwitch.color;
                    currentCell.weight                 = currentCell.GetWeight(paramRanges);

                    //add the partial biome to utility dictionary accessors:
                    partialBiomePerName[currentCell.name] = biomeNode.outputBiome;
                    partialBiomePerId[currentCell.id]     = biomeNode.outputBiome;

                    //load all access condition for this biome switch
                    foreach (var sd in precedentSwitchDatas)
                    {
                        int index = biomeData.GetBiomeIndex(sd.samplerName);
                        var param = currentCell.switchParams.switchParams[index];

                        param.enabled         = true;
                        param.min             = sd.min;
                        param.max             = sd.max;
                        biomeCoverage[index] += (sd.max - sd.min) / (sd.absoluteMax - sd.absoluteMin);
                    }
                }
                else if (nodeType == typeof(NodeBiomeBlender))
                {
                    if (currentCell == null)
                    {
                        throw new InvalidOperationException("[PWBiomeSwitchGraph] idk what happened but this is really bad");
                    }

                    //if the flow reaches the biomeblender everything is OK and add the current cell to the graph
                    cells.Add(currentCell);
                    continue;
                }

                foreach (var outputNode in currentNode.GetOutputNodes())
                {
                    biomeNodes.Push(outputNode);
                }
            }

            //Generate links between all linkable nodes
            BuildLinks();

            if (cells.Count != 0)
            {
                rootCell = cells.First();
            }

            if (!CheckValid())
            {
                return(false);
            }

            isBuilt = true;

            return(true);
        }
コード例 #4
0
        public void FillBiomeMap(BiomeData biomeData, BiomeBlendList blendMatrix, float blendPercent = .15f)
        {
            Sampler terrain           = biomeData.GetSampler(BiomeSamplerName.terrainHeight);
            var     biomeSwitchValues = new BiomeSwitchValues();

            if (!isBuilt)
            {
                Debug.LogError("Biome switch graph is not built, you can't fill the biome map !");
                return;
            }

            //TODO: 3D Biome fill map

            if (biomeData.biomeMap == null || biomeData.biomeMap.NeedResize(terrain.size, terrain.step))
            {
                biomeData.biomeMap = new BiomeMap2D(terrain.size, terrain.step);
            }

            Profiler.BeginSample("FillBiomeMap");

            var blendParams = new BiomeSwitchCellParams();

            //getter for range (faster than dictionary)
            float[] ranges = new float[biomeData.length];
            for (int i = 0; i < biomeData.length; i++)
            {
                ranges[i] = paramRanges.ranges[i].y - paramRanges.ranges[i].x;
            }

            for (int x = 0; x < terrain.size; x++)
            {
                for (int y = 0; y < terrain.size; y++)
                {
                    //fill biomeSwitchValue and blendParams with the current biomeData sampler values
                    for (int i = 0; i < biomeData.length; i++)
                    {
                        biomeSwitchValues[i] = biomeData.biomeSamplers[i].data2D[x, y];
                        var val = biomeSwitchValues[i];
                        var r   = ranges[i];
                        var spc = blendParams.switchParams[i];
                        spc.min     = val - (r * blendPercent);
                        spc.max     = val + (r * blendPercent);
                        spc.enabled = true;
                    }

                    var biomeSwitchCell = FindBiome(biomeSwitchValues);

                    if (biomeSwitchCell == null)
                    {
                        Debug.LogError("Biome can't be found for values: " + biomeSwitchValues);

                        foreach (var c in cells)
                        {
                            Debug.Log("c: " + c.name + " -> " + c.switchParams);
                        }
                        return;
                    }

                    short biomeId = biomeSwitchCell.id;

                    biomeData.ids.Add(biomeId);

                    biomeData.biomeMap.SetPrimaryBiomeId(x, y, biomeId);

                    //add biome that can be blended with the primary biome,
                    if (blendPercent > 0)
                    {
                        foreach (var link in biomeSwitchCell.links)
                        {
                            if (link.Overlaps(blendParams))
                            {
                                float blend = link.ComputeBlend(blendMatrix, paramRanges, biomeSwitchValues, blendPercent);
                                if (blend > 0.001f)
                                {
                                    biomeData.biomeMap.AddBiome(x, y, link.id, blend);
                                }
                            }
                        }
                    }
                }
            }

            Profiler.EndSample();
        }