private void DoExportSplats()
 {
     DesData.alphamapResolution = SrcData.Shading.SplatControlResolution;
     Texture2D[] alphaMaps = DesData.alphamapTextures;
     for (int i = 0; i < alphaMaps.Length; ++i)
     {
         Texture2D controlMap = SrcData.Shading.GetSplatControl(i);
         GCommon.CopyTexture(controlMap, alphaMaps[i]);
     }
 }
Exemplo n.º 2
0
        private void DoImportSplats()
        {
            if (!ImportSplatControlMapsOnly)
            {
                GSplatPrototypeGroup splatGroup = DesData.Shading.Splats;
                if (splatGroup == null ||
                    splatGroup == GRuntimeSettings.Instance.shadingDefault.splats)
                {
                    CreateNewSplatPrototypesGroup = true;
                }

                if (CreateNewSplatPrototypesGroup)
                {
                    splatGroup = ScriptableObject.CreateInstance <GSplatPrototypeGroup>();

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

                TerrainLayer[]    layers     = SrcData.terrainLayers;
                GSplatPrototype[] prototypes = new GSplatPrototype[layers.Length];
                for (int i = 0; i < layers.Length; ++i)
                {
                    if (layers[i] != null)
                    {
                        GSplatPrototype p = (GSplatPrototype)layers[i];
                        prototypes[i] = p;
                    }
                    else
                    {
                        prototypes[i] = new GSplatPrototype();
                    }
                }
                splatGroup.Prototypes = new List <GSplatPrototype>(prototypes);
                GCommon.SetDirty(splatGroup);
            }

            if (ImportSplatControlMapResolution)
            {
                DesData.Shading.SplatControlResolution = SrcData.alphamapResolution;
            }

            Texture2D[] alphaMaps     = SrcData.alphamapTextures;
            int         alphaMapCount = Mathf.Min(alphaMaps.Length, DesData.Shading.SplatControlMapCount);
            for (int i = 0; i < alphaMapCount; ++i)
            {
                try
                {
                    Texture2D controlMap = DesData.Shading.GetSplatControl(i);
                    //controlMap.SetPixels(alphaMaps[i].GetPixels());
                    //controlMap.Apply();
                    GCommon.CopyTexture(alphaMaps[i], controlMap);
                    controlMap.filterMode = alphaMaps[i].filterMode;
                }
                catch (System.Exception e)
                {
                    Debug.LogError(string.Format("Skip import splat alpha map {0}, error: {1}", alphaMaps[i].name, e.ToString()));
                }
            }

            if (ImportSplatsAsAlbedo)
            {
                DesData.Shading.ConvertSplatsToAlbedo();
            }

            DesData.SetDirty(GTerrainData.DirtyFlags.Shading);
            //GC.Collect();
        }
Exemplo n.º 3
0
        public void Import()
        {
            try
            {
#if UNITY_EDITOR
                GCommonGUI.ProgressBar("Working", "Importing textures...", 1f);
#endif
                if (HeightMap != null || VisibilityMap != null)
                {
                    Color[] oldHeightMapColors = DesData.Geometry.HeightMap.GetPixels();

                    Texture2D hm = null;
                    if (HeightMap != null)
                    {
                        hm = GCommon.CloneTexture(HeightMap);
                    }
                    Texture2D vm = null;
                    if (VisibilityMap != null)
                    {
                        vm = GCommon.CloneTexture(VisibilityMap);
                    }

                    int     desResolution = DesData.Geometry.HeightMapResolution;
                    Color[] newColor      = new Color[desResolution * desResolution];
                    Vector2 uv            = Vector2.zero;
                    Vector2 enc           = Vector2.zero;
                    for (int y = 0; y < desResolution; ++y)
                    {
                        for (int x = 0; x < desResolution; ++x)
                        {
                            uv.Set(
                                Mathf.InverseLerp(0, desResolution, x),
                                Mathf.InverseLerp(0, desResolution, y));

                            Color c = Color.clear;
                            if (hm != null)
                            {
                                enc = GCommon.EncodeTerrainHeight(hm.GetPixelBilinear(uv.x, uv.y).r);
                                c.r = enc.x;
                                c.g = enc.y;
                            }
                            else
                            {
                                c = DesData.Geometry.HeightMap.GetPixelBilinear(uv.x, uv.y);
                            }

                            if (vm != null)
                            {
                                c.a = 1 - vm.GetPixelBilinear(uv.x, uv.y).r;
                            }
                            else
                            {
                                c.a = DesData.Geometry.HeightMap.GetPixelBilinear(uv.x, uv.y).a;
                            }

                            newColor[GUtilities.To1DIndex(x, y, desResolution)] = c;
                        }
                    }

                    DesData.Geometry.HeightMap.SetPixels(newColor);
                    DesData.Geometry.HeightMap.Apply();

                    if (hm != null || vm != null)
                    {
                        Color[]            newHeightMapColors = DesData.Geometry.HeightMap.GetPixels();
                        IEnumerable <Rect> dirtyRects         = GCommon.CompareTerrainTexture(DesData.Geometry.ChunkGridSize, oldHeightMapColors, newHeightMapColors);
                        DesData.Geometry.SetRegionDirty(dirtyRects);
                        DesData.SetDirty(GTerrainData.DirtyFlags.GeometryTimeSliced);

                        if (Terrain != null)
                        {
                            DesData.Foliage.SetTreeRegionDirty(GCommon.UnitRect);
                            DesData.Foliage.SetGrassRegionDirty(GCommon.UnitRect);
                            Terrain.UpdateTreesPosition(true);
                            Terrain.UpdateGrassPatches(-1, true);
                            DesData.Foliage.ClearTreeDirtyRegions();
                            DesData.Foliage.ClearGrassDirtyRegions();
                        }
                    }

                    if (hm != null)
                    {
                        GUtilities.DestroyObject(hm);
                    }
                    if (vm != null)
                    {
                        GUtilities.DestroyObject(vm);
                    }
                }

                if (MaskMap != null)
                {
                    GCommon.CopyTexture(MaskMap, DesData.Mask.MaskMap);
                }
                if (AlbedoMap != null)
                {
                    GCommon.CopyTexture(AlbedoMap, DesData.Shading.AlbedoMap);
                    DesData.SetDirty(GTerrainData.DirtyFlags.Shading);
                }
                if (MetallicMap != null)
                {
                    GCommon.CopyTexture(MetallicMap, DesData.Shading.MetallicMap);
                    DesData.SetDirty(GTerrainData.DirtyFlags.Shading);
                }
                if (SplatControlMaps != null)
                {
                    int count = Mathf.Min(SplatControlMaps.Length, DesData.Shading.SplatControlMapCount);
                    for (int i = 0; i < count; ++i)
                    {
                        if (SplatControlMaps[i] != null)
                        {
                            GCommon.CopyTexture(SplatControlMaps[i], DesData.Shading.GetSplatControl(i));
                        }
                    }
                    DesData.SetDirty(GTerrainData.DirtyFlags.Shading);
                }
            }
            catch (System.Exception e)
            {
                Debug.LogError(e);
            }
            finally
            {
#if UNITY_EDITOR
                GCommonGUI.ClearProgressBar();
#endif
            }
        }