public void Start(ComputeShaderContainerGameObject computeShaderContainer)
        {
            var commonExecutor       = new CommonExecutorUTProxy();
            var shaderExecutorObject = new UnityThreadComputeShaderExecutorObject();

            var updater =
                new DesignBodySpotUpdater(new DesignBodySpotChangeCalculator(computeShaderContainer,
                                                                             shaderExecutorObject, commonExecutor, HeightDenormalizer.Identity));

            _designBodySpotUpdaterProxy = new DesignBodySpotUpdaterProxy(updater);
            updater.SetChangesListener(new LambdaSpotPositionChangesListener(null, dict =>
            {
                foreach (var pair in dict)
                {
                    _grassGroupsPlanter.GrassGroupSpotChanged(pair.Key, pair.Value);
                }
            }));
            _designBodySpotUpdaterProxy.StartThreading(() => { });


            var meshGenerator = new GrassMeshGenerator();
            var mesh          = meshGenerator.GetGrassBladeMesh(1);

            var instancingMaterial = new Material(Shader.Find("Custom/Vegetation/Grass.Instanced"));

            instancingMaterial.enableInstancing = true;

            var commonUniforms = new UniformsPack();

            commonUniforms.SetUniform("_BendingStrength", 0.0f);
            commonUniforms.SetUniform("_WindDirection", Vector4.one);

            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),
            })
                );

            _globalGpuInstancingContainer = new GlobalGpuInstancingContainer();
            var bucketId = _globalGpuInstancingContainer.CreateBucket(instancingContainer);
            GrassGroupsContainer grassGroupsContainer =
                new GrassGroupsContainer(_globalGpuInstancingContainer, bucketId);

            IGrassPositionResolver grassPositionResolver = new SimpleRandomSamplerPositionResolver();

            GrassDetailInstancer grassDetailInstancer = new GrassDetailInstancer();

            _grassGroupsPlanter = new GrassGroupsPlanter(
                grassDetailInstancer, grassPositionResolver, grassGroupsContainer, _designBodySpotUpdaterProxy,
                new LegacyGrass2BladeAspectsGenerator(),
                GrassDebugUtils.TemplatesDictionary, Repositioner.Identity);
        }
        public IGrassSplat GenerateSplat(MapAreaPosition position, int entityLodLevel)
        {
            List <GrassEntitiesSet> singleEntities = new List <GrassEntitiesSet>();

            for (int i = 0; i < 1000; i++)
            {
                var aGrass = _entityGenerator.CreateSingleGrass();
                _positionProvider.SetPosition(aGrass, position);
                _grassSingleAbstractSettingGenerator.SetSettings(aGrass);
                if (entityLodLevel == 0)
                {
                    aGrass.EntitiesBeforeTransform.ForEach(c => c.AddUniform(GrassShaderUniformName._DbgColor, new Vector4(99.0f, 0f, 0.0f, 1.0f)));
                }
                if (entityLodLevel == 1)
                {
                    aGrass.EntitiesBeforeTransform.ForEach(c => c.AddUniform(GrassShaderUniformName._DbgColor, new Vector4(99.0f, 99.0f, 0.0f, 1.0f)));
                }
                if (entityLodLevel == 2)
                {
                    aGrass.EntitiesBeforeTransform.ForEach(c => c.AddUniform(GrassShaderUniformName._DbgColor, new Vector4(99.0f, 99.0f, 99.0f, 1.0f)));
                }
                if (entityLodLevel == 3)
                {
                    aGrass.EntitiesBeforeTransform.ForEach(c => c.AddUniform(GrassShaderUniformName._DbgColor, new Vector4(0.0f, 99.0f, 0.0f, 1.0f)));
                }
                if (entityLodLevel == 4)
                {
                    aGrass.EntitiesBeforeTransform.ForEach(c => c.AddUniform(GrassShaderUniformName._DbgColor, new Vector4(0.0f, 99.0f, 99.0f, 1.0f)));
                }
                if (entityLodLevel == 5)
                {
                    aGrass.EntitiesBeforeTransform.ForEach(c => c.AddUniform(GrassShaderUniformName._DbgColor, new Vector4(0.0f, 0.0f, 99.0f, 1.0f)));
                }

                singleEntities.Add(aGrass);
            }
            var  bladeTriangleCount = (int)Mathf.Max(Constants.MAX_GRASS_MESH_LEVEL - entityLodLevel, Constants.MIN_GRASS_MESH_LEVEL);
            Mesh mesh = _meshGenerator.GetGrassBladeMesh(bladeTriangleCount);

            return(_grassInstanceContainer.AddGrassEntities(
                       new GrassEntitiesWithMaterials(singleEntities.SelectMany(c => c.EntitiesAfterTransform).ToList(), _material, mesh,
                                                      ContainerType.Instancing)));
        }
Exemplo n.º 3
0
        public void Start()
        {
            var meshGenerator = new GrassMeshGenerator();
            var mesh          = meshGenerator.GetGrassBladeMesh(1);

            var material = new Material(Shader.Find("Custom/Vegetation/Grass"));

            material.SetFloat("_BendingStrength", 0.2f);
            material.SetFloat("_InitialBendingValue", 0.4f);
            material.SetFloat("_PlantBendingStiffness", 0.3f);
            material.SetVector("_WindDirection", Vector4.one);
            material.SetVector("_PlantDirection", Vector4.one);
            material.SetColor("_Color", Color.green);
            material.SetFloat("_RandSeed", 44.4f);

            var go = GameObject.CreatePrimitive(PrimitiveType.Capsule);

            go.GetComponent <MeshFilter>().mesh       = mesh;
            go.GetComponent <MeshRenderer>().material = material;

            var instancingMaterial = new Material(Shader.Find("Custom/Vegetation/Grass.Instanced"));

            instancingMaterial.enableInstancing = true;


            _globalGpuInstancingContainer = new GlobalGpuInstancingContainer();

            var commonUniforms = new UniformsPack();

            commonUniforms.SetUniform("_BendingStrength", 0.8f);
            commonUniforms.SetUniform("_WindDirection", Vector4.one);

            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),
            })
                );
            var bucketId = _globalGpuInstancingContainer.CreateBucket(instancingContainer);

            var uniforms = new UniformsPack();

            uniforms.SetUniform("_InitialBendingValue", 0.4f);
            uniforms.SetUniform("_PlantBendingStiffness", 0.3f);
            uniforms.SetUniform("_PlantDirection", Vector4.one);
            uniforms.SetUniform("_Color", Color.green);
            uniforms.SetUniform("_RandSeed", 44.4f);

            var msw = new MyStopWatch();

            msw.StartSegment("AddingClassic");
            for (int x = 0; x < 300; x++)
            {
                for (int y = 0; y < 300; y++)
                {
                    var myTriplet = new MyTransformTriplet(new Vector3(x, 0, y), Vector3.zero, Vector3.one);
                    _globalGpuInstancingContainer.AddInstance(bucketId, myTriplet.ToLocalToWorldMatrix(), uniforms);
                }
            }

            _globalGpuInstancingContainer.StartThread();
            _globalGpuInstancingContainer.FinishUpdateBatch();
            Debug.Log("It took: " + msw.CollectResults());
        }
Exemplo n.º 4
0
        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);
        }