Exemplo n.º 1
0
		public void PrepareGenerators ()
		{
			if (generatorsPrepared) return;

			foreach (TextureInput tin in gens.GeneratorsOfType<TextureInput>()) tin.CheckLoadTexture();
			//more will follow

			generatorsPrepared = true;
		}
Exemplo n.º 2
0
		public void Update () 
		{ 
			//checking if instance already exists and disabling if it is another mm
			if (instance != null && instance != this) { Debug.LogError("MapMagic object already present in scene. Disabling duplicate"); this.enabled = false; return; }
		
			//loading old non-asset data
			if (gens == null)
			{
				if (serializer != null && serializer.entities != null && serializer.entities.Count != 0) 
				{	
					Debug.Log("MapMagic: Loading outdated scene format. Please check node consistency and re-save the scene.");
					LoadOldNonAssetData();
					serializer = null;
				}
				else Debug.Log("MapMagic: Could not find the proper graph data. Please assign it manually.");
			}
			
			//checking gens asset
			if (gens == null) gens = ScriptableObject.CreateInstance<GeneratorsAsset>();

			//finding camera positions
			Vector3[] camPoses = GetCamPoses();
			if (camPoses.Length==0) return;

			//displaying debug range
			if (guiDebug && !isEditor) 
				for (int c=0; c<camPoses.Length; c++) 
					transform.TransformPoint(camPoses[c]).DrawDebug(generateRange, Color.green);

			//deploying terrain matrix
			if (!isEditor && generateInfinite) terrains.Deploy(camPoses, allowMove:true);

			//enabling/disabling, switching lods, starting threads
			terrains.SwitchState(camPoses);

			//transforming cam poses to coords
			Coord[] camCoords = GetCamCoords();
			if (camCoords.Length==0) return;

			//calculating number of running threads and checking a need to prepare generators
			runningThreadsCount = 0;
			bool prepareGenerators = false;
			foreach (Chunk tw in terrains.Objects())
			{
				if (tw.running) runningThreadsCount++;
				if (tw.start) prepareGenerators = true; //if any of chunks started this frame
			}

			//preparing generators
			if (prepareGenerators)
			{
				foreach (TextureInput tin in gens.GeneratorsOfType<TextureInput>()) tin.CheckLoadTexture();
			}

			//updating chunks
			foreach (Chunk tw in terrains.ObjectsFromCoords(camCoords)) tw.Update();
		}
Exemplo n.º 3
0
        public static void Process(CoordRect rect, Chunk.Results results, GeneratorsAsset gens, Chunk.Size terrainSize, Func <float, bool> stop = null)
        {
                        #if VOXELAND
            if (stop != null && stop(0))
            {
                return;
            }
            if (voxeland == null)
            {
                return;
            }

            //finding area by rect offset
            Coord areaCoord          = Coord.PickCell(rect.offset.x, rect.offset.z, voxeland.data.areaSize);
            Voxeland5.Data.Area area = voxeland.data.areas[areaCoord.x, areaCoord.z];

            //clearing grass
            area.ClearGrass();

            //preparing random
            //Noise noise = new Noise(12345); //to switch grass depending on it's opacity

            //processing
            foreach (VoxelandGrassOutput gen in gens.GeneratorsOfType <VoxelandGrassOutput>(onlyEnabled:true, checkBiomes:true))
            {
                //reading output directly
                if (stop != null && stop(0))
                {
                    return;                                        //checking stop before reading output
                }
                //loading biome matrix
                Matrix biomeMask = null;
                if (gen.biome != null)
                {
                    object biomeMaskObj = gen.biome.mask.GetObject(results);
                    if (biomeMaskObj == null)
                    {
                        continue;                                         //adding nothing if biome has no mask
                    }
                    biomeMask = (Matrix)biomeMaskObj;
                    if (biomeMask == null)
                    {
                        continue;
                    }
                    if (biomeMask.IsEmpty())
                    {
                        continue;                                          //optimizing empty biomes
                    }
                }

                //iterating layers
                for (int l = 0; l < gen.layers.Length; l++)
                {
                    Layer layer = gen.layers[l];

                    //loading inputs
                    Matrix src = (Matrix)layer.input.GetObject(results);
                    if (src == null)
                    {
                        continue;
                    }
                    //multiplying with biome mask - in SetGrassLayer

                    //apply
                    //area.SetGrassLayer(src, (byte)l, layer.density, noise:noise, layerNum:l, mask:biomeMask);
                    area.SetGrassLayer(src.rect.offset.x, src.rect.offset.z, src.rect.size.x, src.array, (byte)l, layer.density, l, biomeMask == null? null : biomeMask.array);
                }
            }

            //pushing to apply
            if (stop != null && stop(0))
            {
                return;
            }
            results.apply.CheckAdd(typeof(VoxelandOutput), null, replace: true);
                        #endif
        }
Exemplo n.º 4
0
        public static void Process(CoordRect rect, Chunk.Results results, GeneratorsAsset gens, Chunk.Size terrainSize, Func <float, bool> stop = null)
        {
                        #if VOXELAND
            if (stop != null && stop(0))
            {
                return;
            }
            if (voxeland == null)
            {
                return;
            }

            //TODO get height factor
            int heightFactor = 200;

            //finding area by rect offset
            Coord areaCoord          = Coord.PickCell(rect.offset.x, rect.offset.z, voxeland.data.areaSize);
            Voxeland5.Data.Area area = voxeland.data.areas[areaCoord.x, areaCoord.z];

            //clearing objects
            area.ClearObjects();

            //preparing random
            Noise noise = new Noise(12345);             //to disable biome objects

            //processing
            foreach (VoxelandObjectsOutput gen in gens.GeneratorsOfType <VoxelandObjectsOutput>(onlyEnabled:true, checkBiomes:true))
            {
                //reading output directly
                //Output output = gen.areaOutput;
                if (stop != null && stop(0))
                {
                    return;                                        //checking stop before reading output
                }
                //if (!results.results.ContainsKey(output)) continue;
                //Voxeland5.Data.Area genArea = (Voxeland5.Data.Area)results.results[output];

                //loading biome matrix
                Matrix biomeMask = null;
                if (gen.biome != null)
                {
                    object biomeMaskObj = gen.biome.mask.GetObject(results);
                    if (biomeMaskObj == null)
                    {
                        continue;                                         //adding nothing if biome has no mask
                    }
                    biomeMask = (Matrix)biomeMaskObj;
                    if (biomeMask == null)
                    {
                        continue;
                    }
                    if (biomeMask.IsEmpty())
                    {
                        continue;                                          //optimizing empty biomes
                    }
                }

                //iterating layers
                for (int l = 0; l < gen.layers.Length; l++)
                {
                    Layer layer = gen.layers[l];

                    //loading inputs
                    SpatialHash src = (SpatialHash)layer.input.GetObject(results);
                    if (src == null)
                    {
                        continue;
                    }

                    foreach (SpatialObject obj in src.AllObjs())
                    {
                        int objX = (int)(obj.pos.x + 0.5f);
                        int objZ = (int)(obj.pos.y + 0.5f);

                        //biome masking
                        float biomeVal = 1;
                        if (gen.biome != null)
                        {
                            if (biomeMask == null)
                            {
                                biomeVal = 0;
                            }
                            else
                            {
                                biomeVal = biomeMask[objX, objZ];
                            }
                        }
                        if (biomeVal < noise.Random(objX, objZ))
                        {
                            continue;
                        }

                        //flooring
                        float terrainHeight = layer.relativeHeight? results.heights[objX, objZ] : 0;
                        int   objHeight     = (int)((obj.height + terrainHeight) * heightFactor + 0.5f);

                        //area.AddObject(new CoordDir(objX, objHeight, objZ), (short)l);
                        area.AddObject(objX, objHeight, objZ, 0, (short)l);
                    }
                }
            }

            //pushing to apply
            if (stop != null && stop(0))
            {
                return;
            }
            results.apply.CheckAdd(typeof(VoxelandOutput), null, replace: true);
                        #endif
        }
Exemplo n.º 5
0
        public static void Process(CoordRect rect, Chunk.Results results, GeneratorsAsset gens, Chunk.Size terrainSize, Func <float, bool> stop = null)
        {
                        #if VOXELAND
            if (stop != null && stop(0))
            {
                return;
            }
            if (voxeland == null)
            {
                return;
            }

            //TODO get height factor
            int heightFactor = 200;

            //finding area by rect offset
            Coord areaCoord          = Coord.PickCell(rect.offset.x, rect.offset.z, voxeland.data.areaSize);
            Voxeland5.Data.Area area = voxeland.data.areas[areaCoord.x, areaCoord.z];

            //clearing area
            area.ClearLand();

            //finding a list of areas and their opacities
            List <Voxeland5.Data.Area> areas = new List <Voxeland5.Data.Area>();
            List <Matrix> opacities          = new List <Matrix>();
            foreach (VoxelandOutput gen in gens.GeneratorsOfType <VoxelandOutput>(onlyEnabled:true, checkBiomes:true))
            {
                //reading output directly
                Output output = gen.areaOutput;
                if (stop != null && stop(0))
                {
                    return;                                        //checking stop before reading output
                }
                if (!results.results.ContainsKey(output))
                {
                    continue;
                }
                Voxeland5.Data.Area genArea = (Voxeland5.Data.Area)results.results[output];

                //loading biome matrix
                Matrix biomeMask = null;
                if (gen.biome != null)
                {
                    object biomeMaskObj = gen.biome.mask.GetObject(results);
                    if (biomeMaskObj == null)
                    {
                        continue;                                         //adding nothing if biome has no mask
                    }
                    biomeMask = (Matrix)biomeMaskObj;
                    if (biomeMask == null)
                    {
                        continue;
                    }
                    if (biomeMask.IsEmpty())
                    {
                        continue;                                          //optimizing empty biomes
                    }
                }

                areas.Add(genArea);
                opacities.Add(biomeMask);
            }

            //merge areas using biome mask
            if (areas.Count >= 2)
            //area.MixAreas(areas.ToArray(), opacities.ToArray());
            {
                float[][] opacityArrays = new float[opacities.Count][];
                for (int i = 0; i < opacityArrays.Length; i++)
                {
                    if (opacities[i] != null)
                    {
                        opacityArrays[i] = opacities[i].array;
                    }
                }
                area.MixAreas(areas.ToArray(), rect.offset.x, rect.offset.z, rect.size.x, opacityArrays);
            }
            else
            {
                Voxeland5.Data.Area.CopyLand(areas[0], area);
            }

            //reading heights
            if (results.heights == null || results.heights.rect.size.x != rect.size.x)
            {
                results.heights = new Matrix(rect);
            }
            if (results.heights.rect != rect)
            {
                results.heights.Resize(rect);
            }
            results.heights.Clear();

            for (int x = 0; x < results.heights.rect.size.x; x++)
            {
                for (int z = 0; z < results.heights.rect.size.z; z++)
                {
                    results.heights[x + results.heights.rect.offset.x, z + results.heights.rect.offset.z] = 1f * area.lines[x].columns[z].topLevel / heightFactor;
                }
            }

            //pushing to apply
            if (stop != null && stop(0))
            {
                return;
            }
            results.apply.CheckAdd(typeof(VoxelandOutput), null, replace: true);
                        #endif
        }
Exemplo n.º 6
0
		public override void OnGUI (GeneratorsAsset gens)
		{
			#if __MEGASPLAT__
			layout.fieldSize = 0.5f; 

			//finding texture list from other generators
			if (textureList == null) 
				foreach (MegaSplatOutput gen in gens.GeneratorsOfType<MegaSplatOutput>(onlyEnabled: true, checkBiomes: true))
					if (gen.textureList != null) textureList = gen.textureList;

			//wrong material and settings warnings
			if (MapMagic.instance.showBaseMap) 
			{
				layout.Par(30);
            layout.Label("Show Base Map is turned on in Settings.", rect:layout.Inset(0.8f), helpbox:true);
				if (layout.Button("Fix",rect:layout.Inset(0.2f))) MapMagic.instance.showBaseMap = false;
			}

			if (MapMagic.instance.terrainMaterialType != Terrain.MaterialType.Custom)
			{
				layout.Par(30);
            layout.Label("Material Type is not switched to Custom.", rect:layout.Inset(0.8f), helpbox:true);
				if (layout.Button("Fix",rect:layout.Inset(0.2f))) 
				{
					MapMagic.instance.terrainMaterialType = Terrain.MaterialType.Custom;
					foreach (Chunk tw in MapMagic.instance.chunks.All()) tw.SetSettings();
				}
			}

			if (MapMagic.instance.customTerrainMaterial == null || !MapMagic.instance.customTerrainMaterial.shader.name.Contains("MegaSplat"))
			{
				layout.Par(42);
            layout.Label("No MegaSplat material is assigned as Custom Material in Terrain Settings.", rect:layout.Inset(), helpbox:true);
			}

			if (MapMagic.instance.customTerrainMaterial != null)
			{
				if (!MapMagic.instance.customTerrainMaterial.IsKeywordEnabled("_TERRAIN") || !MapMagic.instance.customTerrainMaterial.HasProperty("_SplatControl"))
				{
					layout.Par(42);
               layout.Label("Material must use a MegaSplat shader set to Terrain.", rect:layout.Inset(), helpbox:true);
				}

				if (MapMagic.instance.customTerrainMaterial.GetTexture("_Diffuse") == null)
				{
					layout.Par(42);
					layout.Label("Material does not have texture arrays assigned, please assign them.", rect:layout.Inset(), helpbox:true);
				}
			}

			/*if (!MapMagic.instance.materialTemplateMode)
			{
				layout.Par(30);
				layout.Label("Material Template Mode is off.", rect:layout.Inset(0.8f), helpbox:true);
				if (layout.Button("Fix",rect:layout.Inset(0.2f))) MapMagic.instance.materialTemplateMode = true;
			}*/

			if (MapMagic.instance.assignCustomTerrainMaterial)
			{
				layout.Par(30);
				layout.Label("Assign Custom Material is turned on.", rect:layout.Inset(0.8f), helpbox:true);
				if (layout.Button("Fix",rect:layout.Inset(0.2f))) 
				{
					MapMagic.instance.assignCustomTerrainMaterial = false;
				}
			}

			if (textureList == null || textureList.clusters == null || textureList.clusters.Length <= 0)
			{
				layout.Par(30);
            layout.Label("Please assign textures and list with clusters below:", rect:layout.Inset(), helpbox:true);

				layout.Field<MegaSplatTextureList>(ref textureList, "TextureList");
				foreach(Input input in Inputs()) input.link = null;
				return;
			}

			//drawing texture list field
			layout.Field<MegaSplatTextureList>(ref textureList, "TextureList");
			
			//setting all of the generators list to this one
			if (layout.change)
				foreach (MegaSplatOutput gen in gens.GeneratorsOfType<MegaSplatOutput>(onlyEnabled: true, checkBiomes: true))
					gen.textureList = textureList;

			//noise field
			layout.Par(5);
			layout.Field<float>(ref clusterNoiseScale, "Noise Scale");
			layout.Toggle(ref smoothFallof, "Smooth Fallof");

			//texture format
			layout.Par(5);
			layout.Toggle(ref MegaSplatOutput.formatARGB, "ARGB (since MS 1.14)");

			//gathering cluster names
			if (clusterNames.Length != textureList.clusters.Length)
				clusterNames = new string[textureList.clusters.Length];
			for (int i=0; i<clusterNames.Length; i++)
				clusterNames[i] = textureList.clusters[i].name;

			//drawing layers
			layout.Par(5);
			layout.Label("Layers:"); //needed to reset label bold style
			layout.margin = 20;
			layout.rightMargin = 20; 

			for (int i=baseLayers.Length-1; i>=0; i--)
			{
				if (baseLayers[i] == null)
					baseLayers[i] = new Layer();
			
				if (layout.DrawWithBackground(OnLayerGUI, active:i==selected, num:i, frameDisabled:false)) selected = i;
			}

			layout.Par(3); layout.Par();
			layout.DrawArrayAdd(ref baseLayers, ref selected, layout.Inset(0.25f));
			layout.DrawArrayRemove(ref baseLayers, ref selected, layout.Inset(0.25f));
			layout.DrawArrayUp(ref baseLayers, ref selected, layout.Inset(0.25f), reverseOrder:true);
			layout.DrawArrayDown(ref baseLayers, ref selected, layout.Inset(0.25f), reverseOrder:true);

			 //drawing effect layers
			layout.Par(5);
		 
			layout.Par(20); 
			wetnessIn.DrawIcon(layout);
			layout.Label("Wetness", layout.Inset());

			layout.Par(20); 
			puddlesIn.DrawIcon(layout);
			layout.Label("Puddles", layout.Inset());

			layout.Par(20); 
			displaceDampenIn.DrawIcon(layout);
			layout.Label("Displace Dampen", layout.Inset());

			#else

			layout.margin = 5;
			layout.rightMargin = 5;

			layout.Par(65);
			layout.Label("MegaSplat is not installed. Please install it from the Asset Store, it's really amazing, you'll like it..\n\t   Jason Booth", rect:layout.Inset(), helpbox:true);

			//What about adding a link to MegaSplat asset store page? Denis

			layout.Par(30);
			layout.Label("Restart Unity if you have just installed it.", rect:layout.Inset(), helpbox:true);

			#endif
		}
Exemplo n.º 7
0
		public static void Process(CoordRect rect, Chunk.Results results, GeneratorsAsset gens, Chunk.Size terrainSize, Func<float,bool> stop = null)
		{
			#if __MEGASPLAT__
			if (stop!=null && stop(0)) return;

			//using the first texture list for all
			MegaSplatTextureList textureList = null;
			bool smoothFallof = false;
         float clusterScale = 0.05f;
			foreach (MegaSplatOutput gen in gens.GeneratorsOfType<MegaSplatOutput>(onlyEnabled: true, checkBiomes: true))
			{
				if (gen.textureList != null) textureList = gen.textureList;
				smoothFallof = gen.smoothFallof;
            clusterScale = gen.clusterNoiseScale;
			}

			//creating color arrays
			MegaSplatData result = new MegaSplatData();

			result.control = new Color[MapMagic.instance.resolution * MapMagic.instance.resolution];
			result.param = new Color[MapMagic.instance.resolution * MapMagic.instance.resolution];
			
			//creating all and special layers/biomes lists
			List<Layer> allLayers = new List<Layer>(); //all layers count = gen num * layers num in each gen (excluding empty biomes, matrices, etc)
			List<Matrix> allMatrices = new List<Matrix>();
			List<Matrix> allBiomeMasks = new List<Matrix>();

			List<Matrix> specialWetnessMatrices = new List<Matrix>(); //special count = number of generators (excluding empty biomes only)
			List<Matrix> specialPuddlesMatrices = new List<Matrix>();
			List<Matrix> specialDampeningMatrices = new List<Matrix>();
			List<Matrix> specialBiomeMasks = new List<Matrix>();

			//filling all layers/biomes
			foreach (MegaSplatOutput gen in gens.GeneratorsOfType<MegaSplatOutput>(onlyEnabled: true, checkBiomes: true))
			{
				gen.textureList = textureList;

				//loading biome matrix
				Matrix biomeMask = null;
				if (gen.biome != null)
				{
					object biomeMaskObj = gen.biome.mask.GetObject(results);
					if (biomeMaskObj == null) continue; //adding nothing if biome has no mask
					biomeMask = (Matrix)biomeMaskObj;
					if (biomeMask == null) continue;
					if (biomeMask.IsEmpty()) continue; //optimizing empty biomes
				}

				for (int i = 0; i < gen.baseLayers.Length; i++)
				{
					//reading output directly
					Output output = gen.baseLayers[i].output;
					if (stop!=null && stop(0)) return; //checking stop before reading output
					if (!results.results.ContainsKey(output)) continue;
					Matrix matrix = (Matrix)results.results[output];
					if (matrix.IsEmpty()) continue;

					if (i >= textureList.clusters.Length)
					{
						Debug.LogError("Cluster out of range");
						continue;
					}

					//adding to lists
					allLayers.Add(gen.baseLayers[i]);
					allMatrices.Add(matrix);
					allBiomeMasks.Add(gen.biome == null ? null : biomeMask);
				}

				//adding special
				object wetnessObj = gen.wetnessIn.GetObject(results);
				specialWetnessMatrices.Add( wetnessObj!=null? (Matrix)wetnessObj : null );

				object puddlesObj = gen.puddlesIn.GetObject(results);
				specialPuddlesMatrices.Add( puddlesObj!=null? (Matrix)puddlesObj : null );

				object dampeingObj = gen.displaceDampenIn.GetObject(results);
				specialDampeningMatrices.Add( dampeingObj!=null? (Matrix)dampeingObj : null );

				specialBiomeMasks.Add(gen.biome == null ? null : biomeMask);
			}

			//if no texture list found in any of generators - returning
			if (textureList == null || allLayers.Count==0) return;

			//processing
			int allLayersCount = allLayers.Count;
			int specialCount = specialWetnessMatrices.Count;
			for (int x = 0; x<rect.size.x; x++)
				for (int z = 0; z<rect.size.z; z++)
				{
					int pos = rect.GetPos(x + rect.offset.x, z + rect.offset.z);

					// doesn't use height, normal, but I'm not sure how to get that here..
					Vector3 worldPos = new Vector3(
						1f * (x+rect.offset.x) / MapMagic.instance.resolution * rect.size.x,
						0,
						1f * (z+rect.offset.z) / MapMagic.instance.resolution * rect.size.z);
					float heightRatio = results.heights!=null? results.heights.array[pos] : 0.5f; //0 is the bottom point, 1 is the maximum top
					Vector3 normal = new Vector3(0,1,0);

					// find highest two layers
					int botIdx = 0;
					int topIdx = 0;
					float botWeight = 0;
					float topWeight = 0;

					for (int i = 0; i<allLayersCount; i++)
					{
						float val = allMatrices[i].array[pos];
						if (allBiomeMasks[i] != null) val *= allBiomeMasks[i].array[pos];

						// really want world position, Normal, and height ratio for brushes, but for now, just use x/z..

						if (val > botWeight)
						{
							topWeight = botWeight;
							topIdx = botIdx;

							botWeight = val;
							botIdx = i;
						}
						else if (val > topWeight)
						{
							topIdx = i;
							topWeight = val;
						}
					}

					//converting layer index to texture index
               topIdx = textureList.clusters[ allLayers[topIdx].index ].GetIndex(worldPos *  clusterScale, normal, heightRatio);
               botIdx = textureList.clusters[ allLayers[botIdx].index ].GetIndex(worldPos * clusterScale, normal, heightRatio);

					//swapping indexes to make topIdx always on top
					if (botIdx > topIdx) 
					{
						int tempIdx = topIdx;
						topIdx = botIdx;
						botIdx = tempIdx;

						float tempWeight = topWeight;
						topWeight = botWeight;
						botWeight = tempWeight;
					}

					//finding blend
					float totalWeight = topWeight + botWeight;	if (totalWeight<0.01f) totalWeight = 0.01f; //Mathf.Max and Clamp are slow
					float blend = botWeight / totalWeight;		if (blend>1) blend = 1;

					//adjusting blend curve
					if (smoothFallof) blend = (Mathf.Sqrt(blend) * (1-blend)) + blend*blend*blend;  //Magic secret formula! Inverse to 3*x^2 - 2*x^3

					//setting color
					result.control[pos] = new Color(botIdx / 255.0f, topIdx / 255.0f, 1.0f - blend, 1.0f);

					//params
					for (int i = 0; i<specialCount; i++)
					{
						float biomeVal = specialBiomeMasks[i]!=null? specialBiomeMasks[i].array[pos] : 1;

						if (specialWetnessMatrices[i]!=null) result.param[pos].b = specialWetnessMatrices[i].array[pos] * biomeVal;
						if (specialPuddlesMatrices[i]!=null) 
						{
							result.param[pos].a = specialPuddlesMatrices[i].array[pos] * biomeVal;
							result.param[pos].r = 0.5f;
							result.param[pos].g = 0.5f;
						}
						if (specialDampeningMatrices[i]!=null) result.control[pos].a = specialDampeningMatrices[i].array[pos] * biomeVal;
					}
						
				}
			
			//pushing to apply
			if (stop!=null && stop(0))
				return;
			results.apply.CheckAdd(typeof(MegaSplatOutput), result, replace: true);
			#endif
		}
        public static void Process(CoordRect rect, Chunk.Results results, GeneratorsAsset gens, Chunk.Size terrainSize, Func <float, bool> stop = null)
        {
                        #if VEGETATION_STUDIO
            if (stop != null && stop(0))
            {
                return;
            }

            //preparing and clearing storage
            if (vetStorComponents == null || vetStorComponents.Count == 0)
            {
                return;                                                                        //vs not used (process runs anyway)
            }
            PersistentVegetationStorage storage = vetStorComponents[rect];

            int cellXCount = Mathf.CeilToInt(terrainSize.dimensions / cellSize);
            int cellZCount = Mathf.CeilToInt(terrainSize.dimensions / cellSize);

            Noise noise = new Noise(12345, permutationCount: 128);            //to pick objects based on biome


            //clearing all of the items
            foreach (VegetationStudioOutput gen in gens.GeneratorsOfType <VegetationStudioOutput>(onlyEnabled:true, checkBiomes:true))
            {
                for (int b = 0; b < gen.layers.Length; b++)
                {
                    string id = gen.package.VegetationInfoList[b].VegetationItemID;
                    storage.RemoveVegetationItemInstances(id, VS_MM_id);
                }
                break;                 //iterating in one generator only - they use the same layers
            }

            if (stop != null && stop(0))
            {
                return;
            }

            //object outputs
            foreach (VegetationStudioOutput gen in gens.GeneratorsOfType <VegetationStudioOutput>(onlyEnabled:true, checkBiomes:true))
            {
                //gen biome mask
                Matrix biomeMask = null;
                if (gen.biome != null)
                {
                    object biomeMaskObj = gen.biome.mask.GetObject(results);
                    if (biomeMaskObj == null)
                    {
                        continue;                                           //adding nothing if biome has no mask
                    }
                    biomeMask = (Matrix)biomeMaskObj;
                    if (biomeMask == null)
                    {
                        continue;
                    }
                    if (biomeMask.IsEmpty())
                    {
                        continue;                                          //optimizing empty biomes
                    }
                }

                //iterating in layers
                for (int b = 0; b < gen.layers.Length; b++)
                {
                    if (stop != null && stop(0))
                    {
                        return;                                            //checking stop before reading output
                    }
                    Layer layer = gen.layers[b];

                    string id = gen.package.VegetationInfoList[b].VegetationItemID;


                    //objects layer
                    if (layer.type == Layer.Type.Object)
                    {
                        //loading objects from input
                        SpatialHash hash = (SpatialHash)gen.layers[b].objInput.GetObject(results);
                        if (hash == null)
                        {
                            continue;
                        }

                        //filling instances (no need to check/add key in multidict)
                        foreach (SpatialObject obj in hash.AllObjs())
                        {
                            //skipping on biome not used
                            float biomeFactor = 0;
                            if (gen.biome == null)
                            {
                                biomeFactor = 1;
                            }
                            else if (biomeMask != null)
                            {
                                biomeFactor = biomeMask.GetInterpolated(obj.pos.x, obj.pos.y);
                            }
                            if (biomeFactor < 0.00001f)
                            {
                                continue;
                            }

                            float rnd;
                            switch (biomeBlendType)
                            {
                            case ObjectOutput.BiomeBlendType.Sharp: rnd = 0.5f; break;

                            case ObjectOutput.BiomeBlendType.AdditiveRandom:
                            case ObjectOutput.BiomeBlendType.NormalizedRandom:
                                rnd = noise.Random((int)obj.pos.x, (int)obj.pos.y);
                                if (biomeFactor > 0.5f)
                                {
                                    rnd = 1 - rnd;                                                           //test
                                }
                                break;

                            case ObjectOutput.BiomeBlendType.Scale: rnd = 0.0f; break;

                            default: rnd = 0.5f; break;
                            }

                            if (biomeFactor < rnd)
                            {
                                continue;
                            }

                            //flooring
                            float terrainHeight = 0;
                            if (layer.relativeHeight && results.heights != null)                             //if checbox enabled and heights exist (at least one height generator is in the graph)
                            {
                                terrainHeight = results.heights.GetInterpolated(obj.pos.x, obj.pos.y);
                            }
                            if (terrainHeight > 1)
                            {
                                terrainHeight = 1;
                            }


                            //terrain-space object position
                            Vector3 position = new Vector3(
                                (obj.pos.x - hash.offset.x) / hash.size * terrainSize.dimensions,
                                (obj.height + terrainHeight) * terrainSize.height,
                                (obj.pos.y - hash.offset.y) / hash.size * terrainSize.dimensions);

                            //cell number
                            int cx = (int)(position.x / cellSize);
                            int cz = (int)(position.z / cellSize);
                            PersistentVegetationCell cell = storage.PersistentVegetationStoragePackage.PersistentVegetationCellList[cz + cx * cellXCount];

                            //rotation + taking terrain normal
                            Quaternion rotation;
                            float      objRotation = layer.rotate ? obj.rotation % 360 : 0;
                            if (layer.takeTerrainNormal)
                            {
                                Vector3 terrainNormal = ObjectOutput.GetTerrainNormal(obj.pos.x, obj.pos.y, results.heights, terrainSize.height, terrainSize.pixelSize);
                                Vector3 sideVector    = new Vector3(Mathf.Sin((obj.rotation + 90) * Mathf.Deg2Rad), 0, Mathf.Cos((obj.rotation + 90) * Mathf.Deg2Rad));
                                Vector3 frontVector   = Vector3.Cross(sideVector, terrainNormal);
                                rotation = Quaternion.LookRotation(frontVector, terrainNormal);
                            }
                            else
                            {
                                rotation = objRotation.EulerToQuat();
                            }

                            //scale + biome scale mode
                            Vector3 scale = layer.scale ? new Vector3(layer.scaleY ? 1 : obj.size, obj.size, layer.scaleY ? 1 : obj.size) : Vector3.one;

                            if (biomeBlendType == ObjectOutput.BiomeBlendType.Scale && gen.biome != null)
                            {
                                float biomeVal = 1;
                                if (biomeMask != null)
                                {
                                    biomeVal = biomeMask[obj.pos];
                                }
                                if (biomeVal < 0.001f)
                                {
                                    continue;                                                     //skip zero-scaled objects
                                }
                                scale *= biomeVal;
                            }

                            //storage.AddVegetationItemInstance(id, position, scale, rotation, layer.applyMeshRotation, VS_MM_id, true);
                            cell.AddVegetationItemInstance(id, position, scale, rotation, VS_MM_id);
                        }

                        if (stop != null && stop(0))
                        {
                            return;
                        }
                    }


                    //map outputs
                    if (layer.type == Layer.Type.Map)
                    {
                        //reading output directly
                        //Output output = gen.layers[b].output;
                        //if (stop!=null && stop(0)) return; //checking stop before reading output
                        //if (!results.results.ContainsKey(output)) continue;
                        //Matrix matrix = (Matrix)results.results[output];

                        //loading from input
                        if (stop != null && stop(0))
                        {
                            return;
                        }
                        Matrix matrix = (Matrix)gen.layers[b].mapInput.GetObject(results);
                        if (matrix == null)
                        {
                            continue;
                        }
                        Matrix heights = results.heights;                         //get heights before the chunk is removed

                        //setting bush by bush using the sample dist
                        float sampleDist = 1f / layer.density;

                        //filling
                        float terrainPosX = 1f * rect.offset.x / terrainSize.resolution * terrainSize.dimensions;
                        float terrainPosZ = 1f * rect.offset.z / terrainSize.resolution * terrainSize.dimensions;



                        for (int cx = 0; cx <= cellXCount - 1; cx++)
                        {
                            for (int cz = 0; cz <= cellZCount - 1; cz++)
                            {
                                Vector3 cellCorner            = new Vector3(terrainPosX + (cellSize * cx), 0, terrainPosZ + (cellSize * cz));
                                PersistentVegetationCell cell = storage.PersistentVegetationStoragePackage.PersistentVegetationCellList[cz + cx * cellXCount];

                                for (float x = 0; x < cellSize; x += sampleDist)
                                {
                                    for (float z = 0; z < cellSize; z += sampleDist)
                                    {
                                        //world position
                                        float wx = cellSize * cx + x;
                                        float wz = cellSize * cz + z;

                                        //randomizing position
                                        wx += noise.Random((int)(wx * 10), (int)(wz * 10), 2) * sampleDist - sampleDist / 2;
                                        wz += noise.Random((int)(wx * 10), (int)(wz * 10), 3) * sampleDist - sampleDist / 2;

                                        //map position
                                        float mx = wx / terrainSize.dimensions * rect.size.x + rect.offset.x;                                    // relative (0-1) position * terrain res
                                        float mz = wz / terrainSize.dimensions * rect.size.z + rect.offset.z;

                                        float val = matrix.GetInterpolated(mx, mz);

                                        float biomeFactor = 0;
                                        if (gen.biome == null)
                                        {
                                            biomeFactor = 1;
                                        }
                                        else if (biomeMask != null)
                                        {
                                            biomeFactor = biomeMask.GetInterpolated(mx, mz);
                                        }

                                        //placing object
                                        float rnd = (noise.Random((int)(wx * 10), (int)(wz * 10)));
                                        if (rnd < val * biomeFactor)
                                        {
                                            float terrainHeight = heights.GetInterpolated(mx, mz) * terrainSize.height;

                                            //rotation + taking terrain normal
                                            Quaternion rotation;
                                            float      rotRnd      = noise.Random((int)(wx * 10), (int)(wz * 10), 1);
                                            float      objRotation = layer.rotate ? rotRnd * 360 : 0;
                                            if (layer.takeTerrainNormal)
                                            {
                                                Vector3 terrainNormal = ObjectOutput.GetTerrainNormal(mx, mz, heights, terrainSize.height, terrainSize.pixelSize);
                                                Vector3 sideVector    = new Vector3(Mathf.Sin((objRotation + 90) * Mathf.Deg2Rad), 0, Mathf.Cos((objRotation + 90) * Mathf.Deg2Rad));
                                                Vector3 frontVector   = Vector3.Cross(sideVector, terrainNormal);
                                                rotation = Quaternion.LookRotation(frontVector, terrainNormal);
                                            }
                                            else
                                            {
                                                rotation = objRotation.EulerToQuat();
                                            }

                                            //scale
                                            float rndScale = noise.Random((int)(wx * 10), (int)(wz * 10), 1);
                                            rndScale = layer.scaleMinMax.x + (layer.scaleMinMax.y - layer.scaleMinMax.x) * rndScale;
                                            Vector3 scale = new Vector3(rndScale, rndScale, rndScale);

                                            //storage.AddVegetationItemInstance(id, new Vector3(wx,terrainHeight,wz), scale, rotation, layer.applyMeshRotation, VS_MM_id, true);
                                            cell.AddVegetationItemInstance(id, new Vector3(wx, terrainHeight, wz), scale, rotation, VS_MM_id);
                                        }
                                    }
                                }

                                if (stop != null && stop(0))
                                {
                                    return;
                                }
                            }
                        }
                    }
                }
            }

            //refreshing billboards
            //calling it from thread ruins all the billboards
            //BillboardSystem billboardSys = billboardComponents[rect];
            //if (billboardSys != null)
            //	 billboardSys.RefreshBillboards();
                        #endif

            //pushing anything to apply
            if (stop != null && stop(0))
            {
                return;
            }
            results.apply.CheckAdd(typeof(VegetationStudioOutput), null, replace: true);
        }
Exemplo n.º 9
0
        public static new void Process(CoordRect rect, Chunk.Results results, GeneratorsAsset gens, Chunk.Size terrainSize, Func <float, bool> stop = null)
        {
                        #if CTS_PRESENT
            if (stop != null && stop(0))
            {
                return;
            }

            //gathering prototypes and matrices lists
            List <int>    indexesList = new List <int>();
            List <float>  opacities   = new List <float>();
            List <Matrix> matrices    = new List <Matrix>();
            List <Matrix> biomeMasks  = new List <Matrix>();

            foreach (CTSOutput gen in gens.GeneratorsOfType <CTSOutput>(onlyEnabled: true, checkBiomes: true))
            {
                //loading biome matrix
                Matrix biomeMask = null;
                if (gen.biome != null)
                {
                    object biomeMaskObj = gen.biome.mask.GetObject(results);
                    if (biomeMaskObj == null)
                    {
                        continue;                                           //adding nothing if biome has no mask
                    }
                    biomeMask = (Matrix)biomeMaskObj;
                    if (biomeMask == null)
                    {
                        continue;
                    }
                    if (biomeMask.IsEmpty())
                    {
                        continue;                                          //optimizing empty biomes
                    }
                }

                for (int i = 0; i < gen.baseLayers.Length; i++)
                {
                    //reading output directly
                    Output output = gen.baseLayers[i].output;
                    if (stop != null && stop(0))
                    {
                        return;                                            //checking stop before reading output
                    }
                    if (!results.results.ContainsKey(output))
                    {
                        continue;
                    }
                    Matrix matrix = (Matrix)results.results[output];
                    matrix.Clamp01();

                    //adding to lists
                    matrices.Add(matrix);
                    biomeMasks.Add(gen.biome == null ? null : biomeMask);
                    indexesList.Add(gen.baseLayers[i].index);
                    opacities.Add(gen.baseLayers[i].opacity);
                }
            }

            //optimizing matrices list if they are not used
            //CTS always use 16 channels, so this optimization is useless (and does not work when layer order changed)
            //for (int i = matrices.Count - 1; i >= 0; i--)
            //	if (opacities[i] < 0.001f || matrices[i].IsEmpty() || (biomeMasks[i] != null && biomeMasks[i].IsEmpty()))
            //	{ indexesList.RemoveAt(i); opacities.RemoveAt(i); matrices.RemoveAt(i); biomeMasks.RemoveAt(i); }

            //creating array
            float[,,] splats3D = new float[terrainSize.resolution, terrainSize.resolution, 16];             //TODO: use max index
            if (matrices.Count == 0)
            {
                results.apply.CheckAdd(typeof(CTSOutput), splats3D, replace: true); return;
            }

            //filling array
            if (stop != null && stop(0))
            {
                return;
            }

            int numLayers = matrices.Count;
            int numPrototypes = splats3D.GetLength(2);
            int maxX = splats3D.GetLength(0); int maxZ = splats3D.GetLength(1);             //MapMagic.instance.resolution should not be used because of possible lods
            //CoordRect rect =  matrices[0].rect;

            float[] values = new float[numPrototypes];             //row, to avoid reading/writing 3d array (it is too slow)

            for (int x = 0; x < maxX; x++)
            {
                for (int z = 0; z < maxZ; z++)
                {
                    int   pos = rect.GetPos(x + rect.offset.x, z + rect.offset.z);
                    float sum = 0;

                    //clearing values
                    for (int i = 0; i < numPrototypes; i++)
                    {
                        values[i] = 0;
                    }

                    //getting values
                    for (int i = 0; i < numLayers; i++)
                    {
                        float val = matrices[i].array[pos];
                        if (biomeMasks[i] != null)
                        {
                            val *= biomeMasks[i].array[pos];                                                //if mask is not assigned biome was ignored, so only main outs with mask==null left here
                        }
                        if (val < 0)
                        {
                            val = 0;
                        }
                        if (val > 1)
                        {
                            val = 1;
                        }
                        sum += val;                         //normalizing: calculating sum
                        values[indexesList[i]] += val;
                    }

                    //setting color
                    for (int i = 0; i < numLayers; i++)
                    {
                        splats3D[z, x, i] = values[i] / sum;
                    }
                }
            }

            //pushing to apply
            if (stop != null && stop(0))
            {
                return;
            }
            results.apply.CheckAdd(typeof(CTSOutput), splats3D, replace: true);
                        #endif
        }