Beispiel #1
0
        private void DoImportColors()
        {
            int resolution = LPTCommon.PAINTER_MAP_RESOLUTION;

            DesData.Shading.AlbedoMapResolution = resolution;
            Color[] colors = new Color[resolution * resolution];
            Vector2 uv     = Vector2.zero;
            Color   c      = Color.clear;

            for (int z = 0; z < resolution; ++z)
            {
                for (int x = 0; x < resolution; ++x)
                {
                    uv.Set(
                        Mathf.InverseLerp(0, resolution - 1, x),
                        Mathf.InverseLerp(0, resolution - 1, z));
                    c = SrcData.SurfaceSettings.GetColor(uv);
                    colors[GUtilities.To1DIndex(x, z, resolution)] = c;
                }
            }

            DesData.Shading.Internal_AlbedoMap.SetPixels(colors);
            DesData.Shading.Internal_AlbedoMap.Apply();
            DesData.SetDirty(GTerrainData.DirtyFlags.Shading);
        }
Beispiel #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();
        }
Beispiel #3
0
        private void ImportRaw16()
        {
            byte[] data          = File.ReadAllBytes(FilePath);
            int    rawResolution = Mathf.FloorToInt(Mathf.Sqrt(data.Length / sizeof(UInt16)));

            if (rawResolution * rawResolution != data.Length / sizeof(UInt16))
            {
                throw new Exception("Error on populate data, RAW file doesn't have squared size or bit depth has been mis-configured!");
            }

            if (UseRawResolution)
            {
                DesData.Geometry.HeightMapResolution = rawResolution;
            }

            float[] heightData = new float[rawResolution * rawResolution];
            for (int i = 0; i < heightData.Length; ++i)
            {
                UInt16 pixelValue = BitConverter.ToUInt16(data, i * 2);
                heightData[i] = pixelValue * 1.0f / UInt16.MaxValue;
            }

            Vector2 uv  = Vector2.zero;
            Vector2 enc = Vector2.zero;
            float   h   = 0;

            Color[] heightMapColors    = DesData.Geometry.HeightMap.GetPixels();
            Color[] oldHeightMapColors = new Color[heightMapColors.Length];
            Array.Copy(heightMapColors, oldHeightMapColors, heightMapColors.Length);

            int heightMapResolution = DesData.Geometry.HeightMapResolution;

            for (int z = 0; z < heightMapResolution; ++z)
            {
                for (int x = 0; x < heightMapResolution; ++x)
                {
                    uv.Set(
                        Mathf.InverseLerp(0, heightMapResolution - 1, x),
                        1 - Mathf.InverseLerp(0, heightMapResolution - 1, z));

                    Color c = heightMapColors[GUtilities.To1DIndex(x, z, heightMapResolution)];
                    h   = GUtilities.GetValueBilinear(heightData, rawResolution, rawResolution, uv);
                    enc = GCommon.EncodeTerrainHeight(h);
                    c.r = enc.x;
                    c.g = enc.y;
                    heightMapColors[GUtilities.To1DIndex(x, z, heightMapResolution)] = c;
                }
            }

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

            IEnumerable <Rect> dirtyRects = GCommon.CompareTerrainTexture(DesData.Geometry.ChunkGridSize, oldHeightMapColors, heightMapColors);

            DesData.Geometry.SetRegionDirty(dirtyRects);
            DesData.SetDirty(GTerrainData.DirtyFlags.Geometry);
        }
Beispiel #4
0
        private void DoImportTrees()
        {
            if (!ImportTreeInstancesOnly)
            {
                GTreePrototypeGroup treeGroup = DesData.Foliage.Trees;
                if (treeGroup == null ||
                    treeGroup == GRuntimeSettings.Instance.foliageDefault.trees)
                {
                    CreateNewTreePrototypesGroup = true;
                }

                if (CreateNewTreePrototypesGroup)
                {
                    treeGroup = ScriptableObject.CreateInstance <GTreePrototypeGroup>();

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

                treeGroup.Prototypes.Clear();
                TreePrototype[] treePrototypes = SrcData.treePrototypes;
                for (int i = 0; i < treePrototypes.Length; ++i)
                {
                    GTreePrototype proto = (GTreePrototype)treePrototypes[i];
                    treeGroup.Prototypes.Add(proto);
                }
                GCommon.SetDirty(treeGroup);
            }

            DesData.Foliage.TreeInstances.Clear();
            TreeInstance[] treeInstances = SrcData.treeInstances;
            for (int i = 0; i < treeInstances.Length; ++i)
            {
                GTreeInstance t = (GTreeInstance)treeInstances[i];
                DesData.Foliage.TreeInstances.Add(t);
            }

            if (DesTerrain != null)
            {
                DesData.Foliage.SetTreeRegionDirty(GCommon.UnitRect);
                DesTerrain.UpdateTreesPosition();
                DesData.Foliage.ClearTreeDirtyRegions();
            }

            DesData.SetDirty(GTerrainData.DirtyFlags.Foliage);
            //GC.Collect();
        }
Beispiel #5
0
        private void DoImportSplats()
        {
            GSplatPrototypeGroup splatGroup = DesData.Shading.Splats;

            if (splatGroup == null ||
                splatGroup == GGriffinSettings.Instance.TerrainDataDefault.Shading.Splats)
            {
                CreateNewSplatPrototypesGroup = true;
            }

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

#if UNITY_EDITOR
                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;
            }

            List <LPTSplatInfo> layers     = SrcData.SurfaceSettings.Splats;
            GSplatPrototype[]   prototypes = new GSplatPrototype[layers.Count];
            for (int i = 0; i < layers.Count; ++i)
            {
                GSplatPrototype p = (GSplatPrototype)layers[i];
                prototypes[i] = p;
            }
            splatGroup.Prototypes = new List <GSplatPrototype>(prototypes);

            int controlResolution = SrcData.SurfaceSettings.ControlResolution;
            DesData.Shading.SplatControlResolution = controlResolution;

            Texture[]     alphaMaps       = SrcData.SurfaceSettings.GetSplatControlTextures();
            int           maxControlCount = Mathf.Min(alphaMaps.Length, DesData.Shading.SplatControlMapCount);
            RenderTexture rt = new RenderTexture(controlResolution, controlResolution, 0, RenderTextureFormat.ARGB32);

            for (int i = 0; i < maxControlCount; ++i)
            {
                Texture2D controlMap = DesData.Shading.Internal_GetSplatControl(i);
                GCommon.CopyToRT(alphaMaps[i], rt);
                GCommon.CopyFromRT(controlMap, rt);
                controlMap.filterMode = alphaMaps[i].filterMode;
            }

            rt.Release();
            GUtilities.DestroyObject(rt);

            DesData.SetDirty(GTerrainData.DirtyFlags.Shading);
            GC.Collect();
        }
Beispiel #6
0
        private void DoImportGeometry()
        {
            if (UseUnityTerrainSize)
            {
                DesData.Geometry.Width  = SrcData.size.x;
                DesData.Geometry.Height = SrcData.size.y;
                DesData.Geometry.Length = SrcData.size.z;
            }
            int hmResolution = SrcData.heightmapResolution - 1;

            DesData.Geometry.HeightMapResolution = hmResolution;

            float[,] heightSample = SrcData.GetHeights(0, 0, DesData.Geometry.HeightMapResolution, DesData.Geometry.HeightMapResolution);
            Color[] heightMapColor = new Color[DesData.Geometry.HeightMapResolution * DesData.Geometry.HeightMapResolution];
            float   h = 0;
            Vector2 enc;

            int length = heightSample.GetLength(0);
            int width  = heightSample.GetLength(1);

            for (int z = 0; z < length; ++z)
            {
                for (int x = 0; x < width; ++x)
                {
                    h   = Mathf.Clamp(heightSample[z, x], 0f, 0.99999f);
                    enc = GCommon.EncodeTerrainHeight(h);
                    heightMapColor[GUtilities.To1DIndex(x, z, width)] = new Color(enc.x, enc.y, 0, 0);
                }
            }

            DesData.Geometry.HeightMap.SetPixels(heightMapColor);
            DesData.Geometry.HeightMap.Apply();
            DesData.Geometry.SetRegionDirty(GCommon.UnitRect);
            DesData.SetDirty(GTerrainData.DirtyFlags.GeometryTimeSliced);

            if (!SkipFoliageSnap)
            {
                if (DesTerrain != null)
                {
                    DesData.Foliage.SetTreeRegionDirty(GCommon.UnitRect);
                    DesData.Foliage.SetGrassRegionDirty(GCommon.UnitRect);
                    DesTerrain.UpdateTreesPosition();
                    DesTerrain.UpdateGrassPatches();
                    DesData.Foliage.ClearTreeDirtyRegions();
                    DesData.Foliage.ClearGrassDirtyRegions();
                }
            }

            //GC.Collect();
        }
Beispiel #7
0
        private void DoImportGeometry()
        {
            if (UsePolarisV1TerrainSize)
            {
                Vector3 size = SrcData.OverallShapeSettings.ScaledSize;
                DesData.Geometry.Width  = size.x;
                DesData.Geometry.Height = size.y;
                DesData.Geometry.Length = size.z;
            }

            SrcData.SurfaceSettings.InitHeightMapColors();
            if (SrcData.SurfaceSettings.UseMedianSmooth)
            {
                for (int i = 0; i < SrcData.SurfaceSettings.MedianSmoothPassCount; ++i)
                {
                    SrcData.SurfaceSettings.SmoothHeightMapColorAdditionalPass();
                }
            }

            int resolution = LPTCommon.PAINTER_MAP_RESOLUTION;

            DesData.Geometry.HeightMapResolution = resolution;
            Color[] heightMapColor = new Color[resolution * resolution];
            Vector2 uv             = Vector2.zero;
            float   h = 0;

            for (int z = 0; z < resolution; ++z)
            {
                for (int x = 0; x < resolution; ++x)
                {
                    uv.Set(
                        Mathf.InverseLerp(0, resolution - 1, x),
                        Mathf.InverseLerp(0, resolution - 1, z));
                    h = SrcData.SurfaceSettings.GetHeightSample(uv);
                    heightMapColor[GUtilities.To1DIndex(x, z, resolution)] = new Color(h, 0, 0, 0);
                }
            }

            DesData.Geometry.Internal_HeightMap.SetPixels(heightMapColor);
            DesData.Geometry.Internal_HeightMap.Apply();
            DesData.Geometry.SetRegionDirty(GCommon.UnitRect);
            DesData.SetDirty(GTerrainData.DirtyFlags.Geometry);
            GC.Collect();
        }
Beispiel #8
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();
        }
        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
            }
        }