Exemplo n.º 1
0
        public void Configure(GStylizedTerrain terrain, int prototypeIndex, MaterialPropertyBlock propertyBlock)
        {
            GGrassPrototype proto = terrain.TerrainData.Foliage.Grasses.Prototypes[prototypeIndex];

            propertyBlock.SetTexture(NOISE_TEX, GRuntimeSettings.Instance.foliageRendering.windNoiseTexture);

            IEnumerator <GWindZone> windZone = GWindZone.ActiveWindZones.GetEnumerator();

            if (windZone.MoveNext())
            {
                GWindZone w = windZone.Current;
                propertyBlock.SetVector(WIND, w.GetWindParams());
            }

            propertyBlock.SetColor(COLOR, proto.Color);
            if (proto.Shape != GGrassShape.DetailObject && proto.Texture != null)
            {
                propertyBlock.SetTexture(MAIN_TEX, proto.Texture);
            }
            propertyBlock.SetFloat(BEND_FACTOR, proto.BendFactor);

            if (terrain.TerrainData.Foliage.EnableInteractiveGrass)
            {
                propertyBlock.SetTexture(VECTOR_FIELD, terrain.GetGrassVectorFieldRenderTexture());
                propertyBlock.SetMatrix(WORLD_TO_NORMALIZED, terrain.GetWorldToNormalizedMatrix());
            }

            float fadeMaxDistance = terrain.TerrainData.Rendering.GrassDistance;
            float fadeMinDistance = Mathf.Clamp(terrain.TerrainData.Rendering.GrassFadeStart, 0f, 0.99f) * fadeMaxDistance;

            propertyBlock.SetFloat(FADE_MIN_DISTANCE, fadeMinDistance);
            propertyBlock.SetFloat(FADE_MAX_DISTANCE, fadeMaxDistance);
        }
Exemplo n.º 2
0
        private void DoImportGrasses()
        {
            if (!ImportGrassInstancesOnly)
            {
                GGrassPrototypeGroup grassesGroup = DesData.Foliage.Grasses;
                if (grassesGroup == null ||
                    grassesGroup == GRuntimeSettings.Instance.foliageDefault.grasses)
                {
                    CreateNewGrassPrototypesGroup = true;
                }

                if (CreateNewGrassPrototypesGroup)
                {
                    grassesGroup = ScriptableObject.CreateInstance <GGrassPrototypeGroup>();

#if UNITY_EDITOR
                    if (!Application.isPlaying)
                    {
                        string path      = AssetDatabase.GetAssetPath(DesData);
                        string directory = Path.GetDirectoryName(path);
                        string filePath  = Path.Combine(directory, string.Format("Grasses_{0}_{1}.asset", DesData.Id, System.DateTime.Now.Ticks));
                        AssetDatabase.CreateAsset(grassesGroup, filePath);
                    }
#endif
                    DesData.Foliage.Grasses = grassesGroup;
                }

                grassesGroup.Prototypes.Clear();

                DetailPrototype[] detailPrototypes = SrcData.detailPrototypes;
                for (int i = 0; i < detailPrototypes.Length; ++i)
                {
                    GGrassPrototype proto = (GGrassPrototype)detailPrototypes[i];
                    grassesGroup.Prototypes.Add(proto);
                }
                GCommon.SetDirty(grassesGroup);
            }

            List <GGrassInstance> grasses = new List <GGrassInstance>();
            int detailResolution          = SrcData.detailResolution;
            int detailLayerCount          = SrcData.detailPrototypes.Length;
            for (int layer = 0; layer < detailLayerCount; ++layer)
            {
                int[,] density = SrcData.GetDetailLayer(0, 0, detailResolution, detailResolution, layer);
                DoImportDetailLayer(layer, density, grasses);
            }

            DesData.Foliage.ClearGrassInstances();
            DesData.Foliage.AddGrassInstances(grasses);
            if (DesTerrain != null)
            {
                DesData.Foliage.SetGrassRegionDirty(GCommon.UnitRect);
                DesTerrain.UpdateGrassPatches(-1, true);
                DesData.Foliage.ClearGrassDirtyRegions();
            }

            DesData.SetDirty(GTerrainData.DirtyFlags.Foliage);
            //GC.Collect();
        }
Exemplo n.º 3
0
        private void Submit(int cellIndex)
        {
            GGrassPatchData data = cellsData[cellIndex];

            if (data == null)
            {
                return;
            }
            GInstancedBatch[] batches = data.instancedBatches;
            if (batches == null)
            {
                return;
            }
            for (int i = 0; i < batches.Length; ++i)
            {
                GInstancedBatch b = batches[i];
                if (b.prototypeIndex >= prototypes.Count)
                {
                    continue;
                }

                GGrassPrototype       proto         = prototypes[b.prototypeIndex];
                MaterialPropertyBlock propertyBlock = propertyBlocks[b.prototypeIndex];

                Mesh baseMesh = baseMeshes[b.prototypeIndex];
                if (baseMesh == null)
                {
                    continue;
                }

                Material material = materials[b.prototypeIndex];
                if (material == null || !material.enableInstancing)
                {
                    continue;
                }

                Graphics.DrawMeshInstanced(
                    baseMesh,
                    0,
                    material,
                    b.transforms,
                    b.instanceCount,
                    propertyBlock,
                    proto.shadowCastingMode,
                    proto.receiveShadow,
                    proto.layer,
                    camera,
                    LightProbeUsage.BlendProbes);
            }
        }
Exemplo n.º 4
0
        private void DoImportDetailLayer(int layerIndex, int[,] density, List <GGrassInstance> grasses)
        {
            GGrassPrototype prototype = DesData.Foliage.Grasses.Prototypes[layerIndex];

            Rand    rand       = new Rand(layerIndex + SrcData.detailResolution);
            int     resolution = SrcData.detailResolution;
            int     d          = 0;
            float   maxOffset  = 1.0f / resolution;
            Vector2 uv         = Vector2.zero;
            Vector3 pos        = Vector3.zero;

            for (int z = 0; z < resolution; ++z)
            {
                for (int x = 0; x < resolution; ++x)
                {
                    d = density[z, x];
                    for (int i = 0; i < d; ++i)
                    {
                        if (rand.NextDouble() > GrassDensity)
                        {
                            continue;
                        }
                        uv.Set(
                            Mathf.InverseLerp(0, resolution - 1, x),
                            Mathf.InverseLerp(0, resolution - 1, z));
                        pos.Set(
                            (float)(uv.x + rand.NextDouble() * maxOffset),
                            0,
                            (float)(uv.y + rand.NextDouble() * maxOffset));


                        GGrassInstance grass = GGrassInstance.Create(layerIndex);
                        grass.Position = pos;
                        grass.Rotation = Quaternion.Euler(0, (float)rand.NextDouble() * 360f, 0);
                        grass.Scale    = Mathf.Lerp(0.5f, 1f, (float)rand.NextDouble()) * Vector3.one;

                        grasses.Add(grass);
                    }
                }
            }

            //GC.Collect();
        }
Exemplo n.º 5
0
        private void ConvertToGrassPrototypeGroup()
        {
            GGrassPrototypeGroup group = ScriptableObject.CreateInstance <GGrassPrototypeGroup>();

            for (int i = 0; i < instance.Prototypes.Count; ++i)
            {
                GameObject prefab = instance.Prototypes[i];
                if (prefab != null)
                {
                    group.Prototypes.Add(GGrassPrototype.Create(prefab));
                }
            }

            string path      = AssetDatabase.GetAssetPath(instance);
            string directory = Path.GetDirectoryName(path);
            string filePath  = Path.Combine(directory, string.Format("{0}_{1}_{2}.asset", instance.name, "DetailObjects", GCommon.GetUniqueID()));

            AssetDatabase.CreateAsset(group, filePath);

            Selection.activeObject = group;
        }