Esempio n. 1
0
 public static Color[] CreateHeightTextureArray(HeightmapArray inputHeightmap)
 {
     Color[] colorArray = new Color[inputHeightmap.Width * inputHeightmap.Height];
     for (int y = 0; y < inputHeightmap.Height; y++)
     {
         for (int x = 0; x < inputHeightmap.Width; x++)
         {
             float pixelHeight = inputHeightmap.GetHeight(x, y);
             colorArray[y * inputHeightmap.Height + x] = HeightColorTransform.EncodeHeight(pixelHeight);
         }
     }
     return(colorArray);
 }
Esempio n. 2
0
 public static HeightmapArray CreateHeightmapArrayFromTexture(Texture2D texture)
 {
     float[,] array = new float[texture.width, texture.height];
     Color[] allPixels = texture.GetPixels();
     for (int y = 0; y < texture.height; y++)
     {
         for (int x = 0; x < texture.height; x++)
         {
             array[x, y] = HeightColorTransform.DecodeHeight(allPixels[y * texture.height + x]);
         }
     }
     return(new HeightmapArray(array));
 }
Esempio n. 3
0
        public static Texture2D CreateTextureFromHeightmap(HeightmapArray inputHeightmap)
        {
            var inputTexture = new Texture2D(inputHeightmap.Width, inputHeightmap.Height, TextureFormat.RGBA32, false);

            Color[] textureArray = new Color[inputHeightmap.Width * inputHeightmap.Height];
            for (int y = 0; y < inputHeightmap.Height; y++)
            {
                for (int x = 0; x < inputHeightmap.Width; x++)
                {
                    float pixelHeight = inputHeightmap.GetHeight(x, y);
                    textureArray[y * inputHeightmap.Height + x] = //new Color(0.6f, 0.6f, 0.6f, 0.6f);
                                                                  HeightColorTransform.EncodeHeight(pixelHeight);
                }
            }
            inputTexture.SetPixels(textureArray);
            inputTexture.Apply();
            return(inputTexture);
        }
Esempio n. 4
0
        public static Texture BaseCreateHeightTexture(Func <int, int, float> heightResolver)
        {
            var traditionalHeightMap = new Texture2D(241, 241, TextureFormat.ARGB32, true);

            for (int x = 0; x < 241; x++)
            {
                for (int y = 0; y < 241; y++)
                {
                    float height = heightResolver(x, y);
                    traditionalHeightMap.SetPixel(x, y, HeightColorTransform.EncodeHeight(height));
                }
            }
            traditionalHeightMap.Apply(true);

            var transformator = new TerrainTextureFormatTransformator(new CommonExecutorUTProxy());

            return(transformator.EncodedHeightTextureToPlain(new TextureWithSize()
            {
                Texture = traditionalHeightMap,
                Size = new IntVector2(241, 241)
            }));
        }
Esempio n. 5
0
        private void TestHeightNormalization()
        {
            var db = _gameInitializationFields.Retrive <TerrainShapeDbProxy>();

            var translator = GeoCoordsToUnityTranslator.DefaultTranslator;
            //var unityPosition = translator.TranslateToUnity(new GeoCoordinate(49.613, 19.5510));
            var unityPosition = translator.TranslateToUnity(new GeoCoordinate(49.573343, 19.528953));

            var queryArea = new MyRectangle(unityPosition.x - 10, unityPosition.y - 10, 20, 20);

            var uvdHeight = db.Query(new TerrainDescriptionQuery()
            {
                QueryArea = queryArea,
                RequestedElementDetails = new List <TerrainDescriptionQueryElementDetail>()
                {
                    new TerrainDescriptionQueryElementDetail()
                    {
                        Resolution = TerrainCardinalResolution.MAX_RESOLUTION,
                        Type       = TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY
                    }
                }
            }).Result.GetElementOfType(TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY);

            var uvBase             = uvdHeight.UvBase;
            var plainHeightTexture = uvdHeight.TokenizedElement.DetailElement.Texture;

            var transformator  = new TerrainTextureFormatTransformator(new CommonExecutorUTProxy());
            var encodedTexture = transformator.PlainToEncodedHeightTextureAsync(plainHeightTexture).Result;

            var pixelPoint =
                RectangleUtils.CalculateSubPosition(new MyRectangle(0, 0, 241, 241), uvBase.Center);

            var intCenterPoint = new IntVector2(Mathf.RoundToInt(pixelPoint.x), Mathf.RoundToInt(pixelPoint.y));

            var color  = encodedTexture.GetPixel(intCenterPoint.X, intCenterPoint.Y);
            var height = HeightColorTransform.DecodeHeight(color);

            Debug.Log("Normalized Height is: " + height);
        }
Esempio n. 6
0
        public DebugSlopedTerrainShapeDb(UTTextureRendererProxy textureRenderer)
        {
            var size             = new IntVector2(241, 241);
            var tex              = new Texture2D(size.X, size.Y, TextureFormat.ARGB32, true, true);
            var encodedHeightTex = new Texture2D(size.X, size.Y, TextureFormat.ARGB32, true, true);

            for (int x = 0; x < size.X; x++)
            {
                for (int y = 0; y < size.Y; y++)
                {
                    tex.SetPixel(x, y, new Color(0, 0, 0, 0));

                    //var distanceToCenter = 1 - (Vector2.Distance(new Vector2(120, 120), new Vector2(x, y)) /
                    //                            Mathf.Sqrt(120 * 120)) / 2;
                    //encodedHeightTex.SetPixel(x, y, HeightColorTransform.EncodeHeight(distanceToCenter / 100));

                    //var distanceToCenter = Mathf.Clamp01(
                    //    Mathf.Min(
                    //        Mathf.Abs(y - 100) / 50.0f,
                    //        Mathf.Abs(x - 100) / 50.0f)
                    //        ) / 300;

                    //encodedHeightTex.SetPixel(x, y, HeightColorTransform.EncodeHeight(distanceToCenter));


                    var heightInUnits = HeightDenormalizer.Default.Normalize(5);
                    var encodedHeight = HeightColorTransform.EncodeHeight(heightInUnits);
                    encodedHeightTex.SetPixel(x, y, encodedHeight);
                }
            }
            tex.Apply();
            encodedHeightTex.Apply();

            _blankTexture = new TextureWithSize()
            {
                Texture = tex,
                Size    = size
            };

            var transformator        = new TerrainTextureFormatTransformator(new CommonExecutorUTProxy());
            var encodedHeightTexture = new TextureWithSize()
            {
                Texture = encodedHeightTex,
                Size    = new IntVector2(241, 241)
            };
            var plainTex = transformator.EncodedHeightTextureToPlain(encodedHeightTexture);

            _heightTexture = new TextureWithSize()
            {
                Size    = new IntVector2(241, 241),
                Texture = plainTex
            };

            var pack = new UniformsPack();

            pack.SetTexture("_HeightmapTex", plainTex);
            pack.SetUniform("_HeightMultiplier", 80);
            ConventionalTextureInfo outTextureInfo =
                new ConventionalTextureInfo(size.X, size.Y, TextureFormat.ARGB32, true);

            var renderCoords = new MyRectangle(0, 0, 1, 1);
            TextureRenderingTemplate template = new TextureRenderingTemplate()
            {
                CanMultistep        = false,
                Coords              = renderCoords,
                OutTextureInfo      = outTextureInfo,
                RenderTextureFormat = RenderTextureFormat.ARGB32,
                ShaderName          = "Custom/Terrain/NormalmapGenerator",
                UniformPack         = pack,
                CreateTexture2D     = false
            };
            var outNormalTex = textureRenderer.AddOrder(template).Result;

            _normalTexture = new TextureWithSize()
            {
                Size    = size,
                Texture = outNormalTex
            };
        }