Beispiel #1
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.EncodeFloatRG(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.CompareHeightMap(DesData.Geometry.ChunkGridSize, oldHeightMapColors, heightMapColors);

            DesData.Geometry.SetRegionDirty(dirtyRects);
            DesData.SetDirty(GTerrainData.DirtyFlags.Geometry);
        }
        private void DoImportGeometry()
        {
            if (UseUnityTerrainSize)
            {
                DesData.Geometry.Width  = SrcData.size.x;
                DesData.Geometry.Height = SrcData.size.y;
                DesData.Geometry.Length = SrcData.size.z;
            }

            DesData.Geometry.HeightMapResolution = SrcData.heightmapResolution - 1;
            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   = heightSample[z, x];
                    enc = GCommon.EncodeFloatRG(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.Geometry);
            DesData.Foliage.SetTreeRegionDirty(GCommon.UnitRect);
            DesData.Foliage.SetGrassRegionDirty(GCommon.UnitRect);
            if (DesTerrain != null)
            {
                DesTerrain.UpdateTreesPosition();
                DesTerrain.UpdateGrassPatches();
                DesData.Foliage.ClearTreeDirtyRegions();
                DesData.Foliage.ClearGrassDirtyRegions();
            }

            GC.Collect();
        }
Beispiel #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.EncodeFloatRG(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);
                                c.b = 0;
                            }
                            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)
                    {
                        GUtilities.DestroyObject(hm);
                    }
                    if (vm != null)
                    {
                        GUtilities.DestroyObject(vm);
                    }

                    //Color[] hmColor = hm.GetPixels();
                    //for (int i = 0; i < hmColor.Length; ++i)
                    //{
                    //    Color c = hmColor[i];
                    //    hmColor[i] = new Color(c.r, 0, 0, 0);
                    //}
                    //hm.SetPixels(hmColor);
                    //hm.Apply();
                    //GCommon.CopyTexture(hm, DesData.Geometry.Internal_HeightMap);
                    //GUtilities.DestroyObject(hm);

                    Color[]            newHeightMapColors = DesData.Geometry.HeightMap.GetPixels();
                    IEnumerable <Rect> dirtyRects         = GCommon.CompareHeightMap(DesData.Geometry.ChunkGridSize, oldHeightMapColors, newHeightMapColors);
                    DesData.Geometry.SetRegionDirty(dirtyRects);
                    DesData.SetDirty(GTerrainData.DirtyFlags.Geometry);

                    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 (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
            }
        }