private void InitializeBushObjectsDb(List <VegetationSubjectEntity> smallDb, ForgingVegetationSubjectInstanceContainerProxy forgingContainerProxy) { var repositioner = _veConfiguration.VegetationRepositioner; var supportedSpecies = _veConfiguration.SupportedLeadingBushSpecies; var speciesChanger = new VegetationSpeciesRandomChanger(_veConfiguration.SpeciesChangingList, 661); //todo! var filteredEntities = smallDb .Where(c => supportedSpecies.Contains(c.Detail.SpeciesEnum)) .Select(c => speciesChanger.ChangeSpecies(c)) .Where(c => _veConfiguration.SupportedBushSpecies.Contains(c.Detail.SpeciesEnum)); var quadtree = new Quadtree <VegetationSubjectEntity>(); foreach (var entity in filteredEntities) { quadtree.Insert(MyNetTopologySuiteUtils.ToPointEnvelope(entity.Position2D), entity); } var positionsProvider = new VegetationSubjectsPositionsDatabase(quadtree); var runtimeManagement = new VegetationRuntimeManagement( positionsProvider: positionsProvider, vegetationSubjectsChangesListener: forgingContainerProxy, visibleEntitiesContainer: new VegetationSubjectsVisibleEntitiesContainer(), configuration: _veConfiguration.BushObjectsVegetationRuntimeManagementConfiguration); var vegetationRuntimeManagementProxy = new VegetationRuntimeManagementProxy(runtimeManagement); _ultraUpdatableContainer.AddUpdatableElement(new FieldBasedUltraUpdatable() { StartCameraField = (camera) => { vegetationRuntimeManagementProxy.StartThreading(); var position = camera.Position; vegetationRuntimeManagementProxy.Start(repositioner.InvMove(position)); }, UpdateCameraField = (camera) => { var position = camera.Position; vegetationRuntimeManagementProxy.AddUpdate(repositioner.InvMove(position)); vegetationRuntimeManagementProxy.SynchronicUpdate(repositioner.InvMove(position)); }, }); }
public List <VegetationSubjectEntity> GetEntiesFrom(IGeometry area, VegetationDetailLevel level) { var outPositions = new List <Vector2>(); if (level == VegetationDetailLevel.FULL) { var envelope = area.EnvelopeInternal; var gridPositionsStart = new IntVector2( Mathf.CeilToInt((float)envelope.MinX / _configuration.PositionsGridSize.x), Mathf.CeilToInt((float)envelope.MinY / _configuration.PositionsGridSize.y)); var afterGridingLength = new Vector2( (float)(envelope.MaxX - gridPositionsStart.X * _configuration.PositionsGridSize.x), (float)(envelope.MaxY - gridPositionsStart.Y * _configuration.PositionsGridSize.y)); var gridLength = new IntVector2( //ceil becouse there is point at length 0 !! Mathf.CeilToInt(afterGridingLength.x / _configuration.PositionsGridSize.x), Mathf.CeilToInt(afterGridingLength.y / _configuration.PositionsGridSize.y)); for (int x = 0; x < gridLength.X; x++) { for (int y = 0; y < gridLength.Y; y++) { var position = new Vector2( (0.5f + (gridPositionsStart.X + x)) * _configuration.PositionsGridSize.x, (0.5f + (gridPositionsStart.Y + y)) * _configuration.PositionsGridSize.y); outPositions.Add(position); } } } return(outPositions.Where(c => area.Contains(MyNetTopologySuiteUtils.ToGeometryEnvelope( MyNetTopologySuiteUtils.ToPointEnvelope(c)))).Select(c => new VegetationSubjectEntity( new DesignBodyLevel0Detail() { Pos2D = c, Radius = 0, Size = 0, SpeciesEnum = VegetationSpeciesEnum.Grass2SpotMarker })).ToList()); }
private VegetationSubjectsPositionsDatabase CreateSamplePositionsDatabase() { var tree = new Quadtree <VegetationSubjectEntity>(); for (int x = 0; x < 4000; x += 30) { for (int y = 0; y < 4000; y += 30) { var newEntity = new VegetationSubjectEntity( new DesignBodyLevel0Detail() { Pos2D = new Vector2(x, y), Radius = 0, Size = 0, SpeciesEnum = VegetationSpeciesEnum.Tree1A }); tree.Insert(MyNetTopologySuiteUtils.ToPointEnvelope(newEntity.Position2D), newEntity); } } return(new VegetationSubjectsPositionsDatabase(tree)); }
private async Task <Dictionary <HabitatType, Texture2D> > CreateHabitatTexturesDict( Vector2 startOffset, IntVector2 habitatTextureSize, List <HabitatFieldInTree> habitats) { var habitatTextureTemplatesDict = new Dictionary <HabitatType, MyTextureTemplate>(); var allTypes = habitats.Select(c => c.Field.Type).Distinct(); foreach (var type in allTypes) { //var newTex = new Texture2D(habitatTextureSize.X, habitatTextureSize.Y, TextureFormat.RGB24, false, true); var newTexTemplate = new MyTextureTemplate(habitatTextureSize.X, habitatTextureSize.Y, TextureFormat.ARGB32, false, FilterMode.Bilinear) { wrapMode = TextureWrapMode.Clamp }; habitatTextureTemplatesDict[type] = newTexTemplate; } var enlargedTree = GenerateEnlargedTree(habitats.Select(c => c.Field).ToList()); for (int x = 0; x < habitatTextureSize.X; x++) { for (int y = 0; y < habitatTextureSize.Y; y++) { var centerPoint = new Vector2( (x + 0.5f) * (_configuration.HabitatSamplingUnit), (y + 0.5f) * (_configuration.HabitatSamplingUnit) ) + startOffset; var pointGeometry = MyNetTopologySuiteUtils.ToGeometryEnvelope(MyNetTopologySuiteUtils .ToPointEnvelope(centerPoint)); var marginTouchingHabitats = enlargedTree.QueryWithIntersection(pointGeometry); foreach (var texture in habitatTextureTemplatesDict.Values) { texture.SetPixel(x, y, new Color(0, 0, 0)); } foreach (var habitatType in marginTouchingHabitats.Select(c => c.Type).Distinct()) { var maxIntensityValue = 0f; foreach (var habitat in marginTouchingHabitats.Where(c => c.Type == habitatType)) { if (habitat.StandardHabitatField.Contains(pointGeometry)) { maxIntensityValue = 1; } else { var distance = MyNetTopologySuiteUtils.Distance(pointGeometry, habitat.StandardHabitatField, _configuration.HabitatMargin); maxIntensityValue = Mathf.Max(maxIntensityValue, Mathf.Clamp01(1 - (distance / _configuration.HabitatMargin))); } } habitatTextureTemplatesDict[habitatType].SetPixel(x, y, new Color(maxIntensityValue, 0, 0)); } } } var renderedTextures = await TaskUtils.WhenAll( habitatTextureTemplatesDict.Values .Select(async(c) => await _textureConciever.ConcieveTextureAsync(c))); var outDict = Enumerable.Range(0, renderedTextures.Count) .ToDictionary(i => habitatTextureTemplatesDict.Keys.ToList()[i], i => renderedTextures[i]); return(outDict); }
private void StartTreesRuntimeManagment() { var globalInstancingContainer = _initializationFields.Retrive <GlobalGpuInstancingContainer>(); var representationContainer = new DesignBodyRepresentationContainer(); var instanceBucketsContainer = new DesignBodyInstanceBucketsContainer(globalInstancingContainer); var quadBillboardMesh = GameObject.CreatePrimitive(PrimitiveType.Quad).GetComponent <MeshFilter>().mesh; IDesignBodyRepresentationInstanceCombinationProvider combinationProvider; if (_veConfiguration.Mode == VegetationMode.Legacy) { var gShifter = new GTreeDetailProviderShifter(new DetailProviderRepository(), quadBillboardMesh); var treeFileManager = new TreeFileManager(new TreeFileManagerConfiguration() { WritingTreeCompletedClanDirectory = _veConfiguration.TreeCompletedClanDirectiory }); combinationProvider = new GDesignBodyRepresentationInstanceCombinationProvider(treeFileManager, gShifter); } else { var eVegetationShifter = new EVegetationDetailProviderShifter( _veConfiguration.ShaderNames, new DetailProviderRepository(), quadBillboardMesh, _veConfiguration.ReferencedAssets, _materialCommonPack); combinationProvider = new EVegetationDesignBodyRepresentationInstanceCombinationProvider(new TreePrefabManager(), eVegetationShifter, _veConfiguration.ReferencedAssets); } foreach (var pair in _veConfiguration.ShiftingConfigurations.Where(c => _veConfiguration.SupportedVegetationSpecies.Contains(c.Key))) { var clanRepresentations = combinationProvider.CreateRepresentations(pair.Value, pair.Key); representationContainer.InitializeLists(clanRepresentations); instanceBucketsContainer.InitializeLists(clanRepresentations); } var designBodySpotUpdaterProxy = _initializationFields.Retrive <DesignBodySpotUpdaterProxy>(); var mediatorSpotUpdater = new ListenerCenteredMediatorDesignBodyChangesUpdater(designBodySpotUpdaterProxy); var rootMediator = _initializationFields.Retrive <RootMediatorSpotPositionsUpdater>(); rootMediator.AddListener(mediatorSpotUpdater); var repositioner = _veConfiguration.VegetationRepositioner; var forgingContainerProxy = new ForgingVegetationSubjectInstanceContainerProxy( new ForgingVegetationSubjectInstanceContainer( new DesignBodyPortrayalForger( representationContainer, instanceBucketsContainer, repositioner), mediatorSpotUpdater//teraz napisz tak, zeby info zwrotne se spotupdatera wracalo zgodnie z multithreadingiem (szlo do innego watku!) )); mediatorSpotUpdater.SetTargetChangesListener(new LambdaSpotPositionChangesListener(dict => { forgingContainerProxy.AddSpotModifications(dict); })); _ultraUpdatableContainer.AddOtherThreadProxy(forgingContainerProxy); MyProfiler.BeginSample("Vegetation1: Loading from file"); var baseVegetationList = VegetationDatabaseFileUtils.LoadListFromFiles(_veConfiguration.LoadingVegetationDatabaseDictionaryPath); MyProfiler.EndSample(); if (_veConfiguration.GenerateTrees) { MyProfiler.BeginSample("Vegetation2: pushingToFile"); foreach (var pair in _veConfiguration.PerRankVegetationRuntimeManagementConfigurations) { var rank = pair.Key; var managementConfiguration = pair.Value; var baseRankedDb = baseVegetationList[rank]; var supportedSpecies = _veConfiguration.SupportedTreeSpecies; var filteredEntities = baseRankedDb.Where(c => supportedSpecies.Contains(c.Detail.SpeciesEnum)).ToList(); var stagnantEntities = new List <VegetationSubjectEntity>(); var nonStagnantEntities = new List <VegetationSubjectEntity>(); var nonStagnantVegetationRect = _veConfiguration.NonStagnantVegetationArea; foreach (var entity in filteredEntities) { if (nonStagnantVegetationRect.Contains(entity.Position2D)) { nonStagnantEntities.Add(entity); } else { stagnantEntities.Add(entity); } } var stagnantVegetationRuntimeManagement = new StagnantVegetationRuntimeManagement(forgingContainerProxy, stagnantEntities, _veConfiguration.StagnantVegetationRuntimeManagementConfiguration); var stagnantVegetationRuntimaManagementProxy = new StagnantVegetationRuntimeManagementProxy(stagnantVegetationRuntimeManagement); _ultraUpdatableContainer.AddUpdatableElement(new FieldBasedUltraUpdatable() { StartField = () => { stagnantVegetationRuntimaManagementProxy.StartThreading(); } }); var quadtree = new Quadtree <VegetationSubjectEntity>(); foreach (var entity in nonStagnantEntities) { quadtree.Insert(MyNetTopologySuiteUtils.ToPointEnvelope(entity.Position2D), entity); } var positionsProvider = new VegetationSubjectsPositionsDatabase(quadtree); var runtimeManagement = new VegetationRuntimeManagement( positionsProvider: positionsProvider, vegetationSubjectsChangesListener: forgingContainerProxy, visibleEntitiesContainer: new VegetationSubjectsVisibleEntitiesContainer(), configuration: managementConfiguration); var outerVegetationRuntimeManagementProxy = new VegetationRuntimeManagementProxy(runtimeManagement); _ultraUpdatableContainer.AddUpdatableElement(new FieldBasedUltraUpdatable() { StartCameraField = (camera) => { outerVegetationRuntimeManagementProxy.StartThreading(); var position = camera.Position; outerVegetationRuntimeManagementProxy.Start(repositioner.InvMove(position)); }, UpdateCameraField = (camera) => { var position = camera.Position; outerVegetationRuntimeManagementProxy.AddUpdate(repositioner.InvMove(position)); outerVegetationRuntimeManagementProxy.SynchronicUpdate(repositioner.InvMove(position)); }, }); } MyProfiler.EndSample(); } if (_veConfiguration.GenerateBigBushes) { InitializeBushObjectsDb(baseVegetationList[VegetationLevelRank.Small], forgingContainerProxy); } }