private void StartBushVegetation() { var singleGenerationArea = _veConfiguration.BushSingleGenerationArea; var positionsProvider = new GrassVegetationSubjectsPositionsGenerator( new GrassVegetationSubjectsPositionsGenerator. GrassVegetationSubjectsPositionsGeneratorConfiguration() { PositionsGridSize = singleGenerationArea }); var grassIntensityDbProxy = _initializationFields.Retrive <Grass2IntensityDbProxy>(); OtherThreadExecutingLocation otherThreadExecutingLocation = new OtherThreadExecutingLocation(); var planter = CreateBushGroupsPlanter(otherThreadExecutingLocation); GrassGroupsGrower grassGroupsGrower = new GrassGroupsGrower(planter, grassIntensityDbProxy, _veConfiguration.SupportedGrassTypes, _veConfiguration.GrassPlantingCorrectionRepositioner); Grass2RuntimeManager grass2RuntimeManager = new Grass2RuntimeManager(grassGroupsGrower, new Grass2RuntimeManager.Grass2RuntimeManagerConfiguration() { GroupSize = singleGenerationArea }); var vegetationSubjectsChangesListener = new Grass2RuntimeManagerProxy(grass2RuntimeManager); otherThreadExecutingLocation.SetExecutingTarget(vegetationSubjectsChangesListener); ////// var runtimeManagement = new VegetationRuntimeManagement( positionsProvider: positionsProvider, vegetationSubjectsChangesListener: vegetationSubjectsChangesListener, visibleEntitiesContainer: new VegetationSubjectsVisibleEntitiesContainer(), configuration: _veConfiguration.BushVegetationRuntimeManagementConfiguration); var vegetationRuntimeManagementProxy = new VegetationRuntimeManagementProxy(runtimeManagement); var repositioner = _veConfiguration.VegetationRepositioner; _ultraUpdatableContainer.AddUpdatableElement(new FieldBasedUltraUpdatable() { StartCameraField = (camera) => { vegetationRuntimeManagementProxy.StartThreading(); var newPosition = repositioner.InvMove(camera.Position); vegetationRuntimeManagementProxy.Start(newPosition); }, UpdateCameraField = (camera) => { var newPosition = repositioner.InvMove(camera.Position); vegetationRuntimeManagementProxy.AddUpdate(newPosition); vegetationRuntimeManagementProxy.SynchronicUpdate(newPosition); }, }); _ultraUpdatableContainer.AddOtherThreadProxy(vegetationSubjectsChangesListener); }
private void StartGrassRuntimeManagmentSource() { var positionsGenerator = new GrassVegetationSubjectsPositionsGenerator(_veConfiguration.GrassVegetationSubjectsPositionsGeneratorConfiguration); var grassIntensityDbProxy = _initializationFields.Retrive <Grass2IntensityDbProxy>(); var otherThreadExecutingLocation = new OtherThreadExecutingLocation(); var planter = CreateGrassGroupsPlanter(otherThreadExecutingLocation); var grower = new GrassGroupsGrower(planter, grassIntensityDbProxy, _veConfiguration.SupportedGrassTypes, _veConfiguration.GrassPlantingCorrectionRepositioner); var grass2RuntimeManager = new Grass2RuntimeManager(grower, _veConfiguration.Grass2RuntimeManagerConfiguration); var grass2RuntimeManagerProxy = new Grass2RuntimeManagerProxy(grass2RuntimeManager); otherThreadExecutingLocation.SetExecutingTarget(grass2RuntimeManagerProxy); _ultraUpdatableContainer.AddOtherThreadProxy(grass2RuntimeManagerProxy); var runtimeManagement = new VegetationRuntimeManagement( positionsProvider: positionsGenerator, vegetationSubjectsChangesListener: grass2RuntimeManagerProxy, visibleEntitiesContainer: new VegetationSubjectsVisibleEntitiesContainer(), configuration: _veConfiguration.Grass2VegetationRuntimeManagementConfiguration); var vegetationRuntimeManagementProxy = new VegetationRuntimeManagementProxy(runtimeManagement); var queryRepositioner = _veConfiguration.VegetationRepositioner; _ultraUpdatableContainer.AddUpdatableElement(new FieldBasedUltraUpdatable() { StartCameraField = (camera) => { vegetationRuntimeManagementProxy.StartThreading(); var position = camera.Position; vegetationRuntimeManagementProxy.Start(queryRepositioner.InvMove(position)); }, UpdateCameraField = (camera) => { var position = camera.Position; vegetationRuntimeManagementProxy.AddUpdate(queryRepositioner.InvMove(position)); vegetationRuntimeManagementProxy.SynchronicUpdate(queryRepositioner.InvMove(position)); }, }); }
private GrassGroupsPlanter CreateBushGroupsPlanter(OtherThreadExecutingLocation otherThreadExecutingLocation) { var meshGenerator = new GrassMeshGenerator(); var mesh = meshGenerator.GetGrassBillboardMesh(0, 1); var instancingMaterial = new Material(Shader.Find("Custom/Vegetation/GrassBushBillboard.Instanced")); instancingMaterial.enableInstancing = true; /// CLAN var billboardsFileManger = new Grass2BillboardClanFilesManager(); var clan = billboardsFileManger.Load(_veConfiguration.Grass2BillboardsPath, new IntVector2(256, 256)); var singleToDuo = new Grass2BakingBillboardClanGenerator( _initializationFields.Retrive <ComputeShaderContainerGameObject>(), _initializationFields.Retrive <UnityThreadComputeShaderExecutorObject>()); var bakedClan = singleToDuo.GenerateBakedAsync(clan).Result; /// var commonUniforms = new UniformsPack(); commonUniforms.SetUniform("_BendingStrength", 0.6f); commonUniforms.SetUniform("_WindDirection", Vector4.one); commonUniforms.SetTexture("_DetailTex", bakedClan.DetailTextureArray); commonUniforms.SetTexture("_BladeSeedTex", bakedClan.BladeSeedTextureArray); var instancingContainer = new GpuInstancingVegetationSubjectContainer( new GpuInstancerCommonData(mesh, instancingMaterial, commonUniforms), new GpuInstancingUniformsArrayTemplate(new List <GpuInstancingUniformTemplate>() { new GpuInstancingUniformTemplate("_Color", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_InitialBendingValue", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantBendingStiffness", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantDirection", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_RandSeed", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_ArrayTextureIndex", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_Pointer", GpuInstancingUniformType.Float), }) ); var globalGpuInstancingContainer = _initializationFields.Retrive <GlobalGpuInstancingContainer>(); var bucketId = globalGpuInstancingContainer.CreateBucket(instancingContainer); GrassGroupsContainer grassGroupsContainer = new GrassGroupsContainer(globalGpuInstancingContainer, bucketId); IGrassPositionResolver grassPositionResolver = new PoissonDiskSamplerPositionResolver(_veConfiguration.BushExclusionRadiusRange); GrassDetailInstancer grassDetailInstancer = new GrassDetailInstancer(); DesignBodySpotUpdaterProxy designBodySpotUpdaterProxy = _initializationFields.Retrive <DesignBodySpotUpdaterProxy>(); var mediatorSpotUpdater = new ListenerCenteredMediatorDesignBodyChangesUpdater(designBodySpotUpdaterProxy); var grassGroupsPlanter = new GrassGroupsPlanter( grassDetailInstancer, grassPositionResolver, grassGroupsContainer, mediatorSpotUpdater, new Grass2BushAspectsGenerator(bakedClan), //todo! _veConfiguration.BushTemplatesConfiguration, _veConfiguration.VegetationRepositioner); mediatorSpotUpdater.SetTargetChangesListener(new LambdaSpotPositionChangesListener(null, dict => { otherThreadExecutingLocation.Execute(() => { foreach (var pair in dict) { grassGroupsPlanter.GrassGroupSpotChanged(pair.Key, pair.Value); } return(TaskUtils.EmptyCompleted()); }); })); var rootMediator = _initializationFields.Retrive <RootMediatorSpotPositionsUpdater>(); rootMediator.AddListener(mediatorSpotUpdater); return(grassGroupsPlanter); }
private GrassGroupsPlanter CreateGrassGroupsPlanter(OtherThreadExecutingLocation otherThreadExecutingLocation) { var meshGenerator = new GrassMeshGenerator(); var mesh = meshGenerator.GetGrassBladeMesh(1); MeshGenerationUtils.SetYBoundsToInfinity(mesh); var instancingMaterial = _veConfiguration.ReferencedAssets.GrassMaterial; if (_materialCommonPack != null) { _materialCommonPack.SetToMaterial(instancingMaterial); } var commonUniforms = new UniformsPack(); commonUniforms.SetUniform("_BendingStrength", 0.6f); commonUniforms.SetUniform("_WindDirection", Vector4.one); var gpuInstancerCommonData = new GpuInstancerCommonData(mesh, instancingMaterial, commonUniforms); gpuInstancerCommonData.CastShadows = _veConfiguration.GrassCastShadows; var instancingContainer = new GpuInstancingVegetationSubjectContainer( gpuInstancerCommonData, new GpuInstancingUniformsArrayTemplate(new List <GpuInstancingUniformTemplate>() { new GpuInstancingUniformTemplate("_Color", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_InitialBendingValue", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantBendingStiffness", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_PlantDirection", GpuInstancingUniformType.Vector4), new GpuInstancingUniformTemplate("_RandSeed", GpuInstancingUniformType.Float), new GpuInstancingUniformTemplate("_Pointer", GpuInstancingUniformType.Float), }) ); var globalGpuInstancingContainer = _initializationFields.Retrive <GlobalGpuInstancingContainer>(); var bucketId = globalGpuInstancingContainer.CreateBucket(instancingContainer); var grassGroupsContainer = new GrassGroupsContainer(globalGpuInstancingContainer, bucketId); var grassPositionResolver = new SimpleRandomSamplerPositionResolver(); var grassDetailInstancer = new GrassDetailInstancer(); var designBodySpotUpdaterProxy = _initializationFields.Retrive <DesignBodySpotUpdaterProxy>(); var mediatorSpotUpdater = new ListenerCenteredMediatorDesignBodyChangesUpdater(designBodySpotUpdaterProxy); IGrass2AspectsGenerator grassAspectGenerator; if (_veConfiguration.Mode == VegetationMode.Legacy) { grassAspectGenerator = new LegacyGrass2BladeAspectsGenerator(); } else { grassAspectGenerator = new EVegetationGrass2BladeAspectsGenerator(); } var grassGroupsPlanter = new GrassGroupsPlanter( grassDetailInstancer, grassPositionResolver, grassGroupsContainer, mediatorSpotUpdater, grassAspectGenerator, _veConfiguration.GrassTemplates, _veConfiguration.VegetationRepositioner); mediatorSpotUpdater.SetTargetChangesListener(new LambdaSpotPositionChangesListener(null, dict => { otherThreadExecutingLocation.Execute(() => { foreach (var pair in dict) { grassGroupsPlanter.GrassGroupSpotChanged(pair.Key, pair.Value); } return(TaskUtils.EmptyCompleted()); }); })); var rootMediator = _initializationFields.Retrive <RootMediatorSpotPositionsUpdater>(); rootMediator.AddListener(mediatorSpotUpdater); return(grassGroupsPlanter); }